IO4 output implemented and coin button working

This commit is contained in:
whowechina 2024-02-25 19:32:27 +08:00
parent 8ee584af17
commit c4f3a535e3
9 changed files with 62 additions and 6 deletions

Binary file not shown.

View File

@ -116,7 +116,7 @@ It's CC-NC. So DIY for yourself and for your friend, don't make money from it.
* It has a command line to do configuration. You can use this Web Serial Terminal to connect to the USB serial port of the Mai Pico. (Note: "?" is for help)
https://googlechromelabs.github.io/serial-terminal/
<img src="doc/cmd.png" width="80%">
* Button GPIOs can be remapped using `gpio` command.
* Button GPIOs can be remapped using `gpio` command. Firmware supports 8 main buttons on the ring and 4 auxiliary buttons (Test, Service, Navigate and Coin).
* Daisy chained RGB LED numbers for each button can be assigned using `rgb` command.
* LED brightness can be adjusted by `level` command.
* There are MPR121 parameter tuning and sensitive settings, explore them yourself.

View File

@ -124,7 +124,7 @@ https://github.com/whowechina/
* 它有一个命令行进行配置。你可以使用这个 Web Serial Terminal 连接到 Mai Pico 的 USB 串口。(注意:"?" 是帮助)
https://googlechromelabs.github.io/serial-terminal/
<img src="doc/cmd.png" width="80%">
* 可以使用 `gpio` 命令重新映射按钮 GPIOs。
* 可以使用 `gpio` 命令重新映射按钮 GPIOs,固件支持 8 个主按钮和 4 个辅助按钮分别是Test, Service, Navigate, Coin
* 可以使用 `rgb` 命令为每个按钮分配串联的 RGB LED 数量。
* 可以通过 `level` 命令调整 LED 的亮度。
* 有 MPR121 参数调整和灵敏度设置,自己探索。

View File

@ -14,6 +14,7 @@
#define RGB_ORDER GRB // or RGB
#define RGB_BUTTON_MAP { 5, 4, 3, 2, 1, 0, 7, 6, 8, 9, 10, 11 }
/* 8 main buttons, Test, Service, Navigate, Coin */
#define BUTTON_DEF { 1, 0, 4, 5, 8, 9, 3, 2, 12, 11, 10, 14 }
/* HID Keycode: https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h */

View File

@ -43,10 +43,18 @@ static void report_usb_hid()
{
if (tud_hid_ready()) {
if (mai_cfg->hid.joy) {
static uint16_t last_buttons = 0;
uint16_t buttons = button_read();
hid_joy.buttons[0] = native_to_io4(buttons);
hid_joy.buttons[1] = native_to_io4(0);
if (last_buttons ^ buttons & (1 << 11)) {
if ((buttons & (1 << 11)) && (hid_joy.chutes[0] < 0xff00)) {
// just pressed coin button
hid_joy.chutes[0] += 0x100;
}
}
tud_hid_n_report(0, REPORT_ID_JOYSTICK, &hid_joy, sizeof(hid_joy));
last_buttons = buttons;
}
if (mai_cfg->hid.nkro) {
tud_hid_n_report(1, 0, &hid_nkro, sizeof(hid_nkro));
@ -82,3 +90,33 @@ void hid_update()
gen_nkro_report();
report_usb_hid();
}
typedef struct __attribute__((packed)) {
uint8_t report_id;
uint8_t cmd;
uint8_t payload[62];
} hid_output_t;
void hid_proc(const uint8_t *data, uint8_t len)
{
hid_output_t *output = (hid_output_t *)data;
if (output->report_id == REPORT_ID_OUTPUT) {
switch (output->cmd) {
case 0x01: // Set Timeout
case 0x02: // Set Sampling Count
hid_joy.system_status = 0x30;
break;
case 0x03: // Clear Board Status
hid_joy.chutes[0] = 0;
hid_joy.chutes[1] = 0;
hid_joy.system_status = 0x00;
break;
case 0x04: // Set General Output
case 0x41: // I don't know what this is
break;
default:
printf("USB unknown cmd: %d\n", output->cmd);
break;
}
}
}

View File

@ -7,5 +7,6 @@
#define HID_H_
void hid_update();
void hid_proc(const uint8_t *data, uint8_t len);
#endif

View File

@ -135,4 +135,5 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type, uint8_t const *buffer,
uint16_t bufsize)
{
hid_proc(buffer, bufsize);
}

View File

@ -89,9 +89,13 @@ enum { ITF_NUM_JOY, ITF_NUM_NKRO,
ITF_NUM_CDC_LED, ITF_NUM_CDC_LED_DATA,
ITF_NUM_TOTAL };
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN * 2 + TUD_CDC_DESC_LEN * 3)
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \
TUD_HID_INOUT_DESC_LEN * 1 + \
TUD_HID_DESC_LEN * 1 + \
TUD_CDC_DESC_LEN * 3)
#define EPNUM_JOY 0x81
#define EPNUM_OUTPUT 0x01
#define EPNUM_KEY 0x82
#define EPNUM_CDC_CLI_NOTIF 0x83
@ -114,8 +118,8 @@ uint8_t const desc_configuration_joy[] = {
// Interface number, string index, protocol, report descriptor len, EP In
// address, size & polling interval
TUD_HID_DESCRIPTOR(ITF_NUM_JOY, 4, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_joy), EPNUM_JOY,
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_JOY, 4, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_joy), EPNUM_OUTPUT, EPNUM_JOY,
CFG_TUD_HID_EP_BUFSIZE, 1),
TUD_HID_DESCRIPTOR(ITF_NUM_NKRO, 5, HID_ITF_PROTOCOL_NONE,

View File

@ -6,6 +6,7 @@
enum {
REPORT_ID_JOYSTICK = 1,
REPORT_ID_OUTPUT = 16,
};
// because they are missing from tusb_hid.h
@ -99,6 +100,16 @@ enum {
\
HID_REPORT_COUNT(1), HID_REPORT_SIZE(232), \
HID_INPUT(HID_CONSTANT | HID_ABSOLUTE), \
\
HID_USAGE_PAGE_N(0xffa0, 2), \
HID_USAGE(0x00), \
HID_REPORT_ID(REPORT_ID_OUTPUT) \
HID_COLLECTION(HID_COLLECTION_APPLICATION), \
HID_USAGE(0x00), \
HID_LOGICAL_MIN(0), HID_LOGICAL_MAX(255), \
HID_REPORT_COUNT(63), HID_REPORT_SIZE(8), \
HID_OUTPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
HID_COLLECTION_END, \
HID_COLLECTION_END
#define MAIPICO_REPORT_DESC_NKRO \