Skip to content

Commit

Permalink
增加断电恢复通道
Browse files Browse the repository at this point in the history
  • Loading branch information
TshineZheng committed Jul 23, 2024
1 parent dca6c94 commit eb5167d
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions asm_pcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import utime
from machine import UART, Pin

from mpy_common.file_util import file_exists

# 串口编号设置
M1_UP = 0
M1_DOWN = 1
Expand Down Expand Up @@ -59,6 +61,23 @@ class ACTION:

latest_state = None

CHANNEL_FILE = 'channel_latest'

def update_latest_channel(channel: int):
with open(CHANNEL_FILE, 'w') as f:
f.write(str(channel))

def get_latest_channel() -> int:
# 如果文件不存在,则返回 0
if not file_exists(CHANNEL_FILE):
return 0

try:
with open(CHANNEL_FILE, 'r') as f:
return int(f.read())
except:
return 0


def calculate_crc(data):
# CRC 校验函数
Expand Down Expand Up @@ -97,7 +116,6 @@ def _motor_control(ch: int, action: int):
else:
uart_motor_control(ch-4, action)


def _motor_set(ch: int, action: int):
for i in range(4):
if i != ch:
Expand All @@ -110,8 +128,7 @@ def _motor_set(ch: int, action: int):
else:
uart_motor_set(ch-4, action)


def motor_triiger(ch: int, action: int):
def motor_triiger(ch: int, action: int, test = False):
global cur_ch
global cur_ch_action

Expand All @@ -122,6 +139,11 @@ def motor_triiger(ch: int, action: int):
cur_ch_action = action
_motor_control(ch, action)

if test:
return

update_latest_channel(ch)


def gpio_init():

Expand Down Expand Up @@ -179,17 +201,21 @@ def logic(onbrokenSwitch):


def test_logic():
# save latest channel
latest_channel = get_latest_channel()
print('latest channel', latest_channel)

print('test logic start')
for ch in [1, 5, 0, 2, 3, 4, 6, 7]:
print(f"channel {ch} testing")
motor_triiger(ch, ACTION.STOP)
motor_triiger(ch, ACTION.STOP, True)
end_time = utime.ticks_ms() + 500
while utime.ticks_ms() < end_time:
logic(None)

global cur_ch
global cur_ch_action
motor_triiger(cur_ch, cur_ch_action)
# restore latest channel
motor_triiger(latest_channel, ACTION.STOP, True)

print('test logic end')


Expand Down

0 comments on commit eb5167d

Please sign in to comment.