Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NECnfc_sample Drone_air Drone_ground
NECnfc_msg.cpp
00001 #include "NECnfc.h" 00002 00003 void NECnfc::recvData (char c) { 00004 00005 #ifdef DEBUG_DUMP 00006 if (c < 0x20 || c >= 0x7f) { 00007 printf("%02x", c); 00008 } else { 00009 printf("_%c", c); 00010 } 00011 #endif 00012 switch (_mode) { 00013 case MODE_READY: 00014 switch (_rxcount) { 00015 case 0: 00016 if (c == 0x0f) { 00017 _rxbuf[_rxcount] = c; 00018 _rxcount ++; 00019 _received = 0; 00020 } 00021 break; 00022 case 1: 00023 if (c == 0x5a) { 00024 _rxbuf[_rxcount] = c; 00025 _rxcount ++; 00026 } else { 00027 _rxcount = 0; 00028 } 00029 break; 00030 case 2: 00031 _rxbuf[_rxcount] = c; 00032 _rxcount ++; 00033 _rxlen = (int)((unsigned char)c); 00034 _mode = MODE_DATA; 00035 break; 00036 } 00037 break; 00038 case MODE_DATA: 00039 _rxbuf[_rxcount] = c; 00040 _rxcount ++; 00041 if (_rxcount >= _rxlen) { 00042 _rxcount = 0; 00043 _mode = MODE_READY; 00044 parseMessage(); 00045 } 00046 } 00047 } 00048 00049 void NECnfc::parseMessage () { 00050 DBG("parseMessage %02x\r\n", _rxmsg.msgid); 00051 00052 _received = 0; 00053 if (_rxmsg.msgno != _msgno && _rxmsg.msgid != NECMSG_SEND_DAT && _rxmsg.msgid != NECMSG_SEND_NOACK) { 00054 DBG("error msgno %d %d\r\n", _rxmsg.msgno, _msgno); 00055 return; 00056 } 00057 00058 switch (_rxmsg.msgid) { 00059 case NECMSG_ACK: 00060 _ack = 1; 00061 break; 00062 case NECMSG_NOACK: 00063 _noack = 1; 00064 break; 00065 case NECMSG_RESEND: 00066 _resend = 1; 00067 DBG("error resend %d %d\r\n", _rxmsg.parameter[0], _rxmsg.parameter[1]); 00068 break; 00069 case NECMSG_SEARCH: 00070 break; 00071 case NECMSG_SEND_DAT: 00072 case NECMSG_SEND_NOACK: 00073 _received = 1; 00074 break; 00075 } 00076 return; 00077 } 00078 00079 int NECnfc::send (NECMSG msgid, unsigned int dest, const char *param, int len) { 00080 int i; 00081 struct ifMessage ifmsg; 00082 unsigned char *buf = (unsigned char *)&ifmsg; 00083 Timer t; 00084 00085 if (_mode != MODE_READY) { 00086 t.start(); 00087 while (_mode != MODE_READY) { 00088 poll(); 00089 if (t.read() > NEC_TIMEOUT) { 00090 DBG("timeout\r\n"); 00091 t.stop(); 00092 return -1; 00093 } 00094 } 00095 t.stop(); 00096 t.reset(); 00097 } 00098 00099 if (len > NEC_MAXLENGTH) len = NEC_MAXLENGTH; 00100 _msgno = (_msgno + 1) & 0xff; 00101 ifmsg.start = htons(0x0f5a); 00102 ifmsg.length = NEC_HEADER_SIZE + len; 00103 ifmsg.msgid = msgid; 00104 ifmsg.msgno = _msgno; 00105 ifmsg.dstid = htonl(dest); 00106 ifmsg.srcid = htonl(_id); 00107 memcpy(ifmsg.parameter, param, len); 00108 00109 _ack = 0; 00110 _noack = 0; 00111 _resend = 0; 00112 for (i = 0; i < ifmsg.length; i ++) { 00113 _nec.putc(buf[i]); 00114 } 00115 00116 t.start(); 00117 for (;;) { 00118 if (_ack || _noack || _resend) break; 00119 if (t.read() > NEC_TIMEOUT) { 00120 DBG("timeout\r\n"); 00121 t.stop(); 00122 return -1; 00123 } 00124 } 00125 t.stop(); 00126 DBG(" ack %d, noack %d, resend %d\r\n", _ack, _noack, _resend); 00127 return _ack ? 0 : -1; 00128 }
Generated on Wed Jul 13 2022 14:59:48 by
 1.7.2