MIT License (MIT)
Copyright (c) 2021 Junliang Yan
https://opensource.org/licenses/MIT
example for MicroPython rotary encoder
- uasyncio implementation
https://github.com/MikeTeachman/micro...
https://github.com/redhead-p/pico_dcc
shopee oled, encoder, buttons to control dcc
# rp2040 pololu max 14870
# gpio 0 -- dir
gpio 1 -- pwm
# rp2040 shopee oled, encoder, buttons
# gpio 2 -- encoder b
gpio 3 -- encdoer a
gpio 4 -- oled sda pulled up 4.7k to 3.3v
gpio 5 -- oled scl pulled up 4.7k tp 3.3v
gpio 6 -- confirm button
# don't forget to power the oled, with 3.3v
import sys
if sys.platform == 'esp8266' or sys.platform == 'esp32':
from rotary_irq_esp import RotaryIRQ
elif sys.platform == 'pyboard':
from rotary_irq_pyb import RotaryIRQ
elif sys.platform == 'rp2':
from rotary_irq_rp2 import RotaryIRQ
else:
print('Warning: The Rotary module has not been tested on this platform')
import uasyncio as asyncio
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
from dcc import DCCGen
button=Pin(6,Pin.IN) #button already pulled-up 10k to 3.3v
dcc=DCCGen(0,1,0)
dcc.power(1)
led=Pin(13,Pin.OUT)
i2c=I2C(sda=Pin(4),scl=Pin(5))
oled=SSD1306_I2C(128,32,i2c)
r=RotaryIRQ(pin_num_clk=2,pin_num_dt=3)
dir=1
address=17
speed=0
Use heartbeat to keep event loop not empty
async def heartbeat():
while True:
led.toggle()
await asyncio.sleep_ms(100)
event = asyncio.Event()
def callback():
event.set()
def button_handler(pin):
"""
when confirm button is pressed
stop the current train
change address
zero the value of the rotary encoder
show on the oled current values
"""
global address
global speed
global r
speed=0
if address == 17:
dcc.set_speed(address,dir,speed)
address=14
else:
dcc.set_speed(address,dir,speed)
address=17
r.set(0)
oled.fill(0)
oled.show()
oled.text("address changed",0,0)
oled.text(f"address: {address}",0,10)
oled.show()
async def main():
r.add_listener(callback)
asyncio.create_task(heartbeat())
button.irq(trigger=Pin.IRQ_FALLING,handler=button_handler)
while True:
await event.wait()
oled.fill(0)
oled.show()
if r.value() MORE_THAN_OR_EQUAL 0: dir = 1
else: dir = -1
speed=abs(r.value())
oled.text(f" speed: {r.value()}",0,0)
oled.text(f"address: {address}",0,8)
oled.text("press confirm to",0,16)
oled.text("change train",0,24)
oled.show()
dcc.set_speed(address,dir,speed)
event.clear()
try:
asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
print('Exception {} {}\n'.format(type(e).__name__, e))
finally:
ret = asyncio.new_event_loop() # Clear retained uasyncio state
Информация по комментариям в разработке