44 */
55
66#include " rdm6300.h"
7- #include < Arduino.h>
8-
97
108void 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+ */
117138uint32_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