Commit 6414bb35 authored by Rahix's avatar Rahix
Browse files

Merge "Create weak link from `(u)module` to `module`"

See merge request card10/firmware!388
parents f0807c24 328b4fb8
...@@ -28,18 +28,18 @@ The coordinate system of the BHI160 sensor data looks like this: ...@@ -28,18 +28,18 @@ The coordinate system of the BHI160 sensor data looks like this:
.. code-block:: python .. code-block:: python
import bhi160 import bhi160
import utime import time
bhi = bhi160.BHI160Orientation() bhi = bhi160.BHI160Orientation()
while True: while True:
samples = bhi.read() samples = bhi.read()
if len(samples) == 0: if len(samples) == 0:
utime.sleep(0.25) time.sleep(0.25)
continue continue
# print the latest sample # print the latest sample
print(samples[-1]) print(samples[-1])
utime.sleep(0.25) time.sleep(0.25)
Orientation Orientation
......
...@@ -8,7 +8,7 @@ Allows access to environmental data of card10's surroundings. ...@@ -8,7 +8,7 @@ Allows access to environmental data of card10's surroundings.
.. code-block:: python .. code-block:: python
import bme680, utime import bme680, time
with bme680.Bme680() as environment: with bme680.Bme680() as environment:
while True: while True:
...@@ -19,7 +19,7 @@ Allows access to environmental data of card10's surroundings. ...@@ -19,7 +19,7 @@ Allows access to environmental data of card10's surroundings.
print("Pressure: {:10.2f} hPa".format(d.pressure)) print("Pressure: {:10.2f} hPa".format(d.pressure))
print("Gas Resistance: {:10.2f} Ω".format(d.resistance)) print("Gas Resistance: {:10.2f} Ω".format(d.resistance))
utime.sleep(1) time.sleep(1)
Sensor Class Sensor Class
------------ ------------
......
MicroPython Standard Library MicroPython Standard Library
============================ ============================
Pycardium contains some modules from the MicroPython standard library. These Pycardium contains some modules from the MicroPython standard library.
are:
Some modules below use a standard Python name, but prefixed with “u”,
e.g. ujson instead of json. This is to signify that such a module is a
micro-library, i.e. implements only a subset of CPython module
functionality. Please refer to the official `MicroPython docs`_ for an
explanation why.
All u-name modules can also be imported using their non-u-name. E.g.
``import utime`` and import ``import time`` will both work.
.. _MicroPython docs: http://docs.micropython.org/en/latest/library/index.html#python-standard-libraries-and-micro-libraries
.. py:module:: framebuf .. py:module:: framebuf
......
...@@ -8,6 +8,9 @@ CPython but wouldn't fit anywhere else in our implementation. Most ...@@ -8,6 +8,9 @@ CPython but wouldn't fit anywhere else in our implementation. Most
prominently, this is the :py:func:`utime.alarm` function for setting an RTC prominently, this is the :py:func:`utime.alarm` function for setting an RTC
alarm. alarm.
Like all other u-name modules, ``utime`` can also imported using the standard
``import time`` statement.
.. |time| replace:: ``time`` .. |time| replace:: ``time``
.. _time: https://docs.python.org/3/library/time.html .. _time: https://docs.python.org/3/library/time.html
...@@ -135,13 +138,13 @@ alarm. ...@@ -135,13 +138,13 @@ alarm.
.. code-block:: python .. code-block:: python
import utime import time
def minute_timer(x): def minute_timer(x):
current = utime.time() current = time.time()
print("Current: " + str(current)) print("Current: " + str(current))
alarm = (current // 60 + 1) * 60 alarm = (current // 60 + 1) * 60
utime.alarm(alarm, minute_timer) time.alarm(alarm, minute_timer)
minute_timer(None) minute_timer(None)
...@@ -150,13 +153,13 @@ alarm. ...@@ -150,13 +153,13 @@ alarm.
.. code-block:: python .. code-block:: python
import interrupt, utime import interrupt, time
def 5_second_timer(x): def 5_second_timer(x):
current = utime.time() current = time.time()
print("Current: " + str(current)) print("Current: " + str(current))
alarm = (current // 10) * 10 + 5 alarm = (current // 10) * 10 + 5
utime.alarm(alarm) time.alarm(alarm)
# This time, we need to register and enable the callback manually # This time, we need to register and enable the callback manually
interrupt.set_callback(interrupt.RTC_ALARM, 5_second_timer) interrupt.set_callback(interrupt.RTC_ALARM, 5_second_timer)
......
...@@ -19,7 +19,7 @@ The ``ws2812`` module controls LEDs of the WS2812 type. Just as the ``leds`` mod ...@@ -19,7 +19,7 @@ The ``ws2812`` module controls LEDs of the WS2812 type. Just as the ``leds`` mod
.. code-block:: python .. code-block:: python
import color, utime, ws2812, gpio import color, time, ws2812, gpio
i = 0 i = 0
while True: while True:
...@@ -28,6 +28,6 @@ The ``ws2812`` module controls LEDs of the WS2812 type. Just as the ``leds`` mod ...@@ -28,6 +28,6 @@ The ``ws2812`` module controls LEDs of the WS2812 type. Just as the ``leds`` mod
col3 = color.from_hsv((i + 40) % 360, 1.0, 0.1) col3 = color.from_hsv((i + 40) % 360, 1.0, 0.1)
ws2812.set_all(gpio.WRISTBAND_2, [col1, col2, col3]) ws2812.set_all(gpio.WRISTBAND_2, [col1, col2, col3])
i += 1 i += 1
utime.sleep_ms(10) time.sleep_ms(10)
.. versionadded:: 1.10 .. versionadded:: 1.10
import ujson import json
import os import os
import display import display
import utime import time
import buttons import buttons
CONFIG_NAME = "at-timestamp.json" CONFIG_NAME = "at-timestamp.json"
...@@ -11,7 +11,7 @@ def init(): ...@@ -11,7 +11,7 @@ def init():
if CONFIG_NAME not in os.listdir("."): if CONFIG_NAME not in os.listdir("."):
at_config = {"time_start": "unset"} at_config = {"time_start": "unset"}
f = open(CONFIG_NAME, "w") f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(at_config)) f.write(json.dumps(at_config))
f.close() f.close()
if is_timestamp_set(): if is_timestamp_set():
...@@ -22,7 +22,7 @@ def init(): ...@@ -22,7 +22,7 @@ def init():
def is_timestamp_set(): def is_timestamp_set():
f = open(CONFIG_NAME, "r") f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = json.loads(f.read())
f.close() f.close()
if c["time_start"] == "unset": if c["time_start"] == "unset":
return False return False
...@@ -40,29 +40,29 @@ def triangle(disp, x, y, left): ...@@ -40,29 +40,29 @@ def triangle(disp, x, y, left):
def timestamp_reset(): def timestamp_reset():
f = open(CONFIG_NAME, "r") f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = json.loads(f.read())
c["time_start"] = "unset" c["time_start"] = "unset"
f.close() f.close()
f = open(CONFIG_NAME, "w") f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c)) f.write(json.dumps(c))
f.close() f.close()
def timestamp_read(): def timestamp_read():
global time_start global time_start
f = open(CONFIG_NAME, "r") f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = json.loads(f.read())
time_start = c["time_start"] time_start = c["time_start"]
f.close() f.close()
def timestamp_write(): def timestamp_write():
f = open(CONFIG_NAME, "r") f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = json.loads(f.read())
c["time_start"] = utime.time() c["time_start"] = time.time()
f.close() f.close()
f = open(CONFIG_NAME, "w") f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c)) f.write(json.dumps(c))
f.close() f.close()
...@@ -76,7 +76,7 @@ def menu(): ...@@ -76,7 +76,7 @@ def menu():
triangle(disp, 10, 66, True) triangle(disp, 10, 66, True)
disp.print("start.", posx=15, posy=60, fg=[0, 255, 0]) disp.print("start.", posx=15, posy=60, fg=[0, 255, 0])
elif menu_state == 1: elif menu_state == 1:
seconds = utime.time() - time_start seconds = time.time() - time_start
m, s = divmod(seconds, 60) m, s = divmod(seconds, 60)
h, m = divmod(m, 60) h, m = divmod(m, 60)
disp.print("%02d:%02d:%02d" % (h, m, s), posy=40, fg=[255, 255, 255]) disp.print("%02d:%02d:%02d" % (h, m, s), posy=40, fg=[255, 255, 255])
...@@ -122,4 +122,4 @@ while True: ...@@ -122,4 +122,4 @@ while True:
menu_state = 0 menu_state = 0
menu() menu()
disp.update() disp.update()
utime.sleep(0.1) time.sleep(0.1)
# Adapted from https://github.com/muccc/flipdots/blob/master/scripts/clock.py # Adapted from https://github.com/muccc/flipdots/blob/master/scripts/clock.py
import display import display
from utime import sleep from time import sleep
import utime import time
import math import math
import leds import leds
import buttons import buttons
import ujson import json
import os import os
CONFIG_NAME = "clock.json" CONFIG_NAME = "clock.json"
...@@ -100,7 +100,7 @@ class Clock: ...@@ -100,7 +100,7 @@ class Clock:
def readConfig(self): def readConfig(self):
with open(CONFIG_NAME, "r") as f: with open(CONFIG_NAME, "r") as f:
try: try:
c = ujson.loads(f.read()) c = json.loads(f.read())
if ( if (
"themes" in c "themes" in c
and len(c["themes"]) > 0 and len(c["themes"]) > 0
...@@ -114,7 +114,7 @@ class Clock: ...@@ -114,7 +114,7 @@ class Clock:
def writeConfig(self): def writeConfig(self):
with open(CONFIG_NAME, "w") as f: 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): def setTheme(self, theme):
self.theme = theme % len(self.themes) self.theme = theme % len(self.themes)
...@@ -159,7 +159,7 @@ class Clock: ...@@ -159,7 +159,7 @@ class Clock:
try: try:
with display.open() as disp: with display.open() as disp:
while True: while True:
localtime = utime.localtime() localtime = time.localtime()
self.updateClock(disp, localtime) self.updateClock(disp, localtime)
if self.run_once: if self.run_once:
break break
...@@ -179,7 +179,7 @@ class Clock: ...@@ -179,7 +179,7 @@ class Clock:
elif button_pressed and v & buttons.TOP_RIGHT != 0: elif button_pressed and v & buttons.TOP_RIGHT != 0:
self.setTime(disp, localtime) self.setTime(disp, localtime)
utime.sleep_ms(23) time.sleep_ms(23)
except KeyboardInterrupt: except KeyboardInterrupt:
for i in range(11): for i in range(11):
...@@ -255,7 +255,7 @@ class Clock: ...@@ -255,7 +255,7 @@ class Clock:
accepted = False accepted = False
previously_pressed_button = buttons.TOP_RIGHT previously_pressed_button = buttons.TOP_RIGHT
button_repeat_counter = 0 button_repeat_counter = 0
set_seconds = utime.mktime(localtime) set_seconds = time.mktime(localtime)
while not accepted: while not accepted:
v = buttons.read( v = buttons.read(
...@@ -288,11 +288,11 @@ class Clock: ...@@ -288,11 +288,11 @@ class Clock:
elif previously_pressed_button == buttons.BOTTOM_RIGHT: elif previously_pressed_button == buttons.BOTTOM_RIGHT:
set_seconds += seconds_change set_seconds += seconds_change
self.updateClock(disp, utime.localtime(set_seconds)) self.updateClock(disp, time.localtime(set_seconds))
utime.sleep_ms(23) time.sleep_ms(23)
utime.set_time(int(set_seconds)) time.set_time(int(set_seconds))
utime.sleep_ms(123) time.sleep_ms(123)
def circlePoint(self, t): def circlePoint(self, t):
return ( return (
......
import os import os
import display import display
import utime import time
import buttons import buttons
CONFIG_NAME = "ble.txt" CONFIG_NAME = "ble.txt"
...@@ -84,4 +84,4 @@ while True: ...@@ -84,4 +84,4 @@ while True:
selector() selector()
disp.update() disp.update()
utime.sleep(0.1) time.sleep(0.1)
...@@ -7,7 +7,7 @@ import buttons ...@@ -7,7 +7,7 @@ import buttons
import color import color
import display import display
import os import os
import utime import time
import bme680 import bme680
...@@ -25,7 +25,7 @@ def main(): ...@@ -25,7 +25,7 @@ def main():
disp.print("{:7.2f} rh".format(sensor_data[1]), posy=40) disp.print("{:7.2f} rh".format(sensor_data[1]), posy=40)
disp.print("{:7.2f} hPa".format(sensor_data[2]), posy=60) disp.print("{:7.2f} hPa".format(sensor_data[2]), posy=60)
disp.update() disp.update()
utime.sleep(10) time.sleep(10)
if __name__ == "__main__": if __name__ == "__main__":
......
import utime import time
import display import display
import leds import leds
import ledfx import ledfx
import buttons import buttons
import light_sensor import light_sensor
import ujson import json
import os import os
FILENAME = "nickname.txt" FILENAME = "nickname.txt"
...@@ -54,9 +54,9 @@ def blink_led(led): ...@@ -54,9 +54,9 @@ def blink_led(led):
:param led: led to blink :param led: led to blink
""" """
leds.clear() leds.clear()
utime.sleep(0.1) time.sleep(0.1)
leds.set(led, [255, 0, 0]) leds.set(led, [255, 0, 0])
utime.sleep(0.1) time.sleep(0.1)
leds.clear() leds.clear()
...@@ -118,15 +118,15 @@ def get_time(): ...@@ -118,15 +118,15 @@ def get_time():
:return: timestamp :return: timestamp
""" """
timestamp = "" timestamp = ""
if utime.localtime()[3] < 10: if time.localtime()[3] < 10:
timestamp = timestamp + "0" timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[3]) + ":" timestamp = timestamp + str(time.localtime()[3]) + ":"
if utime.localtime()[4] < 10: if time.localtime()[4] < 10:
timestamp = timestamp + "0" timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[4]) + ":" timestamp = timestamp + str(time.localtime()[4]) + ":"
if utime.localtime()[5] < 10: if time.localtime()[5] < 10:
timestamp = timestamp + "0" timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[5]) timestamp = timestamp + str(time.localtime()[5])
return timestamp return timestamp
...@@ -166,7 +166,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -166,7 +166,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
b = 0 b = 0
rainbow_led_pos = 0 rainbow_led_pos = 0
r_sub = sub r_sub = sub
last_btn_poll = utime.time() - 2 last_btn_poll = time.time() - 2
while True: while True:
sleep = 0.5 sleep = 0.5
if sub == "#time": if sub == "#time":
...@@ -181,8 +181,8 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -181,8 +181,8 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
r_bg = main_bg[dark] r_bg = main_bg[dark]
# Button handling # Button handling
pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
if utime.time() - last_btn_poll >= 1: if time.time() - last_btn_poll >= 1:
last_btn_poll = utime.time() last_btn_poll = time.time()
if pressed & buttons.BOTTOM_RIGHT != 0: if pressed & buttons.BOTTOM_RIGHT != 0:
anim = anim + 1 anim = anim + 1
if anim >= len(ANIM_TYPES): if anim >= len(ANIM_TYPES):
...@@ -267,7 +267,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -267,7 +267,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
) )
disp.update() disp.update()
disp.close() disp.close()
utime.sleep(sleep) time.sleep(sleep)
def get_key(json, key, default): def get_key(json, key, default):
...@@ -291,7 +291,7 @@ with display.open() as disp: ...@@ -291,7 +291,7 @@ with display.open() as disp:
if FILENAME_ADV in os.listdir("."): if FILENAME_ADV in os.listdir("."):
f = open(FILENAME_ADV, "r") f = open(FILENAME_ADV, "r")
try: try:
c = ujson.loads(f.read()) c = json.loads(f.read())
f.close() f.close()
# parse config # parse config
nick = get_key(c, "nickname", "no nick") nick = get_key(c, "nickname", "no nick")
......
...@@ -5,7 +5,7 @@ import display ...@@ -5,7 +5,7 @@ import display
import buttons import buttons
sys.path.append("/apps/digiclk/") sys.path.append("/apps/digiclk/")
import monotime as utime import monotime as time
import draw import draw
DIGITS = [ DIGITS = [
...@@ -48,7 +48,7 @@ def renderBar(d, num): ...@@ -48,7 +48,7 @@ def renderBar(d, num):
def render(d): def render(d):
ltime = utime.localtime() ltime = time.localtime()
years = ltime[0] years = ltime[0]
months = ltime[1] months = ltime[1]
days = ltime[2] days = ltime[2]
...@@ -111,7 +111,7 @@ def checkButton(button, button_long, osbutton, pressed, t): ...@@ -111,7 +111,7 @@ def checkButton(button, button_long, osbutton, pressed, t):
def checkButtons(): def checkButtons():
global pressed_prev global pressed_prev
t = utime.time_monotonic_ms() t = time.time_monotonic_ms()
pressed = buttons.read( pressed = buttons.read(
buttons.BOTTOM_LEFT | buttons.TOP_RIGHT | buttons.BOTTOM_RIGHT buttons.BOTTOM_LEFT | buttons.TOP_RIGHT | buttons.BOTTOM_RIGHT
) )
...@@ -130,8 +130,8 @@ def checkButtons(): ...@@ -130,8 +130,8 @@ def checkButtons():
def modTime(yrs, mth, day, hrs, mns, sec): def modTime(yrs, mth, day, hrs, mns, sec):
ltime = utime.localtime() ltime = time.localtime()