test
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/bluetooth/MicroBitUARTService.h@75:6a5fcf1bf8e9, 2018-04-17 (annotated)
- Committer:
- JKsoft_main
- Date:
- Tue Apr 17 12:10:30 2018 +0000
- Revision:
- 75:6a5fcf1bf8e9
- Parent:
- 66:2fc7d7c2fffc
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MICROBIT_UART_SERVICE_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_UART_SERVICE_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "ble/UUID.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "ble/BLE.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | #include "MicroBitSerial.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | #define MICROBIT_UART_S_DEFAULT_BUF_SIZE 20 |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | #define MICROBIT_UART_S_EVT_DELIM_MATCH 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | #define MICROBIT_UART_S_EVT_HEAD_MATCH 2 |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | #define MICROBIT_UART_S_EVT_RX_FULL 3 |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | * Class definition for the custom MicroBit UART Service. |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | * Provides a BLE service that acts as a UART port, enabling the reception and transmission |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | * of an arbitrary number of bytes. |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | class MicroBitUARTService |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | uint8_t* rxBuffer; |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | uint8_t* txBuffer; |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | uint8_t rxBufferHead; |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | uint8_t rxBufferTail; |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | uint8_t rxBufferSize; |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | uint8_t txBufferSize; |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | |
LancasterUniversity | 66:2fc7d7c2fffc | 58 | uint32_t rxCharacteristicHandle; |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | // Bluetooth stack we're running on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | BLEDevice &ble; |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | //delimeters used for matching on receive. |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | ManagedString delimeters; |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | //a variable used when a user calls the eventAfter() method. |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | int rxBuffHeadMatch; |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | * A callback function for whenever a Bluetooth device writes to our TX characteristic. |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | void onDataWritten(const GattWriteCallbackParams *params); |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | * An internal method that copies values from a circular buffer to a linear buffer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | * @param circularBuff a pointer to the source circular buffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | * @param circularBuffSize the size of the circular buffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * @param linearBuff a pointer to the destination linear buffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * @param tailPosition the tail position in the circular buffer you want to copy from |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * @param headPosition the head position in the circular buffer you want to copy to |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * @note this method assumes that the linear buffer has the appropriate amount of |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | * memory to contain the copy operation |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | void circularCopy(uint8_t *circularBuff, uint8_t circularBuffSize, uint8_t *linearBuff, uint16_t tailPosition, uint16_t headPosition); |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | * Constructor for the UARTService. |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | * @param _ble an instance of BLEDevice |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | * @param rxBufferSize the size of the rxBuffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | * @param txBufferSize the size of the txBuffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * @note The default size is MICROBIT_UART_S_DEFAULT_BUF_SIZE (20 bytes). |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | MicroBitUARTService(BLEDevice &_ble, uint8_t rxBufferSize = MICROBIT_UART_S_DEFAULT_BUF_SIZE, uint8_t txBufferSize = MICROBIT_UART_S_DEFAULT_BUF_SIZE); |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | * Retreives a single character from our RxBuffer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | * ASYNC - Will attempt to read a single character, and return immediately |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | * SYNC_SLEEP - Will configure the event and block the current fiber until the |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | * event is received. |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | * @return MICROBIT_INVALID_PARAMETER if the mode given is SYNC_SPINWAIT, a character or MICROBIT_NO_DATA |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | int getc(MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | /** |
LancasterUniversity | 66:2fc7d7c2fffc | 118 | * Places a single character into our transmission buffer, |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | * @param c the character to transmit |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 122 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
LancasterUniversity | 66:2fc7d7c2fffc | 123 | * gives a different behaviour: |
LancasterUniversity | 66:2fc7d7c2fffc | 124 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 125 | * ASYNC - Will copy as many characters as it can into the buffer for transmission, |
LancasterUniversity | 66:2fc7d7c2fffc | 126 | * and return control to the user. |
LancasterUniversity | 66:2fc7d7c2fffc | 127 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 128 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
LancasterUniversity | 66:2fc7d7c2fffc | 129 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 130 | * SYNC_SLEEP - Will perform a cooperative blocking wait until all |
LancasterUniversity | 66:2fc7d7c2fffc | 131 | * given characters have been received by the connected |
LancasterUniversity | 66:2fc7d7c2fffc | 132 | * device. |
LancasterUniversity | 66:2fc7d7c2fffc | 133 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 134 | * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is |
LancasterUniversity | 66:2fc7d7c2fffc | 135 | * no connected device, or the connected device has not enabled indications. |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | */ |
LancasterUniversity | 66:2fc7d7c2fffc | 137 | int putc(char c, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | * Copies characters into the buffer used for Transmitting to the central device. |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | * @param buf a buffer containing length number of bytes. |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | * @param length the size of the buffer. |
LancasterUniversity | 66:2fc7d7c2fffc | 144 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
LancasterUniversity | 66:2fc7d7c2fffc | 145 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 146 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 147 | * ASYNC - Will copy as many characters as it can into the buffer for transmission, |
LancasterUniversity | 66:2fc7d7c2fffc | 148 | * and return control to the user. |
LancasterUniversity | 66:2fc7d7c2fffc | 149 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 150 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 152 | * SYNC_SLEEP - Will perform a cooperative blocking wait until all |
LancasterUniversity | 66:2fc7d7c2fffc | 153 | * given characters have been received by the connected |
LancasterUniversity | 66:2fc7d7c2fffc | 154 | * device. |
LancasterUniversity | 66:2fc7d7c2fffc | 155 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 156 | * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is |
LancasterUniversity | 66:2fc7d7c2fffc | 157 | * no connected device, or the connected device has not enabled indications. |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | */ |
LancasterUniversity | 66:2fc7d7c2fffc | 159 | int send(const uint8_t *buf, int length, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | * Copies characters into the buffer used for Transmitting to the central device. |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | * @param s the string to transmit |
LancasterUniversity | 66:2fc7d7c2fffc | 165 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
LancasterUniversity | 66:2fc7d7c2fffc | 166 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 168 | * ASYNC - Will copy as many characters as it can into the buffer for transmission, |
LancasterUniversity | 66:2fc7d7c2fffc | 169 | * and return control to the user. |
LancasterUniversity | 66:2fc7d7c2fffc | 170 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 171 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 172 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 173 | * SYNC_SLEEP - Will perform a cooperative blocking wait until all |
LancasterUniversity | 66:2fc7d7c2fffc | 174 | * given characters have been received by the connected |
LancasterUniversity | 66:2fc7d7c2fffc | 175 | * device. |
LancasterUniversity | 66:2fc7d7c2fffc | 176 | * |
LancasterUniversity | 66:2fc7d7c2fffc | 177 | * @return the number of characters written, or MICROBIT_NOT_SUPPORTED if there is |
LancasterUniversity | 66:2fc7d7c2fffc | 178 | * no connected device, or the connected device has not enabled indications. |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | */ |
LancasterUniversity | 66:2fc7d7c2fffc | 180 | int send(ManagedString s, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | * Reads a number of characters from the rxBuffer and fills user given buffer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | * @param buf a pointer to a buffer of len bytes. |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | * @param len the size of the user allocated buffer |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 189 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 190 | * ASYNC - Will attempt to read all available characters, and return immediately |
Jonathan Austin |
1:8aa5cdb4ab67 | 191 | * until the buffer limit is reached |
Jonathan Austin |
1:8aa5cdb4ab67 | 192 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 193 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 194 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 195 | * SYNC_SLEEP - Will first of all determine whether the given number of characters |
Jonathan Austin |
1:8aa5cdb4ab67 | 196 | * are available in our buffer, if not, it will set an event and sleep |
Jonathan Austin |
1:8aa5cdb4ab67 | 197 | * until the number of characters are avaialable. |
Jonathan Austin |
1:8aa5cdb4ab67 | 198 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 199 | * @return the number of characters digested |
Jonathan Austin |
1:8aa5cdb4ab67 | 200 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 201 | int read(uint8_t *buf, int len, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 202 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 203 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 204 | * Reads a number of characters from the rxBuffer and returns them as a ManagedString |
Jonathan Austin |
1:8aa5cdb4ab67 | 205 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 206 | * @param len the number of characters to read. |
Jonathan Austin |
1:8aa5cdb4ab67 | 207 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 208 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 209 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 210 | * ASYNC - Will attempt to read all available characters, and return immediately |
Jonathan Austin |
1:8aa5cdb4ab67 | 211 | * until the buffer limit is reached |
Jonathan Austin |
1:8aa5cdb4ab67 | 212 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 213 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 214 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 215 | * SYNC_SLEEP - Will first of all determine whether the given number of characters |
Jonathan Austin |
1:8aa5cdb4ab67 | 216 | * are available in our buffer, if not, it will set an event and sleep |
Jonathan Austin |
1:8aa5cdb4ab67 | 217 | * until the number of characters are avaialable. |
Jonathan Austin |
1:8aa5cdb4ab67 | 218 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 219 | * @return an empty ManagedString on error, or a ManagedString containing characters |
Jonathan Austin |
1:8aa5cdb4ab67 | 220 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 221 | ManagedString read(int len, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 222 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 223 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 224 | * Reads characters until a character matches one of the given delimeters |
Jonathan Austin |
1:8aa5cdb4ab67 | 225 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 226 | * @param delimeters the number of characters to match against |
Jonathan Austin |
1:8aa5cdb4ab67 | 227 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 228 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 229 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 230 | * ASYNC - Will attempt read the immediate buffer, and look for a match. |
Jonathan Austin |
1:8aa5cdb4ab67 | 231 | * If there isn't, an empty ManagedString will be returned. |
Jonathan Austin |
1:8aa5cdb4ab67 | 232 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 233 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 234 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 235 | * SYNC_SLEEP - Will first of all consider the characters in the immediate buffer, |
Jonathan Austin |
1:8aa5cdb4ab67 | 236 | * if a match is not found, it will block on an event, fired when a |
Jonathan Austin |
1:8aa5cdb4ab67 | 237 | * character is matched. |
Jonathan Austin |
1:8aa5cdb4ab67 | 238 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 239 | * @return an empty ManagedString on error, or a ManagedString containing characters |
Jonathan Austin |
1:8aa5cdb4ab67 | 240 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 241 | ManagedString readUntil(ManagedString delimeters, MicroBitSerialMode mode = SYNC_SLEEP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 242 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 243 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 244 | * Configures an event to be fired on a match with one of the delimeters. |
Jonathan Austin |
1:8aa5cdb4ab67 | 245 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 246 | * @param delimeters the characters to match received characters against e.g. ManagedString("\r\n") |
Jonathan Austin |
1:8aa5cdb4ab67 | 247 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 248 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 249 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 250 | * ASYNC - Will configure the event and return immediately. |
Jonathan Austin |
1:8aa5cdb4ab67 | 251 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 252 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 253 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 254 | * SYNC_SLEEP - Will configure the event and block the current fiber until the |
Jonathan Austin |
1:8aa5cdb4ab67 | 255 | * event is received. |
Jonathan Austin |
1:8aa5cdb4ab67 | 256 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 257 | * @return MICROBIT_INVALID_PARAMETER if the mode given is SYNC_SPINWAIT, otherwise MICROBIT_OK. |
Jonathan Austin |
1:8aa5cdb4ab67 | 258 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 259 | * @note delimeters are matched on a per byte basis. |
Jonathan Austin |
1:8aa5cdb4ab67 | 260 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 261 | int eventOn(ManagedString delimeters, MicroBitSerialMode mode = ASYNC); |
Jonathan Austin |
1:8aa5cdb4ab67 | 262 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 263 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 264 | * Configures an event to be fired after "len" characters. |
Jonathan Austin |
1:8aa5cdb4ab67 | 265 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 266 | * @param len the number of characters to wait before triggering the event |
Jonathan Austin |
1:8aa5cdb4ab67 | 267 | * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode |
Jonathan Austin |
1:8aa5cdb4ab67 | 268 | * gives a different behaviour: |
Jonathan Austin |
1:8aa5cdb4ab67 | 269 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 270 | * ASYNC - Will configure the event and return immediately. |
Jonathan Austin |
1:8aa5cdb4ab67 | 271 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 272 | * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 273 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 274 | * SYNC_SLEEP - Will configure the event and block the current fiber until the |
Jonathan Austin |
1:8aa5cdb4ab67 | 275 | * event is received. |
Jonathan Austin |
1:8aa5cdb4ab67 | 276 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 277 | * @return MICROBIT_INVALID_PARAMETER if the mode given is SYNC_SPINWAIT, otherwise MICROBIT_OK. |
Jonathan Austin |
1:8aa5cdb4ab67 | 278 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 279 | int eventAfter(int len, MicroBitSerialMode mode = ASYNC); |
Jonathan Austin |
1:8aa5cdb4ab67 | 280 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 281 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 282 | * Determines if we have space in our rxBuff. |
Jonathan Austin |
1:8aa5cdb4ab67 | 283 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 284 | * @return 1 if we have space, 0 if we do not. |
Jonathan Austin |
1:8aa5cdb4ab67 | 285 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 286 | int isReadable(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 287 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 288 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 289 | * @return The currently buffered number of bytes in our rxBuff. |
Jonathan Austin |
1:8aa5cdb4ab67 | 290 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 291 | int rxBufferedSize(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 292 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 293 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 294 | * @return The currently buffered number of bytes in our txBuff. |
Jonathan Austin |
1:8aa5cdb4ab67 | 295 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 296 | int txBufferedSize(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 297 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 298 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 299 | extern const uint8_t UARTServiceBaseUUID[UUID::LENGTH_OF_LONG_UUID]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 300 | extern const uint16_t UARTServiceShortUUID; |
Jonathan Austin |
1:8aa5cdb4ab67 | 301 | extern const uint16_t UARTServiceTXCharacteristicShortUUID; |
Jonathan Austin |
1:8aa5cdb4ab67 | 302 | extern const uint16_t UARTServiceRXCharacteristicShortUUID; |
Jonathan Austin |
1:8aa5cdb4ab67 | 303 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 304 | extern const uint8_t UARTServiceUUID[UUID::LENGTH_OF_LONG_UUID]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 305 | extern const uint8_t UARTServiceUUID_reversed[UUID::LENGTH_OF_LONG_UUID]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 306 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 307 | extern const uint8_t UARTServiceTXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 308 | extern const uint8_t UARTServiceRXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 309 | |
LancasterUniversity | 58:2ac8d45f1b08 | 310 | #endif |