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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *      December 31st, 2015     Modified by Kenji Arai
00003  *      January  19th, 2016
00004  *      October  12th, 2017     !! Run on Mbed-os5
00005  *                              http://www.page.sannet.ne.jp/kenjia/index.html
00006  *                              http://mbed.org/users/kenjiArai/
00007  *      -> Works well with latest Rev.
00008  *
00009  *      Original:
00010  *          BLE_RCBController2_Motor
00011  *          https://developer.mbed.org/users/robo8080/code/BLE_RCBController2_Motor/
00012  *      Tested Controller Device:
00013  *          iPhone6 RCBController (Ver1.4.0)
00014  *          https://itunes.apple.com/jp/app/rcbcontroller/id689724127?mt=8
00015  */
00016 
00017 //  Include ---------------------------------------------------------------------------------------
00018 #include "mbed.h"
00019 #include "ble/BLE.h"
00020 #include "RCBController.h"
00021 #include "Servo.h"
00022 
00023 //  Definition ------------------------------------------------------------------------------------
00024 #define NEED_CONSOLE_OUTPUT         1       // Set this if you need debug messages on the console
00025 
00026 #if NEED_CONSOLE_OUTPUT
00027 #define DEBUG(...) { printf(__VA_ARGS__); }
00028 #else
00029 #define DEBUG(...)
00030 #endif
00031 
00032 #define LED_A_PIN                   P0_21
00033 #define LED_B_PIN                   P0_22
00034 #define LED_X_PIN                   P0_23
00035 #define LED_Y_PIN                   P0_24
00036 #define SERVO_0_PIN                 P0_18
00037 #define SERVO_1_PIN                 P0_16
00038 
00039 //  Object ----------------------------------------------------------------------------------------
00040 BLE             ble;
00041 Servo           SERVO_0(SERVO_0_PIN);
00042 Servo           SERVO_1(SERVO_1_PIN);
00043 DigitalOut      LED_A(LED_A_PIN);
00044 DigitalOut      LED_B(LED_B_PIN);
00045 DigitalOut      LED_X(LED_X_PIN);
00046 DigitalOut      LED_Y(LED_Y_PIN);
00047 
00048 //  RAM -------------------------------------------------------------------------------------------
00049 uint8_t RCBControllerPayload[10] = {0,};
00050 RCBController       controller;
00051 
00052 //  ROM / Constant data ---------------------------------------------------------------------------
00053 // RCBController Service
00054 static const uint16_t RCBController_service_uuid = 0xFFF0;
00055 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
00056 const char *deviceName = "mbedBLE";
00057 
00058 //  Function prototypes ---------------------------------------------------------------------------
00059 GattCharacteristic  ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
00060                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00061                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00062 GattCharacteristic *ControllerChars[] = {&ControllerChar};
00063 GattService         RCBControllerService(RCBController_service_uuid, ControllerChars,
00064                             sizeof(ControllerChars) / sizeof(GattCharacteristic *));
00065 
00066 //-------------------------------------------------------------------------------------------------
00067 //  Control Program
00068 //-------------------------------------------------------------------------------------------------
00069 void data_analysis(void){
00070     static uint8_t cont_flg_A = 0;
00071     static uint8_t cont_flg_B = 0;
00072     static uint8_t cont_flg_X = 0;
00073     static uint8_t cont_flg_Y = 0;
00074     float value;
00075 
00076     if (controller.status.A == 1){
00077         if (cont_flg_A == 0){
00078             LED_A = !LED_A;
00079             cont_flg_A = 1;
00080         }
00081     } else {
00082         cont_flg_A = 0;
00083     }
00084     if (controller.status.B == 1){
00085         if (cont_flg_B == 0){
00086             LED_B = !LED_B;
00087             cont_flg_B = 1;
00088         }
00089     } else {
00090         cont_flg_B = 0;
00091     }
00092     if (controller.status.X == 1){
00093         if (cont_flg_X == 0){
00094             LED_X = !LED_X;
00095             cont_flg_X = 1;
00096         }
00097     } else {
00098         cont_flg_X = 0;
00099     }
00100     if (controller.status.Y == 1){
00101         if (cont_flg_Y == 0){
00102             LED_Y = !LED_Y;
00103             cont_flg_Y = 1;
00104         }
00105     } else {
00106         cont_flg_Y = 0;
00107     }
00108     value = (float)controller.status.LeftAnalogUD/255 * 150.0f + 20;
00109     SERVO_0.write(value);
00110     value = (float)controller.status.LeftAnalogLR/255 * 150.0f + 20;
00111     SERVO_1.write(value);   
00112 }
00113 
00114 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
00115     DEBUG("Disconnected!\r\n");
00116     DEBUG("Restarting the advertising process\r\n");
00117     ble.startAdvertising();
00118 }
00119 
00120 void onDataWritten(const GattWriteCallbackParams *params){
00121     if (params->handle == ControllerChar.getValueAttribute().getHandle()) {
00122         uint16_t bytesRead;
00123         ble.readCharacteristicValue(ControllerChar.getValueAttribute().getHandle(),
00124                                     RCBControllerPayload, &bytesRead);
00125         memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
00126         DEBUG("DATA:0x%02x 0x%02x %d %d %d %d %d %d %d 0x%02x\r\n",
00127                 controller.data[0],controller.data[1],controller.data[2],controller.data[3],
00128                 controller.data[4],controller.data[5],controller.data[6],controller.data[7],
00129                 controller.data[8],controller.data[9]);
00130         data_analysis();    
00131     }
00132 }
00133 
00134 int main(void){
00135     DEBUG("\r\nInitialising the nRF51822\r\n");
00136     ble.init();
00137     ble.setDeviceName((const uint8_t *)deviceName);
00138     ble.onDisconnection(disconnectionCallback);
00139     ble.onDataWritten(onDataWritten);
00140     DEBUG("Start RCB Controller\r\n");
00141     /* setup advertising */
00142     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00143     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00144     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00145                                     (const uint8_t *)deviceName, strlen(deviceName));
00146     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
00147                                     (const uint8_t *)RCBController_service_uuid,
00148                                      sizeof(RCBController_service_uuid));
00149     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
00150     ble.startAdvertising();
00151     ble.addService(RCBControllerService);
00152     while (true) {
00153         ble.waitForEvent();
00154     }
00155 }