Cristi Stoican / Mbed 2 deprecated Migration

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 /*
00021  *    The application works with the BLEController iOS/Android App.
00022  *    Type something from the Terminal to send
00023  *    to the BLEController App or vice verse.
00024  *    Characteristics received from App will print on Terminal.
00025  */
00026  
00027 #include "mbed.h"
00028 #include "BTDevice.hpp"
00029 #include "ControllerFactory.hpp"
00030 
00031 BLE  ble;
00032 Ticker periodicCommand;
00033 
00034 Serial pc(USBTX, USBRX);
00035 
00036 BTDevice Nano(ble);
00037 ControllerFactory Control_Law();
00038 //Controller c;
00039 char *old_receive = NULL;
00040 float R[3] = {0.1,0.4,0.6};
00041 float S[3] = {0.2,0.1,0.5};
00042 float T[4] = {0.1,0.7,0.5};
00043 ControllerParams Parameters;
00044 
00045 
00046 // The Nordic UART Service
00047 
00048 /*decode(uint8_t *receive, Controller *c);
00049 
00050 void state_machine(uint8_t *receive)
00051 {
00052             switch((char *)receive)
00053             {
00054                 case 'P': 
00055                 {
00056                     Control_Law.createController(&c, P);
00057                     Parameters.kp = 0.1;
00058                     Control_Law.createControllerParams(&c, P, &Parameters, 0);
00059                     c.updateRef(0.5);
00060                 } 
00061                 case 'PI':
00062                 {
00063                     Control_Law.createController(&c, PI);    
00064                     Parameters.kp = 0.1;
00065                     Parameters.ti = 0.3;
00066                     Control_Law.createControllerParams(&c, PI, &Parameters, 0);
00067                     c.updateRef(0.7);
00068                 }
00069                 case 'PID':
00070                 {
00071                     Control_Law.createController(&c, PID);  
00072                     Parameters.kp = 0.1;
00073                     Parameters.ti = 0.3; 
00074                     Parameters.td = 0.15;
00075                     Control_Law.createControllerParams(&c, PID, &Parameters, 0);
00076                     c.updateRef(0.9);
00077                 }
00078                 case 'RST':
00079                 {
00080                     Control_Law.createController(&c, RST);  
00081                     Parameters.ordR = sizeof(R)/sizeof(*R);
00082                     Parameters.ordS = sizeof(S)/sizeof(*S);
00083                     Parameters.ordT = sizeof(T)/sizeof(*T);
00084                     Parameters.R = R;
00085                     Parameters.S = S;
00086                     Parameters.T = T;
00087                     Control_Law.createControllerParams(&c, RST, &Parameters, Parameters.ordT);
00088                     c.updateRef(1);
00089                 }
00090                 case 'delete':
00091                 {
00092                     delete c;  
00093                 }
00094             }    
00095         decode_message(&receive, &c);
00096 }
00097 
00098 decode_message(uint8_t *receive, Controller c)
00099 {
00100       switch((char *)receive)
00101       {
00102             case 'Command':
00103             {
00104                   Nano.sendMsg((uint8_t *),c.calculateCmd, 16);  
00105             }
00106       }
00107 }*/
00108 
00109 
00110 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00111 {
00112     pc.printf("Disconnected \r\n");
00113     pc.printf("Restart advertising \r\n");
00114     ble.startAdvertising();
00115 }
00116 
00117 void WrittenHandler(const GattWriteCallbackParams *Handler)
00118 {   
00119     uint8_t buf[TXRX_BUF_LEN];
00120     uint16_t bytesRead, index;
00121     
00122     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00123     {
00124         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00125         memset(txPayload, 0, TXRX_BUF_LEN);
00126         memcpy(txPayload, buf, TXRX_BUF_LEN);       
00127         //state_machine(&buf);
00128     }
00129 }
00130 
00131 void uartCB(void)
00132 {   
00133     while(pc.readable())    
00134     {
00135         rx_buf[rx_len++] = pc.getc();    
00136         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
00137         {
00138             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
00139             rx_len = 0;
00140             break;
00141         }
00142     }
00143 }
00144 
00145 int main(void)
00146 {
00147     ble.init();
00148     ble.onDisconnection(disconnectionCallback);
00149     ble.onDataWritten(WrittenHandler);  
00150     
00151     pc.baud(9600);
00152     pc.printf("SimpleChat Init \r\n");
00153     
00154     pc.attach( uartCB , pc.RxIrq);
00155    // setup advertising 
00156     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00157     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00158     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00159                                     (const uint8_t *)"Gigel", sizeof("Biscuit") - 1);
00160     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00161                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00162     // 100ms; in multiples of 0.625ms. 
00163     ble.setAdvertisingInterval(160);
00164 
00165     ble.addService(uartService);
00166     
00167     ble.startAdvertising(); 
00168     pc.printf("Advertising Start \r\n");
00169     
00170     while(1)
00171     {
00172         ble.waitForEvent(); 
00173     }
00174 }
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190