TYBLE16 on os5 sample programs
Dependencies: BME280 TextLCD nRF51_Vdd
Fork of TYBLE16_mbedlized_os5_BASE by
Please refer following notebook.
/users/kenjiArai/notebook/tyble16-module-as-mbed-os-5-board-mbedlization/
4_RCBController/main.cpp@3:c0010c8ad17f, 2018-04-15 (annotated)
- Committer:
- kenjiArai
- Date:
- Sun Apr 15 04:15:18 2018 +0000
- Revision:
- 3:c0010c8ad17f
- Parent:
- 1:9011c83e4178
Separated Uart_Clinent & Uart_Server due to memory overflow
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9011c83e4178 | 1 | /* |
kenjiArai | 1:9011c83e4178 | 2 | * December 31st, 2015 Modified by Kenji Arai |
kenjiArai | 1:9011c83e4178 | 3 | * January 19th, 2016 |
kenjiArai | 1:9011c83e4178 | 4 | * October 12th, 2017 !! Run on Mbed-os5 |
kenjiArai | 1:9011c83e4178 | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 1:9011c83e4178 | 6 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 1:9011c83e4178 | 7 | * April 14th, 2018 run on TYBLE16 |
kenjiArai | 1:9011c83e4178 | 8 | * |
kenjiArai | 1:9011c83e4178 | 9 | * Original: |
kenjiArai | 1:9011c83e4178 | 10 | * BLE_RCBController2_Motor |
kenjiArai | 1:9011c83e4178 | 11 | * https://developer.mbed.org/users/robo8080/code/BLE_RCBController2_Motor/ |
kenjiArai | 1:9011c83e4178 | 12 | * Tested Controller Device: |
kenjiArai | 1:9011c83e4178 | 13 | * iPhone6 RCBController (Ver1.4.0) |
kenjiArai | 1:9011c83e4178 | 14 | * https://itunes.apple.com/jp/app/rcbcontroller/id689724127?mt=8 |
kenjiArai | 1:9011c83e4178 | 15 | */ |
kenjiArai | 1:9011c83e4178 | 16 | |
kenjiArai | 1:9011c83e4178 | 17 | //#define EXAMPLE_4_RCB_CONTROLLER |
kenjiArai | 1:9011c83e4178 | 18 | #ifdef EXAMPLE_4_RCB_CONTROLLER |
kenjiArai | 1:9011c83e4178 | 19 | |
kenjiArai | 1:9011c83e4178 | 20 | #include "mbed.h" |
kenjiArai | 1:9011c83e4178 | 21 | #include "BLE.h" |
kenjiArai | 1:9011c83e4178 | 22 | #include "RCBController.h" |
kenjiArai | 1:9011c83e4178 | 23 | #include "TYBLE16_BASE.h" |
kenjiArai | 1:9011c83e4178 | 24 | |
kenjiArai | 1:9011c83e4178 | 25 | #define NEED_CONSOLE_OUTPUT 1 // Keep 1 |
kenjiArai | 1:9011c83e4178 | 26 | |
kenjiArai | 1:9011c83e4178 | 27 | #if NEED_CONSOLE_OUTPUT |
kenjiArai | 1:9011c83e4178 | 28 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
kenjiArai | 1:9011c83e4178 | 29 | #else |
kenjiArai | 1:9011c83e4178 | 30 | #define DEBUG(...) |
kenjiArai | 1:9011c83e4178 | 31 | #endif |
kenjiArai | 1:9011c83e4178 | 32 | |
kenjiArai | 1:9011c83e4178 | 33 | BLE ble_rcb; |
kenjiArai | 1:9011c83e4178 | 34 | Serial pc(USBTX, USBRX); |
kenjiArai | 1:9011c83e4178 | 35 | |
kenjiArai | 1:9011c83e4178 | 36 | uint8_t RCBControllerPayload[10] = {0,}; |
kenjiArai | 1:9011c83e4178 | 37 | RCBController controller; |
kenjiArai | 1:9011c83e4178 | 38 | |
kenjiArai | 1:9011c83e4178 | 39 | // RCBController Service |
kenjiArai | 1:9011c83e4178 | 40 | static const uint16_t RCBController_service_uuid = 0xFFF0; |
kenjiArai | 1:9011c83e4178 | 41 | static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; |
kenjiArai | 1:9011c83e4178 | 42 | const char *deviceName = "Mbed-BLE"; |
kenjiArai | 1:9011c83e4178 | 43 | char *const opngmsg = |
kenjiArai | 1:9011c83e4178 | 44 | "\x1b[2J\x1b[H"__FILE__ "\r\n"__DATE__ " " __TIME__ " (UTC)\r\n""\r\n"; |
kenjiArai | 1:9011c83e4178 | 45 | |
kenjiArai | 1:9011c83e4178 | 46 | GattCharacteristic ControllerChar ( |
kenjiArai | 1:9011c83e4178 | 47 | RCBController_Characteristic_uuid,RCBControllerPayload,10, 10, |
kenjiArai | 1:9011c83e4178 | 48 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
kenjiArai | 1:9011c83e4178 | 49 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
kenjiArai | 1:9011c83e4178 | 50 | ); |
kenjiArai | 1:9011c83e4178 | 51 | GattCharacteristic *ControllerChars[] = {&ControllerChar}; |
kenjiArai | 1:9011c83e4178 | 52 | GattService RCBControllerService( |
kenjiArai | 1:9011c83e4178 | 53 | RCBController_service_uuid, |
kenjiArai | 1:9011c83e4178 | 54 | ControllerChars, |
kenjiArai | 1:9011c83e4178 | 55 | sizeof(ControllerChars) / sizeof(GattCharacteristic *) |
kenjiArai | 1:9011c83e4178 | 56 | ); |
kenjiArai | 1:9011c83e4178 | 57 | |
kenjiArai | 1:9011c83e4178 | 58 | //------------------------------------------------------------------------------ |
kenjiArai | 1:9011c83e4178 | 59 | // Control Program |
kenjiArai | 1:9011c83e4178 | 60 | //------------------------------------------------------------------------------ |
kenjiArai | 1:9011c83e4178 | 61 | void data_analysis(void) |
kenjiArai | 1:9011c83e4178 | 62 | { |
kenjiArai | 1:9011c83e4178 | 63 | static uint8_t cont_flg_A = 0; |
kenjiArai | 1:9011c83e4178 | 64 | static uint8_t cont_flg_B = 0; |
kenjiArai | 1:9011c83e4178 | 65 | static uint8_t cont_flg_X = 0; |
kenjiArai | 1:9011c83e4178 | 66 | static uint8_t cont_flg_Y = 0; |
kenjiArai | 1:9011c83e4178 | 67 | |
kenjiArai | 1:9011c83e4178 | 68 | if (controller.status.A == 1) { |
kenjiArai | 1:9011c83e4178 | 69 | if (cont_flg_A == 0) { |
kenjiArai | 1:9011c83e4178 | 70 | cont_flg_A = 1; |
kenjiArai | 1:9011c83e4178 | 71 | } |
kenjiArai | 1:9011c83e4178 | 72 | } else { |
kenjiArai | 1:9011c83e4178 | 73 | cont_flg_A = 0; |
kenjiArai | 1:9011c83e4178 | 74 | } |
kenjiArai | 1:9011c83e4178 | 75 | if (controller.status.B == 1) { |
kenjiArai | 1:9011c83e4178 | 76 | if (cont_flg_B == 0) { |
kenjiArai | 1:9011c83e4178 | 77 | cont_flg_B = 1; |
kenjiArai | 1:9011c83e4178 | 78 | } |
kenjiArai | 1:9011c83e4178 | 79 | } else { |
kenjiArai | 1:9011c83e4178 | 80 | cont_flg_B = 0; |
kenjiArai | 1:9011c83e4178 | 81 | } |
kenjiArai | 1:9011c83e4178 | 82 | if (controller.status.X == 1) { |
kenjiArai | 1:9011c83e4178 | 83 | if (cont_flg_X == 0) { |
kenjiArai | 1:9011c83e4178 | 84 | cont_flg_X = 1; |
kenjiArai | 1:9011c83e4178 | 85 | } |
kenjiArai | 1:9011c83e4178 | 86 | } else { |
kenjiArai | 1:9011c83e4178 | 87 | cont_flg_X = 0; |
kenjiArai | 1:9011c83e4178 | 88 | } |
kenjiArai | 1:9011c83e4178 | 89 | if (controller.status.Y == 1) { |
kenjiArai | 1:9011c83e4178 | 90 | if (cont_flg_Y == 0) { |
kenjiArai | 1:9011c83e4178 | 91 | cont_flg_Y = 1; |
kenjiArai | 1:9011c83e4178 | 92 | } |
kenjiArai | 1:9011c83e4178 | 93 | } else { |
kenjiArai | 1:9011c83e4178 | 94 | cont_flg_Y = 0; |
kenjiArai | 1:9011c83e4178 | 95 | } |
kenjiArai | 1:9011c83e4178 | 96 | } |
kenjiArai | 1:9011c83e4178 | 97 | |
kenjiArai | 1:9011c83e4178 | 98 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
kenjiArai | 1:9011c83e4178 | 99 | { |
kenjiArai | 1:9011c83e4178 | 100 | DEBUG("Disconnected!\r\n"); |
kenjiArai | 1:9011c83e4178 | 101 | DEBUG("Restarting the advertising process\r\n"); |
kenjiArai | 1:9011c83e4178 | 102 | ble_rcb.startAdvertising(); |
kenjiArai | 1:9011c83e4178 | 103 | } |
kenjiArai | 1:9011c83e4178 | 104 | |
kenjiArai | 1:9011c83e4178 | 105 | void onDataWritten(const GattWriteCallbackParams *params) |
kenjiArai | 1:9011c83e4178 | 106 | { |
kenjiArai | 1:9011c83e4178 | 107 | if (params->handle == ControllerChar.getValueAttribute().getHandle()) { |
kenjiArai | 1:9011c83e4178 | 108 | uint16_t bytesRead; |
kenjiArai | 1:9011c83e4178 | 109 | ble_rcb.readCharacteristicValue( |
kenjiArai | 1:9011c83e4178 | 110 | ControllerChar.getValueAttribute().getHandle(), |
kenjiArai | 1:9011c83e4178 | 111 | RCBControllerPayload, &bytesRead |
kenjiArai | 1:9011c83e4178 | 112 | ); |
kenjiArai | 1:9011c83e4178 | 113 | memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); |
kenjiArai | 1:9011c83e4178 | 114 | DEBUG("DATA:0x%02x 0x%02x %d %d %d %d %d %d %d 0x%02x\r\n", |
kenjiArai | 1:9011c83e4178 | 115 | controller.data[0],controller.data[1], |
kenjiArai | 1:9011c83e4178 | 116 | controller.data[2],controller.data[3], |
kenjiArai | 1:9011c83e4178 | 117 | controller.data[4],controller.data[5], |
kenjiArai | 1:9011c83e4178 | 118 | controller.data[6],controller.data[7], |
kenjiArai | 1:9011c83e4178 | 119 | controller.data[8],controller.data[9] |
kenjiArai | 1:9011c83e4178 | 120 | ); |
kenjiArai | 1:9011c83e4178 | 121 | data_analysis(); |
kenjiArai | 1:9011c83e4178 | 122 | } |
kenjiArai | 1:9011c83e4178 | 123 | } |
kenjiArai | 1:9011c83e4178 | 124 | |
kenjiArai | 1:9011c83e4178 | 125 | int main(void) |
kenjiArai | 1:9011c83e4178 | 126 | { |
kenjiArai | 1:9011c83e4178 | 127 | DEBUG(opngmsg); |
kenjiArai | 1:9011c83e4178 | 128 | DEBUG("\r\nInitialising the nRF51822\r\n"); |
kenjiArai | 1:9011c83e4178 | 129 | // Check TYBLE-16 configuration |
kenjiArai | 1:9011c83e4178 | 130 | cpu_sys(); |
kenjiArai | 1:9011c83e4178 | 131 | compile_condition(); |
kenjiArai | 1:9011c83e4178 | 132 | |
kenjiArai | 1:9011c83e4178 | 133 | ble_rcb.init(); |
kenjiArai | 1:9011c83e4178 | 134 | ble_rcb.setDeviceName((const uint8_t *)deviceName); |
kenjiArai | 1:9011c83e4178 | 135 | ble_rcb.onDisconnection(disconnectionCallback); |
kenjiArai | 1:9011c83e4178 | 136 | ble_rcb.onDataWritten(onDataWritten); |
kenjiArai | 1:9011c83e4178 | 137 | DEBUG("Start RCB Controller\r\n"); |
kenjiArai | 1:9011c83e4178 | 138 | /* setup advertising */ |
kenjiArai | 1:9011c83e4178 | 139 | ble_rcb.accumulateAdvertisingPayload( |
kenjiArai | 1:9011c83e4178 | 140 | GapAdvertisingData::BREDR_NOT_SUPPORTED |
kenjiArai | 1:9011c83e4178 | 141 | ); |
kenjiArai | 1:9011c83e4178 | 142 | ble_rcb.setAdvertisingType( |
kenjiArai | 1:9011c83e4178 | 143 | GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED |
kenjiArai | 1:9011c83e4178 | 144 | ); |
kenjiArai | 1:9011c83e4178 | 145 | ble_rcb.accumulateAdvertisingPayload( |
kenjiArai | 1:9011c83e4178 | 146 | GapAdvertisingData::SHORTENED_LOCAL_NAME, |
kenjiArai | 1:9011c83e4178 | 147 | (const uint8_t *)deviceName, strlen(deviceName) |
kenjiArai | 1:9011c83e4178 | 148 | ); |
kenjiArai | 1:9011c83e4178 | 149 | ble_rcb.accumulateAdvertisingPayload( |
kenjiArai | 1:9011c83e4178 | 150 | GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
kenjiArai | 1:9011c83e4178 | 151 | (const uint8_t *)RCBController_service_uuid, |
kenjiArai | 1:9011c83e4178 | 152 | sizeof(RCBController_service_uuid) |
kenjiArai | 1:9011c83e4178 | 153 | ); |
kenjiArai | 1:9011c83e4178 | 154 | ble_rcb.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
kenjiArai | 1:9011c83e4178 | 155 | ble_rcb.startAdvertising(); |
kenjiArai | 1:9011c83e4178 | 156 | ble_rcb.addService(RCBControllerService); |
kenjiArai | 1:9011c83e4178 | 157 | while (true) { |
kenjiArai | 1:9011c83e4178 | 158 | ble_rcb.waitForEvent(); |
kenjiArai | 1:9011c83e4178 | 159 | } |
kenjiArai | 1:9011c83e4178 | 160 | } |
kenjiArai | 1:9011c83e4178 | 161 | |
kenjiArai | 1:9011c83e4178 | 162 | #endif |