microbit_BLE_RCBController_mbed
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
Revision 23:53883bb55dc3, committed 2017-10-10
- Comitter:
- robo8080
- Date:
- Tue Oct 10 12:46:14 2017 +0000
- Parent:
- 22:406127954d1f
- Commit message:
- first commit
Changed in this revision
RCBController.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 406127954d1f -r 53883bb55dc3 RCBController.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RCBController.h Tue Oct 10 12:46:14 2017 +0000 @@ -0,0 +1,74 @@ + + +typedef union +{ + + struct { +/* +1-2バイト目:ボタン + UP: 0x0001 + DOWN: 0x0002 + RIGHT: 0x0004 + LEFT: 0x0008 + Y button: 0x0010 + A button: 0x0020 + B button: 0x0040 + X button: 0x0100 + L1: 0x0200 + L2: 0x0400 + R1: 0x0800 + R2: 0x1000 + START: 0x0003 + SELECT: 0x000C +*/ + unsigned X : 1; + unsigned L1 : 1; + unsigned L2 : 1; + unsigned R1 : 1; + unsigned R2 : 1; + unsigned space1 : 3; + unsigned UP : 1; + unsigned DOWN : 1; + unsigned RIGHT : 1; + unsigned LEFT : 1; + unsigned Y : 1; + unsigned A : 1; + unsigned B : 1; + unsigned space2 : 1; +/* +3-4バイト目:左アナログ + 左右: 1-255 (Neutral=128) + 上下: 1-255 (Neutral=128) +*/ + unsigned LeftAnalogLR:8; + unsigned LeftAnalogUD:8; +/* +5-6バイト目:右アナログ + 左右: 1-255 (Neutral=128) + 上下: 1-255 (Neutral=128) +*/ + unsigned RightAnalogLR:8; + unsigned RightAnalogUD:8; +/* +7-9バイト目:アクセラレータ + X軸: 1-255 (Neutral=128) + Y軸: 1-255 (Neutral=128) + Z軸: 1-255 (Neutral=128) +*/ + unsigned AcceleX:8; + unsigned AcceleY:8; + unsigned AcceleZ:8; +/* +10バイト目:設定(向き、設定) + 7-6bit目: アクセラレータ設定(0-3) + 5bit目: 左アナログ(0-1) + 4bit目: 右アナログ(0-1) + 3-1bit目: iOSデバイス向き(1-4) +*/ + unsigned DEV_DIR:4; + unsigned RIGHT_ANALOG:1; + unsigned LEFT_ANALOG:1; + unsigned ACCELE_SETTING:2; + }status; + unsigned char data[10]; +}RCBController; \ No newline at end of file
diff -r 406127954d1f -r 53883bb55dc3 main.cpp --- a/main.cpp Mon Nov 09 17:08:47 2015 +0000 +++ b/main.cpp Tue Oct 10 12:46:14 2017 +0000 @@ -1,31 +1,37 @@ #include "mbed.h" #include "ble/BLE.h" +#include "RCBController.h" -DigitalOut led(LED1, 1); -uint16_t customServiceUUID = 0xA000; -uint16_t readCharUUID = 0xA001; -uint16_t writeCharUUID = 0xA002; +Serial pc(USBTX, USBRX); + +uint16_t RCBController_service_uuid = 0xFFF0; +uint16_t RCBController_Characteristic_uuid = 0xFFF1; -const static char DEVICE_NAME[] = "ChangeMe!!"; // change this +RCBController controller; + +const static char DEVICE_NAME[] = "micro:bit"; // change this static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development +//static const uint16_t uuid16_list[] = {0xFFF0}; //Custom UUID, FFFF is reserved for development /* Set Up custom Characteristics */ -static uint8_t readValue[10] = {0}; -ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); - static uint8_t writeValue[10] = {0}; -WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); +WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> Controller(RCBController_Characteristic_uuid, writeValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); /* Set up custom service */ -GattCharacteristic *characteristics[] = {&readChar, &writeChar}; -GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); +GattCharacteristic *characteristics[] = {&Controller}; +GattService customService(RCBController_service_uuid, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); +void connectionCallback(const Gap::ConnectionCallbackParams_t *) +{ + printf("connectionCallback\n\r"); +} /* * Restart advertising when phone app disconnects */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { + printf("disconnectionCallback\n\r"); BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); } @@ -34,23 +40,17 @@ */ void writeCharCallback(const GattWriteCallbackParams *params) { + printf("writeCharCallback\n\r"); /* Check to see what characteristic was written, by handle */ - if(params->handle == writeChar.getValueHandle()) { + if(params->handle == Controller.getValueHandle()) { /* toggle LED if only 1 byte is written */ - if(params->len == 1) { - led = params->data[0]; - (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle - } - /* Print the data if more than 1 byte is written */ - else { + if(params->len == 10) { printf("Data received: length = %d, data = 0x",params->len); for(int x=0; x < params->len; x++) { printf("%x", params->data[x]); } printf("\n\r"); } - /* Update the readChar with the value of writeChar */ - BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); } } /* @@ -65,6 +65,7 @@ return; } + ble.gap().onConnection(connectionCallback); ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(writeCharCallback); @@ -87,6 +88,7 @@ */ int main(void) { + pc.baud(115200); /* initialize stuff */ printf("\n\r********* Starting Main Loop *********\n\r");