dsf
Dependencies: BLE_API mbed nRF51822
main.cpp@0:b5906c81772b, 2017-02-05 (annotated)
- Committer:
- stoicancristi
- Date:
- Sun Feb 05 16:31:58 2017 +0000
- Revision:
- 0:b5906c81772b
BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stoicancristi | 0:b5906c81772b | 1 | /* |
stoicancristi | 0:b5906c81772b | 2 | |
stoicancristi | 0:b5906c81772b | 3 | Copyright (c) 2012-2014 RedBearLab |
stoicancristi | 0:b5906c81772b | 4 | |
stoicancristi | 0:b5906c81772b | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
stoicancristi | 0:b5906c81772b | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
stoicancristi | 0:b5906c81772b | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
stoicancristi | 0:b5906c81772b | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
stoicancristi | 0:b5906c81772b | 9 | subject to the following conditions: |
stoicancristi | 0:b5906c81772b | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
stoicancristi | 0:b5906c81772b | 11 | |
stoicancristi | 0:b5906c81772b | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
stoicancristi | 0:b5906c81772b | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
stoicancristi | 0:b5906c81772b | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
stoicancristi | 0:b5906c81772b | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
stoicancristi | 0:b5906c81772b | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
stoicancristi | 0:b5906c81772b | 17 | |
stoicancristi | 0:b5906c81772b | 18 | */ |
stoicancristi | 0:b5906c81772b | 19 | |
stoicancristi | 0:b5906c81772b | 20 | /* |
stoicancristi | 0:b5906c81772b | 21 | * The application works with the BLEController iOS/Android App. |
stoicancristi | 0:b5906c81772b | 22 | * Type something from the Terminal to send |
stoicancristi | 0:b5906c81772b | 23 | * to the BLEController App or vice verse. |
stoicancristi | 0:b5906c81772b | 24 | * Characteristics received from App will print on Terminal. |
stoicancristi | 0:b5906c81772b | 25 | */ |
stoicancristi | 0:b5906c81772b | 26 | |
stoicancristi | 0:b5906c81772b | 27 | #include "mbed.h" |
stoicancristi | 0:b5906c81772b | 28 | #include "BTDevice.hpp" |
stoicancristi | 0:b5906c81772b | 29 | #include "ControllerFactory.hpp" |
stoicancristi | 0:b5906c81772b | 30 | |
stoicancristi | 0:b5906c81772b | 31 | BLE ble; |
stoicancristi | 0:b5906c81772b | 32 | Ticker periodicCommand; |
stoicancristi | 0:b5906c81772b | 33 | |
stoicancristi | 0:b5906c81772b | 34 | Serial pc(USBTX, USBRX); |
stoicancristi | 0:b5906c81772b | 35 | |
stoicancristi | 0:b5906c81772b | 36 | BTDevice Nano(ble); |
stoicancristi | 0:b5906c81772b | 37 | ControllerFactory Control_Law(); |
stoicancristi | 0:b5906c81772b | 38 | //Controller c; |
stoicancristi | 0:b5906c81772b | 39 | char *old_receive = NULL; |
stoicancristi | 0:b5906c81772b | 40 | float R[3] = {0.1,0.4,0.6}; |
stoicancristi | 0:b5906c81772b | 41 | float S[3] = {0.2,0.1,0.5}; |
stoicancristi | 0:b5906c81772b | 42 | float T[4] = {0.1,0.7,0.5}; |
stoicancristi | 0:b5906c81772b | 43 | ControllerParams Parameters; |
stoicancristi | 0:b5906c81772b | 44 | |
stoicancristi | 0:b5906c81772b | 45 | |
stoicancristi | 0:b5906c81772b | 46 | // The Nordic UART Service |
stoicancristi | 0:b5906c81772b | 47 | |
stoicancristi | 0:b5906c81772b | 48 | /*decode(uint8_t *receive, Controller *c); |
stoicancristi | 0:b5906c81772b | 49 | |
stoicancristi | 0:b5906c81772b | 50 | void state_machine(uint8_t *receive) |
stoicancristi | 0:b5906c81772b | 51 | { |
stoicancristi | 0:b5906c81772b | 52 | switch((char *)receive) |
stoicancristi | 0:b5906c81772b | 53 | { |
stoicancristi | 0:b5906c81772b | 54 | case 'P': |
stoicancristi | 0:b5906c81772b | 55 | { |
stoicancristi | 0:b5906c81772b | 56 | Control_Law.createController(&c, P); |
stoicancristi | 0:b5906c81772b | 57 | Parameters.kp = 0.1; |
stoicancristi | 0:b5906c81772b | 58 | Control_Law.createControllerParams(&c, P, &Parameters, 0); |
stoicancristi | 0:b5906c81772b | 59 | c.updateRef(0.5); |
stoicancristi | 0:b5906c81772b | 60 | } |
stoicancristi | 0:b5906c81772b | 61 | case 'PI': |
stoicancristi | 0:b5906c81772b | 62 | { |
stoicancristi | 0:b5906c81772b | 63 | Control_Law.createController(&c, PI); |
stoicancristi | 0:b5906c81772b | 64 | Parameters.kp = 0.1; |
stoicancristi | 0:b5906c81772b | 65 | Parameters.ti = 0.3; |
stoicancristi | 0:b5906c81772b | 66 | Control_Law.createControllerParams(&c, PI, &Parameters, 0); |
stoicancristi | 0:b5906c81772b | 67 | c.updateRef(0.7); |
stoicancristi | 0:b5906c81772b | 68 | } |
stoicancristi | 0:b5906c81772b | 69 | case 'PID': |
stoicancristi | 0:b5906c81772b | 70 | { |
stoicancristi | 0:b5906c81772b | 71 | Control_Law.createController(&c, PID); |
stoicancristi | 0:b5906c81772b | 72 | Parameters.kp = 0.1; |
stoicancristi | 0:b5906c81772b | 73 | Parameters.ti = 0.3; |
stoicancristi | 0:b5906c81772b | 74 | Parameters.td = 0.15; |
stoicancristi | 0:b5906c81772b | 75 | Control_Law.createControllerParams(&c, PID, &Parameters, 0); |
stoicancristi | 0:b5906c81772b | 76 | c.updateRef(0.9); |
stoicancristi | 0:b5906c81772b | 77 | } |
stoicancristi | 0:b5906c81772b | 78 | case 'RST': |
stoicancristi | 0:b5906c81772b | 79 | { |
stoicancristi | 0:b5906c81772b | 80 | Control_Law.createController(&c, RST); |
stoicancristi | 0:b5906c81772b | 81 | Parameters.ordR = sizeof(R)/sizeof(*R); |
stoicancristi | 0:b5906c81772b | 82 | Parameters.ordS = sizeof(S)/sizeof(*S); |
stoicancristi | 0:b5906c81772b | 83 | Parameters.ordT = sizeof(T)/sizeof(*T); |
stoicancristi | 0:b5906c81772b | 84 | Parameters.R = R; |
stoicancristi | 0:b5906c81772b | 85 | Parameters.S = S; |
stoicancristi | 0:b5906c81772b | 86 | Parameters.T = T; |
stoicancristi | 0:b5906c81772b | 87 | Control_Law.createControllerParams(&c, RST, &Parameters, Parameters.ordT); |
stoicancristi | 0:b5906c81772b | 88 | c.updateRef(1); |
stoicancristi | 0:b5906c81772b | 89 | } |
stoicancristi | 0:b5906c81772b | 90 | case 'delete': |
stoicancristi | 0:b5906c81772b | 91 | { |
stoicancristi | 0:b5906c81772b | 92 | delete c; |
stoicancristi | 0:b5906c81772b | 93 | } |
stoicancristi | 0:b5906c81772b | 94 | } |
stoicancristi | 0:b5906c81772b | 95 | decode_message(&receive, &c); |
stoicancristi | 0:b5906c81772b | 96 | } |
stoicancristi | 0:b5906c81772b | 97 | |
stoicancristi | 0:b5906c81772b | 98 | decode_message(uint8_t *receive, Controller c) |
stoicancristi | 0:b5906c81772b | 99 | { |
stoicancristi | 0:b5906c81772b | 100 | switch((char *)receive) |
stoicancristi | 0:b5906c81772b | 101 | { |
stoicancristi | 0:b5906c81772b | 102 | case 'Command': |
stoicancristi | 0:b5906c81772b | 103 | { |
stoicancristi | 0:b5906c81772b | 104 | Nano.sendMsg((uint8_t *),c.calculateCmd, 16); |
stoicancristi | 0:b5906c81772b | 105 | } |
stoicancristi | 0:b5906c81772b | 106 | } |
stoicancristi | 0:b5906c81772b | 107 | }*/ |
stoicancristi | 0:b5906c81772b | 108 | |
stoicancristi | 0:b5906c81772b | 109 | |
stoicancristi | 0:b5906c81772b | 110 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
stoicancristi | 0:b5906c81772b | 111 | { |
stoicancristi | 0:b5906c81772b | 112 | pc.printf("Disconnected \r\n"); |
stoicancristi | 0:b5906c81772b | 113 | pc.printf("Restart advertising \r\n"); |
stoicancristi | 0:b5906c81772b | 114 | ble.startAdvertising(); |
stoicancristi | 0:b5906c81772b | 115 | } |
stoicancristi | 0:b5906c81772b | 116 | |
stoicancristi | 0:b5906c81772b | 117 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
stoicancristi | 0:b5906c81772b | 118 | { |
stoicancristi | 0:b5906c81772b | 119 | uint8_t buf[TXRX_BUF_LEN]; |
stoicancristi | 0:b5906c81772b | 120 | uint16_t bytesRead, index; |
stoicancristi | 0:b5906c81772b | 121 | |
stoicancristi | 0:b5906c81772b | 122 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
stoicancristi | 0:b5906c81772b | 123 | { |
stoicancristi | 0:b5906c81772b | 124 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
stoicancristi | 0:b5906c81772b | 125 | memset(txPayload, 0, TXRX_BUF_LEN); |
stoicancristi | 0:b5906c81772b | 126 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
stoicancristi | 0:b5906c81772b | 127 | //state_machine(&buf); |
stoicancristi | 0:b5906c81772b | 128 | } |
stoicancristi | 0:b5906c81772b | 129 | } |
stoicancristi | 0:b5906c81772b | 130 | |
stoicancristi | 0:b5906c81772b | 131 | void uartCB(void) |
stoicancristi | 0:b5906c81772b | 132 | { |
stoicancristi | 0:b5906c81772b | 133 | while(pc.readable()) |
stoicancristi | 0:b5906c81772b | 134 | { |
stoicancristi | 0:b5906c81772b | 135 | rx_buf[rx_len++] = pc.getc(); |
stoicancristi | 0:b5906c81772b | 136 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
stoicancristi | 0:b5906c81772b | 137 | { |
stoicancristi | 0:b5906c81772b | 138 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
stoicancristi | 0:b5906c81772b | 139 | rx_len = 0; |
stoicancristi | 0:b5906c81772b | 140 | break; |
stoicancristi | 0:b5906c81772b | 141 | } |
stoicancristi | 0:b5906c81772b | 142 | } |
stoicancristi | 0:b5906c81772b | 143 | } |
stoicancristi | 0:b5906c81772b | 144 | |
stoicancristi | 0:b5906c81772b | 145 | int main(void) |
stoicancristi | 0:b5906c81772b | 146 | { |
stoicancristi | 0:b5906c81772b | 147 | ble.init(); |
stoicancristi | 0:b5906c81772b | 148 | ble.onDisconnection(disconnectionCallback); |
stoicancristi | 0:b5906c81772b | 149 | ble.onDataWritten(WrittenHandler); |
stoicancristi | 0:b5906c81772b | 150 | |
stoicancristi | 0:b5906c81772b | 151 | pc.baud(9600); |
stoicancristi | 0:b5906c81772b | 152 | pc.printf("SimpleChat Init \r\n"); |
stoicancristi | 0:b5906c81772b | 153 | |
stoicancristi | 0:b5906c81772b | 154 | pc.attach( uartCB , pc.RxIrq); |
stoicancristi | 0:b5906c81772b | 155 | // setup advertising |
stoicancristi | 0:b5906c81772b | 156 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
stoicancristi | 0:b5906c81772b | 157 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
stoicancristi | 0:b5906c81772b | 158 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
stoicancristi | 0:b5906c81772b | 159 | (const uint8_t *)"Gigel", sizeof("Biscuit") - 1); |
stoicancristi | 0:b5906c81772b | 160 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
stoicancristi | 0:b5906c81772b | 161 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
stoicancristi | 0:b5906c81772b | 162 | // 100ms; in multiples of 0.625ms. |
stoicancristi | 0:b5906c81772b | 163 | ble.setAdvertisingInterval(160); |
stoicancristi | 0:b5906c81772b | 164 | |
stoicancristi | 0:b5906c81772b | 165 | ble.addService(uartService); |
stoicancristi | 0:b5906c81772b | 166 | |
stoicancristi | 0:b5906c81772b | 167 | ble.startAdvertising(); |
stoicancristi | 0:b5906c81772b | 168 | pc.printf("Advertising Start \r\n"); |
stoicancristi | 0:b5906c81772b | 169 | |
stoicancristi | 0:b5906c81772b | 170 | while(1) |
stoicancristi | 0:b5906c81772b | 171 | { |
stoicancristi | 0:b5906c81772b | 172 | ble.waitForEvent(); |
stoicancristi | 0:b5906c81772b | 173 | } |
stoicancristi | 0:b5906c81772b | 174 | } |
stoicancristi | 0:b5906c81772b | 175 | |
stoicancristi | 0:b5906c81772b | 176 | |
stoicancristi | 0:b5906c81772b | 177 | |
stoicancristi | 0:b5906c81772b | 178 | |
stoicancristi | 0:b5906c81772b | 179 | |
stoicancristi | 0:b5906c81772b | 180 | |
stoicancristi | 0:b5906c81772b | 181 | |
stoicancristi | 0:b5906c81772b | 182 | |
stoicancristi | 0:b5906c81772b | 183 | |
stoicancristi | 0:b5906c81772b | 184 | |
stoicancristi | 0:b5906c81772b | 185 | |
stoicancristi | 0:b5906c81772b | 186 | |
stoicancristi | 0:b5906c81772b | 187 | |
stoicancristi | 0:b5906c81772b | 188 | |
stoicancristi | 0:b5906c81772b | 189 | |
stoicancristi | 0:b5906c81772b | 190 |