implemented test suite
This commit is contained in:
parent
67a53cbb08
commit
da47717400
1 changed files with 122 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
from machine import UART, Pin, Timer
|
from machine import UART, Pin, PWM, Timer
|
||||||
import time
|
import time
|
||||||
|
|
||||||
led = Pin(25, Pin.OUT)
|
led = Pin(25, Pin.OUT)
|
||||||
|
@ -8,6 +8,113 @@ btn_red = Pin(3, Pin.IN, Pin.PULL_UP)
|
||||||
led_blu = Pin(4, Pin.OUT)
|
led_blu = Pin(4, Pin.OUT)
|
||||||
led_red = Pin(2, Pin.OUT)
|
led_red = Pin(2, Pin.OUT)
|
||||||
|
|
||||||
|
_PWM_MAX = const(65535)
|
||||||
|
_PWM_FREQ = const(2000)
|
||||||
|
|
||||||
|
_PIN_EN_MOTOR_VERT = const(14)
|
||||||
|
_PIN_A_MOTOR_VERT = const(12)
|
||||||
|
_PIN_B_MOTOR_VERT = const(13)
|
||||||
|
|
||||||
|
_PIN_EN_MOTOR_HOR = const(15)
|
||||||
|
_PIN_A_MOTOR_HOR = const(10)
|
||||||
|
_PIN_B_MOTOR_HOR = const(11)
|
||||||
|
|
||||||
|
_PIN_EN_BACKLIGHT = const(17)
|
||||||
|
_PIN_A_BACKLIGHT = const(18)
|
||||||
|
_PIN_B_BACKLIGHT = const(19)
|
||||||
|
_PIN_EN_FRONTLIGHT = const(16)
|
||||||
|
_PIN_A_FRONTLIGHT = const(20)
|
||||||
|
_PIN_B_FRONTLIGHT = const(21)
|
||||||
|
|
||||||
|
_PIN_LED_RED_BTN = const(2)
|
||||||
|
_PIN_LED_BLU_BTN = const(4)
|
||||||
|
|
||||||
|
_PIN_RED_BTN = const(3)
|
||||||
|
_PIN_BLU_BTN = const(5)
|
||||||
|
|
||||||
|
_PIN_SENS_END_VERT = const(9)
|
||||||
|
_PIN_SENS_CNT_VERT = const(7)
|
||||||
|
_PIN_SENS_END_HOR = const(6)
|
||||||
|
_PIN_SENS_CNT_HOR = const(8)
|
||||||
|
|
||||||
|
CMD_MOT_V = 'V' # run vertical motor
|
||||||
|
CMD_MOT_H = 'H' # run horizontal motor
|
||||||
|
CMD_BL = 'B' # backlight
|
||||||
|
CMD_FL = 'F' # frontlight
|
||||||
|
CMD_UI = 'U' # user interaction
|
||||||
|
CMD_RC = 'C' # recording
|
||||||
|
CMD_RW = 'R' # rewind
|
||||||
|
|
||||||
|
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:
|
||||||
|
self.c = 0
|
||||||
|
self._dir = 0
|
||||||
|
self._speed = 0
|
||||||
|
self.en_pin = en_pin
|
||||||
|
self.a_pin = a_pin
|
||||||
|
self.b_pin = b_pin
|
||||||
|
self.c_pin = cnt_pin
|
||||||
|
self.c_pin.irq(self._count, Pin.IRQ_RISING | Pin.IRQ_FALLING)
|
||||||
|
self.e_pin = end_pin
|
||||||
|
self.e_pin.irq(self._stop, Pin.IRQ_RISING)
|
||||||
|
|
||||||
|
def _count(self, pin):
|
||||||
|
self.c += self._dir
|
||||||
|
|
||||||
|
def _stop(self, pin):
|
||||||
|
self.speed = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dir(self):
|
||||||
|
return self._dir
|
||||||
|
|
||||||
|
@dir.setter
|
||||||
|
def dir(self, d):
|
||||||
|
self.a_pin.value(1 if d > 0 else 0)
|
||||||
|
self.b_pin.value(1 if d < 0 else 0)
|
||||||
|
self._dir = d
|
||||||
|
|
||||||
|
@property
|
||||||
|
def speed(self):
|
||||||
|
return self._speed * self._dir
|
||||||
|
|
||||||
|
@speed.setter
|
||||||
|
def speed(self, speed):
|
||||||
|
self.en_pin.duty_u16(abs(speed) * (_PWM_MAX // 100))
|
||||||
|
self.dir = (1 if speed > 0 else
|
||||||
|
(-1 if speed < 0 else 0))
|
||||||
|
self._speed = speed
|
||||||
|
|
||||||
|
pwm_bl = PWM(Pin(_PIN_EN_BACKLIGHT))
|
||||||
|
pwm_bl.freq(_PWM_FREQ)
|
||||||
|
pwm_fl = PWM(Pin(_PIN_EN_FRONTLIGHT))
|
||||||
|
pwm_fl.freq(_PWM_FREQ)
|
||||||
|
pwm_mv = PWM(Pin(_PIN_EN_MOTOR_VERT))
|
||||||
|
pwm_mv.freq(_PWM_FREQ)
|
||||||
|
pwm_mh = PWM(Pin(_PIN_EN_MOTOR_HOR))
|
||||||
|
pwm_mh.freq(_PWM_FREQ)
|
||||||
|
|
||||||
|
pin_mv_a = Pin(_PIN_A_MOTOR_VERT, Pin.OUT)
|
||||||
|
pin_mv_b = Pin(_PIN_B_MOTOR_VERT, Pin.OUT)
|
||||||
|
pin_mh_a = Pin(_PIN_A_MOTOR_HOR, Pin.OUT)
|
||||||
|
pin_mh_b = Pin(_PIN_B_MOTOR_HOR, Pin.OUT)
|
||||||
|
|
||||||
|
pin_bl_a = Pin(_PIN_A_BACKLIGHT, Pin.OUT)
|
||||||
|
pin_bl_b = Pin(_PIN_B_BACKLIGHT, Pin.OUT)
|
||||||
|
pin_fl_a = Pin(_PIN_A_FRONTLIGHT, Pin.OUT)
|
||||||
|
pin_fl_b = Pin(_PIN_B_FRONTLIGHT, Pin.OUT)
|
||||||
|
|
||||||
|
cnt_v = Pin(_PIN_SENS_CNT_VERT, Pin.IN)
|
||||||
|
cnt_h = Pin(_PIN_SENS_CNT_HOR, Pin.IN)
|
||||||
|
end_v = Pin(_PIN_SENS_END_VERT, Pin.IN)
|
||||||
|
end_h = Pin(_PIN_SENS_END_HOR, Pin.IN)
|
||||||
|
|
||||||
|
scroll_h = Scroll(cnt_h, end_h, pwm_mh, pin_mh_a, pin_mh_b)
|
||||||
|
scroll_v = Scroll(cnt_v, end_v, pwm_mv, pin_mv_a, pin_mv_b)
|
||||||
|
|
||||||
|
|
||||||
def user_interaction(timeout):
|
def user_interaction(timeout):
|
||||||
def _toggle_leds(timer):
|
def _toggle_leds(timer):
|
||||||
led_blu.toggle()
|
led_blu.toggle()
|
||||||
|
@ -40,9 +147,7 @@ def recording(timeout):
|
||||||
time.sleep_ms(50)
|
time.sleep_ms(50)
|
||||||
return 'OK'
|
return 'OK'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def uart_comm():
|
||||||
#print(user_interaction(20))
|
|
||||||
#Print
|
|
||||||
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
|
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
|
||||||
rxData = ''
|
rxData = ''
|
||||||
while True:
|
while True:
|
||||||
|
@ -51,4 +156,16 @@ if __name__ == '__main__':
|
||||||
rxData += rxBuf.decode('utf-8')
|
rxData += rxBuf.decode('utf-8')
|
||||||
if rxData.endswith('\n'):
|
if rxData.endswith('\n'):
|
||||||
print(rxData.strip())
|
print(rxData.strip())
|
||||||
rxData = ''
|
uart0.write(b'OK\n')
|
||||||
|
rxData = ''
|
||||||
|
|
||||||
|
def test_sensors():
|
||||||
|
for i in range(4):
|
||||||
|
pin = Pin(i+6, Pin.IN)
|
||||||
|
print(f'pin {i+6}: {pin.value()}')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
#print(user_interaction(20))
|
||||||
|
#Print
|
||||||
|
#test_sensors()
|
||||||
|
uart_comm()
|
||||||
|
|
Loading…
Reference in a new issue