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@4:07e752ff8dce, 2015-09-15 (annotated)
- Committer:
- okini3939
- Date:
- Tue Sep 15 06:17:57 2015 +0000
- Revision:
- 4:07e752ff8dce
- Child:
- 5:e5a358e9ed94
revamp functions;
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 | static int len = 0, count = 0; |
okini3939 | 4:07e752ff8dce | 5 | |
okini3939 | 4:07e752ff8dce | 6 | #ifdef DEBUG_DUMP |
okini3939 | 4:07e752ff8dce | 7 | if (c < 0x20 || c >= 0x7f) { |
okini3939 | 4:07e752ff8dce | 8 | printf("%02x", c); |
okini3939 | 4:07e752ff8dce | 9 | } else { |
okini3939 | 4:07e752ff8dce | 10 | printf("_%c", c); |
okini3939 | 4:07e752ff8dce | 11 | } |
okini3939 | 4:07e752ff8dce | 12 | #endif |
okini3939 | 4:07e752ff8dce | 13 | switch (_mode) { |
okini3939 | 4:07e752ff8dce | 14 | case MODE_READY: |
okini3939 | 4:07e752ff8dce | 15 | _rxbuf[count] = c; |
okini3939 | 4:07e752ff8dce | 16 | switch (count) { |
okini3939 | 4:07e752ff8dce | 17 | case 0: |
okini3939 | 4:07e752ff8dce | 18 | if (c == 0x0f) count ++; |
okini3939 | 4:07e752ff8dce | 19 | break; |
okini3939 | 4:07e752ff8dce | 20 | case 1: |
okini3939 | 4:07e752ff8dce | 21 | if (c == 0x5a) { |
okini3939 | 4:07e752ff8dce | 22 | count ++; |
okini3939 | 4:07e752ff8dce | 23 | } else { |
okini3939 | 4:07e752ff8dce | 24 | count = 0; |
okini3939 | 4:07e752ff8dce | 25 | } |
okini3939 | 4:07e752ff8dce | 26 | break; |
okini3939 | 4:07e752ff8dce | 27 | case 2: |
okini3939 | 4:07e752ff8dce | 28 | count ++; |
okini3939 | 4:07e752ff8dce | 29 | len = (int)((unsigned char)c); |
okini3939 | 4:07e752ff8dce | 30 | _mode = MODE_DATA; |
okini3939 | 4:07e752ff8dce | 31 | break; |
okini3939 | 4:07e752ff8dce | 32 | } |
okini3939 | 4:07e752ff8dce | 33 | break; |
okini3939 | 4:07e752ff8dce | 34 | case MODE_DATA: |
okini3939 | 4:07e752ff8dce | 35 | _rxbuf[count] = c; |
okini3939 | 4:07e752ff8dce | 36 | count ++; |
okini3939 | 4:07e752ff8dce | 37 | if (count >= len) { |
okini3939 | 4:07e752ff8dce | 38 | count = 0; |
okini3939 | 4:07e752ff8dce | 39 | _mode = MODE_READY; |
okini3939 | 4:07e752ff8dce | 40 | parseMessage(); |
okini3939 | 4:07e752ff8dce | 41 | } |
okini3939 | 4:07e752ff8dce | 42 | } |
okini3939 | 4:07e752ff8dce | 43 | } |
okini3939 | 4:07e752ff8dce | 44 | |
okini3939 | 4:07e752ff8dce | 45 | void NECnfc::parseMessage () { |
okini3939 | 4:07e752ff8dce | 46 | DBG("parseMessage %02x\r\n", _rxmsg.msgid); |
okini3939 | 4:07e752ff8dce | 47 | |
okini3939 | 4:07e752ff8dce | 48 | _received = 0; |
okini3939 | 4:07e752ff8dce | 49 | if (_rxmsg.msgno != _msgno && _rxmsg.msgid != NECMSG_SEND_DAT && _rxmsg.msgid != NECMSG_SEND_NOACK) { |
okini3939 | 4:07e752ff8dce | 50 | DBG("error msgno %d %d\r\n", _rxmsg.msgno, _msgno); |
okini3939 | 4:07e752ff8dce | 51 | return; |
okini3939 | 4:07e752ff8dce | 52 | } |
okini3939 | 4:07e752ff8dce | 53 | |
okini3939 | 4:07e752ff8dce | 54 | switch (_rxmsg.msgid) { |
okini3939 | 4:07e752ff8dce | 55 | case NECMSG_ACK: |
okini3939 | 4:07e752ff8dce | 56 | _ack = 1; |
okini3939 | 4:07e752ff8dce | 57 | break; |
okini3939 | 4:07e752ff8dce | 58 | case NECMSG_NOACK: |
okini3939 | 4:07e752ff8dce | 59 | _noack = 1; |
okini3939 | 4:07e752ff8dce | 60 | break; |
okini3939 | 4:07e752ff8dce | 61 | case NECMSG_RESEND: |
okini3939 | 4:07e752ff8dce | 62 | _resend = 1; |
okini3939 | 4:07e752ff8dce | 63 | DBG("error resend %d %d\r\n", _rxmsg.parameter[0], _rxmsg.parameter[1]); |
okini3939 | 4:07e752ff8dce | 64 | break; |
okini3939 | 4:07e752ff8dce | 65 | case NECMSG_SEARCH: |
okini3939 | 4:07e752ff8dce | 66 | break; |
okini3939 | 4:07e752ff8dce | 67 | case NECMSG_SEND_DAT: |
okini3939 | 4:07e752ff8dce | 68 | case NECMSG_SEND_NOACK: |
okini3939 | 4:07e752ff8dce | 69 | _received = 1; |
okini3939 | 4:07e752ff8dce | 70 | break; |
okini3939 | 4:07e752ff8dce | 71 | } |
okini3939 | 4:07e752ff8dce | 72 | return; |
okini3939 | 4:07e752ff8dce | 73 | } |
okini3939 | 4:07e752ff8dce | 74 | |
okini3939 | 4:07e752ff8dce | 75 | int NECnfc::send (NECMSG msgid, unsigned int dest, const char *param, int len) { |
okini3939 | 4:07e752ff8dce | 76 | int i; |
okini3939 | 4:07e752ff8dce | 77 | struct ifMessage ifmsg; |
okini3939 | 4:07e752ff8dce | 78 | unsigned char *buf = (unsigned char *)&ifmsg; |
okini3939 | 4:07e752ff8dce | 79 | Timer t; |
okini3939 | 4:07e752ff8dce | 80 | |
okini3939 | 4:07e752ff8dce | 81 | if (len > NEC_MAXLENGTH) len = NEC_MAXLENGTH; |
okini3939 | 4:07e752ff8dce | 82 | _msgno = (_msgno + 1) & 0xff; |
okini3939 | 4:07e752ff8dce | 83 | ifmsg.start = htons(0x0f5a); |
okini3939 | 4:07e752ff8dce | 84 | ifmsg.length = NEC_HEADER_SIZE + len; |
okini3939 | 4:07e752ff8dce | 85 | ifmsg.msgid = msgid; |
okini3939 | 4:07e752ff8dce | 86 | ifmsg.msgno = _msgno; |
okini3939 | 4:07e752ff8dce | 87 | ifmsg.dstid = htonl(dest); |
okini3939 | 4:07e752ff8dce | 88 | ifmsg.srcid = htonl(_id); |
okini3939 | 4:07e752ff8dce | 89 | memcpy(ifmsg.parameter, param, len); |
okini3939 | 4:07e752ff8dce | 90 | |
okini3939 | 4:07e752ff8dce | 91 | _ack = 0; |
okini3939 | 4:07e752ff8dce | 92 | _noack = 0; |
okini3939 | 4:07e752ff8dce | 93 | _resend = 0; |
okini3939 | 4:07e752ff8dce | 94 | for (i = 0; i < ifmsg.length; i ++) { |
okini3939 | 4:07e752ff8dce | 95 | _nec.putc(buf[i]); |
okini3939 | 4:07e752ff8dce | 96 | } |
okini3939 | 4:07e752ff8dce | 97 | |
okini3939 | 4:07e752ff8dce | 98 | t.start(); |
okini3939 | 4:07e752ff8dce | 99 | for (;;) { |
okini3939 | 4:07e752ff8dce | 100 | if (_ack || _noack || _resend) break; |
okini3939 | 4:07e752ff8dce | 101 | if (t.read() > NEC_TIMEOUT) { |
okini3939 | 4:07e752ff8dce | 102 | DBG("timeout\r\n"); |
okini3939 | 4:07e752ff8dce | 103 | t.stop(); |
okini3939 | 4:07e752ff8dce | 104 | return -1; |
okini3939 | 4:07e752ff8dce | 105 | } |
okini3939 | 4:07e752ff8dce | 106 | } |
okini3939 | 4:07e752ff8dce | 107 | t.stop(); |
okini3939 | 4:07e752ff8dce | 108 | DBG(" ack %d, noack %d, resend %d\r\n", _ack, _noack, _resend); |
okini3939 | 4:07e752ff8dce | 109 | return _ack ? 0 : -1; |
okini3939 | 4:07e752ff8dce | 110 | } |