diff --git a/micropizza/main.py b/micropizza/main.py index edf01b0..cc568cb 100644 --- a/micropizza/main.py +++ b/micropizza/main.py @@ -2,6 +2,8 @@ from machine import PWM, UART, Pin, Timer from micropython import const import time +_DEBUG = const(1) + _PWM_MAX = const(65535) _PWM_FREQ = const(2000) @@ -39,8 +41,7 @@ CMD_UI = 'U' # user interaction CMD_RC = 'C' # recording CMD_RW = 'R' # rewind -LED = PWM(Pin(25)) -LED.freq(_PWM_FREQ) +LED = Pin(25, Pin.OUT) class Scroll: def __init__(self, cnt_pin:Pin, end_pin:Pin, en_pin:PWM, a_pin:Pin, b_pin:Pin) -> None: @@ -128,15 +129,18 @@ def motor_hor(*opts): def _motorctrl(scroll: Scroll, steps: int, *opts): end = scroll.c + steps print(f'motorctrl, end={end}, scroll.c={scroll.c}') - #while True: - # err = end - scroll.c - # if abs(err) > 4: - # speed = (err / (2 * steps)) + 0.5 - # scroll.speed = int(100 * speed) - # else: - # scroll.speed = 0 - # break - # time.sleep_ms(5) + try: + while True: + err = end - scroll.c + if abs(err) > 4: + speed = (err / (2 * steps)) + 0.5 + scroll.speed = int(100 * speed) + else: + scroll.speed = 0 + break + time.sleep_ms(5) + finally: + scroll.speed = 0 def backlight(*opts): _fade_led(pwm_bl, *opts) @@ -226,9 +230,20 @@ def parseCmd(cmd_str: str): parms = [] if len(parm_str) != 0: parms = [int(x) for x in parm_str.split('+')] + if _DEBUG: + print(f'parsed command: {cmd}:{parms}') resp = choice_map[cmd](*parms) uart0.write(f'{resp}\n'.encode('utf-8')) +def flash_led(times: int = 1, on_ms: int = 200, off_ms: int = 500): + LED.off() + times = 1 if times < 1 else times + for i in range(times): + LED.on() + time.sleep_ms(on_ms) + LED.off() + time.sleep_ms(off_ms) + if __name__ == '__main__': pin_bl_a.value(1) @@ -236,6 +251,7 @@ if __name__ == '__main__': pin_fl_a.value(1) pin_fl_b.value(0) rxData = '' + flash_led(2) while True: rxBuf = uart0.read(1) if rxBuf is not None: