Skip to content

Commit

Permalink
all: Replace all uses of umodule in Python code.
Browse files Browse the repository at this point in the history
Applies to drivers/examples/extmod/port-modules/tools.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <[email protected]>
  • Loading branch information
jimmo committed Jun 8, 2023
1 parent 9d7eac0 commit 5fd042e
Show file tree
Hide file tree
Showing 29 changed files with 99 additions and 121 deletions.
6 changes: 3 additions & 3 deletions examples/hwapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ application of this idea would look like:
`app.py`:

from hwconfig import *
import utime
import time

while True:
LED.value(1)
utime.sleep_ms(500)
time.sleep_ms(500)
LED.value(0)
utime.sleep_ms(500)
time.sleep_ms(500)


To deploy this application to a particular board, a user will need:
Expand Down
4 changes: 2 additions & 2 deletions examples/hwapi/button_led.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import utime
import time
from hwconfig import LED, BUTTON

# Light LED when (and while) a BUTTON is pressed

while 1:
LED.value(BUTTON.value())
# Don't burn CPU
utime.sleep_ms(10)
time.sleep_ms(10)
4 changes: 2 additions & 2 deletions examples/hwapi/button_reaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utime
import time
import machine
from hwconfig import LED, BUTTON

Expand All @@ -18,4 +18,4 @@
print("Well, you're *really* slow")
else:
print("You are as slow as %d microseconds!" % delay)
utime.sleep_ms(10)
time.sleep_ms(10)
6 changes: 3 additions & 3 deletions examples/hwapi/soft_pwm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utime
import time
from hwconfig import LED


Expand All @@ -15,10 +15,10 @@ def pwm_cycle(led, duty, cycles):
for i in range(cycles):
if duty:
led.on()
utime.sleep_ms(duty)
time.sleep_ms(duty)
if duty_off:
led.off()
utime.sleep_ms(duty_off)
time.sleep_ms(duty_off)


# At the duty setting of 1, an LED is still pretty bright, then
Expand Down
5 changes: 1 addition & 4 deletions examples/network/http_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
import usocket as socket
except:
import socket
import socket


def main(use_stream=False):
Expand Down
14 changes: 4 additions & 10 deletions examples/network/http_client_ssl.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
try:
import usocket as _socket
except:
import _socket
try:
import ussl as ssl
except:
import ssl
import socket
import ssl


def main(use_stream=True):
s = _socket.socket()
s = socket.socket()

ai = _socket.getaddrinfo("google.com", 443)
ai = socket.getaddrinfo("google.com", 443)
print("Address infos:", ai)
addr = ai[0][-1]

Expand Down
5 changes: 1 addition & 4 deletions examples/network/http_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
import usocket as socket
except:
import socket
import socket


CONTENT = b"""\
Expand Down
5 changes: 1 addition & 4 deletions examples/network/http_server_simplistic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Do not use this code in real projects! Read
# http_server_simplistic_commented.py for details.
try:
import usocket as socket
except:
import socket
import socket


CONTENT = b"""\
Expand Down
5 changes: 1 addition & 4 deletions examples/network/http_server_simplistic_commented.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
# details, and use this code only for quick hacks, preferring
# http_server.py for "real thing".
#
try:
import usocket as socket
except:
import socket
import socket


CONTENT = b"""\
Expand Down
10 changes: 3 additions & 7 deletions examples/network/http_server_ssl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import ubinascii as binascii

try:
import usocket as socket
except:
import socket
import ussl as ssl
import binascii
import socket
import ssl


# This self-signed key/cert pair is randomly generated and to be used for
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/machine_bios.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# It is expected to print 0xaa55, which is a signature at the start of
# Video BIOS.

import umachine as machine
import machine

print(hex(machine.mem16[0xC0000]))
4 changes: 2 additions & 2 deletions extmod/uasyncio/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def wait(self):
# that asyncio will poll until a flag is set.
# Note: Unlike Event, this is self-clearing after a wait().
try:
import uio
import io

class ThreadSafeFlag(uio.IOBase):
class ThreadSafeFlag(io.IOBase):
def __init__(self):
self.state = 0

Expand Down
6 changes: 3 additions & 3 deletions extmod/uasyncio/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def drain(self):
#
# async
def open_connection(host, port):
from uerrno import EINPROGRESS
import usocket as socket
from errno import EINPROGRESS
import socket

ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] # TODO this is blocking!
s = socket.socket(ai[0], ai[1], ai[2])
Expand Down Expand Up @@ -154,7 +154,7 @@ async def _serve(self, s, cb):
# Helper function to start a TCP stream server, running as a new task
# TODO could use an accept-callback on socket read activity instead of creating a task
async def start_server(cb, host, port, backlog=5):
import usocket as socket
import socket

# Create and bind server socket.
host = socket.getaddrinfo(host, port)[0] # TODO this is blocking!
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32/modules/_boot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import gc
import uos
import os
from flashbdev import bdev

try:
if bdev:
uos.mount(bdev, "/")
os.mount(bdev, "/")
except OSError:
import inisetup

Expand Down
8 changes: 4 additions & 4 deletions ports/esp32/modules/inisetup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uos
import os
from flashbdev import bdev


Expand Down Expand Up @@ -33,9 +33,9 @@ def fs_corrupted():
def setup():
check_bootsec()
print("Performing initial setup")
uos.VfsLfs2.mkfs(bdev)
vfs = uos.VfsLfs2(bdev)
uos.mount(vfs, "/")
os.VfsLfs2.mkfs(bdev)
vfs = os.VfsLfs2(bdev)
os.mount(vfs, "/")
with open("boot.py", "w") as f:
f.write(
"""\
Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/boards/GENERIC/board.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Note: v1.12-334 and newer (including v1.13) require an ESP8266 module with
2MiB of flash or more, and use littlefs as the filesystem by default. When
upgrading from older firmware please backup your files first, and either
erase all flash before upgrading, or after upgrading execute
`uos.VfsLfs2.mkfs(bdev)`.
`os.VfsLfs2.mkfs(bdev)`.

### OTA builds
Over-The-Air (OTA) builds of the ESP8266 firmware are also provided.
Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/boards/GENERIC_1M/board.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ The following are daily builds of the ESP8266 firmware tailored for modules with
only 1MiB of flash. This firmware uses littlefs as the filesystem.
When upgrading from older firmware that uses a FAT filesystem please backup your files
first, and either erase all flash before upgrading, or after upgrading execute
`uos.VfsLfs2.mkfs(bdev)`.
`os.VfsLfs2.mkfs(bdev)`.
4 changes: 2 additions & 2 deletions ports/esp8266/modules/_boot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import gc

gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)
import uos
import os
from flashbdev import bdev

if bdev:
try:
uos.mount(bdev, "/")
os.mount(bdev, "/")
except:
import inisetup

Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/modules/espnow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# MIT license; Copyright (c) 2022 Glenn Moloney @glenn20

from _espnow import *
from uselect import poll, POLLIN
from select import poll, POLLIN


class ESPNow(ESPNowBase):
Expand Down
18 changes: 9 additions & 9 deletions ports/esp8266/modules/inisetup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import uos
import os
import network
from flashbdev import bdev


def wifi():
import ubinascii
import binascii

ap_if = network.WLAN(network.AP_IF)
ssid = b"MicroPython-%s" % ubinascii.hexlify(ap_if.config("mac")[-3:])
ssid = b"MicroPython-%s" % binascii.hexlify(ap_if.config("mac")[-3:])
ap_if.config(ssid=ssid, security=network.AUTH_WPA_WPA2_PSK, key=b"micropythoN")


Expand All @@ -32,7 +32,7 @@ def fs_corrupted():
"""\
The filesystem starting at sector %d with size %d sectors looks corrupt.
You may want to make a flash snapshot and try to recover it. Otherwise,
format it with uos.VfsLfs2.mkfs(bdev), or completely erase the flash and
format it with os.VfsLfs2.mkfs(bdev), or completely erase the flash and
reprogram MicroPython.
"""
% (bdev.start_sec, bdev.blocks)
Expand All @@ -44,17 +44,17 @@ def setup():
check_bootsec()
print("Performing initial setup")
wifi()
uos.VfsLfs2.mkfs(bdev)
vfs = uos.VfsLfs2(bdev)
uos.mount(vfs, "/")
os.VfsLfs2.mkfs(bdev)
vfs = os.VfsLfs2(bdev)
os.mount(vfs, "/")
with open("boot.py", "w") as f:
f.write(
"""\
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import uos, machine
#uos.dupterm(None, 1) # disable REPL on UART(0)
import os, machine
#os.dupterm(None, 1) # disable REPL on UART(0)
import gc
#import webrepl
#webrepl.start()
Expand Down
14 changes: 7 additions & 7 deletions ports/nrf/modules/scripts/_mkfs.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import uos, nrf
import os, nrf

try:
from uos import VfsLfs1
from os import VfsLfs1

uos.VfsLfs1.mkfs(nrf.Flash())
os.VfsLfs1.mkfs(nrf.Flash())
except ImportError:
try:
from uos import VfsLfs2
from os import VfsLfs2

uos.VfsLfs2.mkfs(nrf.Flash())
os.VfsLfs2.mkfs(nrf.Flash())
except ImportError:
try:
from uos import VfsFat
from os import VfsFat

uos.VfsFat.mkfs(nrf.Flash())
os.VfsFat.mkfs(nrf.Flash())
except ImportError:
pass
except OSError as e:
Expand Down
4 changes: 2 additions & 2 deletions ports/rp2/modules/rp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def __init__(
pull_thresh=32,
fifo_join=0
):
# uarray is a built-in module so importing it here won't require
# array is a built-in module so importing it here won't require
# scanning the filesystem.
from uarray import array
from array import array

self.labels = {}
execctrl = 0
Expand Down
8 changes: 4 additions & 4 deletions ports/samd/modules/_boot.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import gc
import uos
import os
import samd

bdev = samd.Flash()

# Try to mount the filesystem, and format the flash if it doesn't exist.
fs_type = uos.VfsLfs2 if hasattr(uos, "VfsLfs2") else uos.VfsLfs1
fs_type = os.VfsLfs2 if hasattr(os, "VfsLfs2") else os.VfsLfs1

try:
vfs = fs_type(bdev, progsize=256)
except:
fs_type.mkfs(bdev, progsize=256)
vfs = fs_type(bdev, progsize=256)
uos.mount(vfs, "/")
os.mount(vfs, "/")

del vfs, fs_type, bdev, uos, samd
del vfs, fs_type, bdev, os, samd
gc.collect()
del gc
2 changes: 1 addition & 1 deletion ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

try:
import machine, stm
from ubinascii import crc32
from binascii import crc32
from micropython import const
except ImportError:
# cpython
Expand Down
6 changes: 3 additions & 3 deletions ports/stm32/mboot/fwupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from micropython import const
import struct, time
import uzlib, machine, stm
import zlib, machine, stm

# Constants to be used with update_mpy
VFS_FAT = 1
Expand Down Expand Up @@ -36,7 +36,7 @@ def dfu_read(filename):
if hdr == b"Dfu":
pass
elif hdr == b"\x1f\x8b\x08":
f = uzlib.DecompIO(f, 16 + 15)
f = zlib.DecompIO(f, 16 + 15)
else:
print("Invalid firmware", filename)
return None
Expand Down Expand Up @@ -231,7 +231,7 @@ def update_app_elements(
# Check firmware is of .dfu or .dfu.gz type
try:
with open(filename, "rb") as f:
hdr = uzlib.DecompIO(f, 16 + 15).read(6)
hdr = zlib.DecompIO(f, 16 + 15).read(6)
except Exception:
with open(filename, "rb") as f:
hdr = f.read(6)
Expand Down
Loading

0 comments on commit 5fd042e

Please sign in to comment.