Qi Yao / LinkNode---test123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

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  *    The application works with the Science Journal App.
00021  *    Sensors Value from the BLE sensors tag to send
00022  *    to the Science Journal App.
00023  *    Characteristics Value received from App will print on Terminal.
00024  *    Sensors value will display the Science Jorunal App.
00025  */
00026 
00027 #include <stdio.h>
00028 #include "mbed.h"
00029 #include "ble/BLE.h"
00030 #include "goosci_utility.h"
00031 #include "config_change.h"
00032 #include "BMP180.h"
00033 
00034 #define SCIENCE_UUID_SERVICE                    0x0001 /**< The UUID of the Nordic UART Service. */
00035 #define SCIENCE_UUID_VALUE_CHARACTERISTIC       0x0003 /**< The UUID of the TX Characteristic. */
00036 #define SCIENCE_UUID_CONFIG_CHARACTERISTIC      0x0010 /**< The UUID of the RX Characteristic. */
00037 
00038 #define TXRX_BUF_LEN                     20
00039 #define INTERNAL                         200
00040 
00041 BLE  ble;
00042 Timer mytimer;
00043 Serial pc(P0_23, P0_25);
00044 
00045 //LED setup
00046 DigitalOut LED_R(P0_20);
00047 DigitalOut LED_G(P0_19);
00048 
00049 //Sensors setup
00050 I2C i2c(P0_17, P0_18);
00051 BMP180 bmp180(&i2c);
00052 
00053 //Light sensor
00054 //Analog setup
00055 AnalogIn myanalog(P0_1);
00056 
00057 //LIS3DH set up
00058 SPI spi_master(P0_6,P0_5,P0_7); //mosi miso sclk 
00059 DigitalOut cs(P0_4);
00060 
00061 
00062 uint16_t x_a,y_a,z_a;
00063 
00064 uint8_t LIS3DH_SPI_RD(uint8_t addr)
00065 {
00066      uint8_t  temp;
00067      cs = 0;
00068      wait_us(10);
00069      spi_master.write(addr);      
00070      temp=spi_master.write(0xff);
00071      wait_us(10);
00072      cs = 1;
00073      return temp;
00074 }
00075 void LIS3DH_SPI_WR(uint8_t addr,uint8_t wrdata)
00076 {  
00077      cs = 0;
00078      wait_us(10);
00079      spi_master.write(addr);
00080      spi_master.write(wrdata);
00081      wait_us(10);
00082      cs = 1;
00083 }
00084 void SPI_LIS3DH_Init()
00085 {
00086     spi_master.format(8,3);
00087     spi_master.frequency(100000);
00088     wait_ms(5);
00089     LIS3DH_SPI_WR(0x24,0x80);
00090     wait_ms(5);
00091     LIS3DH_SPI_WR(0x20,0x17);
00092     LIS3DH_SPI_WR(0x23,0x80);
00093 }
00094 void Get_LIS3DH_Val(void)
00095 {
00096     uint8_t Dx_L=1,Dy_L=1,Dz_L=1;
00097     uint8_t Dx_H=1,Dy_H=1,Dz_H=1;
00098     if(LIS3DH_SPI_RD(0x0f|0x80)==0x33)
00099     {
00100          printf("check device ok!\r\n");
00101          Dx_H=LIS3DH_SPI_RD(0x29|0x80);   
00102          Dx_L=LIS3DH_SPI_RD(0x28|0x80);
00103          Dy_H=LIS3DH_SPI_RD(0x2b|0x80);
00104          Dy_L=LIS3DH_SPI_RD(0x2A|0x80);
00105          Dz_H=LIS3DH_SPI_RD(0x2d|0x80);
00106          Dz_L=LIS3DH_SPI_RD(0x2C|0x80);  
00107      }
00108      else printf("check device err!\r\n");
00109      x_a=Dx_H<<8|Dx_L/16;
00110      y_a=Dy_H<<8|Dy_L/16;
00111      z_a=Dz_H<<8|Dz_L/16;
00112 }
00113 
00114 
00115 void Sensors_Init()
00116 {
00117     SPI_LIS3DH_Init();
00118     
00119     bmp180.init();
00120 }
00121 
00122 //
00123 
00124 // The Science Journal Service
00125 static const uint8_t science_uuid_conn[]= {0xe8,0x74,0x2c,0x65,0xf0,0x01,0x38,0x95,0x7a,0x46,0xaa,0x0a,0x01,0x00,0x5a,0x55};
00126 
00127 static const uint8_t science_base_uuid[] = {0x55,0x5a,0x00,0x01,0x0a,0xaa,0x46,0x7a,0x95,0x38,0x01,0xf0,0x65,0x2c,0x74,0xe8};
00128 
00129 static const uint8_t science_value_uuid[]= {0x55,0x5a,0x00,0x03,0x0a,0xaa,0x46,0x7a,0x95,0x38,0x01,0xf0,0x65,0x2c,0x74,0xe8};
00130 
00131 static const uint8_t science_config_uuid[]= {0x55,0x5a,0x00,0x10,0x0a,0xaa,0x46,0x7a,0x95,0x38,0x01,0xf0,0x65,0x2c,0x74,0xe8};
00132 
00133 
00134 
00135 
00136 uint8_t valuePayload[TXRX_BUF_LEN] = {0,};
00137 uint8_t configPayload[TXRX_BUF_LEN] = {0,};
00138 
00139 static uint8_t config_buf[TXRX_BUF_LEN];
00140 static uint8_t config_len=0;
00141 
00142 GattCharacteristic  valueCharacteristic (science_value_uuid, valuePayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00143 
00144 GattCharacteristic  configCharacteristic (science_config_uuid, configPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00145 
00146 GattCharacteristic *scienceChars[] = {&valueCharacteristic, &configCharacteristic};
00147 
00148 GattService         scienceService(science_base_uuid, scienceChars, sizeof(scienceChars) / sizeof(GattCharacteristic *));
00149 
00150 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00151 {
00152     pc.printf("Disconnected \r\n");
00153     pc.printf("Restart advertising \r\n");
00154     ble.startAdvertising();
00155 }
00156 
00157 void WrittenHandler(const GattWriteCallbackParams *Handler)
00158 {
00159     uint8_t buf[TXRX_BUF_LEN];
00160     uint16_t bytesRead;
00161 
00162     if (Handler->handle == configCharacteristic.getValueAttribute().getHandle()) 
00163     {
00164         ble.readCharacteristicValue(configCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00165         memset(valuePayload, 0, TXRX_BUF_LEN);
00166         memcpy(valuePayload, buf, TXRX_BUF_LEN);
00167         pc.printf("WriteHandler \r\n");
00168         int8_t handle_len = bytesRead;
00169         handle(valuePayload,handle_len);
00170         pc.printf("pin num : %d ",pin);
00171 
00172     }
00173 }
00174 
00175 void uartCB(void)
00176 {
00177     while(pc.readable()) {
00178         config_buf[config_len++] = pc.getc();
00179         if(config_len>=20 || config_buf[config_len-1]=='\0' || config_buf[config_len-1]=='\n') 
00180         {
00181             ble.updateCharacteristicValue(valueCharacteristic.getValueAttribute().getHandle(), config_buf, config_len);
00182             pc.printf("RecHandler \r\n");
00183             pc.printf("Length: ");
00184             pc.putc(config_len);
00185             pc.printf("config_len:   %d",config_len);
00186             pc.printf("\r\n");
00187             config_len = 0;
00188             break;
00189         }
00190     }
00191 }
00192 
00193 
00194 int main(void)
00195 {
00196     mytimer.start();
00197     ble.init();
00198     
00199     Sensors_Init();
00200 
00201     ble.onDisconnection(disconnectionCallback);
00202     ble.onDataWritten(WrittenHandler);
00203 
00204     pc.baud(9600);
00205     pc.printf("BLE sensors tag init \r\n");
00206 
00207     pc.attach( uartCB , pc.RxIrq);
00208 
00209     //setup advertising
00210     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00211     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00212 
00213     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00214                                     (const uint8_t *) "BLE-mbed",sizeof("BLE-mbed") - 1);
00215     
00216 
00217     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00218                                      (const uint8_t *)science_uuid_conn, sizeof(science_uuid_conn));
00219     // 100ms; in multiples of 0.625ms.
00220     ble.setAdvertisingInterval (160);
00221 
00222     ble.addService(scienceService);
00223 
00224     ble.startAdvertising();
00225     pc.printf("Advertising Start \r\n");
00226 
00227     while(1) 
00228     {
00229         LED_R = 0;
00230         LED_G = 0;
00231         
00232         if (pin == 0 && pin_type == ANALOG)   //Light sensor
00233         {
00234             //read analog value
00235             unsigned short AnalogValue=myanalog.read_u16();
00236             wait_ms(10);       
00237             pc.printf("Analog: %d\n",AnalogValue);    
00238             send_int_data(valueCharacteristic,mytimer.read_ms(),AnalogValue);  
00239         }
00240         else if(pin == 1 && pin_type == ANALOG)  //Temperature
00241         {
00242             bmp180.startTemperature();
00243             wait_ms(10);     // Wait for conversion to complete
00244             float temp;
00245             bmp180.getTemperature(&temp);
00246             int value = temp;
00247             pc.printf("Tem:%d \n",value);
00248             send_int_data(valueCharacteristic,mytimer.read_ms(),value); 
00249         }
00250         else if(pin == 2 && pin_type == ANALOG)    //Barometric pressure
00251         {
00252             bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
00253             wait_ms(10);    // Wait for conversion to complete
00254             int pressure;
00255             bmp180.getPressure(&pressure);
00256             pc.printf("Pre:%d \n",pressure);
00257             send_int_data(valueCharacteristic,mytimer.read_ms(),pressure);   
00258         }
00259         else if(pin == 3 && pin_type == ANALOG)     //X axis acceleration  
00260         {
00261           Get_LIS3DH_Val();
00262           pc.printf("Dx=:%d\r\n",x_a);
00263           send_int_data(valueCharacteristic,mytimer.read_ms(),x_a);
00264         }
00265         else if(pin == 4 && pin_type == ANALOG)      //Y axis acceleration
00266         {
00267           Get_LIS3DH_Val();
00268           pc.printf("Dy=:%d\r\n",y_a);
00269           send_int_data(valueCharacteristic,mytimer.read_ms(),y_a);
00270         }
00271         else if(pin == 5 && pin_type == ANALOG)     //Z axis acceleration
00272         {
00273           Get_LIS3DH_Val();
00274           pc.printf("Dz=:%d\r\n",z_a);
00275           send_int_data(valueCharacteristic,mytimer.read_ms(),z_a);          
00276         }
00277         else if(pin == 6 && pin_type == ANALOG)
00278         {
00279             //add another sensor in here
00280         }
00281         else
00282         {
00283             int error_code = 404;
00284             send_int_data(valueCharacteristic,mytimer.read_ms(),error_code);
00285         }
00286         LED_R = 1;
00287         LED_G = 1;
00288        
00289         wait_ms(INTERNAL);
00290         ble.waitForEvent();
00291     }
00292 }
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308