2

I am designing a new time/score keeper for an air hockey table using a PyBoard as a base. My plan is to use a TM1627 (4x7seg) for time display, rotary encoder w/ button to set the time, IR and a couple 7segs for scoring, IR reflector sensors for goallines, and a relay to control the fan.

I'm getting hung up trying to separate the clock into its own thread while focusing on reading the sensors. Figured I could use uasyncio to split everything up nicely, but I can't figure out where to put the directives to spin off a thread for the clock and eventually the sensors.

On execution right now, it appears the rotary encoder is assigned the default value, no timer is started, the encoder doesn't set the time, and the program returns control to REPL rather quickly.

Prior to trying to async everything, I had the rotary encoder and timer working well. Now, not so much.

from rotary_irq_pyb import RotaryIRQ
from machine import Pin
import tm1637
import utime
import uasyncio

async def countdown(cntr):
    # just init min/sec to any int > 0
    min = sec = 99
    enableColon = True        
    while True:
        # update the 4x7seg with the time remaining
        min = abs(int((cntr - utime.time()) / 60))
        sec = (cntr - utime.time()) % 60
        #print(str(), str(sec), sep=':' )
        enableColon = not enableColon  # alternately blink the colon
        tm.numbers(min, sec, colon = enableColon)
        if(min + sec == 0):  # once both reach zero, break  
            break
        await uasyncio.sleep(500)

X1 = pyb.Pin.board.X1
X2 = pyb.Pin.board.X2
Y1 = pyb.Pin.board.Y1
Y2 = pyb.Pin.board.Y2
button = pyb.Pin(pyb.Pin.board.X3, pyb.Pin.IN)

r = RotaryIRQ(pin_num_clk=X1, 
              pin_num_dt=X2, 
              min_val=3, 
              max_val=10, 
              reverse=False, 
              range_mode=RotaryIRQ.RANGE_BOUNDED)

tm = tm1637.TM1637(clk = Y1, dio = Y2)
val_old = val_new = 0

while True: 
    val_new = r.value()
    if(val_old != val_new):
        val_old = val_new
        print(str(val_new))
    if(button.value()):  # save value as minutes
        loop = uasyncio.get_event_loop()
        endTime = utime.time() + (60 * val_new) 
        loop.create_task(countdown(endTime))
        r.close() # Turn off Rotary Encoder
        break

#loop = uasyncio.get_event_loop()
#loop.create_task(countdown(et))
#loop.run_until_complete(countdown(et))

I'm sure it's something simple, but this is the first non-CLI python script I've done, so I'm sure there are a bunch of silly mistakes. Any assistance would be appreciated.

Matt
  • 175
  • 5
  • Since no one has replied, I'll ask if you have also tried the [Pyboard forum at Micropython forums](https://forum.micropython.org/viewforum.php?f=6) – Patrick Mar 24 '20 at 23:45

0 Answers0