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@8:d684faf04c9a, 2016-07-21 (annotated)
- Committer:
- cho45
- Date:
- Thu Jul 21 08:45:27 2016 +0900
- Revision:
- 8:d684faf04c9a
- Parent:
- 7:b9270a37345b
- Child:
- 9:d1daefbf1fbd
ble
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:be89b5fdea09 | 17 | #include "mbed.h" |
| cho45 |
6:f1c3ea8bc850 | 18 | |
| cho45 |
6:f1c3ea8bc850 | 19 | #include "HIDController_BLE.h" |
| cho45 |
6:f1c3ea8bc850 | 20 | |
| cho45 |
4:54cb552e50c4 | 21 | #include "mcp23017.h" |
| cho45 |
5:65d4e94735b6 | 22 | #include "keymap.h" |
| cho45 | 0:be89b5fdea09 | 23 | |
| cho45 | 0:be89b5fdea09 | 24 | |
| cho45 |
5:65d4e94735b6 | 25 | class KeyboardMatrixController { |
| cho45 |
5:65d4e94735b6 | 26 | I2C& i2c; |
| cho45 |
5:65d4e94735b6 | 27 | MCP23017 gpio1; |
| cho45 |
5:65d4e94735b6 | 28 | MCP23017 gpio2; |
| cho45 |
5:65d4e94735b6 | 29 | |
| cho45 |
5:65d4e94735b6 | 30 | static const uint8_t GPIO1_SLAVE_ADDRESS = 0b0100000; |
| cho45 |
5:65d4e94735b6 | 31 | static const uint8_t GPIO2_SLAVE_ADDRESS = 0b0100001; |
| cho45 |
5:65d4e94735b6 | 32 | |
| cho45 |
5:65d4e94735b6 | 33 | /** |
| cho45 |
5:65d4e94735b6 | 34 | * COL=GPIOA (output normaly positive) |
| cho45 |
5:65d4e94735b6 | 35 | * ROW=GPIOB (input pulled-up) |
| cho45 |
5:65d4e94735b6 | 36 | */ |
| cho45 |
5:65d4e94735b6 | 37 | |
| cho45 |
5:65d4e94735b6 | 38 | int setupGpio(MCP23017& gpio) { |
| cho45 |
5:65d4e94735b6 | 39 | int ok; |
| cho45 |
5:65d4e94735b6 | 40 | ok = gpio.write8( |
| cho45 |
5:65d4e94735b6 | 41 | MCP23017::IOCON, |
| cho45 |
5:65d4e94735b6 | 42 | 0<<MCP23017::BANK | |
| cho45 |
5:65d4e94735b6 | 43 | 1<<MCP23017::MIRROR | |
| cho45 |
5:65d4e94735b6 | 44 | 1<<MCP23017::SEQOP | |
| cho45 |
5:65d4e94735b6 | 45 | 0<<MCP23017::DISSLW | |
| cho45 |
5:65d4e94735b6 | 46 | 1<<MCP23017::ODR // int pin is open drain |
| cho45 |
5:65d4e94735b6 | 47 | ); |
| cho45 |
5:65d4e94735b6 | 48 | |
| cho45 |
5:65d4e94735b6 | 49 | // IODIR |
| cho45 |
5:65d4e94735b6 | 50 | // 1: input |
| cho45 |
5:65d4e94735b6 | 51 | // 0: output |
| cho45 |
5:65d4e94735b6 | 52 | ok = gpio.write16( |
| cho45 |
5:65d4e94735b6 | 53 | MCP23017::IODIRA, |
| cho45 |
5:65d4e94735b6 | 54 | 0b0000000011111111 |
| cho45 |
5:65d4e94735b6 | 55 | ); |
| cho45 |
4:54cb552e50c4 | 56 | |
| cho45 |
5:65d4e94735b6 | 57 | // INPUT POLARITY |
| cho45 |
5:65d4e94735b6 | 58 | // 1: inverse polarity |
| cho45 |
5:65d4e94735b6 | 59 | // 0: raw |
| cho45 |
5:65d4e94735b6 | 60 | ok = gpio.write8( |
| cho45 |
5:65d4e94735b6 | 61 | MCP23017::IPOLB, |
| cho45 |
5:65d4e94735b6 | 62 | 0b11111111 |
| cho45 |
5:65d4e94735b6 | 63 | ); |
| cho45 |
5:65d4e94735b6 | 64 | |
| cho45 |
5:65d4e94735b6 | 65 | // INTERRUPT-ON-CHANGE Enable |
| cho45 |
5:65d4e94735b6 | 66 | ok = gpio.write8( |
| cho45 |
5:65d4e94735b6 | 67 | MCP23017::GPINTENB, |
| cho45 |
5:65d4e94735b6 | 68 | 0b11111111 |
| cho45 |
5:65d4e94735b6 | 69 | ); |
| cho45 |
5:65d4e94735b6 | 70 | // INTERRUPT-ON-CHANGE Control |
| cho45 |
5:65d4e94735b6 | 71 | // 1: compared with DEFVAL |
| cho45 |
5:65d4e94735b6 | 72 | // 0: compared to previous value |
| cho45 |
5:65d4e94735b6 | 73 | ok = gpio.write8( |
| cho45 |
5:65d4e94735b6 | 74 | MCP23017::INTCONB, |
| cho45 |
5:65d4e94735b6 | 75 | 0b00000000 |
| cho45 |
5:65d4e94735b6 | 76 | ); |
| cho45 |
5:65d4e94735b6 | 77 | // PULL-UP (for input pin) |
| cho45 |
5:65d4e94735b6 | 78 | // 1: pull-up enabled |
| cho45 |
5:65d4e94735b6 | 79 | // 0: pull-up disabled |
| cho45 |
5:65d4e94735b6 | 80 | ok = gpio.write8( |
| cho45 |
5:65d4e94735b6 | 81 | MCP23017::GPPUB, |
| cho45 |
5:65d4e94735b6 | 82 | 0b11111111 |
| cho45 |
5:65d4e94735b6 | 83 | ); |
| cho45 |
5:65d4e94735b6 | 84 | |
| cho45 |
5:65d4e94735b6 | 85 | ok = gpio1.write8( |
| cho45 |
5:65d4e94735b6 | 86 | MCP23017::GPIOA, |
| cho45 |
5:65d4e94735b6 | 87 | 0b00000000 |
| cho45 |
5:65d4e94735b6 | 88 | ); |
| cho45 |
5:65d4e94735b6 | 89 | |
| cho45 |
5:65d4e94735b6 | 90 | return ok; |
| cho45 |
5:65d4e94735b6 | 91 | } |
| cho45 |
5:65d4e94735b6 | 92 | |
| cho45 |
4:54cb552e50c4 | 93 | public: |
| cho45 |
5:65d4e94735b6 | 94 | KeyboardMatrixController(I2C& _i2c) : |
| cho45 |
5:65d4e94735b6 | 95 | i2c(_i2c), |
| cho45 |
5:65d4e94735b6 | 96 | gpio1(i2c, GPIO1_SLAVE_ADDRESS), |
| cho45 |
5:65d4e94735b6 | 97 | gpio2(i2c, GPIO2_SLAVE_ADDRESS) |
| cho45 |
4:54cb552e50c4 | 98 | { |
| cho45 |
4:54cb552e50c4 | 99 | } |
| cho45 |
4:54cb552e50c4 | 100 | |
| cho45 |
5:65d4e94735b6 | 101 | void init() { |
| cho45 |
5:65d4e94735b6 | 102 | setupGpio(gpio1); |
| cho45 |
5:65d4e94735b6 | 103 | // setupGpio(gpio2); |
| cho45 |
5:65d4e94735b6 | 104 | } |
| cho45 |
5:65d4e94735b6 | 105 | |
| cho45 |
5:65d4e94735b6 | 106 | void scanKeyboard(uint8_t* keys) { |
| cho45 |
5:65d4e94735b6 | 107 | int ok; |
| cho45 |
5:65d4e94735b6 | 108 | |
| cho45 |
5:65d4e94735b6 | 109 | for (int i = 0; i < 8; i++) { |
| cho45 |
5:65d4e94735b6 | 110 | ok = gpio1.write8( |
| cho45 |
5:65d4e94735b6 | 111 | MCP23017::GPIOA, |
| cho45 |
5:65d4e94735b6 | 112 | ~(1<<i) |
| cho45 |
5:65d4e94735b6 | 113 | ); |
| cho45 |
5:65d4e94735b6 | 114 | keys[i] = gpio1.read8(MCP23017::GPIOB, ok); |
| cho45 |
4:54cb552e50c4 | 115 | } |
| cho45 |
2:c2e3f240640c | 116 | |
| cho45 |
5:65d4e94735b6 | 117 | // set all output to negative for interrupt |
| cho45 |
5:65d4e94735b6 | 118 | ok = gpio1.write8( |
| cho45 |
5:65d4e94735b6 | 119 | MCP23017::GPIOA, |
| cho45 |
5:65d4e94735b6 | 120 | 0b00000000 |
| cho45 |
5:65d4e94735b6 | 121 | ); |
| cho45 |
5:65d4e94735b6 | 122 | |
| cho45 |
7:b9270a37345b | 123 | // TODO gpio2 |
| cho45 |
7:b9270a37345b | 124 | } |
| cho45 |
7:b9270a37345b | 125 | |
| cho45 |
7:b9270a37345b | 126 | void disableInterrupt() { |
| cho45 |
7:b9270a37345b | 127 | int ok; |
| cho45 |
7:b9270a37345b | 128 | // Disable interrupt |
| cho45 |
7:b9270a37345b | 129 | ok = gpio1.write8( |
| cho45 |
7:b9270a37345b | 130 | MCP23017::GPINTENB, |
| cho45 |
7:b9270a37345b | 131 | 0b00000000 |
| cho45 |
7:b9270a37345b | 132 | ); |
| cho45 |
7:b9270a37345b | 133 | } |
| cho45 |
7:b9270a37345b | 134 | |
| cho45 |
7:b9270a37345b | 135 | void enableInterrupt() { |
| cho45 |
7:b9270a37345b | 136 | int ok; |
| cho45 |
5:65d4e94735b6 | 137 | // Enable interrupt |
| cho45 |
5:65d4e94735b6 | 138 | ok = gpio1.write8( |
| cho45 |
5:65d4e94735b6 | 139 | MCP23017::GPINTENB, |
| cho45 |
5:65d4e94735b6 | 140 | 0b11111111 |
| cho45 |
5:65d4e94735b6 | 141 | ); |
| cho45 |
5:65d4e94735b6 | 142 | |
| cho45 |
5:65d4e94735b6 | 143 | // Clear interrupt |
| cho45 |
5:65d4e94735b6 | 144 | gpio1.read8(MCP23017::GPIOB, ok); |
| cho45 |
4:54cb552e50c4 | 145 | } |
| cho45 |
5:65d4e94735b6 | 146 | }; |
| cho45 |
4:54cb552e50c4 | 147 | |
| cho45 |
5:65d4e94735b6 | 148 | I2C i2c(I2C_SDA0, I2C_SCL0); |
| cho45 |
5:65d4e94735b6 | 149 | KeyboardMatrixController keyboardMatrixController(i2c); |
| cho45 |
6:f1c3ea8bc850 | 150 | Keymap keymap; |
| cho45 |
5:65d4e94735b6 | 151 | InterruptIn buttonInt(P0_5); |
| cho45 |
5:65d4e94735b6 | 152 | DigitalIn buttonIntIn(P0_5); |
| cho45 |
5:65d4e94735b6 | 153 | |
| cho45 |
5:65d4e94735b6 | 154 | // ROWS=8 |
| cho45 |
5:65d4e94735b6 | 155 | // COLS=16 |
| cho45 |
5:65d4e94735b6 | 156 | // 列ごとに1バイトにパックしてキーの状態を保持する |
| cho45 |
5:65d4e94735b6 | 157 | static uint8_t keysA[COLS]; |
| cho45 |
5:65d4e94735b6 | 158 | static uint8_t keysB[COLS]; |
| cho45 |
5:65d4e94735b6 | 159 | static bool state = 0; |
| cho45 |
5:65d4e94735b6 | 160 | |
| cho45 |
5:65d4e94735b6 | 161 | void buttonIntCallback() { |
| cho45 |
8:d684faf04c9a | 162 | // just for wakeup |
| cho45 |
2:c2e3f240640c | 163 | } |
| cho45 |
2:c2e3f240640c | 164 | |
| cho45 | 0:be89b5fdea09 | 165 | int main(void) { |
| cho45 |
5:65d4e94735b6 | 166 | printf("init\r\n"); |
| cho45 |
4:54cb552e50c4 | 167 | |
| cho45 |
4:54cb552e50c4 | 168 | // mbed's Serial of TARGET_RBLAB_BLENANO sucks |
| cho45 |
4:54cb552e50c4 | 169 | // DO NOT CONNECT RTS/CTS AUTOMATICALY! |
| cho45 |
4:54cb552e50c4 | 170 | NRF_UART0->PSELRTS = 0xFFFFFFFFUL; |
| cho45 |
4:54cb552e50c4 | 171 | NRF_UART0->PSELCTS = 0xFFFFFFFFUL; |
| cho45 |
4:54cb552e50c4 | 172 | |
| cho45 |
4:54cb552e50c4 | 173 | // 100kHz |
| cho45 |
4:54cb552e50c4 | 174 | i2c.frequency(100000); |
| cho45 |
4:54cb552e50c4 | 175 | |
| cho45 |
5:65d4e94735b6 | 176 | buttonInt.mode(PullUp); |
| cho45 |
5:65d4e94735b6 | 177 | buttonInt.fall(buttonIntCallback); |
| cho45 |
4:54cb552e50c4 | 178 | |
| cho45 |
5:65d4e94735b6 | 179 | keyboardMatrixController.init(); |
| cho45 |
5:65d4e94735b6 | 180 | buttonIntCallback(); |
| cho45 |
4:54cb552e50c4 | 181 | |
| cho45 |
6:f1c3ea8bc850 | 182 | HIDController::init(); |
| cho45 |
5:65d4e94735b6 | 183 | while (1) { |
| cho45 |
7:b9270a37345b | 184 | if (!buttonIntIn.read()) { |
| cho45 |
7:b9270a37345b | 185 | uint8_t (&keysCurr)[COLS] = state ? keysA : keysB; |
| cho45 |
7:b9270a37345b | 186 | uint8_t (&keysPrev)[COLS] = state ? keysB : keysA; |
| cho45 |
7:b9270a37345b | 187 | |
| cho45 |
7:b9270a37345b | 188 | keyboardMatrixController.disableInterrupt(); |
| cho45 |
7:b9270a37345b | 189 | keyboardMatrixController.scanKeyboard(keysCurr); |
| cho45 |
7:b9270a37345b | 190 | |
| cho45 |
7:b9270a37345b | 191 | for (int col = 0; col < COLS; col++) { |
| cho45 |
7:b9270a37345b | 192 | uint8_t changed = keysPrev[col] ^ keysCurr[col]; |
| cho45 |
7:b9270a37345b | 193 | for (int row = 0; row < ROWS; row++) { |
| cho45 |
7:b9270a37345b | 194 | if (changed & (1<<row)) { |
| cho45 |
7:b9270a37345b | 195 | bool pressed = keysCurr[col] & (1<<row); |
| cho45 |
8:d684faf04c9a | 196 | // printf("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed); |
| cho45 |
7:b9270a37345b | 197 | keymap.execute(col, row, pressed); |
| cho45 |
7:b9270a37345b | 198 | } |
| cho45 |
7:b9270a37345b | 199 | } |
| cho45 |
7:b9270a37345b | 200 | } |
| cho45 |
7:b9270a37345b | 201 | state = !state; |
| cho45 |
7:b9270a37345b | 202 | |
| cho45 |
7:b9270a37345b | 203 | // XXX |
| cho45 |
7:b9270a37345b | 204 | keyboardMatrixController.enableInterrupt(); |
| cho45 |
7:b9270a37345b | 205 | wait_ms(5); |
| cho45 |
8:d684faf04c9a | 206 | } else { |
| cho45 |
8:d684faf04c9a | 207 | HIDController::process(); |
| cho45 |
7:b9270a37345b | 208 | } |
| cho45 |
2:c2e3f240640c | 209 | } |
| cho45 |
2:c2e3f240640c | 210 | } |