|
| 1 | +/* Copyright 2021 OpenAnnePro community |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation, either version 2 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "hal.h" |
| 18 | +#include "annepro2.h" |
| 19 | +#include "annepro2_ble.h" |
| 20 | +#include "spi_master.h" |
| 21 | +#include "ap2_led.h" |
| 22 | +#include "protocol.h" |
| 23 | + |
| 24 | +#define RAM_MAGIC_LOCATION 0x20001ffc |
| 25 | +#define IAP_MAGIC_VALUE 0x0000fab2 |
| 26 | + |
| 27 | +static const SerialConfig ledUartInitConfig = { |
| 28 | + .speed = 115200, |
| 29 | +}; |
| 30 | + |
| 31 | +#ifndef LED_UART_BAUD_RATE |
| 32 | +# define LED_UART_BAUD_RATE 115200 |
| 33 | +#endif // LED_UART_BAUD_RATE |
| 34 | + |
| 35 | +static const SerialConfig ledUartRuntimeConfig = { |
| 36 | + .speed = LED_UART_BAUD_RATE, |
| 37 | +}; |
| 38 | + |
| 39 | +static const SerialConfig bleUartConfig = { |
| 40 | + .speed = 115200, |
| 41 | +}; |
| 42 | + |
| 43 | +static uint8_t ledMcuWakeup[11] = {0x7b, 0x10, 0x43, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x02}; |
| 44 | + |
| 45 | +ble_capslock_t BLECapsLock = {._dummy = {0}, .caps_lock = false}; |
| 46 | + |
| 47 | +void bootloader_jump(void) { |
| 48 | + // Send msg to shine to boot into IAP |
| 49 | + annepro2SetIAP(); |
| 50 | + |
| 51 | + // wait for shine to boot into IAP |
| 52 | + wait_ms(15); |
| 53 | + |
| 54 | + // Load ble into IAP |
| 55 | + annepro2_ble_bootload(); |
| 56 | + wait_ms(15); |
| 57 | + |
| 58 | + // Magic key to set keyboard to IAP |
| 59 | + // It’s from reversing original boot loader |
| 60 | + // If value is that it stays in boot loader aka IAP |
| 61 | + *((uint32_t *)RAM_MAGIC_LOCATION) = IAP_MAGIC_VALUE; |
| 62 | + |
| 63 | + // Load the main MCU into IAP |
| 64 | + __disable_irq(); |
| 65 | + NVIC_SystemReset(); |
| 66 | +} |
| 67 | + |
| 68 | +void keyboard_pre_init_kb(void) { |
| 69 | + // Start LED UART |
| 70 | + sdStart(&SD0, &ledUartInitConfig); |
| 71 | + /* Let the LED chip settle a bit before switching the mode. |
| 72 | + * That helped at least one person. */ |
| 73 | + wait_ms(15); |
| 74 | + sdWrite(&SD0, ledMcuWakeup, sizeof(ledMcuWakeup)); |
| 75 | + |
| 76 | + // wait to receive response from wakeup |
| 77 | + wait_ms(15); |
| 78 | + |
| 79 | + protoInit(&proto, ledCommandCallback); |
| 80 | + |
| 81 | + // loop to clear out receive buffer from shine wakeup |
| 82 | + while (!sdGetWouldBlock(&SD0)) sdGet(&SD0); |
| 83 | + |
| 84 | + sdStart(&SD0, &ledUartRuntimeConfig); |
| 85 | + keyboard_pre_init_user(); |
| 86 | +} |
| 87 | + |
| 88 | +void keyboard_post_init_kb(void) { |
| 89 | + // Start BLE UART |
| 90 | + sdStart(&SD1, &bleUartConfig); |
| 91 | + annepro2_ble_startup(); |
| 92 | + |
| 93 | + // Give the send uart thread some time to |
| 94 | + // send out the queue before we read back |
| 95 | + wait_ms(100); |
| 96 | + |
| 97 | + // loop to clear out receive buffer from ble wakeup |
| 98 | + while (!sdGetWouldBlock(&SD1)) sdGet(&SD1); |
| 99 | + |
| 100 | + annepro2LedGetStatus(); |
| 101 | + |
| 102 | + keyboard_post_init_user(); |
| 103 | +} |
| 104 | + |
| 105 | +void matrix_scan_kb() { |
| 106 | + // if there's stuff on the ble serial buffer |
| 107 | + // read it into the capslock struct |
| 108 | + while (!sdGetWouldBlock(&SD1)) { |
| 109 | + sdReadTimeout(&SD1, (uint8_t *)&BLECapsLock, sizeof(ble_capslock_t), 10); |
| 110 | + } |
| 111 | + |
| 112 | + /* While there's data from LED keyboard sent - read it. */ |
| 113 | + while (!sdGetWouldBlock(&SD0)) { |
| 114 | + uint8_t byte = sdGet(&SD0); |
| 115 | + protoConsume(&proto, byte); |
| 116 | + } |
| 117 | + |
| 118 | + matrix_scan_user(); |
| 119 | +} |
| 120 | + |
| 121 | +bool process_record_kb(uint16_t keycode, keyrecord_t *record) { |
| 122 | + if (record->event.pressed) { |
| 123 | + if (annepro2LedStatus.matrixEnabled && annepro2LedStatus.isReactive) { |
| 124 | + annepro2LedForwardKeypress(record->event.key.row, record->event.key.col); |
| 125 | + } |
| 126 | + |
| 127 | + const annepro2Led_t blue = { |
| 128 | + .p.blue = 0xff, |
| 129 | + .p.red = 0x00, |
| 130 | + .p.green = 0x00, |
| 131 | + .p.alpha = 0xff, |
| 132 | + }; |
| 133 | + |
| 134 | + switch (keycode) { |
| 135 | + case KC_AP2_BT1: |
| 136 | + annepro2_ble_broadcast(0); |
| 137 | + /* FIXME: This hardcodes col/row position */ |
| 138 | + annepro2LedBlink(0, 1, blue, 8, 50); |
| 139 | + return false; |
| 140 | + |
| 141 | + case KC_AP2_BT2: |
| 142 | + annepro2_ble_broadcast(1); |
| 143 | + annepro2LedBlink(0, 2, blue, 8, 50); |
| 144 | + return false; |
| 145 | + |
| 146 | + case KC_AP2_BT3: |
| 147 | + annepro2_ble_broadcast(2); |
| 148 | + annepro2LedBlink(0, 3, blue, 8, 50); |
| 149 | + return false; |
| 150 | + |
| 151 | + case KC_AP2_BT4: |
| 152 | + annepro2_ble_broadcast(3); |
| 153 | + annepro2LedBlink(0, 4, blue, 8, 50); |
| 154 | + return false; |
| 155 | + |
| 156 | + case KC_AP2_USB: |
| 157 | + annepro2_ble_disconnect(); |
| 158 | + return false; |
| 159 | + |
| 160 | + case KC_AP2_BT_UNPAIR: |
| 161 | + annepro2_ble_unpair(); |
| 162 | + return false; |
| 163 | + |
| 164 | + case KC_AP_LED_OFF: |
| 165 | + annepro2LedDisable(); |
| 166 | + break; |
| 167 | + |
| 168 | + case KC_AP_LED_ON: |
| 169 | + if (annepro2LedStatus.matrixEnabled) { |
| 170 | + annepro2LedNextProfile(); |
| 171 | + } else { |
| 172 | + annepro2LedEnable(); |
| 173 | + } |
| 174 | + annepro2LedResetForegroundColor(); |
| 175 | + break; |
| 176 | + |
| 177 | + case KC_AP_LED_NEXT_PROFILE: |
| 178 | + annepro2LedNextProfile(); |
| 179 | + annepro2LedResetForegroundColor(); |
| 180 | + break; |
| 181 | + |
| 182 | + case KC_AP_LED_PREV_PROFILE: |
| 183 | + annepro2LedPrevProfile(); |
| 184 | + annepro2LedResetForegroundColor(); |
| 185 | + break; |
| 186 | + |
| 187 | + case KC_AP_LED_NEXT_INTENSITY: |
| 188 | + annepro2LedNextIntensity(); |
| 189 | + annepro2LedResetForegroundColor(); |
| 190 | + return false; |
| 191 | + |
| 192 | + case KC_AP_LED_SPEED: |
| 193 | + annepro2LedNextAnimationSpeed(); |
| 194 | + annepro2LedResetForegroundColor(); |
| 195 | + return false; |
| 196 | + |
| 197 | + default: |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + return process_record_user(keycode, record); |
| 202 | +} |
0 commit comments