Add Adafuit Macropad serial connection article
This commit is contained in:
parent
4345e877fa
commit
eff6e3b6e5
7 changed files with 627 additions and 0 deletions
|
@ -0,0 +1,47 @@
|
|||
from adafruit_macropad import MacroPad
|
||||
import usb_cdc
|
||||
import time
|
||||
|
||||
macropad = MacroPad()
|
||||
serial = usb_cdc.data
|
||||
|
||||
def exec_command (data):
|
||||
global timer
|
||||
try:
|
||||
command,option = data.split()
|
||||
except:
|
||||
command = ""
|
||||
option = ""
|
||||
print("cmd: {} | opt: {}".format(command, option))
|
||||
if command == 'time':
|
||||
timer = float(option)
|
||||
print("new timer: {}".format(timer))
|
||||
response = "nouveau timer : {}\r\n".format(option)
|
||||
buffer = bytearray(response)
|
||||
serial.write(buffer)
|
||||
|
||||
def blink(led, light):
|
||||
print('light: {}'.format(light))
|
||||
if light:
|
||||
macropad.pixels[led] = (33, 45, 230)
|
||||
else:
|
||||
macropad.pixels[led] = (0, 0, 0)
|
||||
print('c: {}'.format(macropad.pixels[led]))
|
||||
return False if light else True
|
||||
|
||||
timer=2
|
||||
light=False
|
||||
in_data=bytearray()
|
||||
|
||||
while True:
|
||||
light = blink(1, light)
|
||||
time.sleep(timer)
|
||||
|
||||
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
|
|
@ -0,0 +1,5 @@
|
|||
import usb_cdc
|
||||
try:
|
||||
usb_cdc.enable(console=True, data=True) # Enable console and data# Write your code here :-)
|
||||
except Exception as e:
|
||||
print(e)
|
|
@ -0,0 +1,44 @@
|
|||
from adafruit_macropad import MacroPad
|
||||
import usb_cdc
|
||||
import time
|
||||
|
||||
macropad = MacroPad()
|
||||
serial = usb_cdc.data
|
||||
|
||||
def exec_command (data):
|
||||
global timer
|
||||
try:
|
||||
command,option = data.split()
|
||||
except:
|
||||
command = ""
|
||||
option = ""
|
||||
print("cmd: {} | opt: {}".format(command, option))
|
||||
if command == 'time':
|
||||
timer = float(option)
|
||||
print("new timer: {}".format(timer))
|
||||
|
||||
def blink(led, light):
|
||||
print('light: {}'.format(light))
|
||||
if light:
|
||||
macropad.pixels[led] = (33, 45, 230)
|
||||
else:
|
||||
macropad.pixels[led] = (0, 0, 0)
|
||||
print('c: {}'.format(macropad.pixels[led]))
|
||||
return False if light else True
|
||||
|
||||
timer=2
|
||||
light=False
|
||||
in_data=bytearray()
|
||||
|
||||
while True:
|
||||
light = blink(1, light)
|
||||
time.sleep(timer)
|
||||
|
||||
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
|
|
@ -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
|
|
@ -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))
|
Loading…
Add table
Add a link
Reference in a new issue