Python Forum
Getting button pressed number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting button pressed number
#1
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

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
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

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
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?
Reply
#2
Probably something like
ser.write(str(xcoord).encode())
Reply
#3
(Dec-13-2020, 05:01 PM)bowlofred Wrote: Probably something like
ser.write(str(xcoord).encode())

Yes. Almost there.
I try with print first and worked.
I need both x and y
so I try

1
print(str(xcoord).encode(),str(ycoord).encode())
And worked too.
Then I try

1
ser.write(str(xcoord).encode(),str(ycoord).encode())
And I get


1
2
3
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
Reply
#4
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.

1
2
3
4
5
6
# 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())
Reply
#5
Thank you.

That worked!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How many times was the button pressed in pyglet rama27 0 2,370 Oct-10-2020, 10:26 AM
Last Post: rama27
  run different functions each time the same button is pressed? User3000 6 4,487 Jul-31-2020, 11:11 PM
Last Post: User3000
  Terminate a process when hotkey is pressed 66Gramms 0 2,913 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  Advance program when certain keys are pressed? Chrislw324 2 2,908 May-19-2019, 07:13 PM
Last Post: woooee
  Count to movement according to the time pressed button noartist 1 3,202 Feb-27-2019, 01:33 PM
Last Post: noartist
  What key pressed? ian 2 6,129 Jul-29-2018, 02:30 AM
Last Post: snippsat
  Can't edit code after I've pressed enter. xBlackHeartx 2 15,902 Sep-02-2017, 10:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020