Add Adafuit Macropad serial connection article

This commit is contained in:
Yorick Barbanneau 2023-02-26 19:09:52 +01:00
parent 4345e877fa
commit eff6e3b6e5
7 changed files with 627 additions and 0 deletions

View file

@ -0,0 +1,45 @@
from adafruit_macropad import MacroPad
import usb_cdc
import time
macropad = MacroPad()
serial = usb_cdc.data
def exec_command (data):
global muted
try:
print(data)
command,option = data.split()
except:
command = ""
option = ""
print("cmd: {} | opt: {}".format(command, option))
if command == 'mute':
muted = True if option == 'yes' else False
print('muted: {}'.format(muted))
timer=2
in_data=bytearray()
muted=False
while True:
key_event = macropad.keys.events.get()
if muted:
macropad.pixels[1] = (255,0,0)
else:
macropad.pixels[1] = (0,255,0)
if key_event:
if key_event.pressed:
if key_event.key_number == 1:
print('get key 1')
serial.write(bytearray('mute\r\n'))
if serial.in_waiting > 0:
while(serial.in_waiting > 0):
byte = serial.read(1)
if byte == b'\r':
exec_command(in_data.decode("utf-8"))
in_data = bytearray()
else:
in_data += byte

View file

@ -0,0 +1,34 @@
#!/usr/bin/env python
import serial
import subprocess
ser = serial.Serial(port='/dev/ttyACM1')
ser.flushInput()
print('Begin loop')
while True:
line = ser.readline()
try:
command = (line.decode()).split()
print('command received: {}'.format(command[0]))
except:
print('no valid command received')
command = ""
try:
if command[0] == "mute":
# First subprocess for toggle mote the microphone
subprocess.run(
['pactl', 'set-source-mute', '@DEFAULT_SOURCE@', 'toggle'],
)
# Second one for check the states of microphone
result = subprocess.run(
['pactl', 'get-source-mute', '@DEFAULT_SOURCE@'],
capture_output=True
)
message = "mute {}\r".format(result.stdout.split()[1].decode())
ser.write(message.encode())
print('command sent: {}'.format(message))
except Error as e:
print('Error in command: {}'.format(e))