SimpleControls works with the BLEController iOS/Android App. The mbed's pin can acts as DIGITAL_IN, DIGITAL_OUT, PWM, SERVO, ANALOG_IN. The sketch is to show you how to control the pin as one of the abilities.

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 #include "mbed.h"
00021 #include "ble/BLE.h"
00022 #include "Servo.h"
00023 
00024 
00025 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
00026 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
00027 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
00028 
00029 #define TXRX_BUF_LEN                     20
00030 
00031 #define DIGITAL_OUT_PIN                  P0_9       //TXD
00032 #define DIGITAL_IN_PIN                   P0_10      //CTS
00033 #define PWM_PIN                          P0_11      //RXD
00034 #define SERVO_PIN                        P0_8       //RTS
00035 #define ANALOG_IN_PIN                    P0_4       //P04
00036 
00037 BLE             ble;
00038 
00039 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
00040 DigitalIn       BUTTON(DIGITAL_IN_PIN);
00041 PwmOut          PWM(PWM_PIN);
00042 AnalogIn        ANALOG(ANALOG_IN_PIN);
00043 Servo           MYSERVO(SERVO_PIN);
00044 
00045 //Serial pc(USBTX, USBRX);
00046 
00047 static uint8_t analog_enabled = 0;
00048 static uint8_t old_state = 0;
00049 
00050 // The Nordic UART Service
00051 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00052 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00053 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00054 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
00055 
00056 
00057 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00058 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00059 
00060 //static uint8_t rx_buf[TXRX_BUF_LEN];
00061 //static uint8_t rx_len=0;
00062 
00063 
00064 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00065                                       
00066 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00067                                       
00068 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00069 
00070 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00071 
00072 
00073 
00074 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00075 {
00076     //pc.printf("Disconnected \r\n");
00077     //pc.printf("Restart advertising \r\n");
00078     ble.gap().startAdvertising();
00079 }
00080 
00081 void WrittenHandler(const GattWriteCallbackParams *Handler)
00082 {   
00083     uint8_t buf[TXRX_BUF_LEN];
00084     uint16_t bytesRead;
00085     
00086     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00087     {
00088         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00089         memset(txPayload, 0, TXRX_BUF_LEN);
00090         memcpy(txPayload, buf, TXRX_BUF_LEN);       
00091         
00092         //for(index=0; index<bytesRead; index++)
00093             //pc.putc(buf[index]);
00094             
00095         if(buf[0] == 0x01)
00096         {
00097             if(buf[1] == 0x01)
00098                 LED_SET = 1;
00099             else
00100                 LED_SET = 0;    
00101         }
00102         else if(buf[0] == 0xA0)
00103         {
00104             if(buf[1] == 0x01)
00105                 analog_enabled = 1;
00106             else
00107                 analog_enabled = 0;
00108         }
00109         else if(buf[0] == 0x02)
00110         {
00111             float value = (float)buf[1]/255;
00112             PWM = value;
00113         }
00114         else if(buf[0] == 0x03)
00115         {
00116             MYSERVO.write(buf[1]);
00117         }
00118         else if(buf[0] == 0x04)
00119         {
00120             analog_enabled = 0;
00121             PWM = 0;
00122             MYSERVO.write(0);
00123             LED_SET = 0;
00124             old_state = 0;    
00125         }
00126 
00127     }
00128 }
00129 /*
00130 void uartCB(void)
00131 {   
00132     while(pc.readable())    
00133     {
00134         rx_buf[rx_len++] = pc.getc();    
00135         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
00136         {
00137             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
00138             pc.printf("RecHandler \r\n");
00139             pc.printf("Length: ");
00140             pc.putc(rx_len);
00141             pc.printf("\r\n");
00142             rx_len = 0;
00143             break;
00144         }
00145     }
00146 }
00147 */
00148 void m_status_check_handle(void)
00149 {   
00150     uint8_t buf[3];
00151     if (analog_enabled)  // if analog reading enabled
00152     {
00153         // Read and send out
00154         float s = ANALOG;
00155         uint16_t value = s*1024; 
00156         buf[0] = (0x0B);
00157         buf[1] = (value >> 8);
00158         buf[2] = (value);
00159         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00160     }
00161     
00162     // If digital in changes, report the state
00163     if (BUTTON != old_state)
00164     {
00165         old_state = BUTTON;
00166         
00167         if (BUTTON == 1)
00168         {
00169             buf[0] = (0x0A);
00170             buf[1] = (0x01);
00171             buf[2] = (0x00);    
00172             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00173         }
00174         else
00175         {
00176             buf[0] = (0x0A);
00177             buf[1] = (0x00);
00178             buf[2] = (0x00);
00179            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00180         }
00181     }
00182 }
00183 
00184 
00185 int main(void)
00186 {   
00187     Ticker ticker;
00188     ticker.attach_us(m_status_check_handle, 200000);
00189     
00190     ble.init();
00191     ble.onDisconnection(disconnectionCallback);
00192     ble.onDataWritten(WrittenHandler);  
00193     
00194     //pc.baud(9600);
00195     //pc.printf("SimpleChat Init \r\n");
00196 
00197     //pc.attach( uartCB , pc.RxIrq);
00198     
00199     // setup advertising 
00200     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00201     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00202     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00203                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
00204     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00205                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00206     // 100ms; in multiples of 0.625ms. 
00207     ble.setAdvertisingInterval(160);
00208 
00209     ble.addService(uartService);
00210     
00211     ble.startAdvertising(); 
00212     
00213     //pc.printf("Advertising Start \r\n");
00214     
00215     while(1)
00216     {
00217         ble.waitForEvent(); 
00218     }
00219 }
00220 
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250