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