Pour Mike le faible

Dependencies:   BLE_API BMP085 mbed nRF51822

Fork of nRF51822_SimpleControls by RedBearLab

Committer:
tarchoun78
Date:
Wed Dec 09 15:21:33 2015 +0000
Revision:
3:d55e6b560480
Parent:
2:bbcdd23ba9ba
Pour MIKE le faible

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:f03072e32ed3 1 /*
RedBearLab 1:f03072e32ed3 2
RedBearLab 1:f03072e32ed3 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:f03072e32ed3 4
RedBearLab 1:f03072e32ed3 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:f03072e32ed3 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:f03072e32ed3 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:f03072e32ed3 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:f03072e32ed3 9 subject to the following conditions:
RedBearLab 1:f03072e32ed3 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:f03072e32ed3 11
RedBearLab 1:f03072e32ed3 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:f03072e32ed3 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:f03072e32ed3 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:f03072e32ed3 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:f03072e32ed3 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:f03072e32ed3 17
RedBearLab 1:f03072e32ed3 18 */
RedBearLab 0:dfcebc1e442a 19
RedBearLab 0:dfcebc1e442a 20 #include "mbed.h"
RedBearLab 2:bbcdd23ba9ba 21 #include "ble/BLE.h"
RedBearLab 0:dfcebc1e442a 22 #include "Servo.h"
RedBearLab 2:bbcdd23ba9ba 23 #include "GattCallbackParamTypes.h"
tarchoun78 3:d55e6b560480 24 #include "BMP085.h"
RedBearLab 0:dfcebc1e442a 25
RedBearLab 0:dfcebc1e442a 26 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:dfcebc1e442a 27 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:dfcebc1e442a 28 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:dfcebc1e442a 29
RedBearLab 0:dfcebc1e442a 30 #define TXRX_BUF_LEN 20
RedBearLab 0:dfcebc1e442a 31
RedBearLab 0:dfcebc1e442a 32 #define DIGITAL_OUT_PIN P0_17 //D7
RedBearLab 0:dfcebc1e442a 33 #define DIGITAL_IN_PIN P0_5 //A4
RedBearLab 0:dfcebc1e442a 34 #define PWM_PIN P0_16 //D6
RedBearLab 0:dfcebc1e442a 35 #define SERVO_PIN P0_14 //D10
RedBearLab 0:dfcebc1e442a 36 #define ANALOG_IN_PIN P0_6 //A5
RedBearLab 0:dfcebc1e442a 37
RedBearLab 2:bbcdd23ba9ba 38 BLE ble;
RedBearLab 0:dfcebc1e442a 39
RedBearLab 0:dfcebc1e442a 40 DigitalOut LED_SET(DIGITAL_OUT_PIN);
RedBearLab 0:dfcebc1e442a 41 DigitalIn BUTTON(DIGITAL_IN_PIN);
RedBearLab 0:dfcebc1e442a 42 PwmOut PWM(PWM_PIN);
RedBearLab 0:dfcebc1e442a 43 AnalogIn ANALOG(ANALOG_IN_PIN);
RedBearLab 0:dfcebc1e442a 44 Servo MYSERVO(SERVO_PIN);
RedBearLab 0:dfcebc1e442a 45
RedBearLab 0:dfcebc1e442a 46 Serial pc(USBTX, USBRX);
RedBearLab 0:dfcebc1e442a 47
RedBearLab 0:dfcebc1e442a 48 static uint8_t analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 49 static uint8_t old_state = 0;
RedBearLab 0:dfcebc1e442a 50
RedBearLab 0:dfcebc1e442a 51 // The Nordic UART Service
RedBearLab 0:dfcebc1e442a 52 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:dfcebc1e442a 53 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:dfcebc1e442a 54 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:dfcebc1e442a 55 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:dfcebc1e442a 56
RedBearLab 0:dfcebc1e442a 57
RedBearLab 0:dfcebc1e442a 58 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:dfcebc1e442a 59 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:dfcebc1e442a 60
RedBearLab 0:dfcebc1e442a 61 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:dfcebc1e442a 62 //static uint8_t rx_len=0;
RedBearLab 0:dfcebc1e442a 63
RedBearLab 0:dfcebc1e442a 64
RedBearLab 0:dfcebc1e442a 65 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:dfcebc1e442a 66
RedBearLab 0:dfcebc1e442a 67 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:dfcebc1e442a 68
RedBearLab 0:dfcebc1e442a 69 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:dfcebc1e442a 70
RedBearLab 0:dfcebc1e442a 71 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:dfcebc1e442a 72
RedBearLab 0:dfcebc1e442a 73
RedBearLab 0:dfcebc1e442a 74
RedBearLab 0:dfcebc1e442a 75 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:dfcebc1e442a 76 {
RedBearLab 0:dfcebc1e442a 77 pc.printf("Disconnected \r\n");
RedBearLab 0:dfcebc1e442a 78 pc.printf("Restart advertising \r\n");
RedBearLab 0:dfcebc1e442a 79 ble.startAdvertising();
RedBearLab 0:dfcebc1e442a 80 }
RedBearLab 0:dfcebc1e442a 81
RedBearLab 2:bbcdd23ba9ba 82 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:dfcebc1e442a 83 {
RedBearLab 0:dfcebc1e442a 84 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:dfcebc1e442a 85 uint16_t bytesRead, index;
RedBearLab 0:dfcebc1e442a 86
tarchoun78 3:d55e6b560480 87
RedBearLab 2:bbcdd23ba9ba 88 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:dfcebc1e442a 89 {
RedBearLab 0:dfcebc1e442a 90 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:dfcebc1e442a 91 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:dfcebc1e442a 92 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:dfcebc1e442a 93
RedBearLab 0:dfcebc1e442a 94 for(index=0; index<bytesRead; index++)
RedBearLab 0:dfcebc1e442a 95 pc.putc(buf[index]);
RedBearLab 0:dfcebc1e442a 96
RedBearLab 0:dfcebc1e442a 97 if(buf[0] == 0x01)
RedBearLab 0:dfcebc1e442a 98 {
tarchoun78 3:d55e6b560480 99 if(buf[1] == 0x01){
tarchoun78 3:d55e6b560480 100 while(buf[1] == 0x01){
tarchoun78 3:d55e6b560480 101 // on relit ce qui est envoyé pour être sur qu'on veut toujours faire clignoter la LED VIOLETTE
tarchoun78 3:d55e6b560480 102 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
tarchoun78 3:d55e6b560480 103 memset(txPayload, 0, TXRX_BUF_LEN);
tarchoun78 3:d55e6b560480 104 memcpy(txPayload, buf, TXRX_BUF_LEN);
tarchoun78 3:d55e6b560480 105
tarchoun78 3:d55e6b560480 106 for(index=0; index<bytesRead; index++)
tarchoun78 3:d55e6b560480 107 pc.putc(buf[index]);
tarchoun78 3:d55e6b560480 108 // Pour ne pas refaire clignoter alors que l'ordre a été donnée d'arreter
tarchoun78 3:d55e6b560480 109 if(buf[1] == 0x01){
tarchoun78 3:d55e6b560480 110 LED_SET = 1;
tarchoun78 3:d55e6b560480 111 wait_ms(100);
tarchoun78 3:d55e6b560480 112 LED_SET = 0;
tarchoun78 3:d55e6b560480 113 wait_ms(100);
tarchoun78 3:d55e6b560480 114 }
tarchoun78 3:d55e6b560480 115 else;
tarchoun78 3:d55e6b560480 116 }
tarchoun78 3:d55e6b560480 117 }
RedBearLab 0:dfcebc1e442a 118 else
RedBearLab 0:dfcebc1e442a 119 LED_SET = 0;
RedBearLab 0:dfcebc1e442a 120 }
RedBearLab 0:dfcebc1e442a 121 else if(buf[0] == 0xA0)
RedBearLab 0:dfcebc1e442a 122 {
RedBearLab 0:dfcebc1e442a 123 if(buf[1] == 0x01)
RedBearLab 0:dfcebc1e442a 124 analog_enabled = 1;
RedBearLab 0:dfcebc1e442a 125 else
RedBearLab 0:dfcebc1e442a 126 analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 127 }
RedBearLab 0:dfcebc1e442a 128 else if(buf[0] == 0x02)
RedBearLab 0:dfcebc1e442a 129 {
RedBearLab 0:dfcebc1e442a 130 float value = (float)buf[1]/255;
RedBearLab 0:dfcebc1e442a 131 PWM = value;
RedBearLab 0:dfcebc1e442a 132 }
RedBearLab 0:dfcebc1e442a 133 else if(buf[0] == 0x03)
RedBearLab 0:dfcebc1e442a 134 {
RedBearLab 0:dfcebc1e442a 135 MYSERVO.write(buf[1]);
RedBearLab 0:dfcebc1e442a 136 }
RedBearLab 0:dfcebc1e442a 137 else if(buf[0] == 0x04)
RedBearLab 0:dfcebc1e442a 138 {
RedBearLab 0:dfcebc1e442a 139 analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 140 PWM = 0;
RedBearLab 0:dfcebc1e442a 141 MYSERVO.write(0);
RedBearLab 0:dfcebc1e442a 142 LED_SET = 0;
RedBearLab 0:dfcebc1e442a 143 old_state = 0;
RedBearLab 0:dfcebc1e442a 144 }
RedBearLab 0:dfcebc1e442a 145
RedBearLab 0:dfcebc1e442a 146 }
RedBearLab 0:dfcebc1e442a 147 }
RedBearLab 0:dfcebc1e442a 148 /*
RedBearLab 0:dfcebc1e442a 149 void uartCB(void)
RedBearLab 0:dfcebc1e442a 150 {
RedBearLab 0:dfcebc1e442a 151 while(pc.readable())
RedBearLab 0:dfcebc1e442a 152 {
RedBearLab 0:dfcebc1e442a 153 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:dfcebc1e442a 154 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:dfcebc1e442a 155 {
RedBearLab 0:dfcebc1e442a 156 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:dfcebc1e442a 157 pc.printf("RecHandler \r\n");
RedBearLab 0:dfcebc1e442a 158 pc.printf("Length: ");
RedBearLab 0:dfcebc1e442a 159 pc.putc(rx_len);
RedBearLab 0:dfcebc1e442a 160 pc.printf("\r\n");
RedBearLab 0:dfcebc1e442a 161 rx_len = 0;
RedBearLab 0:dfcebc1e442a 162 break;
RedBearLab 0:dfcebc1e442a 163 }
RedBearLab 0:dfcebc1e442a 164 }
RedBearLab 0:dfcebc1e442a 165 }
RedBearLab 0:dfcebc1e442a 166 */
RedBearLab 0:dfcebc1e442a 167 void m_status_check_handle(void)
RedBearLab 0:dfcebc1e442a 168 {
RedBearLab 0:dfcebc1e442a 169 uint8_t buf[3];
RedBearLab 0:dfcebc1e442a 170 if (analog_enabled) // if analog reading enabled
RedBearLab 0:dfcebc1e442a 171 {
RedBearLab 0:dfcebc1e442a 172 // Read and send out
RedBearLab 0:dfcebc1e442a 173 float s = ANALOG;
RedBearLab 0:dfcebc1e442a 174 uint16_t value = s*1024;
RedBearLab 0:dfcebc1e442a 175 buf[0] = (0x0B);
RedBearLab 0:dfcebc1e442a 176 buf[1] = (value >> 8);
RedBearLab 0:dfcebc1e442a 177 buf[2] = (value);
RedBearLab 0:dfcebc1e442a 178 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 179 }
RedBearLab 0:dfcebc1e442a 180
RedBearLab 0:dfcebc1e442a 181 // If digital in changes, report the state
RedBearLab 0:dfcebc1e442a 182 if (BUTTON != old_state)
RedBearLab 0:dfcebc1e442a 183 {
RedBearLab 0:dfcebc1e442a 184 old_state = BUTTON;
RedBearLab 0:dfcebc1e442a 185
RedBearLab 0:dfcebc1e442a 186 if (BUTTON == 1)
RedBearLab 0:dfcebc1e442a 187 {
RedBearLab 0:dfcebc1e442a 188 buf[0] = (0x0A);
RedBearLab 0:dfcebc1e442a 189 buf[1] = (0x01);
RedBearLab 0:dfcebc1e442a 190 buf[2] = (0x00);
RedBearLab 0:dfcebc1e442a 191 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 192 }
RedBearLab 0:dfcebc1e442a 193 else
RedBearLab 0:dfcebc1e442a 194 {
RedBearLab 0:dfcebc1e442a 195 buf[0] = (0x0A);
RedBearLab 0:dfcebc1e442a 196 buf[1] = (0x00);
RedBearLab 0:dfcebc1e442a 197 buf[2] = (0x00);
RedBearLab 0:dfcebc1e442a 198 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 199 }
RedBearLab 0:dfcebc1e442a 200 }
RedBearLab 0:dfcebc1e442a 201 }
RedBearLab 0:dfcebc1e442a 202
RedBearLab 0:dfcebc1e442a 203
RedBearLab 0:dfcebc1e442a 204 int main(void)
RedBearLab 0:dfcebc1e442a 205 {
tarchoun78 3:d55e6b560480 206 BMP085 barometre(D4,D5);
tarchoun78 3:d55e6b560480 207 float temp,press;
RedBearLab 0:dfcebc1e442a 208 Ticker ticker;
RedBearLab 0:dfcebc1e442a 209 ticker.attach_us(m_status_check_handle, 200000);
RedBearLab 0:dfcebc1e442a 210
RedBearLab 0:dfcebc1e442a 211 ble.init();
RedBearLab 0:dfcebc1e442a 212 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:dfcebc1e442a 213 ble.onDataWritten(WrittenHandler);
RedBearLab 0:dfcebc1e442a 214
RedBearLab 0:dfcebc1e442a 215 pc.baud(9600);
RedBearLab 0:dfcebc1e442a 216 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:dfcebc1e442a 217
RedBearLab 0:dfcebc1e442a 218 //pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:dfcebc1e442a 219
RedBearLab 0:dfcebc1e442a 220 // setup advertising
RedBearLab 0:dfcebc1e442a 221 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:dfcebc1e442a 222 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:dfcebc1e442a 223 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
tarchoun78 3:d55e6b560480 224 (const uint8_t *)"MALOU", sizeof("MALOU") - 1);
RedBearLab 0:dfcebc1e442a 225 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:dfcebc1e442a 226 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:dfcebc1e442a 227 // 100ms; in multiples of 0.625ms.
RedBearLab 0:dfcebc1e442a 228 ble.setAdvertisingInterval(160);
RedBearLab 0:dfcebc1e442a 229
RedBearLab 0:dfcebc1e442a 230 ble.addService(uartService);
RedBearLab 0:dfcebc1e442a 231
RedBearLab 0:dfcebc1e442a 232 ble.startAdvertising();
RedBearLab 0:dfcebc1e442a 233
RedBearLab 0:dfcebc1e442a 234 pc.printf("Advertising Start \r\n");
RedBearLab 0:dfcebc1e442a 235
RedBearLab 0:dfcebc1e442a 236 while(1)
RedBearLab 0:dfcebc1e442a 237 {
RedBearLab 0:dfcebc1e442a 238 ble.waitForEvent();
tarchoun78 3:d55e6b560480 239 barometre.update ();
tarchoun78 3:d55e6b560480 240 temp = barometre.get_temperature();
tarchoun78 3:d55e6b560480 241 press = barometre.get_pressure() ;
tarchoun78 3:d55e6b560480 242 printf("Temperature = %f C et Pression = %f Pa",temp,press);
RedBearLab 0:dfcebc1e442a 243 }
RedBearLab 0:dfcebc1e442a 244 }
RedBearLab 0:dfcebc1e442a 245
RedBearLab 0:dfcebc1e442a 246
RedBearLab 0:dfcebc1e442a 247
RedBearLab 0:dfcebc1e442a 248
RedBearLab 0:dfcebc1e442a 249
RedBearLab 0:dfcebc1e442a 250
RedBearLab 0:dfcebc1e442a 251
RedBearLab 0:dfcebc1e442a 252
RedBearLab 0:dfcebc1e442a 253
RedBearLab 0:dfcebc1e442a 254
RedBearLab 0:dfcebc1e442a 255
RedBearLab 0:dfcebc1e442a 256
RedBearLab 0:dfcebc1e442a 257
RedBearLab 0:dfcebc1e442a 258
RedBearLab 0:dfcebc1e442a 259
RedBearLab 0:dfcebc1e442a 260
RedBearLab 0:dfcebc1e442a 261
RedBearLab 0:dfcebc1e442a 262
RedBearLab 0:dfcebc1e442a 263
RedBearLab 0:dfcebc1e442a 264
RedBearLab 0:dfcebc1e442a 265
RedBearLab 0:dfcebc1e442a 266
RedBearLab 0:dfcebc1e442a 267
RedBearLab 0:dfcebc1e442a 268
RedBearLab 0:dfcebc1e442a 269
RedBearLab 0:dfcebc1e442a 270
RedBearLab 0:dfcebc1e442a 271
RedBearLab 0:dfcebc1e442a 272
RedBearLab 0:dfcebc1e442a 273
RedBearLab 0:dfcebc1e442a 274
RedBearLab 0:dfcebc1e442a 275
RedBearLab 0:dfcebc1e442a 276