forked from pycom/pycom-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (72 loc) · 2.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
Simple Pyscan NFC / MiFare Classic Example
Copyright (c) 2020, Pycom Limited.
This example runs the NFC discovery loop in a thread.
If a card is detected it will read the UID and compare it to VALID_CARDS
RGB LED is BLUE while waiting for card,
GREEN if card is valid, RED if card is invalid
'''
DEBUG = False # change to True to see debug messages
from pyscan import Pyscan
from MFRC630 import MFRC630
from LIS2HH12 import LIS2HH12
from LTR329ALS01 import LTR329ALS01
import binascii
import time
import pycom
import _thread
VALID_CARDS = [[0x43, 0x95, 0xDD, 0xF8],
[0x43, 0x95, 0xDD, 0xF9],
[0x46, 0x5A, 0xEB, 0x7D, 0x8A, 0x08, 0x04]]
py = Pyscan()
nfc = MFRC630(py)
lt = LTR329ALS01(py)
li = LIS2HH12(py)
RGB_BRIGHTNESS = 0x8
RGB_RED = (RGB_BRIGHTNESS << 16)
RGB_GREEN = (RGB_BRIGHTNESS << 8)
RGB_BLUE = (RGB_BRIGHTNESS)
# Make sure heartbeat is disabled before setting RGB LED
pycom.heartbeat(False)
# Initialise the MFRC630 with some settings
nfc.mfrc630_cmd_init()
def check_uid(uid, len):
return VALID_CARDS.count(uid[:len])
def print_debug(msg):
if DEBUG:
print(msg)
def send_sensor_data(name, timeout):
while(True):
print(lt.light())
print(li.acceleration())
time.sleep(timeout)
def discovery_loop(nfc, id):
while True:
# Send REQA for ISO14443A card type
print_debug('Sending REQA for ISO14443A card type...')
atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA)
print_debug('Response: {}'.format(atqa))
if (atqa != 0):
# A card has been detected, read UID
print_debug('A card has been detected, read UID...')
uid = bytearray(10)
uid_len = nfc.mfrc630_iso14443a_select(uid)
print_debug('UID has length: {}'.format(uid_len))
if (uid_len > 0):
print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper()))
if (check_uid(list(uid), uid_len)) > 0:
print_debug('Card is listed, turn LED green')
pycom.rgbled(RGB_GREEN)
else:
print_debug('Card is not listed, turn LED red')
pycom.rgbled(RGB_RED)
else:
# No card detected
print_debug('Did not detect any card...')
pycom.rgbled(RGB_BLUE)
nfc.mfrc630_cmd_reset()
time.sleep(.5)
nfc.mfrc630_cmd_init()
# This is the start of our main execution... start the thread
_thread.start_new_thread(discovery_loop, (nfc, 0))
_thread.start_new_thread(send_sensor_data, ('Thread 2', 10))