-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfcshow.py
executable file
·110 lines (92 loc) · 2.84 KB
/
nfcshow.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# NFC interactor
# Raspberry Pi Zero W + PiTFT 240x240
#for display
import board
from digitalio import DigitalInOut
import qrcode
# for NATS
import os
import asyncio
import nats
from nats.errors import TimeoutError
servers = os.environ.get("NATS_URL", "nats://localhost:4222").split(",")
# set up tft display
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import st7789
cs_pin = DigitalInOut(board.CE0)
dc_pin = DigitalInOut(board.D25)
reset_pin = None
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 64000000
# Setup SPI bus using hardware SPI:
spi = board.SPI()
# Create the ST7789 display:
disp = st7789.ST7789(
spi,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=240,
height=240,
x_offset=0,
y_offset=80,
)
# Create blank image for drawing.
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
image = Image.new("RGB", (width, height))
rotation = 180
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height - padding
# Load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24)
# Turn on the backlight
backlight = DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
async def main():
# Create the connection to NATS which takes a list of servers.
nc = await nats.connect(servers=servers)
sub = await nc.subscribe("local.nfcread")
print("NATS listening")
countout = 0
while True:
# clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
try:
msg = await sub.next_msg(timeout=0.1)
x = 0
y = top
HDR = "FOUND TAG"
draw.text((x, y), HDR, font=font, fill="#FFFFFF")
y += font.getbbox(HDR)[3]
UID = f"{msg.data}"
draw.text((x, y), UID, font=font, fill="#FFFF00")
y += font.getbbox(UID)[3]
y += 3
qr_height = bottom - y
qr_shift = int((width - qr_height)/2)
# create a QR code
qr_img = qrcode.make("NFC "+UID).resize((qr_height,qr_height))
#combine
image.paste(qr_img.convert("RGB"),box=(qr_shift, y))
# Display image.
disp.image(image, rotation)
except TimeoutError:
countout += 1
if countout > 10:
disp.image(image, rotation)
countout = 0
pass
if __name__ == '__main__':
asyncio.run(main())