#L_19mai - MIT App Inventor actuator
# MIT App Inventor emite comenzile pentru miscare geam /sus/jos
# Sistemul bazat pe  RP2040 Zero este executant
#
import machine, neopixel
from machine import Pin, UART, SPI, PWM, ADC
import time

import tm1637
#from machine import Pin, SPI, PWM, ADC
import _thread
tm = tm1637.TM1637(clk=Pin(29), dio=Pin(28))
#------------HBridge---------
IN1=Pin(0,Pin.OUT)
IN2=Pin(1,Pin.OUT)
pwm = PWM(Pin(2), freq=50, duty_u16=32768)
adc = ADC(Pin(27))     # c
adc.read_u16()         # max 63000
IN1.value(0) # oprit
IN2.value(0) # desactiv
pwm.duty_u16(0) # oprit
pixel_pin = 16
pixel = neopixel.NeoPixel(Pin(pixel_pin), 1)
                    
ser = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5)) # RP2040 Zero
 
while 1:
    c=ser.read(1)
    
    if c == b'1':    # merge sus
        pixel[0] = (100,0, 0)  # red
        pixel.write()
        IN1.value(0) #         sus
        IN2.value(1) # desactiv
        pwm.duty_u16(15000)
        time.sleep(0.3)
        pwm.duty_u16(0)
        pixel[0] = (0,0, 0)  # sterg
        pixel.write()
        
    if c == b'2':     # merge jos
        pixel[0] = (0,100, 0)  # green
        pixel.write()
        IN1.value(1) # jos
        IN2.value(0) # desactiv
        pwm.duty_u16(15000)
        time.sleep(0.3)
        pwm.duty_u16(0)  # opresc
        pixel[0] = (0,0, 0)  # sterg
        pixel.write()
        
    pixel[0] = (0,0, 50)  # blue
    pixel.write()
        
    
