import io
import json
from machine import Pin, soft_reset
import time
led_red = Pin(18, Pin.OUT, value=1) # set LED pin to high voltage to turn off the LED indicator.
led_green = Pin(19, Pin.OUT, value=1)
led_blue = Pin(20, Pin.OUT, value=1)
btn_pin = Pin(2, Pin.OUT, value=1) # button pin high level means released button state. Low level means pressed state.
config = {
'device_name': 'fingerbot 1',
'click_press_time': 0.2,
'led_blink_time': 0.1,
}
default_config = config.copy()
last_error_msg = None
HELP_INFO = '''Supported commands:
click_btn - command to switch the Fingerbot actuator position.
get device_name - read current device name.
set device_name <new_name> - set new device_name config value.
set click_press_time <time_in_sec> - modify action button press time (in seconds).
get last_error_msg - show last reported error message (it is None after reboot).
get config - show config.json content.
reset_config - reset config to default values.
reboot - reboot device.
machine.soft_reset() - same as reboot. This is an alias that is identical to same REPL command.
repl - stop program and exit to MicroPython Interactive Interpreter (aka REPL mode).
help or help() - show this message again. '''
def main_loop():
print(f'Fingerbot Serial API main_loop started.\n{HELP_INFO}')
led_red(1) # turn off the red LED light that could be turned on by 'break' command
while True:
try:
cmd = input().strip()
if cmd == 'click_btn':
click_btn()
elif cmd == 'reboot' or cmd == 'machine.soft_reset()':
reboot()
elif cmd == 'reset_config':
reset_config()
elif cmd == 'get config':
report_msg(json.dumps(config))
elif cmd == 'get last_error_msg':
report_msg(last_error_msg)
elif cmd.startswith('get '):
get_config_param(cmd[4:])
elif cmd.startswith('set '):
args = cmd[4:].split(' ', 1)
set_config_param(args[0], args[1])
elif cmd == 'repl':
report_msg('Stop program and exit to MicroPython Interactive Interpreter (aka REPL mode).')
led_red(0)
break
elif cmd == 'help' or cmd == 'help()':
report_msg(HELP_INFO)
elif cmd:
report_error(f'Unsupported command "{cmd}".\n{HELP_INFO}')
except Exception as e:
print(str(e))
except KeyboardInterrupt:
print('KeyboardInterrupt received. Exit from the Fingerbot Serial API main_loop. ' +
'Type "main_loop()" command to start it again from the REPL terminal.')
break
def click_btn():
print('Do button click action... ', end='')
btn_pin(0)
led_blue(0)
time.sleep(config.get('click_press_time'))
btn_pin(1)
led_blue(1)
report_msg('Done.')
def get_config_param(param_name: str):
if param_name in ['device_name', 'click_press_time', 'led_blink_time']:
report_msg(config.get(param_name))
else:
report_error(f'Unknown parameter "{param_name}". ' +
f'Known parameters: device_name, click_press_time, led_blink_time.')
def set_config_param(param_name: str, value: str):
if param_name in ['device_name', 'click_press_time', 'led_blink_time']:
if param_name in ['click_press_time', 'led_blink_time']:
config[param_name] = float(value)
else:
config[param_name] = value
save_config_to_file()
report_msg('Done.')
else:
report_error(f'Unknown parameter "{param_name}". ' +
f'Known parameters: device_name, click_press_time, led_blink_time.')
def report_msg(msg: str | None):
print(msg)
led_green(0)
time.sleep(config.get('led_blink_time'))
led_green(1)
def report_error(msg: str):
print(f'Error: {msg}')
global last_error_msg
last_error_msg = msg
led_red(0)
time.sleep(config.get('led_blink_time'))
led_red(1)
def save_config_to_file():
with io.open('config.json', 'wt') as f:
json.dump(config, f)
def read_config_from_file_if_exist():
try:
with io.open('config.json', 'rt') as f:
global config
config = json.load(f)
except Exception as e:
report_error(str(e))
def reset_config():
config.update(default_config)
save_config_to_file()
def reboot():
soft_reset()
report_msg('Reboot device.')
read_config_from_file_if_exist()
main_loop()