MDS_2 / Mbed 2 deprecated project_sensor

Dependencies:   project mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 using namespace std;
00002 
00003 #include "mbed.h"
00004 #include "spi_master.h"
00005 #include "adxl345.h"
00006 #include "ble/BLE.h"
00007 #include "ble/Gap.h"
00008 #include "ble/UUID.h"
00009 #include "ble/GattCharacteristic.h"
00010 #include "ble/GattCallbackParamTypes.h"
00011 #include "ble/GattServer.h"
00012 
00013 //#define CS      P0_7                                //chipselect
00014 #define MOSI    P0_9                                //Master Out Slave In
00015 #define MISO    P0_11                               //Master In Slave Out
00016 #define SCK     P0_8                                //Clock
00017 //additional IO
00018 #define ON_SW   P0_28                               //ON/OFF switch
00019 #define BEPB    P0_29                               //Enable/disable bluetooth push button
00020 #define CHARACT_DATA 0
00021 
00022 #define MIN_CONN_INTERVAL 10 //250 milliseconds
00023 #define MAX_CONN_INTERVAL 30 //350 milliseconds
00024 
00025 DigitalOut spi_cs(P0_28);
00026 DigitalIn int1(P0_15);
00027 DigitalIn location(P0_07);
00028 SPIClass SPI1(NRF_SPI1);
00029 data_member_t data[513];
00030 BLEDevice ble;
00031 Gap::Handle_t connectionHandle = 0xFFFF;
00032 
00033 /*SET UUID*/
00034 const UUID GATT_SERVICE = "88888888-5555-4444-1111-777777777777";
00035 const UUID ADV_DATA = "11001100-2200-3300-4400-550055005500";
00036 const UUID RECV_DATA = "22002200-3300-4400-5500-660066006600";
00037 char *compbuf= new char[26];
00038 uint16_t compbufLen=0;
00039 char *sendbuf= new char[26];
00040 char *sendbufaddr=sendbuf;
00041 uint16_t sendbufLen=20*sizeof(uint8_t);
00042 char *readystr = "data ready";
00043 uint16_t readyLen = sizeof("data ready");
00044 char *startstr = "START";
00045 uint16_t startLen = sizeof("START");
00046 char *sendstr = "SEND";
00047 uint16_t sendLen = sizeof("SEND");
00048 bool data_ready;
00049 uint16_t sendindex;
00050 uint8_t *AdvData= new uint8_t[26];
00051 uint16_t AdvDataLen=0;
00052 uint8_t *RcvData= new uint8_t[26];
00053 uint16_t RcvDataLen=0;
00054 uint8_t devnr = 1;
00055 unsigned count;
00056 /* Device Name, simpel discovey */
00057 const static char DEVICE_NAME[] = "Trump";
00058 GattCharacteristic gattchar = GattCharacteristic(ADV_DATA, AdvData, AdvDataLen, 26, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00059 GattCharacteristic rxCharacteristic = GattCharacteristic(RECV_DATA, RcvData, RcvDataLen, 26, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);// | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00060 
00061 GattCharacteristic *CHARACT_ARRAY[] = {&gattchar,&rxCharacteristic};
00062 GattService gatt_service(GATT_SERVICE,CHARACT_ARRAY,2);
00063 GattAttribute::Handle_t writeHandle;
00064 GattAttribute::Handle_t readHandle;
00065 
00066 
00067     
00068 
00069 bool poll_start_send_data(uint8_t *compare_str, uint8_t comp_length){
00070     uint16_t bufLen = 27;
00071     uint8_t *rdbuf= new uint8_t[bufLen];
00072     ble.readCharacteristicValue(readHandle,rdbuf,&bufLen);
00073     for (uint8_t i=0 ; i<comp_length-1 ; i++)
00074     {
00075         if (*(rdbuf+i*sizeof(uint8_t)) != *(compare_str+i*sizeof(uint8_t)))
00076         {
00077             delete rdbuf;
00078             return false;
00079         }
00080     }
00081     delete rdbuf;
00082     return true;
00083 }
00084 
00085 void Flash_Buff_WriteBytes(uint8_t addr, uint8_t pbuf)
00086 {    
00087     spi_cs = 0;
00088     wait_us(200);      
00089     SPI1.transfer((uint8_t)addr);     
00090     SPI1.transfer(pbuf);       
00091     
00092     wait_us(200);
00093     spi_cs = 1;
00094 }
00095 
00096 void Flash_Buff_ReadBytes(uint8_t addr, uint8_t *pbuf)
00097 {    
00098     spi_cs = 0;
00099     wait_us(20);
00100           
00101     SPI1.transfer((uint8_t)addr|0x80);     
00102     *pbuf = SPI1.transfer(0x00);
00103     
00104     wait_us(20);
00105     spi_cs = 1;
00106 }
00107 
00108 void init_adxl()
00109 {
00110     Flash_Buff_WriteBytes(INT_ENABLE, 0x00);
00111     Flash_Buff_WriteBytes(THRESH_TAP, THRESH_TAP_INIT);
00112     Flash_Buff_WriteBytes(OFSX, OFSX_INIT);
00113     Flash_Buff_WriteBytes(OFSY, OFSY_INIT);
00114     Flash_Buff_WriteBytes(OFSZ, OFSZ_INIT);
00115     Flash_Buff_WriteBytes(DUR, DUR_INIT);
00116     Flash_Buff_WriteBytes(Latent, Latent_INIT);
00117     Flash_Buff_WriteBytes(Window, Window_INIT);
00118     Flash_Buff_WriteBytes(THRESH_ACT, THRESH_ACT_INIT);
00119     Flash_Buff_WriteBytes(THRESH_INACT, THRESH_INACT_INIT);
00120     Flash_Buff_WriteBytes(TIME_INACT, TIME_INACT_INIT);
00121     Flash_Buff_WriteBytes(ACT_INACT_CTL, ACT_INACT_CTL_INIT);
00122     Flash_Buff_WriteBytes(THRESH_FF, THRESH_FF_INIT);
00123     Flash_Buff_WriteBytes(TIME_FF, TIME_FF_INIT);
00124     Flash_Buff_WriteBytes(TAP_AXES, TAP_AXES_INIT);
00125     Flash_Buff_WriteBytes(BW_RATE, BW_RATE_INIT);
00126     Flash_Buff_WriteBytes(POWER_CTL, POWER_CTL_INIT);
00127     Flash_Buff_WriteBytes(INT_MAP, INT_MAP_INIT);
00128     Flash_Buff_WriteBytes(DATA_FORMAT, DATA_FORMAT_INIT);
00129     Flash_Buff_WriteBytes(FIFO_CTL, FIFO_CTL_INIT);
00130     Flash_Buff_WriteBytes(POWER_CTL, POWER_CTL_INIT | 0x08);
00131     Flash_Buff_WriteBytes(INT_ENABLE, INT_ENABLE_INIT);
00132 }
00133 
00134 void read_data(uint16_t index)
00135 {
00136     spi_cs = 0;
00137     wait_us(20);
00138     
00139     SPI1.transfer((uint8_t)(DATAX0|0xC0));
00140     data[index].data_x0 = SPI1.transfer(0x00)^0x80;
00141     data[index].data_x1 = SPI1.transfer(0x00);
00142     data[index].data_y0 = SPI1.transfer(0x00)^0x80;
00143     data[index].data_y1 = SPI1.transfer(0x00);
00144     data[index].data_z0 = SPI1.transfer(0x00)^0x80;
00145     data[index].data_z1 = SPI1.transfer(0x00);
00146     
00147     wait_us(20);
00148     spi_cs = 1;
00149 }
00150 
00151 
00152 /* WHEN CONNECTION ...  */
00153 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
00154     if (params->role == Gap::CENTRAL) {
00155         connectionHandle = params->handle;
00156     }
00157 }
00158 
00159 
00160 /* Restart advertising when the peer disconnects */
00161 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00162 {
00163     BLE::Instance().gap().startAdvertising();
00164 }
00165 
00166 /*This function is called when the ble initialization process has failed*/
00167 void onBleInitError(BLE &ble, ble_error_t error)
00168 {
00169     /* Avoid compiler warnings, library said to do this */
00170     (void) ble;
00171     (void) error;
00172     
00173     /* Initialization error handling should go here */
00174 }
00175 
00176 void dataSentCallback(unsigned count)
00177 {
00178     SPI1.transfer(0xCC);
00179     sendindex+=1;
00180     memcpy(sendbufaddr+0*sizeof(uint8_t), &sendindex, sizeof(uint16_t));
00181     memcpy(sendbufaddr+2*sizeof(uint8_t), &(data[sendindex].data_x0), sizeof(uint8_t));
00182     memcpy(sendbufaddr+3*sizeof(uint8_t), &(data[sendindex].data_x1), sizeof(uint8_t));
00183     memcpy(sendbufaddr+4*sizeof(uint8_t), &(data[sendindex].data_y0), sizeof(uint8_t));
00184     memcpy(sendbufaddr+5*sizeof(uint8_t), &(data[sendindex].data_y1), sizeof(uint8_t));
00185     memcpy(sendbufaddr+6*sizeof(uint8_t), &(data[sendindex].data_z0), sizeof(uint8_t));
00186     memcpy(sendbufaddr+7*sizeof(uint8_t), &(data[sendindex].data_z1), sizeof(uint8_t));
00187     sendindex+=1;
00188     memcpy(sendbufaddr+8*sizeof(uint8_t), &(data[sendindex].data_x0), sizeof(uint8_t));
00189     memcpy(sendbufaddr+9*sizeof(uint8_t), &(data[sendindex].data_x1), sizeof(uint8_t));
00190     memcpy(sendbufaddr+10*sizeof(uint8_t), &(data[sendindex].data_y0), sizeof(uint8_t));
00191     memcpy(sendbufaddr+11*sizeof(uint8_t), &(data[sendindex].data_y1), sizeof(uint8_t));
00192     memcpy(sendbufaddr+12*sizeof(uint8_t), &(data[sendindex].data_z0), sizeof(uint8_t));
00193     memcpy(sendbufaddr+13*sizeof(uint8_t), &(data[sendindex].data_z1), sizeof(uint8_t));
00194     sendindex+=1;
00195     memcpy(sendbufaddr+14*sizeof(uint8_t), &(data[sendindex].data_x0), sizeof(uint8_t));
00196     memcpy(sendbufaddr+15*sizeof(uint8_t), &(data[sendindex].data_x1), sizeof(uint8_t));
00197     memcpy(sendbufaddr+16*sizeof(uint8_t), &(data[sendindex].data_y0), sizeof(uint8_t));
00198     memcpy(sendbufaddr+17*sizeof(uint8_t), &(data[sendindex].data_y1), sizeof(uint8_t));
00199     memcpy(sendbufaddr+18*sizeof(uint8_t), &(data[sendindex].data_z0), sizeof(uint8_t));
00200     memcpy(sendbufaddr+19*sizeof(uint8_t), &(data[sendindex].data_z1), sizeof(uint8_t));
00201     if(sendindex>=512)
00202         data_ready=false;
00203     
00204 }
00205 
00206 /*Callback triggered when ble initialization process has finished*/
00207 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00208 {
00209     BLE&        ble   = params->ble;
00210     ble_error_t error = params->error;
00211 
00212 /* In case of error, forward the error handling to onBleInitError */
00213     if (error != BLE_ERROR_NONE) {
00214         onBleInitError(ble, error);
00215         return;
00216     }
00217 
00218     /* Ensure that it is the default instance of BLE */
00219     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00220         return;
00221     }
00222     
00223     /* Set device name characteristic data */
00224     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
00225     
00226     ble.gattServer().onDataSent(dataSentCallback);
00227         
00228     
00229     /* callback when disconnected */
00230     ble.gap().onDisconnection(disconnectionCallback);
00231 
00232     /* Sacrifice 3B of 31B to some Advertising Flags (see nrfconnect toolbox for flag parameters :) ) */
00233     // for
00234     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);// say this device can be discovered by other BLE supportingdevices
00235     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // say that a connection can be set ( connectable) and that it does not matter with who it connects (unidirected)
00236 
00237     
00238     /* Sacrifice another 2B of 31B (actually 29B) to AdvType overhead, rest goes to AdvData array --> about 26B of actual sendable data :o */
00239     //Overhead  = 1B Length, 1B Type
00240     //Type = AdvData
00241     //Length = sizeof(AdvData)
00242     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData));
00243 
00244     /*fill in device name as local name*/
00245     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00246 
00247     /* Set advertising interval. Longer interval == Less radiothingies use == longer battery life :) */
00248     ble.gap().setAdvertisingInterval(20); /* 100ms */
00249     
00250 
00251 
00252     ble.addService(gatt_service);
00253 
00254     //ble.gap().addData(GapAdvertisingData::SERVICE_DATA, (uint8_t *)AdvData, sizeof(AdvData));
00255     /* Start advertising */
00256     ble.gap().startAdvertising(); //start sending advertising packets
00257     
00258     ble.initializeSecurity();
00259     //end
00260 }
00261 
00262 
00263 main() {
00264     wait_ms(1);
00265     data_ready=false;
00266     BLE& ble = BLE::Instance();
00267     ble.init(bleInitComplete);
00268     spi_cs = 1;
00269     if (location==1) {
00270         char *waitingstr = "waiting1";
00271         uint16_t waitingLen = sizeof("waiting1"); }
00272     else if(location==0) {
00273         char *waitingstr = "waiting0";
00274         uint16_t waitingLen = sizeof("waiting0"); }
00275     uint16_t index;
00276     for(uint16_t i=0; i<513;i++){
00277         data[i].data_x0=(uint8_t)0;
00278         data[i].data_x1=(uint8_t)0;
00279         data[i].data_y0=(uint8_t)0;
00280         data[i].data_y1=(uint8_t)0;
00281         data[i].data_z0=(uint8_t)0;
00282         data[i].data_z1=(uint8_t)0;
00283     }
00284     SPI1.begin(SCK, MOSI, MISO);
00285     init_adxl();
00286     writeHandle = gattchar.getValueHandle();
00287     readHandle = rxCharacteristic.getValueHandle();
00288     
00289     while(1)
00290     {
00291         ble.gattServer().write(writeHandle,(uint8_t *)waitingstr, waitingLen);
00292         while (poll_start_send_data((uint8_t *)startstr, startLen) != true)
00293         {   
00294             ble.waitForEvent();
00295         }
00296         //SPI
00297         Flash_Buff_WriteBytes(POWER_CTL, POWER_CTL_INIT | 0x08);
00298         for (index=0 ; index<513 ; index++){
00299             while(!int1){wait_us(1);}
00300             read_data(index);
00301         }
00302         Flash_Buff_WriteBytes(POWER_CTL, POWER_CTL_INIT | 0x04);
00303         ble.gattServer().write(writeHandle,(uint8_t *)readystr, readyLen);
00304         //condition to send data
00305         while (poll_start_send_data((uint8_t *)sendstr, sendLen) != true)
00306         {   
00307             ble.waitForEvent();
00308         }
00309         index=0;
00310         sendindex=2;
00311         data_ready=true;
00312         memcpy(sendbufaddr+0*sizeof(uint8_t), &index, sizeof(uint16_t));
00313         memcpy(sendbufaddr+2*sizeof(uint8_t), &(data[index].data_x0), sizeof(uint8_t));
00314         memcpy(sendbufaddr+3*sizeof(uint8_t), &(data[index].data_x1), sizeof(uint8_t));
00315         memcpy(sendbufaddr+4*sizeof(uint8_t), &(data[index].data_y0), sizeof(uint8_t));
00316         memcpy(sendbufaddr+5*sizeof(uint8_t), &(data[index].data_y1), sizeof(uint8_t));
00317         memcpy(sendbufaddr+6*sizeof(uint8_t), &(data[index].data_z0), sizeof(uint8_t));
00318         memcpy(sendbufaddr+7*sizeof(uint8_t), &(data[index].data_z1), sizeof(uint8_t));
00319         memcpy(sendbufaddr+8*sizeof(uint8_t), &(data[index+1].data_x0), sizeof(uint8_t));
00320         memcpy(sendbufaddr+9*sizeof(uint8_t), &(data[index+1].data_x1), sizeof(uint8_t));
00321         memcpy(sendbufaddr+10*sizeof(uint8_t), &(data[index+1].data_y0), sizeof(uint8_t));
00322         memcpy(sendbufaddr+11*sizeof(uint8_t), &(data[index+1].data_y1), sizeof(uint8_t));
00323         memcpy(sendbufaddr+12*sizeof(uint8_t), &(data[index+1].data_z0), sizeof(uint8_t));
00324         memcpy(sendbufaddr+13*sizeof(uint8_t), &(data[index+1].data_z1), sizeof(uint8_t));
00325         memcpy(sendbufaddr+14*sizeof(uint8_t), &(data[index+2].data_x0), sizeof(uint8_t));
00326         memcpy(sendbufaddr+15*sizeof(uint8_t), &(data[index+2].data_x1), sizeof(uint8_t));
00327         memcpy(sendbufaddr+16*sizeof(uint8_t), &(data[index+2].data_y0), sizeof(uint8_t));
00328         memcpy(sendbufaddr+17*sizeof(uint8_t), &(data[index+2].data_y1), sizeof(uint8_t));
00329         memcpy(sendbufaddr+18*sizeof(uint8_t), &(data[index+2].data_z0), sizeof(uint8_t));
00330         memcpy(sendbufaddr+19*sizeof(uint8_t), &(data[index+2].data_z1), sizeof(uint8_t));
00331         wait(2);
00332         while (data_ready == true)
00333         {   
00334             ble.gattServer().write(writeHandle, (uint8_t *)sendbufaddr, sendbufLen);
00335             ble.waitForEvent();
00336             
00337         }
00338     }
00339 }