MicroBit as BLE gamepad

Dependencies:   microbit

Fork of microbit-samples by BBC

Committer:
rengro01
Date:
Mon Jan 30 08:31:03 2017 +0000
Revision:
8:110d5af6f70b
MicroBit as BLE gamepad

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rengro01 8:110d5af6f70b 1 /**
rengro01 8:110d5af6f70b 2 * This program implements a complete HID-over-Gatt Profile:
rengro01 8:110d5af6f70b 3 * - HID is provided by JoystickService
rengro01 8:110d5af6f70b 4 * - Battery Service
rengro01 8:110d5af6f70b 5 * - Device Information Service
rengro01 8:110d5af6f70b 6 */
rengro01 8:110d5af6f70b 7
rengro01 8:110d5af6f70b 8 // MicroBit settings in MicroBitConfig.h located at:
rengro01 8:110d5af6f70b 9 // "microbit/microbit-dal/inc/core/MicroBitConfig.h"
rengro01 8:110d5af6f70b 10
rengro01 8:110d5af6f70b 11 #include "MicroBit.h"
rengro01 8:110d5af6f70b 12 #include "ble/services/BatteryService.h"
rengro01 8:110d5af6f70b 13 #include "HIDDeviceInformationService.h"
rengro01 8:110d5af6f70b 14 #include "ble/BLE.h"
rengro01 8:110d5af6f70b 15 #include "HIDServiceBase.h"
rengro01 8:110d5af6f70b 16 #include "JoystickService.h"
rengro01 8:110d5af6f70b 17
rengro01 8:110d5af6f70b 18 MicroBit uBit;
rengro01 8:110d5af6f70b 19 JoystickService *jstServicePtr;
rengro01 8:110d5af6f70b 20
rengro01 8:110d5af6f70b 21 void onConnected(MicroBitEvent)
rengro01 8:110d5af6f70b 22 {
rengro01 8:110d5af6f70b 23 uBit.display.print("C");
rengro01 8:110d5af6f70b 24 }
rengro01 8:110d5af6f70b 25
rengro01 8:110d5af6f70b 26 void onDisconnected(MicroBitEvent)
rengro01 8:110d5af6f70b 27 {
rengro01 8:110d5af6f70b 28 uBit.display.print("D");
rengro01 8:110d5af6f70b 29 }
rengro01 8:110d5af6f70b 30
rengro01 8:110d5af6f70b 31 void onButton(MicroBitEvent e)
rengro01 8:110d5af6f70b 32 {
rengro01 8:110d5af6f70b 33 if(!jstServicePtr)
rengro01 8:110d5af6f70b 34 return;
rengro01 8:110d5af6f70b 35
rengro01 8:110d5af6f70b 36 if(!jstServicePtr->isConnected())
rengro01 8:110d5af6f70b 37 {
rengro01 8:110d5af6f70b 38 // we haven't connected yet...
rengro01 8:110d5af6f70b 39 uBit.display.print("W");
rengro01 8:110d5af6f70b 40 }
rengro01 8:110d5af6f70b 41 else
rengro01 8:110d5af6f70b 42 {
rengro01 8:110d5af6f70b 43 if (e.source == MICROBIT_ID_BUTTON_A)
rengro01 8:110d5af6f70b 44 {
rengro01 8:110d5af6f70b 45 if (e.value == MICROBIT_BUTTON_EVT_UP)
rengro01 8:110d5af6f70b 46 {
rengro01 8:110d5af6f70b 47 jstServicePtr->setButton(JOYSTICK_BUTTON_1, BUTTON_UP);
rengro01 8:110d5af6f70b 48 }
rengro01 8:110d5af6f70b 49 else if (e.value == MICROBIT_BUTTON_EVT_DOWN)
rengro01 8:110d5af6f70b 50 {
rengro01 8:110d5af6f70b 51 jstServicePtr->setButton(JOYSTICK_BUTTON_1, BUTTON_DOWN);
rengro01 8:110d5af6f70b 52 }
rengro01 8:110d5af6f70b 53 } else if (e.source == MICROBIT_ID_BUTTON_B)
rengro01 8:110d5af6f70b 54 {
rengro01 8:110d5af6f70b 55 if (e.value == MICROBIT_BUTTON_EVT_UP)
rengro01 8:110d5af6f70b 56 {
rengro01 8:110d5af6f70b 57 jstServicePtr->setButton(JOYSTICK_BUTTON_2, BUTTON_UP);
rengro01 8:110d5af6f70b 58 }
rengro01 8:110d5af6f70b 59 else if (e.value == MICROBIT_BUTTON_EVT_DOWN)
rengro01 8:110d5af6f70b 60 {
rengro01 8:110d5af6f70b 61 jstServicePtr->setButton(JOYSTICK_BUTTON_2, BUTTON_DOWN);
rengro01 8:110d5af6f70b 62 }
rengro01 8:110d5af6f70b 63 }
rengro01 8:110d5af6f70b 64
rengro01 8:110d5af6f70b 65 jstServicePtr->sendCallback();
rengro01 8:110d5af6f70b 66 }
rengro01 8:110d5af6f70b 67 }
rengro01 8:110d5af6f70b 68
rengro01 8:110d5af6f70b 69 int main()
rengro01 8:110d5af6f70b 70 {
rengro01 8:110d5af6f70b 71 // Initialise the micro:bit runtime.
rengro01 8:110d5af6f70b 72 uBit.init();
rengro01 8:110d5af6f70b 73 // Display a start-up message
rengro01 8:110d5af6f70b 74 uBit.display.scroll("BBB");
rengro01 8:110d5af6f70b 75 uBit.display.print("L");
rengro01 8:110d5af6f70b 76
rengro01 8:110d5af6f70b 77 // Application code will go here and in functions outside of main()
rengro01 8:110d5af6f70b 78
rengro01 8:110d5af6f70b 79 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
rengro01 8:110d5af6f70b 80 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);
rengro01 8:110d5af6f70b 81
rengro01 8:110d5af6f70b 82 PnPID_t pnpID;
rengro01 8:110d5af6f70b 83 pnpID.vendorID_source = 0x2; // from the USB Implementer's Forum
rengro01 8:110d5af6f70b 84 pnpID.vendorID = 0x0D28; // NXP
rengro01 8:110d5af6f70b 85 pnpID.productID = 0x0204; // CMSIS-DAP (well, it's a keyboard but oh well)
rengro01 8:110d5af6f70b 86 pnpID.productVersion = 0x0100; // v1.0
rengro01 8:110d5af6f70b 87 new HIDDeviceInformationService(*uBit.ble, "ARM", "m1", "abc", "def", "ghi", "jkl", &pnpID);
rengro01 8:110d5af6f70b 88
rengro01 8:110d5af6f70b 89 new BatteryService(*uBit.ble, 80);
rengro01 8:110d5af6f70b 90 #if 1
rengro01 8:110d5af6f70b 91 new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);
rengro01 8:110d5af6f70b 92 #else
rengro01 8:110d5af6f70b 93 // causes a 020 :( error, aka OOM
rengro01 8:110d5af6f70b 94 jstServicePtr = new JoystickService(*uBit.ble);
rengro01 8:110d5af6f70b 95 #endif
rengro01 8:110d5af6f70b 96
rengro01 8:110d5af6f70b 97 // Register to receive events when any buttons are clicked
rengro01 8:110d5af6f70b 98 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton);
rengro01 8:110d5af6f70b 99 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton);
rengro01 8:110d5af6f70b 100 uBit.display.print("W");
rengro01 8:110d5af6f70b 101
rengro01 8:110d5af6f70b 102 // end of application code in main()
rengro01 8:110d5af6f70b 103
rengro01 8:110d5af6f70b 104 // If main exits, there may still be other fibers running or registered event handlers etc.
rengro01 8:110d5af6f70b 105 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
rengro01 8:110d5af6f70b 106 // sit in the idle task forever, in a power efficient sleep.
rengro01 8:110d5af6f70b 107 release_fiber();
rengro01 8:110d5af6f70b 108 }