added debugging helpers

This commit is contained in:
jpunkt 2021-11-10 13:38:25 +01:00
parent 761e0f6c8d
commit 08d79cdcb8

View file

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