Mehdi Hamm / Mbed 2 deprecated nRF51822_SimpleControls

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleControls by RedBearLab

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 #include "GattCallbackParamTypes.h"
00024 #include "ble/FunctionPointerWithContext.h"
00025 #include "BMP085.h"
00026 
00027 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
00028 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
00029 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
00030 
00031 #define TXRX_BUF_LEN                     20
00032 
00033 #define DIGITAL_OUT_PIN                  P0_17  //D7
00034 #define DIGITAL_IN_PIN                   P0_5   //A4
00035 #define PWM_PIN                          P0_16  //D6
00036 #define SERVO_PIN                        P0_14  //D10
00037 #define ANALOG_IN_PIN                    P0_1   //A0
00038 
00039 BLE             ble;
00040 
00041 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
00042 DigitalIn       BUTTON(DIGITAL_IN_PIN);
00043 PwmOut          PWM(PWM_PIN);
00044 AnalogIn        ANALOG(ANALOG_IN_PIN);
00045 Servo           MYSERVO(SERVO_PIN);
00046 BMP085  myCaptor(P0_29, P0_28);
00047 
00048 Serial pc(USBTX, USBRX);
00049 
00050 static uint8_t analog_enabled = 0;
00051 static uint8_t old_state = 0;
00052 static uint8_t captor_enabled = 0;
00053 static uint8_t captor_enabled_for_pressure = 0;
00054 
00055 // The Nordic UART Service
00056 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00057 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00058 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00059 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
00060 
00061 
00062 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00063 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00064 
00065 //static uint8_t rx_buf[TXRX_BUF_LEN];
00066 //static uint8_t rx_len=0;
00067 
00068 
00069 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00070                                       
00071 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00072                                       
00073 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00074 
00075 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00076 
00077 
00078 
00079 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00080 {
00081     pc.printf("Disconnected \r\n");
00082     pc.printf("Restart advertising \r\n");
00083     ble.startAdvertising();
00084 }
00085 
00086 void confirmationHandler(uint16_t  Handler)
00087 {
00088     if (captor_enabled)
00089         captor_enabled = false;
00090 }
00091 
00092 void WrittenHandler(const GattWriteCallbackParams *Handler)
00093 {   
00094     uint8_t buf[TXRX_BUF_LEN];
00095     uint16_t bytesRead, index;
00096     
00097     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00098     {
00099         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00100         memset(txPayload, 0, TXRX_BUF_LEN);
00101         memcpy(txPayload, buf, TXRX_BUF_LEN);       
00102         
00103         for(index=0; index<bytesRead; index++)
00104             pc.putc(buf[index]);
00105             
00106         if(buf[0] == 0x01)
00107         {
00108             if(buf[1] == 0x01)
00109                 LED_SET = 1;
00110             else
00111                 LED_SET = 0;    
00112         }
00113         else if(buf[0] == 0xA0)
00114         {
00115             if(buf[1] == 0x01){
00116                 analog_enabled = 1;
00117                 captor_enabled = 1;
00118             }
00119             else
00120                 analog_enabled = 0;
00121                 
00122         }
00123         else if(buf[0] == 0x02)
00124         {
00125             captor_enabled_for_pressure= 1;
00126             float value = (float)buf[1]/255;
00127             PWM = value;
00128         }
00129         else if(buf[0] == 0x03)
00130         {
00131             MYSERVO.write(buf[1]);
00132         }
00133         else if(buf[0] == 0x04)
00134         {
00135             analog_enabled = 0;
00136             PWM = 0;
00137             MYSERVO.write(0);
00138             LED_SET = 0;
00139             old_state = 0;    
00140         }
00141 
00142     }
00143 }
00144 /*
00145 void uartCB(void)
00146 {   
00147     while(pc.readable())    
00148     {
00149         rx_buf[rx_len++] = pc.getc();    
00150         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
00151         {
00152             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
00153             pc.printf("RecHandler \r\n");
00154             pc.printf("Length: ");
00155             pc.putc(rx_len);
00156             pc.printf("\r\n");
00157             rx_len = 0;
00158             break;
00159         }
00160     }
00161 }
00162 */
00163 void m_status_check_handle(void)
00164 {   
00165     uint8_t buf[3];
00166     if (analog_enabled)  // if analog reading enabled
00167     {
00168         // Read and send out
00169         float s = ANALOG;
00170         uint16_t value = s*1024; 
00171         buf[0] = (0x0B);
00172         buf[1] = (value >> 8);
00173         buf[2] = (value);
00174         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00175     }
00176     
00177     if (captor_enabled)
00178     {
00179         // Read and send out
00180         captor_enabled = false;
00181         myCaptor.update();
00182         float s = myCaptor.get_temperature() * 100;
00183         int value = s; 
00184         buf[0] = (value >> 24);
00185         buf[1] = (value >> 16);
00186         buf[2] = (value >> 8);
00187         buf[3] = value;
00188         //for(int i = 0; i < 200000; ++i)
00189         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); 
00190     }
00191     
00192     if (captor_enabled_for_pressure)
00193     {
00194         // Read and send out
00195         captor_enabled_for_pressure = false;
00196         myCaptor.update();
00197         float s = myCaptor.get_pressure() * 100;
00198         uint32_t value = s; 
00199         buf[0] = (value >> 24);
00200         buf[1] = (value >> 16);
00201         buf[2] = (value >> 8);
00202         buf[3] = (uint8_t) value;
00203         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); 
00204     }
00205     
00206     // If digital in changes, report the state
00207     if (BUTTON != old_state)
00208     {
00209         old_state = BUTTON;
00210         
00211         if (BUTTON == 1)
00212         {
00213             buf[0] = (0x0A);
00214             buf[1] = (0x01);
00215             buf[2] = (0x00);    
00216             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00217         }
00218         else
00219         {
00220             buf[0] = (0x0A);
00221             buf[1] = (0x00);
00222             buf[2] = (0x00);
00223            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
00224         }
00225     }
00226 }
00227 
00228 
00229 int main(void)
00230 {   
00231     Ticker ticker;
00232     ticker.attach_us(m_status_check_handle, 200000);
00233     
00234     ble.init();
00235     ble.onDisconnection(disconnectionCallback);
00236     ble.onDataWritten(WrittenHandler);  
00237     ble.onConfirmationReceived(confirmationHandler);
00238     
00239     pc.baud(9600);
00240     pc.printf("SimpleChat Init \r\n");
00241 
00242     //pc.attach( uartCB , pc.RxIrq);
00243     
00244     // setup advertising 
00245     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00246     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00247     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00248                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
00249     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00250                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00251     // 100ms; in multiples of 0.625ms. 
00252     ble.setAdvertisingInterval(160);
00253 
00254     ble.addService(uartService);
00255     
00256     ble.startAdvertising(); 
00257     
00258     pc.printf("Advertising Start \r\n");
00259     
00260     while(1)
00261     {
00262         ble.waitForEvent(); 
00263     }
00264 }
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296