4D display working with Gecko board STK3600
Fork of 4dGENIE by
mbed_genie.cpp@11:9196f72fc325, 2015-03-04 (annotated)
- Committer:
- chris215
- Date:
- Wed Mar 04 03:00:40 2015 +0000
- Revision:
- 11:9196f72fc325
- Parent:
- 10:4d9f1be4a901
Added new constructor overload with the option to specify the baud rate to use (Mbed side only)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris215 | 0:d2ed5a44c802 | 1 | #include "mbed.h" |
chris215 | 0:d2ed5a44c802 | 2 | #include "mbed_genie.h" |
chris215 | 0:d2ed5a44c802 | 3 | |
chris215 | 3:11c49c49cd1a | 4 | Mbed4dGenie::Mbed4dGenie(PinName TxPin,PinName RxPin, PinName resetpin) : _screen(TxPin,RxPin) , _reset(resetpin) |
chris215 | 2:f283764fe9b7 | 5 | { |
chris215 | 3:11c49c49cd1a | 6 | //reset the 4d screen |
chris215 | 3:11c49c49cd1a | 7 | _reset = 0; |
chris215 | 6:f4d3977b0eae | 8 | _screen.baud(9600); |
chris215 | 7:6edb20845684 | 9 | _genieUserHandler = NULL; |
chris215 | 8:b5ba0df2d0db | 10 | RxStateTimeoutErrors = 0; |
chris215 | 2:f283764fe9b7 | 11 | } |
chris215 | 11:9196f72fc325 | 12 | Mbed4dGenie::Mbed4dGenie(PinName TxPin,PinName RxPin, PinName resetpin,uint32_t baud) : _screen(TxPin,RxPin) , _reset(resetpin) |
chris215 | 11:9196f72fc325 | 13 | { |
chris215 | 11:9196f72fc325 | 14 | //reset the 4d screen |
chris215 | 11:9196f72fc325 | 15 | _reset = 0; |
chris215 | 11:9196f72fc325 | 16 | _screen.baud(baud); |
chris215 | 11:9196f72fc325 | 17 | _genieUserHandler = NULL; |
chris215 | 11:9196f72fc325 | 18 | RxStateTimeoutErrors = 0; |
chris215 | 11:9196f72fc325 | 19 | } |
chris215 | 6:f4d3977b0eae | 20 | void Mbed4dGenie::Start() |
chris215 | 6:f4d3977b0eae | 21 | { |
chris215 | 6:f4d3977b0eae | 22 | _reset = 1; |
chris215 | 7:6edb20845684 | 23 | wait(3.0); //4D datasheet says that the screen can take up to 3000 ms before |
chris215 | 7:6edb20845684 | 24 | //becomming responsive to serial commands. |
chris215 | 11:9196f72fc325 | 25 | _waitTimer.start(); |
chris215 | 11:9196f72fc325 | 26 | _receptionTimer.start(); |
chris215 | 7:6edb20845684 | 27 | Reset(); |
chris215 | 7:6edb20845684 | 28 | _genieFlushEventQueue(); |
chris215 | 7:6edb20845684 | 29 | _screen.attach(this,&Mbed4dGenie::RxIrqHandler,Serial::RxIrq); |
chris215 | 7:6edb20845684 | 30 | } |
chris215 | 7:6edb20845684 | 31 | |
chris215 | 11:9196f72fc325 | 32 | |
chris215 | 10:4d9f1be4a901 | 33 | ////////////////////// genieGetEventData //////////////////////// |
chris215 | 10:4d9f1be4a901 | 34 | // |
chris215 | 10:4d9f1be4a901 | 35 | // Returns the LSB and MSB of the event's data combined into |
chris215 | 10:4d9f1be4a901 | 36 | // a single uint16 |
chris215 | 10:4d9f1be4a901 | 37 | // |
chris215 | 10:4d9f1be4a901 | 38 | // The data is transmitted from the display in big-endian format |
chris215 | 10:4d9f1be4a901 | 39 | // and stored the same so the user can't just access it as an int |
chris215 | 10:4d9f1be4a901 | 40 | // directly from the structure. |
chris215 | 10:4d9f1be4a901 | 41 | // |
chris215 | 10:4d9f1be4a901 | 42 | uint16_t Mbed4dGenie::genieGetEventData (genieFrame * e) { |
chris215 | 10:4d9f1be4a901 | 43 | return (e->reportObject.data_msb << 8) + e->reportObject.data_lsb; |
chris215 | 10:4d9f1be4a901 | 44 | } |
chris215 | 7:6edb20845684 | 45 | |
chris215 | 7:6edb20845684 | 46 | void Mbed4dGenie::RxIrqHandler(void) |
chris215 | 7:6edb20845684 | 47 | { |
chris215 | 7:6edb20845684 | 48 | char c; |
chris215 | 7:6edb20845684 | 49 | //Loop to read all byte present in UART FIFO |
chris215 | 7:6edb20845684 | 50 | do |
chris215 | 7:6edb20845684 | 51 | { |
chris215 | 7:6edb20845684 | 52 | c = _screen.getc(); |
chris215 | 11:9196f72fc325 | 53 | /* |
chris215 | 11:9196f72fc325 | 54 | check if we are in reception mode and if we timeout |
chris215 | 11:9196f72fc325 | 55 | */ |
chris215 | 11:9196f72fc325 | 56 | if(_receptionTimer.read_ms() >= RxMaxTimeout && state == CommInProgress) |
chris215 | 9:b74deaac80f9 | 57 | { |
chris215 | 11:9196f72fc325 | 58 | /* |
chris215 | 11:9196f72fc325 | 59 | if we get here, it means something bad has happened and |
chris215 | 11:9196f72fc325 | 60 | we need to reet communication status |
chris215 | 11:9196f72fc325 | 61 | */ |
chris215 | 9:b74deaac80f9 | 62 | Reset(); |
chris215 | 9:b74deaac80f9 | 63 | } |
chris215 | 7:6edb20845684 | 64 | ManageReceiveData(c); |
chris215 | 7:6edb20845684 | 65 | } |
chris215 | 7:6edb20845684 | 66 | while(_screen.readable()); |
chris215 | 7:6edb20845684 | 67 | } |
chris215 | 7:6edb20845684 | 68 | |
chris215 | 7:6edb20845684 | 69 | void Mbed4dGenie::ManageReceiveData(char data) |
chris215 | 7:6edb20845684 | 70 | { |
chris215 | 7:6edb20845684 | 71 | switch(state) |
chris215 | 7:6edb20845684 | 72 | { |
chris215 | 7:6edb20845684 | 73 | case CommIdle: |
chris215 | 2:f283764fe9b7 | 74 | |
chris215 | 7:6edb20845684 | 75 | if(data == GENIE_ACK || data == GENIE_NAK) |
chris215 | 7:6edb20845684 | 76 | { |
chris215 | 7:6edb20845684 | 77 | LastResponse = data; |
chris215 | 7:6edb20845684 | 78 | } |
chris215 | 7:6edb20845684 | 79 | else if(data == GENIE_REPORT_OBJ || data == GENIE_REPORT_EVENT) |
chris215 | 7:6edb20845684 | 80 | { |
chris215 | 11:9196f72fc325 | 81 | //re-initialise the reception timer |
chris215 | 11:9196f72fc325 | 82 | _receptionTimer.reset(); |
chris215 | 11:9196f72fc325 | 83 | RxMaxTimeout = _receptionTimer.read_ms() + RESYNC_PERIOD; |
chris215 | 7:6edb20845684 | 84 | checksum = data; |
chris215 | 7:6edb20845684 | 85 | rx_data[rxframe_count++] = data; |
chris215 | 8:b5ba0df2d0db | 86 | state = CommInProgress; |
chris215 | 7:6edb20845684 | 87 | } |
chris215 | 7:6edb20845684 | 88 | break; |
chris215 | 7:6edb20845684 | 89 | |
chris215 | 7:6edb20845684 | 90 | |
chris215 | 7:6edb20845684 | 91 | case CommInProgress: |
chris215 | 7:6edb20845684 | 92 | checksum = checksum ^ data; |
chris215 | 7:6edb20845684 | 93 | rx_data[rxframe_count++] = data; |
chris215 | 7:6edb20845684 | 94 | |
chris215 | 7:6edb20845684 | 95 | if(rxframe_count >= GENIE_FRAME_SIZE) |
chris215 | 7:6edb20845684 | 96 | { |
chris215 | 7:6edb20845684 | 97 | if (checksum == 0) { |
chris215 | 7:6edb20845684 | 98 | _genieEnqueueEvent(rx_data); |
chris215 | 7:6edb20845684 | 99 | if(_genieUserHandler != NULL) |
chris215 | 7:6edb20845684 | 100 | { |
chris215 | 7:6edb20845684 | 101 | (_genieUserHandler)(); |
chris215 | 7:6edb20845684 | 102 | } |
chris215 | 7:6edb20845684 | 103 | } |
chris215 | 7:6edb20845684 | 104 | state = CommIdle; |
chris215 | 7:6edb20845684 | 105 | rxframe_count = 0; |
chris215 | 7:6edb20845684 | 106 | LastResponse = NO_RESPONSE; |
chris215 | 7:6edb20845684 | 107 | } |
chris215 | 7:6edb20845684 | 108 | break; |
chris215 | 7:6edb20845684 | 109 | default: |
chris215 | 7:6edb20845684 | 110 | state = CommIdle; |
chris215 | 7:6edb20845684 | 111 | rxframe_count = 0; |
chris215 | 7:6edb20845684 | 112 | LastResponse = NO_RESPONSE; |
chris215 | 7:6edb20845684 | 113 | break; |
chris215 | 7:6edb20845684 | 114 | } |
chris215 | 2:f283764fe9b7 | 115 | } |
chris215 | 2:f283764fe9b7 | 116 | |
chris215 | 7:6edb20845684 | 117 | |
chris215 | 2:f283764fe9b7 | 118 | ///////////////////////// genieWriteObject ////////////////////// |
chris215 | 0:d2ed5a44c802 | 119 | // |
chris215 | 2:f283764fe9b7 | 120 | // Write data to an object on the display |
chris215 | 0:d2ed5a44c802 | 121 | // |
chris215 | 6:f4d3977b0eae | 122 | int8_t Mbed4dGenie::genieWriteObject (uint16_t object, uint16_t index, uint16_t data) |
chris215 | 2:f283764fe9b7 | 123 | { |
chris215 | 6:f4d3977b0eae | 124 | uint16_t msb, lsb ; |
chris215 | 6:f4d3977b0eae | 125 | uint8_t checksum ; |
chris215 | 6:f4d3977b0eae | 126 | |
chris215 | 7:6edb20845684 | 127 | //wait for interface to be ready before sending stuff |
chris215 | 7:6edb20845684 | 128 | if(WaitForIdle()) |
chris215 | 7:6edb20845684 | 129 | return ERROR_RESYNC; |
chris215 | 7:6edb20845684 | 130 | |
chris215 | 6:f4d3977b0eae | 131 | lsb = data&0xFF; |
chris215 | 6:f4d3977b0eae | 132 | msb = (data>>8) & 0xFF; |
chris215 | 6:f4d3977b0eae | 133 | |
chris215 | 6:f4d3977b0eae | 134 | _screen.putc(GENIE_WRITE_OBJ) ; |
chris215 | 6:f4d3977b0eae | 135 | checksum = GENIE_WRITE_OBJ ; |
chris215 | 6:f4d3977b0eae | 136 | _screen.putc(object) ; |
chris215 | 6:f4d3977b0eae | 137 | checksum ^= object ; |
chris215 | 6:f4d3977b0eae | 138 | _screen.putc(index) ; |
chris215 | 6:f4d3977b0eae | 139 | checksum ^= index ; |
chris215 | 6:f4d3977b0eae | 140 | _screen.putc(msb) ; |
chris215 | 6:f4d3977b0eae | 141 | checksum ^= msb; |
chris215 | 6:f4d3977b0eae | 142 | _screen.putc(lsb) ; |
chris215 | 6:f4d3977b0eae | 143 | checksum ^= lsb; |
chris215 | 6:f4d3977b0eae | 144 | _screen.putc(checksum) ; |
chris215 | 6:f4d3977b0eae | 145 | |
chris215 | 7:6edb20845684 | 146 | return Mbed4dGenie::WaitForAnswer(); |
chris215 | 7:6edb20845684 | 147 | } |
chris215 | 7:6edb20845684 | 148 | int8_t Mbed4dGenie::genieWriteStr (uint16_t index, char *string) |
chris215 | 7:6edb20845684 | 149 | { |
chris215 | 7:6edb20845684 | 150 | char *p ; |
chris215 | 7:6edb20845684 | 151 | unsigned int checksum ; |
chris215 | 7:6edb20845684 | 152 | int len = strlen (string) ; |
chris215 | 7:6edb20845684 | 153 | |
chris215 | 7:6edb20845684 | 154 | |
chris215 | 7:6edb20845684 | 155 | if (len > 255) |
chris215 | 7:6edb20845684 | 156 | return -1 ; |
chris215 | 7:6edb20845684 | 157 | |
chris215 | 7:6edb20845684 | 158 | |
chris215 | 7:6edb20845684 | 159 | //wait for interface to be ready before sending stuff |
chris215 | 7:6edb20845684 | 160 | if(WaitForIdle()) |
chris215 | 7:6edb20845684 | 161 | return ERROR_RESYNC; |
chris215 | 7:6edb20845684 | 162 | |
chris215 | 7:6edb20845684 | 163 | |
chris215 | 7:6edb20845684 | 164 | _screen.putc(GENIE_WRITE_STR) ; checksum = GENIE_WRITE_STR ; |
chris215 | 7:6edb20845684 | 165 | _screen.putc(index) ; checksum ^= index ; |
chris215 | 7:6edb20845684 | 166 | _screen.putc((unsigned char)len) ; checksum ^= len ; |
chris215 | 7:6edb20845684 | 167 | for (p = string ; *p ; ++p) { |
chris215 | 7:6edb20845684 | 168 | _screen.putc (*p) ; |
chris215 | 7:6edb20845684 | 169 | checksum ^= *p ; |
chris215 | 7:6edb20845684 | 170 | } |
chris215 | 7:6edb20845684 | 171 | _screen.putc(checksum) ; |
chris215 | 2:f283764fe9b7 | 172 | |
chris215 | 7:6edb20845684 | 173 | return Mbed4dGenie::WaitForAnswer(); |
chris215 | 7:6edb20845684 | 174 | } |
chris215 | 7:6edb20845684 | 175 | int8_t Mbed4dGenie::genieReadObj (uint16_t object, uint16_t index) |
chris215 | 7:6edb20845684 | 176 | { |
chris215 | 7:6edb20845684 | 177 | //wait for interface to be ready before sending stuff |
chris215 | 7:6edb20845684 | 178 | if(WaitForIdle()) |
chris215 | 7:6edb20845684 | 179 | return ERROR_RESYNC; |
chris215 | 7:6edb20845684 | 180 | unsigned int checksum ; |
chris215 | 7:6edb20845684 | 181 | |
chris215 | 7:6edb20845684 | 182 | _screen.putc(GENIE_READ_OBJ) ; checksum = GENIE_READ_OBJ ; |
chris215 | 7:6edb20845684 | 183 | _screen.putc(object) ; checksum ^= object ; |
chris215 | 7:6edb20845684 | 184 | _screen.putc(index) ; checksum ^= index ; |
chris215 | 7:6edb20845684 | 185 | _screen.putc(checksum) ; |
chris215 | 7:6edb20845684 | 186 | |
chris215 | 8:b5ba0df2d0db | 187 | //Here we dont wiat for a typical answer |
chris215 | 8:b5ba0df2d0db | 188 | //The screen will respond with an NACK if the command was not understood, otherwise it will send a report object frame |
chris215 | 9:b74deaac80f9 | 189 | return 0;//WaitForReadAnswer(); |
chris215 | 0:d2ed5a44c802 | 190 | } |
chris215 | 7:6edb20845684 | 191 | void Mbed4dGenie::writec(char data) |
chris215 | 7:6edb20845684 | 192 | { |
chris215 | 7:6edb20845684 | 193 | _screen.putc(data); |
chris215 | 7:6edb20845684 | 194 | } |
chris215 | 7:6edb20845684 | 195 | bool Mbed4dGenie::WaitForIdle() |
chris215 | 7:6edb20845684 | 196 | { |
chris215 | 11:9196f72fc325 | 197 | /* |
chris215 | 11:9196f72fc325 | 198 | If the communication is still in progress |
chris215 | 11:9196f72fc325 | 199 | lets wait for it to finish or check/wait for timeout |
chris215 | 11:9196f72fc325 | 200 | */ |
chris215 | 8:b5ba0df2d0db | 201 | if(state != CommIdle) |
chris215 | 8:b5ba0df2d0db | 202 | { |
chris215 | 11:9196f72fc325 | 203 | long timeout = _receptionTimer.read_ms() + TIMEOUT_PERIOD; |
chris215 | 8:b5ba0df2d0db | 204 | long timerReading = 0; |
chris215 | 11:9196f72fc325 | 205 | if(_receptionTimer.read_ms() >= RxMaxTimeout) |
chris215 | 8:b5ba0df2d0db | 206 | { |
chris215 | 8:b5ba0df2d0db | 207 | Reset(); |
chris215 | 8:b5ba0df2d0db | 208 | RxStateTimeoutErrors++; |
chris215 | 8:b5ba0df2d0db | 209 | } |
chris215 | 8:b5ba0df2d0db | 210 | while(timerReading < timeout && state != CommIdle) |
chris215 | 8:b5ba0df2d0db | 211 | { |
chris215 | 11:9196f72fc325 | 212 | timerReading = _receptionTimer.read_ms(); |
chris215 | 8:b5ba0df2d0db | 213 | } |
chris215 | 8:b5ba0df2d0db | 214 | LastResponse = 0; |
chris215 | 8:b5ba0df2d0db | 215 | return (timerReading >= timeout); |
chris215 | 8:b5ba0df2d0db | 216 | } |
chris215 | 8:b5ba0df2d0db | 217 | |
chris215 | 8:b5ba0df2d0db | 218 | return false; |
chris215 | 8:b5ba0df2d0db | 219 | } |
chris215 | 8:b5ba0df2d0db | 220 | |
chris215 | 11:9196f72fc325 | 221 | /* |
chris215 | 11:9196f72fc325 | 222 | Not used for now, since the screen will answer with either a report event or NAK |
chris215 | 11:9196f72fc325 | 223 | Since those two answers are so different, there is no efficient means to receive |
chris215 | 11:9196f72fc325 | 224 | both in interrupt. No wait to NAK will be done. DONT USE THIS FUNCTION ... YET |
chris215 | 11:9196f72fc325 | 225 | */ |
chris215 | 8:b5ba0df2d0db | 226 | int8_t Mbed4dGenie::WaitForReadAnswer() |
chris215 | 8:b5ba0df2d0db | 227 | { |
chris215 | 11:9196f72fc325 | 228 | _waitTimer.reset(); |
chris215 | 11:9196f72fc325 | 229 | long timeout = _waitTimer.read_ms() + TIMEOUT_PERIOD; |
chris215 | 7:6edb20845684 | 230 | long timerReading = 0; |
chris215 | 8:b5ba0df2d0db | 231 | while(state == CommIdle && LastResponse != ERROR_NAK && timerReading < timeout) |
chris215 | 7:6edb20845684 | 232 | { |
chris215 | 11:9196f72fc325 | 233 | timerReading = _waitTimer.read_ms(); |
chris215 | 7:6edb20845684 | 234 | } |
chris215 | 8:b5ba0df2d0db | 235 | if(LastResponse == ERROR_NAK)//check if the screen returned a NACK |
chris215 | 8:b5ba0df2d0db | 236 | { |
chris215 | 8:b5ba0df2d0db | 237 | LastResponse = NO_RESPONSE; |
chris215 | 8:b5ba0df2d0db | 238 | return ERROR_NAK; |
chris215 | 8:b5ba0df2d0db | 239 | } |
chris215 | 11:9196f72fc325 | 240 | else if(_waitTimer.read_ms() >= timeout) //check if we timed out while waiting for response |
chris215 | 8:b5ba0df2d0db | 241 | { |
chris215 | 9:b74deaac80f9 | 242 | |
chris215 | 8:b5ba0df2d0db | 243 | LastResponse = NO_RESPONSE; |
chris215 | 8:b5ba0df2d0db | 244 | return ERROR_TIMEOUT; |
chris215 | 8:b5ba0df2d0db | 245 | } |
chris215 | 8:b5ba0df2d0db | 246 | //if we get here it means we didnt timeout and the screen did accept the command |
chris215 | 8:b5ba0df2d0db | 247 | LastResponse = NO_RESPONSE; |
chris215 | 8:b5ba0df2d0db | 248 | return ERROR_NONE; |
chris215 | 7:6edb20845684 | 249 | } |
chris215 | 0:d2ed5a44c802 | 250 | |
chris215 | 6:f4d3977b0eae | 251 | int8_t Mbed4dGenie::WaitForAnswer() |
chris215 | 6:f4d3977b0eae | 252 | { |
chris215 | 11:9196f72fc325 | 253 | _waitTimer.reset(); |
chris215 | 11:9196f72fc325 | 254 | long timeout = _waitTimer.read_ms() + TIMEOUT_PERIOD; |
chris215 | 6:f4d3977b0eae | 255 | long timerReading = 0; |
chris215 | 7:6edb20845684 | 256 | while(LastResponse != GENIE_ACK && LastResponse != ERROR_NAK && timerReading < timeout) |
chris215 | 6:f4d3977b0eae | 257 | { |
chris215 | 11:9196f72fc325 | 258 | timerReading = _waitTimer.read_ms(); |
chris215 | 6:f4d3977b0eae | 259 | } |
chris215 | 6:f4d3977b0eae | 260 | |
chris215 | 7:6edb20845684 | 261 | if(LastResponse == ERROR_NAK) |
chris215 | 6:f4d3977b0eae | 262 | { |
chris215 | 8:b5ba0df2d0db | 263 | LastResponse = NO_RESPONSE; |
chris215 | 8:b5ba0df2d0db | 264 | return ERROR_NAK; |
chris215 | 6:f4d3977b0eae | 265 | } |
chris215 | 11:9196f72fc325 | 266 | else if(_waitTimer.read_ms() >= timeout) |
chris215 | 6:f4d3977b0eae | 267 | { |
chris215 | 11:9196f72fc325 | 268 | printf("Current timer:%d ; timeout:%d\n\r",_waitTimer.read_ms(),timeout); |
chris215 | 8:b5ba0df2d0db | 269 | LastResponse = NO_RESPONSE; |
chris215 | 6:f4d3977b0eae | 270 | return ERROR_TIMEOUT; |
chris215 | 6:f4d3977b0eae | 271 | } |
chris215 | 8:b5ba0df2d0db | 272 | LastResponse = NO_RESPONSE; |
chris215 | 6:f4d3977b0eae | 273 | return ERROR_NONE; |
chris215 | 7:6edb20845684 | 274 | } |
chris215 | 7:6edb20845684 | 275 | |
chris215 | 7:6edb20845684 | 276 | void Mbed4dGenie::Reset(void) |
chris215 | 7:6edb20845684 | 277 | { |
chris215 | 7:6edb20845684 | 278 | LastResponse = NO_RESPONSE; |
chris215 | 7:6edb20845684 | 279 | state = CommIdle; |
chris215 | 11:9196f72fc325 | 280 | _receptionTimer.reset(); |
chris215 | 11:9196f72fc325 | 281 | _waitTimer.reset(); |
chris215 | 7:6edb20845684 | 282 | while(_screen.readable()) |
chris215 | 7:6edb20845684 | 283 | _screen.getc(); |
chris215 | 7:6edb20845684 | 284 | } |
chris215 | 7:6edb20845684 | 285 | |
chris215 | 7:6edb20845684 | 286 | ////////////////////// _genieFlushEventQueue //////////////////// |
chris215 | 7:6edb20845684 | 287 | // |
chris215 | 7:6edb20845684 | 288 | // Reset all the event queue variables and start from scratch. |
chris215 | 7:6edb20845684 | 289 | // |
chris215 | 7:6edb20845684 | 290 | void Mbed4dGenie::_genieFlushEventQueue(void) { |
chris215 | 7:6edb20845684 | 291 | _genieEventQueue.rd_index = 0; |
chris215 | 7:6edb20845684 | 292 | _genieEventQueue.wr_index = 0; |
chris215 | 7:6edb20845684 | 293 | _genieEventQueue.n_events = 0; |
chris215 | 7:6edb20845684 | 294 | } |
chris215 | 7:6edb20845684 | 295 | |
chris215 | 7:6edb20845684 | 296 | ////////////////////// _genieEnqueueEvent /////////////////// |
chris215 | 7:6edb20845684 | 297 | // |
chris215 | 7:6edb20845684 | 298 | // Copy the bytes from a buffer supplied by the caller |
chris215 | 7:6edb20845684 | 299 | // to the input queue |
chris215 | 7:6edb20845684 | 300 | // |
chris215 | 7:6edb20845684 | 301 | // Parms: uint8_t * data, a pointer to the user's data |
chris215 | 7:6edb20845684 | 302 | // |
chris215 | 7:6edb20845684 | 303 | // Returns: TRUE if there was an empty location in the queue |
chris215 | 7:6edb20845684 | 304 | // to copy the data into |
chris215 | 7:6edb20845684 | 305 | // FALSE if not |
chris215 | 7:6edb20845684 | 306 | // Sets: ERROR_REPLY_OVR if there was no room in the queue |
chris215 | 7:6edb20845684 | 307 | // |
chris215 | 7:6edb20845684 | 308 | bool Mbed4dGenie::_genieEnqueueEvent (uint8_t * data) { |
chris215 | 7:6edb20845684 | 309 | |
chris215 | 7:6edb20845684 | 310 | |
chris215 | 7:6edb20845684 | 311 | if (_genieEventQueue.n_events < MAX_GENIE_EVENTS-2) { |
chris215 | 7:6edb20845684 | 312 | memcpy (&_genieEventQueue.frames[_genieEventQueue.wr_index], data, |
chris215 | 7:6edb20845684 | 313 | GENIE_FRAME_SIZE); |
chris215 | 7:6edb20845684 | 314 | _genieEventQueue.wr_index++; |
chris215 | 7:6edb20845684 | 315 | _genieEventQueue.wr_index &= MAX_GENIE_EVENTS -1; |
chris215 | 7:6edb20845684 | 316 | _genieEventQueue.n_events++; |
chris215 | 7:6edb20845684 | 317 | return TRUE; |
chris215 | 7:6edb20845684 | 318 | } else { |
chris215 | 7:6edb20845684 | 319 | return FALSE; |
chris215 | 7:6edb20845684 | 320 | } |
chris215 | 7:6edb20845684 | 321 | } |
chris215 | 7:6edb20845684 | 322 | ////////////////////// genieDequeueEvent /////////////////// |
chris215 | 7:6edb20845684 | 323 | // |
chris215 | 7:6edb20845684 | 324 | // Copy the bytes from a queued input event to a buffer supplied |
chris215 | 7:6edb20845684 | 325 | // by the caller. |
chris215 | 7:6edb20845684 | 326 | // |
chris215 | 7:6edb20845684 | 327 | // Parms: genieFrame * buff, a pointer to the user's buffer |
chris215 | 7:6edb20845684 | 328 | // |
chris215 | 7:6edb20845684 | 329 | // Returns: TRUE if there was an event to copy |
chris215 | 7:6edb20845684 | 330 | // FALSE if not |
chris215 | 7:6edb20845684 | 331 | // |
chris215 | 7:6edb20845684 | 332 | bool Mbed4dGenie::genieDequeueEvent(genieFrame * buff) { |
chris215 | 7:6edb20845684 | 333 | |
chris215 | 7:6edb20845684 | 334 | |
chris215 | 7:6edb20845684 | 335 | if (_genieEventQueue.n_events > 0) { |
chris215 | 7:6edb20845684 | 336 | memcpy (buff, &_genieEventQueue.frames[_genieEventQueue.rd_index], |
chris215 | 7:6edb20845684 | 337 | GENIE_FRAME_SIZE); |
chris215 | 7:6edb20845684 | 338 | _genieEventQueue.rd_index++; |
chris215 | 7:6edb20845684 | 339 | _genieEventQueue.rd_index &= MAX_GENIE_EVENTS -1; |
chris215 | 7:6edb20845684 | 340 | _genieEventQueue.n_events--; |
chris215 | 7:6edb20845684 | 341 | return TRUE; |
chris215 | 7:6edb20845684 | 342 | } |
chris215 | 7:6edb20845684 | 343 | return FALSE; |
chris215 | 7:6edb20845684 | 344 | } |
chris215 | 7:6edb20845684 | 345 | void Mbed4dGenie::genieAttachEventHandler(genieUserEventHandlerPtr handler) |
chris215 | 7:6edb20845684 | 346 | { |
chris215 | 7:6edb20845684 | 347 | _genieUserHandler = handler; |
chris215 | 7:6edb20845684 | 348 | } |
chris215 | 7:6edb20845684 | 349 | bool Mbed4dGenie::PendingFrames(void) |
chris215 | 7:6edb20845684 | 350 | { |
chris215 | 7:6edb20845684 | 351 | return (_genieEventQueue.n_events>0); |
chris215 | 2:f283764fe9b7 | 352 | } |