![](/media/cache/profiles/profile_pic_for_github_less_than_1MB.jpg.50x50_q85.jpg)
This is example code that can get you started with building your own IR vision robot that communicates over LoRa
Dependencies: Adafruit-MotorShield Adafruit-PWM-Servo-Driver Adafruit_GFX BufferedSerial MAX17055_EZconfig NEO-6m-GPS SX1276GenericLib USBDeviceHT max32630fthr max77650_charger_sample
Fork of MAX326xxFTHR_LoRa_Example_test by
UARTService_custom.h@29:f7a0e49b826b, 2018-07-24 (annotated)
- Committer:
- dev_alexander
- Date:
- Tue Jul 24 00:38:18 2018 +0000
- Revision:
- 29:f7a0e49b826b
- Child:
- 30:66f9750cc44c
Changed the pins that the MAX32630FTHR uses in order to communicate with the LoRa Shield. This makes the UART pins available for the BLE now.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dev_alexander | 29:f7a0e49b826b | 1 | /* mbed Microcontroller Library |
dev_alexander | 29:f7a0e49b826b | 2 | * Copyright (c) 2006-2013 ARM Limited |
dev_alexander | 29:f7a0e49b826b | 3 | * |
dev_alexander | 29:f7a0e49b826b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dev_alexander | 29:f7a0e49b826b | 5 | * you may not use this file except in compliance with the License. |
dev_alexander | 29:f7a0e49b826b | 6 | * You may obtain a copy of the License at |
dev_alexander | 29:f7a0e49b826b | 7 | * |
dev_alexander | 29:f7a0e49b826b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dev_alexander | 29:f7a0e49b826b | 9 | * |
dev_alexander | 29:f7a0e49b826b | 10 | * Unless required by applicable law or agreed to in writing, software |
dev_alexander | 29:f7a0e49b826b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dev_alexander | 29:f7a0e49b826b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dev_alexander | 29:f7a0e49b826b | 13 | * See the License for the specific language governing permissions and |
dev_alexander | 29:f7a0e49b826b | 14 | * limitations under the License. |
dev_alexander | 29:f7a0e49b826b | 15 | */ |
dev_alexander | 29:f7a0e49b826b | 16 | |
dev_alexander | 29:f7a0e49b826b | 17 | #ifndef __BLE_UART_SERVICE_H__ |
dev_alexander | 29:f7a0e49b826b | 18 | #define __BLE_UART_SERVICE_H__ |
dev_alexander | 29:f7a0e49b826b | 19 | |
dev_alexander | 29:f7a0e49b826b | 20 | #ifdef YOTTA_CFG_MBED_OS |
dev_alexander | 29:f7a0e49b826b | 21 | #include "mbed-drivers/mbed.h" |
dev_alexander | 29:f7a0e49b826b | 22 | #include "mbed-drivers/Stream.h" |
dev_alexander | 29:f7a0e49b826b | 23 | #else |
dev_alexander | 29:f7a0e49b826b | 24 | #include "mbed.h" |
dev_alexander | 29:f7a0e49b826b | 25 | #include "Stream.h" |
dev_alexander | 29:f7a0e49b826b | 26 | #endif |
dev_alexander | 29:f7a0e49b826b | 27 | |
dev_alexander | 29:f7a0e49b826b | 28 | #include "ble/UUID.h" |
dev_alexander | 29:f7a0e49b826b | 29 | #include "ble/BLE.h" |
dev_alexander | 29:f7a0e49b826b | 30 | |
dev_alexander | 29:f7a0e49b826b | 31 | extern const uint8_t UARTServiceBaseUUID[UUID::LENGTH_OF_LONG_UUID]; |
dev_alexander | 29:f7a0e49b826b | 32 | extern const uint16_t UARTServiceShortUUID; |
dev_alexander | 29:f7a0e49b826b | 33 | extern const uint16_t UARTServiceTXCharacteristicShortUUID; |
dev_alexander | 29:f7a0e49b826b | 34 | extern const uint16_t UARTServiceRXCharacteristicShortUUID; |
dev_alexander | 29:f7a0e49b826b | 35 | |
dev_alexander | 29:f7a0e49b826b | 36 | extern const uint8_t UARTServiceUUID[UUID::LENGTH_OF_LONG_UUID]; |
dev_alexander | 29:f7a0e49b826b | 37 | extern const uint8_t UARTServiceUUID_reversed[UUID::LENGTH_OF_LONG_UUID]; |
dev_alexander | 29:f7a0e49b826b | 38 | |
dev_alexander | 29:f7a0e49b826b | 39 | extern const uint8_t UARTServiceTXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
dev_alexander | 29:f7a0e49b826b | 40 | extern const uint8_t UARTServiceRXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
dev_alexander | 29:f7a0e49b826b | 41 | |
dev_alexander | 29:f7a0e49b826b | 42 | /** |
dev_alexander | 29:f7a0e49b826b | 43 | * @class UARTService. |
dev_alexander | 29:f7a0e49b826b | 44 | * @brief BLE Service to enable UART over BLE. |
dev_alexander | 29:f7a0e49b826b | 45 | */ |
dev_alexander | 29:f7a0e49b826b | 46 | class UARTService { |
dev_alexander | 29:f7a0e49b826b | 47 | public: |
dev_alexander | 29:f7a0e49b826b | 48 | /**< Maximum length of data (in bytes) that the UART service module can transmit to the peer. */ |
dev_alexander | 29:f7a0e49b826b | 49 | static const unsigned BLE_UART_SERVICE_MAX_DATA_LEN = (BLE_GATT_MTU_SIZE_DEFAULT - 18); |
dev_alexander | 29:f7a0e49b826b | 50 | |
dev_alexander | 29:f7a0e49b826b | 51 | public: |
dev_alexander | 29:f7a0e49b826b | 52 | |
dev_alexander | 29:f7a0e49b826b | 53 | /** |
dev_alexander | 29:f7a0e49b826b | 54 | * @param[ref] ble |
dev_alexander | 29:f7a0e49b826b | 55 | * BLE object for the underlying controller. |
dev_alexander | 29:f7a0e49b826b | 56 | */ |
dev_alexander | 29:f7a0e49b826b | 57 | UARTService(BLE &_ble) : |
dev_alexander | 29:f7a0e49b826b | 58 | ble(_ble), |
dev_alexander | 29:f7a0e49b826b | 59 | receiveBuffer(), |
dev_alexander | 29:f7a0e49b826b | 60 | sendBuffer(), |
dev_alexander | 29:f7a0e49b826b | 61 | sendBufferIndex(0), |
dev_alexander | 29:f7a0e49b826b | 62 | numBytesReceived(0), |
dev_alexander | 29:f7a0e49b826b | 63 | receiveBufferIndex(0), |
dev_alexander | 29:f7a0e49b826b | 64 | txCharacteristic(UARTServiceTXCharacteristicUUID, receiveBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN, |
dev_alexander | 29:f7a0e49b826b | 65 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
dev_alexander | 29:f7a0e49b826b | 66 | rxCharacteristic(UARTServiceRXCharacteristicUUID, sendBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
dev_alexander | 29:f7a0e49b826b | 67 | GattCharacteristic *charTable[] = {&txCharacteristic, &rxCharacteristic}; |
dev_alexander | 29:f7a0e49b826b | 68 | GattService uartService(UARTServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
dev_alexander | 29:f7a0e49b826b | 69 | |
dev_alexander | 29:f7a0e49b826b | 70 | ble.addService(uartService); |
dev_alexander | 29:f7a0e49b826b | 71 | ble.onDataWritten(this, &UARTService::onDataWritten); |
dev_alexander | 29:f7a0e49b826b | 72 | } |
dev_alexander | 29:f7a0e49b826b | 73 | |
dev_alexander | 29:f7a0e49b826b | 74 | /** |
dev_alexander | 29:f7a0e49b826b | 75 | * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service. |
dev_alexander | 29:f7a0e49b826b | 76 | */ |
dev_alexander | 29:f7a0e49b826b | 77 | uint16_t getTXCharacteristicHandle() { |
dev_alexander | 29:f7a0e49b826b | 78 | return txCharacteristic.getValueAttribute().getHandle(); |
dev_alexander | 29:f7a0e49b826b | 79 | } |
dev_alexander | 29:f7a0e49b826b | 80 | |
dev_alexander | 29:f7a0e49b826b | 81 | /** |
dev_alexander | 29:f7a0e49b826b | 82 | * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service. |
dev_alexander | 29:f7a0e49b826b | 83 | */ |
dev_alexander | 29:f7a0e49b826b | 84 | uint16_t getRXCharacteristicHandle() { |
dev_alexander | 29:f7a0e49b826b | 85 | return rxCharacteristic.getValueAttribute().getHandle(); |
dev_alexander | 29:f7a0e49b826b | 86 | } |
dev_alexander | 29:f7a0e49b826b | 87 | |
dev_alexander | 29:f7a0e49b826b | 88 | /** |
dev_alexander | 29:f7a0e49b826b | 89 | * We attempt to collect bytes before pushing them to the UART RX |
dev_alexander | 29:f7a0e49b826b | 90 | * characteristic; writing to the RX characteristic then generates |
dev_alexander | 29:f7a0e49b826b | 91 | * notifications for the client. Updates made in quick succession to a |
dev_alexander | 29:f7a0e49b826b | 92 | * notification-generating characteristic result in data being buffered |
dev_alexander | 29:f7a0e49b826b | 93 | * in the Bluetooth stack as notifications are sent out. The stack has |
dev_alexander | 29:f7a0e49b826b | 94 | * its limits for this buffering - typically a small number under 10. |
dev_alexander | 29:f7a0e49b826b | 95 | * Collecting data into the sendBuffer buffer helps mitigate the rate of |
dev_alexander | 29:f7a0e49b826b | 96 | * updates. But we shouldn't buffer a large amount of data before updating |
dev_alexander | 29:f7a0e49b826b | 97 | * the characteristic, otherwise the client needs to turn around and make |
dev_alexander | 29:f7a0e49b826b | 98 | * a long read request; this is because notifications include only the first |
dev_alexander | 29:f7a0e49b826b | 99 | * 20 bytes of the updated data. |
dev_alexander | 29:f7a0e49b826b | 100 | * |
dev_alexander | 29:f7a0e49b826b | 101 | * @param buffer The received update. |
dev_alexander | 29:f7a0e49b826b | 102 | * @param length Number of characters to be appended. |
dev_alexander | 29:f7a0e49b826b | 103 | * @return Number of characters appended to the rxCharacteristic. |
dev_alexander | 29:f7a0e49b826b | 104 | */ |
dev_alexander | 29:f7a0e49b826b | 105 | size_t write(const void *_buffer, size_t length) { |
dev_alexander | 29:f7a0e49b826b | 106 | size_t origLength = length; |
dev_alexander | 29:f7a0e49b826b | 107 | const uint8_t *buffer = static_cast<const uint8_t *>(_buffer); |
dev_alexander | 29:f7a0e49b826b | 108 | |
dev_alexander | 29:f7a0e49b826b | 109 | if (ble.getGapState().connected) { |
dev_alexander | 29:f7a0e49b826b | 110 | unsigned bufferIndex = 0; |
dev_alexander | 29:f7a0e49b826b | 111 | while (length) { |
dev_alexander | 29:f7a0e49b826b | 112 | unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex; |
dev_alexander | 29:f7a0e49b826b | 113 | unsigned bytesToCopy = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer; |
dev_alexander | 29:f7a0e49b826b | 114 | |
dev_alexander | 29:f7a0e49b826b | 115 | /* Copy bytes into sendBuffer. */ |
dev_alexander | 29:f7a0e49b826b | 116 | memcpy(&sendBuffer[sendBufferIndex], &buffer[bufferIndex], bytesToCopy); |
dev_alexander | 29:f7a0e49b826b | 117 | length -= bytesToCopy; |
dev_alexander | 29:f7a0e49b826b | 118 | sendBufferIndex += bytesToCopy; |
dev_alexander | 29:f7a0e49b826b | 119 | bufferIndex += bytesToCopy; |
dev_alexander | 29:f7a0e49b826b | 120 | |
dev_alexander | 29:f7a0e49b826b | 121 | /* Have we collected enough? */ |
dev_alexander | 29:f7a0e49b826b | 122 | if ((sendBufferIndex == BLE_UART_SERVICE_MAX_DATA_LEN) || |
dev_alexander | 29:f7a0e49b826b | 123 | // (sendBuffer[sendBufferIndex - 1] == '\r') || |
dev_alexander | 29:f7a0e49b826b | 124 | (sendBuffer[sendBufferIndex - 1] == '\n')) { |
dev_alexander | 29:f7a0e49b826b | 125 | ble.gattServer().write(getRXCharacteristicHandle(), static_cast<const uint8_t *>(sendBuffer), sendBufferIndex); |
dev_alexander | 29:f7a0e49b826b | 126 | sendBufferIndex = 0; |
dev_alexander | 29:f7a0e49b826b | 127 | } |
dev_alexander | 29:f7a0e49b826b | 128 | } |
dev_alexander | 29:f7a0e49b826b | 129 | } |
dev_alexander | 29:f7a0e49b826b | 130 | |
dev_alexander | 29:f7a0e49b826b | 131 | return origLength; |
dev_alexander | 29:f7a0e49b826b | 132 | } |
dev_alexander | 29:f7a0e49b826b | 133 | |
dev_alexander | 29:f7a0e49b826b | 134 | /** |
dev_alexander | 29:f7a0e49b826b | 135 | * Helper function to write out strings. |
dev_alexander | 29:f7a0e49b826b | 136 | * @param str The received string. |
dev_alexander | 29:f7a0e49b826b | 137 | * @return Number of characters appended to the rxCharacteristic. |
dev_alexander | 29:f7a0e49b826b | 138 | */ |
dev_alexander | 29:f7a0e49b826b | 139 | size_t writeString(const char *str) { |
dev_alexander | 29:f7a0e49b826b | 140 | return write(str, strlen(str)); |
dev_alexander | 29:f7a0e49b826b | 141 | } |
dev_alexander | 29:f7a0e49b826b | 142 | |
dev_alexander | 29:f7a0e49b826b | 143 | /** |
dev_alexander | 29:f7a0e49b826b | 144 | * Override for Stream::_putc(). |
dev_alexander | 29:f7a0e49b826b | 145 | * @param c |
dev_alexander | 29:f7a0e49b826b | 146 | * This function writes the character c, cast to an unsigned char, to stream. |
dev_alexander | 29:f7a0e49b826b | 147 | * @return |
dev_alexander | 29:f7a0e49b826b | 148 | * The character written as an unsigned char cast to an int or EOF on error. |
dev_alexander | 29:f7a0e49b826b | 149 | */ |
dev_alexander | 29:f7a0e49b826b | 150 | int _putc(int c) { |
dev_alexander | 29:f7a0e49b826b | 151 | return (write(&c, 1) == 1) ? 1 : EOF; |
dev_alexander | 29:f7a0e49b826b | 152 | } |
dev_alexander | 29:f7a0e49b826b | 153 | |
dev_alexander | 29:f7a0e49b826b | 154 | /** |
dev_alexander | 29:f7a0e49b826b | 155 | * Override for Stream::_getc(). |
dev_alexander | 29:f7a0e49b826b | 156 | * @return |
dev_alexander | 29:f7a0e49b826b | 157 | * The character read. |
dev_alexander | 29:f7a0e49b826b | 158 | */ |
dev_alexander | 29:f7a0e49b826b | 159 | int _getc() { |
dev_alexander | 29:f7a0e49b826b | 160 | if (receiveBufferIndex == numBytesReceived) { |
dev_alexander | 29:f7a0e49b826b | 161 | return EOF; |
dev_alexander | 29:f7a0e49b826b | 162 | } |
dev_alexander | 29:f7a0e49b826b | 163 | |
dev_alexander | 29:f7a0e49b826b | 164 | return receiveBuffer[receiveBufferIndex++]; |
dev_alexander | 29:f7a0e49b826b | 165 | } |
dev_alexander | 29:f7a0e49b826b | 166 | |
dev_alexander | 29:f7a0e49b826b | 167 | protected: |
dev_alexander | 29:f7a0e49b826b | 168 | /** |
dev_alexander | 29:f7a0e49b826b | 169 | * This callback allows the UART service to receive updates to the |
dev_alexander | 29:f7a0e49b826b | 170 | * txCharacteristic. The application should forward the call to this |
dev_alexander | 29:f7a0e49b826b | 171 | * function from the global onDataWritten() callback handler; if that's |
dev_alexander | 29:f7a0e49b826b | 172 | * not used, this method can be used as a callback directly. |
dev_alexander | 29:f7a0e49b826b | 173 | */ |
dev_alexander | 29:f7a0e49b826b | 174 | void onDataWritten(const GattWriteCallbackParams *params) { |
dev_alexander | 29:f7a0e49b826b | 175 | if (params->handle == getTXCharacteristicHandle()) { |
dev_alexander | 29:f7a0e49b826b | 176 | uint16_t bytesRead = params->len; |
dev_alexander | 29:f7a0e49b826b | 177 | if (bytesRead <= BLE_UART_SERVICE_MAX_DATA_LEN) { |
dev_alexander | 29:f7a0e49b826b | 178 | numBytesReceived = bytesRead; |
dev_alexander | 29:f7a0e49b826b | 179 | receiveBufferIndex = 0; |
dev_alexander | 29:f7a0e49b826b | 180 | memcpy(receiveBuffer, params->data, numBytesReceived); |
dev_alexander | 29:f7a0e49b826b | 181 | } |
dev_alexander | 29:f7a0e49b826b | 182 | } |
dev_alexander | 29:f7a0e49b826b | 183 | } |
dev_alexander | 29:f7a0e49b826b | 184 | |
dev_alexander | 29:f7a0e49b826b | 185 | protected: |
dev_alexander | 29:f7a0e49b826b | 186 | BLE &ble; |
dev_alexander | 29:f7a0e49b826b | 187 | |
dev_alexander | 29:f7a0e49b826b | 188 | uint8_t receiveBuffer[BLE_UART_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which we receive |
dev_alexander | 29:f7a0e49b826b | 189 | * inbound data before forwarding it to the |
dev_alexander | 29:f7a0e49b826b | 190 | * application. */ |
dev_alexander | 29:f7a0e49b826b | 191 | |
dev_alexander | 29:f7a0e49b826b | 192 | uint8_t sendBuffer[BLE_UART_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which outbound data is |
dev_alexander | 29:f7a0e49b826b | 193 | * accumulated before being pushed to the |
dev_alexander | 29:f7a0e49b826b | 194 | * rxCharacteristic. */ |
dev_alexander | 29:f7a0e49b826b | 195 | uint8_t sendBufferIndex; |
dev_alexander | 29:f7a0e49b826b | 196 | uint8_t numBytesReceived; |
dev_alexander | 29:f7a0e49b826b | 197 | uint8_t receiveBufferIndex; |
dev_alexander | 29:f7a0e49b826b | 198 | |
dev_alexander | 29:f7a0e49b826b | 199 | GattCharacteristic txCharacteristic; /**< From the point of view of the external client, this is the characteristic |
dev_alexander | 29:f7a0e49b826b | 200 | * they'd write into in order to communicate with this application. */ |
dev_alexander | 29:f7a0e49b826b | 201 | GattCharacteristic rxCharacteristic; /**< From the point of view of the external client, this is the characteristic |
dev_alexander | 29:f7a0e49b826b | 202 | * they'd read from in order to receive the bytes transmitted by this |
dev_alexander | 29:f7a0e49b826b | 203 | * application. */ |
dev_alexander | 29:f7a0e49b826b | 204 | }; |
dev_alexander | 29:f7a0e49b826b | 205 | |
dev_alexander | 29:f7a0e49b826b | 206 | #endif /* #ifndef __BLE_UART_SERVICE_H__*/ |