logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

protocol.h (3324B)


  1. /*
  2. * (c) 2021 by Tomasz bla Fortuna
  3. * License: GPLv2
  4. *
  5. * This file is shared with the Shine firmware. Keep it in sync (and in the
  6. * shine's clang formatting).
  7. */
  8. #pragma once
  9. #include <stdint.h>
  10. #define PROTOCOL_SD SD0
  11. enum {
  12. /*
  13. * Main -> LED
  14. */
  15. /* Basic config */
  16. CMD_LED_ON = 0x01,
  17. CMD_LED_OFF = 0x02,
  18. CMD_LED_SET_PROFILE = 0x03,
  19. CMD_LED_NEXT_PROFILE = 0x04,
  20. CMD_LED_PREV_PROFILE = 0x05,
  21. CMD_LED_NEXT_INTENSITY = 0x06,
  22. CMD_LED_NEXT_ANIMATION_SPEED = 0x07,
  23. /* Masks */
  24. /* Override a key color, eg. capslock */
  25. CMD_LED_MASK_SET_KEY = 0x10,
  26. /* Override all keys in a row with configurable colors */
  27. CMD_LED_MASK_SET_ROW = 0x11,
  28. /* Override all keys with single color (eg. foreground color) */
  29. CMD_LED_MASK_SET_MONO = 0x12,
  30. /* Reactive / status */
  31. CMD_LED_GET_STATUS = 0x20, /* unused */
  32. CMD_LED_KEY_BLINK = 0x21,
  33. CMD_LED_KEY_DOWN = 0x22,
  34. CMD_LED_KEY_UP = 0x23, /* TODO */
  35. CMD_LED_IAP = 0x24,
  36. /* Manual color control */
  37. CMD_LED_SET_MANUAL = 0x30,
  38. CMD_LED_COLOR_SET_KEY = 0x31,
  39. CMD_LED_COLOR_SET_ROW = 0x32,
  40. CMD_LED_COLOR_SET_MONO = 0x33,
  41. /* LED -> Main */
  42. /* Payload with data to send over HID */
  43. CMD_LED_DEBUG = 0x40,
  44. /* Number of profiles, current profile, on/off state,
  45. reactive flag, brightness, errors */
  46. CMD_LED_STATUS = 0x41,
  47. /* Set sticky key, meaning the key will light up even when LEDs are turned off */
  48. CMD_LED_STICKY_SET_KEY = 0x50,
  49. CMD_LED_STICKY_SET_ROW = 0x51,
  50. CMD_LED_STICKY_SET_MONO = 0x52,
  51. CMD_LED_STICKY_UNSET_KEY = 0x53,
  52. CMD_LED_STICKY_UNSET_ROW = 0x54,
  53. CMD_LED_STICKY_UNSET_ALL = 0x55,
  54. };
  55. /* 1 ROW * 14 COLS * 4B (RGBX) = 56 + header prefix. */
  56. #define MAX_PAYLOAD_SIZE 64
  57. /** Enum of the states used for the serial protocol finite-state automaton */
  58. enum proto_state {
  59. /* 2-byte initial start-of-message sync */
  60. STATE_SYNC_1,
  61. STATE_SYNC_2,
  62. /* Waiting for command byte */
  63. STATE_CMD,
  64. /* Waiting for ID byte */
  65. STATE_ID,
  66. /* Waiting for payload size */
  67. STATE_PAYLOAD_SIZE,
  68. /* Reading payload until payload_position == payload_size */
  69. STATE_PAYLOAD,
  70. };
  71. /* Buffer holding a single message */
  72. typedef struct {
  73. uint8_t command;
  74. uint8_t msg_id;
  75. uint8_t payload_size;
  76. uint8_t payload[MAX_PAYLOAD_SIZE];
  77. } message_t;
  78. /* Internal protocol state */
  79. typedef struct {
  80. /* Callback to call upon receiving a valid message */
  81. void (*callback)(const message_t *);
  82. /* Number of read payload bytes */
  83. uint8_t payload_position;
  84. /* Current finite-state-automata state */
  85. enum proto_state state;
  86. uint8_t previous_id;
  87. uint8_t errors;
  88. /* Currently received message */
  89. message_t buffer;
  90. } protocol_t;
  91. /* NOTE: This didn't work when defined on stack */
  92. extern protocol_t proto;
  93. /* Init state */
  94. extern void proto_init(protocol_t *proto, void (*callback)(const message_t *));
  95. /* Consume one byte and push state forward - might call the callback */
  96. extern void proto_consume(protocol_t *proto, uint8_t byte);
  97. /* Prolonged silence - reset state */
  98. extern void proto_silence(protocol_t *proto);
  99. /* Transmit message */
  100. extern void proto_tx(uint8_t cmd, const unsigned char *buf, int payload_size, int retries);