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@49:09ae6fb04a32, 2016-08-29 (annotated)
- Committer:
- cho45
- Date:
- Mon Aug 29 14:17:12 2016 +0000
- Revision:
- 49:09ae6fb04a32
- Parent:
- 48:d6938de02f62
- Child:
- 50:c1382a0ff066
cleanup;
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 | 42:2c3be8694896 | 93 | serial.printf("init [%x]\r\n", reason); |
| 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 | 30:f9ebc769118d | 108 | // Enable 2.1V brown out detection for avoiding over discharge of NiMH |
| cho45 | 29:ec548c473d50 | 109 | NRF_POWER->POFCON = |
| cho45 | 29:ec548c473d50 | 110 | POWER_POFCON_POF_Enabled << POWER_POFCON_POF_Pos | |
| cho45 | 29:ec548c473d50 | 111 | POWER_POFCON_THRESHOLD_V21 << POWER_POFCON_THRESHOLD_Pos; |
| cho45 |
43:4de3870b39cb | 112 | |
| cho45 |
4:54cb552e50c4 | 113 | // mbed's Serial of TARGET_RBLAB_BLENANO sucks |
| cho45 | 30:f9ebc769118d | 114 | // DO NOT CONNECT RTS/CTS WITHOUT PRIOR CONSENT! |
| cho45 |
4:54cb552e50c4 | 115 | NRF_UART0->PSELRTS = 0xFFFFFFFFUL; |
| cho45 |
4:54cb552e50c4 | 116 | NRF_UART0->PSELCTS = 0xFFFFFFFFUL; |
| cho45 |
43:4de3870b39cb | 117 | |
| cho45 | 37:4ce71fa47fc3 | 118 | // Set LED Pin as HIGH Current mode |
| cho45 | 37:4ce71fa47fc3 | 119 | NRF_GPIO->PIN_CNF[PIN_STATUS_LED] = |
| cho45 | 41:2b034f22b98f | 120 | (NRF_GPIO->PIN_CNF[PIN_STATUS_LED] & ~GPIO_PIN_CNF_DRIVE_Msk) | |
| cho45 | 41:2b034f22b98f | 121 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos); |
| cho45 | 16:345eebc4f259 | 122 | |
| cho45 | 42:2c3be8694896 | 123 | // Unsed pins. |
| cho45 | 42:2c3be8694896 | 124 | // Set as PullUp for power consumption |
| cho45 | 42:2c3be8694896 | 125 | // Use GPIO registers directly for saving ram |
| cho45 | 42:2c3be8694896 | 126 | // Pad pinouts |
| cho45 | 42:2c3be8694896 | 127 | /* |
| cho45 | 42:2c3be8694896 | 128 | DigitalIn unused_p0_7(P0_7, PullUp); |
| cho45 | 42:2c3be8694896 | 129 | DigitalIn unused_p0_6(P0_6, PullUp); |
| cho45 | 42:2c3be8694896 | 130 | DigitalIn unused_p0_15(P0_15, PullUp); |
| cho45 | 42:2c3be8694896 | 131 | DigitalIn unused_p0_29(P0_29, PullUp); |
| cho45 | 42:2c3be8694896 | 132 | DigitalIn unused_p0_28(P0_28, PullUp); |
| cho45 | 42:2c3be8694896 | 133 | */ |
| cho45 | 42:2c3be8694896 | 134 | NRF_GPIO->PIN_CNF[P0_7] = |
| cho45 | 42:2c3be8694896 | 135 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 136 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 137 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 138 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 139 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 140 | NRF_GPIO->PIN_CNF[P0_6] = |
| cho45 | 42:2c3be8694896 | 141 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 142 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 143 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 144 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 145 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 146 | NRF_GPIO->PIN_CNF[P0_15] = |
| cho45 | 42:2c3be8694896 | 147 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 148 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 149 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 150 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 151 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 152 | NRF_GPIO->PIN_CNF[P0_29] = |
| cho45 | 42:2c3be8694896 | 153 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 154 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 155 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 156 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 157 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 158 | NRF_GPIO->PIN_CNF[P0_28] = |
| cho45 | 42:2c3be8694896 | 159 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 160 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 161 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 162 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 163 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 164 | /* |
| cho45 | 42:2c3be8694896 | 165 | DigitalIn unused_p0_19(P0_19, PullUp); // This is on board LED which connected to VDD |
| cho45 | 42:2c3be8694896 | 166 | DigitalIn unused_p0_11(P0_11, PullUp); // RXD |
| cho45 | 42:2c3be8694896 | 167 | */ |
| cho45 | 42:2c3be8694896 | 168 | NRF_GPIO->PIN_CNF[P0_19] = // This is on board LED which connected to VDD |
| cho45 | 42:2c3be8694896 | 169 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 170 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 171 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 172 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 173 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 174 | NRF_GPIO->PIN_CNF[P0_11] = // RXD |
| cho45 | 42:2c3be8694896 | 175 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 176 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 177 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 178 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 179 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 180 | |
| cho45 | 32:6c0f43fda460 | 181 | WatchDog::init(); |
| cho45 | 42:2c3be8694896 | 182 | |
| cho45 | 42:2c3be8694896 | 183 | // only 100kHz/250khz/400khz |
| cho45 | 36:78c211da4eb0 | 184 | i2c.frequency(250000); |
| cho45 |
4:54cb552e50c4 | 185 | |
| cho45 |
43:4de3870b39cb | 186 | keyboardInterruptIn.mode(PullUp); |
| cho45 |
43:4de3870b39cb | 187 | keyboardInterruptIn.fall(keyboardInterrupt); |
| cho45 |
4:54cb552e50c4 | 188 | |
| cho45 |
5:65d4e94735b6 | 189 | keyboardMatrixController.init(); |
| cho45 | 33:6a2301a89e92 | 190 | pollCount = 10; |
| cho45 |
4:54cb552e50c4 | 191 | |
| cho45 |
6:f1c3ea8bc850 | 192 | HIDController::init(); |
| cho45 |
43:4de3870b39cb | 193 | |
| cho45 | 16:345eebc4f259 | 194 | // STOP UART RX for power consumption |
| cho45 | 21:d801c32231b0 | 195 | NRF_UART0->TASKS_STOPRX = 1; |
| cho45 |
43:4de3870b39cb | 196 | |
| cho45 | 30:f9ebc769118d | 197 | // Disable TWI by default. |
| cho45 | 16:345eebc4f259 | 198 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 |
43:4de3870b39cb | 199 | |
| cho45 | 32:6c0f43fda460 | 200 | while (1) { |
| cho45 | 32:6c0f43fda460 | 201 | WatchDog::reload(); |
| cho45 |
43:4de3870b39cb | 202 | |
| cho45 | 23:b31957ce64e9 | 203 | if (pollCount > 0) { |
| cho45 | 42:2c3be8694896 | 204 | DEBUG_PRINTF("scan keys\r\n"); |
| cho45 |
43:4de3870b39cb | 205 | |
| cho45 | 33:6a2301a89e92 | 206 | while (pollCount-- > 0) { |
| cho45 | 33:6a2301a89e92 | 207 | WatchDog::reload(); |
| cho45 |
43:4de3870b39cb | 208 | |
| cho45 | 23:b31957ce64e9 | 209 | uint8_t (&keysCurr)[COLS] = state ? keysA : keysB; |
| cho45 | 23:b31957ce64e9 | 210 | uint8_t (&keysPrev)[COLS] = state ? keysB : keysA; |
| cho45 |
43:4de3870b39cb | 211 | |
| cho45 | 23:b31957ce64e9 | 212 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 | 23:b31957ce64e9 | 213 | keyboardMatrixController.scanKeyboard(keysCurr); |
| cho45 | 23:b31957ce64e9 | 214 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 |
43:4de3870b39cb | 215 | |
| cho45 | 23:b31957ce64e9 | 216 | bool queue = false; |
| cho45 |
43:4de3870b39cb | 217 | |
| cho45 | 23:b31957ce64e9 | 218 | for (int col = 0; col < COLS; col++) { |
| cho45 | 48:d6938de02f62 | 219 | const uint8_t changed = keysPrev[col] ^ keysCurr[col]; |
| cho45 | 23:b31957ce64e9 | 220 | if (changed) queue = true; |
| cho45 | 23:b31957ce64e9 | 221 | for (int row = 0; row < ROWS; row++) { |
| cho45 | 23:b31957ce64e9 | 222 | if (changed & (1<<row)) { |
| cho45 | 23:b31957ce64e9 | 223 | bool pressed = keysCurr[col] & (1<<row); |
| cho45 | 42:2c3be8694896 | 224 | // DEBUG_KEYEVENT("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed); |
| cho45 | 47:5bf2ae8cc710 | 225 | keymap.execute(row, col, pressed); |
| cho45 | 23:b31957ce64e9 | 226 | } |
| cho45 |
7:b9270a37345b | 227 | } |
| cho45 |
7:b9270a37345b | 228 | } |
| cho45 | 23:b31957ce64e9 | 229 | state = !state; |
| cho45 |
43:4de3870b39cb | 230 | |
| cho45 | 38:115875b8cb6c | 231 | if (HIDController::status() == DISCONNECTED || |
| cho45 | 38:115875b8cb6c | 232 | HIDController::status() == TIMEOUT) { |
| cho45 |
43:4de3870b39cb | 233 | |
| cho45 |
43:4de3870b39cb | 234 | if ( |
| cho45 |
43:4de3870b39cb | 235 | is_pressed(keysCurr, 0, 0) && // left top 1 |
| cho45 |
43:4de3870b39cb | 236 | is_pressed(keysCurr, 1, 0) && // left top 2 |
| cho45 |
43:4de3870b39cb | 237 | is_pressed(keysCurr, 0, 15) // right top |
| cho45 |
43:4de3870b39cb | 238 | ) { |
| cho45 |
43:4de3870b39cb | 239 | DEBUG_PRINTF("re-init connection\r\n"); |
| cho45 |
43:4de3870b39cb | 240 | HIDController::initializeConnection(true); |
| cho45 |
43:4de3870b39cb | 241 | } else { |
| cho45 |
43:4de3870b39cb | 242 | if (HIDController::status() == TIMEOUT) { |
| cho45 |
43:4de3870b39cb | 243 | DEBUG_PRINTF("wakeup\r\n"); |
| cho45 |
43:4de3870b39cb | 244 | HIDController::initializeConnection(false); |
| cho45 |
43:4de3870b39cb | 245 | } |
| cho45 |
43:4de3870b39cb | 246 | } |
| cho45 | 38:115875b8cb6c | 247 | } |
| cho45 |
43:4de3870b39cb | 248 | |
| cho45 | 23:b31957ce64e9 | 249 | if (queue) HIDController::queueCurrentReportData(); |
| cho45 |
43:4de3870b39cb | 250 | |
| cho45 | 47:5bf2ae8cc710 | 251 | // use timer to use wait 5ms |
| cho45 | 47:5bf2ae8cc710 | 252 | keyIntervalInterrupt = false; |
| cho45 | 36:78c211da4eb0 | 253 | timeout.attach_us(wakeupKeyIntervalSleep, 5000); |
| cho45 | 45:f4be69c936f6 | 254 | while (!keyIntervalInterrupt) HIDController::waitForEvent(); |
| cho45 |
7:b9270a37345b | 255 | } |
| cho45 |
8:d684faf04c9a | 256 | } else { |
| cho45 | 35:6a7fddfa14cf | 257 | if (!updateStatudLedEnabled) updateStatusLed(); |
| cho45 |
43:4de3870b39cb | 258 | |
| cho45 | 49:09ae6fb04a32 | 259 | { |
| cho45 | 49:09ae6fb04a32 | 260 | const float batteryVoltage = BatteryLevel::readBatteryVoltage(); |
| cho45 | 49:09ae6fb04a32 | 261 | const uint8_t batteryPercentage = BatteryLevel::readBatteryPercentage(batteryVoltage); |
| cho45 | 49:09ae6fb04a32 | 262 | const bool isLowBattery = batteryVoltage < BatteryLevel::BATTERY_LOW; |
| cho45 | 49:09ae6fb04a32 | 263 | |
| cho45 | 49:09ae6fb04a32 | 264 | DEBUG_PRINTF("%d%% [%d:%s] %s\r\n", |
| cho45 | 49:09ae6fb04a32 | 265 | batteryPercentage, |
| cho45 | 49:09ae6fb04a32 | 266 | HIDController::status(), |
| cho45 | 49:09ae6fb04a32 | 267 | HIDController::statusString(), |
| cho45 | 49:09ae6fb04a32 | 268 | isLowBattery ? "LOWBAT" : "WFE" |
| cho45 | 49:09ae6fb04a32 | 269 | ); |
| cho45 | 49:09ae6fb04a32 | 270 | |
| cho45 | 49:09ae6fb04a32 | 271 | HIDController::updateBatteryLevel(batteryPercentage); |
| cho45 | 49:09ae6fb04a32 | 272 | |
| cho45 | 49:09ae6fb04a32 | 273 | if (isLowBattery) { |
| cho45 | 49:09ae6fb04a32 | 274 | powerOff(); |
| cho45 | 49:09ae6fb04a32 | 275 | } |
| cho45 | 37:4ce71fa47fc3 | 276 | } |
| cho45 | 45:f4be69c936f6 | 277 | |
| cho45 | 45:f4be69c936f6 | 278 | if (HIDController::status() == DISCONNECTED) { |
| cho45 | 45:f4be69c936f6 | 279 | HIDController::initializeConnection(false); |
| cho45 | 45:f4be69c936f6 | 280 | } |
| cho45 |
43:4de3870b39cb | 281 | |
| cho45 | 42:2c3be8694896 | 282 | if (DEBUG_BLE_INTERRUPT) { |
| cho45 | 42:2c3be8694896 | 283 | HIDController::waitForEvent(); |
| cho45 | 42:2c3be8694896 | 284 | } else { |
| cho45 | 42:2c3be8694896 | 285 | // disable internal HFCLK RC Clock surely. It consume 1mA constantly |
| cho45 | 42:2c3be8694896 | 286 | // TWI / SPI / UART must be disabled and boot without debug mode |
| cho45 | 42:2c3be8694896 | 287 | while (NRF_UART0->EVENTS_TXDRDY != 1); |
| cho45 |
43:4de3870b39cb | 288 | |
| cho45 | 48:d6938de02f62 | 289 | const uint32_t tx = NRF_UART0->PSELTXD; |
| cho45 |
43:4de3870b39cb | 290 | |
| cho45 | 42:2c3be8694896 | 291 | NRF_UART0->TASKS_STOPTX = 1; |
| cho45 | 42:2c3be8694896 | 292 | NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos); |
| cho45 |
43:4de3870b39cb | 293 | |
| cho45 | 42:2c3be8694896 | 294 | HIDController::waitForEvent(); |
| cho45 |
43:4de3870b39cb | 295 | |
| cho45 | 42:2c3be8694896 | 296 | NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos); |
| cho45 | 42:2c3be8694896 | 297 | NRF_UART0->TASKS_STARTTX = 1; |
| cho45 | 42:2c3be8694896 | 298 | // dummy send to wakeup... |
| cho45 | 42:2c3be8694896 | 299 | NRF_UART0->PSELTXD = 0xFFFFFFFF; |
| cho45 |
43:4de3870b39cb | 300 | NRF_UART0->EVENTS_TXDRDY = 0; |
| cho45 |
43:4de3870b39cb | 301 | NRF_UART0->TXD = 0; |
| cho45 |
43:4de3870b39cb | 302 | while (NRF_UART0->EVENTS_TXDRDY != 1); |
| cho45 | 42:2c3be8694896 | 303 | NRF_UART0->PSELTXD = tx; |
| cho45 | 42:2c3be8694896 | 304 | } |
| cho45 |
7:b9270a37345b | 305 | } |
| cho45 |
2:c2e3f240640c | 306 | } |
| cho45 |
43:4de3870b39cb | 307 | } |