10 #include "MultiChannelDevice.h"
13 #ifdef USE_MFRC522_I2C
15 #include <MFRC522_I2C.h>
22 #ifdef USE_MFRC522_SPI
27 #include <SoftwareSerial.h>
30 #define ID_ADDR_SIZE 8
34 DEFREGISTER(RFIDReg1,CREG_AES_ACTIVE,0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7)
35 class RFIDList1 :
public RegList1<RFIDReg1> {
37 RFIDList1 (uint16_t addr) : RegList1<RFIDReg1>(addr) {}
46 bool free(uint8_t*addr) {
48 for (uint8_t n = 0; n < ID_ADDR_SIZE; n++) {
49 if (addr[n] != 0x00) {
57 void init(uint8_t msgcnt, uint8_t ch, uint8_t*addr) {
58 char hexstr[ID_ADDR_SIZE * 2];
60 for (uint8_t n = 0; n < (ID_ADDR_SIZE * 2); n++)
64 unsigned char * pin = addr;
65 const char * hex =
"0123456789ABCDEF";
68 for(; i < ID_ADDR_SIZE-1; ++i){
69 *pout++ = hex[(*pin>>4)&0xF];
70 *pout++ = hex[(*pin++)&0xF];
72 *pout++ = hex[(*pin>>4)&0xF];
73 *pout++ = hex[(*pin)&0xF];
78 Message::init(0x1a, msgcnt, AS_MESSAGE_SENSOR_DATA, BIDI , ch , hexstr[0]);
79 for (uint8_t i = 1; i < (ID_ADDR_SIZE * 2); i++) {
80 pload[i-1] = hexstr[i];
85 template<
class HALTYPE,
int PEERCOUNT,
class List0Type=List0>
89 enum { none=0, released, longpressed, longreleased };
91 uint8_t state, matches, repeatcnt;
99 virtual void trigger (__attribute__((unused))
AlarmClock& clock) {
104 uint8_t status ()
const {
108 uint8_t flags ()
const {
116 bool check (uint8_t* addr) {
117 if( free() ==
false && isID(addr) ==
true ) {
118 matches |= 0b00000001;
125 uint8_t buf[ID_ADDR_SIZE];
126 RFIDList1 l = this->getList1();
127 for( uint8_t n=0; n< ID_ADDR_SIZE; ++n ) {
128 buf[n] = l.readRegister(0xe0+n);
131 chipIdMsg.init(this->device().nextcount(), this->number(), buf);
133 this->device().sendPeerEvent(chipIdMsg, *
this);
139 if( (matches & 0b00111111) == 0b00000111 || (matches & 0b00111111) == 0b00111111 ) {
141 DPRINTLN(F(
"longpressed"));
142 this->device().buzzer().on();
144 matches &= 0b11000111;
147 else if( (matches & 0b00001111) == 0b00001110 ) {
149 DPRINTLN(F(
"longreleased"));
150 this->device().buzzer().off();
153 else if( (matches & 0b00000011) == 0b00000010 ) {
155 DPRINTLN(F(
"released"));
156 this->device().buzzer().on(millis2ticks(100));
160 msg.init(this->device().nextcount(),this->number(),repeatcnt,(s==longreleased || s==longpressed),this->device().battery().low());
161 if( s == released || s == longreleased) {
163 this->device().sendPeerEvent(msg,*
this);
166 else if (s == longpressed) {
168 this->device().broadcastPeerEvent(msg,*
this);
173 bool match (uint8_t* addr) {
175 bool res = check(addr);
180 bool isID (uint8_t* buf) {
181 RFIDList1 l = this->getList1();
182 for( uint8_t n=0; n< ID_ADDR_SIZE; ++n ) {
183 if( l.readRegister(0xe0+n) != buf[n] ) {
190 void storeID (uint8_t* buf) {
191 if( learn() ==
true ) {
192 for( uint8_t n=0; n < ID_ADDR_SIZE; ++n ) {
193 this->getList1().writeRegister(0xe0+n,buf[n]);
198 sysclock.cancel(*
this);
204 this->getList1().readRegister(0xe0) == 0x00 &&
205 this->getList1().readRegister(0xe1) == 0x00 &&
206 this->getList1().readRegister(0xe2) == 0x00 &&
207 this->getList1().readRegister(0xe3) == 0x00 &&
208 this->getList1().readRegister(0xe4) == 0x00 &&
209 this->getList1().readRegister(0xe5) == 0x00 &&
210 this->getList1().readRegister(0xe6) == 0x00 &&
211 this->getList1().readRegister(0xe7) == 0x00
215 bool learn ()
const {
223 sysclock.cancel(*
this);
224 set(seconds2ticks(60));
231 if ( (msg.len() == ID_ADDR_SIZE) || (msg.len() == 1 && msg.value(0) == 0xcc) ) {
232 for( uint8_t n=0; n < ID_ADDR_SIZE; ++n ) {
233 uint8_t val = msg.len() == 1 ? 0x00:msg.value(n);
234 this->getList1().writeRegister(0xe0+n,val);
237 this->device().getHal().buzzer.on(millis2ticks(300), millis2ticks(200),2);
242 if (msg.value(0) == 0xba) {
244 this->device().getHal().buzzer.on(decis2ticks(msg.value(1)),decis2ticks(msg.value(2)), -1);
247 this->device().getHal().buzzer.on(decis2ticks(msg.value(1)),decis2ticks(msg.value(2)),msg.value(3));
250 if (msg.len() == 1 && msg.value(0) == 0xb1) {
251 this->device().getHal().buzzer.on();
254 if (msg.len() == 1 && msg.value(0) == 0xb0) {
255 this->device().getHal().buzzer.off(
true);
258 if (msg.len() == 1 && msg.value(0) == 0xfe) {
262 if (msg.len() == 2 && msg.value(0) == 0xff) {
263 this->device().getHal().standbyLedInvert(msg.value(1) == 0x01);
269 bool process (__attribute__((unused))
const RemoteEventMsg& msg) {
return false; }
270 bool process (__attribute__((unused))
const SensorEventMsg& msg) {
return false; }
274 template <
class RFIDDev,
class RFIDChannel,WIEGAND& rdrDev,
int LED_GREEN,
int LED_RED>
276 #if (defined(USE_MFRC522_I2C) || defined(USE_MFRC522_SPI))
277 template <
class RFIDDev,
class RFIDChannel,MFRC522& rdrDev,
int LED_GREEN,
int LED_RED>
280 template <
class RFIDDev,
class RFIDChannel,SoftwareSerial& rdrDev,
int LED_GREEN,
int LED_RED>
295 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
297 if( rc.learn() ==
true ) {
305 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
307 if( rc.match(addr) ==
true ) {
315 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
317 if( rc.isID(addr) ==
true ) {
324 void DADDR(uint8_t * addr) {
325 for (uint8_t i = 0; i < ID_ADDR_SIZE; i++)
331 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
338 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
344 bool check (uint8_t* addr) {
346 for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
348 res |= rc.check(addr);
353 bool getRfidAddress(uint8_t *addr) {
355 if (rdrDev.available()) {
356 memset(addr,0x00, ID_ADDR_SIZE);
357 unsigned long wgAddr = rdrDev.getCode();
359 for (uint8_t i = 0; i < ID_ADDR_SIZE; i++)
360 addrArr[i] = wgAddr >> (i*8) & 0xff;
361 memcpy(addr, addrArr, ID_ADDR_SIZE);
369 #if (defined(USE_MFRC522_I2C) || defined(USE_MFRC522_SPI))
370 if (!rdrDev.PICC_IsNewCardPresent())
371 if (!rdrDev.PICC_IsNewCardPresent())
373 if (!rdrDev.PICC_ReadCardSerial())
return false;
374 memset(addr,0x00,ID_ADDR_SIZE);
375 memcpy(addr,rdrDev.uid.uidByte,rdrDev.uid.size);
382 while (rdrDev.available() > 0) {
383 char d = rdrDev.read();
384 static uint8_t bytecount = 0;
385 static uint8_t addrval = 0;
386 static bool decode =
false;
390 memset(addr,0x00,ID_ADDR_SIZE);
395 while (rdrDev.available()) rdrDev.read();
399 if (decode ==
true) {
400 uint8_t val = (d > 57) ? d -= 55 : d -= 48;
401 if (bytecount % 2 == 0) {
405 addr[bytecount/2] = addrval;
418 bool readRfid(uint8_t *addr) {
419 uint8_t iD[ID_ADDR_SIZE];
420 static uint8_t last_addr[ID_ADDR_SIZE];
422 bool success =
false;
424 memset(addr,0,ID_ADDR_SIZE);
425 if (getRfidAddress(iD)) {
426 memcpy(addr,iD,ID_ADDR_SIZE);
427 if (memcmp(addr, last_addr, ID_ADDR_SIZE) != 0) {
428 dev.buzzer().on(millis2ticks(100));
432 memcpy(last_addr,addr,ID_ADDR_SIZE);
438 uint8_t addr[ID_ADDR_SIZE];
441 bool readID = readRfid(addr);
443 if( check(addr) ==
true ) {
444 led.ledOn(millis2ticks(500),0);
447 dev.buzzer().on(millis2ticks(40),millis2ticks(40),3);
453 uint8_t addr[ID_ADDR_SIZE];
454 while( readRfid(addr) ==
true ) {
455 if( find(addr) == 0 ) {
457 dev.buzzer().on(millis2ticks(40), millis2ticks(50),10);
466 set(millis2ticks(500));
472 uint8_t cycle = cnt & 0x01;
473 led.ledOn(cycle == 0 ? tick : 0, cycle == 0 ? 0 : tick);
474 dev.buzzer().on(millis2ticks(40));
476 if( learn(lc) ==
true ) {
478 set(seconds2ticks(5));