Repo for th IOT course
Dependencies: BLE_API BMP085 mbed nRF51822
Fork of nRF51822_SimpleControls by
Caca
main.cpp@4:762404f17d9d, 2017-05-09 (annotated)
- Committer:
- Cornetin
- Date:
- Tue May 09 08:01:53 2017 +0000
- Revision:
- 4:762404f17d9d
- Parent:
- 3:823f105078c7
led blinking working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:f03072e32ed3 | 1 | /* |
RedBearLab | 1:f03072e32ed3 | 2 | |
RedBearLab | 1:f03072e32ed3 | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:f03072e32ed3 | 4 | |
Cornetin | 4:762404f17d9d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Cornetin | 4:762404f17d9d | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
Cornetin | 4:762404f17d9d | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Cornetin | 4:762404f17d9d | 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 | |
Cornetin | 4:762404f17d9d | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
Cornetin | 4:762404f17d9d | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
Cornetin | 4:762404f17d9d | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
Cornetin | 4:762404f17d9d | 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" |
RedBearLab | 0:dfcebc1e442a | 24 | |
RedBearLab | 0:dfcebc1e442a | 25 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:dfcebc1e442a | 26 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:dfcebc1e442a | 27 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:dfcebc1e442a | 28 | |
RedBearLab | 0:dfcebc1e442a | 29 | #define TXRX_BUF_LEN 20 |
RedBearLab | 0:dfcebc1e442a | 30 | |
Cornetin | 4:762404f17d9d | 31 | #define DIGITAL_OUT_PIN_LED P0_17 //D7 |
Cornetin | 4:762404f17d9d | 32 | #define DIGITAL_OUT_PIN_BARO P0_10 //D2 |
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 |
Cornetin | 4:762404f17d9d | 36 | #define ANALOG_IN_PIN P0_1 //A0 |
RedBearLab | 0:dfcebc1e442a | 37 | |
RedBearLab | 2:bbcdd23ba9ba | 38 | BLE ble; |
RedBearLab | 0:dfcebc1e442a | 39 | |
Cornetin | 4:762404f17d9d | 40 | DigitalOut LED_SET(DIGITAL_OUT_PIN_LED); |
Cornetin | 4:762404f17d9d | 41 | bool LED_BLINK; |
Cornetin | 4:762404f17d9d | 42 | |
Cornetin | 4:762404f17d9d | 43 | |
RedBearLab | 0:dfcebc1e442a | 44 | DigitalIn BUTTON(DIGITAL_IN_PIN); |
RedBearLab | 0:dfcebc1e442a | 45 | PwmOut PWM(PWM_PIN); |
RedBearLab | 0:dfcebc1e442a | 46 | AnalogIn ANALOG(ANALOG_IN_PIN); |
RedBearLab | 0:dfcebc1e442a | 47 | Servo MYSERVO(SERVO_PIN); |
RedBearLab | 0:dfcebc1e442a | 48 | |
RedBearLab | 0:dfcebc1e442a | 49 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:dfcebc1e442a | 50 | |
RedBearLab | 0:dfcebc1e442a | 51 | static uint8_t analog_enabled = 0; |
RedBearLab | 0:dfcebc1e442a | 52 | static uint8_t old_state = 0; |
RedBearLab | 0:dfcebc1e442a | 53 | |
RedBearLab | 0:dfcebc1e442a | 54 | // The Nordic UART Service |
RedBearLab | 0:dfcebc1e442a | 55 | 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 | 56 | 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 | 57 | 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 | 58 | 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 | 59 | |
RedBearLab | 0:dfcebc1e442a | 60 | |
RedBearLab | 0:dfcebc1e442a | 61 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:dfcebc1e442a | 62 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:dfcebc1e442a | 63 | |
RedBearLab | 0:dfcebc1e442a | 64 | //static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:dfcebc1e442a | 65 | //static uint8_t rx_len=0; |
RedBearLab | 0:dfcebc1e442a | 66 | |
RedBearLab | 0:dfcebc1e442a | 67 | |
RedBearLab | 0:dfcebc1e442a | 68 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
Cornetin | 4:762404f17d9d | 69 | |
RedBearLab | 0:dfcebc1e442a | 70 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
Cornetin | 4:762404f17d9d | 71 | |
RedBearLab | 0:dfcebc1e442a | 72 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:dfcebc1e442a | 73 | |
RedBearLab | 0:dfcebc1e442a | 74 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:dfcebc1e442a | 75 | |
RedBearLab | 0:dfcebc1e442a | 76 | |
RedBearLab | 0:dfcebc1e442a | 77 | |
RedBearLab | 3:823f105078c7 | 78 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
RedBearLab | 0:dfcebc1e442a | 79 | { |
RedBearLab | 0:dfcebc1e442a | 80 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:dfcebc1e442a | 81 | pc.printf("Restart advertising \r\n"); |
RedBearLab | 0:dfcebc1e442a | 82 | ble.startAdvertising(); |
RedBearLab | 0:dfcebc1e442a | 83 | } |
RedBearLab | 0:dfcebc1e442a | 84 | |
RedBearLab | 2:bbcdd23ba9ba | 85 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
Cornetin | 4:762404f17d9d | 86 | { |
RedBearLab | 0:dfcebc1e442a | 87 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:dfcebc1e442a | 88 | uint16_t bytesRead, index; |
Cornetin | 4:762404f17d9d | 89 | |
Cornetin | 4:762404f17d9d | 90 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { |
RedBearLab | 0:dfcebc1e442a | 91 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:dfcebc1e442a | 92 | memset(txPayload, 0, TXRX_BUF_LEN); |
Cornetin | 4:762404f17d9d | 93 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
Cornetin | 4:762404f17d9d | 94 | |
RedBearLab | 0:dfcebc1e442a | 95 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:dfcebc1e442a | 96 | pc.putc(buf[index]); |
Cornetin | 4:762404f17d9d | 97 | |
Cornetin | 4:762404f17d9d | 98 | if(buf[0] == 0x01) { |
Cornetin | 4:762404f17d9d | 99 | if(buf[1] == 0x01){ |
Cornetin | 4:762404f17d9d | 100 | LED_BLINK = 1; |
Cornetin | 4:762404f17d9d | 101 | } |
RedBearLab | 0:dfcebc1e442a | 102 | else |
Cornetin | 4:762404f17d9d | 103 | LED_BLINK = 0; |
Cornetin | 4:762404f17d9d | 104 | } else if(buf[0] == 0xA0) { |
RedBearLab | 0:dfcebc1e442a | 105 | if(buf[1] == 0x01) |
RedBearLab | 0:dfcebc1e442a | 106 | analog_enabled = 1; |
RedBearLab | 0:dfcebc1e442a | 107 | else |
RedBearLab | 0:dfcebc1e442a | 108 | analog_enabled = 0; |
Cornetin | 4:762404f17d9d | 109 | } else if(buf[0] == 0x02) { |
RedBearLab | 0:dfcebc1e442a | 110 | float value = (float)buf[1]/255; |
RedBearLab | 0:dfcebc1e442a | 111 | PWM = value; |
Cornetin | 4:762404f17d9d | 112 | } else if(buf[0] == 0x03) { |
RedBearLab | 0:dfcebc1e442a | 113 | MYSERVO.write(buf[1]); |
Cornetin | 4:762404f17d9d | 114 | } else if(buf[0] == 0x04) { |
RedBearLab | 0:dfcebc1e442a | 115 | analog_enabled = 0; |
RedBearLab | 0:dfcebc1e442a | 116 | PWM = 0; |
RedBearLab | 0:dfcebc1e442a | 117 | MYSERVO.write(0); |
Cornetin | 4:762404f17d9d | 118 | LED_BLINK = 0; |
Cornetin | 4:762404f17d9d | 119 | old_state = 0; |
RedBearLab | 0:dfcebc1e442a | 120 | } |
RedBearLab | 0:dfcebc1e442a | 121 | |
RedBearLab | 0:dfcebc1e442a | 122 | } |
RedBearLab | 0:dfcebc1e442a | 123 | } |
RedBearLab | 0:dfcebc1e442a | 124 | /* |
RedBearLab | 0:dfcebc1e442a | 125 | void uartCB(void) |
Cornetin | 4:762404f17d9d | 126 | { |
Cornetin | 4:762404f17d9d | 127 | while(pc.readable()) |
RedBearLab | 0:dfcebc1e442a | 128 | { |
Cornetin | 4:762404f17d9d | 129 | rx_buf[rx_len++] = pc.getc(); |
RedBearLab | 0:dfcebc1e442a | 130 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
RedBearLab | 0:dfcebc1e442a | 131 | { |
Cornetin | 4:762404f17d9d | 132 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
RedBearLab | 0:dfcebc1e442a | 133 | pc.printf("RecHandler \r\n"); |
RedBearLab | 0:dfcebc1e442a | 134 | pc.printf("Length: "); |
RedBearLab | 0:dfcebc1e442a | 135 | pc.putc(rx_len); |
RedBearLab | 0:dfcebc1e442a | 136 | pc.printf("\r\n"); |
RedBearLab | 0:dfcebc1e442a | 137 | rx_len = 0; |
RedBearLab | 0:dfcebc1e442a | 138 | break; |
RedBearLab | 0:dfcebc1e442a | 139 | } |
RedBearLab | 0:dfcebc1e442a | 140 | } |
RedBearLab | 0:dfcebc1e442a | 141 | } |
RedBearLab | 0:dfcebc1e442a | 142 | */ |
RedBearLab | 0:dfcebc1e442a | 143 | void m_status_check_handle(void) |
Cornetin | 4:762404f17d9d | 144 | { |
RedBearLab | 0:dfcebc1e442a | 145 | uint8_t buf[3]; |
Cornetin | 4:762404f17d9d | 146 | if (analog_enabled) { // if analog reading enabled |
RedBearLab | 0:dfcebc1e442a | 147 | // Read and send out |
RedBearLab | 0:dfcebc1e442a | 148 | float s = ANALOG; |
Cornetin | 4:762404f17d9d | 149 | uint16_t value = s*1024; |
RedBearLab | 0:dfcebc1e442a | 150 | buf[0] = (0x0B); |
RedBearLab | 0:dfcebc1e442a | 151 | buf[1] = (value >> 8); |
RedBearLab | 0:dfcebc1e442a | 152 | buf[2] = (value); |
Cornetin | 4:762404f17d9d | 153 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
RedBearLab | 0:dfcebc1e442a | 154 | } |
Cornetin | 4:762404f17d9d | 155 | |
RedBearLab | 0:dfcebc1e442a | 156 | // If digital in changes, report the state |
Cornetin | 4:762404f17d9d | 157 | if (BUTTON != old_state) { |
RedBearLab | 0:dfcebc1e442a | 158 | old_state = BUTTON; |
Cornetin | 4:762404f17d9d | 159 | |
Cornetin | 4:762404f17d9d | 160 | if (BUTTON == 1) { |
RedBearLab | 0:dfcebc1e442a | 161 | buf[0] = (0x0A); |
RedBearLab | 0:dfcebc1e442a | 162 | buf[1] = (0x01); |
Cornetin | 4:762404f17d9d | 163 | buf[2] = (0x00); |
Cornetin | 4:762404f17d9d | 164 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
Cornetin | 4:762404f17d9d | 165 | } else { |
RedBearLab | 0:dfcebc1e442a | 166 | buf[0] = (0x0A); |
RedBearLab | 0:dfcebc1e442a | 167 | buf[1] = (0x00); |
RedBearLab | 0:dfcebc1e442a | 168 | buf[2] = (0x00); |
Cornetin | 4:762404f17d9d | 169 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
RedBearLab | 0:dfcebc1e442a | 170 | } |
RedBearLab | 0:dfcebc1e442a | 171 | } |
RedBearLab | 0:dfcebc1e442a | 172 | } |
RedBearLab | 0:dfcebc1e442a | 173 | |
RedBearLab | 0:dfcebc1e442a | 174 | |
RedBearLab | 0:dfcebc1e442a | 175 | int main(void) |
Cornetin | 4:762404f17d9d | 176 | { |
RedBearLab | 0:dfcebc1e442a | 177 | Ticker ticker; |
RedBearLab | 0:dfcebc1e442a | 178 | ticker.attach_us(m_status_check_handle, 200000); |
Cornetin | 4:762404f17d9d | 179 | |
RedBearLab | 0:dfcebc1e442a | 180 | ble.init(); |
RedBearLab | 0:dfcebc1e442a | 181 | ble.onDisconnection(disconnectionCallback); |
Cornetin | 4:762404f17d9d | 182 | ble.onDataWritten(WrittenHandler); |
Cornetin | 4:762404f17d9d | 183 | |
RedBearLab | 0:dfcebc1e442a | 184 | pc.baud(9600); |
RedBearLab | 0:dfcebc1e442a | 185 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:dfcebc1e442a | 186 | |
RedBearLab | 0:dfcebc1e442a | 187 | //pc.attach( uartCB , pc.RxIrq); |
Cornetin | 4:762404f17d9d | 188 | |
Cornetin | 4:762404f17d9d | 189 | // setup advertising |
RedBearLab | 0:dfcebc1e442a | 190 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:dfcebc1e442a | 191 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:dfcebc1e442a | 192 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Cornetin | 4:762404f17d9d | 193 | (const uint8_t *)"NRFTRUC", sizeof("NRFTRUC") - 1); |
RedBearLab | 0:dfcebc1e442a | 194 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Cornetin | 4:762404f17d9d | 195 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
Cornetin | 4:762404f17d9d | 196 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:dfcebc1e442a | 197 | ble.setAdvertisingInterval(160); |
RedBearLab | 0:dfcebc1e442a | 198 | |
RedBearLab | 0:dfcebc1e442a | 199 | ble.addService(uartService); |
Cornetin | 4:762404f17d9d | 200 | |
Cornetin | 4:762404f17d9d | 201 | ble.startAdvertising(); |
Cornetin | 4:762404f17d9d | 202 | |
RedBearLab | 0:dfcebc1e442a | 203 | pc.printf("Advertising Start \r\n"); |
RedBearLab | 0:dfcebc1e442a | 204 | |
Cornetin | 4:762404f17d9d | 205 | BMP085 baro(); |
Cornetin | 4:762404f17d9d | 206 | while(1) { |
Cornetin | 4:762404f17d9d | 207 | ble.waitForEvent(); |
Cornetin | 4:762404f17d9d | 208 | |
Cornetin | 4:762404f17d9d | 209 | if (LED_BLINK) { |
Cornetin | 4:762404f17d9d | 210 | LED_SET = 1; |
Cornetin | 4:762404f17d9d | 211 | wait(0.05); |
Cornetin | 4:762404f17d9d | 212 | LED_SET = 0; |
Cornetin | 4:762404f17d9d | 213 | wait(0.05); |
Cornetin | 4:762404f17d9d | 214 | } |
Cornetin | 4:762404f17d9d | 215 | else |
Cornetin | 4:762404f17d9d | 216 | LED_SET = 0; |
RedBearLab | 0:dfcebc1e442a | 217 | } |
RedBearLab | 0:dfcebc1e442a | 218 | } |
RedBearLab | 0:dfcebc1e442a | 219 | |
RedBearLab | 0:dfcebc1e442a | 220 | |
RedBearLab | 0:dfcebc1e442a | 221 | |
RedBearLab | 0:dfcebc1e442a | 222 | |
RedBearLab | 0:dfcebc1e442a | 223 | |
RedBearLab | 0:dfcebc1e442a | 224 | |
RedBearLab | 0:dfcebc1e442a | 225 | |
RedBearLab | 0:dfcebc1e442a | 226 | |
RedBearLab | 0:dfcebc1e442a | 227 | |
RedBearLab | 0:dfcebc1e442a | 228 | |
RedBearLab | 0:dfcebc1e442a | 229 | |
RedBearLab | 0:dfcebc1e442a | 230 | |
RedBearLab | 0:dfcebc1e442a | 231 | |
RedBearLab | 0:dfcebc1e442a | 232 | |
RedBearLab | 0:dfcebc1e442a | 233 | |
RedBearLab | 0:dfcebc1e442a | 234 | |
RedBearLab | 0:dfcebc1e442a | 235 | |
RedBearLab | 0:dfcebc1e442a | 236 | |
RedBearLab | 0:dfcebc1e442a | 237 | |
RedBearLab | 0:dfcebc1e442a | 238 | |
RedBearLab | 0:dfcebc1e442a | 239 | |
RedBearLab | 0:dfcebc1e442a | 240 | |
RedBearLab | 0:dfcebc1e442a | 241 | |
RedBearLab | 0:dfcebc1e442a | 242 | |
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 |