![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
試作テストバージョン
Dependencies: BLE_API TB6612FNG mbed
Fork of BLE_RCBController2 by
main.cpp@4:ca2ee032bc5a, 2014-08-29 (annotated)
- Committer:
- jksoft
- Date:
- Fri Aug 29 04:40:46 2014 +0000
- Revision:
- 4:ca2ee032bc5a
- Parent:
- 2:dd85fdc18224
test
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 | 4:ca2ee032bc5a | 4 | #include "TB6612.h" |
jksoft | 0:8c643bfe55b7 | 5 | |
jksoft | 1:48f6e08a3ac2 | 6 | #define DBG 1 |
jksoft | 0:8c643bfe55b7 | 7 | |
jksoft | 1:48f6e08a3ac2 | 8 | BLEDevice ble; |
jksoft | 0:8c643bfe55b7 | 9 | Serial pc(USBTX, USBRX); |
jksoft | 0:8c643bfe55b7 | 10 | /* LEDs for indication: */ |
jksoft | 4:ca2ee032bc5a | 11 | DigitalOut ConnectStateLed(P0_18); |
jksoft | 4:ca2ee032bc5a | 12 | DigitalOut outlow(P0_20); |
jksoft | 4:ca2ee032bc5a | 13 | //PwmOut ControllerStateLed(LED2); |
jksoft | 0:8c643bfe55b7 | 14 | |
jksoft | 4:ca2ee032bc5a | 15 | TB6612 left(P0_29,P0_24,P0_23); |
jksoft | 4:ca2ee032bc5a | 16 | TB6612 right(P0_28,P0_30,P0_0); |
jksoft | 0:8c643bfe55b7 | 17 | |
jksoft | 0:8c643bfe55b7 | 18 | /* RCBController Service */ |
jksoft | 0:8c643bfe55b7 | 19 | static const uint16_t RCBController_service_uuid = 0xFFF0; |
jksoft | 0:8c643bfe55b7 | 20 | static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; |
jksoft | 1:48f6e08a3ac2 | 21 | uint8_t RCBControllerPayload[10] = {0,}; |
jksoft | 1:48f6e08a3ac2 | 22 | |
jksoft | 1:48f6e08a3ac2 | 23 | GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10, |
jksoft | 0:8c643bfe55b7 | 24 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
jksoft | 0:8c643bfe55b7 | 25 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
jksoft | 1:48f6e08a3ac2 | 26 | GattCharacteristic *ControllerChars[] = {&ControllerChar}; |
jksoft | 1:48f6e08a3ac2 | 27 | GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); |
jksoft | 0:8c643bfe55b7 | 28 | |
jksoft | 0:8c643bfe55b7 | 29 | RCBController controller; |
jksoft | 0:8c643bfe55b7 | 30 | |
jksoft | 1:48f6e08a3ac2 | 31 | void onConnected(uint16_t h) |
jksoft | 0:8c643bfe55b7 | 32 | { |
jksoft | 1:48f6e08a3ac2 | 33 | ConnectStateLed = 0; |
jksoft | 0:8c643bfe55b7 | 34 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 35 | pc.printf("Connected\n\r"); |
jksoft | 0:8c643bfe55b7 | 36 | #endif |
jksoft | 1:48f6e08a3ac2 | 37 | } |
jksoft | 0:8c643bfe55b7 | 38 | |
jksoft | 1:48f6e08a3ac2 | 39 | void onDisconnected(uint16_t h) |
jksoft | 1:48f6e08a3ac2 | 40 | { |
jksoft | 1:48f6e08a3ac2 | 41 | ble.startAdvertising(); |
jksoft | 1:48f6e08a3ac2 | 42 | ConnectStateLed = 1; |
jksoft | 0:8c643bfe55b7 | 43 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 44 | pc.printf("Disconnected\n\r"); |
jksoft | 0:8c643bfe55b7 | 45 | #endif |
jksoft | 1:48f6e08a3ac2 | 46 | } |
jksoft | 1:48f6e08a3ac2 | 47 | |
jksoft | 0:8c643bfe55b7 | 48 | |
jksoft | 0:8c643bfe55b7 | 49 | // GattEvent |
jksoft | 1:48f6e08a3ac2 | 50 | void onDataWritten(uint16_t charHandle) |
jksoft | 0:8c643bfe55b7 | 51 | { |
jksoft | 1:48f6e08a3ac2 | 52 | if (charHandle == ControllerChar.getHandle()) { |
jksoft | 1:48f6e08a3ac2 | 53 | uint16_t bytesRead; |
jksoft | 1:48f6e08a3ac2 | 54 | ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead); |
jksoft | 1:48f6e08a3ac2 | 55 | memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); |
jksoft | 0:8c643bfe55b7 | 56 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 57 | |
jksoft | 1:48f6e08a3ac2 | 58 | 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 | 59 | controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); |
jksoft | 0:8c643bfe55b7 | 60 | #endif |
jksoft | 4:ca2ee032bc5a | 61 | left = ((float)((int)controller.status.LeftAnalogUD -128) / 128.0); |
jksoft | 4:ca2ee032bc5a | 62 | right = ((float)((int)controller.status.RightAnalogUD -128) / 128.0); |
jksoft | 4:ca2ee032bc5a | 63 | //ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0; |
jksoft | 1:48f6e08a3ac2 | 64 | } |
jksoft | 0:8c643bfe55b7 | 65 | |
jksoft | 1:48f6e08a3ac2 | 66 | } |
jksoft | 0:8c643bfe55b7 | 67 | |
jksoft | 0:8c643bfe55b7 | 68 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 69 | /*! |
jksoft | 0:8c643bfe55b7 | 70 | @brief Program entry point |
jksoft | 0:8c643bfe55b7 | 71 | */ |
jksoft | 0:8c643bfe55b7 | 72 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 73 | int main(void) |
jksoft | 0:8c643bfe55b7 | 74 | { |
jksoft | 4:ca2ee032bc5a | 75 | pc.baud(921600); |
jksoft | 4:ca2ee032bc5a | 76 | |
jksoft | 4:ca2ee032bc5a | 77 | |
jksoft | 4:ca2ee032bc5a | 78 | ConnectStateLed = 1; |
jksoft | 0:8c643bfe55b7 | 79 | #if DBG |
jksoft | 0:8c643bfe55b7 | 80 | pc.printf("Start\n\r"); |
jksoft | 0:8c643bfe55b7 | 81 | #endif |
jksoft | 4:ca2ee032bc5a | 82 | outlow = 0; |
jksoft | 1:48f6e08a3ac2 | 83 | |
jksoft | 2:dd85fdc18224 | 84 | ble.init(); |
jksoft | 1:48f6e08a3ac2 | 85 | ble.onConnection(onConnected); |
jksoft | 1:48f6e08a3ac2 | 86 | ble.onDisconnection(onDisconnected); |
jksoft | 1:48f6e08a3ac2 | 87 | ble.onDataWritten(onDataWritten); |
jksoft | 1:48f6e08a3ac2 | 88 | |
jksoft | 1:48f6e08a3ac2 | 89 | /* setup advertising */ |
jksoft | 1:48f6e08a3ac2 | 90 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jksoft | 1:48f6e08a3ac2 | 91 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jksoft | 1:48f6e08a3ac2 | 92 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
jksoft | 4:ca2ee032bc5a | 93 | (const uint8_t *)"mbed WallbotBLE", sizeof("mbed WallbotBLE") - 1); |
jksoft | 1:48f6e08a3ac2 | 94 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
jksoft | 1:48f6e08a3ac2 | 95 | (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid)); |
jksoft | 0:8c643bfe55b7 | 96 | |
jksoft | 1:48f6e08a3ac2 | 97 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
jksoft | 1:48f6e08a3ac2 | 98 | ble.startAdvertising(); |
jksoft | 0:8c643bfe55b7 | 99 | |
jksoft | 1:48f6e08a3ac2 | 100 | ble.addService(RCBControllerService); |
jksoft | 4:ca2ee032bc5a | 101 | |
jksoft | 0:8c643bfe55b7 | 102 | |
jksoft | 1:48f6e08a3ac2 | 103 | while (true) { |
jksoft | 1:48f6e08a3ac2 | 104 | ble.waitForEvent(); |
jksoft | 0:8c643bfe55b7 | 105 | } |
jksoft | 0:8c643bfe55b7 | 106 | } |
jksoft | 0:8c643bfe55b7 | 107 |