micro:bitにRCBControllerを接続するサンプルプログラム。 接続が不安定。micro:bitの電源入り切りやリセットSW押下を何度か試せば接続できるかも。

Dependencies:   microbit

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
robo8080
Date:
Thu Oct 12 06:05:04 2017 +0000
Revision:
24:cc9bc216c814
Parent:
23:ceaedc4f00fe
X,Y,A,B ????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robo8080 23:ceaedc4f00fe 1 #include "MicroBit.h"
andresag 19:477567297aac 2 #include "ble/BLE.h"
robo8080 23:ceaedc4f00fe 3 #include "RCBController.h"
robo8080 23:ceaedc4f00fe 4
robo8080 23:ceaedc4f00fe 5 Serial pc(USBTX, USBRX);
mbedAustin 1:94152e7d8b5c 6
robo8080 23:ceaedc4f00fe 7 MicroBit uBit;
robo8080 23:ceaedc4f00fe 8 MicroBitImage ARROW_N("\
robo8080 23:ceaedc4f00fe 9 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 10 0 1 1 1 0\n\
robo8080 23:ceaedc4f00fe 11 1 0 1 0 1\n\
robo8080 23:ceaedc4f00fe 12 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 13 0 0 1 0 0\n");
robo8080 23:ceaedc4f00fe 14 MicroBitImage ARROW_S("\
robo8080 23:ceaedc4f00fe 15 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 16 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 17 1 0 1 0 1\n\
robo8080 23:ceaedc4f00fe 18 0 1 1 1 0\n\
robo8080 23:ceaedc4f00fe 19 0 0 1 0 0\n");
robo8080 23:ceaedc4f00fe 20 MicroBitImage ARROW_E("\
robo8080 23:ceaedc4f00fe 21 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 22 0 0 0 1 0\n\
robo8080 23:ceaedc4f00fe 23 1 1 1 1 1\n\
robo8080 23:ceaedc4f00fe 24 0 0 0 1 0\n\
robo8080 23:ceaedc4f00fe 25 0 0 1 0 0\n");
robo8080 23:ceaedc4f00fe 26 MicroBitImage ARROW_W("\
robo8080 23:ceaedc4f00fe 27 0 0 1 0 0\n\
robo8080 23:ceaedc4f00fe 28 0 1 0 0 0\n\
robo8080 23:ceaedc4f00fe 29 1 1 1 1 1\n\
robo8080 23:ceaedc4f00fe 30 0 1 0 0 0\n\
robo8080 23:ceaedc4f00fe 31 0 0 1 0 0\n");
mbedAustin 1:94152e7d8b5c 32
robo8080 23:ceaedc4f00fe 33 uint16_t RCBController_service_uuid = 0xFFF0;
robo8080 23:ceaedc4f00fe 34 uint16_t RCBController_Characteristic_uuid = 0xFFF1;
robo8080 23:ceaedc4f00fe 35
robo8080 23:ceaedc4f00fe 36 RCBController controller;
robo8080 23:ceaedc4f00fe 37
robo8080 23:ceaedc4f00fe 38 const static char DEVICE_NAME[] = "micro:bit"; // change this
mbedAustin 8:60ede963dfe2 39 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
robo8080 23:ceaedc4f00fe 40 //static const uint16_t uuid16_list[] = {0xFFF0}; //Custom UUID, FFFF is reserved for development
mbedAustin 1:94152e7d8b5c 41
andresag 22:406127954d1f 42 /* Set Up custom Characteristics */
mbedAustin 9:b33f42191584 43 static uint8_t writeValue[10] = {0};
robo8080 23:ceaedc4f00fe 44 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> Controller(RCBController_Characteristic_uuid, writeValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
mbedAustin 2:e84c13abc479 45
andresag 22:406127954d1f 46 /* Set up custom service */
robo8080 23:ceaedc4f00fe 47 GattCharacteristic *characteristics[] = {&Controller};
robo8080 23:ceaedc4f00fe 48 GattService customService(RCBController_service_uuid, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
mbedAustin 2:e84c13abc479 49
robo8080 23:ceaedc4f00fe 50 void connectionCallback(const Gap::ConnectionCallbackParams_t *)
robo8080 23:ceaedc4f00fe 51 {
robo8080 23:ceaedc4f00fe 52 uBit.display.print("C");
robo8080 23:ceaedc4f00fe 53 printf("connectionCallback\n\r");
robo8080 23:ceaedc4f00fe 54 }
mbedAustin 2:e84c13abc479 55
mbedAustin 8:60ede963dfe2 56 /*
mbedAustin 8:60ede963dfe2 57 * Restart advertising when phone app disconnects
andresag 19:477567297aac 58 */
andresag 19:477567297aac 59 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 60 {
robo8080 23:ceaedc4f00fe 61 uBit.display.print("D");
robo8080 23:ceaedc4f00fe 62 printf("disconnectionCallback\n\r");
andresag 22:406127954d1f 63 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 64 }
mbedAustin 0:cd5b6733aeb1 65
andresag 19:477567297aac 66 /*
andresag 22:406127954d1f 67 * Handle writes to writeCharacteristic
mbedAustin 8:60ede963dfe2 68 */
melmon 18:7d89c4fba362 69 void writeCharCallback(const GattWriteCallbackParams *params)
mbedAustin 3:0fb60f81f693 70 {
robo8080 23:ceaedc4f00fe 71 printf("writeCharCallback\n\r");
andresag 22:406127954d1f 72 /* Check to see what characteristic was written, by handle */
robo8080 23:ceaedc4f00fe 73 if(params->handle == Controller.getValueHandle()) {
robo8080 23:ceaedc4f00fe 74 if(params->len == 10) {
andresag 19:477567297aac 75 printf("Data received: length = %d, data = 0x",params->len);
mbedAustin 8:60ede963dfe2 76 for(int x=0; x < params->len; x++) {
andresag 19:477567297aac 77 printf("%x", params->data[x]);
mbedAustin 8:60ede963dfe2 78 }
andresag 19:477567297aac 79 printf("\n\r");
robo8080 23:ceaedc4f00fe 80 memcpy( &controller.data[0], &params->data[0], sizeof(controller));
robo8080 23:ceaedc4f00fe 81 if(controller.status.UP) {
robo8080 23:ceaedc4f00fe 82 uBit.display.print(ARROW_N);
robo8080 23:ceaedc4f00fe 83 }
robo8080 23:ceaedc4f00fe 84 else if(controller.status.DOWN) {
robo8080 23:ceaedc4f00fe 85 uBit.display.print(ARROW_S);
robo8080 23:ceaedc4f00fe 86 }
robo8080 23:ceaedc4f00fe 87 else if(controller.status.RIGHT) {
robo8080 23:ceaedc4f00fe 88 uBit.display.print(ARROW_E);
robo8080 23:ceaedc4f00fe 89 }
robo8080 23:ceaedc4f00fe 90 else if(controller.status.LEFT) {
robo8080 23:ceaedc4f00fe 91 uBit.display.print(ARROW_W);
robo8080 23:ceaedc4f00fe 92 }
robo8080 24:cc9bc216c814 93 else if(controller.status.X) {
robo8080 24:cc9bc216c814 94 uBit.display.print("X");
robo8080 24:cc9bc216c814 95 }
robo8080 24:cc9bc216c814 96 else if(controller.status.Y) {
robo8080 24:cc9bc216c814 97 uBit.display.print("Y");
robo8080 24:cc9bc216c814 98 }
robo8080 24:cc9bc216c814 99 else if(controller.status.A) {
robo8080 24:cc9bc216c814 100 uBit.display.print("A");
robo8080 24:cc9bc216c814 101 }
robo8080 24:cc9bc216c814 102 else if(controller.status.B) {
robo8080 24:cc9bc216c814 103 uBit.display.print("B");
robo8080 24:cc9bc216c814 104 }
robo8080 23:ceaedc4f00fe 105 else {
robo8080 23:ceaedc4f00fe 106 uBit.display.clear();
robo8080 23:ceaedc4f00fe 107 }
mbedAustin 8:60ede963dfe2 108 }
mbedAustin 8:60ede963dfe2 109 }
mbedAustin 3:0fb60f81f693 110 }
andresag 22:406127954d1f 111 /*
andresag 22:406127954d1f 112 * Initialization callback
andresag 22:406127954d1f 113 */
andresag 22:406127954d1f 114 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 115 {
andresag 22:406127954d1f 116 BLE &ble = params->ble;
andresag 22:406127954d1f 117 ble_error_t error = params->error;
andresag 22:406127954d1f 118
andresag 22:406127954d1f 119 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 120 return;
andresag 22:406127954d1f 121 }
mbedAustin 0:cd5b6733aeb1 122
robo8080 23:ceaedc4f00fe 123 ble.gap().onConnection(connectionCallback);
andresag 19:477567297aac 124 ble.gap().onDisconnection(disconnectionCallback);
andresag 19:477567297aac 125 ble.gattServer().onDataWritten(writeCharCallback);
mbedAustin 2:e84c13abc479 126
andresag 22:406127954d1f 127 /* Setup advertising */
andresag 19:477567297aac 128 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 129 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
andresag 19:477567297aac 130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
andresag 19:477567297aac 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
andresag 19:477567297aac 132 ble.gap().setAdvertisingInterval(100); // 100ms.
mbedAustin 2:e84c13abc479 133
andresag 22:406127954d1f 134 /* Add our custom service */
mbedAustin 13:62b1d32745ac 135 ble.addService(customService);
mbedAustin 2:e84c13abc479 136
andresag 22:406127954d1f 137 /* Start advertising */
andresag 19:477567297aac 138 ble.gap().startAdvertising();
andresag 22:406127954d1f 139 }
andresag 19:477567297aac 140
andresag 22:406127954d1f 141 /*
andresag 22:406127954d1f 142 * Main loop
andresag 22:406127954d1f 143 */
andresag 22:406127954d1f 144 int main(void)
andresag 22:406127954d1f 145 {
robo8080 23:ceaedc4f00fe 146 pc.baud(115200);
andresag 22:406127954d1f 147 /* initialize stuff */
andresag 22:406127954d1f 148 printf("\n\r********* Starting Main Loop *********\n\r");
andresag 22:406127954d1f 149
andresag 22:406127954d1f 150 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 151 ble.init(bleInitComplete);
andresag 22:406127954d1f 152
andresag 22:406127954d1f 153 /* SpinWait for initialization to complete. This is necessary because the
andresag 22:406127954d1f 154 * BLE object is used in the main loop below. */
andresag 22:406127954d1f 155 while (ble.hasInitialized() == false) { /* spin loop */ }
andresag 22:406127954d1f 156
robo8080 23:ceaedc4f00fe 157 uBit.init();
robo8080 23:ceaedc4f00fe 158 uBit.display.print("D");
andresag 22:406127954d1f 159 /* Infinite loop waiting for BLE interrupt events */
mbedAustin 2:e84c13abc479 160 while (true) {
andresag 22:406127954d1f 161 ble.waitForEvent(); /* Save power */
mbedAustin 2:e84c13abc479 162 }
andresag 20:fcc752d401ec 163 }