Skip to content

Commit 8fb1cb0

Browse files
committed
V2.0.0! API changed, fix tag timeout handling.
Remove ```update```. ```get_new_tag_id``` acts like the previous ```get_tag_id```. ```get_tag_id``` can be use instead of ```is_tag_near```. Fix previous wrong tag timeout handling.
1 parent 015da41 commit 8fb1cb0

File tree

11 files changed

+125
-74
lines changed

11 files changed

+125
-74
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ A simple library to interface with RDM6300 RFID reader.
33

44
## Features
55
* Fast and single tag reading, even if it held near the antenna for a while.
6-
* Using a single configurable GPIO pin.
6+
* Using a single given GPIO pin.
77
* Can tell if the tag is still near the antenna.
88
* Both hardware and software uart (serial) support on esp8266.
99
* SAMD hardware uart (serial) support.
@@ -23,10 +23,14 @@ A simple RFID tag reader with textual serial output:
2323
[```examples/read_to_serial/read_to_serial.ino```](examples/read_to_serial/read_to_serial.ino)
2424

2525
#### API
26-
* ```void begin(Stream *stream)``` - Initialize the object to use the given stream to read from the RDM6300.
27-
* ```void begin(int rxPin, uint8_t uart_nr=1)``` - Initialize the object to use the given GPIO pin as RX from the RDM6300.
28-
* ```bool update()``` - Updates the internal values by reading from the RDM6300, returns true on tag detection, must be called repeatedly!
29-
* ```uint32_t get_tag_id()``` - Returns the last tag id read by ```update```- can be called only once after ```update```.
30-
* ```bool is_tag_near()``` - Returns whether a tag is currently held near the antenna- can be called many times after ```update```.
26+
* ```void begin(Stream *stream)``` - Initialize instance to read from a given stream.
27+
* ```void begin(int rx_pin, uint8_t uart_nr=1)``` - Initialize instance to read from a given pin.
28+
* ```void set_tag_timeout(uint32_t tag_timeout_ms)``` - sets the tag "valid" timeout, (300ms default)
29+
RDM6300 sends packet every 65ms when tag is near- better higher values for debouncing.
30+
* ```uint32_t get_tag_id()``` - Returns the tag_id as long as it is near, 0 otherwise.
31+
* ```uint32_t get_new_tag_id()``` - Returns the tag_id of a "new" near tag,
32+
following calls will return 0 as long as the same tag is kept near.
33+
* ~~```bool update()``` - Updates the internal values must be called repeatedly!~~ **deprecated!**
34+
* ~~```bool is_tag_near()``` - Returns whether a tag is held near.~~ **deprecated!** use ```get_tag_id```.
3135
## Enjoy!
3236
A.E.TECH

examples/altsoftserial_based/altsoftserial_based.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
Rdm6300 rdm6300;
2727
AltSoftSerial alt_soft_serial;
2828

29-
3029
void setup()
3130
{
3231
Serial.begin(115200);
@@ -42,11 +41,13 @@ void setup()
4241

4342
void loop()
4443
{
45-
/* if non-zero tag_id, update() returns true- a new tag is near! */
46-
if (rdm6300.update())
44+
/* get_new_tag_id returns the tag_id of a "new" near tag,
45+
following calls will return 0 as long as the same tag is kept near. */
46+
if (rdm6300.get_new_tag_id())
4747
Serial.println(rdm6300.get_tag_id(), HEX);
4848

49-
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
49+
/* get_tag_id returns the tag_id as long as it is near, 0 otherwise. */
50+
digitalWrite(READ_LED_PIN, rdm6300.get_tag_id());
5051

5152
delay(10);
5253
}

examples/esp8266_hardware_uart/esp8266_hardware_uart.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ void setup()
4747

4848
void loop()
4949
{
50-
/* if non-zero tag_id, update() returns true- a new tag is near! */
51-
if (rdm6300.update())
50+
/* get_new_tag_id returns the tag_id of a "new" near tag,
51+
following calls will return 0 as long as the same tag is kept near. */
52+
if (rdm6300.get_new_tag_id())
5253
Serial1.println(rdm6300.get_tag_id(), HEX);
5354

54-
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
55+
/* get_tag_id returns the tag_id as long as it is near, 0 otherwise. */
56+
digitalWrite(READ_LED_PIN, rdm6300.get_tag_id());
5557

5658
delay(10);
5759
}

examples/hardware_uart/hardware_uart.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ void setup()
2929

3030
void loop()
3131
{
32-
/* if non-zero tag_id, update() returns true- a new tag is near! */
33-
if (rdm6300.update())
32+
/* get_new_tag_id returns the tag_id of a "new" near tag,
33+
following calls will return 0 as long as the same tag is kept near. */
34+
if (rdm6300.get_new_tag_id())
3435
Serial.println(rdm6300.get_tag_id(), HEX);
3536

36-
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
37+
/* get_tag_id returns the tag_id as long as it is near, 0 otherwise. */
38+
digitalWrite(READ_LED_PIN, rdm6300.get_tag_id());
3739

3840
delay(10);
3941
}

examples/multiple_software_serials/multiple_software_serials.ino

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

2929
#include <rdm6300.h>
3030

31-
#define RDM6300_1_RX_PIN 5
32-
#define RDM6300_1_LED_PIN 6
33-
#define RDM6300_2_RX_PIN 7
34-
#define RDM6300_2_LED_PIN 8
31+
#define RDM6300_1_RX_PIN (5)
32+
#define RDM6300_1_LED_PIN (6)
33+
#define RDM6300_2_RX_PIN (7)
34+
#define RDM6300_2_LED_PIN (8)
3535

3636
Rdm6300 rdm6300_1;
3737
Rdm6300 rdm6300_2;
@@ -55,23 +55,23 @@ void setup()
5555

5656
void loop()
5757
{
58-
// checkout listening if needed
59-
if (digitalRead(RDM6300_1_LED_PIN) == LOW
60-
&& !rdm6300_1.is_listening()) {
58+
/* listen to a "talking" RDM6300 if needed */
59+
if (!digitalRead(RDM6300_1_LED_PIN) && !rdm6300_1.is_listening()) {
6160
Serial.println("Switch to listening RDM6300_1");
6261
rdm6300_1.listen();
6362
current_rdm6300 = &rdm6300_1;
64-
} else if (digitalRead(RDM6300_2_LED_PIN) == LOW
65-
&& !rdm6300_2.is_listening()) {
63+
}
64+
else if (!digitalRead(RDM6300_2_LED_PIN) && !rdm6300_2.is_listening()) {
6665
Serial.println("Switch to listening RDM6300_2");
6766
rdm6300_2.listen();
6867
current_rdm6300 = &rdm6300_2;
6968
}
7069

71-
/* If you use rdm6300 with other devices, you had better set a TIMEOUT and run a while-loop to
72-
make sure the tag can be read correctely. */
73-
if (current_rdm6300->update())
70+
/* if you use rdm6300 with other devices,
71+
you had better set a TIMEOUT and run a while-loop to
72+
make sure the tag can be read correctely */
73+
if (current_rdm6300->get_new_tag_id())
7474
Serial.println(current_rdm6300->get_tag_id(), HEX);
75-
75+
7676
delay(10);
7777
}

examples/read_to_serial/read_to_serial.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Arad Eizen (https://github.com/arduino12).
1414
*/
1515

16+
#include <Arduino.h>
1617
#include <rdm6300.h>
1718

1819
#define RDM6300_RX_PIN 4 // read the SoftwareSerial doc above! may need to change this pin to 10...
@@ -34,11 +35,13 @@ void setup()
3435

3536
void loop()
3637
{
37-
/* if non-zero tag_id, update() returns true- a new tag is near! */
38-
if (rdm6300.update())
38+
/* get_new_tag_id returns the tag_id of a "new" near tag,
39+
following calls will return 0 as long as the same tag is kept near. */
40+
if (rdm6300.get_new_tag_id())
3941
Serial.println(rdm6300.get_tag_id(), HEX);
4042

41-
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
43+
/* get_tag_id returns the tag_id as long as it is near, 0 otherwise. */
44+
digitalWrite(READ_LED_PIN, rdm6300.get_tag_id());
4245

4346
delay(10);
4447
}

keywords.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ Rdm6300 KEYWORD1
1313
#######################################
1414
begin KEYWORD2
1515
end KEYWORD2
16-
update KEYWORD2
16+
set_tag_timeout KEYWORD2
1717
get_tag_id KEYWORD2
18-
is_tag_near KEYWORD2
18+
get_new_tag_id KEYWORD2
1919
listen KEYWORD2
2020
is_listening KEYWORD2
2121

2222
#######################################
2323
# Constants (LITERAL1)
2424
#######################################
25-
RDM6300_BAUDRATE LITERAL1
26-
RDM6300_PACKET_SIZE LITERAL1
27-
RDM6300_PACKET_BEGIN LITERAL1
28-
RDM6300_PACKET_END LITERAL1
29-
RDM6300_NEXT_READ_MS LITERAL1
30-
RDM6300_READ_TIMEOUT LITERAL1
25+
RDM6300_BAUDRATE LITERAL1
26+
RDM6300_PACKET_SIZE LITERAL1
27+
RDM6300_PACKET_BEGIN LITERAL1
28+
RDM6300_PACKET_END LITERAL1
29+
RDM6300_DEFAULT_TAG_TIMEOUT_MS LITERAL1
30+
RDM6300_READ_TIMEOUT LITERAL1

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rdm6300",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"keywords": "rdm6300, rfid",
55
"description": "A simple library to interface with rdm6300 rfid reader.",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Rdm6300
2-
version=1.3.0
2+
version=2.0.0
33
author=Arad Eizen
44
maintainer=Arad Eizen <https://github.com/arduino12>
55
sentence=A simple library to interface with rdm6300 rfid reader.

src/rdm6300.cpp

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55

66
#include "rdm6300.h"
7-
#include <Arduino.h>
8-
97

108
void Rdm6300::begin(Stream *stream)
119
{
@@ -55,29 +53,29 @@ void Rdm6300::end()
5553
#endif
5654
}
5755

58-
bool Rdm6300::update(void)
56+
uint32_t Rdm6300::_read_tag_id(void)
5957
{
6058
char buff[RDM6300_PACKET_SIZE];
61-
uint32_t tag_id;
6259
uint8_t checksum;
60+
uint32_t tag_id;
6361

6462
if (!_stream)
65-
return false;
63+
return 0;
6664

6765
if (!_stream->available())
68-
return false;
66+
return 0;
6967

7068
/* if a packet doesn't begin with the right byte, remove that byte */
7169
if (_stream->peek() != RDM6300_PACKET_BEGIN && _stream->read())
72-
return false;
70+
return 0;
7371

7472
/* if read a packet with the wrong size, drop it */
7573
if (RDM6300_PACKET_SIZE != _stream->readBytes(buff, RDM6300_PACKET_SIZE))
76-
return false;
74+
return 0;
7775

7876
/* if a packet doesn't end with the right byte, drop it */
7977
if (buff[13] != RDM6300_PACKET_END)
80-
return false;
78+
return 0;
8179

8280
/* add null and parse checksum */
8381
buff[13] = 0;
@@ -93,31 +91,66 @@ bool Rdm6300::update(void)
9391
for (uint8_t i = 0; i < 32; i += 8)
9492
checksum ^= ((tag_id >> i) & 0xFF);
9593
if (checksum)
96-
return false;
94+
return 0;
95+
96+
return tag_id;
97+
}
98+
99+
void Rdm6300::_update(void)
100+
{
101+
uint32_t cur_ms = millis();
102+
uint32_t tag_id = _read_tag_id();
97103

98104
/* if a new tag appears- return it */
99-
if (_last_tag_id != tag_id) {
100-
_last_tag_id = tag_id;
101-
_last_read_ms = 0;
105+
if (tag_id) {
106+
_tag_id = tag_id;
107+
if (_last_tag_id != tag_id) {
108+
_last_tag_id = tag_id;
109+
_new_tag_id = tag_id;
110+
}
111+
_last_tag_ms = cur_ms;
112+
return;
102113
}
103-
/* if the old tag is still here set tag_id to zero */
104-
if (is_tag_near())
105-
tag_id = 0;
106-
_last_read_ms = millis();
107114

108-
_tag_id = tag_id;
109-
return tag_id;
115+
/* if the old tag id is still valid- leve it */
116+
if (!_tag_id || (cur_ms - _last_tag_ms < _tag_timeout_ms))
117+
return;
118+
119+
_tag_id = _last_tag_id = _new_tag_id = 0;
110120
}
111121

112-
bool Rdm6300::is_tag_near(void)
122+
/*
123+
* Sets the tag "valid" timeout,
124+
* RDM6300 sends packet every 65ms when tag is near-
125+
* so theoretically this timeout should be the same,
126+
* but it is better to debounce it by setting it higher,
127+
* like the RDM6300_DEFAULT_TAG_TIMEOUT_MS = 300.
128+
*/
129+
void Rdm6300::set_tag_timeout(uint32_t tag_timeout_ms)
113130
{
114-
return millis() - _last_read_ms < RDM6300_NEXT_READ_MS;
131+
_tag_timeout_ms = tag_timeout_ms;
115132
}
116133

134+
/*
135+
* Returns the tag_id as long as it is near (and the tag timout is valid),
136+
* or 0 if no tag is near.
137+
*/
117138
uint32_t Rdm6300::get_tag_id(void)
118139
{
119-
uint32_t tag_id = _tag_id;
120-
_tag_id = 0;
140+
_update();
141+
return _tag_id;
142+
}
143+
144+
/*
145+
* Returns the tag_id only once per "new" near tag,
146+
* or 0 if no tag is near.
147+
* Following calls will return 0 as long as the same tag is kept near.
148+
*/
149+
uint32_t Rdm6300::get_new_tag_id(void)
150+
{
151+
_update();
152+
uint32_t tag_id = _new_tag_id;
153+
_new_tag_id = 0;
121154
return tag_id;
122155
}
123156

0 commit comments

Comments
 (0)