![]() |
Getting button pressed number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Getting button pressed number (/thread-31463.html) |
Getting button pressed number - Moris526 - Dec-13-2020 Hi all. Im using Adafruit's Neotrellis led/button board with Python code on Raspberry pi I want to send, via serial, the button pressed to Pc. Im new to python so Im using the code from Adafruit import time from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis from adafruit_neotrellis.multitrellis import MultiTrellis i2c_bus = busio.I2C(SCL, SDA) trelli = [ [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)], [NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31)], ] trellis = MultiTrellis(trelli) OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) def blink(xcoord, ycoord, edge): # turn the LED on when a rising edge is detected if edge == NeoTrellis.EDGE_RISING: trellis.color(xcoord, ycoord, BLUE) # turn the LED off when a rising edge is detected elif edge == NeoTrellis.EDGE_FALLING: trellis.color(xcoord, ycoord, OFF) for y in range(8): for x in range(8): # activate rising edge events on all keys trellis.activate_key(x, y, NeoTrellis.EDGE_RISING) # activate falling edge events on all keys trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING) trellis.set_callback(x, y, blink) trellis.color(x, y, PURPLE) time.sleep(0.05) for y in range(8): for x in range(8): trellis.color(x, y, OFF) time.sleep(0.05) while True: trellis.sync() time.sleep(0.02)I try this def blink(xcoord, ycoord, edge): # turn the LED on when a rising edge is detected if edge == NeoTrellis.EDGE_RISING: ser.write(b'xcoord') # turn the LED off when a rising edge is detected elif edge == NeoTrellis.EDGE_FALLING: trellis.color(xcoord, ycoord, OFF)But i get ¨xcoord¨ printed on PC. I try def blink(xcoord, ycoord, edge): # turn the LED on when a rising edge is detected if edge == NeoTrellis.EDGE_RISING: ser.write(b'x') # turn the LED off when a rising edge is detected elif edge == NeoTrellis.EDGE_FALLING: trellis.color(xcoord, ycoord, OFF)and I get ¨x¨ printed on PC My question: How do I get the button pressed number? RE: Getting button pressed number - bowlofred - Dec-13-2020 Probably something like ser.write(str(xcoord).encode())
RE: Getting button pressed number - Moris526 - Dec-13-2020 (Dec-13-2020, 05:01 PM)bowlofred Wrote: Probably something like Yes. Almost there. I try with print first and worked. I need both x and y so I try print(str(xcoord).encode(),str(ycoord).encode())And worked too. Then I try ser.write(str(xcoord).encode(),str(ycoord).encode())And I get t.callbacks[evt.number](x,y,evt.edge) ser.write(str(xcoord),encode(),str(ycoord).encode()) TypeError: write() takes 2 positional arguments but 3 were given RE: Getting button pressed number - bowlofred - Dec-13-2020 write() normally takes a single object at a time. If you want to write two things, either combine them into a single object, or perform two writes.# two things separated by a space ser.write(f"{xcoord} {ycoord}".encode()) # two consecutive writes (no space separates the data elements) ser.write(str(xcoord).encode()) ser.write(str(ycoord).encode()) RE: Getting button pressed number - Moris526 - Dec-14-2020 Thank you. That worked!! |