BLE switch interface using micro:bit with 3 tact switches or 3 Makey Makey sensors

Dependencies:   microbit

Revision:
2:8e2e6c6658be
Parent:
1:9d0e2e5b5d25
Child:
3:d8fd4efb63cc
--- a/main.cpp	Thu Jun 06 19:06:06 2019 +0000
+++ b/main.cpp	Sat Jun 08 04:40:57 2019 +0000
@@ -2,14 +2,13 @@
 // microbit_switch_if_3sw
 //=================================
 //  BLE switch interface using micro:bit with 3 tact switches
-//                                       or   3 Makey Makey sensors
 //    It is intended for use with ios devices.
 //
 //    The MIT License (MIT)   Copyright (c) 2019 Masatomo Kojima
 //---------------------------------
 
-#define VERSION     "3SW-190607"
-//#define NO_DEBUG
+#define VERSION     "3SW-190608"
+#define NO_DEBUG
 
 #include "microbit_switch_if_3sw.h"
 #include "KeyValueInt.h"
@@ -28,6 +27,7 @@
 char DispChar = DISP_NO_MESSAGE;  // LEDに表示する文字コード
 char DispCharLast = 0;            // 最後に表示した文字コード
 bool TurnOffMode = false;         // LED非表示モードのフラグ
+int  Cnt = 0;                     // カウンター
 
 /** ----------
  * @brief 整数値をLEDに表示する
@@ -82,9 +82,9 @@
 //---------------------------------
 //  Setting
 //---------------------------------
-bool setting_inc;           // 設定値を増加
-bool setting_enter;         // 設定値を入力
-bool setting_next;          // 次の設定値に移動
+bool Setting_inc;           // 設定値を増加
+bool Setting_enter;         // 設定値を入力
+bool Setting_next;          // 次の設定値に移動
 
 KeyValueInt kviKeyCode1("keycode1",DISP_SETTING_FREQ, 1, 1, NUM_GROUP1, true);
 
@@ -109,11 +109,11 @@
     }
 
     DEBUG("== paraSetting\r\n");
-    setting_inc = false;
-    setting_next = setting_enter = false;
-    while( ! setting_next) {      // Bボタンが離されるまで
-        if (setting_inc) {        // Aボタンが押されたら
-            setting_inc = false;
+    Setting_inc = false;
+    Setting_next = Setting_enter = false;
+    while( ! Setting_next) {      // Bボタンが離されるまで
+        if (Setting_inc) {        // Aボタンが押されたら
+            Setting_inc = false;
             para->inc();
             displayNumber(para->value);
         }
@@ -161,8 +161,8 @@
 //  BLE & HID
 //---------------------------------
 BLE ble;
-KeyboardService *kbdServicePtr;
-BLE_MESSAGE bleMessage;
+KeyboardService *KbdServicePtr;
+BLE_MESSAGE BleMessage;
 
 /** ----------  
  * @brief  BLE接続が切断された時のコールバック関数
@@ -171,7 +171,7 @@
 {
     DEBUG("(BLE)disconnected\r\n");
     ble.gap().startAdvertising(); // restart advertising
-    bleMessage = BLE_NO_MESSAGE;
+    BleMessage = BLE_NO_MESSAGE;
 }
 
 /** ----------  
@@ -179,10 +179,10 @@
  */
 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
 {
-    if(kbdServicePtr->isConnected()) {
+    if(KbdServicePtr->isConnected()) {
         DEBUG("(BLE)connected\r\n");
         ble.gap().stopAdvertising();
-        bleMessage = BLE_CONNECTED;
+        BleMessage = BLE_CONNECTED;
     }
 }
 
@@ -191,11 +191,11 @@
  */
 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
 {
-    printf("(BLE)Input passKey: ");
-    for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
-        printf("%c", passkey[i]);
-    }
-    printf("\r\n");
+//    printf("(BLE)Input passKey: ");
+//    for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
+//        printf("%c", passkey[i]);
+//    }
+//    printf("\r\n");
 }
 
 /** ----------  
@@ -205,11 +205,11 @@
 {
     if (status == SecurityManager::SEC_STATUS_SUCCESS) {
 //        DEBUG("(BLE)Security success %d\r\n", status);
-        bleMessage = BLE_PAIRING_SUCCESS;
+        BleMessage = BLE_PAIRING_SUCCESS;
 
     } else {
 //        DEBUG("(BLE)Security failed %d\r\n", status);
-        bleMessage = BLE_PAIRING_FAILED;
+        BleMessage = BLE_PAIRING_FAILED;
     }
 }
 
@@ -281,7 +281,7 @@
 
     DEBUG("(BLE)adding hid service\r\n");
     KeyboardService kbdService(ble);
-    kbdServicePtr = &kbdService;
+    KbdServicePtr = &kbdService;
 
     DEBUG("(BLE)adding device info and battery service\r\n");
     initializeHOGP(ble);
@@ -304,15 +304,13 @@
 MicroBitMessageBus bus;
 MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
 MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
-MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
-MicroBitPin P1(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ALL);
-MicroBitPin P2(MICROBIT_ID_IO_P2, MICROBIT_PIN_P2, PIN_CAPABILITY_ALL);
+//MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
+//MicroBitPin P1(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ALL);
+//MicroBitPin P2(MICROBIT_ID_IO_P2, MICROBIT_PIN_P2, PIN_CAPABILITY_ALL);
 
-//MicroBitButton button0(MICROBIT_PIN_P0, MICROBIT_ID_IO_P0, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
-//MicroBitButton button1(MICROBIT_PIN_P1, MICROBIT_ID_IO_P1, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
-//MicroBitButton button2(MICROBIT_PIN_P2, MICROBIT_ID_IO_P2, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
-MicroBitButton button15(MICROBIT_PIN_P15, MICROBIT_ID_IO_P15, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
-MicroBitButton button16(MICROBIT_PIN_P16, MICROBIT_ID_IO_P16, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
+MicroBitButton button0(MICROBIT_PIN_P0, MICROBIT_ID_IO_P0, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
+MicroBitButton button1(MICROBIT_PIN_P1, MICROBIT_ID_IO_P1, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
+MicroBitButton button2(MICROBIT_PIN_P2, MICROBIT_ID_IO_P2, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
 
 /** ----------  
  * @brief  キーコードを送信する
@@ -326,11 +324,12 @@
         if (key > KEYMAP_SIZE ) {
             DispChar = DISP_ERROR_INCORRECT_CODE;      // キーコード設定間違い
         } else {
-            ble_error_t ret = kbdServicePtr->keyDownCode(key, modif);
-//                    DEBUG("  code=%d  modif=%d\r\n",key , modif);
+            printf("  Cnt=%d  code=%d  modif=%d\r\n",Cnt++, key , modif);
+            wait(0.05);
+            ble_error_t ret = KbdServicePtr->keyDownCode(key, modif);
             if (ret) {
                 DispChar = DISP_BLE_ERROR_SENDDATA;
-                DispCharLast = DISP_NO_MESSAGE;   // E が続く時に表示
+                DispCharLast = DISP_NO_MESSAGE;   
                 DEBUG("(BLE)Error %d\r\n",ret);
             }
         }
@@ -366,17 +365,15 @@
  */
 static void onButtonDown(MicroBitEvent e)
 {
-//   DEBUG("  Button Down %d  State=%d\r\n", e.source, State);
-
     switch (State) {
         case STATE_SETTING_FREQ :
         case STATE_SETTING_DEVICE :
             switch(e.source) {
                 case MICROBIT_ID_BUTTON_A :
-                    setting_inc = true;
+                    Setting_inc = true;
                     break;
                 case MICROBIT_ID_BUTTON_B :
-                    setting_enter = true;
+                    Setting_enter = true;
                     break;
             }
             break;
@@ -392,11 +389,9 @@
 					KeyBuff.push(BUTTON_STATUS_DOWN + BUTTON_NAME_P0);
                     break;
                 case MICROBIT_ID_IO_P1 :
-                case MICROBIT_ID_IO_P16 :
 					KeyBuff.push(BUTTON_STATUS_DOWN + BUTTON_NAME_P1);
                     break;
                 case MICROBIT_ID_IO_P2 :
-                case MICROBIT_ID_IO_P15 :
 					KeyBuff.push(BUTTON_STATUS_DOWN + BUTTON_NAME_P2);
                     break;
             }
@@ -437,11 +432,10 @@
  */
 static void onButtonUp(MicroBitEvent e)
 {
-//    DEBUG("  Button up %d\r\n", e.source);
     switch (State) {
         case STATE_SETTING_FREQ :
         case STATE_SETTING_DEVICE :
-            if(setting_enter) setting_next = true;    // 決定ボタンを離したら次へ
+            if(Setting_enter) Setting_next = true;    // 決定ボタンを離したら次へ
             break;
         case STATE_OPERATING :
 			KeyBuff.push(BUTTON_STATUS_UP);
@@ -451,10 +445,11 @@
 static void ButtonUpProcess() 
 {
     DispChar = DISP_NO_MESSAGE;
-    ble_error_t ret = kbdServicePtr->keyUpCode();
+    wait(0.05);
+    ble_error_t ret = KbdServicePtr->keyUpCode();
     if (ret) {
         DispChar = DISP_BLE_ERROR_SENDDATA;
-        DispCharLast = DISP_NO_MESSAGE;   // E が続く時に表示
+        DispCharLast = DISP_NO_MESSAGE;   
     }
 }
 
@@ -491,12 +486,6 @@
     bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_UP,   onButtonUp);
     bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_HOLD, onButtonHold);
 
-    // Put the P0, P1 and P2 pins into touch sense mode.
-    P0.isTouched();
-    P1.isTouched();
-    P2.isTouched();
-
-
 //----- Device specific settings  
     DispChar = paraSettingSpec();
 //----- Setting
@@ -508,25 +497,24 @@
 
 //----- Wait to connect
     display.printChar('W');
-    while (bleMessage != BLE_CONNECTED) {
+    while (BleMessage != BLE_CONNECTED) {
         wait(0.15);
         ble.waitForEvent();        // BLEイベントを待つ
-        if(bleMessage != BLE_NO_MESSAGE) {    // BLEの状態を表示する
-            display.printChar(bleDispChar[bleMessage] );
+        if(BleMessage != BLE_NO_MESSAGE) {    // BLEの状態を表示する
+            display.printChar(bleDispChar[BleMessage] );
         }
     }
     wait(3.0);
-    bleMessage = BLE_NO_MESSAGE;
+    BleMessage = BLE_NO_MESSAGE;
     display.clear();
 //----- Loop
     State = STATE_OPERATING;
     while (true) {
-        wait(0.02);
         ble.waitForEvent();        // BLEイベントを待つ
 
-        if(bleMessage != BLE_NO_MESSAGE) {    // BLEの状態を表示する
-            DispChar = bleDispChar[bleMessage];
-            bleMessage = BLE_NO_MESSAGE;    
+        if(BleMessage != BLE_NO_MESSAGE) {    // BLEの状態を表示する
+            DispChar = bleDispChar[BleMessage];
+            BleMessage = BLE_NO_MESSAGE;    
         }
 
 		if(! KeyBuff.empty()) {          // キーバッファ処理
@@ -562,5 +550,3 @@
         DispCharLast = DispChar;
     }
 }
-
-