import _thread
import settings
from machine import Pin
from time import sleep, sleep_ms
default_json = {"aantal":5,"on_time": 100,"off_time": 900}
LED_PIN = 2
BUTTON_PIN = 26
button = Pin(BUTTON_PIN , Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)
def update_settings(cmd, params, sep="/"):
if sep not in cmd: # check if separator present
print("error: command separator missing")
else:
key, val = cmd.split(sep)
if not val.isdigit():
print("error: no numeric value in command")
return params
if key in params.keys(): # check if key exists
my_dict = {} # create dict from command
my_dict[key] = int(val)
params.update(my_dict) # update dict with command
settings.write_jsonfile(params)
else:
print("error: no valid key")
return params
def led_blink(params):
for _ in range(params["aantal"]):
led.value(True)
sleep_ms(params["on_time"])
led.value(False)
sleep_ms(params["off_time"])
# listen for terminal-commands
def wait_for_commands():
global dict_params
while True:
command = input("cmd >")
print("cmd = ", command)
dict_params = update_settings(command, dict_params)
_thread.start_new_thread(wait_for_commands, ())
# bij het opstarten
button_before = 1
dict_params = settings.get_params(default_json)
while True:
button_now = button.value()
if button_now != button_before:
button_before = button_now
if button_now == 0:
led_blink(dict_params)
sleep(0.005)
sleep(0.001) # voor thread