Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Tue Aug 23 15:25:30 2016 +0000
Revision:
28:1f843a3daab0
Parent:
27:7370b8994603
Child:
29:ec548c473d50
MyTimerEvent ????? stuck ??????????????RTC0 ?????????RTC1 ?????????????????????????????mbed ???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cho45 0:be89b5fdea09 1 /* mbed Microcontroller Library
cho45 0:be89b5fdea09 2 * Copyright (c) 2015 ARM Limited
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 28:1f843a3daab0 17 // ignore load us_ticker_api
cho45 0:be89b5fdea09 18 #include "mbed.h"
cho45 6:f1c3ea8bc850 19
cho45 6:f1c3ea8bc850 20 #include "HIDController_BLE.h"
cho45 6:f1c3ea8bc850 21
cho45 4:54cb552e50c4 22 #include "mcp23017.h"
cho45 5:65d4e94735b6 23 #include "keymap.h"
cho45 0:be89b5fdea09 24
cho45 0:be89b5fdea09 25
cho45 5:65d4e94735b6 26 class KeyboardMatrixController {
cho45 5:65d4e94735b6 27 I2C& i2c;
cho45 5:65d4e94735b6 28 MCP23017 gpio1;
cho45 5:65d4e94735b6 29 MCP23017 gpio2;
cho45 13:b0ffdf2012b9 30 bool gpio1_ready;
cho45 13:b0ffdf2012b9 31 bool gpio2_ready;
cho45 5:65d4e94735b6 32
cho45 5:65d4e94735b6 33 static const uint8_t GPIO1_SLAVE_ADDRESS = 0b0100000;
cho45 14:3a8c126b7834 34 static const uint8_t GPIO2_SLAVE_ADDRESS = 0b0100100;
cho45 5:65d4e94735b6 35
cho45 5:65d4e94735b6 36 /**
cho45 5:65d4e94735b6 37 * COL=GPIOA (output normaly positive)
cho45 5:65d4e94735b6 38 * ROW=GPIOB (input pulled-up)
cho45 5:65d4e94735b6 39 */
cho45 5:65d4e94735b6 40
cho45 13:b0ffdf2012b9 41 bool setupGpio(MCP23017& gpio) {
cho45 5:65d4e94735b6 42 int ok;
cho45 13:b0ffdf2012b9 43 printf("SET IOCON\r\n");
cho45 5:65d4e94735b6 44 ok = gpio.write8(
cho45 5:65d4e94735b6 45 MCP23017::IOCON,
cho45 5:65d4e94735b6 46 0<<MCP23017::BANK |
cho45 5:65d4e94735b6 47 1<<MCP23017::MIRROR |
cho45 5:65d4e94735b6 48 1<<MCP23017::SEQOP |
cho45 5:65d4e94735b6 49 0<<MCP23017::DISSLW |
cho45 5:65d4e94735b6 50 1<<MCP23017::ODR // int pin is open drain
cho45 5:65d4e94735b6 51 );
cho45 13:b0ffdf2012b9 52 if (!ok) return false;
cho45 5:65d4e94735b6 53
cho45 5:65d4e94735b6 54 // IODIR
cho45 5:65d4e94735b6 55 // 1: input
cho45 5:65d4e94735b6 56 // 0: output
cho45 13:b0ffdf2012b9 57 printf("SET IODIRA\r\n");
cho45 5:65d4e94735b6 58 ok = gpio.write16(
cho45 5:65d4e94735b6 59 MCP23017::IODIRA,
cho45 5:65d4e94735b6 60 0b0000000011111111
cho45 5:65d4e94735b6 61 );
cho45 13:b0ffdf2012b9 62 if (!ok) return false;
cho45 4:54cb552e50c4 63
cho45 5:65d4e94735b6 64 // INPUT POLARITY
cho45 5:65d4e94735b6 65 // 1: inverse polarity
cho45 5:65d4e94735b6 66 // 0: raw
cho45 13:b0ffdf2012b9 67 printf("SET IPOLB\r\n");
cho45 5:65d4e94735b6 68 ok = gpio.write8(
cho45 5:65d4e94735b6 69 MCP23017::IPOLB,
cho45 5:65d4e94735b6 70 0b11111111
cho45 5:65d4e94735b6 71 );
cho45 13:b0ffdf2012b9 72 if (!ok) return false;
cho45 5:65d4e94735b6 73
cho45 5:65d4e94735b6 74 // INTERRUPT-ON-CHANGE Enable
cho45 13:b0ffdf2012b9 75 printf("SET GPINTENB\r\n");
cho45 5:65d4e94735b6 76 ok = gpio.write8(
cho45 5:65d4e94735b6 77 MCP23017::GPINTENB,
cho45 5:65d4e94735b6 78 0b11111111
cho45 5:65d4e94735b6 79 );
cho45 13:b0ffdf2012b9 80 if (!ok) return false;
cho45 13:b0ffdf2012b9 81
cho45 5:65d4e94735b6 82 // INTERRUPT-ON-CHANGE Control
cho45 5:65d4e94735b6 83 // 1: compared with DEFVAL
cho45 5:65d4e94735b6 84 // 0: compared to previous value
cho45 13:b0ffdf2012b9 85 printf("SET INTCONB\r\n");
cho45 5:65d4e94735b6 86 ok = gpio.write8(
cho45 5:65d4e94735b6 87 MCP23017::INTCONB,
cho45 5:65d4e94735b6 88 0b00000000
cho45 5:65d4e94735b6 89 );
cho45 13:b0ffdf2012b9 90 if (!ok) return false;
cho45 13:b0ffdf2012b9 91
cho45 5:65d4e94735b6 92 // PULL-UP (for input pin)
cho45 5:65d4e94735b6 93 // 1: pull-up enabled
cho45 5:65d4e94735b6 94 // 0: pull-up disabled
cho45 13:b0ffdf2012b9 95 printf("SET GPPUB\r\n");
cho45 5:65d4e94735b6 96 ok = gpio.write8(
cho45 5:65d4e94735b6 97 MCP23017::GPPUB,
cho45 5:65d4e94735b6 98 0b11111111
cho45 5:65d4e94735b6 99 );
cho45 13:b0ffdf2012b9 100 if (!ok) return false;
cho45 5:65d4e94735b6 101
cho45 13:b0ffdf2012b9 102 printf("SET GPIOA\r\n");
cho45 5:65d4e94735b6 103 ok = gpio1.write8(
cho45 5:65d4e94735b6 104 MCP23017::GPIOA,
cho45 5:65d4e94735b6 105 0b00000000
cho45 5:65d4e94735b6 106 );
cho45 13:b0ffdf2012b9 107 if (!ok) return false;
cho45 5:65d4e94735b6 108
cho45 13:b0ffdf2012b9 109 return true;
cho45 5:65d4e94735b6 110 }
cho45 5:65d4e94735b6 111
cho45 4:54cb552e50c4 112 public:
cho45 5:65d4e94735b6 113 KeyboardMatrixController(I2C& _i2c) :
cho45 5:65d4e94735b6 114 i2c(_i2c),
cho45 5:65d4e94735b6 115 gpio1(i2c, GPIO1_SLAVE_ADDRESS),
cho45 5:65d4e94735b6 116 gpio2(i2c, GPIO2_SLAVE_ADDRESS)
cho45 4:54cb552e50c4 117 {
cho45 4:54cb552e50c4 118 }
cho45 4:54cb552e50c4 119
cho45 5:65d4e94735b6 120 void init() {
cho45 13:b0ffdf2012b9 121 printf("init gpio1\r\n");
cho45 13:b0ffdf2012b9 122 gpio1_ready = setupGpio(gpio1);
cho45 13:b0ffdf2012b9 123 printf("gpio1 initialized: %s\r\n", gpio1_ready ? "success" : "failed");
cho45 13:b0ffdf2012b9 124
cho45 13:b0ffdf2012b9 125 printf("init gpio2\r\n");
cho45 13:b0ffdf2012b9 126 gpio2_ready = setupGpio(gpio2);
cho45 13:b0ffdf2012b9 127 printf("gpio2 initialized: %s\r\n", gpio2_ready ? "success" : "failed");
cho45 13:b0ffdf2012b9 128
cho45 5:65d4e94735b6 129 }
cho45 5:65d4e94735b6 130
cho45 5:65d4e94735b6 131 void scanKeyboard(uint8_t* keys) {
cho45 5:65d4e94735b6 132 int ok;
cho45 13:b0ffdf2012b9 133
cho45 13:b0ffdf2012b9 134 disableInterrupt();
cho45 5:65d4e94735b6 135
cho45 13:b0ffdf2012b9 136 if (gpio1_ready) {
cho45 13:b0ffdf2012b9 137 for (int i = 0; i < 8; i++) {
cho45 13:b0ffdf2012b9 138 ok = gpio1.write8(
cho45 13:b0ffdf2012b9 139 MCP23017::GPIOA,
cho45 13:b0ffdf2012b9 140 ~(1<<i)
cho45 13:b0ffdf2012b9 141 );
cho45 13:b0ffdf2012b9 142 keys[i] = gpio1.read8(MCP23017::GPIOB, ok);
cho45 13:b0ffdf2012b9 143 }
cho45 13:b0ffdf2012b9 144
cho45 13:b0ffdf2012b9 145 // set all output to negative for interrupt
cho45 5:65d4e94735b6 146 ok = gpio1.write8(
cho45 5:65d4e94735b6 147 MCP23017::GPIOA,
cho45 13:b0ffdf2012b9 148 0b00000000
cho45 5:65d4e94735b6 149 );
cho45 4:54cb552e50c4 150 }
cho45 2:c2e3f240640c 151
cho45 5:65d4e94735b6 152
cho45 13:b0ffdf2012b9 153 if (gpio2_ready) {
cho45 13:b0ffdf2012b9 154 for (int i = 0; i < 8; i++) {
cho45 13:b0ffdf2012b9 155 ok = gpio2.write8(
cho45 13:b0ffdf2012b9 156 MCP23017::GPIOA,
cho45 13:b0ffdf2012b9 157 ~(1<<i)
cho45 13:b0ffdf2012b9 158 );
cho45 13:b0ffdf2012b9 159 keys[i+8] = gpio2.read8(MCP23017::GPIOB, ok);
cho45 13:b0ffdf2012b9 160 }
cho45 13:b0ffdf2012b9 161
cho45 13:b0ffdf2012b9 162 // set all output to negative for interrupt
cho45 13:b0ffdf2012b9 163 ok = gpio2.write8(
cho45 13:b0ffdf2012b9 164 MCP23017::GPIOA,
cho45 13:b0ffdf2012b9 165 0b00000000
cho45 13:b0ffdf2012b9 166 );
cho45 13:b0ffdf2012b9 167 }
cho45 13:b0ffdf2012b9 168
cho45 13:b0ffdf2012b9 169 enableInterrupt();
cho45 7:b9270a37345b 170 }
cho45 13:b0ffdf2012b9 171
cho45 7:b9270a37345b 172 void disableInterrupt() {
cho45 7:b9270a37345b 173 int ok;
cho45 13:b0ffdf2012b9 174 if (gpio1_ready) {
cho45 13:b0ffdf2012b9 175 // Disable interrupt
cho45 13:b0ffdf2012b9 176 ok = gpio1.write8(
cho45 13:b0ffdf2012b9 177 MCP23017::GPINTENB,
cho45 13:b0ffdf2012b9 178 0b00000000
cho45 13:b0ffdf2012b9 179 );
cho45 13:b0ffdf2012b9 180 }
cho45 13:b0ffdf2012b9 181
cho45 13:b0ffdf2012b9 182 if (gpio2_ready) {
cho45 13:b0ffdf2012b9 183 // Disable interrupt
cho45 13:b0ffdf2012b9 184 ok = gpio2.write8(
cho45 13:b0ffdf2012b9 185 MCP23017::GPINTENB,
cho45 13:b0ffdf2012b9 186 0b00000000
cho45 13:b0ffdf2012b9 187 );
cho45 13:b0ffdf2012b9 188 }
cho45 7:b9270a37345b 189 }
cho45 7:b9270a37345b 190
cho45 7:b9270a37345b 191 void enableInterrupt() {
cho45 7:b9270a37345b 192 int ok;
cho45 13:b0ffdf2012b9 193 if (gpio1_ready) {
cho45 13:b0ffdf2012b9 194 // Enable interrupt
cho45 13:b0ffdf2012b9 195 ok = gpio1.write8(
cho45 13:b0ffdf2012b9 196 MCP23017::GPINTENB,
cho45 13:b0ffdf2012b9 197 0b11111111
cho45 13:b0ffdf2012b9 198 );
cho45 13:b0ffdf2012b9 199 }
cho45 13:b0ffdf2012b9 200
cho45 13:b0ffdf2012b9 201 if (gpio2_ready) {
cho45 13:b0ffdf2012b9 202 // Enable interrupt
cho45 13:b0ffdf2012b9 203 ok = gpio2.write8(
cho45 13:b0ffdf2012b9 204 MCP23017::GPINTENB,
cho45 13:b0ffdf2012b9 205 0b11111111
cho45 13:b0ffdf2012b9 206 );
cho45 13:b0ffdf2012b9 207 }
cho45 5:65d4e94735b6 208
cho45 5:65d4e94735b6 209 // Clear interrupt
cho45 13:b0ffdf2012b9 210 // gpio1.read8(MCP23017::GPIOB, ok);
cho45 4:54cb552e50c4 211 }
cho45 5:65d4e94735b6 212 };
cho45 4:54cb552e50c4 213
cho45 5:65d4e94735b6 214 I2C i2c(I2C_SDA0, I2C_SCL0);
cho45 16:345eebc4f259 215 // Serial serial(USBTX, USBRX);
cho45 5:65d4e94735b6 216 KeyboardMatrixController keyboardMatrixController(i2c);
cho45 6:f1c3ea8bc850 217 Keymap keymap;
cho45 15:70bf079d3ee1 218
cho45 15:70bf079d3ee1 219 // Interrupt from MCP23017
cho45 15:70bf079d3ee1 220 // (pulled-up and two MCP23017 is configured with open drain INT)
cho45 5:65d4e94735b6 221 InterruptIn buttonInt(P0_5);
cho45 5:65d4e94735b6 222
cho45 28:1f843a3daab0 223 DigitalOut statusLed(P0_4, 0);
cho45 26:78ee13f69ec3 224
cho45 25:094df0d9e95b 225
cho45 15:70bf079d3ee1 226 // Unsed pins. Set to output for power consumption
cho45 20:d8840ac38434 227 DigitalIn unused_p0_7(P0_7, PullUp);
cho45 20:d8840ac38434 228 DigitalIn unused_p0_6(P0_6, PullUp);
cho45 20:d8840ac38434 229 DigitalIn unused_p0_15(P0_15, PullUp);
cho45 20:d8840ac38434 230 DigitalIn unused_p0_29(P0_29, PullUp);
cho45 20:d8840ac38434 231 DigitalIn unused_p0_28(P0_28, PullUp);
cho45 20:d8840ac38434 232 DigitalIn unused_p0_19(P0_19, PullUp); // This is on board LED which connected to VDD
cho45 22:a78f0a91280a 233 DigitalIn unused_p0_11(P0_11, PullUp); // RXD
cho45 15:70bf079d3ee1 234
cho45 5:65d4e94735b6 235 // ROWS=8
cho45 5:65d4e94735b6 236 // COLS=16
cho45 5:65d4e94735b6 237 // 列ごとに1バイトにパックしてキーの状態を保持する
cho45 5:65d4e94735b6 238 static uint8_t keysA[COLS];
cho45 5:65d4e94735b6 239 static uint8_t keysB[COLS];
cho45 5:65d4e94735b6 240 static bool state = 0;
cho45 28:1f843a3daab0 241 #define is_pressed(keys, row, col) (!!(keys[col] & (1<<row)))
cho45 28:1f843a3daab0 242
cho45 9:d1daefbf1fbd 243 // delay for interrupt
cho45 23:b31957ce64e9 244 static volatile int8_t pollCount = 50;
cho45 5:65d4e94735b6 245
cho45 5:65d4e94735b6 246 void buttonIntCallback() {
cho45 8:d684faf04c9a 247 // just for wakeup
cho45 9:d1daefbf1fbd 248 pollCount = 100;
cho45 2:c2e3f240640c 249 }
cho45 2:c2e3f240640c 250
cho45 23:b31957ce64e9 251 void powerOff() {
cho45 23:b31957ce64e9 252 printf("power off\r\n");
cho45 23:b31957ce64e9 253 NRF_POWER->SYSTEMOFF = 1;
cho45 23:b31957ce64e9 254 }
cho45 23:b31957ce64e9 255
cho45 28:1f843a3daab0 256 /*
cho45 28:1f843a3daab0 257 class MyTimerEvent {
cho45 28:1f843a3daab0 258 public:
cho45 28:1f843a3daab0 259 static void irq(uint32_t id) {
cho45 28:1f843a3daab0 260 MyTimerEvent *timer_event = (MyTimerEvent*)id;
cho45 28:1f843a3daab0 261 timer_event->handler();
cho45 28:1f843a3daab0 262 }
cho45 28:1f843a3daab0 263
cho45 28:1f843a3daab0 264 MyTimerEvent() : event(), _ticker_data(get_us_ticker_data()) {
cho45 28:1f843a3daab0 265 ticker_set_handler(_ticker_data, (&MyTimerEvent::irq));
cho45 28:1f843a3daab0 266 }
cho45 28:1f843a3daab0 267 virtual ~MyTimerEvent() {
cho45 28:1f843a3daab0 268 remove();
cho45 28:1f843a3daab0 269 }
cho45 28:1f843a3daab0 270
cho45 28:1f843a3daab0 271 protected:
cho45 28:1f843a3daab0 272 // The handler called to service the timer event of the derived class
cho45 28:1f843a3daab0 273 virtual void handler() = 0;
cho45 28:1f843a3daab0 274
cho45 28:1f843a3daab0 275 // insert in to linked list
cho45 28:1f843a3daab0 276 void insert(timestamp_t timestamp) {
cho45 28:1f843a3daab0 277 ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this);
cho45 28:1f843a3daab0 278 }
cho45 28:1f843a3daab0 279
cho45 28:1f843a3daab0 280 // remove from linked list, if in it
cho45 28:1f843a3daab0 281 void remove() {
cho45 28:1f843a3daab0 282 ticker_remove_event(_ticker_data, &event);
cho45 28:1f843a3daab0 283 }
cho45 28:1f843a3daab0 284
cho45 28:1f843a3daab0 285 ticker_event_t event;
cho45 28:1f843a3daab0 286
cho45 28:1f843a3daab0 287 const ticker_data_t *_ticker_data;
cho45 28:1f843a3daab0 288 };
cho45 28:1f843a3daab0 289
cho45 28:1f843a3daab0 290 class MyTicker : public MyTimerEvent {
cho45 28:1f843a3daab0 291
cho45 28:1f843a3daab0 292 public:
cho45 28:1f843a3daab0 293 MyTicker() : MyTimerEvent() {
cho45 28:1f843a3daab0 294 }
cho45 28:1f843a3daab0 295
cho45 28:1f843a3daab0 296 void attach(void (*fptr)(void), float t) {
cho45 28:1f843a3daab0 297 attach_us(fptr, t * 1000000.0f);
cho45 28:1f843a3daab0 298 }
cho45 28:1f843a3daab0 299
cho45 28:1f843a3daab0 300 void attach_us(void (*fptr)(void), timestamp_t t) {
cho45 28:1f843a3daab0 301 _function.attach(fptr);
cho45 28:1f843a3daab0 302 setup(t);
cho45 28:1f843a3daab0 303 }
cho45 28:1f843a3daab0 304
cho45 28:1f843a3daab0 305 virtual ~MyTicker() {
cho45 28:1f843a3daab0 306 detach();
cho45 28:1f843a3daab0 307 }
cho45 28:1f843a3daab0 308
cho45 28:1f843a3daab0 309 void detach() {
cho45 28:1f843a3daab0 310 remove();
cho45 28:1f843a3daab0 311 _function.attach(0);
cho45 28:1f843a3daab0 312 }
cho45 28:1f843a3daab0 313
cho45 28:1f843a3daab0 314 protected:
cho45 28:1f843a3daab0 315 void setup(timestamp_t t) {
cho45 28:1f843a3daab0 316 remove();
cho45 28:1f843a3daab0 317 _delay = t;
cho45 28:1f843a3daab0 318 insert(_delay + ticker_read(_ticker_data));
cho45 28:1f843a3daab0 319 }
cho45 28:1f843a3daab0 320 virtual void handler() {
cho45 28:1f843a3daab0 321 insert(event.timestamp + _delay);
cho45 28:1f843a3daab0 322 _function.call();
cho45 28:1f843a3daab0 323 }
cho45 28:1f843a3daab0 324
cho45 28:1f843a3daab0 325 protected:
cho45 28:1f843a3daab0 326 timestamp_t _delay;
cho45 28:1f843a3daab0 327 FunctionPointer _function;
cho45 28:1f843a3daab0 328 };
cho45 28:1f843a3daab0 329
cho45 28:1f843a3daab0 330 MyTicker ticker;
cho45 28:1f843a3daab0 331 */
cho45 28:1f843a3daab0 332
cho45 26:78ee13f69ec3 333 void tickerStatus() {
cho45 28:1f843a3daab0 334 statusLed = !statusLed;
cho45 26:78ee13f69ec3 335 }
cho45 26:78ee13f69ec3 336
cho45 28:1f843a3daab0 337
cho45 0:be89b5fdea09 338 int main(void) {
cho45 5:65d4e94735b6 339 printf("init\r\n");
cho45 4:54cb552e50c4 340
cho45 4:54cb552e50c4 341 // mbed's Serial of TARGET_RBLAB_BLENANO sucks
cho45 4:54cb552e50c4 342 // DO NOT CONNECT RTS/CTS AUTOMATICALY!
cho45 4:54cb552e50c4 343 NRF_UART0->PSELRTS = 0xFFFFFFFFUL;
cho45 4:54cb552e50c4 344 NRF_UART0->PSELCTS = 0xFFFFFFFFUL;
cho45 16:345eebc4f259 345
cho45 4:54cb552e50c4 346 // 100kHz
cho45 4:54cb552e50c4 347 i2c.frequency(100000);
cho45 4:54cb552e50c4 348
cho45 5:65d4e94735b6 349 buttonInt.mode(PullUp);
cho45 5:65d4e94735b6 350 buttonInt.fall(buttonIntCallback);
cho45 4:54cb552e50c4 351
cho45 5:65d4e94735b6 352 keyboardMatrixController.init();
cho45 5:65d4e94735b6 353 buttonIntCallback();
cho45 4:54cb552e50c4 354
cho45 6:f1c3ea8bc850 355 HIDController::init();
cho45 16:345eebc4f259 356
cho45 16:345eebc4f259 357 // STOP UART RX for power consumption
cho45 21:d801c32231b0 358 NRF_UART0->TASKS_STOPRX = 1;
cho45 16:345eebc4f259 359
cho45 16:345eebc4f259 360 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
cho45 23:b31957ce64e9 361
cho45 23:b31957ce64e9 362
cho45 27:7370b8994603 363 // ticker.attach(tickerStatus, 0.5);
cho45 5:65d4e94735b6 364 while (1) {
cho45 27:7370b8994603 365 /*
cho45 27:7370b8994603 366 ticker.detach();
cho45 27:7370b8994603 367 switch (HIDController::status()) {
cho45 27:7370b8994603 368 case DISCONNECTED: ticker.attach(tickerStatus, 1); break;
cho45 27:7370b8994603 369 case CONNECTING: ticker.attach(tickerStatus, 2); break;
cho45 27:7370b8994603 370 case CONNECTED: ticker.attach(tickerStatus, 60);break;
cho45 27:7370b8994603 371 }
cho45 27:7370b8994603 372 */
cho45 27:7370b8994603 373
cho45 27:7370b8994603 374
cho45 23:b31957ce64e9 375 if (pollCount > 0) {
cho45 23:b31957ce64e9 376 printf("scan keys\r\n");
cho45 23:b31957ce64e9 377 while (pollCount -- > 0) {
cho45 23:b31957ce64e9 378 uint8_t (&keysCurr)[COLS] = state ? keysA : keysB;
cho45 23:b31957ce64e9 379 uint8_t (&keysPrev)[COLS] = state ? keysB : keysA;
cho45 23:b31957ce64e9 380
cho45 23:b31957ce64e9 381 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
cho45 23:b31957ce64e9 382 keyboardMatrixController.scanKeyboard(keysCurr);
cho45 23:b31957ce64e9 383 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
cho45 23:b31957ce64e9 384
cho45 23:b31957ce64e9 385 bool queue = false;
cho45 23:b31957ce64e9 386
cho45 23:b31957ce64e9 387 for (int col = 0; col < COLS; col++) {
cho45 23:b31957ce64e9 388 uint8_t changed = keysPrev[col] ^ keysCurr[col];
cho45 23:b31957ce64e9 389 if (changed) queue = true;
cho45 23:b31957ce64e9 390 for (int row = 0; row < ROWS; row++) {
cho45 23:b31957ce64e9 391 if (changed & (1<<row)) {
cho45 23:b31957ce64e9 392 bool pressed = keysCurr[col] & (1<<row);
cho45 23:b31957ce64e9 393 // printf("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed);
cho45 23:b31957ce64e9 394 keymap.execute(col, row, pressed);
cho45 23:b31957ce64e9 395 }
cho45 7:b9270a37345b 396 }
cho45 7:b9270a37345b 397 }
cho45 23:b31957ce64e9 398 state = !state;
cho45 23:b31957ce64e9 399
cho45 23:b31957ce64e9 400
cho45 23:b31957ce64e9 401 if (queue) HIDController::queueCurrentReportData();
cho45 23:b31957ce64e9 402 wait_ms(5);
cho45 7:b9270a37345b 403 }
cho45 8:d684faf04c9a 404 } else {
cho45 13:b0ffdf2012b9 405 printf("wait for events...\r\n");
cho45 16:345eebc4f259 406
cho45 21:d801c32231b0 407 /*// save 50uA
cho45 16:345eebc4f259 408 while (NRF_UART0->EVENTS_TXDRDY != 1);
cho45 16:345eebc4f259 409
cho45 16:345eebc4f259 410 uint32_t tx = NRF_UART0->PSELTXD;
cho45 16:345eebc4f259 411
cho45 14:3a8c126b7834 412 NRF_UART0->TASKS_STOPTX = 1;
cho45 14:3a8c126b7834 413 NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
cho45 21:d801c32231b0 414 /**/
cho45 28:1f843a3daab0 415
cho45 28:1f843a3daab0 416 printf("COUNTER %x\r\n", NRF_RTC1->COUNTER);
cho45 10:1aed2481a743 417 HIDController::waitForEvent();
cho45 16:345eebc4f259 418
cho45 23:b31957ce64e9 419
cho45 21:d801c32231b0 420 /*/
cho45 14:3a8c126b7834 421 NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
cho45 14:3a8c126b7834 422 NRF_UART0->TASKS_STARTTX = 1;
cho45 16:345eebc4f259 423 NRF_UART0->PSELTXD = 0xFFFFFFFF;
cho45 16:345eebc4f259 424 NRF_UART0->EVENTS_TXDRDY = 0;
cho45 16:345eebc4f259 425 NRF_UART0->TXD = 0;
cho45 16:345eebc4f259 426 while (NRF_UART0->EVENTS_TXDRDY != 1);
cho45 16:345eebc4f259 427 NRF_UART0->PSELTXD = tx;
cho45 21:d801c32231b0 428 /**/
cho45 7:b9270a37345b 429 }
cho45 2:c2e3f240640c 430 }
cho45 2:c2e3f240640c 431 }