Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
main.cpp@5:1b9734e68327, 2015-04-17 (annotated)
- Committer:
- AntonLS
- Date:
- Fri Apr 17 04:20:07 2015 +0000
- Revision:
- 5:1b9734e68327
- Parent:
- 4:17b8edf264c3
- Child:
- 6:ef758ac3c928
Waits for BLE ready on characteristic update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AntonLS | 0:28ca4562fe1a | 1 | /* |
AntonLS | 0:28ca4562fe1a | 2 | * TA test |
AntonLS | 0:28ca4562fe1a | 3 | * |
AntonLS | 0:28ca4562fe1a | 4 | */ |
AntonLS | 0:28ca4562fe1a | 5 | |
AntonLS | 0:28ca4562fe1a | 6 | /* mbed Microcontroller Library |
AntonLS | 0:28ca4562fe1a | 7 | * Copyright (c) 2006-2013 ARM Limited |
AntonLS | 0:28ca4562fe1a | 8 | * |
AntonLS | 0:28ca4562fe1a | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AntonLS | 0:28ca4562fe1a | 10 | * you may not use this file except in compliance with the License. |
AntonLS | 0:28ca4562fe1a | 11 | * You may obtain a copy of the License at |
AntonLS | 0:28ca4562fe1a | 12 | * |
AntonLS | 0:28ca4562fe1a | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
AntonLS | 0:28ca4562fe1a | 14 | * |
AntonLS | 0:28ca4562fe1a | 15 | * Unless required by applicable law or agreed to in writing, software |
AntonLS | 0:28ca4562fe1a | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
AntonLS | 0:28ca4562fe1a | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AntonLS | 0:28ca4562fe1a | 18 | * See the License for the specific language governing permissions and |
AntonLS | 0:28ca4562fe1a | 19 | * limitations under the License. |
AntonLS | 0:28ca4562fe1a | 20 | */ |
AntonLS | 0:28ca4562fe1a | 21 | |
AntonLS | 0:28ca4562fe1a | 22 | #include "mbed.h" |
AntonLS | 0:28ca4562fe1a | 23 | #include "BLEDevice.h" |
AntonLS | 0:28ca4562fe1a | 24 | |
AntonLS | 0:28ca4562fe1a | 25 | #include "DFUService.h" |
AntonLS | 0:28ca4562fe1a | 26 | #include "UARTService.h" |
AntonLS | 0:28ca4562fe1a | 27 | #include "DeviceInformationService.h" |
AntonLS | 0:28ca4562fe1a | 28 | |
AntonLS | 0:28ca4562fe1a | 29 | #include "MTSSerialFlowControl.h" |
AntonLS | 5:1b9734e68327 | 30 | #include "PhoneAppIO.h" |
AntonLS | 0:28ca4562fe1a | 31 | |
AntonLS | 0:28ca4562fe1a | 32 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
AntonLS | 0:28ca4562fe1a | 33 | * it will have an impact on code-size and power consumption. */ |
AntonLS | 0:28ca4562fe1a | 34 | |
AntonLS | 5:1b9734e68327 | 35 | // #define LOOPBACK_MODE // Loopback mode |
AntonLS | 4:17b8edf264c3 | 36 | |
AntonLS | 0:28ca4562fe1a | 37 | #if NEED_CONSOLE_OUTPUT |
AntonLS | 0:28ca4562fe1a | 38 | #define DEBUG(...) { printf(__VA_ARGS__); } |
AntonLS | 0:28ca4562fe1a | 39 | #else |
AntonLS | 0:28ca4562fe1a | 40 | #define DEBUG(...) /* nothing */ |
AntonLS | 0:28ca4562fe1a | 41 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
AntonLS | 0:28ca4562fe1a | 42 | |
AntonLS | 0:28ca4562fe1a | 43 | BLEDevice ble; |
AntonLS | 0:28ca4562fe1a | 44 | |
AntonLS | 5:1b9734e68327 | 45 | |
AntonLS | 0:28ca4562fe1a | 46 | #define TXRX_BUF_LEN 20 |
AntonLS | 0:28ca4562fe1a | 47 | |
AntonLS | 0:28ca4562fe1a | 48 | extern "C" |
AntonLS | 0:28ca4562fe1a | 49 | { |
AntonLS | 2:fe1566cdb6e7 | 50 | // serial_t _my_serial; |
AntonLS | 0:28ca4562fe1a | 51 | void pin_mode( PinName, PinMode ); |
AntonLS | 0:28ca4562fe1a | 52 | } |
AntonLS | 0:28ca4562fe1a | 53 | |
AntonLS | 1:0ba687d4196f | 54 | // Note: From the datasheet: |
AntonLS | 1:0ba687d4196f | 55 | // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled. |
AntonLS | 1:0ba687d4196f | 56 | // But serial_init() erroneously enables the uart before the setting of those, |
AntonLS | 1:0ba687d4196f | 57 | // which messes up flow control. Apparently the setting is ONCE per ON mode. ARGH! |
AntonLS | 1:0ba687d4196f | 58 | // So we will make our own versions of Serial and SerialBase to (MySerial and MySerialBase) |
AntonLS | 1:0ba687d4196f | 59 | // to not use serial_init() in serial_api.c, so flow control is setup correctly. |
AntonLS | 1:0ba687d4196f | 60 | // MTSSerial now uses our MySerial instead of Serial. |
AntonLS | 1:0ba687d4196f | 61 | // Since MTSSerial now uses hardware flow control by default, MTSSerialFlowControl isn't needed |
AntonLS | 3:388e441be8df | 62 | // unless the hardware doesn't have hardware flow control capability or sw is always fast enough. |
AntonLS | 1:0ba687d4196f | 63 | // |
AntonLS | 5:1b9734e68327 | 64 | // mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1536, 1536 ); |
AntonLS | 5:1b9734e68327 | 65 | mts::MTSSerial pcfc( USBTX, USBRX, 384, 2688 ); |
AntonLS | 0:28ca4562fe1a | 66 | |
AntonLS | 0:28ca4562fe1a | 67 | uint8_t txPayload[TXRX_BUF_LEN] = { 0 }; |
AntonLS | 0:28ca4562fe1a | 68 | |
AntonLS | 0:28ca4562fe1a | 69 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
AntonLS | 0:28ca4562fe1a | 70 | static uint8_t rx_len = 0; |
AntonLS | 0:28ca4562fe1a | 71 | |
AntonLS | 0:28ca4562fe1a | 72 | |
AntonLS | 1:0ba687d4196f | 73 | DigitalOut led1( LED1 ); |
AntonLS | 1:0ba687d4196f | 74 | DigitalOut rts( RTS_PIN_NUMBER ); |
AntonLS | 1:0ba687d4196f | 75 | |
AntonLS | 1:0ba687d4196f | 76 | |
AntonLS | 5:1b9734e68327 | 77 | char deviceName[6]; // "TAF00"; |
AntonLS | 5:1b9734e68327 | 78 | Gap::address_t macAddr; |
AntonLS | 5:1b9734e68327 | 79 | Gap::addr_type_t *pAdType; |
AntonLS | 5:1b9734e68327 | 80 | |
AntonLS | 0:28ca4562fe1a | 81 | |
AntonLS | 0:28ca4562fe1a | 82 | const uint8_t DevInfoServiceUUID_rev[] = |
AntonLS | 0:28ca4562fe1a | 83 | { |
AntonLS | 0:28ca4562fe1a | 84 | (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8) |
AntonLS | 0:28ca4562fe1a | 85 | }; |
AntonLS | 0:28ca4562fe1a | 86 | |
AntonLS | 0:28ca4562fe1a | 87 | UARTService *uartServicePtr; |
AntonLS | 0:28ca4562fe1a | 88 | |
AntonLS | 5:1b9734e68327 | 89 | // PhoneAppIO toPhone( &ble ); |
AntonLS | 5:1b9734e68327 | 90 | |
AntonLS | 5:1b9734e68327 | 91 | |
AntonLS | 0:28ca4562fe1a | 92 | void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ) |
AntonLS | 0:28ca4562fe1a | 93 | { |
AntonLS | 0:28ca4562fe1a | 94 | DEBUG( "Disconnected!\n\r" ); |
AntonLS | 0:28ca4562fe1a | 95 | DEBUG( "Restarting the advertising process\n\r" ); |
AntonLS | 0:28ca4562fe1a | 96 | ble.startAdvertising(); |
AntonLS | 0:28ca4562fe1a | 97 | } |
AntonLS | 0:28ca4562fe1a | 98 | |
AntonLS | 5:1b9734e68327 | 99 | bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead ) |
AntonLS | 5:1b9734e68327 | 100 | { |
AntonLS | 5:1b9734e68327 | 101 | ble_error_t err; |
AntonLS | 5:1b9734e68327 | 102 | |
AntonLS | 5:1b9734e68327 | 103 | // toPhone.toPhoneBuf( (uint8_t *)data, bytesRead ); |
AntonLS | 5:1b9734e68327 | 104 | |
AntonLS | 5:1b9734e68327 | 105 | err = ble.updateCharacteristicValue( handle, data, bytesRead ); |
AntonLS | 5:1b9734e68327 | 106 | |
AntonLS | 5:1b9734e68327 | 107 | if( (err == BLE_ERROR_BUFFER_OVERFLOW) || |
AntonLS | 5:1b9734e68327 | 108 | (err == BLE_STACK_BUSY) || |
AntonLS | 5:1b9734e68327 | 109 | (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) ) |
AntonLS | 5:1b9734e68327 | 110 | { |
AntonLS | 5:1b9734e68327 | 111 | // pcfc.printf( "\r\nBLE %d! ", err ); |
AntonLS | 5:1b9734e68327 | 112 | } |
AntonLS | 5:1b9734e68327 | 113 | |
AntonLS | 5:1b9734e68327 | 114 | return (err != BLE_ERROR_NONE); |
AntonLS | 5:1b9734e68327 | 115 | } |
AntonLS | 5:1b9734e68327 | 116 | |
AntonLS | 0:28ca4562fe1a | 117 | void onDataWritten( const GattCharacteristicWriteCBParams *params ) |
AntonLS | 0:28ca4562fe1a | 118 | { |
AntonLS | 0:28ca4562fe1a | 119 | if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) ) |
AntonLS | 0:28ca4562fe1a | 120 | { |
AntonLS | 0:28ca4562fe1a | 121 | uint8_t buf[TXRX_BUF_LEN]; |
AntonLS | 0:28ca4562fe1a | 122 | uint16_t bytesRead = params->len; |
AntonLS | 0:28ca4562fe1a | 123 | |
AntonLS | 0:28ca4562fe1a | 124 | DEBUG( "received %u bytes\n\r", bytesRead ); |
AntonLS | 0:28ca4562fe1a | 125 | |
AntonLS | 4:17b8edf264c3 | 126 | #ifdef LOOPBACK_MODE |
AntonLS | 0:28ca4562fe1a | 127 | // Loopback data from Central. |
AntonLS | 5:1b9734e68327 | 128 | updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead ); // Notifies. |
AntonLS | 4:17b8edf264c3 | 129 | #endif |
AntonLS | 0:28ca4562fe1a | 130 | |
AntonLS | 5:1b9734e68327 | 131 | ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead ); |
AntonLS | 5:1b9734e68327 | 132 | |
AntonLS | 0:28ca4562fe1a | 133 | // Also write to serial port... |
AntonLS | 0:28ca4562fe1a | 134 | memset( txPayload, 0, TXRX_BUF_LEN ); |
AntonLS | 0:28ca4562fe1a | 135 | memcpy( txPayload, buf, TXRX_BUF_LEN ); |
AntonLS | 1:0ba687d4196f | 136 | // pcfc.printf( "From app: " ); |
AntonLS | 0:28ca4562fe1a | 137 | pcfc.write( (char *)txPayload, bytesRead ); |
AntonLS | 1:0ba687d4196f | 138 | // pcfc.printf( "\r\n" ); |
AntonLS | 0:28ca4562fe1a | 139 | } |
AntonLS | 0:28ca4562fe1a | 140 | } |
AntonLS | 0:28ca4562fe1a | 141 | |
AntonLS | 5:1b9734e68327 | 142 | bool bleWasntReady; |
AntonLS | 0:28ca4562fe1a | 143 | |
AntonLS | 0:28ca4562fe1a | 144 | void onDataSent( unsigned count ) |
AntonLS | 0:28ca4562fe1a | 145 | { |
AntonLS | 0:28ca4562fe1a | 146 | } |
AntonLS | 0:28ca4562fe1a | 147 | |
AntonLS | 0:28ca4562fe1a | 148 | void periodicCallback( void ) |
AntonLS | 0:28ca4562fe1a | 149 | { |
AntonLS | 0:28ca4562fe1a | 150 | led1 = !led1; |
AntonLS | 3:388e441be8df | 151 | // rts = !rts; |
AntonLS | 0:28ca4562fe1a | 152 | } |
AntonLS | 0:28ca4562fe1a | 153 | |
AntonLS | 0:28ca4562fe1a | 154 | void uartCB( void ) |
AntonLS | 0:28ca4562fe1a | 155 | { |
AntonLS | 3:388e441be8df | 156 | if( 0 != rts.read() ) pcfc.puts( "\r\n!RTS disengaged.\r\n" ); // Can we read rts when in HW fc mode? |
AntonLS | 3:388e441be8df | 157 | |
AntonLS | 0:28ca4562fe1a | 158 | // Set line from serial port to RX characteristic (From cone) |
AntonLS | 0:28ca4562fe1a | 159 | while( pcfc.readable() ) |
AntonLS | 0:28ca4562fe1a | 160 | { |
AntonLS | 5:1b9734e68327 | 161 | if( !bleWasntReady ) |
AntonLS | 0:28ca4562fe1a | 162 | { |
AntonLS | 5:1b9734e68327 | 163 | char ch; |
AntonLS | 5:1b9734e68327 | 164 | pcfc.read( ch ); |
AntonLS | 5:1b9734e68327 | 165 | rx_buf[rx_len++] = ch; |
AntonLS | 5:1b9734e68327 | 166 | } |
AntonLS | 5:1b9734e68327 | 167 | if( bleWasntReady || rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' ) |
AntonLS | 5:1b9734e68327 | 168 | { |
AntonLS | 5:1b9734e68327 | 169 | bleWasntReady = updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len ); // Notifies. |
AntonLS | 0:28ca4562fe1a | 170 | |
AntonLS | 1:0ba687d4196f | 171 | // pcfc.printf( "RecHandler \r\n" ); |
AntonLS | 5:1b9734e68327 | 172 | |
AntonLS | 5:1b9734e68327 | 173 | if( !bleWasntReady ) rx_len = 0; |
AntonLS | 0:28ca4562fe1a | 174 | break; |
AntonLS | 0:28ca4562fe1a | 175 | } |
AntonLS | 0:28ca4562fe1a | 176 | } |
AntonLS | 0:28ca4562fe1a | 177 | } |
AntonLS | 0:28ca4562fe1a | 178 | |
AntonLS | 0:28ca4562fe1a | 179 | int main( void ) |
AntonLS | 0:28ca4562fe1a | 180 | { |
AntonLS | 0:28ca4562fe1a | 181 | led1 = 1; |
AntonLS | 0:28ca4562fe1a | 182 | Ticker ticker; |
AntonLS | 0:28ca4562fe1a | 183 | ticker.attach( periodicCallback, 1 ); |
AntonLS | 0:28ca4562fe1a | 184 | |
AntonLS | 0:28ca4562fe1a | 185 | |
AntonLS | 0:28ca4562fe1a | 186 | pcfc.baud( 57600 ); |
AntonLS | 0:28ca4562fe1a | 187 | pcfc.rxClear(); |
AntonLS | 0:28ca4562fe1a | 188 | // pc.attach( uartCB, pc.RxIrq ); |
AntonLS | 0:28ca4562fe1a | 189 | |
AntonLS | 0:28ca4562fe1a | 190 | DEBUG( "Initialising the nRF51822\n\r" ); |
AntonLS | 1:0ba687d4196f | 191 | |
AntonLS | 2:fe1566cdb6e7 | 192 | |
AntonLS | 0:28ca4562fe1a | 193 | ble.init(); |
AntonLS | 0:28ca4562fe1a | 194 | ble.onDisconnection( disconnectionCallback ); |
AntonLS | 0:28ca4562fe1a | 195 | ble.onDataWritten( onDataWritten ); |
AntonLS | 0:28ca4562fe1a | 196 | ble.onDataSent( onDataSent ); |
AntonLS | 0:28ca4562fe1a | 197 | |
AntonLS | 0:28ca4562fe1a | 198 | /* setup advertising */ |
AntonLS | 0:28ca4562fe1a | 199 | ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED ); |
AntonLS | 0:28ca4562fe1a | 200 | ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); |
AntonLS | 5:1b9734e68327 | 201 | |
AntonLS | 5:1b9734e68327 | 202 | // Get MAC addr so we can create a device name using it. |
AntonLS | 5:1b9734e68327 | 203 | ble.getAddress( pAdType, macAddr ); |
AntonLS | 5:1b9734e68327 | 204 | sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); |
AntonLS | 5:1b9734e68327 | 205 | |
AntonLS | 5:1b9734e68327 | 206 | pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName ); |
AntonLS | 5:1b9734e68327 | 207 | #ifdef LOOPBACK_MODE |
AntonLS | 5:1b9734e68327 | 208 | pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" ); |
AntonLS | 5:1b9734e68327 | 209 | #endif |
AntonLS | 5:1b9734e68327 | 210 | |
AntonLS | 0:28ca4562fe1a | 211 | ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, |
AntonLS | 0:28ca4562fe1a | 212 | (const uint8_t *)deviceName, strlen(deviceName) ); |
AntonLS | 0:28ca4562fe1a | 213 | ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, |
AntonLS | 0:28ca4562fe1a | 214 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) ); |
AntonLS | 0:28ca4562fe1a | 215 | |
AntonLS | 0:28ca4562fe1a | 216 | ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
AntonLS | 0:28ca4562fe1a | 217 | (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) ); |
AntonLS | 0:28ca4562fe1a | 218 | |
AntonLS | 0:28ca4562fe1a | 219 | ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 1000 ) ); |
AntonLS | 0:28ca4562fe1a | 220 | ble.startAdvertising(); |
AntonLS | 0:28ca4562fe1a | 221 | |
AntonLS | 0:28ca4562fe1a | 222 | DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" ); |
AntonLS | 0:28ca4562fe1a | 223 | |
AntonLS | 0:28ca4562fe1a | 224 | /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a |
AntonLS | 0:28ca4562fe1a | 225 | * control characteristic which can be used to trigger the application to |
AntonLS | 0:28ca4562fe1a | 226 | * handover control to a resident bootloader. */ |
AntonLS | 0:28ca4562fe1a | 227 | DFUService dfu( ble ); |
AntonLS | 0:28ca4562fe1a | 228 | |
AntonLS | 0:28ca4562fe1a | 229 | UARTService uartService( ble ); |
AntonLS | 0:28ca4562fe1a | 230 | uartServicePtr = &uartService; |
AntonLS | 0:28ca4562fe1a | 231 | |
AntonLS | 0:28ca4562fe1a | 232 | while( true ) |
AntonLS | 0:28ca4562fe1a | 233 | { |
AntonLS | 0:28ca4562fe1a | 234 | ble.waitForEvent(); |
AntonLS | 0:28ca4562fe1a | 235 | |
AntonLS | 0:28ca4562fe1a | 236 | uartCB(); |
AntonLS | 0:28ca4562fe1a | 237 | } |
AntonLS | 0:28ca4562fe1a | 238 | } |
AntonLS | 0:28ca4562fe1a | 239 | |
AntonLS | 0:28ca4562fe1a | 240 | /* EOF */ |