4D display working with Gecko board STK3600

Dependents:   Genie_mbed_Gecko

Fork of 4dGENIE by christian b

Committer:
chris215
Date:
Sun Feb 23 18:01:55 2014 +0000
Revision:
4:23c46eb6f0db
Parent:
3:11c49c49cd1a
Child:
5:3eb81723edc5
fixed reset pin behavior

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