NEC Near Field Communication RF module library for mbed H001-000003-001 (950MHz), H001-000013-001 (920MHz), TY24FM-E2024 (2.4GHz)
Dependents: NECnfc_sample Drone_air Drone_ground
NEC Near Field Communication RF module library
NEC製の近距離無線モジュール用のライブラリです。
NECnfc_msg.cpp@7:9c963cb53ef7, 2016-04-07 (annotated)
- Committer:
- okini3939
- Date:
- Thu Apr 07 00:56:50 2016 +0000
- Revision:
- 7:9c963cb53ef7
- Parent:
- 6:2e1fc47e5bca
bug fix;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 4:07e752ff8dce | 1 | #include "NECnfc.h" |
okini3939 | 4:07e752ff8dce | 2 | |
okini3939 | 4:07e752ff8dce | 3 | void NECnfc::recvData (char c) { |
okini3939 | 4:07e752ff8dce | 4 | |
okini3939 | 4:07e752ff8dce | 5 | #ifdef DEBUG_DUMP |
okini3939 | 4:07e752ff8dce | 6 | if (c < 0x20 || c >= 0x7f) { |
okini3939 | 4:07e752ff8dce | 7 | printf("%02x", c); |
okini3939 | 4:07e752ff8dce | 8 | } else { |
okini3939 | 4:07e752ff8dce | 9 | printf("_%c", c); |
okini3939 | 4:07e752ff8dce | 10 | } |
okini3939 | 4:07e752ff8dce | 11 | #endif |
okini3939 | 4:07e752ff8dce | 12 | switch (_mode) { |
okini3939 | 4:07e752ff8dce | 13 | case MODE_READY: |
okini3939 | 6:2e1fc47e5bca | 14 | switch (_rxcount) { |
okini3939 | 4:07e752ff8dce | 15 | case 0: |
okini3939 | 5:e5a358e9ed94 | 16 | if (c == 0x0f) { |
okini3939 | 6:2e1fc47e5bca | 17 | _rxbuf[_rxcount] = c; |
okini3939 | 6:2e1fc47e5bca | 18 | _rxcount ++; |
okini3939 | 5:e5a358e9ed94 | 19 | _received = 0; |
okini3939 | 5:e5a358e9ed94 | 20 | } |
okini3939 | 4:07e752ff8dce | 21 | break; |
okini3939 | 4:07e752ff8dce | 22 | case 1: |
okini3939 | 4:07e752ff8dce | 23 | if (c == 0x5a) { |
okini3939 | 6:2e1fc47e5bca | 24 | _rxbuf[_rxcount] = c; |
okini3939 | 6:2e1fc47e5bca | 25 | _rxcount ++; |
okini3939 | 4:07e752ff8dce | 26 | } else { |
okini3939 | 6:2e1fc47e5bca | 27 | _rxcount = 0; |
okini3939 | 4:07e752ff8dce | 28 | } |
okini3939 | 4:07e752ff8dce | 29 | break; |
okini3939 | 4:07e752ff8dce | 30 | case 2: |
okini3939 | 6:2e1fc47e5bca | 31 | _rxbuf[_rxcount] = c; |
okini3939 | 6:2e1fc47e5bca | 32 | _rxcount ++; |
okini3939 | 6:2e1fc47e5bca | 33 | _rxlen = (int)((unsigned char)c); |
okini3939 | 4:07e752ff8dce | 34 | _mode = MODE_DATA; |
okini3939 | 4:07e752ff8dce | 35 | break; |
okini3939 | 4:07e752ff8dce | 36 | } |
okini3939 | 4:07e752ff8dce | 37 | break; |
okini3939 | 4:07e752ff8dce | 38 | case MODE_DATA: |
okini3939 | 6:2e1fc47e5bca | 39 | _rxbuf[_rxcount] = c; |
okini3939 | 6:2e1fc47e5bca | 40 | _rxcount ++; |
okini3939 | 6:2e1fc47e5bca | 41 | if (_rxcount >= _rxlen) { |
okini3939 | 6:2e1fc47e5bca | 42 | _rxcount = 0; |
okini3939 | 4:07e752ff8dce | 43 | _mode = MODE_READY; |
okini3939 | 4:07e752ff8dce | 44 | parseMessage(); |
okini3939 | 4:07e752ff8dce | 45 | } |
okini3939 | 4:07e752ff8dce | 46 | } |
okini3939 | 4:07e752ff8dce | 47 | } |
okini3939 | 4:07e752ff8dce | 48 | |
okini3939 | 4:07e752ff8dce | 49 | void NECnfc::parseMessage () { |
okini3939 | 4:07e752ff8dce | 50 | DBG("parseMessage %02x\r\n", _rxmsg.msgid); |
okini3939 | 4:07e752ff8dce | 51 | |
okini3939 | 4:07e752ff8dce | 52 | _received = 0; |
okini3939 | 4:07e752ff8dce | 53 | if (_rxmsg.msgno != _msgno && _rxmsg.msgid != NECMSG_SEND_DAT && _rxmsg.msgid != NECMSG_SEND_NOACK) { |
okini3939 | 4:07e752ff8dce | 54 | DBG("error msgno %d %d\r\n", _rxmsg.msgno, _msgno); |
okini3939 | 4:07e752ff8dce | 55 | return; |
okini3939 | 4:07e752ff8dce | 56 | } |
okini3939 | 4:07e752ff8dce | 57 | |
okini3939 | 4:07e752ff8dce | 58 | switch (_rxmsg.msgid) { |
okini3939 | 4:07e752ff8dce | 59 | case NECMSG_ACK: |
okini3939 | 4:07e752ff8dce | 60 | _ack = 1; |
okini3939 | 4:07e752ff8dce | 61 | break; |
okini3939 | 4:07e752ff8dce | 62 | case NECMSG_NOACK: |
okini3939 | 4:07e752ff8dce | 63 | _noack = 1; |
okini3939 | 4:07e752ff8dce | 64 | break; |
okini3939 | 4:07e752ff8dce | 65 | case NECMSG_RESEND: |
okini3939 | 4:07e752ff8dce | 66 | _resend = 1; |
okini3939 | 4:07e752ff8dce | 67 | DBG("error resend %d %d\r\n", _rxmsg.parameter[0], _rxmsg.parameter[1]); |
okini3939 | 4:07e752ff8dce | 68 | break; |
okini3939 | 4:07e752ff8dce | 69 | case NECMSG_SEARCH: |
okini3939 | 4:07e752ff8dce | 70 | break; |
okini3939 | 4:07e752ff8dce | 71 | case NECMSG_SEND_DAT: |
okini3939 | 4:07e752ff8dce | 72 | case NECMSG_SEND_NOACK: |
okini3939 | 4:07e752ff8dce | 73 | _received = 1; |
okini3939 | 4:07e752ff8dce | 74 | break; |
okini3939 | 4:07e752ff8dce | 75 | } |
okini3939 | 4:07e752ff8dce | 76 | return; |
okini3939 | 4:07e752ff8dce | 77 | } |
okini3939 | 4:07e752ff8dce | 78 | |
okini3939 | 4:07e752ff8dce | 79 | int NECnfc::send (NECMSG msgid, unsigned int dest, const char *param, int len) { |
okini3939 | 4:07e752ff8dce | 80 | int i; |
okini3939 | 4:07e752ff8dce | 81 | struct ifMessage ifmsg; |
okini3939 | 4:07e752ff8dce | 82 | unsigned char *buf = (unsigned char *)&ifmsg; |
okini3939 | 4:07e752ff8dce | 83 | Timer t; |
okini3939 | 4:07e752ff8dce | 84 | |
okini3939 | 6:2e1fc47e5bca | 85 | if (_mode != MODE_READY) { |
okini3939 | 6:2e1fc47e5bca | 86 | t.start(); |
okini3939 | 6:2e1fc47e5bca | 87 | while (_mode != MODE_READY) { |
okini3939 | 6:2e1fc47e5bca | 88 | poll(); |
okini3939 | 6:2e1fc47e5bca | 89 | if (t.read() > NEC_TIMEOUT) { |
okini3939 | 6:2e1fc47e5bca | 90 | DBG("timeout\r\n"); |
okini3939 | 6:2e1fc47e5bca | 91 | t.stop(); |
okini3939 | 6:2e1fc47e5bca | 92 | return -1; |
okini3939 | 6:2e1fc47e5bca | 93 | } |
okini3939 | 6:2e1fc47e5bca | 94 | } |
okini3939 | 7:9c963cb53ef7 | 95 | t.stop(); |
okini3939 | 6:2e1fc47e5bca | 96 | t.reset(); |
okini3939 | 6:2e1fc47e5bca | 97 | } |
okini3939 | 6:2e1fc47e5bca | 98 | |
okini3939 | 4:07e752ff8dce | 99 | if (len > NEC_MAXLENGTH) len = NEC_MAXLENGTH; |
okini3939 | 4:07e752ff8dce | 100 | _msgno = (_msgno + 1) & 0xff; |
okini3939 | 4:07e752ff8dce | 101 | ifmsg.start = htons(0x0f5a); |
okini3939 | 4:07e752ff8dce | 102 | ifmsg.length = NEC_HEADER_SIZE + len; |
okini3939 | 4:07e752ff8dce | 103 | ifmsg.msgid = msgid; |
okini3939 | 4:07e752ff8dce | 104 | ifmsg.msgno = _msgno; |
okini3939 | 4:07e752ff8dce | 105 | ifmsg.dstid = htonl(dest); |
okini3939 | 4:07e752ff8dce | 106 | ifmsg.srcid = htonl(_id); |
okini3939 | 4:07e752ff8dce | 107 | memcpy(ifmsg.parameter, param, len); |
okini3939 | 4:07e752ff8dce | 108 | |
okini3939 | 4:07e752ff8dce | 109 | _ack = 0; |
okini3939 | 4:07e752ff8dce | 110 | _noack = 0; |
okini3939 | 4:07e752ff8dce | 111 | _resend = 0; |
okini3939 | 4:07e752ff8dce | 112 | for (i = 0; i < ifmsg.length; i ++) { |
okini3939 | 4:07e752ff8dce | 113 | _nec.putc(buf[i]); |
okini3939 | 4:07e752ff8dce | 114 | } |
okini3939 | 4:07e752ff8dce | 115 | |
okini3939 | 4:07e752ff8dce | 116 | t.start(); |
okini3939 | 4:07e752ff8dce | 117 | for (;;) { |
okini3939 | 4:07e752ff8dce | 118 | if (_ack || _noack || _resend) break; |
okini3939 | 4:07e752ff8dce | 119 | if (t.read() > NEC_TIMEOUT) { |
okini3939 | 4:07e752ff8dce | 120 | DBG("timeout\r\n"); |
okini3939 | 4:07e752ff8dce | 121 | t.stop(); |
okini3939 | 4:07e752ff8dce | 122 | return -1; |
okini3939 | 4:07e752ff8dce | 123 | } |
okini3939 | 4:07e752ff8dce | 124 | } |
okini3939 | 4:07e752ff8dce | 125 | t.stop(); |
okini3939 | 4:07e752ff8dce | 126 | DBG(" ack %d, noack %d, resend %d\r\n", _ack, _noack, _resend); |
okini3939 | 4:07e752ff8dce | 127 | return _ack ? 0 : -1; |
okini3939 | 4:07e752ff8dce | 128 | } |