This commit is contained in:
Benjamin Tayehanpour 2026-09-26 20:29:12 +00:00
parent b0b4012973
commit 6e3bb75aeb
4 changed files with 47 additions and 8 deletions

12
button.py Normal file
View file

@ -0,0 +1,12 @@
import asyncio
import asyncpio
class button:
def __init__(self, pi, gpio, callback):
self.pi = pi
self.gpio = gpio
self.callback = callback
async def start(self):
await self.pi.set_mode(self.gpio, asyncpio.INPUT)
await self.pi.set_pull_up_down(self.gpio, asyncpio.PUD_UP)
self.cb = await self.pi.callback(self.gpio, asyncpio.EITHER_EDGE, self.callback)

View file

@ -1,18 +1,23 @@
import asyncio
from LCD import LCD from LCD import LCD
class display: class display:
def __init__(self, width=16, height=4, i2c_addr=0x27): async def __init__(self, width=20, height=4, i2c_addr=0x27):
self.width = width self.width = width
self.height = height self.height = height
self.device = LCD(2, i2c_addr, True) self.device = LCD(2, i2c_addr, True)
self.contents = [""] * self.height self.contents = [""] * self.height
def redraw(self): async def redraw(self, row = None):
for row, line in enumerate(self.contents): if row == None:
self.device.message(row + 1, line) for row, line in enumerate(self.contents):
await self.device.async_message(row + 1, line)
else:
await self.device.async_message(row + 1, line)
def read(self): async def read(self, row):
pass return self.contents[row]
def write(self): async def write(self, row, data, offset = 0):
pass self.contents[row] = f'{self.contents[row][:offset]}{data}{self.contents[row][offset+len(data):]}'
self.redraw(row)

View file

@ -1,4 +1,26 @@
import asyncio import asyncio
from button import button
from decoder import decoder from decoder import decoder
from display import display from display import display
async def wheel_handler(dir):
print(f"wheel: {dir}")
async def button_test(**args):
print(args)
async def main():
pi = asyncpio.pi()
dec = decoder(pi, 27, 17, wheel_handler)
b1 = button(pi, 23, button_test)
b2 = button(pi, 24, button_test)
await pi.connect()
await dec.start()
await b1.start()
await b2.start()
if __name__ == "__main__":
disp = display()
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()