import supply_voltage #导入它，开启电量指示呼吸灯功能
import sensor
import motor
import controller
import tool
import machine
import os

shoot_timer = None
shoot_mod = None
lift_mod = None

auto_mode_executed = False


shoot_timer = 0
shoot_mod = 0
lift_mod = 0
触碰1 = sensor.limit_switch('A')
投射 = motor.smart_motor(3, direction=True)
投射.set_soft_break(True)
吸轮 = motor.smart_motor(4, direction=False)
吸轮.set_soft_break(True)
左 = motor.smart_motor(2, direction=True)
左.set_soft_break(True)
右 = motor.smart_motor(1, direction=False)
右.set_soft_break(True)
#读取本地的自动模式已运行标识
try:
    with open("auto.flag", "r") as f:
        auto_mode_executed = True
    os.remove("auto.flag")
except OSError:
    pass
#停止自动模式程序。通过重启停止，停止前在本地保存标识
def stop_auto_mode(a):
    with open("auto.flag", "w") as f:
        f.write("1")
    machine.reset()
#主程序
while True:
    # 自动模式
    if controller.get_button(controller.UP) and not auto_mode_executed:
        __tim = machine.Timer(-1)  #通过定时器自动重启以停止自动模式程序
        __tim.init(period=1*5000, mode=machine.Timer.ONE_SHOT, callback=stop_auto_mode)
        auto_mode_executed = True


    # 手动模式
    左.openloop_turn(controller.get_axis(controller.LEFT_Y))
    右.openloop_turn(controller.get_axis(controller.RIGHT_Y))
    if controller.get_button(controller.R1):
        shoot_mod = 1
        if 触碰1.get_state():
            吸轮.openloop_turn(100)
        else:
            吸轮.openloop_turn(0)
    elif controller.get_button(controller.A):
        吸轮.openloop_turn((-100))
    else:
        吸轮.openloop_turn(0)
    if controller.get_button(controller.R2):
        shoot_mod = 1
        shoot_timer = 1
        投射.openloop_turn(90)
    elif controller.get_button(controller.P):
        shoot_timer = 0
        shoot_mod = 0
        lift_mod = 0
    elif shoot_mod == 1:
        if 触碰1.get_state():
            投射.openloop_turn(5)
        else:
            投射.openloop_turn(60)
    else:
        shoot_timer = shoot_timer + 1
        if shoot_timer < 500:
            投射.openloop_turn((-1))
        else:
            投射.openloop_turn(0)
    tool.my_print(触碰1.get_state())
