Initial release

Dependencies:   microbit

Committer:
masakjm
Date:
Sat Sep 22 00:31:39 2018 +0000
Revision:
3:b6e9850d3e76
Parent:
1:3b9ae1dbcdcf
Child:
4:4fe5674bf409
Minor fixes and annotations added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masakjm 0:28fb3e9ef81a 1 //=================================
masakjm 0:28fb3e9ef81a 2 // microbit_switch_if_3sw
masakjm 0:28fb3e9ef81a 3 //=================================
masakjm 0:28fb3e9ef81a 4 // BLE switch interface with 3 tact switches for micro:bit
masakjm 0:28fb3e9ef81a 5 // It is intended for use with ios devices.
masakjm 0:28fb3e9ef81a 6 //
masakjm 0:28fb3e9ef81a 7 // The MIT License (MIT) Copyright (c) 2018 Masatomo Kojima
masakjm 0:28fb3e9ef81a 8 //
masakjm 0:28fb3e9ef81a 9 // LED message
masakjm 0:28fb3e9ef81a 10 // S key code setting sw1,2
masakjm 0:28fb3e9ef81a 11 // T key code setting sw3
masakjm 0:28fb3e9ef81a 12 // C Conected
masakjm 0:28fb3e9ef81a 13 // P success Pearing
masakjm 0:28fb3e9ef81a 14 // F Fail pearing
masakjm 0:28fb3e9ef81a 15 // E Error at sending data by ble
masakjm 0:28fb3e9ef81a 16 // e Error at writting data to flash memory
masakjm 0:28fb3e9ef81a 17 // I Error by Incorrect cording
masakjm 0:28fb3e9ef81a 18 //
masakjm 0:28fb3e9ef81a 19 // Please refer to the following site. (Japanese only)
masakjm 0:28fb3e9ef81a 20 // http://mahoro-ba.net/e2036.html
masakjm 0:28fb3e9ef81a 21 // http://mahoro-ba.net/e2038.html
masakjm 0:28fb3e9ef81a 22 //
masakjm 0:28fb3e9ef81a 23 // I refer to information written on the following sites.
masakjm 0:28fb3e9ef81a 24 // (1)https://os.mbed.com/teams/microbit/code/microbit_presenter/
masakjm 0:28fb3e9ef81a 25 // Licensed under the Apache License, Version 2.0
masakjm 0:28fb3e9ef81a 26 //
masakjm 0:28fb3e9ef81a 27 // (2)https://lancaster-university.github.io/microbit-docs/
masakjm 0:28fb3e9ef81a 28 // The MIT License (MIT)
masakjm 0:28fb3e9ef81a 29 // Copyright (c) 2016 British Broadcasting Corporation.
masakjm 0:28fb3e9ef81a 30 //
masakjm 0:28fb3e9ef81a 31 //---------------------------------
masakjm 0:28fb3e9ef81a 32
masakjm 3:b6e9850d3e76 33 #define VERSION "3SW-180922"
masakjm 0:28fb3e9ef81a 34 //#define NO_DEBUG
masakjm 0:28fb3e9ef81a 35
masakjm 0:28fb3e9ef81a 36 #include "microbit_switch_if.h"
masakjm 0:28fb3e9ef81a 37 #include "KeyValueInt.h"
masakjm 0:28fb3e9ef81a 38
masakjm 0:28fb3e9ef81a 39 //---------------------------------
masakjm 0:28fb3e9ef81a 40 // Display
masakjm 0:28fb3e9ef81a 41 //---------------------------------
masakjm 0:28fb3e9ef81a 42 MicroBitDisplay display;
masakjm 3:b6e9850d3e76 43 int state; // 状態遷移
masakjm 0:28fb3e9ef81a 44 char dispChar = 0; // LEDに表示する文字コード
masakjm 0:28fb3e9ef81a 45 char dispCharLast = 0; // 最後に表示した文字コード
masakjm 0:28fb3e9ef81a 46 bool turnOffMode = false; // LED非表示モードのフラグ
masakjm 0:28fb3e9ef81a 47
masakjm 3:b6e9850d3e76 48 /** ----------
masakjm 3:b6e9850d3e76 49 * @brief 整数値をLEDに表示する
masakjm 3:b6e9850d3e76 50 * @param data 整数値
masakjm 3:b6e9850d3e76 51 */
masakjm 0:28fb3e9ef81a 52 static void displayNumber(int data)
masakjm 0:28fb3e9ef81a 53 {
masakjm 0:28fb3e9ef81a 54 if (0<=data && data<=9) { // 1桁はスクロールしない
masakjm 0:28fb3e9ef81a 55 display.print(data);
masakjm 0:28fb3e9ef81a 56 } else {
masakjm 0:28fb3e9ef81a 57 display.scroll(data);
masakjm 0:28fb3e9ef81a 58 }
masakjm 0:28fb3e9ef81a 59 }
masakjm 0:28fb3e9ef81a 60
masakjm 0:28fb3e9ef81a 61 //---------------------------------
masakjm 0:28fb3e9ef81a 62 // Flash Memory
masakjm 0:28fb3e9ef81a 63 //---------------------------------
masakjm 3:b6e9850d3e76 64 /** ----------
masakjm 3:b6e9850d3e76 65 * @brief キー名を指定して不揮発メモリから値を読み出す
masakjm 3:b6e9850d3e76 66 * @param key キー名
masakjm 3:b6e9850d3e76 67 * @param init データが保存されていなかった時の初期値
masakjm 3:b6e9850d3e76 68 * @return int 取得したデータ
masakjm 3:b6e9850d3e76 69 */
masakjm 0:28fb3e9ef81a 70 static int FlashGet(MicroBitStorage storage, const char* key, int init=0)
masakjm 0:28fb3e9ef81a 71 {
masakjm 0:28fb3e9ef81a 72 KeyValuePair* kvp = storage.get(key);
masakjm 0:28fb3e9ef81a 73 if(kvp != NULL) {
masakjm 0:28fb3e9ef81a 74 int data;
masakjm 0:28fb3e9ef81a 75 memcpy(&data, kvp->value, sizeof(int));
masakjm 0:28fb3e9ef81a 76 DEBUG("== FlashGet exist %s = %d\r\n", key, data);
masakjm 0:28fb3e9ef81a 77 return data;
masakjm 0:28fb3e9ef81a 78 }
masakjm 0:28fb3e9ef81a 79 return init;
masakjm 0:28fb3e9ef81a 80 }
masakjm 0:28fb3e9ef81a 81
masakjm 3:b6e9850d3e76 82 /** ----------
masakjm 3:b6e9850d3e76 83 * @brief 不揮発メモリに値を書き込む
masakjm 3:b6e9850d3e76 84 * @param storage 不揮発メモリインスタンス
masakjm 3:b6e9850d3e76 85 * @param key キー名
masakjm 3:b6e9850d3e76 86 * @param data 値
masakjm 3:b6e9850d3e76 87 * @return int MICROBIT_OK = 0
masakjm 3:b6e9850d3e76 88 * MICROBIT_INVALID_PARAMETER = -1001,
masakjm 3:b6e9850d3e76 89 * MICROBIT_NO_RESOURCES = -1005,
masakjm 3:b6e9850d3e76 90 */
masakjm 0:28fb3e9ef81a 91 static int FlashPut(MicroBitStorage storage, const char* key, int data)
masakjm 0:28fb3e9ef81a 92 {
masakjm 0:28fb3e9ef81a 93 int ret = storage.put(key, (uint8_t *)&data, sizeof(int));
masakjm 0:28fb3e9ef81a 94 DEBUG("== FlashPut %s %d %d\r\n", key, data, ret);
masakjm 0:28fb3e9ef81a 95 return ret;
masakjm 0:28fb3e9ef81a 96 }
masakjm 0:28fb3e9ef81a 97
masakjm 0:28fb3e9ef81a 98 //---------------------------------
masakjm 0:28fb3e9ef81a 99 // Setting
masakjm 0:28fb3e9ef81a 100 //---------------------------------
masakjm 0:28fb3e9ef81a 101 bool setting_inc; // 設定値を増加
masakjm 0:28fb3e9ef81a 102 bool setting_enter; // 設定値を入力
masakjm 0:28fb3e9ef81a 103 bool setting_next; // 次の設定値に移動
masakjm 0:28fb3e9ef81a 104
masakjm 0:28fb3e9ef81a 105 KeyValueInt kviKeyCode1("keycode1",'S', 1, 1, NUM_GROUP1, true);
masakjm 0:28fb3e9ef81a 106 KeyValueInt kviKeyCode2("keycode2",'T', 1, 1, NUM_GROUP2, true);
masakjm 0:28fb3e9ef81a 107
masakjm 3:b6e9850d3e76 108 /** ----------
masakjm 3:b6e9850d3e76 109 * @brief 1つのパラメータの表示と変更
masakjm 3:b6e9850d3e76 110 * @param storage 不揮発メモリインスタンス
masakjm 3:b6e9850d3e76 111 * @param para パラメータのキーと値の組
masakjm 3:b6e9850d3e76 112 * @param change 変更する時 true
masakjm 3:b6e9850d3e76 113 * @return bool true : Success false : Failed
masakjm 3:b6e9850d3e76 114 */
masakjm 3:b6e9850d3e76 115 static bool paraSettingOne(MicroBitStorage storage, KeyValueInt* para, bool change)
masakjm 0:28fb3e9ef81a 116 {
masakjm 0:28fb3e9ef81a 117 display.print(para->disp); // 識別文字をLED表示
masakjm 0:28fb3e9ef81a 118 wait(SETTING_DISPLAY_WAIT);
masakjm 0:28fb3e9ef81a 119 displayNumber(para->value); // 値をLED表示
masakjm 0:28fb3e9ef81a 120 if (!change) {
masakjm 0:28fb3e9ef81a 121 wait(SETTING_DISPLAY_WAIT);
masakjm 3:b6e9850d3e76 122 return true;
masakjm 0:28fb3e9ef81a 123 }
masakjm 0:28fb3e9ef81a 124
masakjm 0:28fb3e9ef81a 125 DEBUG("== paraSetting\r\n");
masakjm 0:28fb3e9ef81a 126 setting_inc = false;
masakjm 0:28fb3e9ef81a 127 setting_next = setting_enter = false;
masakjm 0:28fb3e9ef81a 128 while( ! setting_next) { // Bボタンが離されるまで
masakjm 0:28fb3e9ef81a 129 if (setting_inc) { // Aボタンが押されたら
masakjm 0:28fb3e9ef81a 130 setting_inc = false;
masakjm 0:28fb3e9ef81a 131 para->inc();
masakjm 0:28fb3e9ef81a 132 displayNumber(para->value);
masakjm 0:28fb3e9ef81a 133 }
masakjm 0:28fb3e9ef81a 134 wait(0.2);
masakjm 0:28fb3e9ef81a 135 }
masakjm 3:b6e9850d3e76 136 int ret = FlashPut(storage, para->key, para->value);
masakjm 3:b6e9850d3e76 137 if (ret) DEBUG("(strage)Error %d\r\n",ret);
masakjm 3:b6e9850d3e76 138 return (ret == MICROBIT_OK);
masakjm 0:28fb3e9ef81a 139 }
masakjm 0:28fb3e9ef81a 140
masakjm 3:b6e9850d3e76 141 /** ----------
masakjm 3:b6e9850d3e76 142 * @brief パラメータの表示と変更
masakjm 3:b6e9850d3e76 143 * @param change true:変更する時
masakjm 3:b6e9850d3e76 144 */
masakjm 3:b6e9850d3e76 145 static bool paraSetting(bool change=false)
masakjm 0:28fb3e9ef81a 146 {
masakjm 0:28fb3e9ef81a 147 MicroBitStorage storage;
masakjm 0:28fb3e9ef81a 148
masakjm 0:28fb3e9ef81a 149 kviKeyCode1.set(FlashGet(storage, kviKeyCode1.key, 1));
masakjm 0:28fb3e9ef81a 150 kviKeyCode2.set(FlashGet(storage, kviKeyCode2.key, 1));
masakjm 3:b6e9850d3e76 151 if(!paraSettingOne(storage, &kviKeyCode1, change)) return false;
masakjm 3:b6e9850d3e76 152 return paraSettingOne(storage, &kviKeyCode2, change);
masakjm 0:28fb3e9ef81a 153 }
masakjm 0:28fb3e9ef81a 154 //---------------------------------
masakjm 0:28fb3e9ef81a 155 // BLE & HID
masakjm 0:28fb3e9ef81a 156 //---------------------------------
masakjm 0:28fb3e9ef81a 157 BLE ble;
masakjm 0:28fb3e9ef81a 158 KeyboardService *kbdServicePtr;
masakjm 3:b6e9850d3e76 159 BLE_MESSAGE bleMessage;
masakjm 0:28fb3e9ef81a 160
masakjm 3:b6e9850d3e76 161 /** ----------
masakjm 3:b6e9850d3e76 162 * @brief BLE接続が切断された時のコールバック関数
masakjm 3:b6e9850d3e76 163 */
masakjm 0:28fb3e9ef81a 164 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
masakjm 0:28fb3e9ef81a 165 {
masakjm 0:28fb3e9ef81a 166 DEBUG("(BLE)disconnected\r\n");
masakjm 0:28fb3e9ef81a 167 ble.gap().startAdvertising(); // restart advertising
masakjm 3:b6e9850d3e76 168 bleMessage = BLE_NO_MESSAGE;
masakjm 0:28fb3e9ef81a 169 }
masakjm 0:28fb3e9ef81a 170
masakjm 3:b6e9850d3e76 171 /** ----------
masakjm 3:b6e9850d3e76 172 * @brief BLE接続が接続された時のコールバック関数
masakjm 3:b6e9850d3e76 173 */
masakjm 0:28fb3e9ef81a 174 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
masakjm 0:28fb3e9ef81a 175 {
masakjm 0:28fb3e9ef81a 176 if(kbdServicePtr->isConnected()) {
masakjm 0:28fb3e9ef81a 177 DEBUG("(BLE)connected\r\n");
masakjm 3:b6e9850d3e76 178 ble.gap().stopAdvertising();
masakjm 3:b6e9850d3e76 179 bleMessage = BLE_CONNECTED;
masakjm 0:28fb3e9ef81a 180 }
masakjm 0:28fb3e9ef81a 181 }
masakjm 0:28fb3e9ef81a 182
masakjm 3:b6e9850d3e76 183 /** ----------
masakjm 3:b6e9850d3e76 184 * @brief パスキー入力を求められた時のコールバック関数
masakjm 3:b6e9850d3e76 185 */
masakjm 0:28fb3e9ef81a 186 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
masakjm 0:28fb3e9ef81a 187 {
masakjm 0:28fb3e9ef81a 188 printf("(BLE)Input passKey: ");
masakjm 0:28fb3e9ef81a 189 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
masakjm 0:28fb3e9ef81a 190 printf("%c", passkey[i]);
masakjm 0:28fb3e9ef81a 191 }
masakjm 0:28fb3e9ef81a 192 printf("\r\n");
masakjm 0:28fb3e9ef81a 193 }
masakjm 0:28fb3e9ef81a 194
masakjm 3:b6e9850d3e76 195 /** ----------
masakjm 3:b6e9850d3e76 196 * @brief セキュリティ設定完了時のコールバック関数
masakjm 3:b6e9850d3e76 197 */
masakjm 0:28fb3e9ef81a 198 static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
masakjm 0:28fb3e9ef81a 199 {
masakjm 0:28fb3e9ef81a 200 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
masakjm 0:28fb3e9ef81a 201 // DEBUG("(BLE)Security success %d\r\n", status);
masakjm 3:b6e9850d3e76 202 bleMessage = BLE_PAIRING_SUCCESS;
masakjm 0:28fb3e9ef81a 203
masakjm 0:28fb3e9ef81a 204 } else {
masakjm 0:28fb3e9ef81a 205 // DEBUG("(BLE)Security failed %d\r\n", status);
masakjm 3:b6e9850d3e76 206 bleMessage = BLE_PAIRING_FAILED;
masakjm 0:28fb3e9ef81a 207 }
masakjm 0:28fb3e9ef81a 208 }
masakjm 0:28fb3e9ef81a 209
masakjm 3:b6e9850d3e76 210 /** ----------
masakjm 3:b6e9850d3e76 211 * @brief セキュリティ初期化
masakjm 3:b6e9850d3e76 212 * @para _ble BLEインスタンス
masakjm 3:b6e9850d3e76 213 */
masakjm 3:b6e9850d3e76 214 static void initializeSecurity(BLE &_ble)
masakjm 0:28fb3e9ef81a 215 {
masakjm 0:28fb3e9ef81a 216 bool enableBonding = true;
masakjm 0:28fb3e9ef81a 217 bool requireMITM = HID_SECURITY_REQUIRE_MITM;
masakjm 0:28fb3e9ef81a 218
masakjm 3:b6e9850d3e76 219 _ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
masakjm 3:b6e9850d3e76 220 _ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
masakjm 0:28fb3e9ef81a 221
masakjm 3:b6e9850d3e76 222 _ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS);
masakjm 0:28fb3e9ef81a 223 }
masakjm 0:28fb3e9ef81a 224
masakjm 3:b6e9850d3e76 225 /** ----------
masakjm 3:b6e9850d3e76 226 * @brief HOGP (HID Over GATT Profile) 初期化
masakjm 3:b6e9850d3e76 227 * @para _ble BLEインスタンス
masakjm 3:b6e9850d3e76 228 */
masakjm 3:b6e9850d3e76 229 static void initializeHOGP(BLE &_ble)
masakjm 0:28fb3e9ef81a 230 {
masakjm 0:28fb3e9ef81a 231 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
masakjm 0:28fb3e9ef81a 232 GattService::UUID_DEVICE_INFORMATION_SERVICE,
masakjm 0:28fb3e9ef81a 233 GattService::UUID_BATTERY_SERVICE
masakjm 0:28fb3e9ef81a 234 };
masakjm 0:28fb3e9ef81a 235
masakjm 0:28fb3e9ef81a 236 PnPID_t pnpID;
masakjm 0:28fb3e9ef81a 237 pnpID.vendorID_source = 0x2; // from the USB Implementer's Forum
masakjm 0:28fb3e9ef81a 238 pnpID.vendorID = 0x0D28; // NXP
masakjm 0:28fb3e9ef81a 239 pnpID.productID = 0x0204; // CMSIS-DAP (well, it's a keyboard but oh well)
masakjm 0:28fb3e9ef81a 240 pnpID.productVersion = 0x0100; // v1.0
masakjm 3:b6e9850d3e76 241 HIDDeviceInformationService deviceInfo(_ble, "ARM", "m1", "abc", "def", "ghi", "jkl", &pnpID);
masakjm 0:28fb3e9ef81a 242
masakjm 3:b6e9850d3e76 243 BatteryService batteryInfo(_ble, 80);
masakjm 0:28fb3e9ef81a 244
masakjm 3:b6e9850d3e76 245 _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |
masakjm 0:28fb3e9ef81a 246 GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
masakjm 3:b6e9850d3e76 247 _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
masakjm 0:28fb3e9ef81a 248 (uint8_t *)uuid16_list, sizeof(uuid16_list));
masakjm 0:28fb3e9ef81a 249
masakjm 0:28fb3e9ef81a 250 // see 5.1.2: HID over GATT Specification (pg. 25)
masakjm 3:b6e9850d3e76 251 _ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
masakjm 0:28fb3e9ef81a 252 // 30ms to 50ms is recommended (5.1.2)
masakjm 3:b6e9850d3e76 253 _ble.gap().setAdvertisingInterval(50);
masakjm 0:28fb3e9ef81a 254 }
masakjm 0:28fb3e9ef81a 255
masakjm 3:b6e9850d3e76 256 /** ----------
masakjm 3:b6e9850d3e76 257 * @brief BLE接続のHID(human interface device) の初期化
masakjm 3:b6e9850d3e76 258 */
masakjm 0:28fb3e9ef81a 259 static void initialize_BLE_HID()
masakjm 0:28fb3e9ef81a 260 {
masakjm 0:28fb3e9ef81a 261 static const char SHORT_DEVICE_NAME[] = "micro:bit";
masakjm 0:28fb3e9ef81a 262 static char DeviceName[20];
masakjm 0:28fb3e9ef81a 263
masakjm 0:28fb3e9ef81a 264 int id = microbit_serial_number()& 0xfff; // シリアル番号の下12桁を4桁の10進で表示
masakjm 0:28fb3e9ef81a 265 sprintf(DeviceName, "micro:bit %04d", id);
masakjm 0:28fb3e9ef81a 266 DEBUG("(BLE)id %s\r\n", DeviceName);
masakjm 0:28fb3e9ef81a 267
masakjm 0:28fb3e9ef81a 268 // DEBUG("(BLE)initialising ble\r\n");
masakjm 0:28fb3e9ef81a 269 ble.init();
masakjm 0:28fb3e9ef81a 270
masakjm 0:28fb3e9ef81a 271 ble.gap().onDisconnection(onDisconnect);
masakjm 0:28fb3e9ef81a 272 ble.gap().onConnection(onConnect);
masakjm 0:28fb3e9ef81a 273
masakjm 0:28fb3e9ef81a 274 initializeSecurity(ble);
masakjm 0:28fb3e9ef81a 275
masakjm 0:28fb3e9ef81a 276 DEBUG("(BLE)adding hid service\r\n");
masakjm 0:28fb3e9ef81a 277 KeyboardService kbdService(ble);
masakjm 0:28fb3e9ef81a 278 kbdServicePtr = &kbdService;
masakjm 0:28fb3e9ef81a 279
masakjm 0:28fb3e9ef81a 280 DEBUG("(BLE)adding device info and battery service\r\n");
masakjm 0:28fb3e9ef81a 281 initializeHOGP(ble);
masakjm 0:28fb3e9ef81a 282
masakjm 0:28fb3e9ef81a 283 DEBUG("(BLE)setting up gap\r\n");
masakjm 0:28fb3e9ef81a 284 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
masakjm 0:28fb3e9ef81a 285 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
masakjm 0:28fb3e9ef81a 286 (uint8_t *)DeviceName, sizeof(DeviceName));
masakjm 0:28fb3e9ef81a 287 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
masakjm 0:28fb3e9ef81a 288 (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
masakjm 0:28fb3e9ef81a 289 ble.gap().setDeviceName((const uint8_t *)DeviceName);
masakjm 0:28fb3e9ef81a 290
masakjm 0:28fb3e9ef81a 291 DEBUG("(BLE)advertising\r\n");
masakjm 0:28fb3e9ef81a 292 ble.gap().startAdvertising();
masakjm 0:28fb3e9ef81a 293 }
masakjm 0:28fb3e9ef81a 294
masakjm 0:28fb3e9ef81a 295 //---------------------------------
masakjm 0:28fb3e9ef81a 296 // Button
masakjm 0:28fb3e9ef81a 297 //---------------------------------
masakjm 0:28fb3e9ef81a 298 MicroBitMessageBus bus;
masakjm 0:28fb3e9ef81a 299 MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
masakjm 0:28fb3e9ef81a 300 MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
masakjm 0:28fb3e9ef81a 301 MicroBitButton button0(MICROBIT_PIN_P0, MICROBIT_ID_IO_P0, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
masakjm 0:28fb3e9ef81a 302 MicroBitButton button1(MICROBIT_PIN_P1, MICROBIT_ID_IO_P1, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
masakjm 0:28fb3e9ef81a 303 MicroBitButton button2(MICROBIT_PIN_P2, MICROBIT_ID_IO_P2, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
masakjm 0:28fb3e9ef81a 304
masakjm 3:b6e9850d3e76 305 /** ----------
masakjm 3:b6e9850d3e76 306 * @brief キーコードを送信する
masakjm 3:b6e9850d3e76 307 * @para code 上位8bit:修飾キー 下位8bit:キーコード    
masakjm 3:b6e9850d3e76 308 */
masakjm 1:3b9ae1dbcdcf 309 static void sendKeyCode(int code) {
masakjm 1:3b9ae1dbcdcf 310 if (code) {
masakjm 1:3b9ae1dbcdcf 311 uint8_t key = code & 0xff;
masakjm 1:3b9ae1dbcdcf 312 uint8_t modif = code >> 8;
masakjm 1:3b9ae1dbcdcf 313
masakjm 1:3b9ae1dbcdcf 314 if (key > KEYMAP_SIZE ) {
masakjm 1:3b9ae1dbcdcf 315 dispChar ='I'; // キーコード設定間違い
masakjm 1:3b9ae1dbcdcf 316 } else {
masakjm 1:3b9ae1dbcdcf 317 ble_error_t ret = kbdServicePtr->keyDownCode(key, modif);
masakjm 1:3b9ae1dbcdcf 318 // DEBUG(" code=%d modif=%d\r\n",key , modif);
masakjm 1:3b9ae1dbcdcf 319 if (ret) {
masakjm 1:3b9ae1dbcdcf 320 dispChar ='E';
masakjm 1:3b9ae1dbcdcf 321 dispCharLast =0; // E が続く時に表示
masakjm 1:3b9ae1dbcdcf 322 DEBUG("(BLE)Error %d\r\n",ret);
masakjm 1:3b9ae1dbcdcf 323 }
masakjm 1:3b9ae1dbcdcf 324 }
masakjm 1:3b9ae1dbcdcf 325 }
masakjm 1:3b9ae1dbcdcf 326 }
masakjm 3:b6e9850d3e76 327 /** ----------
masakjm 3:b6e9850d3e76 328 * @brief ボタン保持時イベント
masakjm 3:b6e9850d3e76 329 * @para e イベント
masakjm 3:b6e9850d3e76 330 */
masakjm 1:3b9ae1dbcdcf 331 static void onButtonHold(MicroBitEvent e)
masakjm 1:3b9ae1dbcdcf 332 {
masakjm 1:3b9ae1dbcdcf 333 if ((e.source == MICROBIT_ID_BUTTON_A && buttonB.isPressed()) ||
masakjm 1:3b9ae1dbcdcf 334 (e.source == MICROBIT_ID_BUTTON_B && buttonA.isPressed()) ) {
masakjm 1:3b9ae1dbcdcf 335 dispChar = 'H';
masakjm 1:3b9ae1dbcdcf 336 sendKeyCode(keyCodeGroup0[2]);
masakjm 1:3b9ae1dbcdcf 337 }
masakjm 1:3b9ae1dbcdcf 338 }
masakjm 1:3b9ae1dbcdcf 339
masakjm 3:b6e9850d3e76 340 /** ----------
masakjm 3:b6e9850d3e76 341 * @brief ボタン押下げ時イベント
masakjm 3:b6e9850d3e76 342 * @para e イベント
masakjm 3:b6e9850d3e76 343 */
masakjm 0:28fb3e9ef81a 344 static void onButtonDown(MicroBitEvent e)
masakjm 0:28fb3e9ef81a 345 {
masakjm 0:28fb3e9ef81a 346 // DEBUG(" Button Down %d state=%d\r\n", e.source, state);
masakjm 0:28fb3e9ef81a 347
masakjm 0:28fb3e9ef81a 348 switch (state) {
masakjm 0:28fb3e9ef81a 349 case STATE_SETTING :
masakjm 0:28fb3e9ef81a 350 switch(e.source) {
masakjm 0:28fb3e9ef81a 351 case MICROBIT_ID_BUTTON_A :
masakjm 0:28fb3e9ef81a 352 setting_inc = true;
masakjm 0:28fb3e9ef81a 353 break;
masakjm 0:28fb3e9ef81a 354 case MICROBIT_ID_BUTTON_B :
masakjm 0:28fb3e9ef81a 355 setting_enter = true;
masakjm 0:28fb3e9ef81a 356 break;
masakjm 0:28fb3e9ef81a 357 }
masakjm 0:28fb3e9ef81a 358 break;
masakjm 0:28fb3e9ef81a 359 case STATE_OPERATING :
masakjm 1:3b9ae1dbcdcf 360 int code = 0;
masakjm 0:28fb3e9ef81a 361 switch(e.source) {
masakjm 0:28fb3e9ef81a 362 case MICROBIT_ID_BUTTON_A :
masakjm 0:28fb3e9ef81a 363 dispChar = 'A';
masakjm 0:28fb3e9ef81a 364 code = keyCodeGroup0[0];
masakjm 0:28fb3e9ef81a 365 break;
masakjm 0:28fb3e9ef81a 366 case MICROBIT_ID_BUTTON_B :
masakjm 0:28fb3e9ef81a 367 dispChar = 'B';
masakjm 0:28fb3e9ef81a 368 code = keyCodeGroup0[1];
masakjm 0:28fb3e9ef81a 369 break;
masakjm 0:28fb3e9ef81a 370 case MICROBIT_ID_IO_P0 :
masakjm 0:28fb3e9ef81a 371 dispChar = '1';
masakjm 0:28fb3e9ef81a 372 code = keyCodeGroup1[kviKeyCode1.value-1][0];
masakjm 0:28fb3e9ef81a 373 break;
masakjm 0:28fb3e9ef81a 374 case MICROBIT_ID_IO_P1 :
masakjm 0:28fb3e9ef81a 375 dispChar = '2';
masakjm 0:28fb3e9ef81a 376 code = keyCodeGroup1[kviKeyCode1.value-1][1];
masakjm 0:28fb3e9ef81a 377 break;
masakjm 0:28fb3e9ef81a 378 case MICROBIT_ID_IO_P2 :
masakjm 0:28fb3e9ef81a 379 dispChar = '3';
masakjm 0:28fb3e9ef81a 380 code = keyCodeGroup2[kviKeyCode2.value-1];
masakjm 0:28fb3e9ef81a 381 break;
masakjm 0:28fb3e9ef81a 382 }
masakjm 1:3b9ae1dbcdcf 383 sendKeyCode(code);
masakjm 0:28fb3e9ef81a 384 }
masakjm 0:28fb3e9ef81a 385 }
masakjm 0:28fb3e9ef81a 386
masakjm 3:b6e9850d3e76 387 /** ----------
masakjm 3:b6e9850d3e76 388 * @brief ボタン離し時イベント
masakjm 3:b6e9850d3e76 389 * @para e イベント
masakjm 3:b6e9850d3e76 390 */
masakjm 0:28fb3e9ef81a 391 static void onButtonUp(MicroBitEvent e)
masakjm 0:28fb3e9ef81a 392 {
masakjm 0:28fb3e9ef81a 393 // DEBUG(" Button up %d\r\n", e.source);
masakjm 0:28fb3e9ef81a 394 switch (state) {
masakjm 0:28fb3e9ef81a 395 case STATE_SETTING :
masakjm 0:28fb3e9ef81a 396 if(setting_enter) setting_next = true; // 決定ボタンを離したら次へ
masakjm 0:28fb3e9ef81a 397 break;
masakjm 0:28fb3e9ef81a 398 case STATE_OPERATING :
masakjm 0:28fb3e9ef81a 399 dispChar = 0;
masakjm 0:28fb3e9ef81a 400 ble_error_t ret = kbdServicePtr->keyUpCode();
masakjm 0:28fb3e9ef81a 401 if (ret) {
masakjm 0:28fb3e9ef81a 402 dispChar ='E';
masakjm 0:28fb3e9ef81a 403 dispCharLast =0; // E が続く時に表示
masakjm 0:28fb3e9ef81a 404 }
masakjm 0:28fb3e9ef81a 405 }
masakjm 0:28fb3e9ef81a 406 }
masakjm 0:28fb3e9ef81a 407
masakjm 0:28fb3e9ef81a 408 //---------------------------------
masakjm 0:28fb3e9ef81a 409 // Main
masakjm 0:28fb3e9ef81a 410 //---------------------------------
masakjm 0:28fb3e9ef81a 411 int main()
masakjm 0:28fb3e9ef81a 412 {
masakjm 3:b6e9850d3e76 413 state = STATE_DESABLE_INPUT;
masakjm 0:28fb3e9ef81a 414
masakjm 0:28fb3e9ef81a 415 wait(0.1); // ボタン状態の更新
masakjm 0:28fb3e9ef81a 416 bool bA = buttonA.isPressed();
masakjm 0:28fb3e9ef81a 417 bool bB = buttonB.isPressed();
masakjm 0:28fb3e9ef81a 418
masakjm 0:28fb3e9ef81a 419 if (bA && bB) { // ボタンABを押しながら起動
masakjm 3:b6e9850d3e76 420 for(int i=0;i<3;i++) {
masakjm 0:28fb3e9ef81a 421 display.scroll(VERSION);
masakjm 0:28fb3e9ef81a 422 wait(0.5);
masakjm 0:28fb3e9ef81a 423 }
masakjm 0:28fb3e9ef81a 424 } else if(bA) // ボタンAを押しながら起動
masakjm 0:28fb3e9ef81a 425 state = STATE_SETTING;
masakjm 0:28fb3e9ef81a 426
masakjm 0:28fb3e9ef81a 427 //----- Display
masakjm 0:28fb3e9ef81a 428 display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);
masakjm 0:28fb3e9ef81a 429 display.clear();
masakjm 0:28fb3e9ef81a 430 Timer dispTime; // 連続表示タイマー
masakjm 0:28fb3e9ef81a 431 dispTime.start();
masakjm 0:28fb3e9ef81a 432
masakjm 0:28fb3e9ef81a 433 //----- Button
masakjm 0:28fb3e9ef81a 434 bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_DOWN, onButtonDown);
masakjm 0:28fb3e9ef81a 435 bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_UP, onButtonUp);
masakjm 1:3b9ae1dbcdcf 436 bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_HOLD, onButtonHold);
masakjm 0:28fb3e9ef81a 437
masakjm 0:28fb3e9ef81a 438 //----- Setting
masakjm 3:b6e9850d3e76 439 if (paraSetting(state == STATE_SETTING)) dispChar =0;
masakjm 3:b6e9850d3e76 440 else dispChar ='e';
masakjm 0:28fb3e9ef81a 441
masakjm 0:28fb3e9ef81a 442 //----- BLE & HID
masakjm 3:b6e9850d3e76 443 state = STATE_DESABLE_INPUT;
masakjm 0:28fb3e9ef81a 444 initialize_BLE_HID();
masakjm 0:28fb3e9ef81a 445 display.clear();
masakjm 0:28fb3e9ef81a 446
masakjm 0:28fb3e9ef81a 447 //----- Loop
masakjm 3:b6e9850d3e76 448 state = STATE_OPERATING;
masakjm 0:28fb3e9ef81a 449 while (true) {
masakjm 0:28fb3e9ef81a 450 ble.waitForEvent(); // BLEイベントを待つ
masakjm 0:28fb3e9ef81a 451
masakjm 3:b6e9850d3e76 452 if(bleMessage != BLE_NO_MESSAGE) { // BLEの状態を表示する
masakjm 3:b6e9850d3e76 453 dispChar = bleDispChar[bleMessage];
masakjm 3:b6e9850d3e76 454 bleMessage = BLE_NO_MESSAGE;
masakjm 3:b6e9850d3e76 455 }
masakjm 3:b6e9850d3e76 456
masakjm 0:28fb3e9ef81a 457 if (dispChar != dispCharLast) { // 表示文字が変更
masakjm 0:28fb3e9ef81a 458 turnOffMode = false;
masakjm 0:28fb3e9ef81a 459 dispTime.reset();
masakjm 0:28fb3e9ef81a 460 display.enable();
masakjm 0:28fb3e9ef81a 461 if (dispChar) display.printChar(dispChar);
masakjm 0:28fb3e9ef81a 462 else display.clear();
masakjm 0:28fb3e9ef81a 463 }
masakjm 0:28fb3e9ef81a 464 if (!turnOffMode) {
masakjm 0:28fb3e9ef81a 465 if (dispTime.read() > TIME_TURN_OFF) { // 長時間表示
masakjm 0:28fb3e9ef81a 466 turnOffMode = true;
masakjm 0:28fb3e9ef81a 467 display.disable();
masakjm 0:28fb3e9ef81a 468 }
masakjm 0:28fb3e9ef81a 469 }
masakjm 0:28fb3e9ef81a 470 dispCharLast = dispChar;
masakjm 0:28fb3e9ef81a 471 }
masakjm 0:28fb3e9ef81a 472 }