A fork of the mbed 4dgenie library with updated baud rate and reset function

Fork of 4dGENIE by Alexander Lang

Committer:
langster1980
Date:
Fri Jul 04 18:28:14 2014 +0000
Revision:
5:bf1df2d55307
Parent:
3:11c49c49cd1a
updated serial comms rate to 4D display to 115200 and inverted reset function pin toggle.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris215 0:d2ed5a44c802 1
chris215 0:d2ed5a44c802 2 #include "mbed.h"
chris215 0:d2ed5a44c802 3 #include "mbed_genie.h"
chris215 0:d2ed5a44c802 4
chris215 0:d2ed5a44c802 5 Serial pc(USBTX,USBRX);
chris215 0:d2ed5a44c802 6
chris215 3:11c49c49cd1a 7 Mbed4dGenie::Mbed4dGenie(PinName TxPin,PinName RxPin, PinName resetpin) : _screen(TxPin,RxPin) , _reset(resetpin)
chris215 2:f283764fe9b7 8 {
chris215 3:11c49c49cd1a 9 //reset the 4d screen
chris215 3:11c49c49cd1a 10 _reset = 1;
chris215 3:11c49c49cd1a 11 wait(0.25);
chris215 3:11c49c49cd1a 12 _reset = 0;
langster1980 5:bf1df2d55307 13 wait(0.25);
langster1980 5:bf1df2d55307 14 _reset = 1;
chris215 2:f283764fe9b7 15 _genieLinkStates[4] = GENIE_LINK_IDLE;
chris215 2:f283764fe9b7 16 _genieLinkStates[3] = GENIE_LINK_IDLE;
chris215 2:f283764fe9b7 17 _genieLinkStates[2] = GENIE_LINK_IDLE;
chris215 2:f283764fe9b7 18 _genieLinkStates[1] = GENIE_LINK_IDLE;
chris215 2:f283764fe9b7 19 _genieLinkStates[0] = GENIE_LINK_IDLE;
chris215 2:f283764fe9b7 20 _genieLinkState = &_genieLinkStates[0];
chris215 2:f283764fe9b7 21
chris215 2:f283764fe9b7 22 _genieTimeout = TIMEOUT_PERIOD;
chris215 2:f283764fe9b7 23 _genieError = ERROR_NONE;
chris215 2:f283764fe9b7 24 rxframe_count = 0;
chris215 2:f283764fe9b7 25
chris215 2:f283764fe9b7 26 _genieUserHandler = NULL;
langster1980 5:bf1df2d55307 27 _screen.baud (115200); //set screen baud rate to 9600
chris215 2:f283764fe9b7 28 _screen.attach(this,&Mbed4dGenie::RxIrqHandler,Serial::RxIrq);
chris215 2:f283764fe9b7 29 _t.start();
chris215 2:f283764fe9b7 30 }
chris215 2:f283764fe9b7 31 void Mbed4dGenie::genieAttachEventHandler(genieUserEventHandlerPtr handler)
chris215 2:f283764fe9b7 32 {
chris215 2:f283764fe9b7 33 _genieUserHandler = handler;
chris215 2:f283764fe9b7 34 }
chris215 2:f283764fe9b7 35 void Mbed4dGenie::RxIrqHandler(void)
chris215 2:f283764fe9b7 36 {
chris215 2:f283764fe9b7 37 do
chris215 2:f283764fe9b7 38 {
chris215 3:11c49c49cd1a 39 genieDoEvents();
chris215 2:f283764fe9b7 40 }
chris215 2:f283764fe9b7 41 while(_screen.readable ());
chris215 2:f283764fe9b7 42 }
chris215 2:f283764fe9b7 43 //////////////////////// _genieGetchar //////////////////////////
chris215 2:f283764fe9b7 44 //
chris215 2:f283764fe9b7 45 // Get a character from the selected Genie serial port
chris215 2:f283764fe9b7 46 //
chris215 2:f283764fe9b7 47 // Returns: ERROR_NOHANDLER if an Rx handler has not
chris215 2:f283764fe9b7 48 // been defined
chris215 2:f283764fe9b7 49 // ERROR_NOCHAR if no bytes have beeb received
chris215 2:f283764fe9b7 50 // The char if there was one to get
chris215 2:f283764fe9b7 51 // Sets: _genieError with any errors encountered
chris215 2:f283764fe9b7 52 //
chris215 2:f283764fe9b7 53 uint8_t Mbed4dGenie::_genieGetchar() {
chris215 2:f283764fe9b7 54
chris215 2:f283764fe9b7 55 _genieError = ERROR_NONE;
chris215 2:f283764fe9b7 56
chris215 2:f283764fe9b7 57 return (_screen.getc());
chris215 2:f283764fe9b7 58 }
langster1980 5:bf1df2d55307 59
chris215 2:f283764fe9b7 60 ///////////////////// _genieSetLinkState ////////////////////////
chris215 2:f283764fe9b7 61 //
chris215 2:f283764fe9b7 62 // Set the logical state of the link to the display.
chris215 2:f283764fe9b7 63 //
chris215 2:f283764fe9b7 64 // Parms: uint16_t newstate, a value to be written to the
chris215 2:f283764fe9b7 65 // link's _genieLinkState variable. Valid values are
chris215 2:f283764fe9b7 66 // GENIE_LINK_IDLE 0
chris215 2:f283764fe9b7 67 // GENIE_LINK_WFAN 1 // waiting for Ack or Nak
chris215 2:f283764fe9b7 68 // GENIE_LINK_WF_RXREPORT 2 // waiting for a report frame
chris215 2:f283764fe9b7 69 // GENIE_LINK_RXREPORT 3 // receiving a report frame
chris215 2:f283764fe9b7 70 // GENIE_LINK_RXEVENT 4 // receiving an event frame
chris215 2:f283764fe9b7 71 // GENIE_LINK_SHDN 5
chris215 2:f283764fe9b7 72 //
chris215 2:f283764fe9b7 73 void Mbed4dGenie::_genieSetLinkState (uint16_t newstate) {
chris215 2:f283764fe9b7 74
chris215 2:f283764fe9b7 75 *_genieLinkState = newstate;
chris215 0:d2ed5a44c802 76
chris215 0:d2ed5a44c802 77
chris215 2:f283764fe9b7 78 if (newstate == GENIE_LINK_RXREPORT || \
chris215 2:f283764fe9b7 79 newstate == GENIE_LINK_RXEVENT)
chris215 2:f283764fe9b7 80 rxframe_count = 0;
chris215 2:f283764fe9b7 81 }
chris215 2:f283764fe9b7 82
chris215 2:f283764fe9b7 83
chris215 2:f283764fe9b7 84 /////////////////////// _genieGetLinkState //////////////////////
chris215 2:f283764fe9b7 85 //
chris215 2:f283764fe9b7 86 // Get the current logical state of the link to the display.
chris215 2:f283764fe9b7 87 //
chris215 2:f283764fe9b7 88 uint16_t Mbed4dGenie::_genieGetLinkState (void) {
chris215 2:f283764fe9b7 89 return *_genieLinkState;
chris215 2:f283764fe9b7 90 }
chris215 2:f283764fe9b7 91
chris215 2:f283764fe9b7 92 /////////////////////// _geniePutchar ///////////////////////////
chris215 2:f283764fe9b7 93 //
chris215 2:f283764fe9b7 94 // Output the supplied character to the Genie display over
chris215 2:f283764fe9b7 95 // the selected serial port
chris215 2:f283764fe9b7 96 //
chris215 2:f283764fe9b7 97 void Mbed4dGenie::_geniePutchar (uint8_t c) {
chris215 2:f283764fe9b7 98 // if (screen != NULL)
chris215 2:f283764fe9b7 99 _screen.putc(c);
chris215 2:f283764fe9b7 100 }
chris215 2:f283764fe9b7 101 ////////////////////// _genieEnqueueEvent ///////////////////
chris215 2:f283764fe9b7 102 //
chris215 2:f283764fe9b7 103 // Copy the bytes from a buffer supplied by the caller
chris215 2:f283764fe9b7 104 // to the input queue
chris215 2:f283764fe9b7 105 //
chris215 2:f283764fe9b7 106 // Parms: uint8_t * data, a pointer to the user's data
chris215 0:d2ed5a44c802 107 //
chris215 2:f283764fe9b7 108 // Returns: TRUE if there was an empty location in the queue
chris215 2:f283764fe9b7 109 // to copy the data into
chris215 2:f283764fe9b7 110 // FALSE if not
chris215 2:f283764fe9b7 111 // Sets: ERROR_REPLY_OVR if there was no room in the queue
chris215 2:f283764fe9b7 112 //
chris215 2:f283764fe9b7 113 bool Mbed4dGenie::_genieEnqueueEvent (uint8_t * data) {
chris215 2:f283764fe9b7 114
chris215 0:d2ed5a44c802 115
chris215 2:f283764fe9b7 116 if (_genieEventQueue.n_events < MAX_GENIE_EVENTS-2) {
chris215 2:f283764fe9b7 117 memcpy (&_genieEventQueue.frames[_genieEventQueue.wr_index], data,
chris215 2:f283764fe9b7 118 GENIE_FRAME_SIZE);
chris215 2:f283764fe9b7 119 _genieEventQueue.wr_index++;
chris215 2:f283764fe9b7 120 _genieEventQueue.wr_index &= MAX_GENIE_EVENTS -1;
chris215 2:f283764fe9b7 121 _genieEventQueue.n_events++;
chris215 2:f283764fe9b7 122 return TRUE;
chris215 2:f283764fe9b7 123 } else {
chris215 2:f283764fe9b7 124 _genieError = ERROR_REPLY_OVR;
chris215 2:f283764fe9b7 125 _handleError();
chris215 2:f283764fe9b7 126 return FALSE;
chris215 2:f283764fe9b7 127 }
chris215 2:f283764fe9b7 128 }
chris215 2:f283764fe9b7 129 ////////////////////// genieDequeueEvent ///////////////////
chris215 0:d2ed5a44c802 130 //
chris215 2:f283764fe9b7 131 // Copy the bytes from a queued input event to a buffer supplied
chris215 2:f283764fe9b7 132 // by the caller.
chris215 2:f283764fe9b7 133 //
chris215 2:f283764fe9b7 134 // Parms: genieFrame * buff, a pointer to the user's buffer
chris215 2:f283764fe9b7 135 //
chris215 2:f283764fe9b7 136 // Returns: TRUE if there was an event to copy
chris215 2:f283764fe9b7 137 // FALSE if not
chris215 2:f283764fe9b7 138 //
chris215 2:f283764fe9b7 139 bool Mbed4dGenie::genieDequeueEvent(genieFrame * buff) {
chris215 0:d2ed5a44c802 140
chris215 0:d2ed5a44c802 141
chris215 2:f283764fe9b7 142 if (_genieEventQueue.n_events > 0) {
chris215 2:f283764fe9b7 143 memcpy (buff, &_genieEventQueue.frames[_genieEventQueue.rd_index],
chris215 2:f283764fe9b7 144 GENIE_FRAME_SIZE);
chris215 2:f283764fe9b7 145 _genieEventQueue.rd_index++;
chris215 2:f283764fe9b7 146 _genieEventQueue.rd_index &= MAX_GENIE_EVENTS -1;
chris215 2:f283764fe9b7 147 _genieEventQueue.n_events--;
chris215 2:f283764fe9b7 148 return TRUE;
chris215 2:f283764fe9b7 149 }
chris215 2:f283764fe9b7 150 return FALSE;
chris215 2:f283764fe9b7 151 }
chris215 2:f283764fe9b7 152 ////////////////////// _genieWaitForIdle ////////////////////////
chris215 2:f283764fe9b7 153 //
chris215 2:f283764fe9b7 154 // Wait for the link to become idle or for the timeout period,
chris215 2:f283764fe9b7 155 // whichever comes first.
chris215 0:d2ed5a44c802 156 //
chris215 2:f283764fe9b7 157 void Mbed4dGenie::_genieWaitForIdle (void) {
chris215 2:f283764fe9b7 158 uint16_t do_event_result;
chris215 2:f283764fe9b7 159 long timeout = _t.read_ms() + _genieTimeout;
chris215 2:f283764fe9b7 160
chris215 2:f283764fe9b7 161 for ( ; _t.read_ms() < timeout;) {
chris215 2:f283764fe9b7 162
chris215 2:f283764fe9b7 163
chris215 2:f283764fe9b7 164 do_event_result = genieDoEvents();
chris215 2:f283764fe9b7 165 // if there was a character received from the
chris215 2:f283764fe9b7 166 // display restart the timeout because doEvents
chris215 2:f283764fe9b7 167 // is in the process of receiving something
chris215 2:f283764fe9b7 168 if (do_event_result == GENIE_EVENT_RXCHAR) {
chris215 2:f283764fe9b7 169 timeout = _t.read_ms() + _genieTimeout;
chris215 2:f283764fe9b7 170 return;
chris215 2:f283764fe9b7 171 }
chris215 2:f283764fe9b7 172
chris215 2:f283764fe9b7 173 if (_genieGetLinkState() == GENIE_LINK_IDLE) {
chris215 2:f283764fe9b7 174 return;
chris215 2:f283764fe9b7 175 }
chris215 2:f283764fe9b7 176 }
chris215 2:f283764fe9b7 177 _genieError = ERROR_TIMEOUT;
chris215 2:f283764fe9b7 178 _handleError();
chris215 2:f283764fe9b7 179 return;
chris215 2:f283764fe9b7 180 }
chris215 2:f283764fe9b7 181 ///////////////////////// genieWriteObject //////////////////////
chris215 0:d2ed5a44c802 182 //
chris215 2:f283764fe9b7 183 // Write data to an object on the display
chris215 0:d2ed5a44c802 184 //
chris215 2:f283764fe9b7 185 uint16_t Mbed4dGenie::genieWriteObject (uint16_t object, uint16_t index, uint16_t data)
chris215 2:f283764fe9b7 186 {
chris215 2:f283764fe9b7 187 uint16_t msb, lsb ;
chris215 2:f283764fe9b7 188 uint8_t checksum ;
chris215 2:f283764fe9b7 189
chris215 2:f283764fe9b7 190
chris215 2:f283764fe9b7 191 _genieWaitForIdle();
chris215 2:f283764fe9b7 192
chris215 2:f283764fe9b7 193
chris215 2:f283764fe9b7 194 lsb = data&0xFF;
chris215 2:f283764fe9b7 195 msb = (data>>8) & 0xFF;
chris215 2:f283764fe9b7 196
chris215 2:f283764fe9b7 197
chris215 2:f283764fe9b7 198 _genieError = ERROR_NONE;
chris215 2:f283764fe9b7 199
chris215 2:f283764fe9b7 200
chris215 2:f283764fe9b7 201 _geniePutchar(GENIE_WRITE_OBJ) ; checksum = GENIE_WRITE_OBJ ;
chris215 2:f283764fe9b7 202 _geniePutchar(object) ; checksum ^= object ;
chris215 2:f283764fe9b7 203 _geniePutchar(index) ; checksum ^= index ;
chris215 2:f283764fe9b7 204 _geniePutchar(msb) ; checksum ^= msb;
chris215 2:f283764fe9b7 205 _geniePutchar(lsb) ; checksum ^= lsb;
chris215 2:f283764fe9b7 206 _geniePutchar(checksum) ;
chris215 0:d2ed5a44c802 207
chris215 0:d2ed5a44c802 208
chris215 2:f283764fe9b7 209 _geniePushLinkState(GENIE_LINK_WFAN);
chris215 2:f283764fe9b7 210 return GENIE_EVENT_NONE;
chris215 2:f283764fe9b7 211 }
chris215 2:f283764fe9b7 212 /////////////////////// genieWriteContrast //////////////////////
chris215 2:f283764fe9b7 213 //
chris215 2:f283764fe9b7 214 // Alter the display contrast (backlight)
chris215 2:f283764fe9b7 215 //
chris215 2:f283764fe9b7 216 // Parms: uint8_t value: The required contrast setting, only
chris215 2:f283764fe9b7 217 // values from 0 to 15 are valid. 0 or 1 for most displays
chris215 2:f283764fe9b7 218 // and 0 to 15 for the uLCD-43
chris215 2:f283764fe9b7 219 //
chris215 2:f283764fe9b7 220 void Mbed4dGenie::genieWriteContrast (uint16_t value) {
chris215 2:f283764fe9b7 221 unsigned int checksum ;
chris215 2:f283764fe9b7 222
chris215 2:f283764fe9b7 223
chris215 2:f283764fe9b7 224 _genieWaitForIdle();
chris215 0:d2ed5a44c802 225
chris215 0:d2ed5a44c802 226
chris215 2:f283764fe9b7 227 _geniePutchar(GENIE_WRITE_CONTRAST) ; checksum = GENIE_WRITE_CONTRAST ;
chris215 2:f283764fe9b7 228 _geniePutchar(value) ; checksum ^= value ;
chris215 2:f283764fe9b7 229 _geniePutchar(checksum) ;
chris215 2:f283764fe9b7 230
chris215 2:f283764fe9b7 231
chris215 2:f283764fe9b7 232 _geniePushLinkState(GENIE_LINK_WFAN);
chris215 2:f283764fe9b7 233
chris215 2:f283764fe9b7 234
chris215 2:f283764fe9b7 235 }
chris215 2:f283764fe9b7 236 //////////////////////// _genieWriteStrX ///////////////////////
chris215 2:f283764fe9b7 237 //
chris215 2:f283764fe9b7 238 // Non-user function used by genieWriteStr() and genieWriteStrU()
chris215 2:f283764fe9b7 239 //
chris215 2:f283764fe9b7 240 int Mbed4dGenie::_genieWriteStrX (uint16_t code, uint16_t index, char *string)
chris215 2:f283764fe9b7 241 {
chris215 2:f283764fe9b7 242 char *p ;
chris215 2:f283764fe9b7 243 unsigned int checksum ;
chris215 2:f283764fe9b7 244 int len = strlen (string) ;
chris215 0:d2ed5a44c802 245
chris215 0:d2ed5a44c802 246
chris215 2:f283764fe9b7 247 if (len > 255)
chris215 2:f283764fe9b7 248 return -1 ;
chris215 2:f283764fe9b7 249
chris215 2:f283764fe9b7 250
chris215 2:f283764fe9b7 251 _genieWaitForIdle();
chris215 2:f283764fe9b7 252
chris215 2:f283764fe9b7 253
chris215 2:f283764fe9b7 254 _geniePutchar(code) ; checksum = code ;
chris215 2:f283764fe9b7 255 _geniePutchar(index) ; checksum ^= index ;
chris215 2:f283764fe9b7 256 _geniePutchar((unsigned char)len) ; checksum ^= len ;
chris215 2:f283764fe9b7 257 for (p = string ; *p ; ++p) {
chris215 2:f283764fe9b7 258 _geniePutchar (*p) ;
chris215 2:f283764fe9b7 259 checksum ^= *p ;
chris215 2:f283764fe9b7 260 }
chris215 2:f283764fe9b7 261 _geniePutchar(checksum) ;
chris215 2:f283764fe9b7 262
chris215 2:f283764fe9b7 263
chris215 2:f283764fe9b7 264 _geniePushLinkState(GENIE_LINK_WFAN);
chris215 0:d2ed5a44c802 265
chris215 0:d2ed5a44c802 266
chris215 2:f283764fe9b7 267 return 0 ;
chris215 2:f283764fe9b7 268 }
chris215 2:f283764fe9b7 269 /////////////////////// genieWriteStr ////////////////////////
chris215 2:f283764fe9b7 270 //
chris215 2:f283764fe9b7 271 // Write a string to the display (ASCII)
chris215 2:f283764fe9b7 272 //
chris215 2:f283764fe9b7 273 uint16_t Mbed4dGenie::genieWriteStr (uint16_t index, char *string) {
chris215 2:f283764fe9b7 274
chris215 2:f283764fe9b7 275 return _genieWriteStrX (GENIE_WRITE_STR, index, string);
chris215 2:f283764fe9b7 276 }
chris215 2:f283764fe9b7 277 /////////////////////// genieWriteStrU ////////////////////////
chris215 2:f283764fe9b7 278 //
chris215 2:f283764fe9b7 279 // Write a string to the display (Unicode)
chris215 2:f283764fe9b7 280 //
chris215 2:f283764fe9b7 281 uint16_t Mbed4dGenie::genieWriteStrU (uint16_t index, char *string) {
chris215 0:d2ed5a44c802 282
chris215 2:f283764fe9b7 283 return _genieWriteStrX (GENIE_WRITE_STRU, index, string);
chris215 0:d2ed5a44c802 284
chris215 2:f283764fe9b7 285 }
chris215 0:d2ed5a44c802 286 //////////////////////////////////////////////////////////////
chris215 0:d2ed5a44c802 287 // Number of fatal errors encountered
chris215 1:95e0e194a412 288 //static int _genieFatalErrors = 0;
chris215 0:d2ed5a44c802 289 ////////////////////// genieGetEventData ////////////////////////
chris215 0:d2ed5a44c802 290 //
chris215 0:d2ed5a44c802 291 // Returns the LSB and MSB of the event's data combined into
chris215 0:d2ed5a44c802 292 // a single uint16
chris215 0:d2ed5a44c802 293 //
chris215 0:d2ed5a44c802 294 // The data is transmitted from the display in big-endian format
chris215 0:d2ed5a44c802 295 // and stored the same so the user can't just access it as an int
chris215 0:d2ed5a44c802 296 // directly from the structure.
chris215 0:d2ed5a44c802 297 //
chris215 2:f283764fe9b7 298 uint16_t Mbed4dGenie::genieGetEventData (genieFrame * e) {
chris215 0:d2ed5a44c802 299 return (e->reportObject.data_msb << 8) + e->reportObject.data_lsb;
chris215 0:d2ed5a44c802 300 }
chris215 0:d2ed5a44c802 301
chris215 0:d2ed5a44c802 302
chris215 0:d2ed5a44c802 303
chris215 0:d2ed5a44c802 304
chris215 0:d2ed5a44c802 305 //////////////////////// genieEventIs ///////////////////////////
chris215 0:d2ed5a44c802 306 //
chris215 0:d2ed5a44c802 307 // Compares the cmd, object and index fields of the event's
chris215 0:d2ed5a44c802 308 // structure.
chris215 0:d2ed5a44c802 309 //
chris215 0:d2ed5a44c802 310 // Returns: TRUE if all the fields match the caller's parms
chris215 0:d2ed5a44c802 311 // FALSE if any of them don't
chris215 0:d2ed5a44c802 312 //
chris215 2:f283764fe9b7 313 bool Mbed4dGenie::genieEventIs(genieFrame * e, uint8_t cmd, uint8_t object, uint8_t index) {
chris215 0:d2ed5a44c802 314
chris215 0:d2ed5a44c802 315
chris215 0:d2ed5a44c802 316 return (e->reportObject.cmd == cmd &&
chris215 0:d2ed5a44c802 317 e->reportObject.object == object &&
chris215 0:d2ed5a44c802 318 e->reportObject.index == index);
chris215 0:d2ed5a44c802 319
chris215 0:d2ed5a44c802 320
chris215 0:d2ed5a44c802 321 }
chris215 0:d2ed5a44c802 322
chris215 0:d2ed5a44c802 323 ////////////////////// _geniePushLinkState //////////////////////
chris215 0:d2ed5a44c802 324 //
chris215 0:d2ed5a44c802 325 // Push a link state onto a FILO stack
chris215 0:d2ed5a44c802 326 //
chris215 2:f283764fe9b7 327 void Mbed4dGenie::_geniePushLinkState (uint8_t newstate) {
chris215 0:d2ed5a44c802 328
chris215 0:d2ed5a44c802 329
chris215 0:d2ed5a44c802 330 _genieLinkState++;
chris215 0:d2ed5a44c802 331 _genieSetLinkState(newstate);
chris215 0:d2ed5a44c802 332
chris215 0:d2ed5a44c802 333
chris215 0:d2ed5a44c802 334 }
chris215 0:d2ed5a44c802 335
chris215 0:d2ed5a44c802 336
chris215 0:d2ed5a44c802 337 ////////////////////// _geniePopLinkState //////////////////////
chris215 0:d2ed5a44c802 338 //
chris215 0:d2ed5a44c802 339 // Pop a link state from a FILO stack
chris215 0:d2ed5a44c802 340 //
chris215 2:f283764fe9b7 341 void Mbed4dGenie::_geniePopLinkState (void) {
chris215 0:d2ed5a44c802 342 if (_genieLinkState > &_genieLinkStates[0]) {
chris215 0:d2ed5a44c802 343 *_genieLinkState = 0xFF;
chris215 0:d2ed5a44c802 344 _genieLinkState--;
chris215 0:d2ed5a44c802 345 }
chris215 0:d2ed5a44c802 346 }
chris215 0:d2ed5a44c802 347
chris215 0:d2ed5a44c802 348 ///////////////// _genieFlushSerialInput ///////////////////
chris215 0:d2ed5a44c802 349 //
chris215 0:d2ed5a44c802 350 // Removes and discards all characters from the currently
chris215 0:d2ed5a44c802 351 // used serial port's Rx buffer.
chris215 0:d2ed5a44c802 352 //
chris215 2:f283764fe9b7 353 void Mbed4dGenie::_genieFlushSerialInput(void) {
chris215 0:d2ed5a44c802 354 do {
chris215 0:d2ed5a44c802 355 _genieGetchar();
chris215 0:d2ed5a44c802 356 } while (_genieError != ERROR_NOCHAR);
chris215 0:d2ed5a44c802 357 }
chris215 0:d2ed5a44c802 358
chris215 0:d2ed5a44c802 359 ///////////////////////// _handleError /////////////////////////
chris215 0:d2ed5a44c802 360 //
chris215 0:d2ed5a44c802 361 // So far really just a debugging aid, but can be enhanced to
chris215 0:d2ed5a44c802 362 // help recover from errors.
chris215 0:d2ed5a44c802 363 //
chris215 2:f283764fe9b7 364 void Mbed4dGenie::_handleError (void) {
chris215 0:d2ed5a44c802 365 }
chris215 0:d2ed5a44c802 366
chris215 0:d2ed5a44c802 367
chris215 0:d2ed5a44c802 368
chris215 0:d2ed5a44c802 369
chris215 0:d2ed5a44c802 370 ////////////////////// _genieFlushEventQueue ////////////////////
chris215 0:d2ed5a44c802 371 //
chris215 0:d2ed5a44c802 372 // Reset all the event queue variables and start from scratch.
chris215 0:d2ed5a44c802 373 //
chris215 2:f283764fe9b7 374 void Mbed4dGenie::_genieFlushEventQueue(void) {
chris215 0:d2ed5a44c802 375 _genieEventQueue.rd_index = 0;
chris215 0:d2ed5a44c802 376 _genieEventQueue.wr_index = 0;
chris215 0:d2ed5a44c802 377 _genieEventQueue.n_events = 0;
chris215 0:d2ed5a44c802 378 }
chris215 2:f283764fe9b7 379 bool Mbed4dGenie::GenieReadable(void){
chris215 2:f283764fe9b7 380 if (_screen.readable())
chris215 0:d2ed5a44c802 381 {
chris215 0:d2ed5a44c802 382 return TRUE;
chris215 0:d2ed5a44c802 383 }
chris215 0:d2ed5a44c802 384 else
chris215 0:d2ed5a44c802 385 {
chris215 0:d2ed5a44c802 386 return FALSE;
chris215 0:d2ed5a44c802 387 }
chris215 0:d2ed5a44c802 388 }
chris215 0:d2ed5a44c802 389 ///////////////////////// genieDoEvents /////////////////////////
chris215 0:d2ed5a44c802 390 //
chris215 0:d2ed5a44c802 391 // This is the heart of the Genie comms state machine.
chris215 0:d2ed5a44c802 392 //
chris215 2:f283764fe9b7 393 uint16_t Mbed4dGenie::genieDoEvents (void) {
chris215 0:d2ed5a44c802 394 uint8_t c;
chris215 0:d2ed5a44c802 395 static uint8_t rx_data[6];
chris215 0:d2ed5a44c802 396 static uint8_t checksum = 0;
chris215 0:d2ed5a44c802 397
chris215 0:d2ed5a44c802 398 if (GenieReadable())
chris215 0:d2ed5a44c802 399 {
chris215 0:d2ed5a44c802 400 c = _genieGetchar();
chris215 0:d2ed5a44c802 401 //pc.putc(c);
chris215 0:d2ed5a44c802 402
chris215 0:d2ed5a44c802 403 ////////////////////////////////////////////
chris215 0:d2ed5a44c802 404 //
chris215 0:d2ed5a44c802 405 // If there are no characters to process and we have
chris215 0:d2ed5a44c802 406 // queued events call the user's handler function.
chris215 0:d2ed5a44c802 407 //
chris215 0:d2ed5a44c802 408 if (_genieError == ERROR_NOCHAR) {
chris215 0:d2ed5a44c802 409 //pc.printf("EventCalled!\n\r");
chris215 0:d2ed5a44c802 410 if (_genieEventQueue.n_events > 0 && _genieUserHandler!= NULL) (_genieUserHandler)();
chris215 0:d2ed5a44c802 411 return GENIE_EVENT_NONE;
chris215 0:d2ed5a44c802 412 }
chris215 0:d2ed5a44c802 413
chris215 0:d2ed5a44c802 414 ///////////////////////////////////////////
chris215 0:d2ed5a44c802 415 //
chris215 0:d2ed5a44c802 416 // Main state machine
chris215 0:d2ed5a44c802 417 //
chris215 0:d2ed5a44c802 418 switch (_genieGetLinkState()) {
chris215 0:d2ed5a44c802 419 case GENIE_LINK_IDLE:
chris215 0:d2ed5a44c802 420 switch (c) {
chris215 0:d2ed5a44c802 421 case GENIE_REPORT_EVENT:
chris215 0:d2ed5a44c802 422 // event frame out of the blue, set the link state
chris215 0:d2ed5a44c802 423 // and fall through to the frame-accumulate code
chris215 0:d2ed5a44c802 424 // at the end of this function
chris215 0:d2ed5a44c802 425 _geniePushLinkState(GENIE_LINK_RXEVENT);
chris215 0:d2ed5a44c802 426 break;
chris215 0:d2ed5a44c802 427
chris215 0:d2ed5a44c802 428 default:
chris215 0:d2ed5a44c802 429 // error, bad character, no other character
chris215 0:d2ed5a44c802 430 // is acceptable in this state
chris215 0:d2ed5a44c802 431 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 432
chris215 0:d2ed5a44c802 433 }
chris215 0:d2ed5a44c802 434 break;
chris215 0:d2ed5a44c802 435
chris215 0:d2ed5a44c802 436 case GENIE_LINK_WFAN:
chris215 0:d2ed5a44c802 437 switch (c) {
chris215 0:d2ed5a44c802 438
chris215 0:d2ed5a44c802 439
chris215 0:d2ed5a44c802 440 case GENIE_ACK:
chris215 0:d2ed5a44c802 441 _geniePopLinkState();
chris215 0:d2ed5a44c802 442 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 443
chris215 0:d2ed5a44c802 444
chris215 0:d2ed5a44c802 445 case GENIE_NAK:
chris215 0:d2ed5a44c802 446 _geniePopLinkState();
chris215 0:d2ed5a44c802 447 _genieError = ERROR_NAK;
chris215 0:d2ed5a44c802 448 _handleError();
chris215 0:d2ed5a44c802 449 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 450
chris215 0:d2ed5a44c802 451 case GENIE_REPORT_EVENT:
chris215 0:d2ed5a44c802 452 // event frame out of the blue while waiting for an ACK
chris215 0:d2ed5a44c802 453 // save/set the link state and fall through to the
chris215 0:d2ed5a44c802 454 // frame-accumulate code at the end of this function
chris215 0:d2ed5a44c802 455 _geniePushLinkState(GENIE_LINK_RXEVENT);
chris215 0:d2ed5a44c802 456 break;
chris215 0:d2ed5a44c802 457
chris215 0:d2ed5a44c802 458
chris215 0:d2ed5a44c802 459 case GENIE_REPORT_OBJ:
chris215 0:d2ed5a44c802 460 default:
chris215 0:d2ed5a44c802 461 // error, bad character
chris215 0:d2ed5a44c802 462 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 463 }
chris215 0:d2ed5a44c802 464 break;
chris215 0:d2ed5a44c802 465
chris215 0:d2ed5a44c802 466
chris215 0:d2ed5a44c802 467 case GENIE_LINK_WF_RXREPORT: // waiting for the first byte of a report
chris215 0:d2ed5a44c802 468 switch (c) {
chris215 0:d2ed5a44c802 469
chris215 0:d2ed5a44c802 470 case GENIE_REPORT_EVENT:
chris215 0:d2ed5a44c802 471 // event frame out of the blue while waiting for the first
chris215 0:d2ed5a44c802 472 // byte of a report frame
chris215 0:d2ed5a44c802 473 // save/set the link state and fall through to the
chris215 0:d2ed5a44c802 474 // frame-accumulate code at the end of this function
chris215 0:d2ed5a44c802 475 _geniePushLinkState(GENIE_LINK_RXEVENT);
chris215 0:d2ed5a44c802 476 break;
chris215 0:d2ed5a44c802 477
chris215 0:d2ed5a44c802 478
chris215 0:d2ed5a44c802 479 case GENIE_REPORT_OBJ:
chris215 0:d2ed5a44c802 480 // first byte of a report frame
chris215 0:d2ed5a44c802 481 // replace the GENIE_LINK_WF_RXREPORT link state
chris215 0:d2ed5a44c802 482 // with GENIE_LINK_RXREPORT to indicate that we
chris215 0:d2ed5a44c802 483 // are now receiving a report frame
chris215 0:d2ed5a44c802 484 _geniePopLinkState();
chris215 0:d2ed5a44c802 485 _geniePushLinkState(GENIE_LINK_RXREPORT);
chris215 0:d2ed5a44c802 486 break;
chris215 0:d2ed5a44c802 487
chris215 0:d2ed5a44c802 488
chris215 0:d2ed5a44c802 489 case GENIE_ACK:
chris215 0:d2ed5a44c802 490 case GENIE_NAK:
chris215 0:d2ed5a44c802 491 default:
chris215 0:d2ed5a44c802 492 // error, bad character
chris215 0:d2ed5a44c802 493 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 494 // break;
chris215 0:d2ed5a44c802 495 }
chris215 0:d2ed5a44c802 496
chris215 0:d2ed5a44c802 497
chris215 0:d2ed5a44c802 498 case GENIE_LINK_RXREPORT: // already receiving report
chris215 0:d2ed5a44c802 499 case GENIE_LINK_RXEVENT: // already receiving event
chris215 0:d2ed5a44c802 500 default:
chris215 0:d2ed5a44c802 501 break;
chris215 0:d2ed5a44c802 502
chris215 0:d2ed5a44c802 503 }
chris215 0:d2ed5a44c802 504
chris215 0:d2ed5a44c802 505
chris215 0:d2ed5a44c802 506 ///////////////////////////////////////////////////////
chris215 0:d2ed5a44c802 507 // We get here if we are in the process of receiving
chris215 0:d2ed5a44c802 508 // a report or event frame. Accumulate GENIE_FRAME_SIZE
chris215 0:d2ed5a44c802 509 // bytes into a local buffer then queue them as a frame
chris215 0:d2ed5a44c802 510 // into the event queue
chris215 0:d2ed5a44c802 511 //
chris215 0:d2ed5a44c802 512 if (_genieGetLinkState() == GENIE_LINK_RXREPORT || \
chris215 0:d2ed5a44c802 513 _genieGetLinkState() == GENIE_LINK_RXEVENT) {
chris215 0:d2ed5a44c802 514
chris215 0:d2ed5a44c802 515 checksum = (rxframe_count == 0) ? c : checksum ^ c;
chris215 0:d2ed5a44c802 516
chris215 0:d2ed5a44c802 517
chris215 0:d2ed5a44c802 518 rx_data[rxframe_count] = c;
chris215 0:d2ed5a44c802 519
chris215 0:d2ed5a44c802 520
chris215 0:d2ed5a44c802 521 if (rxframe_count == GENIE_FRAME_SIZE -1) {
chris215 0:d2ed5a44c802 522 //pc.printf("FrameReceived!\n\r");
chris215 0:d2ed5a44c802 523 // all bytes received, if the CS is good
chris215 0:d2ed5a44c802 524 // queue the frame and restore the link state
chris215 0:d2ed5a44c802 525 if (checksum == 0) {
chris215 0:d2ed5a44c802 526 _genieEnqueueEvent(rx_data);
chris215 0:d2ed5a44c802 527 if (_genieEventQueue.n_events > 0 && _genieUserHandler!= NULL) (_genieUserHandler)();
chris215 0:d2ed5a44c802 528 //return GENIE_EVENT_NONE;
chris215 0:d2ed5a44c802 529 rxframe_count = 0;
chris215 0:d2ed5a44c802 530 // revert the link state to whatever it was before
chris215 0:d2ed5a44c802 531 // we started accumulating this frame
chris215 0:d2ed5a44c802 532 _geniePopLinkState();
chris215 0:d2ed5a44c802 533 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 534 } else {
chris215 0:d2ed5a44c802 535 _genieError = ERROR_BAD_CS;
chris215 0:d2ed5a44c802 536 _handleError();
chris215 0:d2ed5a44c802 537 }
chris215 0:d2ed5a44c802 538 }
chris215 0:d2ed5a44c802 539 rxframe_count++;
chris215 0:d2ed5a44c802 540 return GENIE_EVENT_RXCHAR;
chris215 0:d2ed5a44c802 541 }
chris215 1:95e0e194a412 542 }
chris215 1:95e0e194a412 543 return GENIE_EVENT_NONE;
chris215 2:f283764fe9b7 544 }