Tas Lab
/
BLE_LightingSwitch
Release for tas lab
Fork of BLE_RCBController by
main.cpp@2:dd85fdc18224, 2014-08-20 (annotated)
- Committer:
- jksoft
- Date:
- Wed Aug 20 13:30:11 2014 +0000
- Revision:
- 2:dd85fdc18224
- Parent:
- 1:48f6e08a3ac2
- Child:
- 3:6a9f4ea2e7c6
???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:8c643bfe55b7 | 1 | #include "mbed.h" |
jksoft | 1:48f6e08a3ac2 | 2 | #include "BLEDevice.h" |
jksoft | 0:8c643bfe55b7 | 3 | #include "RCBController.h" |
jksoft | 0:8c643bfe55b7 | 4 | |
jksoft | 1:48f6e08a3ac2 | 5 | #define DBG 1 |
jksoft | 0:8c643bfe55b7 | 6 | |
jksoft | 1:48f6e08a3ac2 | 7 | BLEDevice ble; |
jksoft | 0:8c643bfe55b7 | 8 | Serial pc(USBTX, USBRX); |
jksoft | 0:8c643bfe55b7 | 9 | /* LEDs for indication: */ |
jksoft | 0:8c643bfe55b7 | 10 | DigitalOut ConnectStateLed(LED1); |
jksoft | 0:8c643bfe55b7 | 11 | PwmOut ControllerStateLed(LED2); |
jksoft | 0:8c643bfe55b7 | 12 | |
jksoft | 0:8c643bfe55b7 | 13 | |
jksoft | 0:8c643bfe55b7 | 14 | /* RCBController Service */ |
jksoft | 0:8c643bfe55b7 | 15 | static const uint16_t RCBController_service_uuid = 0xFFF0; |
jksoft | 0:8c643bfe55b7 | 16 | static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; |
jksoft | 1:48f6e08a3ac2 | 17 | uint8_t RCBControllerPayload[10] = {0,}; |
jksoft | 1:48f6e08a3ac2 | 18 | |
jksoft | 1:48f6e08a3ac2 | 19 | GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10, |
jksoft | 0:8c643bfe55b7 | 20 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
jksoft | 0:8c643bfe55b7 | 21 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
jksoft | 1:48f6e08a3ac2 | 22 | GattCharacteristic *ControllerChars[] = {&ControllerChar}; |
jksoft | 1:48f6e08a3ac2 | 23 | GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); |
jksoft | 0:8c643bfe55b7 | 24 | |
jksoft | 0:8c643bfe55b7 | 25 | RCBController controller; |
jksoft | 0:8c643bfe55b7 | 26 | |
jksoft | 1:48f6e08a3ac2 | 27 | void onConnected(uint16_t h) |
jksoft | 0:8c643bfe55b7 | 28 | { |
jksoft | 1:48f6e08a3ac2 | 29 | ConnectStateLed = 0; |
jksoft | 0:8c643bfe55b7 | 30 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 31 | pc.printf("Connected\n\r"); |
jksoft | 0:8c643bfe55b7 | 32 | #endif |
jksoft | 1:48f6e08a3ac2 | 33 | } |
jksoft | 0:8c643bfe55b7 | 34 | |
jksoft | 1:48f6e08a3ac2 | 35 | void onDisconnected(uint16_t h) |
jksoft | 1:48f6e08a3ac2 | 36 | { |
jksoft | 1:48f6e08a3ac2 | 37 | ble.startAdvertising(); |
jksoft | 1:48f6e08a3ac2 | 38 | ConnectStateLed = 1; |
jksoft | 0:8c643bfe55b7 | 39 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 40 | pc.printf("Disconnected\n\r"); |
jksoft | 0:8c643bfe55b7 | 41 | #endif |
jksoft | 1:48f6e08a3ac2 | 42 | } |
jksoft | 1:48f6e08a3ac2 | 43 | |
jksoft | 0:8c643bfe55b7 | 44 | |
jksoft | 0:8c643bfe55b7 | 45 | // GattEvent |
jksoft | 1:48f6e08a3ac2 | 46 | void onDataWritten(uint16_t charHandle) |
jksoft | 0:8c643bfe55b7 | 47 | { |
jksoft | 1:48f6e08a3ac2 | 48 | if (charHandle == ControllerChar.getHandle()) { |
jksoft | 1:48f6e08a3ac2 | 49 | uint16_t bytesRead; |
jksoft | 1:48f6e08a3ac2 | 50 | ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead); |
jksoft | 1:48f6e08a3ac2 | 51 | memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); |
jksoft | 0:8c643bfe55b7 | 52 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 53 | |
jksoft | 1:48f6e08a3ac2 | 54 | pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4], |
jksoft | 0:8c643bfe55b7 | 55 | controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); |
jksoft | 0:8c643bfe55b7 | 56 | #endif |
jksoft | 1:48f6e08a3ac2 | 57 | ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0; |
jksoft | 1:48f6e08a3ac2 | 58 | } |
jksoft | 0:8c643bfe55b7 | 59 | |
jksoft | 1:48f6e08a3ac2 | 60 | } |
jksoft | 0:8c643bfe55b7 | 61 | |
jksoft | 0:8c643bfe55b7 | 62 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 63 | /*! |
jksoft | 0:8c643bfe55b7 | 64 | @brief Program entry point |
jksoft | 0:8c643bfe55b7 | 65 | */ |
jksoft | 0:8c643bfe55b7 | 66 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 67 | int main(void) |
jksoft | 0:8c643bfe55b7 | 68 | { |
jksoft | 0:8c643bfe55b7 | 69 | #if DBG |
jksoft | 0:8c643bfe55b7 | 70 | pc.printf("Start\n\r"); |
jksoft | 0:8c643bfe55b7 | 71 | #endif |
jksoft | 1:48f6e08a3ac2 | 72 | |
jksoft | 2:dd85fdc18224 | 73 | ble.init(); |
jksoft | 1:48f6e08a3ac2 | 74 | ble.onConnection(onConnected); |
jksoft | 1:48f6e08a3ac2 | 75 | ble.onDisconnection(onDisconnected); |
jksoft | 1:48f6e08a3ac2 | 76 | ble.onDataWritten(onDataWritten); |
jksoft | 1:48f6e08a3ac2 | 77 | |
jksoft | 1:48f6e08a3ac2 | 78 | /* setup advertising */ |
jksoft | 1:48f6e08a3ac2 | 79 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jksoft | 1:48f6e08a3ac2 | 80 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jksoft | 1:48f6e08a3ac2 | 81 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
jksoft | 1:48f6e08a3ac2 | 82 | (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1); |
jksoft | 1:48f6e08a3ac2 | 83 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
jksoft | 1:48f6e08a3ac2 | 84 | (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid)); |
jksoft | 0:8c643bfe55b7 | 85 | |
jksoft | 1:48f6e08a3ac2 | 86 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
jksoft | 1:48f6e08a3ac2 | 87 | ble.startAdvertising(); |
jksoft | 0:8c643bfe55b7 | 88 | |
jksoft | 1:48f6e08a3ac2 | 89 | ble.addService(RCBControllerService); |
jksoft | 0:8c643bfe55b7 | 90 | |
jksoft | 1:48f6e08a3ac2 | 91 | while (true) { |
jksoft | 1:48f6e08a3ac2 | 92 | ble.waitForEvent(); |
jksoft | 0:8c643bfe55b7 | 93 | } |
jksoft | 0:8c643bfe55b7 | 94 | } |
jksoft | 0:8c643bfe55b7 | 95 |