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

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
masakjm
Date:
Tue Jun 11 18:08:53 2019 +0000
Parent:
2:8e2e6c6658be
Commit message:
Change the usage of timer.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
microbit_switch_if_3sw.h Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
diff -r 8e2e6c6658be -r d8fd4efb63cc main.cpp
--- a/main.cpp	Sat Jun 08 04:40:57 2019 +0000
+++ b/main.cpp	Tue Jun 11 18:08:53 2019 +0000
@@ -7,7 +7,7 @@
 //    The MIT License (MIT)   Copyright (c) 2019 Masatomo Kojima
 //---------------------------------
 
-#define VERSION     "3SW-190608"
+#define VERSION     "3SW-190612"
 #define NO_DEBUG
 
 #include "microbit_switch_if_3sw.h"
@@ -26,7 +26,6 @@
 int  State;                       // 状態遷移
 char DispChar = DISP_NO_MESSAGE;  // LEDに表示する文字コード
 char DispCharLast = 0;            // 最後に表示した文字コード
-bool TurnOffMode = false;         // LED非表示モードのフラグ
 int  Cnt = 0;                     // カウンター
 
 /** ----------
@@ -42,6 +41,13 @@
     }
 }
 
+/** ----------
+ * @brief 一定期間後に表示をOFFにする
+ */
+static void turnOff() {
+    display.disable();
+}
+ 
 //---------------------------------
 //  Flash Memory
 //---------------------------------
@@ -86,13 +92,13 @@
 bool Setting_enter;         // 設定値を入力
 bool Setting_next;          // 次の設定値に移動
 
-KeyValueInt kviKeyCode1("keycode1",DISP_SETTING_FREQ, 1, 1, NUM_GROUP1, true);
+KeyValueInt kviKeyCode1("keycode1",DISP_SETTING_FREQ, 1, 1, NUM_GROUP_3SW, true);
 
 /** ----------  
  * @brief   1つのパラメータの表示と変更
  * @param   storage  不揮発メモリインスタンス
  * @param   para     パラメータのキーと値の組
- * @param   change   変更する時 true
+ * @param   change   変更する時        true
  * @param   disp     今の値を表示する時 true
  * @return  bool true : Success    false : Failed
  */
@@ -127,7 +133,7 @@
 /** ----------  
  * @brief  よく変更するパラメータの表示と変更
  * @param  change   true:変更する時
- * @return  char 0 : Success    'e' : Failed
+ * @return  char 0 : Success    DISP_BLE_ERROR_WRITEDDATA : Failed
  */
 static char paraSettingFreq(bool change=false)
 {
@@ -135,13 +141,13 @@
 
     kviKeyCode1.set(FlashGet(storage, kviKeyCode1.key, 1));
     if (paraSettingOne(storage, &kviKeyCode1, change, true)) return 0;
-    else return 'e';
+    else return DISP_BLE_ERROR_WRITEDDATA;
 }
 
 /** ----------  
  * @brief  デバイス固有のパラメータの表示と変更
  * @param  change   true:変更する時
- * @return  char 0 : Success    'e' : Failed
+ * @return  char 0 : Success    DISP_BLE_ERROR_WRITEDDATA : Failed
  */
 static char paraSettingSpec(bool change=false)
 {
@@ -151,10 +157,10 @@
 
     if(State == STATE_SETTING_DEVICE) {
 //        if (paraSettingOne(storage, &kviStickDirec, true , true)) return 0;
-//        else return 'e';
+//        else return DISP_BLE_ERROR_WRITEDDATA;
     } else  {
 //        if (paraSettingOne(storage, &kviStickDirec, false , false)) return 0;
-//        else return 'e';
+//        else return DISP_BLE_ERROR_WRITEDDATA;
     }
 }
 //---------------------------------
@@ -304,13 +310,9 @@
 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);
-
-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 P0(MICROBIT_PIN_P0, MICROBIT_ID_IO_P0, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
+MicroBitButton P1(MICROBIT_PIN_P1, MICROBIT_ID_IO_P1, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
+MicroBitButton P2(MICROBIT_PIN_P2, MICROBIT_ID_IO_P2, MICROBIT_BUTTON_ALL_EVENTS, PullUp);
 
 /** ----------  
  * @brief  キーコードを送信する
@@ -478,8 +480,7 @@
 //----- Display
     display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);
     display.clear();
-    Timer dispTime;         // 連続表示タイマー
-    dispTime.start();
+	Timeout dispTime;
 
 //----- Button
     bus.listen(MICROBIT_ID_ANY, MICROBIT_BUTTON_EVT_DOWN, onButtonDown);
@@ -496,7 +497,7 @@
     display.clear();
 
 //----- Wait to connect
-    display.printChar('W');
+    display.printChar(DISP_BLE_WAIT);
     while (BleMessage != BLE_CONNECTED) {
         wait(0.15);
         ble.waitForEvent();        // BLEイベントを待つ
@@ -535,18 +536,13 @@
 		}
 
         if (DispChar != DispCharLast) {  // 表示文字が変更
-            TurnOffMode = false;
-            dispTime.reset();
             display.enable(); 
             if (DispChar) display.printChar(DispChar);
             else          display.clear();
-        }
-        if (!TurnOffMode) {
-            if (dispTime.read() > TIME_TURN_OFF) {  // 長時間表示
-                TurnOffMode = true;
-                display.disable();
-            }
+            dispTime.detach();
+			dispTime.attach(&turnOff, TIME_TURN_OFF);    // 一定時間後に表示をOFF
         }
         DispCharLast = DispChar;
     }
 }
+
diff -r 8e2e6c6658be -r d8fd4efb63cc microbit_switch_if_3sw.h
--- a/microbit_switch_if_3sw.h	Sat Jun 08 04:40:57 2019 +0000
+++ b/microbit_switch_if_3sw.h	Tue Jun 11 18:08:53 2019 +0000
@@ -13,6 +13,28 @@
 #include "HIDDeviceInformationService.h"
 
 //----------------------
+//  GROVE JoyStick
+//----------------------
+#define JOY_ERR_THRE     0.4      // JoyStick 異常入力の閾値
+#define JOY_LOW_THRE     0.35     // JoyStick 入力値の低い側の閾値
+#define JOY_HIGH_THRE    0.65     // JoyStick 入力値の高い側の閾値
+#define JOY_CENTER_THRE  0.90     // JoyStick の押しスイッチの閾値
+#define EMA_ALPHA        0.22222  // N=8  2/(N+1)
+enum JOY_STATUS {
+    JOY_NEUTRAL = -1,
+    JOY_CENTER_PRESS ,   // 中央
+    JOY_YHIGH_PRESS,    // 左
+    JOY_YLOW_PRESS,     // 右
+    JOY_XHIGH_PRESS,    // 上
+    JOY_XLOW_PRESS,     // 下
+};
+
+float JoyX1[4]={ 1, 0,-1, 0};
+float JoyX2[4]={ 0, 1, 0,-1};
+float JoyY1[4]={ 0,-1, 0, 1};
+float JoyY2[4]={ 1, 0,-1, 0};
+
+//----------------------
 //  Keybord
 //----------------------
 #define MODIFY_CTRL     0x100
@@ -53,14 +75,11 @@
 //----------------------
 //  Setting
 //----------------------
-#define NUM_GROUP1         7
-#define NUM_G1MEMBER       3
-
 #define NUM_GROUP_3SW      7
 #define NUM_3SW_MEMBER     3
 #define NUM_GROUP_JOY      5
 #define NUM_JOY_MEMBER     5
-#define NUM_DEVICE         3
+#define NUM_JOY_DIREC      4
 
 const int keyCodeGroup_AB[3] = {   // Button A, Button B, Button A&B
    'a', 
@@ -210,8 +229,8 @@
 		DISP_NO_MESSAGE, 
 		DISP_BLE_CONNECTED,
 		DISP_BLE_PAIRING_SUCCESS,
-		DISP_BLE_PAIRING_FAILED};
-
+		DISP_BLE_PAIRING_FAILED
+};
 
 //----------------------
 //  Input Buffer
@@ -222,12 +241,7 @@
 
 #define  BUTTON_NAME_A                 1     // A
 #define  BUTTON_NAME_B                 2     // B
-#define  BUTTON_NAME_P0                3     // P0
-#define  BUTTON_NAME_P1                4     // P1
-#define  BUTTON_NAME_P2                5     // P2
-
+#define  BUTTON_NAME_P0                4     // P0
+#define  BUTTON_NAME_P1                8     // P1
+#define  BUTTON_NAME_P2               16     // P2
 
-
-
- 
-
diff -r 8e2e6c6658be -r d8fd4efb63cc readme.txt
--- a/readme.txt	Sat Jun 08 04:40:57 2019 +0000
+++ b/readme.txt	Tue Jun 11 18:08:53 2019 +0000
@@ -7,8 +7,8 @@
 //    The MIT License (MIT)   Copyright (c) 2019 Masatomo Kojima
 //
 //  Please refer to the following documents. (Japanese only)
-//     http://mahoro-ba.net/files/workshop_text_switch_if_701.pdf
-//     http://mahoro-ba.net/files/workshop_text_switch_if_208.pdf
+//     http://mahoro-ba.net/files/workshop_text_switch_if_702.pdf
+//     http://mahoro-ba.net/files/workshop_text_switch_if_209.pdf
 //
 //  I refer to information written on the following sites.
 //    (1)https://os.mbed.com/teams/microbit/code/microbit_presenter/
@@ -31,5 +31,5 @@
 3SW-190313  設定Tを廃止し、設定Sで3つのスイッチの組み合わせを指定するように変更
        S=3 をスイッチコントロール用に他で使わないコードを割り当てる
 3SW-190316  S=3のキー割り当てを変更
-3SW-190608  キーバッファ方式に変更            
-
+3SW-190607  キーバッファ方式に変更            
+3SW-190612  timerの使い方を変更