Jackson Lv / Mbed 2 deprecated BLENano_RGB_Demo

Dependencies:   BLE_API ChainableLED mbed nRF51822

Fork of BLENano_RGB_Demo by Jackson Lv

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 "ChainableLED.h"
00023 
00024 /******************************************************
00025  *                      Macros
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                     5
00032 
00033 #define LOCAL_NAME                       "RGB-L"
00034 #define BUTTON_PIN                       P0_8//D3
00035 #define DEVICE_ID                        0xFF
00036 #define LIGHT_PIN                        P0_5//A3
00037 #define VOL_PIN                          P0_4//A4
00038 
00039 /******************************************************
00040  *               Variable Definitions
00041  ******************************************************/
00042 
00043 InterruptIn button(BUTTON_PIN);
00044 
00045 BLEDevice       ble;
00046 
00047 ChainableLED    leds(P0_11, P0_9, P0_10,1);
00048 
00049 AnalogIn       light_sen(LIGHT_PIN);
00050 AnalogIn       vin(VOL_PIN);
00051 
00052 DigitalOut myled(P0_19);
00053 
00054 static uint8_t rgb_data[5]={0xA5,0xFF,0xFF, 0xFF, 0xFF};
00055 static unsigned char rgb_status=0;
00056 
00057 static uint8_t light_flag = 0;
00058 
00059 // The Nordic UART Service
00060 static const uint8_t uart_base_uuid[]       = {0x5A, 0x2D, 0x3B, 0xF8, 0xF0, 0xBC, 0x11, 0xE5, 0x9C, 0xE9, 0x5E, 0x55, 0x17, 0x50, 0x7E, 0x66}; 
00061 static const uint8_t uart_tx_uuid[]         = {0x5A, 0x2D, 0x40, 0xEE, 0xF0, 0xBC, 0x11, 0xE5, 0x9C, 0xE9, 0x5E, 0x55, 0x17, 0x50, 0x7E, 0x66};
00062 static const uint8_t uart_rx_uuid[]         = {0x5A, 0x2D, 0x42, 0x9C, 0xF0, 0xBC, 0x11, 0xE5, 0x9C, 0xE9, 0x5E, 0x55, 0x17, 0x50, 0x7E, 0x66};
00063 static const uint8_t uart_light_uuid[]      = {0x5A, 0x2D, 0x43, 0x78, 0xF0, 0xBC, 0x11, 0xE5, 0x9C, 0xE9, 0x5E, 0x55, 0x17, 0x50, 0x7E, 0x66};
00064 static const uint8_t uart_base_uuid_rev[]   = {0x66, 0x7E, 0x50, 0x17, 0x55, 0x5E, 0xE9, 0x9C, 0xE5, 0x11, 0xBC, 0xF0, 0xF8, 0x3B, 0x2D, 0x5A};
00065 
00066 
00067 uint8_t txPayload[TXRX_BUF_LEN] = {0,0,0,0,0};
00068 uint8_t rxPayload[TXRX_BUF_LEN] = {0,0,0,0,0};
00069 uint8_t lightPayload[TXRX_BUF_LEN] = {0,0,0,0,0};
00070 uint8_t vinPayload[TXRX_BUF_LEN] = {0,0,0,0,0};
00071 
00072 
00073 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE );
00074 
00075 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00076 
00077 GattCharacteristic  lightCharacteristic (uart_light_uuid, lightPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00078 
00079 GattCharacteristic  batCharacteristic (0x2A19, vinPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00080 
00081 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic,&lightCharacteristic,&batCharacteristic};
00082 
00083 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00084 
00085 
00086 
00087  /******************************************************
00088  *               Function Definitions
00089  ******************************************************/
00090 
00091 static void connectCallBack(const Gap::ConnectionCallbackParams_t *params)
00092 {
00093     leds.setColorRGB(0, 0XFF, 0XFF, 0XFF);  
00094 }
00095 
00096 
00097 void disconnectionCallBack(const Gap::DisconnectionCallbackParams_t *params)
00098 {
00099     leds.setColorRGB(0, 0, 0, 0);  
00100     ble.gap().startAdvertising();
00101 }
00102 
00103 void timeoutCallBack()
00104 {
00105     
00106 }
00107 
00108 
00109 
00110 // GATT call back handle
00111 void writtenHandle(const GattWriteCallbackParams *Handler)
00112 {
00113     uint8_t buf[TXRX_BUF_LEN];
00114     uint16_t bytesRead;
00115 
00116     //pc.printf("Write Handle : \n");
00117  
00118     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
00119     {
00120         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00121         memset(txPayload, 0, TXRX_BUF_LEN);
00122         memcpy(txPayload, buf, TXRX_BUF_LEN);  
00123         
00124         if( (buf[2] != 0x00) || (buf[3] != 0x00) || (buf[4] != 0x00) )  
00125         {
00126             rgb_status = 1;
00127             leds.setColorRGB(0, buf[2], buf[3], buf[4]);  
00128             rgb_data[0] = 0xA5;
00129             rgb_data[1] = 0xff;  
00130             rgb_data[2] = buf[2];
00131             rgb_data[3] = buf[3];
00132             rgb_data[4] = buf[4];
00133             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5, true);   
00134         }
00135         else
00136         {
00137             rgb_status = 0;
00138             leds.setColorRGB(0, 0, 0, 0);   
00139             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5, true);      
00140         }
00141     }
00142 }
00143 
00144 //switch on/off the rgb led and notify the value
00145 void button_handle()
00146 {
00147     uint8_t buf[5];
00148     buf[0] = 0xA5;
00149     buf[1] = 0xff;
00150     if(rgb_status == 0)  
00151     {
00152         rgb_status = 1;
00153         leds.setColorRGB(0, rgb_data[2], rgb_data[3], rgb_data[4]);       
00154         buf[2] = rgb_data[2];
00155         buf[3] = rgb_data[3];
00156         buf[4] = rgb_data[4];
00157     }
00158     else 
00159     {
00160         rgb_status = 0;
00161         leds.setColorRGB(0, 0, 0, 0);
00162         buf[2] = 0x00;
00163         buf[3] = 0x00;
00164         buf[4] = 0x00;
00165     }
00166     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);
00167     
00168 }
00169 
00170 //check the light value and battery value 
00171 void m_status_check_handle(void)
00172 {
00173     uint16_t light_value, vin_val=0;
00174     uint8_t light_buf[2] = {0,0};
00175     
00176     uint8_t vin_buf[2] = {0,0};
00177     
00178     light_value = light_sen.read_u16();
00179     
00180     light_buf[0] = (light_value&0xFF00)>>8;
00181     light_buf[1] = light_value&0x00FF;
00182     
00183     vin_val = vin.read_u16();
00184     
00185     vin_buf[0] = (vin_val&0xFF00)>>8;
00186     vin_buf[1] = vin_val&0x00FF;
00187     
00188     
00189     ble.updateCharacteristicValue(batCharacteristic.getValueAttribute().getHandle(),vin_buf , 2,true); 
00190     if(vin_val<0x230)
00191     {
00192         ble.updateCharacteristicValue(batCharacteristic.getValueAttribute().getHandle(),vin_buf , 2); 
00193     }
00194     //trigger value is 0x100,you can change it base your environment
00195     if((light_value<0x100)&&(light_flag==0))
00196     {
00197         ble.updateCharacteristicValue(lightCharacteristic.getValueAttribute().getHandle(),light_buf , 2);   
00198         light_flag = 1;
00199     }
00200     if((light_value>0x100)&&(light_flag==1))
00201     {
00202         ble.updateCharacteristicValue(lightCharacteristic.getValueAttribute().getHandle(),light_buf , 2);   
00203         light_flag = 0;
00204     }
00205     
00206 }
00207 
00208 int main() {
00209     
00210       
00211     uint8_t buf[5]    = {0xA5,0xFF,0x00, 0x00, 0x00};
00212     
00213     rgb_status = 0;
00214     leds.setColorRGB(0, 0, 0, 0);  
00215         
00216     button.mode(PullUp);
00217     button.fall(&button_handle);
00218     Ticker ticker;
00219     ticker.attach_us(m_status_check_handle, 200000);
00220        
00221     ble.init();
00222 
00223     ble.onConnection(connectCallBack);
00224     ble.onDisconnection(disconnectionCallBack);
00225     ble.onDataWritten(writtenHandle);
00226       
00227 
00228     // setup adv_data and srp_data
00229     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00230     
00231     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00232                                      (const uint8_t *)LOCAL_NAME, sizeof(LOCAL_NAME) - 1);   
00233     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00234                                      (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev));
00235                                                             
00236     // add service
00237     ble.addService(uartService);
00238     // set device name
00239     ble.setDeviceName((const uint8_t *)LOCAL_NAME);
00240     // set appearance
00241     //ble.setAppearance(BLE_APPEARANCE_UNKNOWN);
00242     // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
00243     ble.setTxPower(4);
00244     // set adv_type
00245     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00246     // set adv_interval, 100ms in multiples of 0.625ms.
00247     ble.setAdvertisingInterval(160);
00248     // set adv_timeout, in seconds
00249     ble.setAdvertisingTimeout(0);
00250     // ger BLE stack version
00251     //pc.printf( ble.getVersion() );
00252     // set RGB default status
00253     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5, true);   
00254     // start advertising
00255     ble.startAdvertising();
00256 
00257     memset(rgb_data, 0xFF, 5);
00258     myled = 1;   
00259     
00260     while(1)
00261     {
00262      
00263         ble.waitForEvent(); 
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 
00297 
00298 
00299