Skip to content

Commit 7bd10d3

Browse files
committed
libs/unit/uhf_rfid.py: Add NXP command support.
Signed-off-by: lbuque <[email protected]>
1 parent 7c41ad9 commit 7bd10d3

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

m5stack/libs/driver/jrd4035/__init__.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ def inventory(self) -> bytes:
7676
self._verbose and print("RSSI: %d, PC: %d, EPC: %s, CRC: %d" % (rssi, pc, epc, crc))
7777
return epc
7878

79-
def continuous_inventory(self, count: int):
79+
def continuous_inventory(self, count: int) -> tuple:
8080
struct.pack_into(">BH", self._buffer3, 0, 0x22, count)
8181
rxdata, status = self.command(0x27, data=bytes(self._buffer1))
8282
if status is False:
83-
return
83+
return (0, 0, b"", 0)
8484
rssi, pc, epc, crc = struct.unpack_from(">BH%dsH" % (len(rxdata) - 5), rxdata)
8585
self._verbose and print("RSSI: %d, PC: %d, EPC: %s, CRC: %d" % (rssi, pc, epc, crc))
86-
return rssi, pc, epc, crc
86+
return (rssi, pc, epc, crc)
8787

8888
def stop_continuous_inventory(self) -> bool:
8989
rxdata, status = self.command(0x28)
@@ -97,7 +97,7 @@ def select(
9797
pointer: int,
9898
truncate: bool,
9999
mask: bytes,
100-
):
100+
) -> bool:
101101
buffer = struct.pack(
102102
">BIBB%ds" % (len(mask)),
103103
(target << 5 | action << 3 | membank),
@@ -160,7 +160,7 @@ def read_mem_bank(
160160

161161
def write_mem_bank(
162162
self, bank: int, offset: int, data: str, access: bytes = b"\x00\x00\x00\x00"
163-
):
163+
) -> bool:
164164
data = bytes.fromhex(data)
165165
buffer = bytearray(9 + len(data))
166166
struct.pack_into(
@@ -182,7 +182,7 @@ def lock_mem_bank(
182182
tid_lock: int = 0b00,
183183
user_lock: int = 0b00,
184184
access: bytes = b"\x00\x00\x00\x00",
185-
):
185+
) -> bool:
186186
payload = 0b1111_1111_1100_0000_0000
187187
payload |= kill_lock << 8
188188
payload |= access_lock << 6
@@ -205,19 +205,19 @@ def lock_mem_bank(
205205
(_, _, _, param) = struct.unpack_from(">BH%dsB" % (len(rxdata) - 4), rxdata)
206206
return True if param == 0x00 else False
207207

208-
def set_access_password(self, password: bytes):
208+
def set_access_password(self, password: bytes) -> None:
209209
self.write_mem_bank(self, self.RFU, 0x00, password)
210210

211-
def set_kill_password(self, password: bytes):
211+
def set_kill_password(self, password: bytes) -> None:
212212
self.write_mem_bank(self, self.RFU, 0x20, password)
213213

214-
def kill(self, password: bytes = b"\x00\x00\x00\x00"):
214+
def kill(self, password: bytes = b"\x00\x00\x00\x00") -> bool:
215215
rxdata, status = self.command(0x65, data=password)
216216
return True if status and rxdata == b"\x00" else False
217217

218218
def set_query_param(
219219
self, dr=0b0, m=0b00, tr_ext=0b1, sel=0b00, session=0b00, target=0b0, q=0b0100
220-
):
220+
) -> bool:
221221
if dr != 0b0 and m != 0b00 and tr_ext != 0b1:
222222
raise ValueError("Invalid query parameters")
223223
payload = 0b0000_0000_0000_0000
@@ -326,58 +326,58 @@ def get_blocking_signal_strength(self, channel: int) -> int:
326326
strengths = self.get_blocking_signal_strength_all()
327327
return strengths[channel] if strengths else -1
328328

329-
def get_blocking_signal_strength_all(self) -> [tuple | None]:
329+
def get_blocking_signal_strength_all(self) -> tuple:
330330
rxdata, status = self.command(0xF2)
331331
if status is False:
332-
return None
332+
return (-1 for _ in range(20))
333333
fmt = "".join("b" for _ in range((len(rxdata) - 2)))
334334
return struct.unpack_from(">%s" % (fmt), rxdata, 2)
335335

336336
def get_channel_rssi(self, channel: int) -> int:
337337
return self.get_channel_rssi_all()[channel]
338338

339-
def get_channel_rssi_all(self) -> [tuple | None]:
339+
def get_channel_rssi_all(self) -> tuple:
340340
rxdata, status = self.command(0xF3)
341341
if status is False:
342-
return None
342+
return (-1 for _ in range(20))
343343
fmt = "".join("b" for _ in range((len(rxdata) - 2)))
344344
return struct.unpack_from(">%s" % (fmt), rxdata, 2)
345345

346346
def sleep(self) -> bool:
347347
rxdata, status = self.command(0x17)
348348
return True if status and rxdata == b"\x00" else False
349349

350-
def wake(self):
350+
def wake(self) -> None:
351351
self._uart.write(b"\x55")
352352
time.sleep(0.1)
353353

354-
def set_automatic_sleep_time(self, min: int):
354+
def set_automatic_sleep_time(self, min: int) -> bool:
355355
struct.pack_into(">B", self._buffer1, 0, min)
356356
rxdata, status = self.command(0x1D, data=self._buffer1)
357357
return True if status and rxdata == b"\x00" else False
358358

359-
def disable_automatic_sleep(self):
359+
def disable_automatic_sleep(self) -> bool:
360360
return self.set_automatic_sleep_time(0)
361361

362-
def nxp_read_protect(self, set, access_password: bytes = b"\x00\x00\x00\x00"):
362+
def nxp_read_protect(self, set, access_password: bytes = b"\x00\x00\x00\x00") -> bool:
363363
struct.pack_into(">4sB", self._buffer, 0, access_password, set)
364364
rxdata, status = self.command(0xE1, data=self._buffer[0:5])
365365
return True if status and rxdata == b"\x00" else False
366366

367-
def nxp_change_eas(self, set, access_password: bytes = b"\x00\x00\x00\x00"):
368-
struct.pack_into(">4sB", self._buffer, 0, access_password, 0x01)
367+
def nxp_change_eas(self, set, access_password: bytes = b"\x00\x00\x00\x00") -> bool:
368+
struct.pack_into(">4sB", self._buffer, 0, access_password, set)
369369
rxdata, status = self.command(0xE3, data=self._buffer[0:5])
370370
return True if status and rxdata == b"\x00" else False
371371

372-
def nxp_eas_alarm(self):
372+
def nxp_eas_alarm(self) -> bytes:
373373
rxdata, status = self.command(0xE4)
374374
if status is False:
375375
return b""
376376

377377
(code,) = struct.unpack_from(">8s", rxdata)
378378
return code
379379

380-
def nxp_read_config_word(self, access_password: bytes = b"\x00\x00\x00\x00"):
380+
def nxp_read_config_word(self, access_password: bytes = b"\x00\x00\x00\x00") -> int:
381381
struct.pack_into(">4sH", self._buffer, 0, access_password, 0x0000)
382382
rxdata, status = self.command(0xE2, data=self._buffer[0:6])
383383
if status is False:
@@ -386,20 +386,24 @@ def nxp_read_config_word(self, access_password: bytes = b"\x00\x00\x00\x00"):
386386
(config_word,) = struct.unpack_from(">H", rxdata, 1)
387387
return config_word
388388

389-
def set_nxp_confog_word(self, config_word, access_password: bytes = b"\x00\x00\x00\x00"):
389+
def set_nxp_confog_word(
390+
self, config_word: int, access_password: bytes = b"\x00\x00\x00\x00"
391+
) -> bool:
390392
old_config_word = self.nxp_read_config_word(access_password)
391393
config_word = config_word ^ old_config_word
392394
struct.pack_into(">4sH", self._buffer, 0, access_password, config_word)
393395
rxdata, status = self.command(0xE2, data=self._buffer[0:6])
394396
return True if status and rxdata == b"\x00" else False
395397

396-
def get_impinj_monza_qt_sr(self, persistence, password: bytes = b"\x00\x00\x00\x00"):
398+
def get_impinj_monza_qt_sr(self, persistence, password: bytes = b"\x00\x00\x00\x00") -> bool:
397399
return self.impinj_monza_qt_read_cmd(persistence, password=password)[0]
398400

399-
def get_impinj_monza_qt_mem(self, persistence, password: bytes = b"\x00\x00\x00\x00"):
401+
def get_impinj_monza_qt_mem(self, persistence, password: bytes = b"\x00\x00\x00\x00") -> bool:
400402
return self.impinj_monza_qt_read_cmd(persistence, password=password)[1]
401403

402-
def impinj_monza_qt_read_cmd(self, persistence, password: bytes = b"\x00\x00\x00\x00"):
404+
def impinj_monza_qt_read_cmd(
405+
self, persistence, password: bytes = b"\x00\x00\x00\x00"
406+
) -> tuple:
403407
# TODO
404408
payload = 0b0000_0000_0000_0000
405409
struct.pack_into(">4sBBH", self._buffer8, 0, password, 0x00, persistence, payload)
@@ -412,19 +416,19 @@ def impinj_monza_qt_read_cmd(self, persistence, password: bytes = b"\x00\x00\x00
412416

413417
def set_impinj_monza_qt_sr(
414418
self, persistence, qt_sr: bool, password: bytes = b"\x00\x00\x00\x00"
415-
):
419+
) -> bool:
416420
_, qt_mem = self.impinj_monza_qt_read_cmd(persistence, password)
417421
return self.impinj_monza_qt_write_cmd(persistence, qt_sr, qt_mem, password)
418422

419423
def set_impinj_monza_qt_mem(
420424
self, persistence, qt_mem: bool, password: bytes = b"\x00\x00\x00\x00"
421-
):
425+
) -> bool:
422426
qt_sr, _ = self.impinj_monza_qt_read_cmd(persistence, password)
423427
return self.impinj_monza_qt_write_cmd(persistence, qt_sr, qt_mem, password)
424428

425429
def impinj_monza_qt_write_cmd(
426430
self, persistence, qt_sr: bool, qt_mem: bool, password: bytes = b"\x00\x00\x00\x00"
427-
):
431+
) -> bool:
428432
payload = 0b0000_0000_0000_0000
429433
payload |= int(qt_sr) << 15
430434
payload |= int(qt_mem) << 14
@@ -436,12 +440,12 @@ def impinj_monza_qt_write_cmd(
436440
(ul, pc, epc, param) = struct.unpack_from(">BH%dsH" % (len(rxdata) - 4), rxdata)
437441
return True if param == 0x00 else False
438442

439-
def command(self, cmd, data=b"", timeout=5000):
443+
def command(self, cmd, data=b"", timeout=5000) -> tuple:
440444
self._send(cmd, data)
441445
rxdata, status = self._receive(length=100, timeout=timeout)
442446
return rxdata, status
443447

444-
def _send(self, cmd, data=b""):
448+
def _send(self, cmd, data=b"") -> None:
445449
checksum = self._checksum(self._FRAME_CMD, cmd, len(data), data)
446450
frame = struct.pack(
447451
"!BBBH%dsBB" % len(data),
@@ -456,7 +460,7 @@ def _send(self, cmd, data=b""):
456460
self._verbose and print("Frame to send: %s" % (" ".join(f"{byte:02x}" for byte in frame)))
457461
self._uart.write(frame)
458462

459-
def _receive(self, length: int = 8, timeout=1000):
463+
def _receive(self, length: int = 8, timeout=1000) -> tuple:
460464
_buffer = b""
461465
startpos = -1
462466
endpos = -1
@@ -495,7 +499,7 @@ def _receive(self, length: int = 8, timeout=1000):
495499
self._verbose and print("Malformed packet received, ignore it")
496500
return b"", False
497501

498-
def _checksum(self, *args):
502+
def _checksum(self, *args) -> int:
499503
chcksum = 0
500504
for arg in args:
501505
if isinstance(arg, int):
@@ -505,7 +509,7 @@ def _checksum(self, *args):
505509
chcksum += x
506510
return chcksum & 0xFF
507511

508-
def check_error_code(self, code):
512+
def check_error_code(self, code) -> None:
509513
if code == 0x17:
510514
raise Exception("Invalid command")
511515
if code == 0x20:

m5stack/libs/unit/uhf_rfid.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,18 @@ def set_impinj_monza_qt_sr(self, qt_sr: bool, persistence: int, password: str =
7272

7373
def set_impinj_monza_qt_mem(self, qt_mem: bool, persistence: int, password: str = "00000000"):
7474
return super().set_impinj_monza_qt_mem(persistence, qt_mem, bytes.fromhex(password))
75+
76+
def nxp_eas_alarm(self):
77+
return super().nxp_eas_alarm().hex()
78+
79+
def get_nxp_config_word(self, password: str = "00000000") -> int:
80+
return super().nxp_read_config_word(bytes.fromhex(password))
81+
82+
def set_nxp_config_word(self, config_word: int, password: str = "00000000"):
83+
return super().set_nxp_confog_word(config_word, bytes.fromhex(password))
84+
85+
def nxp_read_protect(self, set, password: str = "00000000"):
86+
return super().nxp_read_protect(set, bytes.fromhex(password))
87+
88+
def nxp_change_eas(self, set, password: str = "00000000"):
89+
return super().nxp_change_eas(set, bytes.fromhex(password))

0 commit comments

Comments
 (0)