annepro2: make code compatible with QMK coding conventions

This commit is contained in:
bwisn
2021-09-12 09:26:48 +02:00
parent bc761b5170
commit b276ac6845
16 changed files with 522 additions and 600 deletions
+30 -40
View File
@@ -23,32 +23,26 @@
/* -------------------- Static Function Prototypes -------------------------- */
static uint8_t ap2_ble_leds(void);
static void ap2_ble_mouse(report_mouse_t *report);
static void ap2_ble_system(uint16_t data);
static void ap2_ble_consumer(uint16_t data);
static void ap2_ble_keyboard(report_keyboard_t *report);
static void ap2_ble_mouse(report_mouse_t *report);
static void ap2_ble_system(uint16_t data);
static void ap2_ble_consumer(uint16_t data);
static void ap2_ble_keyboard(report_keyboard_t *report);
static void ap2_ble_swtich_ble_driver(void);
/* -------------------- Static Local Variables ------------------------------ */
static host_driver_t ap2_ble_driver = {
ap2_ble_leds,
ap2_ble_keyboard,
ap2_ble_mouse,
ap2_ble_system,
ap2_ble_consumer,
ap2_ble_leds, ap2_ble_keyboard, ap2_ble_mouse, ap2_ble_system, ap2_ble_consumer,
};
static uint8_t bleMcuWakeup[11] = {
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x01, 0x7d, 0x02, 0x01, 0x02
};
static uint8_t bleMcuWakeup[11] = {0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x01, 0x7d, 0x02, 0x01, 0x02};
static uint8_t bleMcuStartBroadcast[11] = {
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x01, 0x00 // Broadcast ID[0-3]
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x01, 0x00 // Broadcast ID[0-3]
};
static uint8_t bleMcuConnect[11] = {
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x04, 0x00 // Connect ID [0-3]
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x04, 0x00 // Connect ID [0-3]
};
static uint8_t bleMcuSendReport[10] = {
@@ -63,24 +57,18 @@ static uint8_t bleMcuUnpair[10] = {
0x7b, 0x12, 0x53, 0x00, 0x02, 0x00, 0x00, 0x7d, 0x40, 0x05,
};
static uint8_t bleMcuBootload[11] = {
0x7b, 0x10, 0x51, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x01
};
static uint8_t bleMcuBootload[11] = {0x7b, 0x10, 0x51, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x01};
static host_driver_t *lastHostDriver = NULL;
#ifdef NKRO_ENABLE
static bool lastNkroStatus = false;
#endif // NKRO_ENABLE
#endif // NKRO_ENABLE
/* -------------------- Public Function Implementation ---------------------- */
void annepro2_ble_bootload(void) {
sdWrite(&SD1, bleMcuBootload, 11);
}
void annepro2_ble_bootload(void) { sdWrite(&SD1, bleMcuBootload, 11); }
void annepro2_ble_startup(void) {
sdWrite(&SD1, bleMcuWakeup, 11);
}
void annepro2_ble_startup(void) { sdWrite(&SD1, bleMcuWakeup, 11); }
void annepro2_ble_broadcast(uint8_t port) {
if (port > 3) {
@@ -138,26 +126,29 @@ static void ap2_ble_swtich_ble_driver(void) {
}
static uint8_t ap2_ble_leds(void) {
return 0; // TODO: Figure out how to obtain LED status
}
static void ap2_ble_mouse(report_mouse_t *report){
}
static void ap2_ble_system(uint16_t data) {
return 0; // TODO: Figure out how to obtain LED status
}
static void ap2_ble_mouse(report_mouse_t *report) {}
static void ap2_ble_system(uint16_t data) {}
static inline uint16_t CONSUMER2AP2(uint16_t usage) {
switch(usage) {
case AUDIO_VOL_DOWN: return 0x04;
case AUDIO_VOL_UP: return 0x02;
case AUDIO_MUTE: return 0x01;
case TRANSPORT_PLAY_PAUSE: return 0x08;
case TRANSPORT_NEXT_TRACK: return 0x10;
case TRANSPORT_PREV_TRACK: return 0x20;
default: return 0x00;
switch (usage) {
case AUDIO_VOL_DOWN:
return 0x04;
case AUDIO_VOL_UP:
return 0x02;
case AUDIO_MUTE:
return 0x01;
case TRANSPORT_PLAY_PAUSE:
return 0x08;
case TRANSPORT_NEXT_TRACK:
return 0x10;
case TRANSPORT_PREV_TRACK:
return 0x20;
default:
return 0x00;
}
}
@@ -176,4 +167,3 @@ static void ap2_ble_keyboard(report_keyboard_t *report) {
sdWrite(&SD1, bleMcuSendReport, 10);
sdWrite(&SD1, &report->raw[0], 8);
}
+7 -9
View File
@@ -18,13 +18,11 @@
#pragma once
/* USB Device descriptor parameter */
#define VENDOR_ID 0x04d9
#define PRODUCT_ID 0xa290
#define DEVICE_VER 0x1337
#define MANUFACTURER Holtek
#define PRODUCT Anne Pro 2 QMK
#define DESCRIPTION Anne Pro 2 with QMK
#define NAME_SLUG "annepro2"
#define VENDOR_ID 0x04d9
#define PRODUCT_ID 0xa290
#define DEVICE_VER 0x1337
#define MANUFACTURER Holtek
#define PRODUCT Anne Pro 2 QMK
#define ANNEPRO2_C15
@@ -42,7 +40,7 @@
#define NUM_LAYOUTS 4
// Matrix keymap
// clang-format off
#define LAYOUT( \
K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0D, \
K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, \
@@ -57,7 +55,7 @@
/* ROW4 */ { K30, KC_NO, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3C, KC_NO}, \
/* ROW5 */ { K40, KC_NO, K42, K43, KC_NO, KC_NO, K46, KC_NO, KC_NO, K49, K4A, K4B, K4C, KC_NO}, \
}
// clang-format on
/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5
+8 -10
View File
@@ -18,13 +18,11 @@
#pragma once
/* USB Device descriptor parameter */
#define VENDOR_ID 0x04d9
#define PRODUCT_ID 0xa291
#define DEVICE_VER 0x1337
#define MANUFACTURER Holtek
#define PRODUCT Anne Pro 2 (c18) QMK
#define DESCRIPTION Anne Pro 2 (c18) with QMK
#define NAME_SLUG "annepro2"
#define VENDOR_ID 0x04d9
#define PRODUCT_ID 0xa291
#define DEVICE_VER 0x1337
#define MANUFACTURER Holtek
#define PRODUCT Anne Pro 2(c18)QMK
#define ANNEPRO2_C18
@@ -42,7 +40,7 @@
#define NUM_LAYOUTS 4
// Matrix keymap
// clang-format off
#define LAYOUT( \
K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0D, \
K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, \
@@ -57,7 +55,7 @@
/* ROW4 */ { K30, KC_NO, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3C, KC_NO}, \
/* ROW5 */ { K40, KC_NO, K42, K43, KC_NO, KC_NO, K46, KC_NO, KC_NO, K49, K4A, K4B, K4C, KC_NO}, \
}
// clang-format on
/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5
@@ -73,7 +71,7 @@
// EEPROM Config for W25X20CL
#define EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN A3
#define EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR 16
#define EXTERNAL_EEPROM_BYTE_COUNT 1024 // 262144
#define EXTERNAL_EEPROM_BYTE_COUNT 1024 // 262144
#define EXTERNAL_EEPROM_PAGE_SIZE 256
#define EXTERNAL_EEPROM_ADDRESS_SIZE 3
#define EXTERNAL_EEPROM_SPI_LSBFIRST false
+2 -5
View File
@@ -45,7 +45,7 @@
#define CMD_WRITE 0x02u
#define CMD_SECTOR_ERASE 0x20u
#define SR_WIP 0x01u
#define SR_WIP 0x01u
// #define DEBUG_EEPROM_OUTPUT
@@ -53,9 +53,7 @@
# define EXTERNAL_EEPROM_SPI_TIMEOUT 100
#endif
bool spi_eeprom_start(void) {
return spi_start(EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN, EXTERNAL_EEPROM_SPI_LSBFIRST, EXTERNAL_EEPROM_SPI_MODE, EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR);
}
bool spi_eeprom_start(void) { return spi_start(EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN, EXTERNAL_EEPROM_SPI_LSBFIRST, EXTERNAL_EEPROM_SPI_MODE, EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR); }
static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
uint32_t deadline = timer_read32() + timeout;
@@ -141,7 +139,6 @@ void eeprom_read(void *buf, uint32_t addr, size_t len) {
}
dprintf("\n");
#endif // DEBUG_EEPROM_OUTPUT
}
void eeprom_write(const void *buf, uint32_t addr, size_t len) {
+24 -18
View File
@@ -4,38 +4,44 @@
#define bkpt() __asm volatile("BKPT #0\n")
OSAL_IRQ_HANDLER(HardFault_Handler) {
//Copy to local variables (not pointers) to allow GDB "i loc" to directly show the info
struct port_extctx ctx;
volatile unsigned long _CFSR ;
volatile unsigned long _HFSR ;
volatile unsigned long _DFSR ;
volatile unsigned long _AFSR ;
volatile unsigned long _BFAR ;
volatile unsigned long _MMAR ;
//Get thread context. Contains main registers including PC and LR
memcpy(&ctx, (void*)__get_PSP(), sizeof(struct port_extctx));
// Copy to local variables (not pointers) to allow GDB "i loc" to directly show the info
struct port_extctx ctx;
volatile unsigned long _CFSR;
volatile unsigned long _HFSR;
volatile unsigned long _DFSR;
volatile unsigned long _AFSR;
volatile unsigned long _BFAR;
volatile unsigned long _MMAR;
// Get thread context. Contains main registers including PC and LR
memcpy(&ctx, (void *)__get_PSP(), sizeof(struct port_extctx));
(void)ctx;
// Configurable Fault Status Register
// Consists of MMSR, BFSR and UFSR
_CFSR = (*((volatile unsigned long *)(0xE000ED28))); (void)(_CFSR);
_CFSR = (*((volatile unsigned long *)(0xE000ED28)));
(void)(_CFSR);
// Hard Fault Status Register
_HFSR = (*((volatile unsigned long *)(0xE000ED2C))); (void)(_HFSR);
_HFSR = (*((volatile unsigned long *)(0xE000ED2C)));
(void)(_HFSR);
// Debug Fault Status Register
_DFSR = (*((volatile unsigned long *)(0xE000ED30))); (void)(_DFSR);
_DFSR = (*((volatile unsigned long *)(0xE000ED30)));
(void)(_DFSR);
// Auxiliary Fault Status Register
_AFSR = (*((volatile unsigned long *)(0xE000ED3C))); (void)(_AFSR);
_AFSR = (*((volatile unsigned long *)(0xE000ED3C)));
(void)(_AFSR);
// Read the Fault Address Registers. These may not contain valid values.
// Check BFARVALID/MMARVALID to see if they are valid values
// MemManage Fault Address Register
_MMAR = (*((volatile unsigned long *)(0xE000ED34))) ; (void)(_MMAR);
_MMAR = (*((volatile unsigned long *)(0xE000ED34)));
(void)(_MMAR);
// Bus Fault Address Register
_BFAR = (*((volatile unsigned long *)(0xE000ED38))); (void)(_BFAR);
//Cause debugger to stop. Ignored if no debugger is attached
_BFAR = (*((volatile unsigned long *)(0xE000ED38)));
(void)(_BFAR);
// Cause debugger to stop. Ignored if no debugger is attached
bkpt();
NVIC_SystemReset(); // If no debugger connected, just reset the board
NVIC_SystemReset(); // If no debugger connected, just reset the board
}
+2
View File
@@ -36,6 +36,7 @@ enum anne_pro_layers {
* | | `~ | | | | LEFT | DOWN | RIGHT |
* \-----------------------------------------------------------------------------------------/
*/
// clang-format off
const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_BASE_LAYER] = KEYMAP( /* Base */
KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC,
@@ -89,6 +90,7 @@ enum anne_pro_layers {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_FN1_LY), MO(_FN2_LY), KC_TRNS
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
@@ -4,11 +4,13 @@
#include "config.h"
enum anne_pro_layers {
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
};
// clang-format off
// Key symbols are based on QMK. Use them to remap your keyboard
/*
* Layer _BASE_LAYER
@@ -89,15 +91,14 @@ enum anne_pro_layers {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_FN1_LAYER), MO(_FN2_LAYER), KC_TRNS
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
void matrix_init_user(void) {}
void matrix_init_user(void) {
}
void matrix_scan_user(void) {
}
void matrix_scan_user(void) {}
// Code to run after initializing the keyboard
void keyboard_post_init_user(void) {
@@ -111,19 +112,16 @@ void keyboard_post_init_user(void) {
// annepro2LedSetProfile(i);
}
layer_state_t layer_state_set_user(layer_state_t layer) {
return layer;
}
layer_state_t layer_state_set_user(layer_state_t layer) { return layer; }
// The function to handle the caps lock logic
bool led_update_user(led_t leds) {
if (leds.caps_lock) {
// Set the leds to red
annepro2LedSetForegroundColor(0xFF, 0x00, 0x00);
} else {
annepro2LedResetForegroundColor();
}
if (leds.caps_lock) {
// Set the leds to red
annepro2LedSetForegroundColor(0xFF, 0x00, 0x00);
} else {
annepro2LedResetForegroundColor();
}
return true;
return true;
}
@@ -4,11 +4,13 @@
#include "config.h"
enum anne_pro_layers {
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
};
// clang-format off
// Key symbols are based on QMK. Use them to remap your keyboard
/*
* Layer _BASE_LAYER
@@ -89,15 +91,13 @@ enum anne_pro_layers {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_FN1_LAYER), MO(_FN2_LAYER), KC_TRNS
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
void matrix_init_user(void) {}
void matrix_init_user(void) {
}
void matrix_scan_user(void) {
}
void matrix_scan_user(void) {}
// Code to run after initializing the keyboard
void keyboard_post_init_user(void) {
@@ -112,50 +112,40 @@ void keyboard_post_init_user(void) {
}
layer_state_t layer_state_set_user(layer_state_t layer) {
switch(get_highest_layer(layer)) {
case _FN1_LAYER:
// Set the leds to green
annepro2LedSetForegroundColor(0x00, 0xFF, 0x00);
break;
case _FN2_LAYER:
// Set the leds to blue
annepro2LedSetForegroundColor(0x00, 0x00, 0xFF);
break;
default:
// Reset back to the current profile
annepro2LedResetForegroundColor();
break;
}
return layer;
switch (get_highest_layer(layer)) {
case _FN1_LAYER:
// Set the leds to green
annepro2LedSetForegroundColor(0x00, 0xFF, 0x00);
break;
case _FN2_LAYER:
// Set the leds to blue
annepro2LedSetForegroundColor(0x00, 0x00, 0xFF);
break;
default:
// Reset back to the current profile
annepro2LedResetForegroundColor();
break;
}
return layer;
}
// The function to handle the caps lock logic
// It's called after the capslock changes state or after entering layers 1 and 2.
bool led_update_user(led_t leds) {
if (leds.caps_lock) {
// Set the caps-lock to red
const annepro2Led_t color = {
.p.red = 0xff,
.p.green = 0x00,
.p.blue = 0x00,
.p.alpha = 0xff
};
if (leds.caps_lock) {
// Set the caps-lock to red
const annepro2Led_t color = {.p.red = 0xff, .p.green = 0x00, .p.blue = 0x00, .p.alpha = 0xff};
annepro2LedMaskSetKey(2, 0, color);
/* NOTE: Instead of colouring the capslock only, you can change the whole
keyboard with annepro2LedSetForegroundColor */
} else {
// Reset the capslock if there is no layer active
if(!layer_state_is(_FN1_LAYER) && !layer_state_is(_FN2_LAYER)) {
const annepro2Led_t color = {
.p.red = 0xff,
.p.green = 0x00,
.p.blue = 0x00,
.p.alpha = 0x00
};
annepro2LedMaskSetKey(2, 0, color);
annepro2LedMaskSetKey(2, 0, color);
/* NOTE: Instead of colouring the capslock only, you can change the whole
keyboard with annepro2LedSetForegroundColor */
} else {
// Reset the capslock if there is no layer active
if (!layer_state_is(_FN1_LAYER) && !layer_state_is(_FN2_LAYER)) {
const annepro2Led_t color = {.p.red = 0xff, .p.green = 0x00, .p.blue = 0x00, .p.alpha = 0x00};
annepro2LedMaskSetKey(2, 0, color);
}
}
}
return true;
return true;
}
+8 -12
View File
@@ -4,11 +4,12 @@
#include "config.h"
enum anne_pro_layers {
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
};
// clang-format off
// Key symbols are based on QMK. Use them to remap your keyboard
/*
* Layer _BASE_LAYER
@@ -89,15 +90,12 @@ enum anne_pro_layers {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_FN1_LAYER), MO(_FN2_LAYER), KC_TRNS
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
void matrix_init_user(void) {}
void matrix_init_user(void) {
}
void matrix_scan_user(void) {
}
void matrix_scan_user(void) {}
// Code to run after initializing the keyboard
void keyboard_post_init_user(void) {
@@ -111,6 +109,4 @@ void keyboard_post_init_user(void) {
// annepro2LedSetProfile(i);
}
layer_state_t layer_state_set_user(layer_state_t layer) {
return layer;
}
layer_state_t layer_state_set_user(layer_state_t layer) { return layer; }
+8 -13
View File
@@ -3,11 +3,11 @@
#include "qmk_ap2_led.h"
enum anne_pro_layers {
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
_BASE_LAYER,
_FN1_LAYER,
_FN2_LAYER,
};
// clang-format off
/*
* Layer _BASE_LAYER
* ,-----------------------------------------------------------------------------------------.
@@ -76,16 +76,11 @@ enum anne_pro_layers {
KC_TRNS, KC_TRNS, KC_TRNS, KC_ENT, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
void matrix_init_user(void) {}
void matrix_init_user(void) {
void matrix_scan_user(void) {}
}
void matrix_scan_user(void) {
}
layer_state_t layer_state_set_user(layer_state_t layer) {
return layer;
}
layer_state_t layer_state_set_user(layer_state_t layer) { return layer; }
+53 -62
View File
@@ -3,7 +3,7 @@
#include <print.h>
#include "qmk_ap2_led.h"
#ifdef ANNEPRO2_C18
#include "eeprom_w25x20cl.h"
# include "eeprom_w25x20cl.h"
#endif
// layout using eeprom and bidir-comms to keep user led settings persistent
@@ -13,7 +13,7 @@ typedef union {
uint32_t raw;
struct {
uint8_t magic : 8;
bool leds_on : 1;
bool leds_on : 1;
uint8_t leds_profile : 8;
};
} user_config_t;
@@ -29,7 +29,7 @@ enum anne_pro_layers {
_FN1_LAYER,
_FN2_LAYER,
};
// clang-format off
/*
* Layer _BASE_LAYER
* ,-----------------------------------------------------------------------------------------.
@@ -106,91 +106,82 @@ const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_INS, KC_DEL, KC_TRNS,
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, MO(_FN2_LAYER), MO(_FN1_LAYER), KC_TRNS),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
void matrix_init_user(void)
{
}
void matrix_init_user(void) {}
void matrix_scan_user(void)
{
}
void matrix_scan_user(void) {}
layer_state_t layer_state_set_user(layer_state_t layer)
{
return layer;
}
layer_state_t layer_state_set_user(layer_state_t layer) { return layer; }
void raw_hid_receive(uint8_t *data, uint8_t length) {
uprintf("raw_hid len: %u\n", length);
if (length == 1)
annepro2LedSetProfile(data[0]);
else {
for (uint8_t i = 0; i < length; i++){
usb_buf[buf_fil + i] = data[i];
void raw_hid_receive(uint8_t* data, uint8_t length) {
uprintf("raw_hid len: %u\n", length);
if (length == 1)
annepro2LedSetProfile(data[0]);
else {
for (uint8_t i = 0; i < length; i++) {
usb_buf[buf_fil + i] = data[i];
}
buf_fil += length;
if (buf_fil >= 211) {
sdWrite(&SD0, usb_buf, 211);
buf_fil = 0;
}
// for (int i = 0; i < length; i++) {
// sdPut(&SD0, data[i]);
// sdGet(&SD0);
// }
}
buf_fil += length;
if (buf_fil >= 211) {
sdWrite(&SD0, usb_buf, 211);
buf_fil = 0;
}
// for (int i = 0; i < length; i++) {
// sdPut(&SD0, data[i]);
// sdGet(&SD0);
// }
}
}
/*!
* @returns false processing for this keycode has been completed.
*/
bool process_record_user(uint16_t keycode, keyrecord_t* record)
{
bool process_record_user(uint16_t keycode, keyrecord_t* record) {
#ifdef ANNEPRO2_C18
switch (keycode) {
case KC_AP_LED_OFF:
if (record->event.pressed) {
user_config.leds_on = false;
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
case KC_AP_LED_ON:
if (record->event.pressed) {
user_config.leds_on = true;
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
case KC_AP_LED_NEXT_PROFILE:
if (record->event.pressed) {
user_config.leds_profile = (user_config.leds_profile + 1) % annepro2LedStatus.amountOfProfiles;
annepro2LedSetProfile(user_config.leds_profile);
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
default:
break;
case KC_AP_LED_OFF:
if (record->event.pressed) {
user_config.leds_on = false;
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
case KC_AP_LED_ON:
if (record->event.pressed) {
user_config.leds_on = true;
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
case KC_AP_LED_NEXT_PROFILE:
if (record->event.pressed) {
user_config.leds_profile = (user_config.leds_profile + 1) % annepro2LedStatus.amountOfProfiles;
annepro2LedSetProfile(user_config.leds_profile);
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
return false;
default:
break;
}
#endif
return true;
}
void keyboard_post_init_user(void)
{
void keyboard_post_init_user(void) {
// Customize these values to desired behavior
debug_enable = true;
//debug_matrix = true;
//debug_keyboard=true;
//debug_mouse=true;
// debug_matrix = true;
// debug_keyboard=true;
// debug_mouse=true;
#ifdef ANNEPRO2_C18
// Read the user config from EEPROM
eeprom_read((void*)&user_config, 0, sizeof(user_config_t));
// initialize a new eeprom
if (user_config.magic != 0xDE)
{
user_config.magic = 0xDE;
user_config.leds_on = false;
if (user_config.magic != 0xDE) {
user_config.magic = 0xDE;
user_config.leds_on = false;
user_config.leds_profile = 0;
eeprom_write((void*)&user_config, 0, sizeof(user_config_t));
}
+180 -215
View File
@@ -4,60 +4,32 @@
#include "config.h"
enum anne_pro_layers {
_BASE_LAYER,
_MOUSE_LAYER,
_FUNCTION_LAYER,
_MEDIA_AND_NAVIGATION_LAYER,
_NUMPAD_LAYER,
_BASE_LAYER,
_MOUSE_LAYER,
_FUNCTION_LAYER,
_MEDIA_AND_NAVIGATION_LAYER,
_NUMPAD_LAYER,
};
typedef struct {
bool is_press_action;
bool is_press_action;
uint8_t state;
} tap;
// Define a type for as many tap dance states as you need
enum {
SINGLE_TAP = 1,
SINGLE_HOLD,
DOUBLE_TAP,
DOUBLE_HOLD,
TRIPLE_TAP,
TRIPLE_HOLD
};
enum { SINGLE_TAP = 1, SINGLE_HOLD, DOUBLE_TAP, DOUBLE_HOLD, TRIPLE_TAP, TRIPLE_HOLD };
enum profile {
RED,
GREEN,
BLUE,
RAINBOWHORIZONTAL,
RAINBOWVERTICAL,
ANIMATEDRAINBOWVERTICAL,
ANIMATEDRAINBOWFLOW,
ANIMATEDRAINBOWWATERFALL,
ANIMATEDBREATHING,
ANIMATEDSPECTRUM
};
enum profile { RED, GREEN, BLUE, RAINBOWHORIZONTAL, RAINBOWVERTICAL, ANIMATEDRAINBOWVERTICAL, ANIMATEDRAINBOWFLOW, ANIMATEDRAINBOWWATERFALL, ANIMATEDBREATHING, ANIMATEDSPECTRUM };
uint8_t cyclabe_profiles[] = {
IDLE_PROFILE_INDEX,
ANIMATEDRAINBOWFLOW,
ANIMATEDRAINBOWVERTICAL,
ANIMATEDRAINBOWWATERFALL,
ANIMATEDBREATHING,
ANIMATEDSPECTRUM
};
uint8_t cyclabe_profiles[] = {IDLE_PROFILE_INDEX, ANIMATEDRAINBOWFLOW, ANIMATEDRAINBOWVERTICAL, ANIMATEDRAINBOWWATERFALL, ANIMATEDBREATHING, ANIMATEDSPECTRUM};
enum custom_codes {
NEXT_PROFILE = AP2_SAFE_RANGE,
ENABLE_OR_DISABLE_LEDS
};
enum custom_codes { NEXT_PROFILE = AP2_SAFE_RANGE, ENABLE_OR_DISABLE_LEDS };
enum {
ESC_TAP_DANCE,
GRV_TAP_DANCE,
};
// clang-format off
/*
* Layer _BASE_LAYER
* ,-----------------------------------------------------------------------------------------.
@@ -179,241 +151,234 @@ enum {
_______, _______, _______, KC_BTN1, _______, _______, _______, _______
),
};
// clang-format on
const uint16_t keymaps_size = sizeof(keymaps);
// Declare the functions to be used with your tap dance key(s)
// Function associated with all tap dances
uint8_t cur_dance(qk_tap_dance_state_t *state);
// Functions associated with individual tap dances
void enableProfileColor(uint8_t * profile);
void enableProfileColor(uint8_t *profile);
void resetProfileColor(void);
void esc_layer_finished(qk_tap_dance_state_t *state, void *user_data);
void esc_layer_reset(qk_tap_dance_state_t *state, void *user_data);
bool is_caps_set = false;
bool is_led_on = true;
bool is_caps_set = false;
bool is_led_on = true;
uint8_t base_profile = IDLE_PROFILE_INDEX;
uint8_t idle_profile[] = {0x00,0x00,0x00};
uint8_t caps_profile[] = {0xFF,0x00,0x00};
uint8_t function_profile[] = {0x00,0xFF,0x00};
uint8_t navigation_profile[] = {0x44,0x00,0xFF};
uint8_t numpad_profile[] = {0xFF,0xDD,0x00};
uint8_t mouse_profile[] = {0x00,0x88,0xFF};
uint8_t idle_profile[] = {0x00, 0x00, 0x00};
uint8_t caps_profile[] = {0xFF, 0x00, 0x00};
uint8_t function_profile[] = {0x00, 0xFF, 0x00};
uint8_t navigation_profile[] = {0x44, 0x00, 0xFF};
uint8_t numpad_profile[] = {0xFF, 0xDD, 0x00};
uint8_t mouse_profile[] = {0x00, 0x88, 0xFF};
void matrix_init_user(void) {
void matrix_init_user(void) {}
}
void matrix_scan_user(void) {
}
void matrix_scan_user(void) {}
void keyboard_post_init_user(void) {
annepro2LedEnable();
resetProfileColor();
annepro2LedEnable();
resetProfileColor();
}
// The function to handle the caps lock logic
bool led_update_user(led_t leds) {
if (leds.caps_lock) {
is_caps_set = true;
enableProfileColor(caps_profile);
return true;
} else if(is_caps_set) {
is_caps_set = false;
resetProfileColor();
}
if (leds.caps_lock) {
is_caps_set = true;
enableProfileColor(caps_profile);
return true;
} else if (is_caps_set) {
is_caps_set = false;
resetProfileColor();
}
return true;
return true;
}
layer_state_t layer_state_set_user(layer_state_t state) {
switch(get_highest_layer(state)) {
case _FUNCTION_LAYER:
enableProfileColor(function_profile);
break;
case _NUMPAD_LAYER:
enableProfileColor(numpad_profile);
break;
case _MOUSE_LAYER:
enableProfileColor(mouse_profile);
break;
case _MEDIA_AND_NAVIGATION_LAYER:
enableProfileColor(navigation_profile);
break;
default:
resetProfileColor();
break;
}
switch (get_highest_layer(state)) {
case _FUNCTION_LAYER:
enableProfileColor(function_profile);
break;
case _NUMPAD_LAYER:
enableProfileColor(numpad_profile);
break;
case _MOUSE_LAYER:
enableProfileColor(mouse_profile);
break;
case _MEDIA_AND_NAVIGATION_LAYER:
enableProfileColor(navigation_profile);
break;
default:
resetProfileColor();
break;
}
return state;
return state;
}
// Determine the current tap dance state
uint8_t cur_dance(qk_tap_dance_state_t *state) {
if (state->count == 1) {
if (!state->pressed) return SINGLE_TAP;
else return SINGLE_HOLD;
} else if (state->count == 2) {
if (!state->pressed) return DOUBLE_TAP;
else return DOUBLE_HOLD;
} else if (state->count == 3) {
if (!state->pressed) return TRIPLE_TAP;
else return TRIPLE_HOLD;
} else return 8;
if (state->count == 1) {
if (!state->pressed)
return SINGLE_TAP;
else
return SINGLE_HOLD;
} else if (state->count == 2) {
if (!state->pressed)
return DOUBLE_TAP;
else
return DOUBLE_HOLD;
} else if (state->count == 3) {
if (!state->pressed)
return TRIPLE_TAP;
else
return TRIPLE_HOLD;
} else
return 8;
}
// Initialize tap structure associated with example tap dance key
static tap esc_tap_state = {
.is_press_action = true,
.state = 0
};
static tap esc_tap_state = {.is_press_action = true, .state = 0};
static tap grav_tap_state = {
.is_press_action = true,
.state = 0
};
static tap grav_tap_state = {.is_press_action = true, .state = 0};
void enableProfileColor (uint8_t * profile) {
if(is_caps_set) {
annepro2LedSetForegroundColor(caps_profile[0], caps_profile[1], caps_profile[2]);
} else {
annepro2LedSetForegroundColor(profile[0], profile[1], profile[2]);
}
void enableProfileColor(uint8_t *profile) {
if (is_caps_set) {
annepro2LedSetForegroundColor(caps_profile[0], caps_profile[1], caps_profile[2]);
} else {
annepro2LedSetForegroundColor(profile[0], profile[1], profile[2]);
}
}
void resetProfileColor(void) {
if(is_caps_set) {
annepro2LedSetForegroundColor(caps_profile[0], caps_profile[1], caps_profile[2]);
} else if(base_profile == IDLE_PROFILE_INDEX) {
annepro2LedSetForegroundColor(idle_profile[0], idle_profile[1], idle_profile[2]);
} else {
annepro2LedSetProfile(cyclabe_profiles[base_profile]);
}
if (is_caps_set) {
annepro2LedSetForegroundColor(caps_profile[0], caps_profile[1], caps_profile[2]);
} else if (base_profile == IDLE_PROFILE_INDEX) {
annepro2LedSetForegroundColor(idle_profile[0], idle_profile[1], idle_profile[2]);
} else {
annepro2LedSetProfile(cyclabe_profiles[base_profile]);
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case NEXT_PROFILE:
if (record->event.pressed) {
base_profile++;
if(base_profile >= (sizeof(cyclabe_profiles)/sizeof(cyclabe_profiles[0])))
base_profile = IDLE_PROFILE_INDEX;
switch (keycode) {
case NEXT_PROFILE:
if (record->event.pressed) {
base_profile++;
if (base_profile >= (sizeof(cyclabe_profiles) / sizeof(cyclabe_profiles[0]))) base_profile = IDLE_PROFILE_INDEX;
if(base_profile == IDLE_PROFILE_INDEX) {
annepro2LedSetForegroundColor(idle_profile[0], idle_profile[1], idle_profile[2]);
} else {
annepro2LedSetProfile(cyclabe_profiles[base_profile]);
}
}
return true;
case ENABLE_OR_DISABLE_LEDS:
if (record->event.pressed) {
if(is_led_on) {
is_led_on = false;
annepro2LedDisable();
} else {
annepro2LedEnable();
is_led_on = true;
}
}
return true;
default:
return true;
}
if (base_profile == IDLE_PROFILE_INDEX) {
annepro2LedSetForegroundColor(idle_profile[0], idle_profile[1], idle_profile[2]);
} else {
annepro2LedSetProfile(cyclabe_profiles[base_profile]);
}
}
return true;
case ENABLE_OR_DISABLE_LEDS:
if (record->event.pressed) {
if (is_led_on) {
is_led_on = false;
annepro2LedDisable();
} else {
annepro2LedEnable();
is_led_on = true;
}
}
return true;
default:
return true;
}
}
// Functions that control what our tap dance key does
void esc_layer_finished(qk_tap_dance_state_t *state, void *user_data) {
esc_tap_state.state = cur_dance(state);
esc_tap_state.state = cur_dance(state);
switch (esc_tap_state.state) {
case SINGLE_TAP:
tap_code(KC_ESC);
break;
case SINGLE_HOLD:
layer_on(_FUNCTION_LAYER);
break;
case DOUBLE_TAP:
if (layer_state_is(_MOUSE_LAYER) || layer_state_is(_NUMPAD_LAYER)) {
layer_off(_MOUSE_LAYER);
layer_off(_NUMPAD_LAYER);
} else {
tap_code(KC_ESC);
tap_code(KC_ESC);
}
break;
case DOUBLE_HOLD:
layer_on(_NUMPAD_LAYER);
break;
case TRIPLE_TAP:
if (layer_state_is(_MOUSE_LAYER)) {
layer_off(_MOUSE_LAYER);
} else {
layer_on(_MOUSE_LAYER);
}
break;
case TRIPLE_HOLD:
layer_on(_MOUSE_LAYER);
break;
}
switch (esc_tap_state.state) {
case SINGLE_TAP:
tap_code(KC_ESC);
break;
case SINGLE_HOLD:
layer_on(_FUNCTION_LAYER);
break;
case DOUBLE_TAP:
if (layer_state_is(_MOUSE_LAYER) || layer_state_is(_NUMPAD_LAYER)) {
layer_off(_MOUSE_LAYER);
layer_off(_NUMPAD_LAYER);
} else {
tap_code(KC_ESC);
tap_code(KC_ESC);
}
break;
case DOUBLE_HOLD:
layer_on(_NUMPAD_LAYER);
break;
case TRIPLE_TAP:
if (layer_state_is(_MOUSE_LAYER)) {
layer_off(_MOUSE_LAYER);
} else {
layer_on(_MOUSE_LAYER);
}
break;
case TRIPLE_HOLD:
layer_on(_MOUSE_LAYER);
break;
}
}
void esc_layer_reset(qk_tap_dance_state_t *state, void *user_data) {
// If the key was held down and now is released then switch off the layer
if (esc_tap_state.state == SINGLE_HOLD) {
layer_off(_FUNCTION_LAYER);
}
if (esc_tap_state.state == DOUBLE_HOLD) {
layer_off(_NUMPAD_LAYER);
}
if (esc_tap_state.state == TRIPLE_HOLD) {
layer_off(_MOUSE_LAYER);
}
esc_tap_state.state = 0;
// If the key was held down and now is released then switch off the layer
if (esc_tap_state.state == SINGLE_HOLD) {
layer_off(_FUNCTION_LAYER);
}
if (esc_tap_state.state == DOUBLE_HOLD) {
layer_off(_NUMPAD_LAYER);
}
if (esc_tap_state.state == TRIPLE_HOLD) {
layer_off(_MOUSE_LAYER);
}
esc_tap_state.state = 0;
}
// Functions that control what our tap dance key does
void grave_layer_finished(qk_tap_dance_state_t *state, void *user_data) {
grav_tap_state.state = cur_dance(state);
grav_tap_state.state = cur_dance(state);
switch (grav_tap_state.state) {
case SINGLE_TAP:
tap_code(KC_GRV);
break;
case SINGLE_HOLD:
tap_code(KC_GRV);
break;
case DOUBLE_TAP:
// Check to see if the layer is already set
if (layer_state_is(_MOUSE_LAYER)) {
// If already set, then switch it off
layer_off(_MOUSE_LAYER);
} else {
// If not already set, then switch the layer on
layer_on(_MOUSE_LAYER);
}
break;
}
switch (grav_tap_state.state) {
case SINGLE_TAP:
tap_code(KC_GRV);
break;
case SINGLE_HOLD:
tap_code(KC_GRV);
break;
case DOUBLE_TAP:
// Check to see if the layer is already set
if (layer_state_is(_MOUSE_LAYER)) {
// If already set, then switch it off
layer_off(_MOUSE_LAYER);
} else {
// If not already set, then switch the layer on
layer_on(_MOUSE_LAYER);
}
break;
}
}
void grave_layer_reset(qk_tap_dance_state_t *state, void *user_data) {
// If the key was held down and now is released then switch off the layer
if (grav_tap_state.state == SINGLE_HOLD) {
layer_off(_FUNCTION_LAYER);
}
if (grav_tap_state.state == DOUBLE_HOLD) {
layer_off(_NUMPAD_LAYER);
}
grav_tap_state.state = 0;
// If the key was held down and now is released then switch off the layer
if (grav_tap_state.state == SINGLE_HOLD) {
layer_off(_FUNCTION_LAYER);
}
if (grav_tap_state.state == DOUBLE_HOLD) {
layer_off(_NUMPAD_LAYER);
}
grav_tap_state.state = 0;
}
// Associate our tap dance key with its functionality
qk_tap_dance_action_t tap_dance_actions[] = {
[ESC_TAP_DANCE] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, esc_layer_finished, esc_layer_reset, 250),
[GRV_TAP_DANCE] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, grave_layer_finished, grave_layer_reset, 300)
};
qk_tap_dance_action_t tap_dance_actions[] = {[ESC_TAP_DANCE] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, esc_layer_finished, esc_layer_reset, 250), [GRV_TAP_DANCE] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, grave_layer_finished, grave_layer_reset, 300)};
@@ -18,9 +18,9 @@ MCU = cortex-m0plus
ARMV = 6
USE_FPU = no
MCU_FAMILY = HT32
MCU_SERIES = HT32F523x2
MCU_SERIES = HT32F523xx
MCU_LDSCRIPT = HT32F52342_ANNEPRO2
MCU_STARTUP = ht32f523x2
MCU_STARTUP = ht32f523xx
BOARD = ANNEPRO2_C18
+15 -16
View File
@@ -35,33 +35,32 @@
// 48 MHz to UART
// 48 MHz to USB
#define HT32_CK_HSE_FREQUENCY 8000000UL // 8 MHz
#define HT32_CKCU_SW CKCU_GCCR_SW_PLL
#define HT32_PLL_USE_HSE TRUE
#define HT32_PLL_FBDIV 6 // 8 MHz -> 48 MHz
#define HT32_PLL_OTDIV 0
#define HT32_AHB_PRESCALER 1 // 48 MHz -> 48 MHz
#define HT32_USART_PRESCALER 1 // 48 MHz
#define HT32_USB_PRESCALER 1 // 48 MHz -> 48 MHz
#define HT32_CK_HSE_FREQUENCY 8000000UL // 8 MHz
#define HT32_CKCU_SW CKCU_GCCR_SW_PLL
#define HT32_PLL_USE_HSE TRUE
#define HT32_PLL_FBDIV 6 // 8 MHz -> 48 MHz
#define HT32_PLL_OTDIV 0
#define HT32_AHB_PRESCALER 1 // 48 MHz -> 48 MHz
#define HT32_USART_PRESCALER 1 // 48 MHz
#define HT32_USB_PRESCALER 1 // 48 MHz -> 48 MHz
// SysTick uses processor clock at 48MHz
#define HT32_ST_USE_HCLK TRUE
#define HT32_ST_USE_HCLK TRUE
/*
* Peripheral driver settings
*/
#define HT32_SERIAL_USE_USART0 TRUE
#define HT32_USART0_IRQ_PRIORITY 6
#define HT32_SERIAL_USE_USART0 TRUE
#define HT32_USART0_IRQ_PRIORITY 6
#define HT32_SERIAL_USE_USART1 TRUE
#define HT32_USART1_IRQ_PRIORITY 7
#define HT32_SERIAL_USE_USART1 TRUE
#define HT32_USART1_IRQ_PRIORITY 7
/*
* USB driver settings
*/
#define HT32_USB_USE_USB0 TRUE
#define HT32_USB_USB0_IRQ_PRIORITY 5
#define HT32_USB_USE_USB0 TRUE
#define HT32_USB_USB0_IRQ_PRIORITY 5
#endif /* _MCUCONF_H_ */
+72 -74
View File
@@ -21,98 +21,96 @@
protocol_t proto;
void protoInit(protocol_t *proto, void (*callback)(const message_t *)) {
proto->previousId = 0;
proto->callback = callback;
proto->state = STATE_SYNC_1;
proto->errors = 0;
proto->previousId = 0;
proto->callback = callback;
proto->state = STATE_SYNC_1;
proto->errors = 0;
}
static uint8_t msgId = 0;
void protoTx(uint8_t cmd, const unsigned char *buf, int payloadSize,
int retries) {
chDbgCheck(payloadSize <= MAX_PAYLOAD_SIZE);
void protoTx(uint8_t cmd, const unsigned char *buf, int payloadSize, int retries) {
chDbgCheck(payloadSize <= MAX_PAYLOAD_SIZE);
const uint8_t header[5] = {
0x7A, 0x1D, cmd, ++msgId, payloadSize,
};
const uint8_t header[5] = {
0x7A, 0x1D, cmd, ++msgId, payloadSize,
};
/* We don't implement ACKs, yet some messages should not be lost. */
for (int i = 0; i < retries; i++) {
sdWrite(&PROTOCOL_SD, header, sizeof(header));
if (payloadSize)
sdWrite(&PROTOCOL_SD, buf, payloadSize);
}
/* We don't implement ACKs, yet some messages should not be lost. */
for (int i = 0; i < retries; i++) {
sdWrite(&PROTOCOL_SD, header, sizeof(header));
if (payloadSize) sdWrite(&PROTOCOL_SD, buf, payloadSize);
}
}
static inline void messageReceived(protocol_t *proto) {
if (proto->buffer.msgId != proto->previousId) {
/* It's not a resend / duplicate */
proto->callback(&proto->buffer);
proto->previousId = proto->buffer.msgId;
}
proto->state = STATE_SYNC_1;
if (proto->buffer.msgId != proto->previousId) {
/* It's not a resend / duplicate */
proto->callback(&proto->buffer);
proto->previousId = proto->buffer.msgId;
}
proto->state = STATE_SYNC_1;
}
void protoConsume(protocol_t *proto, uint8_t byte) {
switch (proto->state) {
case STATE_SYNC_1:
if (byte == 0x7A) {
proto->state = STATE_SYNC_2;
} else {
proto->errors++;
}
return;
switch (proto->state) {
case STATE_SYNC_1:
if (byte == 0x7A) {
proto->state = STATE_SYNC_2;
} else {
proto->errors++;
}
return;
case STATE_SYNC_2:
if (byte == 0x1D) {
proto->state = STATE_CMD;
} else {
proto->state = STATE_SYNC_1;
proto->errors++;
}
return;
case STATE_SYNC_2:
if (byte == 0x1D) {
proto->state = STATE_CMD;
} else {
proto->state = STATE_SYNC_1;
proto->errors++;
}
return;
case STATE_CMD:
proto->buffer.command = byte;
proto->state = STATE_ID;
return;
case STATE_CMD:
proto->buffer.command = byte;
proto->state = STATE_ID;
return;
case STATE_ID:
proto->buffer.msgId = byte;
proto->state = STATE_PAYLOAD_SIZE;
return;
case STATE_ID:
proto->buffer.msgId = byte;
proto->state = STATE_PAYLOAD_SIZE;
return;
case STATE_PAYLOAD_SIZE:
proto->buffer.payloadSize = byte;
if (proto->buffer.payloadSize > MAX_PAYLOAD_SIZE) {
proto->buffer.payloadSize = MAX_PAYLOAD_SIZE;
proto->errors++;
}
proto->payloadPosition = 0;
if (proto->buffer.payloadSize == 0) {
/* No payload - whole message received */
messageReceived(proto);
} else {
proto->state = STATE_PAYLOAD;
}
return;
case STATE_PAYLOAD_SIZE:
proto->buffer.payloadSize = byte;
if (proto->buffer.payloadSize > MAX_PAYLOAD_SIZE) {
proto->buffer.payloadSize = MAX_PAYLOAD_SIZE;
proto->errors++;
}
proto->payloadPosition = 0;
if (proto->buffer.payloadSize == 0) {
/* No payload - whole message received */
messageReceived(proto);
} else {
proto->state = STATE_PAYLOAD;
}
return;
case STATE_PAYLOAD:
/* NOTE: This could be read with sdReadTimeout probably, but that breaks
* abstraction */
proto->buffer.payload[proto->payloadPosition] = byte;
proto->payloadPosition++;
if (proto->payloadPosition == proto->buffer.payloadSize) {
/* Payload read - message received */
messageReceived(proto);
case STATE_PAYLOAD:
/* NOTE: This could be read with sdReadTimeout probably, but that breaks
* abstraction */
proto->buffer.payload[proto->payloadPosition] = byte;
proto->payloadPosition++;
if (proto->payloadPosition == proto->buffer.payloadSize) {
/* Payload read - message received */
messageReceived(proto);
}
return;
}
return;
}
}
void protoSilence(protocol_t *proto) {
if (proto->state != STATE_SYNC_1) {
proto->state = STATE_SYNC_1;
proto->errors++;
}
if (proto->state != STATE_SYNC_1) {
proto->state = STATE_SYNC_1;
proto->errors++;
}
}
+56 -57
View File
@@ -13,43 +13,43 @@
#define PROTOCOL_SD SD0
enum {
/*
* Main -> LED
*/
/* Basic config */
CMD_LED_ON = 0x01,
CMD_LED_OFF = 0x02,
/*
* Main -> LED
*/
/* Basic config */
CMD_LED_ON = 0x01,
CMD_LED_OFF = 0x02,
CMD_LED_SET_PROFILE = 0x03,
CMD_LED_NEXT_PROFILE = 0x04,
CMD_LED_PREV_PROFILE = 0x05,
CMD_LED_SET_PROFILE = 0x03,
CMD_LED_NEXT_PROFILE = 0x04,
CMD_LED_PREV_PROFILE = 0x05,
CMD_LED_NEXT_INTENSITY = 0x06,
CMD_LED_NEXT_ANIMATION_SPEED = 0x07,
CMD_LED_NEXT_INTENSITY = 0x06,
CMD_LED_NEXT_ANIMATION_SPEED = 0x07,
/* Masks */
/* Override a key color, eg. capslock */
CMD_LED_MASK_SET_KEY = 0x10,
/* Override all keys in a row with configurable colors */
CMD_LED_MASK_SET_ROW = 0x11,
/* Masks */
/* Override a key color, eg. capslock */
CMD_LED_MASK_SET_KEY = 0x10,
/* Override all keys in a row with configurable colors */
CMD_LED_MASK_SET_ROW = 0x11,
/* Override all keys with single color (eg. foreground color) */
CMD_LED_MASK_SET_MONO = 0x12,
/* Override all keys with single color (eg. foreground color) */
CMD_LED_MASK_SET_MONO = 0x12,
/* Reactive / status */
CMD_LED_GET_STATUS = 0x20,
CMD_LED_KEY_BLINK = 0x21,
CMD_LED_KEY_DOWN = 0x22,
CMD_LED_KEY_UP = 0x23, /* TODO */
CMD_LED_IAP = 0x24,
/* Reactive / status */
CMD_LED_GET_STATUS = 0x20,
CMD_LED_KEY_BLINK = 0x21,
CMD_LED_KEY_DOWN = 0x22,
CMD_LED_KEY_UP = 0x23, /* TODO */
CMD_LED_IAP = 0x24,
/* LED -> Main */
/* Payload with data to send over HID */
CMD_LED_DEBUG = 0x40,
/* LED -> Main */
/* Payload with data to send over HID */
CMD_LED_DEBUG = 0x40,
/* Number of profiles, current profile, on/off state,
reactive flag, brightness, errors */
CMD_LED_STATUS = 0x41,
/* Number of profiles, current profile, on/off state,
reactive flag, brightness, errors */
CMD_LED_STATUS = 0x41,
};
/* 1 ROW * 14 COLS * 4B (RGBX) = 56 + header prefix. */
@@ -57,43 +57,43 @@ enum {
/** Enum of the states used for the serial protocol finite-state automaton */
enum protoState {
/* 2-byte initial start-of-message sync */
STATE_SYNC_1,
STATE_SYNC_2,
/* Waiting for command byte */
STATE_CMD,
/* Waiting for ID byte */
STATE_ID,
/* Waiting for payload size */
STATE_PAYLOAD_SIZE,
/* Reading payload until payloadPosition == payloadSize */
STATE_PAYLOAD,
/* 2-byte initial start-of-message sync */
STATE_SYNC_1,
STATE_SYNC_2,
/* Waiting for command byte */
STATE_CMD,
/* Waiting for ID byte */
STATE_ID,
/* Waiting for payload size */
STATE_PAYLOAD_SIZE,
/* Reading payload until payloadPosition == payloadSize */
STATE_PAYLOAD,
};
/* Buffer holding a single message */
typedef struct {
uint8_t command;
uint8_t msgId;
uint8_t payloadSize;
uint8_t payload[MAX_PAYLOAD_SIZE];
uint8_t command;
uint8_t msgId;
uint8_t payloadSize;
uint8_t payload[MAX_PAYLOAD_SIZE];
} message_t;
/* Internal protocol state */
typedef struct {
/* Callback to call upon receiving a valid message */
void (*callback)(const message_t *);
/* Callback to call upon receiving a valid message */
void (*callback)(const message_t *);
/* Number of read payload bytes */
uint8_t payloadPosition;
/* Number of read payload bytes */
uint8_t payloadPosition;
/* Current finite-state-automata state */
enum protoState state;
/* Current finite-state-automata state */
enum protoState state;
uint8_t previousId;
uint8_t errors;
uint8_t previousId;
uint8_t errors;
/* Currently received message */
message_t buffer;
/* Currently received message */
message_t buffer;
} protocol_t;
/* NOTE: This didn't work when defined on stack */
@@ -109,7 +109,6 @@ extern void protoConsume(protocol_t *proto, uint8_t byte);
extern void protoSilence(protocol_t *proto);
/* Transmit message */
extern void protoTx(uint8_t cmd, const unsigned char *buf, int payloadSize,
int retries);
extern void protoTx(uint8_t cmd, const unsigned char *buf, int payloadSize, int retries);
#endif