started conversion of pico code for communication with pc, use of stdin/out instead of input and print

This commit is contained in:
Angoosh Leviocki 2023-09-26 21:52:14 +02:00
parent 6a5b730f81
commit e123b57165
Signed by: angoosh
GPG Key ID: 1CCB948F5CBF1995

View File

@ -12,6 +12,21 @@ BRIGHTNESS = 0.2 # Adjust the brightness (0.0 - 1.0)
led_strip = neopixel.NeoPixel(Pin(ws_pin), led_num) led_strip = neopixel.NeoPixel(Pin(ws_pin), led_num)
def set_neopixel_brightness(color):
r, g, b = color
r = int(r * BRIGHTNESS)
g = int(g * BRIGHTNESS)
b = int(b * BRIGHTNESS)
return (r, g, b)
def set_neopixel_color(r,g,b):
color = (r,g,b)
color = set_neopixel_brightness(color)
led_strip.fill(color)
led_strip.write()
set_neopixel_color(0,0,0)
# SPI # SPI
spi_dev = SPI(0, baudrate=1000000) spi_dev = SPI(0, baudrate=1000000)
cs = Pin(17, Pin.OUT) cs = Pin(17, Pin.OUT)
@ -20,7 +35,7 @@ cs.on()
# SENSOR INIT # SENSOR INIT
pn532 = nfc.PN532(spi_dev,cs) pn532 = nfc.PN532(spi_dev,cs)
ic, ver, rev, support = pn532.get_firmware_version() ic, ver, rev, support = pn532.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev)) #print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
# Configure PN532 to communicate with MiFare cards # Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration() pn532.SAM_configuration()
@ -38,20 +53,6 @@ def read_nfc(dev, tmot):
print('Found card with UID:', [hex(i) for i in uid]) print('Found card with UID:', [hex(i) for i in uid])
print('Number_id: {}'.format(string_ID)) print('Number_id: {}'.format(string_ID))
#set neopixel brightness
def set_neopixel_brightness(color):
r, g, b = color
r = int(r * BRIGHTNESS)
g = int(g * BRIGHTNESS)
b = int(b * BRIGHTNESS)
return (r, g, b)
def set_neopixel_color(r,g,b):
color = (r,g,b)
color = set_neopixel_brightness(color)
led_strip.fill(color)
led_strip.write()
def loop_neopixel(): def loop_neopixel():
# Display red # Display red
color = (255, 0, 0) # Red color color = (255, 0, 0) # Red color
@ -75,7 +76,7 @@ def loop_neopixel():
time.sleep(1) time.sleep(1)
def read_nfc_loop(): def read_nfc_loop():
print("Waiting for RFID/NFC card to read!") sys.stdout.write('Waiting for RFID/NFC card to read!\r')
set_neopixel_color(255,0,0) set_neopixel_color(255,0,0)
while True: while True:
# Check if a card is available to read # Check if a card is available to read
@ -84,12 +85,10 @@ def read_nfc_loop():
if uid is not None: if uid is not None:
break break
print("")
print("Found card with UID:", [hex(i) for i in uid])
set_neopixel_color(255,255,0) set_neopixel_color(255,255,0)
ntag_string = '' ntag_string = 'Data: '
sys.stdout.write('Reading ntag \r')
for page in range(20,40): for page in range(20,40):
try: try:
ntag2xx_page = pn532.ntag2xx_read_block(page) ntag2xx_page = pn532.ntag2xx_read_block(page)
@ -98,14 +97,14 @@ def read_nfc_loop():
for char in ntag2xx_page: for char in ntag2xx_page:
ntag_string += chr(char) ntag_string += chr(char)
else: else:
print('Error reading card') sys.stdout.write('Error reading card\r')
return 1 return 1
except: except:
print('Error reading card') sys.stdout.write('Error reading card\r')
return 1 return 1
set_neopixel_color(0,255,0) set_neopixel_color(0,255,0)
print(ntag_string) sys.stdout.write(ntag_string + '\r')
time.sleep(3) time.sleep(3)
return 0 return 0
@ -118,7 +117,7 @@ def write_nfc_loop(data):
if (length % 4) != 0: if (length % 4) != 0:
pages += 1 pages += 1
print("Waiting for RFID/NFC card to write to!") sys.stdout.write("Waiting for RFID/NFC card to write to!\r")
set_neopixel_color(255,0,0) set_neopixel_color(255,0,0)
while True: while True:
# Check if a card is available to read # Check if a card is available to read
@ -132,12 +131,12 @@ def write_nfc_loop(data):
set_neopixel_color(255,255,0) set_neopixel_color(255,255,0)
print('Erasing card') sys.stdout.write('Erasing card\r')
for page in range(20,41): for page in range(20,41):
content_erase = b'\x00\x00\x00\x00' content_erase = b'\x00\x00\x00\x00'
pn532.ntag2xx_write_block(page, content_erase) pn532.ntag2xx_write_block(page, content_erase)
print('Writing card') sys.stdout.write('Writing card\r')
for page in range(20,20 + pages): for page in range(20,20 + pages):
content_slice = bytearray(4) content_slice = bytearray(4)
for i in range(0,4): for i in range(0,4):
@ -149,15 +148,19 @@ def write_nfc_loop(data):
print(content_slice) print(content_slice)
pn532.ntag2xx_write_block(page, content_slice) pn532.ntag2xx_write_block(page, content_slice)
print('Write done') sys.stdout.write('Write done\r')
set_neopixel_color(0,255,0) set_neopixel_color(0,255,0)
def loop(): def loop():
rw = input('Read or Write tags? [R/w]') poll_results = poll_obj.poll(1)
if poll_results:
rw = sys.stdin.readline().strip()
sys.stdout.write("received data: " + rw + "\r")
rw = rw.lower() rw = rw.lower()
if rw == 'w': if rw == 'w':
while True: while True:
data = input('ID:') sys.stdout.write('ID: \r')
data = sys.stdin.readline().strip()
write_nfc_loop(data) write_nfc_loop(data)
elif rw == 'r': elif rw == 'r':
while True: while True:
@ -166,24 +169,24 @@ def loop():
while True: while True:
read_nfc_loop() read_nfc_loop()
else: else:
print('Invalid option') sys.stdout.write('Invalid option\r')
poll_obj = select.poll() poll_obj = select.poll()
poll_obj.register(sys.stdin, select.POLLIN) poll_obj.register(sys.stdin, select.POLLIN)
set_neopixel_color(0,0,255) set_neopixel_color(0,0,255)
try:
while True: while True:
poll_results = poll_obj.poll(1) poll_results = poll_obj.poll(1)
if poll_results: if poll_results:
while True: while True:
try: try:
ret = loop() ret = loop()
except: except:
set_neopixel_color(0,0,255) set_neopixel_color(0,0,255)
break break
else: else:
continue continue
except: #except:
set_neopixel_color(0,0,0) # set_neopixel_color(0,0,0)