Skip to content

Commit

Permalink
Various improvements for the AnnePro2 (qmk#16579)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpe230 authored and zykrah committed Jul 2, 2022
1 parent 8dd4a7d commit 1227890
Show file tree
Hide file tree
Showing 19 changed files with 526 additions and 304 deletions.
92 changes: 58 additions & 34 deletions keyboards/annepro2/annepro2.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,33 @@
#define RAM_MAGIC_LOCATION 0x20001ffc
#define IAP_MAGIC_VALUE 0x0000fab2

static const SerialConfig ledUartInitConfig = {
static const SerialConfig led_uart_init_config = {
.speed = 115200,
};

#ifndef LED_UART_BAUD_RATE
# define LED_UART_BAUD_RATE 115200
#endif // LED_UART_BAUD_RATE

static const SerialConfig ledUartRuntimeConfig = {
static const SerialConfig led_uart_runtine_config = {
.speed = LED_UART_BAUD_RATE,
};

static const SerialConfig bleUartConfig = {
static const SerialConfig ble_uart_config = {
.speed = 115200,
};

static uint8_t ledMcuWakeup[11] = {0x7b, 0x10, 0x43, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x02};
static uint8_t led_mcu_wakeup[11] = {0x7b, 0x10, 0x43, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x02};

ble_capslock_t BLECapsLock = {._dummy = {0}, .caps_lock = false};
ble_capslock_t ble_capslock = {._dummy = {0}, .caps_lock = false};

#ifdef RGB_MATRIX_ENABLE
static uint8_t current_rgb_row = 0;
#endif

void bootloader_jump(void) {
// Send msg to shine to boot into IAP
annepro2SetIAP();
ap2_set_IAP();

// wait for shine to boot into IAP
wait_ms(15);
Expand All @@ -67,27 +71,27 @@ void bootloader_jump(void) {

void keyboard_pre_init_kb(void) {
// Start LED UART
sdStart(&SD0, &ledUartInitConfig);
sdStart(&SD0, &led_uart_init_config);
/* Let the LED chip settle a bit before switching the mode.
* That helped at least one person. */
wait_ms(15);
sdWrite(&SD0, ledMcuWakeup, sizeof(ledMcuWakeup));
sdWrite(&SD0, led_mcu_wakeup, sizeof(led_mcu_wakeup));

// wait to receive response from wakeup
wait_ms(15);

protoInit(&proto, ledCommandCallback);
proto_init(&proto, led_command_callback);

// loop to clear out receive buffer from shine wakeup
while (!sdGetWouldBlock(&SD0)) sdGet(&SD0);

sdStart(&SD0, &ledUartRuntimeConfig);
sdStart(&SD0, &led_uart_runtine_config);
keyboard_pre_init_user();
}

void keyboard_post_init_kb(void) {
// Start BLE UART
sdStart(&SD1, &bleUartConfig);
sdStart(&SD1, &ble_uart_config);
annepro2_ble_startup();

// Give the send uart thread some time to
Expand All @@ -97,7 +101,11 @@ void keyboard_post_init_kb(void) {
// loop to clear out receive buffer from ble wakeup
while (!sdGetWouldBlock(&SD1)) sdGet(&SD1);

annepro2LedGetStatus();
ap2_led_get_status();

#ifdef RGB_MATRIX_ENABLE
ap2_led_enable();
#endif

keyboard_post_init_user();
}
Expand All @@ -106,25 +114,35 @@ void matrix_scan_kb() {
// if there's stuff on the ble serial buffer
// read it into the capslock struct
while (!sdGetWouldBlock(&SD1)) {
sdReadTimeout(&SD1, (uint8_t *)&BLECapsLock, sizeof(ble_capslock_t), 10);
sdReadTimeout(&SD1, (uint8_t *)&ble_capslock, sizeof(ble_capslock_t), 10);
}

/* While there's data from LED keyboard sent - read it. */
while (!sdGetWouldBlock(&SD0)) {
uint8_t byte = sdGet(&SD0);
protoConsume(&proto, byte);
proto_consume(&proto, byte);
}

#ifdef RGB_MATRIX_ENABLE
/* If there's data ready to be sent to LED MCU - send it. */
if(rgb_row_changed[current_rgb_row])
{
rgb_row_changed[current_rgb_row] = 0;
ap2_led_mask_set_row(current_rgb_row);
}
current_rgb_row = (current_rgb_row + 1) % NUM_ROW;
#endif

matrix_scan_user();
}

bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
if (annepro2LedStatus.matrixEnabled && annepro2LedStatus.isReactive) {
annepro2LedForwardKeypress(record->event.key.row, record->event.key.col);
if (ap2_led_status.matrix_enabled && ap2_led_status.is_reactive) {
ap2_led_forward_keypress(record->event.key.row, record->event.key.col);
}

const annepro2Led_t blue = {
const ap2_led_t blue = {
.p.blue = 0xff,
.p.red = 0x00,
.p.green = 0x00,
Expand All @@ -135,22 +153,22 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
case KC_AP2_BT1:
annepro2_ble_broadcast(0);
/* FIXME: This hardcodes col/row position */
annepro2LedBlink(0, 1, blue, 8, 50);
ap2_led_blink(0, 1, blue, 8, 50);
return false;

case KC_AP2_BT2:
annepro2_ble_broadcast(1);
annepro2LedBlink(0, 2, blue, 8, 50);
ap2_led_blink(0, 2, blue, 8, 50);
return false;

case KC_AP2_BT3:
annepro2_ble_broadcast(2);
annepro2LedBlink(0, 3, blue, 8, 50);
ap2_led_blink(0, 3, blue, 8, 50);
return false;

case KC_AP2_BT4:
annepro2_ble_broadcast(3);
annepro2LedBlink(0, 4, blue, 8, 50);
ap2_led_blink(0, 4, blue, 8, 50);
return false;

case KC_AP2_USB:
Expand All @@ -162,37 +180,43 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
return false;

case KC_AP_LED_OFF:
annepro2LedDisable();
ap2_led_disable();
break;

case KC_AP_LED_ON:
if (annepro2LedStatus.matrixEnabled) {
annepro2LedNextProfile();
if (ap2_led_status.matrix_enabled) {
ap2_led_next_profile();
} else {
annepro2LedEnable();
ap2_led_enable();
}
annepro2LedResetForegroundColor();
ap2_led_reset_foreground_color();
break;

case KC_AP_LED_NEXT_PROFILE:
annepro2LedNextProfile();
annepro2LedResetForegroundColor();
ap2_led_next_profile();
ap2_led_reset_foreground_color();
break;

case KC_AP_LED_PREV_PROFILE:
annepro2LedPrevProfile();
annepro2LedResetForegroundColor();
ap2_led_prev_profile();
ap2_led_reset_foreground_color();
break;

case KC_AP_LED_NEXT_INTENSITY:
annepro2LedNextIntensity();
annepro2LedResetForegroundColor();
ap2_led_next_intensity();
ap2_led_reset_foreground_color();
return false;

case KC_AP_LED_SPEED:
annepro2LedNextAnimationSpeed();
annepro2LedResetForegroundColor();
ap2_led_next_animation_speed();
ap2_led_reset_foreground_color();
return false;
#ifdef RGB_MATRIX_ENABLE
case RGB_TOG:
if(rgb_matrix_is_enabled()) ap2_led_disable();
else ap2_led_enable();
return true;
#endif

default:
break;
Expand Down
6 changes: 3 additions & 3 deletions keyboards/annepro2/annepro2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ typedef struct __attribute__((__packed__)) {
uint8_t _dummy[10];
bool caps_lock;
} ble_capslock_t;
extern ble_capslock_t BLECapsLock;
extern ble_capslock_t ble_capslock;

// Matrix keymap
// clang-format off
#define LAYOUT( \
#define LAYOUT_60_ansi( \
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, \
K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, \
K30, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3C, \
K40, K42, K43, K46, K49, K4A, K4B, K4C \
K40, K42, K43, K46, K49, K4A, K4B, K4C \
) { \
/* COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 COL12 COL13 COL14*/ \
/* ROW1 */ { K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0D }, \
Expand Down
34 changes: 17 additions & 17 deletions keyboards/annepro2/annepro2_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@ static host_driver_t ap2_ble_driver = {
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 ble_mcu_wakeup[11] = {0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x01, 0x7d, 0x02, 0x01, 0x02};

static uint8_t bleMcuStartBroadcast[11] = {
static uint8_t ble_mcu_start_broadcast[11] = {
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x01, 0x00 // Broadcast ID[0-3]
};

static uint8_t bleMcuConnect[11] = {
static uint8_t ble_mcu_connect[11] = {
0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x04, 0x00 // Connect ID [0-3]
};

static uint8_t bleMcuSendReport[10] = {
static uint8_t ble_mcu_send_report[10] = {
0x7b, 0x12, 0x53, 0x00, 0x0A, 0x00, 0x00, 0x7d, 0x10, 0x04,
};

static uint8_t bleMcuSendConsumerReport[10] = {
static uint8_t ble_mcu_send_consumer_report[10] = {
0x7b, 0x12, 0x53, 0x00, 0x06, 0x00, 0x00, 0x7d, 0x10, 0x08,
};

static uint8_t bleMcuUnpair[10] = {
static uint8_t ble_mcu_unpair[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 ble_mcu_bootload[11] = {0x7b, 0x10, 0x51, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x01};

static host_driver_t *lastHostDriver = NULL;
static host_driver_t *last_host_driver = NULL;
#ifdef NKRO_ENABLE
static bool lastNkroStatus = false;
#endif // NKRO_ENABLE

/* -------------------- Public Function Implementation ---------------------- */

void annepro2_ble_bootload(void) { sdWrite(&SD1, bleMcuBootload, sizeof(bleMcuBootload)); }
void annepro2_ble_bootload(void) { sdWrite(&SD1, ble_mcu_bootload, sizeof(ble_mcu_bootload)); }

void annepro2_ble_startup(void) { sdWrite(&SD1, bleMcuWakeup, sizeof(bleMcuWakeup)); }
void annepro2_ble_startup(void) { sdWrite(&SD1, ble_mcu_wakeup, sizeof(ble_mcu_wakeup)); }

void annepro2_ble_broadcast(uint8_t port) {
if (port > 3) {
port = 3;
}
// sdPut(&SD1, 0x00);
sdWrite(&SD1, bleMcuStartBroadcast, sizeof(bleMcuStartBroadcast));
sdWrite(&SD1, ble_mcu_start_broadcast, sizeof(ble_mcu_start_broadcast));
sdPut(&SD1, port);
static int lastBroadcast = -1;
if (lastBroadcast == port) {
Expand All @@ -88,7 +88,7 @@ void annepro2_ble_connect(uint8_t port) {
if (port > 3) {
port = 3;
}
sdWrite(&SD1, bleMcuConnect, sizeof(bleMcuConnect));
sdWrite(&SD1, ble_mcu_connect, sizeof(ble_mcu_connect));
sdPut(&SD1, port);
ap2_ble_swtich_ble_driver();
}
Expand All @@ -103,12 +103,12 @@ void annepro2_ble_disconnect(void) {
#ifdef NKRO_ENABLE
keymap_config.nkro = lastNkroStatus;
#endif
host_set_driver(lastHostDriver);
host_set_driver(last_host_driver);
}

void annepro2_ble_unpair(void) {
// sdPut(&SD1, 0x0);
sdWrite(&SD1, bleMcuUnpair, sizeof(bleMcuUnpair));
sdWrite(&SD1, ble_mcu_unpair, sizeof(ble_mcu_unpair));
}

/* ------------------- Static Function Implementation ----------------------- */
Expand All @@ -117,7 +117,7 @@ static void ap2_ble_swtich_ble_driver(void) {
return;
}
clear_keyboard();
lastHostDriver = host_get_driver();
last_host_driver = host_get_driver();
#ifdef NKRO_ENABLE
lastNkroStatus = keymap_config.nkro;
#endif
Expand Down Expand Up @@ -154,7 +154,7 @@ static inline uint16_t CONSUMER2AP2(uint16_t usage) {

static void ap2_ble_consumer(uint16_t data) {
sdPut(&SD1, 0x0);
sdWrite(&SD1, bleMcuSendConsumerReport, sizeof(bleMcuSendConsumerReport));
sdWrite(&SD1, ble_mcu_send_consumer_report, sizeof(ble_mcu_send_consumer_report));
sdPut(&SD1, CONSUMER2AP2(data));
static const uint8_t dummy[3] = {0};
sdWrite(&SD1, dummy, sizeof(dummy));
Expand All @@ -165,6 +165,6 @@ static void ap2_ble_consumer(uint16_t data) {
*/
static void ap2_ble_keyboard(report_keyboard_t *report) {
sdPut(&SD1, 0x0);
sdWrite(&SD1, bleMcuSendReport, sizeof(bleMcuSendReport));
sdWrite(&SD1, ble_mcu_send_report, sizeof(ble_mcu_send_report));
sdWrite(&SD1, &report->raw[0], KEYBOARD_REPORT_SIZE);
}
Loading

0 comments on commit 1227890

Please sign in to comment.