micro:bitにRCBControllerを接続するサンプルプログラム。 接続が不安定。micro:bitの電源入り切りやリセットSW押下を何度か試せば接続できるかも。
Fork of BLE_GATT_Example by
main.cpp@23:ceaedc4f00fe, 2017-10-11 (annotated)
- Committer:
- robo8080
- Date:
- Wed Oct 11 12:38:27 2017 +0000
- Revision:
- 23:ceaedc4f00fe
- Parent:
- 22:406127954d1f
- Child:
- 24:cc9bc216c814
first commit
Who changed what in which revision?
User | Revision | Line number | New 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], ¶ms->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 | 23:ceaedc4f00fe | 93 | else { |
robo8080 | 23:ceaedc4f00fe | 94 | uBit.display.clear(); |
robo8080 | 23:ceaedc4f00fe | 95 | } |
mbedAustin | 8:60ede963dfe2 | 96 | } |
mbedAustin | 8:60ede963dfe2 | 97 | } |
mbedAustin | 3:0fb60f81f693 | 98 | } |
andresag | 22:406127954d1f | 99 | /* |
andresag | 22:406127954d1f | 100 | * Initialization callback |
andresag | 22:406127954d1f | 101 | */ |
andresag | 22:406127954d1f | 102 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 22:406127954d1f | 103 | { |
andresag | 22:406127954d1f | 104 | BLE &ble = params->ble; |
andresag | 22:406127954d1f | 105 | ble_error_t error = params->error; |
andresag | 22:406127954d1f | 106 | |
andresag | 22:406127954d1f | 107 | if (error != BLE_ERROR_NONE) { |
andresag | 22:406127954d1f | 108 | return; |
andresag | 22:406127954d1f | 109 | } |
mbedAustin | 0:cd5b6733aeb1 | 110 | |
robo8080 | 23:ceaedc4f00fe | 111 | ble.gap().onConnection(connectionCallback); |
andresag | 19:477567297aac | 112 | ble.gap().onDisconnection(disconnectionCallback); |
andresag | 19:477567297aac | 113 | ble.gattServer().onDataWritten(writeCharCallback); |
mbedAustin | 2:e84c13abc479 | 114 | |
andresag | 22:406127954d1f | 115 | /* Setup advertising */ |
andresag | 19:477567297aac | 116 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
andresag | 19:477567297aac | 117 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
andresag | 19:477567297aac | 118 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
andresag | 19:477567297aac | 119 | 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 | 120 | ble.gap().setAdvertisingInterval(100); // 100ms. |
mbedAustin | 2:e84c13abc479 | 121 | |
andresag | 22:406127954d1f | 122 | /* Add our custom service */ |
mbedAustin | 13:62b1d32745ac | 123 | ble.addService(customService); |
mbedAustin | 2:e84c13abc479 | 124 | |
andresag | 22:406127954d1f | 125 | /* Start advertising */ |
andresag | 19:477567297aac | 126 | ble.gap().startAdvertising(); |
andresag | 22:406127954d1f | 127 | } |
andresag | 19:477567297aac | 128 | |
andresag | 22:406127954d1f | 129 | /* |
andresag | 22:406127954d1f | 130 | * Main loop |
andresag | 22:406127954d1f | 131 | */ |
andresag | 22:406127954d1f | 132 | int main(void) |
andresag | 22:406127954d1f | 133 | { |
robo8080 | 23:ceaedc4f00fe | 134 | pc.baud(115200); |
andresag | 22:406127954d1f | 135 | /* initialize stuff */ |
andresag | 22:406127954d1f | 136 | printf("\n\r********* Starting Main Loop *********\n\r"); |
andresag | 22:406127954d1f | 137 | |
andresag | 22:406127954d1f | 138 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
andresag | 22:406127954d1f | 139 | ble.init(bleInitComplete); |
andresag | 22:406127954d1f | 140 | |
andresag | 22:406127954d1f | 141 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 22:406127954d1f | 142 | * BLE object is used in the main loop below. */ |
andresag | 22:406127954d1f | 143 | while (ble.hasInitialized() == false) { /* spin loop */ } |
andresag | 22:406127954d1f | 144 | |
robo8080 | 23:ceaedc4f00fe | 145 | uBit.init(); |
robo8080 | 23:ceaedc4f00fe | 146 | uBit.display.print("D"); |
andresag | 22:406127954d1f | 147 | /* Infinite loop waiting for BLE interrupt events */ |
mbedAustin | 2:e84c13abc479 | 148 | while (true) { |
andresag | 22:406127954d1f | 149 | ble.waitForEvent(); /* Save power */ |
mbedAustin | 2:e84c13abc479 | 150 | } |
andresag | 20:fcc752d401ec | 151 | } |