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@42:2c3be8694896, 2016-08-27 (annotated)
- Committer:
- cho45
- Date:
- Sat Aug 27 08:43:53 2016 +0000
- Revision:
- 42:2c3be8694896
- Parent:
- 41:2b034f22b98f
- Child:
- 43:4de3870b39cb
?????????
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 |
5:65d4e94735b6 | 29 | I2C i2c(I2C_SDA0, I2C_SCL0); |
| cho45 |
5:65d4e94735b6 | 30 | KeyboardMatrixController keyboardMatrixController(i2c); |
| cho45 |
6:f1c3ea8bc850 | 31 | Keymap keymap; |
| cho45 | 15:70bf079d3ee1 | 32 | |
| cho45 | 15:70bf079d3ee1 | 33 | // Interrupt from MCP23017 |
| cho45 | 15:70bf079d3ee1 | 34 | // (pulled-up and two MCP23017 is configured with open drain INT) |
| cho45 |
5:65d4e94735b6 | 35 | InterruptIn buttonInt(P0_5); |
| cho45 |
5:65d4e94735b6 | 36 | |
| cho45 | 37:4ce71fa47fc3 | 37 | #define PIN_STATUS_LED P0_4 |
| cho45 | 37:4ce71fa47fc3 | 38 | DigitalOut statusLed(PIN_STATUS_LED, 0); |
| cho45 | 25:094df0d9e95b | 39 | |
| cho45 | 35:6a7fddfa14cf | 40 | Timeout timeout; |
| cho45 | 35:6a7fddfa14cf | 41 | |
| cho45 |
5:65d4e94735b6 | 42 | // ROWS=8 |
| cho45 |
5:65d4e94735b6 | 43 | // COLS=16 |
| cho45 |
5:65d4e94735b6 | 44 | // 列ごとに1バイトにパックしてキーの状態を保持する |
| cho45 |
5:65d4e94735b6 | 45 | static uint8_t keysA[COLS]; |
| cho45 |
5:65d4e94735b6 | 46 | static uint8_t keysB[COLS]; |
| cho45 |
5:65d4e94735b6 | 47 | static bool state = 0; |
| cho45 | 28:1f843a3daab0 | 48 | #define is_pressed(keys, row, col) (!!(keys[col] & (1<<row))) |
| cho45 | 28:1f843a3daab0 | 49 | |
| cho45 |
9:d1daefbf1fbd | 50 | // delay for interrupt |
| cho45 | 23:b31957ce64e9 | 51 | static volatile int8_t pollCount = 50; |
| cho45 |
5:65d4e94735b6 | 52 | |
| cho45 |
5:65d4e94735b6 | 53 | void buttonIntCallback() { |
| cho45 |
8:d684faf04c9a | 54 | // just for wakeup |
| cho45 | 33:6a2301a89e92 | 55 | pollCount = 25; |
| cho45 |
2:c2e3f240640c | 56 | } |
| cho45 |
2:c2e3f240640c | 57 | |
| cho45 | 23:b31957ce64e9 | 58 | void powerOff() { |
| cho45 | 42:2c3be8694896 | 59 | DEBUG_PRINTF("power off\r\n"); |
| cho45 | 23:b31957ce64e9 | 60 | NRF_POWER->SYSTEMOFF = 1; |
| cho45 | 23:b31957ce64e9 | 61 | } |
| cho45 | 23:b31957ce64e9 | 62 | |
| cho45 | 26:78ee13f69ec3 | 63 | void tickerStatus() { |
| cho45 | 28:1f843a3daab0 | 64 | statusLed = !statusLed; |
| cho45 | 26:78ee13f69ec3 | 65 | } |
| cho45 | 26:78ee13f69ec3 | 66 | |
| cho45 | 35:6a7fddfa14cf | 67 | static bool updateStatudLedEnabled = false; |
| cho45 | 35:6a7fddfa14cf | 68 | void updateStatusLed() { |
| cho45 | 35:6a7fddfa14cf | 69 | switch (HIDController::status()) { |
| cho45 | 35:6a7fddfa14cf | 70 | case TIMEOUT: |
| cho45 | 38:115875b8cb6c | 71 | case DISCONNECTED: |
| cho45 | 38:115875b8cb6c | 72 | case CONNECTED: |
| cho45 | 38:115875b8cb6c | 73 | statusLed = 0; |
| cho45 | 38:115875b8cb6c | 74 | updateStatudLedEnabled = false; |
| cho45 | 38:115875b8cb6c | 75 | return; |
| cho45 | 35:6a7fddfa14cf | 76 | case ADVERTISING: |
| cho45 | 38:115875b8cb6c | 77 | case CONNECTING: |
| cho45 | 38:115875b8cb6c | 78 | statusLed = !statusLed; |
| cho45 | 38:115875b8cb6c | 79 | updateStatudLedEnabled = true; |
| cho45 | 38:115875b8cb6c | 80 | timeout.attach(updateStatusLed, statusLed ? 0.5 : 0.1); |
| cho45 | 38:115875b8cb6c | 81 | break; |
| cho45 | 35:6a7fddfa14cf | 82 | } |
| cho45 | 35:6a7fddfa14cf | 83 | } |
| cho45 | 35:6a7fddfa14cf | 84 | |
| cho45 | 36:78c211da4eb0 | 85 | static volatile bool keyIntervalInterrupt = false; |
| cho45 | 36:78c211da4eb0 | 86 | void wakeupKeyIntervalSleep() { |
| cho45 | 36:78c211da4eb0 | 87 | keyIntervalInterrupt = true; |
| cho45 | 30:f9ebc769118d | 88 | } |
| cho45 | 30:f9ebc769118d | 89 | |
| cho45 | 38:115875b8cb6c | 90 | #define is_pressed(keys, row, col) (!!(keys[col] & (1<<row))) |
| cho45 | 35:6a7fddfa14cf | 91 | |
| cho45 | 0:be89b5fdea09 | 92 | int main(void) { |
| cho45 | 33:6a2301a89e92 | 93 | { |
| cho45 | 33:6a2301a89e92 | 94 | uint32_t reason = NRF_POWER->RESETREAS; |
| cho45 | 33:6a2301a89e92 | 95 | NRF_POWER->RESETREAS = 0xffffffff; // clear reason |
| cho45 | 42:2c3be8694896 | 96 | serial.printf("init [%x]\r\n", reason); |
| cho45 | 33:6a2301a89e92 | 97 | } |
| cho45 | 37:4ce71fa47fc3 | 98 | |
| cho45 | 42:2c3be8694896 | 99 | float battery = BatteryLevel::readBatteryVoltage(); |
| cho45 | 42:2c3be8694896 | 100 | if (battery < BatteryLevel::BATTERY_LOW) { |
| cho45 | 37:4ce71fa47fc3 | 101 | powerOff(); |
| cho45 | 37:4ce71fa47fc3 | 102 | } |
| cho45 | 35:6a7fddfa14cf | 103 | |
| cho45 | 30:f9ebc769118d | 104 | // Enable Pin-reset on DEBUG mode |
| cho45 | 30:f9ebc769118d | 105 | // This makes possiable booting without normal mode easily. |
| cho45 | 30:f9ebc769118d | 106 | NRF_POWER->RESET = 1; |
| cho45 | 30:f9ebc769118d | 107 | // Disable Internal DC/DC step down converter surely |
| cho45 | 29:ec548c473d50 | 108 | NRF_POWER->DCDCEN = 0; |
| cho45 | 30:f9ebc769118d | 109 | // Enable 2.1V brown out detection for avoiding over discharge of NiMH |
| cho45 | 29:ec548c473d50 | 110 | NRF_POWER->POFCON = |
| cho45 | 29:ec548c473d50 | 111 | POWER_POFCON_POF_Enabled << POWER_POFCON_POF_Pos | |
| cho45 | 29:ec548c473d50 | 112 | POWER_POFCON_THRESHOLD_V21 << POWER_POFCON_THRESHOLD_Pos; |
| cho45 | 29:ec548c473d50 | 113 | |
| cho45 |
4:54cb552e50c4 | 114 | // mbed's Serial of TARGET_RBLAB_BLENANO sucks |
| cho45 | 30:f9ebc769118d | 115 | // DO NOT CONNECT RTS/CTS WITHOUT PRIOR CONSENT! |
| cho45 |
4:54cb552e50c4 | 116 | NRF_UART0->PSELRTS = 0xFFFFFFFFUL; |
| cho45 |
4:54cb552e50c4 | 117 | NRF_UART0->PSELCTS = 0xFFFFFFFFUL; |
| cho45 | 37:4ce71fa47fc3 | 118 | |
| cho45 | 37:4ce71fa47fc3 | 119 | // Set LED Pin as HIGH Current mode |
| cho45 | 37:4ce71fa47fc3 | 120 | NRF_GPIO->PIN_CNF[PIN_STATUS_LED] = |
| cho45 | 41:2b034f22b98f | 121 | (NRF_GPIO->PIN_CNF[PIN_STATUS_LED] & ~GPIO_PIN_CNF_DRIVE_Msk) | |
| cho45 | 41:2b034f22b98f | 122 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos); |
| cho45 | 16:345eebc4f259 | 123 | |
| cho45 | 42:2c3be8694896 | 124 | // Unsed pins. |
| cho45 | 42:2c3be8694896 | 125 | // Set as PullUp for power consumption |
| cho45 | 42:2c3be8694896 | 126 | // Use GPIO registers directly for saving ram |
| cho45 | 42:2c3be8694896 | 127 | // Pad pinouts |
| cho45 | 42:2c3be8694896 | 128 | /* |
| cho45 | 42:2c3be8694896 | 129 | DigitalIn unused_p0_7(P0_7, PullUp); |
| cho45 | 42:2c3be8694896 | 130 | DigitalIn unused_p0_6(P0_6, PullUp); |
| cho45 | 42:2c3be8694896 | 131 | DigitalIn unused_p0_15(P0_15, PullUp); |
| cho45 | 42:2c3be8694896 | 132 | DigitalIn unused_p0_29(P0_29, PullUp); |
| cho45 | 42:2c3be8694896 | 133 | DigitalIn unused_p0_28(P0_28, PullUp); |
| cho45 | 42:2c3be8694896 | 134 | */ |
| cho45 | 42:2c3be8694896 | 135 | NRF_GPIO->PIN_CNF[P0_7] = |
| cho45 | 42:2c3be8694896 | 136 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 137 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 138 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 139 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 140 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 141 | NRF_GPIO->PIN_CNF[P0_6] = |
| cho45 | 42:2c3be8694896 | 142 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 143 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 144 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 145 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 146 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 147 | NRF_GPIO->PIN_CNF[P0_15] = |
| cho45 | 42:2c3be8694896 | 148 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 149 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 150 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 151 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 152 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 153 | NRF_GPIO->PIN_CNF[P0_29] = |
| cho45 | 42:2c3be8694896 | 154 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 155 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 156 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 157 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 158 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 159 | NRF_GPIO->PIN_CNF[P0_28] = |
| cho45 | 42:2c3be8694896 | 160 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 161 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 162 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 163 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 164 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 165 | /* |
| cho45 | 42:2c3be8694896 | 166 | DigitalIn unused_p0_19(P0_19, PullUp); // This is on board LED which connected to VDD |
| cho45 | 42:2c3be8694896 | 167 | DigitalIn unused_p0_11(P0_11, PullUp); // RXD |
| cho45 | 42:2c3be8694896 | 168 | */ |
| cho45 | 42:2c3be8694896 | 169 | NRF_GPIO->PIN_CNF[P0_19] = // This is on board LED which connected to VDD |
| cho45 | 42:2c3be8694896 | 170 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 171 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 172 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 173 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 174 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 175 | NRF_GPIO->PIN_CNF[P0_11] = // RXD |
| cho45 | 42:2c3be8694896 | 176 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | |
| cho45 | 42:2c3be8694896 | 177 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | |
| cho45 | 42:2c3be8694896 | 178 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | |
| cho45 | 42:2c3be8694896 | 179 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
| cho45 | 42:2c3be8694896 | 180 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
| cho45 | 42:2c3be8694896 | 181 | |
| cho45 | 32:6c0f43fda460 | 182 | WatchDog::init(); |
| cho45 | 42:2c3be8694896 | 183 | |
| cho45 | 42:2c3be8694896 | 184 | // only 100kHz/250khz/400khz |
| cho45 | 36:78c211da4eb0 | 185 | i2c.frequency(250000); |
| cho45 |
4:54cb552e50c4 | 186 | |
| cho45 |
5:65d4e94735b6 | 187 | buttonInt.mode(PullUp); |
| cho45 |
5:65d4e94735b6 | 188 | buttonInt.fall(buttonIntCallback); |
| cho45 |
4:54cb552e50c4 | 189 | |
| cho45 |
5:65d4e94735b6 | 190 | keyboardMatrixController.init(); |
| cho45 | 33:6a2301a89e92 | 191 | pollCount = 10; |
| cho45 |
4:54cb552e50c4 | 192 | |
| cho45 |
6:f1c3ea8bc850 | 193 | HIDController::init(); |
| cho45 | 16:345eebc4f259 | 194 | |
| cho45 | 16:345eebc4f259 | 195 | // STOP UART RX for power consumption |
| cho45 | 21:d801c32231b0 | 196 | NRF_UART0->TASKS_STOPRX = 1; |
| cho45 | 16:345eebc4f259 | 197 | |
| cho45 | 30:f9ebc769118d | 198 | // Disable TWI by default. |
| cho45 | 16:345eebc4f259 | 199 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 | 35:6a7fddfa14cf | 200 | |
| cho45 | 32:6c0f43fda460 | 201 | while (1) { |
| cho45 | 32:6c0f43fda460 | 202 | WatchDog::reload(); |
| cho45 | 35:6a7fddfa14cf | 203 | |
| cho45 | 23:b31957ce64e9 | 204 | if (pollCount > 0) { |
| cho45 | 42:2c3be8694896 | 205 | DEBUG_PRINTF("scan keys\r\n"); |
| cho45 | 31:010a44d53627 | 206 | |
| cho45 | 33:6a2301a89e92 | 207 | while (pollCount-- > 0) { |
| cho45 | 33:6a2301a89e92 | 208 | WatchDog::reload(); |
| cho45 | 33:6a2301a89e92 | 209 | |
| cho45 | 23:b31957ce64e9 | 210 | uint8_t (&keysCurr)[COLS] = state ? keysA : keysB; |
| cho45 | 23:b31957ce64e9 | 211 | uint8_t (&keysPrev)[COLS] = state ? keysB : keysA; |
| cho45 | 23:b31957ce64e9 | 212 | |
| cho45 | 23:b31957ce64e9 | 213 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 | 23:b31957ce64e9 | 214 | keyboardMatrixController.scanKeyboard(keysCurr); |
| cho45 | 23:b31957ce64e9 | 215 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
| cho45 | 23:b31957ce64e9 | 216 | |
| cho45 | 23:b31957ce64e9 | 217 | bool queue = false; |
| cho45 | 23:b31957ce64e9 | 218 | |
| cho45 | 23:b31957ce64e9 | 219 | for (int col = 0; col < COLS; col++) { |
| cho45 | 23:b31957ce64e9 | 220 | uint8_t changed = keysPrev[col] ^ keysCurr[col]; |
| cho45 | 23:b31957ce64e9 | 221 | if (changed) queue = true; |
| cho45 | 23:b31957ce64e9 | 222 | for (int row = 0; row < ROWS; row++) { |
| cho45 | 23:b31957ce64e9 | 223 | if (changed & (1<<row)) { |
| cho45 | 23:b31957ce64e9 | 224 | bool pressed = keysCurr[col] & (1<<row); |
| cho45 | 42:2c3be8694896 | 225 | // DEBUG_KEYEVENT("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed); |
| cho45 | 23:b31957ce64e9 | 226 | keymap.execute(col, row, pressed); |
| cho45 | 23:b31957ce64e9 | 227 | } |
| cho45 |
7:b9270a37345b | 228 | } |
| cho45 |
7:b9270a37345b | 229 | } |
| cho45 | 23:b31957ce64e9 | 230 | state = !state; |
| cho45 | 35:6a7fddfa14cf | 231 | |
| cho45 | 38:115875b8cb6c | 232 | if (HIDController::status() == DISCONNECTED || |
| cho45 | 38:115875b8cb6c | 233 | HIDController::status() == TIMEOUT) { |
| cho45 | 38:115875b8cb6c | 234 | |
| cho45 | 38:115875b8cb6c | 235 | if ( |
| cho45 | 38:115875b8cb6c | 236 | is_pressed(keysCurr, 0, 0) && // left top 1 |
| cho45 | 38:115875b8cb6c | 237 | is_pressed(keysCurr, 1, 0) && // left top 2 |
| cho45 | 38:115875b8cb6c | 238 | is_pressed(keysCurr, 0, 15) // right top |
| cho45 | 38:115875b8cb6c | 239 | ) { |
| cho45 | 42:2c3be8694896 | 240 | DEBUG_PRINTF("re-init connection\r\n"); |
| cho45 | 40:364deaa190fe | 241 | HIDController::initializeConnection(true); |
| cho45 | 40:364deaa190fe | 242 | } else { |
| cho45 | 40:364deaa190fe | 243 | if (HIDController::status() == TIMEOUT) { |
| cho45 | 42:2c3be8694896 | 244 | DEBUG_PRINTF("wakeup\r\n"); |
| cho45 | 40:364deaa190fe | 245 | HIDController::initializeConnection(false); |
| cho45 | 40:364deaa190fe | 246 | } |
| cho45 | 38:115875b8cb6c | 247 | } |
| cho45 | 38:115875b8cb6c | 248 | } |
| cho45 | 35:6a7fddfa14cf | 249 | |
| cho45 | 23:b31957ce64e9 | 250 | if (queue) HIDController::queueCurrentReportData(); |
| cho45 | 30:f9ebc769118d | 251 | |
| cho45 | 31:010a44d53627 | 252 | // wait_ms(5); is busy loop |
| cho45 | 31:010a44d53627 | 253 | // use timer1 to use wait 5ms |
| cho45 | 36:78c211da4eb0 | 254 | timeout.attach_us(wakeupKeyIntervalSleep, 5000); |
| cho45 | 36:78c211da4eb0 | 255 | keyIntervalInterrupt = false; |
| cho45 | 36:78c211da4eb0 | 256 | while (!keyIntervalInterrupt) sleep(); |
| cho45 |
7:b9270a37345b | 257 | } |
| cho45 |
8:d684faf04c9a | 258 | } else { |
| cho45 | 35:6a7fddfa14cf | 259 | if (!updateStatudLedEnabled) updateStatusLed(); |
| cho45 | 37:4ce71fa47fc3 | 260 | |
| cho45 | 42:2c3be8694896 | 261 | float batteryVoltage = BatteryLevel::readBatteryVoltage(); |
| cho45 | 42:2c3be8694896 | 262 | uint8_t batteryPercentage = BatteryLevel::readBatteryPercentage(batteryVoltage); |
| cho45 | 42:2c3be8694896 | 263 | bool isLowBattery = batteryVoltage < BatteryLevel::BATTERY_LOW; |
| cho45 | 37:4ce71fa47fc3 | 264 | |
| cho45 | 42:2c3be8694896 | 265 | DEBUG_PRINTF("%d%% [%d:%s] %s\r\n", |
| cho45 | 37:4ce71fa47fc3 | 266 | batteryPercentage, |
| cho45 | 37:4ce71fa47fc3 | 267 | HIDController::status(), |
| cho45 | 37:4ce71fa47fc3 | 268 | HIDController::statusString(), |
| cho45 | 39:b7889285c9ef | 269 | isLowBattery ? "LOWBAT" : "WFE" |
| cho45 | 37:4ce71fa47fc3 | 270 | ); |
| cho45 | 37:4ce71fa47fc3 | 271 | |
| cho45 | 37:4ce71fa47fc3 | 272 | HIDController::updateBatteryLevel(batteryPercentage); |
| cho45 | 37:4ce71fa47fc3 | 273 | |
| cho45 | 37:4ce71fa47fc3 | 274 | if (isLowBattery) { |
| cho45 | 37:4ce71fa47fc3 | 275 | powerOff(); |
| cho45 | 37:4ce71fa47fc3 | 276 | } |
| cho45 | 29:ec548c473d50 | 277 | |
| cho45 | 39:b7889285c9ef | 278 | |
| cho45 | 42:2c3be8694896 | 279 | if (DEBUG_BLE_INTERRUPT) { |
| cho45 | 42:2c3be8694896 | 280 | HIDController::waitForEvent(); |
| cho45 | 42:2c3be8694896 | 281 | } else { |
| cho45 | 42:2c3be8694896 | 282 | // disable internal HFCLK RC Clock surely. It consume 1mA constantly |
| cho45 | 42:2c3be8694896 | 283 | // TWI / SPI / UART must be disabled and boot without debug mode |
| cho45 | 42:2c3be8694896 | 284 | while (NRF_UART0->EVENTS_TXDRDY != 1); |
| cho45 | 42:2c3be8694896 | 285 | |
| cho45 | 42:2c3be8694896 | 286 | uint32_t tx = NRF_UART0->PSELTXD; |
| cho45 | 42:2c3be8694896 | 287 | |
| cho45 | 42:2c3be8694896 | 288 | NRF_UART0->TASKS_STOPTX = 1; |
| cho45 | 42:2c3be8694896 | 289 | NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos); |
| cho45 | 42:2c3be8694896 | 290 | |
| cho45 | 42:2c3be8694896 | 291 | HIDController::waitForEvent(); |
| cho45 | 42:2c3be8694896 | 292 | |
| cho45 | 42:2c3be8694896 | 293 | NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos); |
| cho45 | 42:2c3be8694896 | 294 | NRF_UART0->TASKS_STARTTX = 1; |
| cho45 | 42:2c3be8694896 | 295 | // dummy send to wakeup... |
| cho45 | 42:2c3be8694896 | 296 | NRF_UART0->PSELTXD = 0xFFFFFFFF; |
| cho45 | 42:2c3be8694896 | 297 | NRF_UART0->EVENTS_TXDRDY = 0; |
| cho45 | 42:2c3be8694896 | 298 | NRF_UART0->TXD = 0; |
| cho45 | 42:2c3be8694896 | 299 | while (NRF_UART0->EVENTS_TXDRDY != 1); |
| cho45 | 42:2c3be8694896 | 300 | NRF_UART0->PSELTXD = tx; |
| cho45 | 42:2c3be8694896 | 301 | } |
| cho45 |
7:b9270a37345b | 302 | } |
| cho45 |
2:c2e3f240640c | 303 | } |
| cho45 | 31:010a44d53627 | 304 | } |