mirror of
https://github.com/google/mozc-devices.git
synced 2025-11-09 01:03:26 +03:00
Add mozc double sided version.
Change-Id: I3c50b5c3435c3c01c79bfc14c5d605701a423356 Co-authored-by: Takashi Toyoshima <toyoshim@google.com> Co-authored-by: Shun Ikejima <ikejima@google.com> Reviewed-by: Eliot Courtney <edcourtney@google.com>
This commit is contained in:
14
mozc-doublesided/firmware/Core/Inc/hid.h
Normal file
14
mozc-doublesided/firmware/Core/Inc/hid.h
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#ifndef HID_H_
|
||||
#define HID_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void hid_init(void);
|
||||
void hid_update(const uint8_t* keys);
|
||||
void hid_deinit(void);
|
||||
|
||||
#endif // HID_H_
|
||||
15
mozc-doublesided/firmware/Core/Inc/i2c.h
Normal file
15
mozc-doublesided/firmware/Core/Inc/i2c.h
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#ifndef I2C_H_
|
||||
#define I2C_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void i2c_init(void);
|
||||
bool i2c_is_host(void);
|
||||
void i2c_activate_host(void);
|
||||
void i2c_maybe_listen(void);
|
||||
|
||||
#endif // I2C_H_
|
||||
13
mozc-doublesided/firmware/Core/Inc/led.h
Normal file
13
mozc-doublesided/firmware/Core/Inc/led.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#ifndef LED_H_
|
||||
#define LED_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void led_set(uint8_t index, uint8_t r, uint8_t g, uint8_t b);
|
||||
void led_flush(void);
|
||||
|
||||
#endif // LED_H_
|
||||
11
mozc-doublesided/firmware/Core/Inc/mozc.h
Normal file
11
mozc-doublesided/firmware/Core/Inc/mozc.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#ifndef MOZC_H_
|
||||
#define MOZC_H_
|
||||
|
||||
void mozc_init(void);
|
||||
void mozc_loop(void);
|
||||
|
||||
#endif // MOZC_H_
|
||||
262
mozc-doublesided/firmware/Core/Src/hid.c
Normal file
262
mozc-doublesided/firmware/Core/Src/hid.c
Normal file
@@ -0,0 +1,262 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#include "hid.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "i2c.h"
|
||||
#include "usbd_customhid.h"
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
#define REPORT_SIZE 17
|
||||
#define KEYBOARD_REPORT_ID 1
|
||||
#define KEYBOARD_REPORT_MODIFIER_OFFSET 1
|
||||
#define KEYBOARD_REPORT_OFFSET 3
|
||||
#define UNICODE_REPORT_ID 2
|
||||
#define UNICODE_REPORT_OFFSET 1
|
||||
#define KEY_LINES 26
|
||||
#define KEY_COLUMNS 8
|
||||
|
||||
static bool active = false;
|
||||
static uint8_t prev_keys[KEY_LINES];
|
||||
static uint8_t prev_key_message[REPORT_SIZE];
|
||||
static uint8_t key_message[REPORT_SIZE];
|
||||
static uint8_t key_index = KEYBOARD_REPORT_OFFSET;
|
||||
static uint8_t unicode_message[REPORT_SIZE];
|
||||
static uint8_t unicode_index = UNICODE_REPORT_OFFSET;
|
||||
static uint8_t usage_id[26][8] = {
|
||||
{0x1f, 0x1a, 0x16, 0x1d, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x20, 0x08, 0x07, 0x1b, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x21, 0x15, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x22, 0x17, 0x0a, 0x19, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x23, 0x1c, 0x0b, 0x05, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x24, 0x18, 0x0d, 0x11, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x25, 0x0c, 0x0e, 0x10, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x26, 0x12, 0x0f, 0x36, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x27, 0x13, 0x33, 0x37, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x2d, 0x2f, 0x34, 0x38, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x2e, 0x30, 0x32, 0x87, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x89, 0x2a, 0x28, 0xe6, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3a, 0x2c, 0xe4, 0xe5, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3b, 0x46, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3c, 0x47, 0x4a, 0x4d, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3d, 0x48, 0x4b, 0x4e, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3e, 0x53, 0x50, 0xe3, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x3f, 0x52, 0x51, 0x54, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x40, 0x55, 0x4f, 0x62, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x41, 0x5f, 0x5c, 0x59, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x42, 0x60, 0x5d, 0x5a, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x43, 0x61, 0x5e, 0x5b, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x44, 0x56, 0x57, 0x58, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x45, 0x8b, 0x8a, 0x4c, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x29, 0x2b, 0xe0, 0xe1, 0x39, 0x88, 0x28, 0x2a},
|
||||
{0x1e, 0x14, 0x04, 0xe2, 0x00, 0x00, 0x00, 0x2c},
|
||||
};
|
||||
|
||||
static uint16_t unicode[26][8] = {
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3075, 0x3066, 0x3068, 0x3064},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3042, 0x3044, 0x3057, 0x3055},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3046, 0x3059, 0x306f, 0x305d},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3048, 0x304b, 0x304d, 0x3072},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x304a, 0x3093, 0x304f, 0x3053},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3084, 0x306a, 0x307e, 0x307f},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3086, 0x306b, 0x306e, 0x3082},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3088, 0x3089, 0x308a, 0x306d},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x308f, 0x305b, 0x308c, 0x308b},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x307b, 0x307b, 0x3051, 0x3081},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x3078, 0x309c, 0x3080, 0x308d},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x30FC, 0x0000, 0x0001, 0x0002},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0003, 0x0004, 0x0005, 0x0006},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0007, 0x0008, 0x0009, 0x000a},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x000b, 0x000c, 0x000d, 0x000e},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x000f, 0x0010, 0x0011, 0x0012},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0013, 0x0014, 0x0015, 0x0016},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0017, 0x0018, 0x0019, 0x001a},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x001b, 0x001c, 0x2797, 0x001d},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x001e, 0x001f, 0x0020, 0x0021},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0022, 0x0023, 0x0024, 0x0025},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x0026, 0x0027, 0x0028, 0x0029},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x267e, 0x2796, 0x2795, 0x002a},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x002b, 0x002c, 0x002d, 0x002e},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff},
|
||||
{0xffff, 0xffff, 0xffff, 0xffff, 0x306c, 0x305f, 0x3061, 0xffff},
|
||||
};
|
||||
|
||||
static uint16_t unicode_sequence_00[] = {0xd83d, 0xde48, 0}; // 🙈
|
||||
static uint16_t unicode_sequence_01[] = {0xd83d, 0xde49, 0}; // 🙉
|
||||
static uint16_t unicode_sequence_02[] = {0xd83d, 0xde4a, 0}; // 🙊
|
||||
static uint16_t unicode_sequence_03[] = {0xd83d, 0xde00, 0}; // 😀
|
||||
static uint16_t unicode_sequence_04[] = {0xd83d, 0xde04, 0}; // 😄
|
||||
static uint16_t unicode_sequence_05[] = {0xd83d, 0xde01, 0}; // 😁
|
||||
static uint16_t unicode_sequence_06[] = {0xd83d, 0xde06, 0}; // 😆
|
||||
static uint16_t unicode_sequence_07[] = {0xd83d, 0xde05, 0}; // 😆
|
||||
static uint16_t unicode_sequence_08[] = {0xd83d, 0xde02, 0}; // 😆
|
||||
static uint16_t unicode_sequence_09[] = {0xd83e, 0xdd23, 0}; // 🤣
|
||||
static uint16_t unicode_sequence_0a[] = {0xd83d, 0xde2d, 0}; // 😭
|
||||
static uint16_t unicode_sequence_0b[] = {0xd83d, 0xde09, 0}; // 😉
|
||||
static uint16_t unicode_sequence_0c[] = {0xd83d, 0xde17, 0}; // 😗
|
||||
static uint16_t unicode_sequence_0d[] = {0xd83d, 0xde1a, 0}; // 😚
|
||||
static uint16_t unicode_sequence_0e[] = {0xd83d, 0xde18, 0}; // 😘
|
||||
static uint16_t unicode_sequence_0f[] = {0xd83e, 0xdd70, 0}; // 🥰
|
||||
static uint16_t unicode_sequence_10[] = {0xd83d, 0xde0d, 0}; // 😍
|
||||
static uint16_t unicode_sequence_11[] = {0xd83e, 0xdd29, 0}; // 🤩
|
||||
static uint16_t unicode_sequence_12[] = {0xd83e, 0xdd73, 0}; // 🥳
|
||||
static uint16_t unicode_sequence_13[] = {0xd83e, 0xdee0, 0}; // 🫠
|
||||
static uint16_t unicode_sequence_14[] = {0xd83d, 0xde43, 0}; // 🙃
|
||||
static uint16_t unicode_sequence_15[] = {0xd83e, 0xdd72, 0}; // 🥲
|
||||
static uint16_t unicode_sequence_16[] = {0xd83e, 0xdd79, 0}; // 🥹
|
||||
static uint16_t unicode_sequence_17[] = {0xd83e, 0xdee1, 0}; // 🫡
|
||||
static uint16_t unicode_sequence_18[] = {0xd83e, 0xdd14, 0}; // 🤔
|
||||
static uint16_t unicode_sequence_19[] = {0xd83e, 0xdee2, 0}; // 🫢
|
||||
static uint16_t unicode_sequence_1a[] = {0xd83d, 0xde31, 0}; // 😱
|
||||
static uint16_t unicode_sequence_1b[] = {0xd83d, 0xdd25, 0}; // 🔥
|
||||
static uint16_t unicode_sequence_1c[] = {0x2716, 0xfe0f, 0}; // ✖
|
||||
static uint16_t unicode_sequence_1d[] = {0x0030, 0xfe0f, 0x20e3, 0}; // 0️⃣
|
||||
static uint16_t unicode_sequence_1e[] = {0xd83d, 0xdd1f, 0}; // 🔟
|
||||
static uint16_t unicode_sequence_1f[] = {0x0037, 0xfe0f, 0x20e3, 0}; // 7️⃣
|
||||
static uint16_t unicode_sequence_20[] = {0x0034, 0xfe0f, 0x20e3, 0}; // 4️⃣
|
||||
static uint16_t unicode_sequence_21[] = {0x0031, 0xfe0f, 0x20e3, 0}; // 1️⃣
|
||||
static uint16_t unicode_sequence_22[] = {0x2764, 0xfe0f, 0}; // ❤️
|
||||
static uint16_t unicode_sequence_23[] = {0x0038, 0xfe0f, 0x20e3, 0}; // 8️⃣
|
||||
static uint16_t unicode_sequence_24[] = {0x0035, 0xfe0f, 0x20e3, 0}; // 5️⃣
|
||||
static uint16_t unicode_sequence_25[] = {0x0032, 0xfe0f, 0x20e3, 0}; // 2️⃣
|
||||
static uint16_t unicode_sequence_26[] = {0x2764, 0xfe0f, 0x200d,
|
||||
0xd83e, 0xde79, 0}; // ❤️🩹
|
||||
static uint16_t unicode_sequence_27[] = {0x0039, 0xfe0f, 0x20e3, 0}; // 9️⃣
|
||||
static uint16_t unicode_sequence_28[] = {0x0036, 0xfe0f, 0x20e3, 0}; // 6️⃣
|
||||
static uint16_t unicode_sequence_29[] = {0x0033, 0xfe0f, 0x20e3, 0}; // 3️⃣
|
||||
static uint16_t unicode_sequence_2a[] = {0xd83d, 0xdff0, 0}; // 🟰
|
||||
static uint16_t unicode_sequence_2b[] = {0xd83d, 0xdc4c, 0}; // 👌
|
||||
static uint16_t unicode_sequence_2c[] = {0xd83d, 0xde4f, 0}; // 🙏
|
||||
static uint16_t unicode_sequence_2d[] = {0xd83d, 0xdc4d, 0}; // 👍
|
||||
static uint16_t unicode_sequence_2e[] = {0xd83e, 0xdef5, 0}; // 🫵
|
||||
|
||||
static uint16_t* unicode_sequence[] = {
|
||||
unicode_sequence_00, unicode_sequence_01, unicode_sequence_02,
|
||||
unicode_sequence_03, unicode_sequence_04, unicode_sequence_05,
|
||||
unicode_sequence_06, unicode_sequence_07, unicode_sequence_08,
|
||||
unicode_sequence_09, unicode_sequence_0a, unicode_sequence_0b,
|
||||
unicode_sequence_0c, unicode_sequence_0d, unicode_sequence_0e,
|
||||
unicode_sequence_0f, unicode_sequence_10, unicode_sequence_11,
|
||||
unicode_sequence_12, unicode_sequence_13, unicode_sequence_14,
|
||||
unicode_sequence_15, unicode_sequence_16, unicode_sequence_17,
|
||||
unicode_sequence_18, unicode_sequence_19, unicode_sequence_1a,
|
||||
unicode_sequence_1b, unicode_sequence_1c, unicode_sequence_1d,
|
||||
unicode_sequence_1e, unicode_sequence_1f, unicode_sequence_20,
|
||||
unicode_sequence_21, unicode_sequence_22, unicode_sequence_23,
|
||||
unicode_sequence_24, unicode_sequence_25, unicode_sequence_26,
|
||||
unicode_sequence_27, unicode_sequence_28, unicode_sequence_29,
|
||||
unicode_sequence_2a, unicode_sequence_2b, unicode_sequence_2c,
|
||||
unicode_sequence_2d, unicode_sequence_2e,
|
||||
};
|
||||
|
||||
static void message_reset(void) {
|
||||
for (int i = 0; i < REPORT_SIZE; ++i) {
|
||||
prev_key_message[i] = key_message[i];
|
||||
key_message[i] = 0;
|
||||
unicode_message[i] = 0;
|
||||
}
|
||||
key_index = KEYBOARD_REPORT_OFFSET;
|
||||
unicode_index = UNICODE_REPORT_OFFSET;
|
||||
key_message[0] = KEYBOARD_REPORT_ID;
|
||||
unicode_message[0] = UNICODE_REPORT_ID;
|
||||
}
|
||||
|
||||
static void message_set_phantom_state(void) {
|
||||
for (int i = KEYBOARD_REPORT_OFFSET; i < REPORT_SIZE; ++i) {
|
||||
key_message[i] = 1;
|
||||
}
|
||||
key_index = REPORT_SIZE + 1;
|
||||
for (int i = UNICODE_REPORT_OFFSET; i < REPORT_SIZE; ++i) {
|
||||
unicode_message[i] = 0;
|
||||
}
|
||||
unicode_index = REPORT_SIZE + 1;
|
||||
}
|
||||
|
||||
static void message_push_usage(uint8_t usage) {
|
||||
if (0xe0 <= usage && usage <= 0xe7) {
|
||||
// modifiers
|
||||
key_message[KEYBOARD_REPORT_MODIFIER_OFFSET] |= (1 << (usage & 7));
|
||||
return;
|
||||
}
|
||||
if (key_index == REPORT_SIZE) {
|
||||
message_set_phantom_state();
|
||||
} else if (key_index < REPORT_SIZE) {
|
||||
key_message[key_index++] = usage;
|
||||
}
|
||||
}
|
||||
|
||||
// Experimental to send Unicode events over HID report.
|
||||
// Unicode Page (0x10) is defined in the HID spec, but haven't used for
|
||||
// keyboards and no operating system seems to support the case for now.
|
||||
// You can handle this directly if your application can speak HID directly,
|
||||
// e.g. web sites using WebHID.
|
||||
// Unicode Page assumes the Unicode Standard, Version 1.1 that was the newest
|
||||
// version when the usage page was defined, and 16bit code here is expected
|
||||
// to be UCS-2. However, we may send UTF-16 here as a natural expansion.
|
||||
static void message_push_unicode(uint16_t code) {
|
||||
if (unicode_index == REPORT_SIZE) {
|
||||
message_set_phantom_state();
|
||||
} else if (unicode_index < REPORT_SIZE) {
|
||||
unicode_message[unicode_index++] = code & 0xff;
|
||||
unicode_message[unicode_index++] = code >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
void hid_init(void) {
|
||||
if (!i2c_is_host()) {
|
||||
i2c_activate_host();
|
||||
}
|
||||
active = true;
|
||||
for (int i = 0; i < KEY_LINES; ++i) {
|
||||
prev_keys[i] = 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
void hid_update(const uint8_t* keys) {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
message_reset();
|
||||
for (int i = 0; i < KEY_LINES; ++i) {
|
||||
for (int bit = 0; bit < KEY_COLUMNS; ++bit) {
|
||||
if ((keys[i] & (1 << bit)) == 0) {
|
||||
uint8_t usage = usage_id[i][bit];
|
||||
if (usage) {
|
||||
message_push_usage(usage);
|
||||
} else if (prev_keys[i] & (1 << bit)) {
|
||||
// Unicode sequence will be sent only on pushing edge.
|
||||
uint16_t code = unicode[i][bit];
|
||||
if (code < 0x0100) {
|
||||
for (int sequence = 0; unicode_sequence[code][sequence];
|
||||
++sequence) {
|
||||
message_push_unicode(unicode_sequence[code][sequence]);
|
||||
}
|
||||
} else if (code != 0xffff) {
|
||||
message_push_unicode(code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prev_keys[i] = keys[i];
|
||||
}
|
||||
bool changed = false;
|
||||
for (int i = 0; i < REPORT_SIZE; ++i) {
|
||||
if (prev_key_message[i] != key_message[i]) {
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, key_message, REPORT_SIZE);
|
||||
}
|
||||
|
||||
if (unicode_index > UNICODE_REPORT_OFFSET) {
|
||||
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, unicode_message, REPORT_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void hid_deinit(void) { active = false; }
|
||||
300
mozc-doublesided/firmware/Core/Src/i2c.c
Normal file
300
mozc-doublesided/firmware/Core/Src/i2c.c
Normal file
@@ -0,0 +1,300 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "hid.h"
|
||||
#include "led.h"
|
||||
#include "main.h"
|
||||
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
|
||||
enum {
|
||||
STATE_IDLE,
|
||||
STATE_LISTENING,
|
||||
STATE_WRITTEN,
|
||||
STATE_READ,
|
||||
STATE_HOST,
|
||||
STATE_INIT_HOST,
|
||||
};
|
||||
|
||||
static volatile uint8_t state = STATE_IDLE;
|
||||
static volatile uint8_t address = 0;
|
||||
static volatile uint8_t commands[24];
|
||||
static volatile uint8_t sw_pushed = 0xff;
|
||||
static volatile uint8_t sw_current = 0xff;
|
||||
static bool ready = false;
|
||||
static uint8_t keys[26];
|
||||
static uint8_t leds[26 * 24];
|
||||
|
||||
void HAL_I2C_AddrCallback(I2C_HandleTypeDef* hi2c, uint8_t TransferDirection,
|
||||
uint16_t AddrMatchCode) {
|
||||
address = AddrMatchCode >> 1;
|
||||
if (TransferDirection == I2C_DIRECTION_RECEIVE) {
|
||||
HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &sw_pushed, 1,
|
||||
I2C_FIRST_AND_LAST_FRAME);
|
||||
} else {
|
||||
if (address == 0x01) {
|
||||
HAL_I2C_Slave_Seq_Receive_IT(hi2c, commands, 1, I2C_FIRST_AND_LAST_FRAME);
|
||||
} else {
|
||||
HAL_I2C_Slave_Seq_Receive_IT(hi2c, commands, 24,
|
||||
I2C_FIRST_AND_LAST_FRAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef* I2cHandle) {
|
||||
state = STATE_READ;
|
||||
}
|
||||
|
||||
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef* I2cHandle) {
|
||||
state = STATE_WRITTEN;
|
||||
}
|
||||
|
||||
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef* hi2c) { state = STATE_IDLE; }
|
||||
|
||||
static void update_leds(uint8_t* data) {
|
||||
led_set(0, data[0], data[1], data[2]);
|
||||
led_set(1, data[3], data[4], data[5]);
|
||||
led_set(2, data[6], data[7], data[8]);
|
||||
led_set(3, data[9], data[10], data[11]);
|
||||
led_set(4, data[12], data[13], data[14]);
|
||||
led_set(5, data[15], data[16], data[17]);
|
||||
led_set(6, data[18], data[19], data[20]);
|
||||
led_set(7, data[21], data[22], data[23]);
|
||||
led_flush();
|
||||
}
|
||||
|
||||
static void h2rgb(uint8_t h, uint8_t* r, uint8_t* g, uint8_t* b) {
|
||||
uint8_t i = h / 30;
|
||||
uint8_t k = (h % 30) * 6;
|
||||
uint8_t n = 180 - k;
|
||||
switch (i) {
|
||||
case 0:
|
||||
*r = 180;
|
||||
*g = k;
|
||||
*b = 180;
|
||||
break;
|
||||
case 1:
|
||||
*r = n;
|
||||
*g = 180;
|
||||
*b = 180;
|
||||
break;
|
||||
case 2:
|
||||
*r = 180;
|
||||
*g = 180;
|
||||
*b = k;
|
||||
break;
|
||||
case 3:
|
||||
*r = 180;
|
||||
*g = n;
|
||||
*b = 180;
|
||||
break;
|
||||
case 4:
|
||||
*r = k;
|
||||
*g = 180;
|
||||
*b = 180;
|
||||
break;
|
||||
case 5:
|
||||
*r = 180;
|
||||
*g = 180;
|
||||
*b = n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void led_step(void) {
|
||||
uint8_t data[26 * 24];
|
||||
for (int i = 0; i < 26 * 24; ++i) {
|
||||
uint8_t half = leds[i] >> 1;
|
||||
uint8_t quarter = half >> 1;
|
||||
data[i] = half + quarter;
|
||||
}
|
||||
for (int i = 0; i < 26 * 24; ++i) {
|
||||
uint16_t v = data[i] >> 5;
|
||||
v += (data[(i + 26 * 24 - 24) % (26 * 24)] >> 1) +
|
||||
(data[(i + 24) % (26 * 24)] >> 1);
|
||||
v += (data[(i + 26 * 24 - 3) % (26 * 24)] >> 5) +
|
||||
(data[(i + 3) % (26 * 24)] >> 5);
|
||||
leds[i] = (v > 128) ? 128 : v;
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_pull(bool pullup) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
||||
GPIO_InitStruct.Pull = pullup ? GPIO_PULLUP : GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
|
||||
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
static void setup_host(void) {
|
||||
hi2c1.Init.Timing = 0x00101D2D;
|
||||
hi2c1.Init.OwnAddress1 = 0;
|
||||
HAL_I2C_Init(&hi2c1);
|
||||
|
||||
setup_pull(true);
|
||||
state = STATE_HOST;
|
||||
uint8_t addr = 0xa0;
|
||||
for (int i = 0; i < 24; ++i) {
|
||||
commands[i] = 0;
|
||||
}
|
||||
while (GPIO_PIN_RESET == HAL_GPIO_ReadPin(RDYin_GPIO_Port, RDYin_Pin)) {
|
||||
HAL_StatusTypeDef s =
|
||||
HAL_I2C_Master_Transmit(&hi2c1, 1 << 1, &addr, 1, 100);
|
||||
if (s == HAL_OK) {
|
||||
HAL_Delay(50);
|
||||
HAL_I2C_Master_Transmit(&hi2c1, addr << 1, commands, 24, 100);
|
||||
addr++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 26; ++i) {
|
||||
keys[i] = 0xff;
|
||||
for (int j = 0; j < 24; ++j) {
|
||||
leds[i * 24 + j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_client(uint8_t address) {
|
||||
hi2c1.Init.OwnAddress1 = address << 1;
|
||||
HAL_I2C_Init(&hi2c1);
|
||||
|
||||
setup_pull(false);
|
||||
}
|
||||
|
||||
void i2c_init(void) {
|
||||
HAL_GPIO_WritePin(RDYout_GPIO_Port, RDYout_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(COM2_GPIO_Port, COM2_Pin, GPIO_PIN_SET);
|
||||
const bool is_host = !HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin) &&
|
||||
!HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin) &&
|
||||
!HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin) &&
|
||||
!HAL_GPIO_ReadPin(SW4_GPIO_Port, SW4_Pin);
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_SET);
|
||||
if (is_host) {
|
||||
setup_host();
|
||||
} else {
|
||||
setup_client(0x00);
|
||||
}
|
||||
}
|
||||
|
||||
static void maybe_listen_client(void) {
|
||||
HAL_GPIO_WritePin(COM2_GPIO_Port, COM2_Pin, GPIO_PIN_RESET);
|
||||
uint8_t sw = HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin) << 4 |
|
||||
HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin) << 5 |
|
||||
HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin) << 6 |
|
||||
HAL_GPIO_ReadPin(SW4_GPIO_Port, SW4_Pin) << 7;
|
||||
HAL_GPIO_WritePin(COM2_GPIO_Port, COM2_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_RESET);
|
||||
|
||||
const bool ready_now =
|
||||
GPIO_PIN_SET == HAL_GPIO_ReadPin(RDYin_GPIO_Port, RDYin_Pin);
|
||||
if (ready != ready_now) {
|
||||
ready = ready_now;
|
||||
HAL_I2C_DisableListen_IT(&hi2c1);
|
||||
setup_client(ready ? 0x01 : 0x00);
|
||||
state = STATE_IDLE;
|
||||
}
|
||||
|
||||
sw |= HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin) << 0 |
|
||||
HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin) << 1 |
|
||||
HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin) << 2 |
|
||||
HAL_GPIO_ReadPin(SW4_GPIO_Port, SW4_Pin) << 3;
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_SET);
|
||||
sw_current = sw;
|
||||
sw_pushed &= sw;
|
||||
|
||||
if (state != STATE_LISTENING) {
|
||||
if (state == STATE_READ) {
|
||||
sw_pushed |= sw_current;
|
||||
} else if (state == STATE_WRITTEN) {
|
||||
if (address == 1) {
|
||||
uint8_t new_address = commands[0] & 0x7f;
|
||||
setup_client(new_address);
|
||||
HAL_GPIO_WritePin(RDYout_GPIO_Port, RDYout_Pin, GPIO_PIN_SET);
|
||||
if (commands[0] & 0x80) {
|
||||
for (uint8_t bit = 0; bit < 8; ++bit) {
|
||||
uint8_t v = new_address & (1 << bit) ? 255 : 0;
|
||||
led_set(bit, v, v, v);
|
||||
}
|
||||
led_flush();
|
||||
}
|
||||
} else {
|
||||
update_leds(commands);
|
||||
}
|
||||
}
|
||||
if (HAL_OK == HAL_I2C_EnableListen_IT(&hi2c1)) {
|
||||
state = STATE_LISTENING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void maybe_listen_host(void) {
|
||||
HAL_GPIO_WritePin(COM2_GPIO_Port, COM2_Pin, GPIO_PIN_RESET);
|
||||
keys[25] = HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin) << 4 |
|
||||
HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin) << 5 |
|
||||
HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin) << 6 |
|
||||
HAL_GPIO_ReadPin(SW4_GPIO_Port, SW4_Pin) << 7;
|
||||
HAL_GPIO_WritePin(COM2_GPIO_Port, COM2_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_RESET);
|
||||
for (uint8_t addr = 0; addr < 25; ++addr) {
|
||||
HAL_I2C_Master_Receive(&hi2c1, (0x20 + addr) << 1, &keys[addr], 1, 100);
|
||||
}
|
||||
keys[25] |= HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin) << 0 |
|
||||
HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin) << 1 |
|
||||
HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin) << 2 |
|
||||
HAL_GPIO_ReadPin(SW4_GPIO_Port, SW4_Pin) << 3;
|
||||
HAL_GPIO_WritePin(COM1_GPIO_Port, COM1_Pin, GPIO_PIN_SET);
|
||||
|
||||
hid_update(keys);
|
||||
|
||||
led_step();
|
||||
|
||||
static uint8_t h = 0;
|
||||
uint8_t r = 0;
|
||||
uint8_t g = 0;
|
||||
uint8_t b = 0;
|
||||
h2rgb(h, &r, &g, &b);
|
||||
h = (h + 1) % 180;
|
||||
|
||||
for (int i = 0; i < 26; ++i) {
|
||||
uint8_t key = keys[i];
|
||||
uint8_t* led = &leds[i * 24];
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1) {
|
||||
if (0 == (key & mask)) {
|
||||
led[0] = r;
|
||||
led[1] = g;
|
||||
led[2] = b;
|
||||
}
|
||||
led += 3;
|
||||
}
|
||||
if (i != 25) {
|
||||
HAL_I2C_Master_Transmit(&hi2c1, (0x20 + i) << 1, led - 24, 24, 100);
|
||||
} else {
|
||||
update_leds(led - 24);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool i2c_is_host(void) { return state == STATE_HOST; }
|
||||
|
||||
void i2c_activate_host(void) { state = STATE_INIT_HOST; }
|
||||
|
||||
void i2c_maybe_listen(void) {
|
||||
if (state == STATE_INIT_HOST) {
|
||||
setup_host();
|
||||
}
|
||||
if (state == STATE_HOST) {
|
||||
maybe_listen_host();
|
||||
} else {
|
||||
maybe_listen_client();
|
||||
}
|
||||
}
|
||||
57
mozc-doublesided/firmware/Core/Src/led.c
Normal file
57
mozc-doublesided/firmware/Core/Src/led.c
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#include "led.h"
|
||||
|
||||
#include "main.h"
|
||||
|
||||
extern SPI_HandleTypeDef hspi1;
|
||||
|
||||
#define RESET_COUNT 16
|
||||
#define LED_COUNT 8
|
||||
#define DATA_LENGTH 6
|
||||
#define DMA_BUFFER_SIZE (RESET_COUNT + LED_COUNT * DATA_LENGTH + 1) << 1
|
||||
|
||||
// 0xe: 1110 - HIGH
|
||||
// 0x8: 1000 - LOW
|
||||
static uint16_t dma_buffer[DMA_BUFFER_SIZE] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // RESET
|
||||
0, 0, 0, 0, 0, 0, // LED 0
|
||||
0, 0, 0, 0, 0, 0, // LED 1
|
||||
0, 0, 0, 0, 0, 0, // LED 2
|
||||
0, 0, 0, 0, 0, 0, // LED 3
|
||||
0, 0, 0, 0, 0, 0, // LED 4
|
||||
0, 0, 0, 0, 0, 0, // LED 5
|
||||
0, 0, 0, 0, 0, 0, // LED 6
|
||||
0, 0, 0, 0, 0, 0, // LED 7
|
||||
0xffff, // EOD
|
||||
};
|
||||
|
||||
static uint32_t to_spi_value(uint8_t value) {
|
||||
uint32_t spi_value = 0;
|
||||
for (uint8_t bit_mask = 0x80; bit_mask != 0; bit_mask >>= 1) {
|
||||
spi_value <<= 4;
|
||||
if (value & bit_mask) {
|
||||
spi_value |= 0xe;
|
||||
} else {
|
||||
spi_value |= 0x8;
|
||||
}
|
||||
}
|
||||
return spi_value;
|
||||
}
|
||||
|
||||
void set_spi_value(uint8_t offset, uint32_t value) {
|
||||
dma_buffer[RESET_COUNT + offset * 2 + 0] = value >> 16;
|
||||
dma_buffer[RESET_COUNT + offset * 2 + 1] = value & 0xffff;
|
||||
}
|
||||
|
||||
void led_set(uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
|
||||
set_spi_value(index * 3 + 0, to_spi_value(g));
|
||||
set_spi_value(index * 3 + 1, to_spi_value(r));
|
||||
set_spi_value(index * 3 + 2, to_spi_value(b));
|
||||
}
|
||||
|
||||
void led_flush(void) {
|
||||
HAL_SPI_Transmit_DMA(&hspi1, (uint8_t *)dma_buffer, DMA_BUFFER_SIZE);
|
||||
}
|
||||
15
mozc-doublesided/firmware/Core/Src/mozc.c
Normal file
15
mozc-doublesided/firmware/Core/Src/mozc.c
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2024 Google Inc.
|
||||
// Use of this source code is governed by an Apache License that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#include "mozc.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "i2c.h"
|
||||
#include "main.h"
|
||||
|
||||
void mozc_init(void) { i2c_init(); }
|
||||
|
||||
void mozc_loop(void) { i2c_maybe_listen(); }
|
||||
Reference in New Issue
Block a user