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

Dependencies:   microbit

microbit_switch_if_3sw.h

Committer:
masakjm
Date:
2019-06-08
Revision:
2:8e2e6c6658be
Parent:
1:9d0e2e5b5d25
Child:
3:d8fd4efb63cc

File content as of revision 2:8e2e6c6658be:

//=================================
// microbit_switch_if_3sw
//=================================
//    BLE switch interface with 3 tact switches for micro:bit 
//    The MIT License (MIT)   Copyright (c) 2019 Masatomo Kojima

#include "mbed.h"
#include "MicroBit.h"
#include "ble/services/BatteryService.h"
#include "KeyboardService.h"
#include "Keyboard_types.h"
#include "HIDServiceBase.h"
#include "HIDDeviceInformationService.h"

//----------------------
//  Keybord
//----------------------
#define MODIFY_CTRL     0x100
#define MODIFY_SHIFT    0x200
#define MODIFY_OPTION   0x400  /* option or alt */
#define MODIFY_COMMAND  0x800  /* command or Windows */

const int KEY_BS    = 8;  /* Keyboard Backspace */
const int KEY_TAB   = 9;  /* Keyboard Tab */
const int KEY_ENTER = 10; /* Keyboard Return (Enter) */
//const int KEY_ESC = 27;   /* Keyboard Escape */
const int KEY_SPACE = 32; /* Keyboard Space */

const int KEY_EQUAL    = 0x2b;                   /* = */
const int KEY_COMMA    = 0x2c;                   /* , */
const int KEY_MINUS    = 0x2d;                   /* - */
const int KEY_PIRIOD   = 0x2e;                   /* . */
const int KEY_SLASH    = 0x2f;                   /* / */
const int KEY_QUOT     = 0x40;                   /* ' */

const int KEY_PLUS     = MODIFY_SHIFT +0x2b;     /* + */
const int KEY_LT       = MODIFY_SHIFT +0x2c;     /* < */
const int KEY_UNDER    = MODIFY_SHIFT +0x2d;     /* _ */
const int KEY_GT       = MODIFY_SHIFT +0x2e;     /* > */
const int KEY_QUESTION = MODIFY_SHIFT +0x2f;     /* ? */
const int KEY_RBRACKET = MODIFY_SHIFT +0x30;     /* )  shif+0 */
const int KEY_EXCLA    = MODIFY_SHIFT +0x31;     /* !  shif+1 */
const int KEY_AT       = MODIFY_SHIFT +0x32;     /* @  shif+2 */
const int KEY_NUMBER   = MODIFY_SHIFT +0x33;     /* #  shif+3 */
const int KEY_DOLL     = MODIFY_SHIFT +0x34;     /* $  shif+4 */
const int KEY_PERCENT  = MODIFY_SHIFT +0x35;     /* %  shif+5 */
const int KEY_CARET    = MODIFY_SHIFT +0x36;     /* ^  shif+6 */
const int KEY_AMP      = MODIFY_SHIFT +0x37;     /* &  shif+7 */
const int KEY_ASTERISK = MODIFY_SHIFT +0x38;     /* *  shif+8 */
const int KEY_LBRACKET = MODIFY_SHIFT +0x39;     /* (  shif+9 */
const int KEY_DBLQUOT  = MODIFY_SHIFT +0x40;     /* " */

//----------------------
//  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

const int keyCodeGroup_AB[3] = {   // Button A, Button B, Button A&B
   'a', 
   'b', 
   MODIFY_COMMAND + 'h'       // アプリ終了
};
const int keyCodeGroup_3SW[NUM_GROUP_3SW][NUM_3SW_MEMBER] = { // P0 P1 P2
    {KEY_TAB,              KEY_SPACE,        KEY_ENTER},
    {KEY_F6,               RIGHT_ARROW,      LEFT_ARROW}, 
    {KEY_F1,               KEY_F2,           KEY_F3},
    {MODIFY_COMMAND + 'r', MODIFY_OPTION + DOWN_ARROW, MODIFY_OPTION + UP_ARROW},
    {KEY_F7,               '1',              '3'},
    {KEY_F8,               'h',              'j'},
    {'0',                  DOWN_ARROW,       UP_ARROW}
};

const int keyCodeGroup_JOY[NUM_GROUP_JOY][NUM_JOY_MEMBER] = { // 中央 左 右 上 下
    {0,           KEY_TAB,     KEY_SPACE,   0,           KEY_ENTER  },
    {KEY_HOME,    RIGHT_ARROW, LEFT_ARROW,  DOWN_ARROW,  UP_ARROW   },
    {KEY_F1,      KEY_F2,      KEY_F3,      KEY_F4,      KEY_F5},
    {MODIFY_COMMAND+'r', 0, 0, MODIFY_OPTION+DOWN_ARROW, MODIFY_OPTION+UP_ARROW},
    {KEY_ENTER,   KEY_TAB,     KEY_SPACE,   0,           0  },
};

//in "BLE_HID\Keyboad_types.h"
//enum FUNCTION_KEY {
//    KEY_F1 = 128,   /* F1 key */
//    KEY_F2,         /* F2 key */
//    KEY_F3,         /* F3 key */
//    KEY_F4,         /* F4 key */
//    KEY_F5,         /* F5 key */
//    KEY_F6,         /* F6 key */
//    KEY_F7,         /* F7 key */
//    KEY_F8,         /* F8 key */
//    KEY_F9,         /* F9 key */
//    KEY_F10,        /* F10 key */
//    KEY_F11,        /* F11 key */
//    KEY_F12,        /* F12 key */
// 
//    KEY_PRINT_SCREEN,   /* Print Screen key */
//    KEY_SCROLL_LOCK,    /* Scroll lock */
//    KEY_CAPS_LOCK,      /* caps lock */
//    KEY_NUM_LOCK,       /* num lock */
//    KEY_INSERT,         /* Insert key */
//    KEY_HOME,           /* Home key */
//    KEY_PAGE_UP,        /* Page Up key */
//    KEY_PAGE_DOWN,      /* Page Down key */
// 
//    RIGHT_ARROW,        /* Right arrow */
//    LEFT_ARROW,         /* Left arrow */
//    DOWN_ARROW,         /* Down arrow */
//    UP_ARROW,           /* Up arrow */
//};

//----------------------
//  Display
//----------------------
#define TIME_TURN_OFF          7.0   // 非表示モードに入るまでの時間 (s)
#define SETTING_DISPLAY_WAIT   1.0   // 設定モード表示時間 (s)

//----------------------
//  State
//----------------------
enum STATE {
    STATE_DESABLE_INPUT,    // 入力無効時
    STATE_SETTING_FREQ,     // よく変更する設定
    STATE_SETTING_DEVICE,   // デバイス設定時
    STATE_OPERATING,        // 操作時
};
//----------------------
// Disable debug messages by setting NO_DEBUG
//----------------------
#ifndef NO_DEBUG
#define DEBUG(...)
#else
#define DEBUG(...) printf(__VA_ARGS__)
#endif

//----------------------
//  BLE & HID
//----------------------
/**
 * IO capabilities of the device. During development, you most likely want "JustWorks", which means
 * no IO capabilities.
 * It is also possible to use IO_CAPS_DISPLAY_ONLY to generate and show a pincode on the serial
 * output.
 */
#ifndef HID_SECURITY_IOCAPS
#define HID_SECURITY_IOCAPS (SecurityManager::IO_CAPS_NONE)
#endif

/**
 * Security level. MITM disabled forces "Just Works". If you require MITM, HID_SECURITY_IOCAPS must
 * be at least IO_CAPS_DISPLAY_ONLY.
 */
#ifndef HID_SECURITY_REQUIRE_MITM
#define HID_SECURITY_REQUIRE_MITM false
#endif

/**
 * Initialize security manager: set callback functions and required security level
 */
void initializeSecurity(BLE &_ble);

/**
 * - Initialize auxiliary services required by the HID-over-GATT Profile.
 * - Initialize common Gap advertisement.
 *
 * Demos only have to set a custom device name and appearance, and their HID
 * service.
 */
void initializeHOGP(BLE &_ble);

enum BLE_MESSAGE {
    BLE_NO_MESSAGE,         // メッセージ無し
    BLE_CONNECTED,          // BLE接続完了
    BLE_PAIRING_SUCCESS,    // ペアリング成功
    BLE_PAIRING_FAILED,     // ペアリング失敗
};

//----------------------
//  Display Message
//----------------------
#define  DISP_NO_MESSAGE             0       // メッセージ無し
#define  DISP_PRESS_BUTTON_A         'A'     // ボタンA
#define  DISP_PRESS_BUTTON_B         'B'     // ボタンB
#define  DISP_HOLD_BUTTON_AB         'H'     // ボタンABの長押し
#define  DISP_ACTIVE_IO_P0           '0'     // 端子P0
#define  DISP_ACTIVE_IO_P1           '1'     // 端子P1
#define  DISP_ACTIVE_IO_P2           '2'     // 端子P2
#define  DISP_BLE_WAIT               'W'     // BLE接続待ち
#define  DISP_BLE_CONNECTED          'C'     // BLE接続完了
#define  DISP_BLE_PAIRING_SUCCESS    'P'     // ペアリング成功
#define  DISP_BLE_PAIRING_FAILED     'F'     // ペアリング失敗
#define  DISP_BLE_ERROR_SENDDATA     'E'     // データ送出時エラー
#define  DISP_BLE_ERROR_WRITEDDATA   'e'     // データ書込時エラー
#define  DISP_ERROR_INCORRECT_CODE   'I'     // コーディングエラー
#define  DISP_ERROR_DEVICE_CONNECT   'V'     // デバイス接続エラー

#define  DISP_SETTING_FREQ           'S'     // よく変更する設定
#define  DISP_SETTING_DEVICE         'D'     // デバイス設定
#define  DISP_SETTING_SPEC_1         'X'     // 個別設定1
#define  DISP_SETTING_SPEC_2         'Y'     // 個別設定2
#define  DISP_SETTING_SPEC_3         'Z'     // 個別設定3

const char bleDispChar[]={	
		DISP_NO_MESSAGE, 
		DISP_BLE_CONNECTED,
		DISP_BLE_PAIRING_SUCCESS,
		DISP_BLE_PAIRING_FAILED};


//----------------------
//  Input Buffer
//----------------------
#define  BUTTON_STATUS_DOWN            0x100     // 押された
#define  BUTTON_STATUS_UP              0x200     // 離された
#define  BUTTON_STATUS_HOLD            0x300     // 保持された

#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