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.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 | 0:337524afec9b | 1 | /** |
okini3939 | 0:337524afec9b | 2 | * NEC Near Field Communication RF module library for mbed |
okini3939 | 4:07e752ff8dce | 3 | * Copyright (c) 2015 Suga |
okini3939 | 0:337524afec9b | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 0:337524afec9b | 5 | */ |
okini3939 | 0:337524afec9b | 6 | |
okini3939 | 0:337524afec9b | 7 | /** @file |
okini3939 | 0:337524afec9b | 8 | * @brief NEC Near Field Communication RF module library for mbed |
okini3939 | 2:9e108187ccfe | 9 | * @note H001-000003-001 (950MHz), TY24FM-E2024 (2.4GHz), H001-000013-001 (920MHz) |
okini3939 | 0:337524afec9b | 10 | */ |
okini3939 | 0:337524afec9b | 11 | |
okini3939 | 0:337524afec9b | 12 | #include "mbed.h" |
okini3939 | 0:337524afec9b | 13 | #include "NECnfc.h" |
okini3939 | 0:337524afec9b | 14 | #include <string.h> |
okini3939 | 0:337524afec9b | 15 | |
okini3939 | 0:337524afec9b | 16 | |
okini3939 | 4:07e752ff8dce | 17 | NECnfc::NECnfc (PinName tx, PinName rx, PinName reset, int baud, NECTYPE type) : _nec(tx, rx) { |
okini3939 | 0:337524afec9b | 18 | |
okini3939 | 4:07e752ff8dce | 19 | _rxbuf = (char*)&_rxmsg; |
okini3939 | 4:07e752ff8dce | 20 | _type = type; |
okini3939 | 4:07e752ff8dce | 21 | _msgno = 0; |
okini3939 | 4:07e752ff8dce | 22 | _id = NEC_DUMMYID; |
okini3939 | 4:07e752ff8dce | 23 | _received = 0; |
okini3939 | 6:2e1fc47e5bca | 24 | _rxcount = 0; |
okini3939 | 0:337524afec9b | 25 | |
okini3939 | 4:07e752ff8dce | 26 | initUart(reset, NC, NC, baud); |
okini3939 | 4:07e752ff8dce | 27 | setReset(true); |
okini3939 | 4:07e752ff8dce | 28 | wait_ms(20); |
okini3939 | 4:07e752ff8dce | 29 | setReset(false); |
okini3939 | 4:07e752ff8dce | 30 | wait_ms(100); |
okini3939 | 0:337524afec9b | 31 | } |
okini3939 | 0:337524afec9b | 32 | |
okini3939 | 0:337524afec9b | 33 | |
okini3939 | 4:07e752ff8dce | 34 | NECnfc::NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud, NECTYPE type) : _nec(tx, rx) { |
okini3939 | 0:337524afec9b | 35 | |
okini3939 | 4:07e752ff8dce | 36 | _rxbuf = (char*)&_rxmsg; |
okini3939 | 4:07e752ff8dce | 37 | _type = type; |
okini3939 | 4:07e752ff8dce | 38 | _msgno = 0; |
okini3939 | 4:07e752ff8dce | 39 | _id = NEC_DUMMYID; |
okini3939 | 4:07e752ff8dce | 40 | _received = 0; |
okini3939 | 6:2e1fc47e5bca | 41 | _rxcount = 0; |
okini3939 | 0:337524afec9b | 42 | |
okini3939 | 4:07e752ff8dce | 43 | initUart(reset, wakeup, mode, baud); |
okini3939 | 4:07e752ff8dce | 44 | setReset(true); |
okini3939 | 4:07e752ff8dce | 45 | wait_ms(20); |
okini3939 | 4:07e752ff8dce | 46 | setReset(false); |
okini3939 | 4:07e752ff8dce | 47 | wait_ms(100); |
okini3939 | 0:337524afec9b | 48 | } |
okini3939 | 0:337524afec9b | 49 | |
okini3939 | 4:07e752ff8dce | 50 | void NECnfc::poll () { |
okini3939 | 4:07e752ff8dce | 51 | |
okini3939 | 4:07e752ff8dce | 52 | if (_received) { |
okini3939 | 4:07e752ff8dce | 53 | _func.call(); |
okini3939 | 4:07e752ff8dce | 54 | _received = 0; |
okini3939 | 0:337524afec9b | 55 | } |
okini3939 | 0:337524afec9b | 56 | } |
okini3939 | 0:337524afec9b | 57 | |
okini3939 | 4:07e752ff8dce | 58 | int NECnfc::sendData(int dest, const char *data, int len) { |
okini3939 | 0:337524afec9b | 59 | |
okini3939 | 4:07e752ff8dce | 60 | if (dest == NEC_DUMMYID) { |
okini3939 | 4:07e752ff8dce | 61 | if (send(NECMSG_SEND_NOACK, dest, data, len)) { |
okini3939 | 4:07e752ff8dce | 62 | return -1; |
okini3939 | 0:337524afec9b | 63 | } |
okini3939 | 4:07e752ff8dce | 64 | } else { |
okini3939 | 4:07e752ff8dce | 65 | if (send(NECMSG_SEND_DAT, dest, data, len)) { |
okini3939 | 4:07e752ff8dce | 66 | return -1; |
okini3939 | 4:07e752ff8dce | 67 | } |
okini3939 | 4:07e752ff8dce | 68 | _rssi = _rxmsg.parameter[2]; |
okini3939 | 0:337524afec9b | 69 | } |
okini3939 | 0:337524afec9b | 70 | return 0; |
okini3939 | 0:337524afec9b | 71 | } |
okini3939 | 0:337524afec9b | 72 | |
okini3939 | 4:07e752ff8dce | 73 | int NECnfc::readData(int *dest, int *src, char *data, int len) { |
okini3939 | 4:07e752ff8dce | 74 | int r; |
okini3939 | 4:07e752ff8dce | 75 | |
okini3939 | 4:07e752ff8dce | 76 | if (!_received) return -1; |
okini3939 | 5:e5a358e9ed94 | 77 | if (_rxmsg.msgid != NECMSG_SEND_DAT && _rxmsg.msgid != NECMSG_SEND_NOACK) return -1; |
okini3939 | 4:07e752ff8dce | 78 | |
okini3939 | 4:07e752ff8dce | 79 | r = _rxmsg.length - NEC_HEADER_SIZE; |
okini3939 | 5:e5a358e9ed94 | 80 | if (r > len) r = len; |
okini3939 | 5:e5a358e9ed94 | 81 | memcpy(data, _rxmsg.parameter, r); |
okini3939 | 4:07e752ff8dce | 82 | if (dest) *dest = ntohl(_rxmsg.dstid); |
okini3939 | 4:07e752ff8dce | 83 | if (src) *src = ntohl(_rxmsg.srcid); |
okini3939 | 4:07e752ff8dce | 84 | return r; |
okini3939 | 0:337524afec9b | 85 | } |