# Display Image & text on I2C driven ssd1306 OLED display
from MIDI import Midi
from machine import Pin
import time
import uasyncio
# Changeable Settings: ######
m = [Midi("CCTOGGLE", [0], [10], [127]), Midi("CCTOGGLE", [0], [10], [127]), Midi("CCTOGGLE", [0], [10], [127])]
l = [Pin(26, Pin.OUT), Pin(27, Pin.OUT), Pin(28, Pin.OUT)]
b = [Pin(22, Pin.IN, Pin.PULL_UP), Pin(21, Pin.IN, Pin.PULL_UP), Pin(20, Pin.IN, Pin.PULL_UP)]
bToggle = [True, True, True]
bLatch = [False, True, False]
#############################
for i in l:
i.value(1)
# Raspberry Pi logo as 32x32 bytearray
#buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
bPressed = [False, False, False]
bReleased = [False, False, False]
sleepTime = 50
async def checkButtonPress(i):
while True:
bPressed[i] = False
prevVal = b[i].value()
while (b[i].value() == 1) or (b[i].value() == prevVal):
prevVal = b[i].value()
await uasyncio.sleep_ms(sleepTime)
bPressed[i] = True
await uasyncio.sleep_ms(sleepTime)
async def checkButtonRelease(i):
while True:
bReleased[i] = False
prevVal = b[i].value()
while (b[i].value() == 0) or (b[i].value() == prevVal):
prevVal = b[i].value()
await uasyncio.sleep_ms(sleepTime)
bReleased[i] = True
await uasyncio.sleep_ms(sleepTime)
async def checkMidi():
while True:
for i in range(len(b)):
if bPressed[i]:
await m[i].sendMidi()
elif bReleased[i] and not bLatch[i]:
await m[i].sendMidi()
await uasyncio.sleep_ms(sleepTime)
async def checkLed():
while True:
for i in range(len(b)):
if bPressed[i] and bLatch[i]:
l[i].toggle()
elif bPressed[i] and not bLatch[i]:
l[i].value(0)
elif bReleased[i] and not bLatch[i]:
l[i].value(1)
await uasyncio.sleep_ms(sleepTime)
async def run():
for i in range(len(b)):
uasyncio.create_task(checkButtonPress(i))
uasyncio.create_task(checkButtonRelease(i))
uasyncio.create_task(checkLed())
uasyncio.create_task(checkMidi())
while True:
await uasyncio.sleep_ms(10)
uasyncio.run(run())