Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed-dev nRF51822
main.cpp@68:13e2343452d5, 2016-09-02 (annotated)
- Committer:
- cho45 
- Date:
- Fri Sep 02 07:41:33 2016 +0900
- Revision:
- 68:13e2343452d5
- Parent:
- 60:b899414e1d34
- Child:
- 69:9d6ecd584a0c
battery ?????????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| cho45 | 42:2c3be8694896 | 1 | /* BLE Keyboard Implementation for mbed | 
| cho45 | 42:2c3be8694896 | 2 | * Copyright (c) 2016 cho45 <cho45@lowreal.net> | 
| cho45 | 0:be89b5fdea09 | 3 | * | 
| cho45 | 0:be89b5fdea09 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| cho45 | 0:be89b5fdea09 | 5 | * you may not use this file except in compliance with the License. | 
| cho45 | 0:be89b5fdea09 | 6 | * You may obtain a copy of the License at | 
| cho45 | 0:be89b5fdea09 | 7 | * | 
| cho45 | 0:be89b5fdea09 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| cho45 | 0:be89b5fdea09 | 9 | * | 
| cho45 | 0:be89b5fdea09 | 10 | * Unless required by applicable law or agreed to in writing, software | 
| cho45 | 0:be89b5fdea09 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| cho45 | 0:be89b5fdea09 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| cho45 | 0:be89b5fdea09 | 13 | * See the License for the specific language governing permissions and | 
| cho45 | 0:be89b5fdea09 | 14 | * limitations under the License. | 
| cho45 | 0:be89b5fdea09 | 15 | */ | 
| cho45 | 0:be89b5fdea09 | 16 | |
| cho45 | 30:f9ebc769118d | 17 | #include <cmath> | 
| cho45 | 0:be89b5fdea09 | 18 | #include "mbed.h" | 
| cho45 | 6:f1c3ea8bc850 | 19 | |
| cho45 | 6:f1c3ea8bc850 | 20 | #include "HIDController_BLE.h" | 
| cho45 | 42:2c3be8694896 | 21 | #include "KeyboardMatrixController.h" | 
| cho45 | 42:2c3be8694896 | 22 | #include "BatteryLevel.h" | 
| cho45 | 42:2c3be8694896 | 23 | #include "WatchDog.h" | 
| cho45 | 4:54cb552e50c4 | 24 | |
| cho45 | 42:2c3be8694896 | 25 | #include "keymap.h" | 
| cho45 | 42:2c3be8694896 | 26 | #include "config.h" | 
| cho45 | 5:65d4e94735b6 | 27 | |
| cho45 | 42:2c3be8694896 | 28 | RawSerial serial(USBTX, USBRX); | 
| cho45 | 48:d6938de02f62 | 29 | |
| cho45 | 48:d6938de02f62 | 30 | static I2C i2c(I2C_SDA0, I2C_SCL0); | 
| cho45 | 48:d6938de02f62 | 31 | static KeyboardMatrixController keyboardMatrixController(i2c); | 
| cho45 | 48:d6938de02f62 | 32 | static Keymap keymap; | 
| cho45 | 15:70bf079d3ee1 | 33 | |
| cho45 | 15:70bf079d3ee1 | 34 | // Interrupt from MCP23017 | 
| cho45 | 15:70bf079d3ee1 | 35 | // (pulled-up and two MCP23017 is configured with open drain INT) | 
| cho45 | 48:d6938de02f62 | 36 | static InterruptIn keyboardInterruptIn(P0_5); | 
| cho45 | 5:65d4e94735b6 | 37 | |
| cho45 | 37:4ce71fa47fc3 | 38 | #define PIN_STATUS_LED P0_4 | 
| cho45 | 48:d6938de02f62 | 39 | static DigitalOut statusLed(PIN_STATUS_LED, 0); | 
| cho45 | 25:094df0d9e95b | 40 | |
| cho45 | 43:4de3870b39cb | 41 | // timeout for status led and wakeup from sleep | 
| cho45 | 48:d6938de02f62 | 42 | static Timeout timeout; | 
| cho45 | 35:6a7fddfa14cf | 43 | |
| cho45 | 5:65d4e94735b6 | 44 | // ROWS=8 | 
| cho45 | 5:65d4e94735b6 | 45 | // COLS=16 | 
| cho45 | 5:65d4e94735b6 | 46 | // 列ごとに1バイトにパックしてキーの状態を保持する | 
| cho45 | 5:65d4e94735b6 | 47 | static uint8_t keysA[COLS]; | 
| cho45 | 5:65d4e94735b6 | 48 | static uint8_t keysB[COLS]; | 
| cho45 | 5:65d4e94735b6 | 49 | static bool state = 0; | 
| cho45 | 28:1f843a3daab0 | 50 | #define is_pressed(keys, row, col) (!!(keys[col] & (1<<row))) | 
| cho45 | 28:1f843a3daab0 | 51 | |
| cho45 | 9:d1daefbf1fbd | 52 | // delay for interrupt | 
| cho45 | 23:b31957ce64e9 | 53 | static volatile int8_t pollCount = 50; | 
| cho45 | 5:65d4e94735b6 | 54 | |
| cho45 | 48:d6938de02f62 | 55 | static void keyboardInterrupt() { | 
| cho45 | 8:d684faf04c9a | 56 | // just for wakeup | 
| cho45 | 33:6a2301a89e92 | 57 | pollCount = 25; | 
| cho45 | 2:c2e3f240640c | 58 | } | 
| cho45 | 2:c2e3f240640c | 59 | |
| cho45 | 48:d6938de02f62 | 60 | static void powerOff() { | 
| cho45 | 42:2c3be8694896 | 61 | DEBUG_PRINTF("power off\r\n"); | 
| cho45 | 23:b31957ce64e9 | 62 | NRF_POWER->SYSTEMOFF = 1; | 
| cho45 | 23:b31957ce64e9 | 63 | } | 
| cho45 | 23:b31957ce64e9 | 64 | |
| cho45 | 35:6a7fddfa14cf | 65 | static bool updateStatudLedEnabled = false; | 
| cho45 | 48:d6938de02f62 | 66 | static void updateStatusLed() { | 
| cho45 | 35:6a7fddfa14cf | 67 | switch (HIDController::status()) { | 
| cho45 | 35:6a7fddfa14cf | 68 | case TIMEOUT: | 
| cho45 | 38:115875b8cb6c | 69 | case DISCONNECTED: | 
| cho45 | 38:115875b8cb6c | 70 | case CONNECTED: | 
| cho45 | 38:115875b8cb6c | 71 | statusLed = 0; | 
| cho45 | 38:115875b8cb6c | 72 | updateStatudLedEnabled = false; | 
| cho45 | 38:115875b8cb6c | 73 | return; | 
| cho45 | 35:6a7fddfa14cf | 74 | case ADVERTISING: | 
| cho45 | 38:115875b8cb6c | 75 | case CONNECTING: | 
| cho45 | 38:115875b8cb6c | 76 | statusLed = !statusLed; | 
| cho45 | 38:115875b8cb6c | 77 | updateStatudLedEnabled = true; | 
| cho45 | 49:09ae6fb04a32 | 78 | timeout.attach(updateStatusLed, statusLed ? 0.1 : 0.5); | 
| cho45 | 38:115875b8cb6c | 79 | break; | 
| cho45 | 35:6a7fddfa14cf | 80 | } | 
| cho45 | 35:6a7fddfa14cf | 81 | } | 
| cho45 | 35:6a7fddfa14cf | 82 | |
| cho45 | 36:78c211da4eb0 | 83 | static volatile bool keyIntervalInterrupt = false; | 
| cho45 | 48:d6938de02f62 | 84 | static void wakeupKeyIntervalSleep() { | 
| cho45 | 36:78c211da4eb0 | 85 | keyIntervalInterrupt = true; | 
| cho45 | 30:f9ebc769118d | 86 | } | 
| cho45 | 30:f9ebc769118d | 87 | |
| cho45 | 0:be89b5fdea09 | 88 | int main(void) { | 
| cho45 | 33:6a2301a89e92 | 89 | { | 
| cho45 | 48:d6938de02f62 | 90 | const uint32_t reason = NRF_POWER->RESETREAS; | 
| cho45 | 33:6a2301a89e92 | 91 | NRF_POWER->RESETREAS = 0xffffffff; // clear reason | 
| cho45 | 43:4de3870b39cb | 92 | // reset cause should be shown everytime | 
| cho45 | 60:b899414e1d34 | 93 | serial.printf("init [%x] sp:%x\r\n", reason, GET_SP()); | 
| cho45 | 33:6a2301a89e92 | 94 | } | 
| cho45 | 43:4de3870b39cb | 95 | |
| cho45 | 49:09ae6fb04a32 | 96 | { | 
| cho45 | 49:09ae6fb04a32 | 97 | const float battery = BatteryLevel::readBatteryVoltage(); | 
| cho45 | 49:09ae6fb04a32 | 98 | if (battery < BatteryLevel::BATTERY_LOW) { | 
| cho45 | 49:09ae6fb04a32 | 99 | powerOff(); | 
| cho45 | 49:09ae6fb04a32 | 100 | } | 
| cho45 | 37:4ce71fa47fc3 | 101 | } | 
| cho45 | 43:4de3870b39cb | 102 | |
| cho45 | 30:f9ebc769118d | 103 | // Enable Pin-reset on DEBUG mode | 
| cho45 | 30:f9ebc769118d | 104 | // This makes possiable booting without normal mode easily. | 
| cho45 | 30:f9ebc769118d | 105 | NRF_POWER->RESET = 1; | 
| cho45 | 30:f9ebc769118d | 106 | // Disable Internal DC/DC step down converter surely | 
| cho45 | 29:ec548c473d50 | 107 | NRF_POWER->DCDCEN = 0; | 
| cho45 | 43:4de3870b39cb | 108 | |
| cho45 | 4:54cb552e50c4 | 109 | // mbed's Serial of TARGET_RBLAB_BLENANO sucks | 
| cho45 | 30:f9ebc769118d | 110 | // DO NOT CONNECT RTS/CTS WITHOUT PRIOR CONSENT! | 
| cho45 | 4:54cb552e50c4 | 111 | NRF_UART0->PSELRTS = 0xFFFFFFFFUL; | 
| cho45 | 4:54cb552e50c4 | 112 | NRF_UART0->PSELCTS = 0xFFFFFFFFUL; | 
| cho45 | 43:4de3870b39cb | 113 | |
| cho45 | 37:4ce71fa47fc3 | 114 | // Set LED Pin as HIGH Current mode | 
| cho45 | 37:4ce71fa47fc3 | 115 | NRF_GPIO->PIN_CNF[PIN_STATUS_LED] = | 
| cho45 | 41:2b034f22b98f | 116 | (NRF_GPIO->PIN_CNF[PIN_STATUS_LED] & ~GPIO_PIN_CNF_DRIVE_Msk) | | 
| cho45 | 41:2b034f22b98f | 117 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos); | 
| cho45 | 16:345eebc4f259 | 118 | |
| cho45 | 42:2c3be8694896 | 119 | // Unsed pins. | 
| cho45 | 42:2c3be8694896 | 120 | // Set as PullUp for power consumption | 
| cho45 | 42:2c3be8694896 | 121 | // Use GPIO registers directly for saving ram | 
| cho45 | 42:2c3be8694896 | 122 | // Pad pinouts | 
| cho45 | 42:2c3be8694896 | 123 | /* | 
| cho45 | 42:2c3be8694896 | 124 | DigitalIn unused_p0_7(P0_7, PullUp); | 
| cho45 | 42:2c3be8694896 | 125 | DigitalIn unused_p0_6(P0_6, PullUp); | 
| cho45 | 42:2c3be8694896 | 126 | DigitalIn unused_p0_15(P0_15, PullUp); | 
| cho45 | 42:2c3be8694896 | 127 | DigitalIn unused_p0_29(P0_29, PullUp); | 
| cho45 | 42:2c3be8694896 | 128 | DigitalIn unused_p0_28(P0_28, PullUp); | 
| cho45 | 42:2c3be8694896 | 129 | */ | 
| cho45 | 42:2c3be8694896 | 130 | NRF_GPIO->PIN_CNF[P0_7] = | 
| cho45 | 42:2c3be8694896 | 131 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 132 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 133 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 134 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 135 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 136 | NRF_GPIO->PIN_CNF[P0_6] = | 
| cho45 | 42:2c3be8694896 | 137 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 138 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 139 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 140 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 141 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 142 | NRF_GPIO->PIN_CNF[P0_15] = | 
| cho45 | 42:2c3be8694896 | 143 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 144 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 145 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 146 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 147 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 148 | NRF_GPIO->PIN_CNF[P0_29] = | 
| cho45 | 42:2c3be8694896 | 149 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 150 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 151 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 152 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 153 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 154 | NRF_GPIO->PIN_CNF[P0_28] = | 
| cho45 | 42:2c3be8694896 | 155 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 156 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 157 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 158 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 159 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 160 | /* | 
| cho45 | 42:2c3be8694896 | 161 | DigitalIn unused_p0_19(P0_19, PullUp); // This is on board LED which connected to VDD | 
| cho45 | 42:2c3be8694896 | 162 | DigitalIn unused_p0_11(P0_11, PullUp); // RXD | 
| cho45 | 42:2c3be8694896 | 163 | */ | 
| cho45 | 42:2c3be8694896 | 164 | NRF_GPIO->PIN_CNF[P0_19] = // This is on board LED which connected to VDD | 
| cho45 | 42:2c3be8694896 | 165 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 166 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 167 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 168 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 169 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 170 | NRF_GPIO->PIN_CNF[P0_11] = // RXD | 
| cho45 | 42:2c3be8694896 | 171 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | | 
| cho45 | 42:2c3be8694896 | 172 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | | 
| cho45 | 42:2c3be8694896 | 173 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | | 
| cho45 | 42:2c3be8694896 | 174 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | | 
| cho45 | 42:2c3be8694896 | 175 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); | 
| cho45 | 42:2c3be8694896 | 176 | |
| cho45 | 32:6c0f43fda460 | 177 | WatchDog::init(); | 
| cho45 | 42:2c3be8694896 | 178 | |
| cho45 | 42:2c3be8694896 | 179 | // only 100kHz/250khz/400khz | 
| cho45 | 36:78c211da4eb0 | 180 | i2c.frequency(250000); | 
| cho45 | 4:54cb552e50c4 | 181 | |
| cho45 | 43:4de3870b39cb | 182 | keyboardInterruptIn.mode(PullUp); | 
| cho45 | 43:4de3870b39cb | 183 | keyboardInterruptIn.fall(keyboardInterrupt); | 
| cho45 | 4:54cb552e50c4 | 184 | |
| cho45 | 5:65d4e94735b6 | 185 | keyboardMatrixController.init(); | 
| cho45 | 33:6a2301a89e92 | 186 | pollCount = 10; | 
| cho45 | 4:54cb552e50c4 | 187 | |
| cho45 | 6:f1c3ea8bc850 | 188 | HIDController::init(); | 
| cho45 | 43:4de3870b39cb | 189 | |
| cho45 | 16:345eebc4f259 | 190 | // STOP UART RX for power consumption | 
| cho45 | 21:d801c32231b0 | 191 | NRF_UART0->TASKS_STOPRX = 1; | 
| cho45 | 43:4de3870b39cb | 192 | |
| cho45 | 30:f9ebc769118d | 193 | // Disable TWI by default. | 
| cho45 | 16:345eebc4f259 | 194 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; | 
| cho45 | 43:4de3870b39cb | 195 | |
| cho45 | 32:6c0f43fda460 | 196 | while (1) { | 
| cho45 | 32:6c0f43fda460 | 197 | WatchDog::reload(); | 
| cho45 | 43:4de3870b39cb | 198 | |
| cho45 | 23:b31957ce64e9 | 199 | if (pollCount > 0) { | 
| cho45 | 42:2c3be8694896 | 200 | DEBUG_PRINTF("scan keys\r\n"); | 
| cho45 | 43:4de3870b39cb | 201 | |
| cho45 | 33:6a2301a89e92 | 202 | while (pollCount-- > 0) { | 
| cho45 | 33:6a2301a89e92 | 203 | WatchDog::reload(); | 
| cho45 | 43:4de3870b39cb | 204 | |
| cho45 | 23:b31957ce64e9 | 205 | uint8_t (&keysCurr)[COLS] = state ? keysA : keysB; | 
| cho45 | 23:b31957ce64e9 | 206 | uint8_t (&keysPrev)[COLS] = state ? keysB : keysA; | 
| cho45 | 43:4de3870b39cb | 207 | |
| cho45 | 23:b31957ce64e9 | 208 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; | 
| cho45 | 23:b31957ce64e9 | 209 | keyboardMatrixController.scanKeyboard(keysCurr); | 
| cho45 | 23:b31957ce64e9 | 210 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; | 
| cho45 | 43:4de3870b39cb | 211 | |
| cho45 | 23:b31957ce64e9 | 212 | bool queue = false; | 
| cho45 | 43:4de3870b39cb | 213 | |
| cho45 | 23:b31957ce64e9 | 214 | for (int col = 0; col < COLS; col++) { | 
| cho45 | 48:d6938de02f62 | 215 | const uint8_t changed = keysPrev[col] ^ keysCurr[col]; | 
| cho45 | 23:b31957ce64e9 | 216 | if (changed) queue = true; | 
| cho45 | 23:b31957ce64e9 | 217 | for (int row = 0; row < ROWS; row++) { | 
| cho45 | 23:b31957ce64e9 | 218 | if (changed & (1<<row)) { | 
| cho45 | 23:b31957ce64e9 | 219 | bool pressed = keysCurr[col] & (1<<row); | 
| cho45 | 42:2c3be8694896 | 220 | // DEBUG_KEYEVENT("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed); | 
| cho45 | 47:5bf2ae8cc710 | 221 | keymap.execute(row, col, pressed); | 
| cho45 | 23:b31957ce64e9 | 222 | } | 
| cho45 | 7:b9270a37345b | 223 | } | 
| cho45 | 7:b9270a37345b | 224 | } | 
| cho45 | 23:b31957ce64e9 | 225 | state = !state; | 
| cho45 | 43:4de3870b39cb | 226 | |
| cho45 | 38:115875b8cb6c | 227 | if (HIDController::status() == DISCONNECTED || | 
| cho45 | 38:115875b8cb6c | 228 | HIDController::status() == TIMEOUT) { | 
| cho45 | 43:4de3870b39cb | 229 | |
| cho45 | 43:4de3870b39cb | 230 | if ( | 
| cho45 | 43:4de3870b39cb | 231 | is_pressed(keysCurr, 0, 0) && // left top 1 | 
| cho45 | 43:4de3870b39cb | 232 | is_pressed(keysCurr, 1, 0) && // left top 2 | 
| cho45 | 43:4de3870b39cb | 233 | is_pressed(keysCurr, 0, 15) // right top | 
| cho45 | 43:4de3870b39cb | 234 | ) { | 
| cho45 | 43:4de3870b39cb | 235 | DEBUG_PRINTF("re-init connection\r\n"); | 
| cho45 | 43:4de3870b39cb | 236 | HIDController::initializeConnection(true); | 
| cho45 | 43:4de3870b39cb | 237 | } else { | 
| cho45 | 43:4de3870b39cb | 238 | if (HIDController::status() == TIMEOUT) { | 
| cho45 | 43:4de3870b39cb | 239 | DEBUG_PRINTF("wakeup\r\n"); | 
| cho45 | 43:4de3870b39cb | 240 | HIDController::initializeConnection(false); | 
| cho45 | 43:4de3870b39cb | 241 | } | 
| cho45 | 43:4de3870b39cb | 242 | } | 
| cho45 | 38:115875b8cb6c | 243 | } | 
| cho45 | 43:4de3870b39cb | 244 | |
| cho45 | 23:b31957ce64e9 | 245 | if (queue) HIDController::queueCurrentReportData(); | 
| cho45 | 43:4de3870b39cb | 246 | |
| cho45 | 47:5bf2ae8cc710 | 247 | // use timer to use wait 5ms | 
| cho45 | 58:64df960619ce | 248 | timeout.detach(); | 
| cho45 | 47:5bf2ae8cc710 | 249 | keyIntervalInterrupt = false; | 
| cho45 | 36:78c211da4eb0 | 250 | timeout.attach_us(wakeupKeyIntervalSleep, 5000); | 
| cho45 | 45:f4be69c936f6 | 251 | while (!keyIntervalInterrupt) HIDController::waitForEvent(); | 
| cho45 | 7:b9270a37345b | 252 | } | 
| cho45 | 8:d684faf04c9a | 253 | } else { | 
| cho45 | 35:6a7fddfa14cf | 254 | if (!updateStatudLedEnabled) updateStatusLed(); | 
| cho45 | 43:4de3870b39cb | 255 | |
| cho45 | 49:09ae6fb04a32 | 256 | { | 
| cho45 | 49:09ae6fb04a32 | 257 | const float batteryVoltage = BatteryLevel::readBatteryVoltage(); | 
| cho45 | 49:09ae6fb04a32 | 258 | const uint8_t batteryPercentage = BatteryLevel::readBatteryPercentage(batteryVoltage); | 
| cho45 | 49:09ae6fb04a32 | 259 | const bool isLowBattery = batteryVoltage < BatteryLevel::BATTERY_LOW; | 
| cho45 | 49:09ae6fb04a32 | 260 | |
| cho45 | 58:64df960619ce | 261 | HIDController::updateBatteryLevel(batteryPercentage, batteryVoltage * 1000); | 
| cho45 | 49:09ae6fb04a32 | 262 | |
| cho45 | 49:09ae6fb04a32 | 263 | if (isLowBattery) { | 
| cho45 | 68:13e2343452d5 | 264 | DEBUG_PRINTF("LOWBAT %f %d%%", batteryVoltage, batteryPercentage); | 
| cho45 | 49:09ae6fb04a32 | 265 | powerOff(); | 
| cho45 | 49:09ae6fb04a32 | 266 | } | 
| cho45 | 37:4ce71fa47fc3 | 267 | } | 
| cho45 | 45:f4be69c936f6 | 268 | |
| cho45 | 45:f4be69c936f6 | 269 | if (HIDController::status() == DISCONNECTED) { | 
| cho45 | 45:f4be69c936f6 | 270 | HIDController::initializeConnection(false); | 
| cho45 | 45:f4be69c936f6 | 271 | } | 
| cho45 | 68:13e2343452d5 | 272 | |
| cho45 | 68:13e2343452d5 | 273 | DEBUG_PRINTF("WFE [%d:%s]\r\n", | 
| cho45 | 68:13e2343452d5 | 274 | HIDController::status(), | 
| cho45 | 68:13e2343452d5 | 275 | HIDController::statusString() | 
| cho45 | 68:13e2343452d5 | 276 | ); | 
| cho45 | 58:64df960619ce | 277 | HIDController::waitForEvent(); | 
| cho45 | 7:b9270a37345b | 278 | } | 
| cho45 | 2:c2e3f240640c | 279 | } | 
| cho45 | 43:4de3870b39cb | 280 | } |