So, after considerable effort I was able to turn the Pimoroni RGB keypad into a wireless macro keypad using MicroPython. I didn’t want to use CircuitPython because it is my understanding that only it supports usb_hid and I wasn’t sure if you could do usb_hid over wifi. It took considerable effort because my Python skills would best be described as beginner level, so, my “solution” is really just a crude proof-of-concept which has many holes and lots of room for improvement, for example, using a simple web server rather than Flask. I can’t even remember why I ended up using Flask. With all that said, this little hack works really well.
I’ve included the code below and here is a summary of my hack solution:
Two scripts - one on the Pico and one on the Mac
The Pico script establishes the WLAN connection, creates the keypad then sets a trigger on each of the keys associated with the “macro” which is actually not a macro, rather, an os.system call on the Mac which launches and/or brings the target app to the foreground. The communication between the Pico and the Mac is done via urequests.get.
On the Mac, I used Flask to receive and process the path value which is the string representing the name of the app to be launched and/or brought to foreground and returns a success or fail in the response to the Pico. I turned up the Flask server with: flask run -h 0.0.0.0 -p 8000
The script on the Pico:
from rgbkeypad import RGBKeypad
from random import randint, uniform
from time import sleep
from machine import Pin
import network
import urequests
import secrets
#create onboard LED
led = Pin("LED")
#establish WLAN connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID, secrets.PASSWORD)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Manage connection errors
if wlan.status() != 3:
raise RuntimeError('Network Connection has failed')
else:
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
led.off()
led.on()
#create the keypad object and set default brightness
keypad = RGBKeypad()
keypad.brightness = .5
#just a little startup lightshow
def lightshow():
for x in range(4):
for y in range(4):
key = keypad[x,y]
key.brightness = uniform(.1,1)
key.color = (randint(0,255), randint(0,255), randint(0,255))
sleep(.1)
keypad.clear()
lightshow()
#create, light and associate keys with specific apps on Mac
key1 = keypad[0,0]
key1.color = (0,255,0)
key1.app = 'TraderWorkstation'
key2 = keypad[3,3]
key2.color = (255,0,0)
key2.app = 'Telegram.app'
key3 = keypad[0,3]
key3.color = (0,0,255)
key3.app = 'GoogleChrome.app'
key4 = keypad[3,0]
key4.color = (0,0,100)
key4.app = 'Thonny.app'
#create the infinite loop and trigger for each key/app
while True:
if key1.is_pressed():
r = urequests.get('http://192.168.4.57:8000/'+ key1.app)
print(r.content)
r.close()
if key2.is_pressed():
r = urequests.get('http://192.168.4.57:8000/' + key2.app)
print(r.content)
r.close()
if key3.is_pressed():
r = urequests.get('http://192.168.4.57:8000/' + key3.app)
print(r.content)
r.close()
if key4.is_pressed():
r = urequests.get('http://192.168.4.57:8000/' + key4.app)
print(r.content)
r.close()
The script on the Mac:
from flask import Flask, request
import os
app = Flask(__name__)
@app.route("/", defaults={'path': ''})
@app.route("/<path:path>")
def (path):
app = path
r = os.system("open /Applications/" + app)
if r == 0:
return "<p>Success with code: %s</p>" % r
else:
return "<p>Fail with code: %s</p>" % r
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)