PID

Dependencies:   BLE_API mbed nRF51822

Committer:
stoicancristi
Date:
Thu Jan 12 16:04:37 2017 +0000
Revision:
1:d3e12393b71d
Parent:
0:1f4d5c5491b8
v2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stoicancristi 0:1f4d5c5491b8 1 /*
stoicancristi 0:1f4d5c5491b8 2
stoicancristi 0:1f4d5c5491b8 3 Copyright (c) 2012-2014 RedBearLab
stoicancristi 0:1f4d5c5491b8 4
stoicancristi 0:1f4d5c5491b8 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
stoicancristi 0:1f4d5c5491b8 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
stoicancristi 0:1f4d5c5491b8 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
stoicancristi 0:1f4d5c5491b8 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
stoicancristi 0:1f4d5c5491b8 9 subject to the following conditions:
stoicancristi 0:1f4d5c5491b8 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
stoicancristi 0:1f4d5c5491b8 11
stoicancristi 0:1f4d5c5491b8 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
stoicancristi 0:1f4d5c5491b8 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
stoicancristi 0:1f4d5c5491b8 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
stoicancristi 0:1f4d5c5491b8 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
stoicancristi 0:1f4d5c5491b8 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
stoicancristi 0:1f4d5c5491b8 17
stoicancristi 0:1f4d5c5491b8 18 */
stoicancristi 0:1f4d5c5491b8 19
stoicancristi 0:1f4d5c5491b8 20 /*
stoicancristi 0:1f4d5c5491b8 21 * The application works with the BLEController iOS/Android App.
stoicancristi 0:1f4d5c5491b8 22 * Type something from the Terminal to send
stoicancristi 0:1f4d5c5491b8 23 * to the BLEController App or vice verse.
stoicancristi 0:1f4d5c5491b8 24 * Characteristics received from App will print on Terminal.
stoicancristi 0:1f4d5c5491b8 25 */
stoicancristi 0:1f4d5c5491b8 26
stoicancristi 0:1f4d5c5491b8 27 //#include "mbed.h"
stoicancristi 0:1f4d5c5491b8 28 #include "PID.h"
stoicancristi 1:d3e12393b71d 29 #include "RST.h"
stoicancristi 0:1f4d5c5491b8 30 #include "ble/BLE.h"
stoicancristi 1:d3e12393b71d 31 #include "StateMachine.h"
stoicancristi 1:d3e12393b71d 32 #include <Gap.h>
stoicancristi 0:1f4d5c5491b8 33
stoicancristi 0:1f4d5c5491b8 34
stoicancristi 0:1f4d5c5491b8 35 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
stoicancristi 0:1f4d5c5491b8 36 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
stoicancristi 0:1f4d5c5491b8 37 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
stoicancristi 0:1f4d5c5491b8 38
stoicancristi 1:d3e12393b71d 39 //#define MyASSERT(cond , serialpc, errVal) assert_error_app((bool)cond, serialpc, (uint16_t)errVal, __LINE__, __FILE__)
stoicancristi 0:1f4d5c5491b8 40 #define TXRX_BUF_LEN 20
stoicancristi 0:1f4d5c5491b8 41
stoicancristi 0:1f4d5c5491b8 42 BLE ble;
stoicancristi 0:1f4d5c5491b8 43
stoicancristi 0:1f4d5c5491b8 44 Serial pc(USBTX, USBRX);
stoicancristi 0:1f4d5c5491b8 45
stoicancristi 0:1f4d5c5491b8 46
stoicancristi 0:1f4d5c5491b8 47 // The Nordic UART Service
stoicancristi 0:1f4d5c5491b8 48 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 49 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 50 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 51 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
stoicancristi 0:1f4d5c5491b8 52
stoicancristi 0:1f4d5c5491b8 53
stoicancristi 0:1f4d5c5491b8 54 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
stoicancristi 0:1f4d5c5491b8 55 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
stoicancristi 1:d3e12393b71d 56 //static uint32_t GlobalTime = 1;
stoicancristi 1:d3e12393b71d 57 Ticker periodicCommandRST;
stoicancristi 1:d3e12393b71d 58 Ticker periodicCommandPID;
stoicancristi 1:d3e12393b71d 59 static float PIDGlobalCommand = 0;
stoicancristi 1:d3e12393b71d 60 static float RSTGlobalCommand = 0;
stoicancristi 1:d3e12393b71d 61 static const float GlobalSamplingTime = 0.5;
stoicancristi 1:d3e12393b71d 62 //static const float DecodeTime = 0.1;
stoicancristi 0:1f4d5c5491b8 63
stoicancristi 0:1f4d5c5491b8 64 static uint8_t rx_buf[TXRX_BUF_LEN];
stoicancristi 0:1f4d5c5491b8 65 static uint8_t rx_len=0;
stoicancristi 1:d3e12393b71d 66 static States sm_states;
stoicancristi 0:1f4d5c5491b8 67
stoicancristi 1:d3e12393b71d 68 bool g_bIsConnected = false;
stoicancristi 1:d3e12393b71d 69 bool g_bIsAdvertising = false;
stoicancristi 1:d3e12393b71d 70 bool g_bConnDisabled = false;
stoicancristi 1:d3e12393b71d 71
stoicancristi 1:d3e12393b71d 72 bool apply_PIDCommand = false;
stoicancristi 1:d3e12393b71d 73 bool apply_RSTCommand = false;
stoicancristi 1:d3e12393b71d 74
stoicancristi 1:d3e12393b71d 75 DigitalOut myled(LED1);
stoicancristi 1:d3e12393b71d 76 AnalogIn input(P0_4);
stoicancristi 1:d3e12393b71d 77 PwmOut output(D4);
stoicancristi 0:1f4d5c5491b8 78
stoicancristi 0:1f4d5c5491b8 79 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
stoicancristi 0:1f4d5c5491b8 80
stoicancristi 0:1f4d5c5491b8 81 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
stoicancristi 0:1f4d5c5491b8 82
stoicancristi 0:1f4d5c5491b8 83 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
stoicancristi 0:1f4d5c5491b8 84
stoicancristi 0:1f4d5c5491b8 85 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
stoicancristi 0:1f4d5c5491b8 86
stoicancristi 1:d3e12393b71d 87 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
stoicancristi 1:d3e12393b71d 88 {
stoicancristi 1:d3e12393b71d 89 pc.printf("Connected \r\n");
stoicancristi 1:d3e12393b71d 90 g_bIsConnected = true;
stoicancristi 1:d3e12393b71d 91 g_bIsAdvertising = false;
stoicancristi 1:d3e12393b71d 92 g_bConnDisabled = false;
stoicancristi 1:d3e12393b71d 93 }
stoicancristi 0:1f4d5c5491b8 94
stoicancristi 0:1f4d5c5491b8 95 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
stoicancristi 0:1f4d5c5491b8 96 {
stoicancristi 0:1f4d5c5491b8 97 pc.printf("Disconnected \r\n");
stoicancristi 0:1f4d5c5491b8 98 pc.printf("Restart advertising \r\n");
stoicancristi 0:1f4d5c5491b8 99 ble.startAdvertising();
stoicancristi 1:d3e12393b71d 100 g_bIsConnected = false;
stoicancristi 1:d3e12393b71d 101 g_bConnDisabled = true;
stoicancristi 1:d3e12393b71d 102 }
stoicancristi 1:d3e12393b71d 103
stoicancristi 1:d3e12393b71d 104 void connectionUpdate(conn_update option)
stoicancristi 1:d3e12393b71d 105 {
stoicancristi 1:d3e12393b71d 106 switch(option)
stoicancristi 1:d3e12393b71d 107 {
stoicancristi 1:d3e12393b71d 108 case start_advertising:
stoicancristi 1:d3e12393b71d 109 {
stoicancristi 1:d3e12393b71d 110 g_bIsAdvertising = true;
stoicancristi 1:d3e12393b71d 111 g_bIsConnected = true;
stoicancristi 1:d3e12393b71d 112 g_bConnDisabled = false;
stoicancristi 1:d3e12393b71d 113 pc.printf("Start advertising \n");
stoicancristi 1:d3e12393b71d 114 ble.startAdvertising();
stoicancristi 1:d3e12393b71d 115 break;
stoicancristi 1:d3e12393b71d 116 }
stoicancristi 1:d3e12393b71d 117 case stop_advertising:
stoicancristi 1:d3e12393b71d 118 {
stoicancristi 1:d3e12393b71d 119 g_bIsAdvertising = false;
stoicancristi 1:d3e12393b71d 120 g_bIsConnected = true;
stoicancristi 1:d3e12393b71d 121 g_bConnDisabled = false;
stoicancristi 1:d3e12393b71d 122 pc.printf("Stop advertising \n");
stoicancristi 1:d3e12393b71d 123 break;
stoicancristi 1:d3e12393b71d 124 }
stoicancristi 1:d3e12393b71d 125 case stop_connection:
stoicancristi 1:d3e12393b71d 126 {
stoicancristi 1:d3e12393b71d 127 pc.printf("All stop \n");
stoicancristi 1:d3e12393b71d 128 g_bIsAdvertising = false;
stoicancristi 1:d3e12393b71d 129 g_bConnDisabled = true;
stoicancristi 1:d3e12393b71d 130 g_bIsConnected = false;
stoicancristi 1:d3e12393b71d 131 break;
stoicancristi 1:d3e12393b71d 132 }
stoicancristi 1:d3e12393b71d 133 case connected:
stoicancristi 1:d3e12393b71d 134 {
stoicancristi 1:d3e12393b71d 135 pc.printf("Connected \n");
stoicancristi 1:d3e12393b71d 136 g_bIsConnected = true;
stoicancristi 1:d3e12393b71d 137 g_bConnDisabled = false;
stoicancristi 1:d3e12393b71d 138 g_bIsAdvertising = false;
stoicancristi 1:d3e12393b71d 139
stoicancristi 1:d3e12393b71d 140 }
stoicancristi 1:d3e12393b71d 141 }
stoicancristi 1:d3e12393b71d 142
stoicancristi 1:d3e12393b71d 143 }
stoicancristi 1:d3e12393b71d 144
stoicancristi 1:d3e12393b71d 145 void sendMsg(const uint8_t *buf, uint16_t length)
stoicancristi 1:d3e12393b71d 146 {
stoicancristi 1:d3e12393b71d 147 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, length);
stoicancristi 1:d3e12393b71d 148 }
stoicancristi 1:d3e12393b71d 149
stoicancristi 1:d3e12393b71d 150 void WriteBuffer(char* myBuf)
stoicancristi 1:d3e12393b71d 151 {
stoicancristi 1:d3e12393b71d 152 uint16_t bytesRead = 20;
stoicancristi 1:d3e12393b71d 153 sendMsg((uint8_t *)myBuf, bytesRead);
stoicancristi 1:d3e12393b71d 154 }
stoicancristi 1:d3e12393b71d 155
stoicancristi 1:d3e12393b71d 156
stoicancristi 1:d3e12393b71d 157 void computeCommand()
stoicancristi 1:d3e12393b71d 158 {
stoicancristi 1:d3e12393b71d 159 static PIDClass PID(0.05,0.02,0.01,0.6);
stoicancristi 1:d3e12393b71d 160 //float out;
stoicancristi 1:d3e12393b71d 161
stoicancristi 1:d3e12393b71d 162 if(apply_PIDCommand){
stoicancristi 1:d3e12393b71d 163 float out = PID.ComputeCommand(input.read());
stoicancristi 1:d3e12393b71d 164 PIDGlobalCommand = out;
stoicancristi 1:d3e12393b71d 165 }else
stoicancristi 1:d3e12393b71d 166 {
stoicancristi 1:d3e12393b71d 167 PIDGlobalCommand = 0;
stoicancristi 1:d3e12393b71d 168 }
stoicancristi 1:d3e12393b71d 169 }
stoicancristi 1:d3e12393b71d 170
stoicancristi 1:d3e12393b71d 171 void computeRSTCommand()
stoicancristi 1:d3e12393b71d 172 {
stoicancristi 1:d3e12393b71d 173 static RST_aditionalData PolinomialGrades;
stoicancristi 1:d3e12393b71d 174 PolinomialGrades.gradR = 3;
stoicancristi 1:d3e12393b71d 175 PolinomialGrades.gradT = 4;
stoicancristi 1:d3e12393b71d 176 PolinomialGrades.gradS = 5;
stoicancristi 1:d3e12393b71d 177 PolinomialGrades.setpoint = 0.8;
stoicancristi 1:d3e12393b71d 178 static uint8_t R_pol[10] = {1,2,3,3,0,0,0,0,0,0};
stoicancristi 1:d3e12393b71d 179 static uint8_t T_pol[10] = {1,3,2,3,5,0,0,0,0,0};
stoicancristi 1:d3e12393b71d 180 static uint8_t S_pol[10] = {1,5,3,2,1,5,0,0,0,0};
stoicancristi 1:d3e12393b71d 181 static RST new_RST((float*)R_pol, (float*)T_pol, (float*)S_pol, PolinomialGrades);
stoicancristi 1:d3e12393b71d 182 if(apply_RSTCommand)
stoicancristi 1:d3e12393b71d 183 {
stoicancristi 1:d3e12393b71d 184 float out = new_RST.ComputeCommand(input.read());
stoicancristi 1:d3e12393b71d 185 RSTGlobalCommand = out;
stoicancristi 1:d3e12393b71d 186 }else
stoicancristi 1:d3e12393b71d 187 {
stoicancristi 1:d3e12393b71d 188 RSTGlobalCommand = 0;
stoicancristi 1:d3e12393b71d 189 }
stoicancristi 1:d3e12393b71d 190 }
stoicancristi 1:d3e12393b71d 191
stoicancristi 1:d3e12393b71d 192 void decode(uint8_t* buf)
stoicancristi 1:d3e12393b71d 193 {
stoicancristi 1:d3e12393b71d 194 char myBuf[TXRX_BUF_LEN];
stoicancristi 1:d3e12393b71d 195 switch(buf[0])
stoicancristi 1:d3e12393b71d 196 {
stoicancristi 1:d3e12393b71d 197 case 'P':
stoicancristi 1:d3e12393b71d 198 {
stoicancristi 1:d3e12393b71d 199 sprintf(myBuf,"command=%2f", PIDGlobalCommand);
stoicancristi 1:d3e12393b71d 200 WriteBuffer(myBuf);
stoicancristi 1:d3e12393b71d 201 }
stoicancristi 1:d3e12393b71d 202 case 'm':
stoicancristi 1:d3e12393b71d 203 {
stoicancristi 1:d3e12393b71d 204 sprintf(myBuf,"ReadADC=%2f",input.read());
stoicancristi 1:d3e12393b71d 205 WriteBuffer(myBuf);
stoicancristi 1:d3e12393b71d 206 }
stoicancristi 1:d3e12393b71d 207 case 'R':
stoicancristi 1:d3e12393b71d 208 {
stoicancristi 1:d3e12393b71d 209 sprintf(myBuf,"RSTcommand=%2f", RSTGlobalCommand);
stoicancristi 1:d3e12393b71d 210 WriteBuffer(myBuf);
stoicancristi 1:d3e12393b71d 211 }
stoicancristi 1:d3e12393b71d 212 case 's':
stoicancristi 1:d3e12393b71d 213 {
stoicancristi 1:d3e12393b71d 214 sprintf(myBuf,"Safe mode=%2f", PIDGlobalCommand);
stoicancristi 1:d3e12393b71d 215 WriteBuffer(myBuf);
stoicancristi 1:d3e12393b71d 216 }
stoicancristi 1:d3e12393b71d 217 default:
stoicancristi 1:d3e12393b71d 218 {
stoicancristi 1:d3e12393b71d 219 sprintf(myBuf,"Default Case %d", 0);
stoicancristi 1:d3e12393b71d 220 WriteBuffer(myBuf);
stoicancristi 1:d3e12393b71d 221 }
stoicancristi 1:d3e12393b71d 222 }
stoicancristi 1:d3e12393b71d 223 }
stoicancristi 1:d3e12393b71d 224
stoicancristi 1:d3e12393b71d 225 void decode_state_machine()
stoicancristi 1:d3e12393b71d 226 {
stoicancristi 1:d3e12393b71d 227 char buf;
stoicancristi 1:d3e12393b71d 228 switch(sm_states)
stoicancristi 1:d3e12393b71d 229 {
stoicancristi 1:d3e12393b71d 230 case shut_down:
stoicancristi 1:d3e12393b71d 231 {
stoicancristi 1:d3e12393b71d 232 //do nothing
stoicancristi 1:d3e12393b71d 233 apply_PIDCommand = false;
stoicancristi 1:d3e12393b71d 234 apply_RSTCommand = false;
stoicancristi 1:d3e12393b71d 235 break;
stoicancristi 1:d3e12393b71d 236 }
stoicancristi 1:d3e12393b71d 237 case safe_state:
stoicancristi 1:d3e12393b71d 238 {
stoicancristi 1:d3e12393b71d 239 apply_PIDCommand = false;
stoicancristi 1:d3e12393b71d 240 apply_RSTCommand = false;
stoicancristi 1:d3e12393b71d 241 buf = 's';
stoicancristi 1:d3e12393b71d 242 decode((uint8_t*)buf);
stoicancristi 1:d3e12393b71d 243 break;
stoicancristi 1:d3e12393b71d 244 }
stoicancristi 1:d3e12393b71d 245 case monitoring:
stoicancristi 1:d3e12393b71d 246 {
stoicancristi 1:d3e12393b71d 247 apply_PIDCommand = false;
stoicancristi 1:d3e12393b71d 248 apply_RSTCommand = false;
stoicancristi 1:d3e12393b71d 249 buf = 'm';
stoicancristi 1:d3e12393b71d 250 decode((uint8_t*)buf);
stoicancristi 1:d3e12393b71d 251 break;
stoicancristi 1:d3e12393b71d 252 }
stoicancristi 1:d3e12393b71d 253 case local_control_PID:
stoicancristi 1:d3e12393b71d 254 {
stoicancristi 1:d3e12393b71d 255 apply_PIDCommand = true;
stoicancristi 1:d3e12393b71d 256 apply_RSTCommand = false;
stoicancristi 1:d3e12393b71d 257 buf = 'P';
stoicancristi 1:d3e12393b71d 258 decode((uint8_t*)buf);
stoicancristi 1:d3e12393b71d 259 break;
stoicancristi 1:d3e12393b71d 260 }
stoicancristi 1:d3e12393b71d 261 case local_control_RST:
stoicancristi 1:d3e12393b71d 262 {
stoicancristi 1:d3e12393b71d 263 apply_PIDCommand = false;
stoicancristi 1:d3e12393b71d 264 apply_RSTCommand = true;
stoicancristi 1:d3e12393b71d 265 buf = 'R';
stoicancristi 1:d3e12393b71d 266 decode((uint8_t*)buf);
stoicancristi 1:d3e12393b71d 267 break;
stoicancristi 1:d3e12393b71d 268 }
stoicancristi 1:d3e12393b71d 269 }
stoicancristi 1:d3e12393b71d 270 }
stoicancristi 1:d3e12393b71d 271
stoicancristi 1:d3e12393b71d 272 void conn_state_machine(uint8_t *letter, uint8_t *letter_2)
stoicancristi 1:d3e12393b71d 273 {
stoicancristi 1:d3e12393b71d 274 switch(letter[0]){
stoicancristi 1:d3e12393b71d 275 case 'a':
stoicancristi 1:d3e12393b71d 276 {
stoicancristi 1:d3e12393b71d 277 connectionUpdate(start_advertising);
stoicancristi 1:d3e12393b71d 278 sm_states = safe_state;
stoicancristi 1:d3e12393b71d 279 break;
stoicancristi 1:d3e12393b71d 280 }
stoicancristi 1:d3e12393b71d 281 case 'c':
stoicancristi 1:d3e12393b71d 282 {
stoicancristi 1:d3e12393b71d 283 connectionUpdate(connected);
stoicancristi 1:d3e12393b71d 284 switch(letter_2[0])
stoicancristi 1:d3e12393b71d 285 {
stoicancristi 1:d3e12393b71d 286 case 'm':
stoicancristi 1:d3e12393b71d 287 {
stoicancristi 1:d3e12393b71d 288 sm_states = monitoring;
stoicancristi 1:d3e12393b71d 289 break;
stoicancristi 1:d3e12393b71d 290 }
stoicancristi 1:d3e12393b71d 291 case 'P':
stoicancristi 1:d3e12393b71d 292 {
stoicancristi 1:d3e12393b71d 293 sm_states = local_control_PID;
stoicancristi 1:d3e12393b71d 294 break;
stoicancristi 1:d3e12393b71d 295 }
stoicancristi 1:d3e12393b71d 296 case 'R':
stoicancristi 1:d3e12393b71d 297 {
stoicancristi 1:d3e12393b71d 298 sm_states = local_control_RST;
stoicancristi 1:d3e12393b71d 299 break;
stoicancristi 1:d3e12393b71d 300 }
stoicancristi 1:d3e12393b71d 301 case 'r':
stoicancristi 1:d3e12393b71d 302 {
stoicancristi 1:d3e12393b71d 303 sm_states = remote_control;
stoicancristi 1:d3e12393b71d 304 break;
stoicancristi 1:d3e12393b71d 305 }
stoicancristi 1:d3e12393b71d 306 }
stoicancristi 1:d3e12393b71d 307 break;
stoicancristi 1:d3e12393b71d 308 }
stoicancristi 1:d3e12393b71d 309 case 's':
stoicancristi 1:d3e12393b71d 310 {
stoicancristi 1:d3e12393b71d 311 connectionUpdate(stop_advertising);
stoicancristi 1:d3e12393b71d 312 sm_states = safe_state;
stoicancristi 1:d3e12393b71d 313 }
stoicancristi 1:d3e12393b71d 314 case 'd':
stoicancristi 1:d3e12393b71d 315 {
stoicancristi 1:d3e12393b71d 316 connectionUpdate(stop_connection);
stoicancristi 1:d3e12393b71d 317 sm_states = shut_down;
stoicancristi 1:d3e12393b71d 318 }
stoicancristi 1:d3e12393b71d 319 }
stoicancristi 0:1f4d5c5491b8 320 }
stoicancristi 0:1f4d5c5491b8 321
stoicancristi 0:1f4d5c5491b8 322 void WrittenHandler(const GattWriteCallbackParams *Handler)
stoicancristi 0:1f4d5c5491b8 323 {
stoicancristi 0:1f4d5c5491b8 324 uint8_t buf[TXRX_BUF_LEN];
stoicancristi 0:1f4d5c5491b8 325 uint16_t bytesRead, index;
stoicancristi 0:1f4d5c5491b8 326
stoicancristi 0:1f4d5c5491b8 327 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
stoicancristi 0:1f4d5c5491b8 328 {
stoicancristi 0:1f4d5c5491b8 329 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
stoicancristi 0:1f4d5c5491b8 330 memset(txPayload, 0, TXRX_BUF_LEN);
stoicancristi 1:d3e12393b71d 331 memcpy(txPayload, buf, TXRX_BUF_LEN);
stoicancristi 1:d3e12393b71d 332 //decode();
stoicancristi 1:d3e12393b71d 333 //WriteBuffer(GlobalCommand);
stoicancristi 1:d3e12393b71d 334 conn_state_machine(&buf[0], &buf[1]);
stoicancristi 0:1f4d5c5491b8 335 }
stoicancristi 0:1f4d5c5491b8 336 }
stoicancristi 0:1f4d5c5491b8 337
stoicancristi 0:1f4d5c5491b8 338 void uartCB(void)
stoicancristi 0:1f4d5c5491b8 339 {
stoicancristi 0:1f4d5c5491b8 340 while(pc.readable())
stoicancristi 0:1f4d5c5491b8 341 {
stoicancristi 0:1f4d5c5491b8 342 rx_buf[rx_len++] = pc.getc();
stoicancristi 0:1f4d5c5491b8 343 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
stoicancristi 0:1f4d5c5491b8 344 {
stoicancristi 0:1f4d5c5491b8 345 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
stoicancristi 0:1f4d5c5491b8 346 rx_len = 0;
stoicancristi 0:1f4d5c5491b8 347 break;
stoicancristi 0:1f4d5c5491b8 348 }
stoicancristi 0:1f4d5c5491b8 349 }
stoicancristi 0:1f4d5c5491b8 350 }
stoicancristi 0:1f4d5c5491b8 351
stoicancristi 0:1f4d5c5491b8 352 int main(void)
stoicancristi 0:1f4d5c5491b8 353 {
stoicancristi 0:1f4d5c5491b8 354 ble.init();
stoicancristi 0:1f4d5c5491b8 355 ble.onDisconnection(disconnectionCallback);
stoicancristi 1:d3e12393b71d 356 ble.onConnection(connectionCallback);
stoicancristi 0:1f4d5c5491b8 357 ble.onDataWritten(WrittenHandler);
stoicancristi 0:1f4d5c5491b8 358
stoicancristi 0:1f4d5c5491b8 359 pc.baud(9600);
stoicancristi 0:1f4d5c5491b8 360 pc.printf("SimpleChat Init \r\n");
stoicancristi 0:1f4d5c5491b8 361
stoicancristi 0:1f4d5c5491b8 362 pc.attach( uartCB , pc.RxIrq);
stoicancristi 0:1f4d5c5491b8 363 // setup advertising
stoicancristi 0:1f4d5c5491b8 364 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
stoicancristi 0:1f4d5c5491b8 365 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
stoicancristi 0:1f4d5c5491b8 366 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
stoicancristi 0:1f4d5c5491b8 367 (const uint8_t *)"Gigel", sizeof("Gigel") - 1);
stoicancristi 0:1f4d5c5491b8 368 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
stoicancristi 0:1f4d5c5491b8 369 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
stoicancristi 0:1f4d5c5491b8 370 // 100ms; in multiples of 0.625ms.
stoicancristi 0:1f4d5c5491b8 371 ble.setAdvertisingInterval(160);
stoicancristi 0:1f4d5c5491b8 372
stoicancristi 0:1f4d5c5491b8 373 ble.addService(uartService);
stoicancristi 0:1f4d5c5491b8 374
stoicancristi 0:1f4d5c5491b8 375 ble.startAdvertising();
stoicancristi 1:d3e12393b71d 376 //periodicActions.attach(&returnCommand, GlobalTime);
stoicancristi 1:d3e12393b71d 377 periodicCommandPID.attach(&computeCommand, GlobalSamplingTime);
stoicancristi 0:1f4d5c5491b8 378 pc.printf("Advertising Start \r\n");
stoicancristi 0:1f4d5c5491b8 379 while(1)
stoicancristi 0:1f4d5c5491b8 380 {
stoicancristi 1:d3e12393b71d 381 decode_state_machine();
stoicancristi 1:d3e12393b71d 382 ble.waitForEvent();
stoicancristi 0:1f4d5c5491b8 383 }
stoicancristi 1:d3e12393b71d 384 }