Commit 99dfb411 authored by schneider's avatar schneider

fix(preload): Use standard Python module names

Changes use of utime and ujson to time and json
parent d2ba21bb
import ujson
import json
import os
import display
import utime
import time
import buttons
CONFIG_NAME = "at-timestamp.json"
......@@ -11,7 +11,7 @@ def init():
if CONFIG_NAME not in os.listdir("."):
at_config = {"time_start": "unset"}
f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(at_config))
f.write(json.dumps(at_config))
f.close()
if is_timestamp_set():
......@@ -22,7 +22,7 @@ def init():
def is_timestamp_set():
f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read())
c = json.loads(f.read())
f.close()
if c["time_start"] == "unset":
return False
......@@ -40,29 +40,29 @@ def triangle(disp, x, y, left):
def timestamp_reset():
f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read())
c = json.loads(f.read())
c["time_start"] = "unset"
f.close()
f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c))
f.write(json.dumps(c))
f.close()
def timestamp_read():
global time_start
f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read())
c = json.loads(f.read())
time_start = c["time_start"]
f.close()
def timestamp_write():
f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read())
c["time_start"] = utime.time()
c = json.loads(f.read())
c["time_start"] = time.time()
f.close()
f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c))
f.write(json.dumps(c))
f.close()
......@@ -76,7 +76,7 @@ def menu():
triangle(disp, 10, 66, True)
disp.print("start.", posx=15, posy=60, fg=[0, 255, 0])
elif menu_state == 1:
seconds = utime.time() - time_start
seconds = time.time() - time_start
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
disp.print("%02d:%02d:%02d" % (h, m, s), posy=40, fg=[255, 255, 255])
......@@ -122,4 +122,4 @@ while True:
menu_state = 0
menu()
disp.update()
utime.sleep(0.1)
time.sleep(0.1)
# Adapted from https://github.com/muccc/flipdots/blob/master/scripts/clock.py
import display
from utime import sleep
import utime
from time import sleep
import time
import math
import leds
import buttons
import ujson
import json
import os
CONFIG_NAME = "clock.json"
......@@ -100,7 +100,7 @@ class Clock:
def readConfig(self):
with open(CONFIG_NAME, "r") as f:
try:
c = ujson.loads(f.read())
c = json.loads(f.read())
if (
"themes" in c
and len(c["themes"]) > 0
......@@ -114,7 +114,7 @@ class Clock:
def writeConfig(self):
with open(CONFIG_NAME, "w") as f:
f.write(ujson.dumps({"theme": self.theme, "themes": self.themes}))
f.write(json.dumps({"theme": self.theme, "themes": self.themes}))
def setTheme(self, theme):
self.theme = theme % len(self.themes)
......@@ -159,7 +159,7 @@ class Clock:
try:
with display.open() as disp:
while True:
localtime = utime.localtime()
localtime = time.localtime()
self.updateClock(disp, localtime)
if self.run_once:
break
......@@ -179,7 +179,7 @@ class Clock:
elif button_pressed and v & buttons.TOP_RIGHT != 0:
self.setTime(disp, localtime)
utime.sleep_ms(23)
time.sleep_ms(23)
except KeyboardInterrupt:
for i in range(11):
......@@ -255,7 +255,7 @@ class Clock:
accepted = False
previously_pressed_button = buttons.TOP_RIGHT
button_repeat_counter = 0
set_seconds = utime.mktime(localtime)
set_seconds = time.mktime(localtime)
while not accepted:
v = buttons.read(
......@@ -288,11 +288,11 @@ class Clock:
elif previously_pressed_button == buttons.BOTTOM_RIGHT:
set_seconds += seconds_change
self.updateClock(disp, utime.localtime(set_seconds))
utime.sleep_ms(23)
self.updateClock(disp, time.localtime(set_seconds))
time.sleep_ms(23)
utime.set_time(int(set_seconds))
utime.sleep_ms(123)
time.set_time(int(set_seconds))
time.sleep_ms(123)
def circlePoint(self, t):
return (
......
import os
import display
import utime
import time
import buttons
CONFIG_NAME = "ble.txt"
......@@ -84,4 +84,4 @@ while True:
selector()
disp.update()
utime.sleep(0.1)
time.sleep(0.1)
......@@ -7,7 +7,7 @@ import buttons
import color
import display
import os
import utime
import time
import bme680
......@@ -25,7 +25,7 @@ def main():
disp.print("{:7.2f} rh".format(sensor_data[1]), posy=40)
disp.print("{:7.2f} hPa".format(sensor_data[2]), posy=60)
disp.update()
utime.sleep(10)
time.sleep(10)
if __name__ == "__main__":
......
import utime
import time
import display
import leds
import ledfx
import buttons
import light_sensor
import ujson
import json
import os
FILENAME = "nickname.txt"
......@@ -54,9 +54,9 @@ def blink_led(led):
:param led: led to blink
"""
leds.clear()
utime.sleep(0.1)
time.sleep(0.1)
leds.set(led, [255, 0, 0])
utime.sleep(0.1)
time.sleep(0.1)
leds.clear()
......@@ -118,15 +118,15 @@ def get_time():
:return: timestamp
"""
timestamp = ""
if utime.localtime()[3] < 10:
if time.localtime()[3] < 10:
timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[3]) + ":"
if utime.localtime()[4] < 10:
timestamp = timestamp + str(time.localtime()[3]) + ":"
if time.localtime()[4] < 10:
timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[4]) + ":"
if utime.localtime()[5] < 10:
timestamp = timestamp + str(time.localtime()[4]) + ":"
if time.localtime()[5] < 10:
timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[5])
timestamp = timestamp + str(time.localtime()[5])
return timestamp
......@@ -166,7 +166,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
b = 0
rainbow_led_pos = 0
r_sub = sub
last_btn_poll = utime.time() - 2
last_btn_poll = time.time() - 2
while True:
sleep = 0.5
if sub == "#time":
......@@ -181,8 +181,8 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
r_bg = main_bg[dark]
# Button handling
pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
if utime.time() - last_btn_poll >= 1:
last_btn_poll = utime.time()
if time.time() - last_btn_poll >= 1:
last_btn_poll = time.time()
if pressed & buttons.BOTTOM_RIGHT != 0:
anim = anim + 1
if anim >= len(ANIM_TYPES):
......@@ -267,7 +267,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
)
disp.update()
disp.close()
utime.sleep(sleep)
time.sleep(sleep)
def get_key(json, key, default):
......@@ -291,7 +291,7 @@ with display.open() as disp:
if FILENAME_ADV in os.listdir("."):
f = open(FILENAME_ADV, "r")
try:
c = ujson.loads(f.read())
c = json.loads(f.read())
f.close()
# parse config
nick = get_key(c, "nickname", "no nick")
......
......@@ -5,7 +5,7 @@ import display
import buttons
sys.path.append("/apps/digiclk/")
import monotime as utime
import monotime as time
import draw
DIGITS = [
......@@ -48,7 +48,7 @@ def renderBar(d, num):
def render(d):
ltime = utime.localtime()
ltime = time.localtime()
years = ltime[0]
months = ltime[1]
days = ltime[2]
......@@ -111,7 +111,7 @@ def checkButton(button, button_long, osbutton, pressed, t):
def checkButtons():
global pressed_prev
t = utime.time_monotonic_ms()
t = time.time_monotonic_ms()
pressed = buttons.read(
buttons.BOTTOM_LEFT | buttons.TOP_RIGHT | buttons.BOTTOM_RIGHT
)
......@@ -130,8 +130,8 @@ def checkButtons():
def modTime(yrs, mth, day, hrs, mns, sec):
ltime = utime.localtime()
new = utime.mktime(
ltime = time.localtime()
new = time.mktime(
(
ltime[0] + yrs,
ltime[1] + mth,
......@@ -143,7 +143,7 @@ def modTime(yrs, mth, day, hrs, mns, sec):
None,
)
)
utime.set_time(new)
time.set_time(new)
def ctrl_display(bs):
......@@ -334,13 +334,13 @@ def main():
bs = checkButtons()
CTRL_FNS[MODE](bs)
last_secs, secs = secs, utime.time_monotonic()
last_secs, secs = secs, time.time_monotonic()
if updated or secs > last_secs:
render(d)
last_msecs, msecs = msecs, utime.time_monotonic_ms()
last_msecs, msecs = msecs, time.time_monotonic_ms()
if msecs - last_msecs < BUTTON_UPDATE_TIME:
utime.sleep_ms(BUTTON_UPDATE_TIME - (msecs - last_msecs))
time.sleep_ms(BUTTON_UPDATE_TIME - (msecs - last_msecs))
except KeyboardInterrupt:
pass
......
import utime as _utime
import time as _utime
_offset_ms = 0
def time_monotonic():
return _utime.time() + _offset_ms // 1000
return _time.time() + _offset_ms // 1000
def time_monotonic_ms():
return _utime.time_ms() + _offset_ms
return _time.time_ms() + _offset_ms
def sleep(s):
return _utime.sleep(s)
return _time.sleep(s)
def sleep_ms(ms):
return _utime.sleep_ms(ms)
return _time.sleep_ms(ms)
def sleep_us(us):
return _utime.sleep_us(us)
return _time.sleep_us(us)
def time():
return _utime.time()
return _time.time()
def time_ms():
return _utime.time_ms()
return _time.time_ms()
def set_time(t):
global _offset_ms
cur_t = _utime.time_ms()
_utime.set_time(t)
new_t = _utime.time_ms()
cur_t = _time.time_ms()
_time.set_time(t)
new_t = _time.time_ms()
diff = cur_t - new_t
_offset_ms += diff
......@@ -45,9 +45,9 @@ def set_time(t):
def set_unix_time(t):
global _offset_ms
cur_t = _utime.time_ms()
_utime.set_unix_time(t)
new_t = _utime.time_ms()
cur_t = _time.time_ms()
_time.set_unix_time(t)
new_t = _time.time_ms()
diff = cur_t - new_t
_offset_ms += diff
......@@ -55,17 +55,17 @@ def set_unix_time(t):
def localtime(s=None):
if s != None:
return _utime.localtime(s)
return _time.localtime(s)
else:
return _utime.localtime()
return _time.localtime()
def mktime(t):
return _utime.mktime(t)
return _time.mktime(t)
def alarm(s, cb=None):
if cb != None:
return _utime.alarm(s, cb)
return _time.alarm(s, cb)
else:
return _utime.alarm(s)
return _time.alarm(s)
import os
import display
import leds
import utime
import time
import buttons
import max30001
import math
......@@ -178,7 +178,7 @@ def append_to_file(fileprefix, content):
def write_pulse():
# estimates timestamp as calls to utime.time() take too much time
# estimates timestamp as calls to time.time() take too much time
approx_timestamp = write + samples_since_start_of_write // config.get_option("Rate")
append_to_file("pulse", struct.pack("ib", approx_timestamp, pulse))
......@@ -206,7 +206,7 @@ def close_sensor():
def toggle_write():
global write, disp, pause_screen, filebuffer, samples_since_start_of_write, write_time_string
pause_screen = utime.time_ms() + 1000
pause_screen = time.time_ms() + 1000
disp.clear(COLOR_BACKGROUND)
if write > 0:
write_filebuffer()
......@@ -215,8 +215,8 @@ def toggle_write():
disp.print("logging", posx=30, posy=40, fg=COLOR_TEXT)
else:
filebuffer = bytearray()
write = utime.time()
lt = utime.localtime(write)
write = time.time()
lt = time.localtime(write)
write_time_string = "{:04d}-{:02d}-{:02d}_{:02d}{:02d}{:02d}".format(*lt)
samples_since_start_of_write = 0
try:
......@@ -285,7 +285,7 @@ def draw_graph():
if pause_screen == -1:
return
elif pause_screen > 0:
t = utime.time_ms()
t = time.time_ms()
if t > pause_screen:
pause_screen = 0
else:
......@@ -356,7 +356,7 @@ def draw_graph():
# announce writing ecg log
if write > 0:
t = utime.time()
t = time.time()
if write > 0 and t % 2 == 0:
disp.print("LOG", posx=0, posy=60, fg=COLOR_WRITE_FG, bg=COLOR_WRITE_BG)
......@@ -381,7 +381,7 @@ def main():
"< WriteLog ", posx=0, posy=64, fg=COLOR_WRITE_BG, font=display.FONT16
)
disp.update()
utime.sleep(3)
time.sleep(3)
# start ecg
open_sensor()
......
import display
import leds
import utime
import time
_rand = 123456789
......@@ -43,4 +43,4 @@ while True:
d.close()
leds.set(rand() % 11, colors[rand() % len(colors)])
leds.set_rocket(rand() % 3, rand() % 32)
utime.sleep_us(1) # Feed watch doge
time.sleep_us(1) # Feed watch doge
import os
import display
import utime
import time
import buttons
import light_sensor
import math
......@@ -32,4 +32,4 @@ while True:
disp.pixel(WIDTH - i, HEIGHT - y - 1)
disp.update()
utime.sleep(0.1)
time.sleep(0.1)
......@@ -7,7 +7,7 @@ import buttons
import color
import display
import os
import utime
import time
STATE_LIST = "List"
STATE_SHOW = "Show"
......@@ -64,7 +64,7 @@ def button_events(timeout=0):
button_pressed = True
yield buttons.TOP_RIGHT
utime.sleep_ms(10)
time.sleep_ms(10)
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
......
......@@ -12,8 +12,8 @@ import display
import os
import simple_menu
import sys
import ujson
import utime
import json
import time
App = collections.namedtuple("App", ["name", "path"])
......@@ -49,7 +49,7 @@ def enumerate_apps(apps=None):
try:
with open("/apps/" + app + "/metadata.json") as f:
info = ujson.load(f)
info = json.load(f)
yield App(
info["name"], "/apps/{}/{}".format(app, info.get("bin", "__init__.py"))
......@@ -74,7 +74,7 @@ def usb_mode(disp):
disp.print("open", posx=52, posy=40, fg=color.CAMPGREEN_DARK)
disp.update()
utime.sleep_ms(200)
time.sleep_ms(200)
# Wait for select button to be released
while buttons.read() == buttons.TOP_RIGHT:
......@@ -104,7 +104,7 @@ class SubMenu(simple_menu.Menu):
import config
config.set_string("default_app", self.app.path)
utime.sleep(1)
time.sleep(1)
self.exit()
......@@ -127,7 +127,7 @@ class MainMenu(simple_menu.Menu):
print("Loading failed: ")
sys.print_exception(e)
self.error("Loading", "failed")
utime.sleep(1.0)
time.sleep(1.0)
os.exit(1)
def on_long_select(self, app, index):
......@@ -168,7 +168,7 @@ def no_apps_message():
disp.update()
while True:
utime.sleep(0.5)
time.sleep(0.5)
if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment