From 754b5c131abb6c01cdaad3a96b39b575da59359f Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Mon, 1 May 2023 18:38:53 +0200 Subject: [PATCH] Adjust constants and button event handling Signed-off-by: TheJackiMonster --- interface_lib/include/device4.h | 67 ++++++++++++++++++++++++++++----- interface_lib/src/device4.c | 55 ++++++++++++++++----------- 2 files changed, 91 insertions(+), 31 deletions(-) diff --git a/interface_lib/include/device4.h b/interface_lib/include/device4.h index bbc1cb7..6668efa 100644 --- a/interface_lib/include/device4.h +++ b/interface_lib/include/device4.h @@ -33,15 +33,62 @@ #include #endif -#define DEVICE4_ACTION_PASSIVE_POLL_START 0b00010 -#define DEVICE4_ACTION_BRIGHTNESS_COMMAND 0b00011 -#define DEVICE4_ACTION_MANUAL_POLL_CLICK 0b00101 -#define DEVICE4_ACTION_ACTIVE_POLL 0b01001 -#define DEVICE4_ACTION_PASSIVE_POLL_END 0b10010 +#define DEVICE4_MSG_P_BRIGHTNESS 0x03 +#define DEVICE4_MSG_W_DISP_MODE 0x08 +#define DEVICE4_MSG_R_GLASSID 0x15 +#define DEVICE4_MSG_R_DP7911_FW_VERSION 0x16 +#define DEVICE4_MSG_R_DSP_VERSION 0x18 +#define DEVICE4_MSG_W_CANCEL_ACTIVATION 0x19 +#define DEVICE4_MSG_P_HEARTBEAT 0x1A +#define DEVICE4_MSG_W_SLEEP_TIME 0x1E -#define DEVICE4_BUTTON_DISPLAY_TOGGLE 0x1 -#define DEVICE4_BUTTON_BRIGHTNESS_UP 0x2 -#define DEVICE4_BUTTON_BRIGHTNESS_DOWN 0x3 +#define DEVICE4_MSG_R_DSP_APP_FW_VERSION 0x21 +#define DEVICE4_MSG_R_MCU_APP_FW_VERSION 0x26 +#define DEVICE4_MSG_R_ACTIVATION_TIME 0x29 +#define DEVICE4_MSG_W_ACTIVATION_TIME 0x2A + +#define DEVICE4_MSG_R_DP7911_FW_IS_UPDATE 0x3C +#define DEVICE4_MSG_W_UPDATE_DP 0x3D +#define DEVICE4_MSG_W_UPDATE_MCU_APP_FW_PREPARE 0x3E +#define DEVICE4_MSG_W_UPDATE_MCU_APP_FW_START 0x3F + +#define DEVICE4_MSG_W_UPDATE_MCU_APP_FW_TRANSMIT 0x40 +#define DEVICE4_MSG_W_UPDATE_MCU_APP_FW_FINISH 0x41 +#define DEVICE4_MSG_W_BOOT_JUMP_TO_APP 0x42 +#define DEVICE4_MSG_W_MCU_APP_JUMP_TO_BOOT 0x44 +#define DEVICE4_MSG_W_UPDATE_DSP_APP_FW_PREPARE 0x45 +#define DEVICE4_MSG_W_UPDATE_DSP_APP_FW_START 0x46 +#define DEVICE4_MSG_W_UPDATE_DSP_APP_FW_TRANSMIT 0x47 +#define DEVICE4_MSG_W_UPDATE_DSP_APP_FW_FINISH 0x48 +#define DEVICE4_MSG_R_IS_NEED_UPGRADE_DSP_FW 0x49 + +#define DEVICE4_MSG_W_FORCE_UPGRADE_DSP_FW 0x69 + +#define DEVICE4_MSG_W_BOOT_UPDATE_MODE 0x1100 +#define DEVICE4_MSG_W_BOOT_UPDATE_CONFIRM 0x1101 +#define DEVICE4_MSG_W_BOOT_UPDATE_PREPARE 0x1102 +#define DEVICE4_MSG_W_BOOT_UPDATE_START 0x1103 +#define DEVICE4_MSG_W_BOOT_UPDATE_TRANSMIT 0x1104 +#define DEVICE4_MSG_W_BOOT_UPDATE_FINISH 0x1105 + +#define DEVICE4_MSG_P_START_HEARTBEAT 0x6c02 +#define DEVICE4_MSG_P_BUTTON_PRESSED 0x6C05 +#define DEVICE4_MSG_P_END_HEARTBEAT 0x6c12 +#define DEVICE4_MSG_P_ASYNC_TEXT_LOG 0x6c09 + +#define DEVICE4_MSG_E_DSP_ONE_PACKGE_WRITE_FINISH 0x6C0E +#define DEVICE4_MSG_E_DSP_UPDATE_PROGRES 0x6C10 +#define DEVICE4_MSG_E_DSP_UPDATE_ENDING 0x6C11 + +#define DEVICE4_BUTTON_PHYS_DISPLAY_TOGGLE 0x1 +#define DEVICE4_BUTTON_PHYS_BRIGHTNESS_UP 0x2 +#define DEVICE4_BUTTON_PHYS_BRIGHTNESS_DOWN 0x3 + +#define DEVICE4_BUTTON_VIRT_DISPLAY_TOGGLE 0x1 +#define DEVICE4_BUTTON_VIRT_BRIGHTNESS_UP 0x6 +#define DEVICE4_BUTTON_VIRT_BRIGHTNESS_DOWN 0x7 +#define DEVICE4_BUTTON_VIRT_MODE_UP 0x8 +#define DEVICE4_BUTTON_VIRT_MODE_DOWN 0x9 #ifdef __cplusplus extern "C" { @@ -53,8 +100,8 @@ struct __attribute__((__packed__)) device4_packet_t { uint16_t length; uint8_t _padding0 [4]; uint32_t timestamp; - uint8_t action; - uint8_t _padding1 [6]; + uint16_t msgid; + uint8_t _padding1 [5]; union { char text [42]; uint8_t data [42]; diff --git a/interface_lib/src/device4.c b/interface_lib/src/device4.c index b57a22b..6ad8835 100644 --- a/interface_lib/src/device4.c +++ b/interface_lib/src/device4.c @@ -52,7 +52,7 @@ static bool send_payload(device4_type* device, uint8_t size, const uint8_t* payl return (transferred == size); } -static bool send_payload_action(device4_type* device, uint8_t action, uint8_t len, const uint8_t* data) { +static bool send_payload_action(device4_type* device, uint16_t msgid, uint8_t len, const uint8_t* data) { static device4_packet_type packet; const uint16_t packet_len = 11 + len; @@ -62,12 +62,12 @@ static bool send_payload_action(device4_type* device, uint8_t action, uint8_t le packet.length = packet_len; memset(packet._padding0, 0, 4); packet.timestamp = 0; - packet.action = action; - memset(packet._padding1, 0, 6); + packet.msgid = msgid; + memset(packet._padding1, 0, 5); memcpy(packet.data, data, len); packet.checksum = crc32_checksum((const uint8_t*) (&packet.length), packet.length); - + return send_payload(device, payload_len, (uint8_t*) (&packet)); } @@ -113,7 +113,7 @@ device4_type* device4_open(device4_event_callback callback) { device4_clear(device); - /*if (!send_payload_action(device, DEVICE4_ACTION_BRIGHTNESS_COMMAND, 0, NULL)) { + /*if (!send_payload_action(device, DEVICE4_MSG_P_BRIGHTNESS, 0, NULL)) { perror("Sending brightness command action failed!\n"); return device; } @@ -142,7 +142,7 @@ void device4_clear(device4_type* device) { } int device4_read(device4_type* device, int timeout) { - if (!device) { + if ((!device) || (!device->handle)) { return -1; } @@ -172,12 +172,24 @@ int device4_read(device4_type* device, int timeout) { const uint32_t timestamp = packet.timestamp; const size_t data_len = (size_t) &(packet.data) - (size_t) &(packet.length); + +#ifndef NDEBUG + printf("MSG: %d = %04x (%d)\n", packet.msgid, packet.msgid, packet.length); + + if (packet.length > 11) { + for (int i = 0; i < packet.length - 11; i++) { + printf("%02x ", packet.data[i]); + } + + printf("\n"); + } +#endif - switch (packet.action) { - case DEVICE4_ACTION_PASSIVE_POLL_START: { + switch (packet.msgid) { + case DEVICE4_MSG_P_START_HEARTBEAT: { break; } - case DEVICE4_ACTION_BRIGHTNESS_COMMAND: { + case DEVICE4_MSG_P_BRIGHTNESS: { const uint8_t brightness = packet.data[1]; device->brightness = brightness; @@ -191,13 +203,14 @@ int device4_read(device4_type* device, int timeout) { ); break; } - case DEVICE4_ACTION_MANUAL_POLL_CLICK: { - const uint8_t button = packet.data[0]; - const uint8_t brightness = packet.data[8]; + case DEVICE4_MSG_P_BUTTON_PRESSED: { + const uint8_t phys_button = packet.data[0]; + const uint8_t virt_button = packet.data[4]; + const uint8_t value = packet.data[8]; - switch (button) { - case DEVICE4_BUTTON_DISPLAY_TOGGLE: - device->active = !device->active; + switch (virt_button) { + case DEVICE4_BUTTON_VIRT_DISPLAY_TOGGLE: + device->active = value; if (device->active) { device4_callback( @@ -217,8 +230,8 @@ int device4_read(device4_type* device, int timeout) { ); } break; - case DEVICE4_BUTTON_BRIGHTNESS_UP: - device->brightness = brightness; + case DEVICE4_BUTTON_VIRT_BRIGHTNESS_UP: + device->brightness = value; device4_callback( device, @@ -228,8 +241,8 @@ int device4_read(device4_type* device, int timeout) { NULL ); break; - case DEVICE4_BUTTON_BRIGHTNESS_DOWN: - device->brightness = brightness; + case DEVICE4_BUTTON_VIRT_BRIGHTNESS_DOWN: + device->brightness = value; device4_callback( device, @@ -245,7 +258,7 @@ int device4_read(device4_type* device, int timeout) { break; } - case DEVICE4_ACTION_ACTIVE_POLL: { + case DEVICE4_MSG_P_ASYNC_TEXT_LOG: { const char* text = packet.text; const size_t text_len = strlen(text); @@ -265,7 +278,7 @@ int device4_read(device4_type* device, int timeout) { ); break; } - case DEVICE4_ACTION_PASSIVE_POLL_END: { + case DEVICE4_MSG_P_END_HEARTBEAT: { break; } default: