nRF51822 BLE control program using RCBController(iOS9). Control one LED(on/off), one LED(duty/PWM) and Servo Motor. Compile with latest (as of today) Lib's.

Fork of BLE_RCBController2_Motor by robo 8080

Original software by jksoft-san. -> /users/jksoft/code/BLE_RCBController2/

Additional functions by robo8080-san. -> /users/robo8080/code/BLE_RCBController2_Motor/

Small modification under latest lib's at this moment (Jan. 1st, 2016)

Hardware: RedBearLab nRF51822 -> /platforms/RedBearLab-nRF51822/

Committer:
kenjiArai
Date:
Wed Oct 11 21:32:18 2017 +0000
Revision:
8:7ab6a5b29812
Parent:
7:45655ac58160
changed comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 6:1aff9f05d487 1 /*
kenjiArai 6:1aff9f05d487 2 * December 31st, 2015 Modified by Kenji Arai
kenjiArai 7:45655ac58160 3 * January 19th, 2016
kenjiArai 8:7ab6a5b29812 4 * October 12th, 2017 !! Run on Mbed-os5
kenjiArai 6:1aff9f05d487 5 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 6:1aff9f05d487 6 * http://mbed.org/users/kenjiArai/
kenjiArai 6:1aff9f05d487 7 * -> Works well with latest Rev.
kenjiArai 6:1aff9f05d487 8 *
kenjiArai 6:1aff9f05d487 9 * Original:
kenjiArai 6:1aff9f05d487 10 * BLE_RCBController2_Motor
kenjiArai 6:1aff9f05d487 11 * https://developer.mbed.org/users/robo8080/code/BLE_RCBController2_Motor/
kenjiArai 6:1aff9f05d487 12 * Tested Controller Device:
kenjiArai 6:1aff9f05d487 13 * iPhone6 RCBController (Ver1.4.0)
kenjiArai 6:1aff9f05d487 14 * https://itunes.apple.com/jp/app/rcbcontroller/id689724127?mt=8
kenjiArai 6:1aff9f05d487 15 */
jksoft 0:8c643bfe55b7 16
kenjiArai 6:1aff9f05d487 17 // Include ---------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 18 #include "mbed.h"
kenjiArai 6:1aff9f05d487 19 #include "ble/BLE.h"
kenjiArai 6:1aff9f05d487 20 #include "RCBController.h"
kenjiArai 6:1aff9f05d487 21 #include "Servo.h"
kenjiArai 6:1aff9f05d487 22
kenjiArai 6:1aff9f05d487 23 // Definition ------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 24 #define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console
jksoft 0:8c643bfe55b7 25
kenjiArai 6:1aff9f05d487 26 #if NEED_CONSOLE_OUTPUT
kenjiArai 6:1aff9f05d487 27 #define DEBUG(...) { printf(__VA_ARGS__); }
kenjiArai 6:1aff9f05d487 28 #else
kenjiArai 6:1aff9f05d487 29 #define DEBUG(...)
kenjiArai 6:1aff9f05d487 30 #endif
kenjiArai 6:1aff9f05d487 31
kenjiArai 7:45655ac58160 32 #define LED_A_PIN P0_21
kenjiArai 7:45655ac58160 33 #define LED_B_PIN P0_22
kenjiArai 7:45655ac58160 34 #define LED_X_PIN P0_23
kenjiArai 7:45655ac58160 35 #define LED_Y_PIN P0_24
kenjiArai 7:45655ac58160 36 #define SERVO_0_PIN P0_18
kenjiArai 7:45655ac58160 37 #define SERVO_1_PIN P0_16
robo8080 5:1c04bd9f8457 38
kenjiArai 6:1aff9f05d487 39 // Object ----------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 40 BLE ble;
kenjiArai 6:1aff9f05d487 41 Servo SERVO_0(SERVO_0_PIN);
kenjiArai 7:45655ac58160 42 Servo SERVO_1(SERVO_1_PIN);
kenjiArai 7:45655ac58160 43 DigitalOut LED_A(LED_A_PIN);
kenjiArai 7:45655ac58160 44 DigitalOut LED_B(LED_B_PIN);
kenjiArai 7:45655ac58160 45 DigitalOut LED_X(LED_X_PIN);
kenjiArai 7:45655ac58160 46 DigitalOut LED_Y(LED_Y_PIN);
kenjiArai 6:1aff9f05d487 47
kenjiArai 6:1aff9f05d487 48 // RAM -------------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 49 uint8_t RCBControllerPayload[10] = {0,};
kenjiArai 6:1aff9f05d487 50 RCBController controller;
kenjiArai 6:1aff9f05d487 51
kenjiArai 6:1aff9f05d487 52 // ROM / Constant data ---------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 53 // RCBController Service
kenjiArai 6:1aff9f05d487 54 static const uint16_t RCBController_service_uuid = 0xFFF0;
kenjiArai 6:1aff9f05d487 55 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
kenjiArai 6:1aff9f05d487 56 const char *deviceName = "mbedBLE";
robo8080 5:1c04bd9f8457 57
kenjiArai 6:1aff9f05d487 58 // Function prototypes ---------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 59 GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
kenjiArai 6:1aff9f05d487 60 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
kenjiArai 6:1aff9f05d487 61 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
kenjiArai 6:1aff9f05d487 62 GattCharacteristic *ControllerChars[] = {&ControllerChar};
kenjiArai 6:1aff9f05d487 63 GattService RCBControllerService(RCBController_service_uuid, ControllerChars,
kenjiArai 6:1aff9f05d487 64 sizeof(ControllerChars) / sizeof(GattCharacteristic *));
kenjiArai 6:1aff9f05d487 65
kenjiArai 6:1aff9f05d487 66 //-------------------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 67 // Control Program
kenjiArai 6:1aff9f05d487 68 //-------------------------------------------------------------------------------------------------
kenjiArai 6:1aff9f05d487 69 void data_analysis(void){
kenjiArai 7:45655ac58160 70 static uint8_t cont_flg_A = 0;
kenjiArai 7:45655ac58160 71 static uint8_t cont_flg_B = 0;
kenjiArai 7:45655ac58160 72 static uint8_t cont_flg_X = 0;
kenjiArai 7:45655ac58160 73 static uint8_t cont_flg_Y = 0;
kenjiArai 6:1aff9f05d487 74 float value;
kenjiArai 6:1aff9f05d487 75
kenjiArai 7:45655ac58160 76 if (controller.status.A == 1){
kenjiArai 7:45655ac58160 77 if (cont_flg_A == 0){
kenjiArai 7:45655ac58160 78 LED_A = !LED_A;
kenjiArai 7:45655ac58160 79 cont_flg_A = 1;
kenjiArai 7:45655ac58160 80 }
kenjiArai 7:45655ac58160 81 } else {
kenjiArai 7:45655ac58160 82 cont_flg_A = 0;
kenjiArai 7:45655ac58160 83 }
kenjiArai 6:1aff9f05d487 84 if (controller.status.B == 1){
kenjiArai 7:45655ac58160 85 if (cont_flg_B == 0){
kenjiArai 7:45655ac58160 86 LED_B = !LED_B;
kenjiArai 7:45655ac58160 87 cont_flg_B = 1;
kenjiArai 6:1aff9f05d487 88 }
kenjiArai 6:1aff9f05d487 89 } else {
kenjiArai 7:45655ac58160 90 cont_flg_B = 0;
kenjiArai 7:45655ac58160 91 }
kenjiArai 7:45655ac58160 92 if (controller.status.X == 1){
kenjiArai 7:45655ac58160 93 if (cont_flg_X == 0){
kenjiArai 7:45655ac58160 94 LED_X = !LED_X;
kenjiArai 7:45655ac58160 95 cont_flg_X = 1;
kenjiArai 7:45655ac58160 96 }
kenjiArai 7:45655ac58160 97 } else {
kenjiArai 7:45655ac58160 98 cont_flg_X = 0;
kenjiArai 6:1aff9f05d487 99 }
kenjiArai 7:45655ac58160 100 if (controller.status.Y == 1){
kenjiArai 7:45655ac58160 101 if (cont_flg_Y == 0){
kenjiArai 7:45655ac58160 102 LED_Y = !LED_Y;
kenjiArai 7:45655ac58160 103 cont_flg_Y = 1;
kenjiArai 7:45655ac58160 104 }
kenjiArai 7:45655ac58160 105 } else {
kenjiArai 7:45655ac58160 106 cont_flg_Y = 0;
kenjiArai 7:45655ac58160 107 }
kenjiArai 6:1aff9f05d487 108 value = (float)controller.status.LeftAnalogUD/255 * 150.0f + 20;
kenjiArai 7:45655ac58160 109 SERVO_0.write(value);
kenjiArai 7:45655ac58160 110 value = (float)controller.status.LeftAnalogLR/255 * 150.0f + 20;
kenjiArai 7:45655ac58160 111 SERVO_1.write(value);
robo8080 5:1c04bd9f8457 112 }
robo8080 5:1c04bd9f8457 113
kenjiArai 6:1aff9f05d487 114 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
kenjiArai 6:1aff9f05d487 115 DEBUG("Disconnected!\r\n");
kenjiArai 6:1aff9f05d487 116 DEBUG("Restarting the advertising process\r\n");
kenjiArai 6:1aff9f05d487 117 ble.startAdvertising();
robo8080 5:1c04bd9f8457 118 }
jksoft 0:8c643bfe55b7 119
kenjiArai 6:1aff9f05d487 120 void onDataWritten(const GattWriteCallbackParams *params){
kenjiArai 6:1aff9f05d487 121 if (params->handle == ControllerChar.getValueAttribute().getHandle()) {
kenjiArai 6:1aff9f05d487 122 uint16_t bytesRead;
kenjiArai 6:1aff9f05d487 123 ble.readCharacteristicValue(ControllerChar.getValueAttribute().getHandle(),
kenjiArai 6:1aff9f05d487 124 RCBControllerPayload, &bytesRead);
kenjiArai 6:1aff9f05d487 125 memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
kenjiArai 6:1aff9f05d487 126 DEBUG("DATA:0x%02x 0x%02x %d %d %d %d %d %d %d 0x%02x\r\n",
kenjiArai 6:1aff9f05d487 127 controller.data[0],controller.data[1],controller.data[2],controller.data[3],
kenjiArai 6:1aff9f05d487 128 controller.data[4],controller.data[5],controller.data[6],controller.data[7],
kenjiArai 6:1aff9f05d487 129 controller.data[8],controller.data[9]);
kenjiArai 6:1aff9f05d487 130 data_analysis();
kenjiArai 6:1aff9f05d487 131 }
jksoft 1:48f6e08a3ac2 132 }
jksoft 1:48f6e08a3ac2 133
kenjiArai 6:1aff9f05d487 134 int main(void){
kenjiArai 6:1aff9f05d487 135 DEBUG("\r\nInitialising the nRF51822\r\n");
kenjiArai 6:1aff9f05d487 136 ble.init();
kenjiArai 6:1aff9f05d487 137 ble.setDeviceName((const uint8_t *)deviceName);
kenjiArai 6:1aff9f05d487 138 ble.onDisconnection(disconnectionCallback);
jksoft 1:48f6e08a3ac2 139 ble.onDataWritten(onDataWritten);
kenjiArai 6:1aff9f05d487 140 DEBUG("Start RCB Controller\r\n");
jksoft 1:48f6e08a3ac2 141 /* setup advertising */
jksoft 1:48f6e08a3ac2 142 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 1:48f6e08a3ac2 143 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jksoft 1:48f6e08a3ac2 144 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
kenjiArai 6:1aff9f05d487 145 (const uint8_t *)deviceName, strlen(deviceName));
jksoft 1:48f6e08a3ac2 146 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
kenjiArai 7:45655ac58160 147 (const uint8_t *)RCBController_service_uuid,
kenjiArai 7:45655ac58160 148 sizeof(RCBController_service_uuid));
jksoft 1:48f6e08a3ac2 149 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
jksoft 1:48f6e08a3ac2 150 ble.startAdvertising();
jksoft 1:48f6e08a3ac2 151 ble.addService(RCBControllerService);
jksoft 1:48f6e08a3ac2 152 while (true) {
jksoft 1:48f6e08a3ac2 153 ble.waitForEvent();
jksoft 0:8c643bfe55b7 154 }
jksoft 0:8c643bfe55b7 155 }