From e123b571659e12f2e35daa8327823d922e3fa30a Mon Sep 17 00:00:00 2001 From: Angoosh Date: Tue, 26 Sep 2023 21:52:14 +0200 Subject: [PATCH] started conversion of pico code for communication with pc, use of stdin/out instead of input and print --- SW/RPi_Pico/main.py | 91 +++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/SW/RPi_Pico/main.py b/SW/RPi_Pico/main.py index 131711e..1f6a3ae 100644 --- a/SW/RPi_Pico/main.py +++ b/SW/RPi_Pico/main.py @@ -12,6 +12,21 @@ BRIGHTNESS = 0.2 # Adjust the brightness (0.0 - 1.0) 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_dev = SPI(0, baudrate=1000000) cs = Pin(17, Pin.OUT) @@ -20,7 +35,7 @@ cs.on() # SENSOR INIT pn532 = nfc.PN532(spi_dev,cs) 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 pn532.SAM_configuration() @@ -38,20 +53,6 @@ def read_nfc(dev, tmot): print('Found card with UID:', [hex(i) for i in uid]) 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(): # Display red color = (255, 0, 0) # Red color @@ -75,7 +76,7 @@ def loop_neopixel(): time.sleep(1) 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) while True: # Check if a card is available to read @@ -83,13 +84,11 @@ def read_nfc_loop(): # Try again if no card is available. if uid is not None: break - - print("") - print("Found card with UID:", [hex(i) for i in uid]) set_neopixel_color(255,255,0) - ntag_string = '' + ntag_string = 'Data: ' + sys.stdout.write('Reading ntag \r') for page in range(20,40): try: ntag2xx_page = pn532.ntag2xx_read_block(page) @@ -98,14 +97,14 @@ def read_nfc_loop(): for char in ntag2xx_page: ntag_string += chr(char) else: - print('Error reading card') + sys.stdout.write('Error reading card\r') return 1 except: - print('Error reading card') + sys.stdout.write('Error reading card\r') return 1 set_neopixel_color(0,255,0) - print(ntag_string) + sys.stdout.write(ntag_string + '\r') time.sleep(3) return 0 @@ -118,7 +117,7 @@ def write_nfc_loop(data): if (length % 4) != 0: 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) while True: # Check if a card is available to read @@ -132,12 +131,12 @@ def write_nfc_loop(data): set_neopixel_color(255,255,0) - print('Erasing card') + sys.stdout.write('Erasing card\r') for page in range(20,41): content_erase = b'\x00\x00\x00\x00' pn532.ntag2xx_write_block(page, content_erase) - print('Writing card') + sys.stdout.write('Writing card\r') for page in range(20,20 + pages): content_slice = bytearray(4) for i in range(0,4): @@ -149,15 +148,19 @@ def write_nfc_loop(data): print(content_slice) pn532.ntag2xx_write_block(page, content_slice) - print('Write done') + sys.stdout.write('Write done\r') set_neopixel_color(0,255,0) 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() if rw == 'w': while True: - data = input('ID:') + sys.stdout.write('ID: \r') + data = sys.stdin.readline().strip() write_nfc_loop(data) elif rw == 'r': while True: @@ -166,24 +169,24 @@ def loop(): while True: read_nfc_loop() else: - print('Invalid option') + sys.stdout.write('Invalid option\r') poll_obj = select.poll() poll_obj.register(sys.stdin, select.POLLIN) set_neopixel_color(0,0,255) -try: - while True: - poll_results = poll_obj.poll(1) - if poll_results: - while True: - try: - ret = loop() - except: - set_neopixel_color(0,0,255) - break - else: - continue -except: - set_neopixel_color(0,0,0) \ No newline at end of file + +while True: + poll_results = poll_obj.poll(1) + if poll_results: + while True: + try: + ret = loop() + except: + set_neopixel_color(0,0,255) + break + else: + continue +#except: +# set_neopixel_color(0,0,0) \ No newline at end of file