diff options
author | Damien George <damien.p.george@gmail.com> | 2017-09-06 13:40:51 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-09-06 13:40:51 +1000 |
commit | 01dd7804b87d60b2deab16712eccb3b97351a9b7 (patch) | |
tree | 1aa21f38a872b8e62a3d4e4f74f68033c6f827e4 /esp8266/modules | |
parent | a9862b30068fc9df1022f08019fb35aaa5085f64 (diff) | |
download | micropython-01dd7804b87d60b2deab16712eccb3b97351a9b7.tar.gz micropython-01dd7804b87d60b2deab16712eccb3b97351a9b7.zip |
ports: Make new ports/ sub-directory and move all ports there.
This is to keep the top-level directory clean, to make it clear what is
core and what is a port, and to allow the repository to grow with new ports
in a sustainable way.
Diffstat (limited to 'esp8266/modules')
-rw-r--r-- | esp8266/modules/_boot.py | 13 | ||||
-rw-r--r-- | esp8266/modules/apa102.py | 17 | ||||
-rw-r--r-- | esp8266/modules/dht.py | 32 | ||||
l--------- | esp8266/modules/ds18x20.py | 1 | ||||
-rw-r--r-- | esp8266/modules/flashbdev.py | 35 | ||||
-rw-r--r-- | esp8266/modules/inisetup.py | 52 | ||||
-rw-r--r-- | esp8266/modules/neopixel.py | 32 | ||||
-rw-r--r-- | esp8266/modules/ntptime.py | 36 | ||||
l--------- | esp8266/modules/onewire.py | 1 | ||||
-rw-r--r-- | esp8266/modules/port_diag.py | 33 | ||||
l--------- | esp8266/modules/upip.py | 1 | ||||
l--------- | esp8266/modules/upip_utarfile.py | 1 | ||||
-rw-r--r-- | esp8266/modules/webrepl.py | 77 | ||||
-rw-r--r-- | esp8266/modules/webrepl_setup.py | 111 | ||||
-rw-r--r-- | esp8266/modules/websocket_helper.py | 74 |
15 files changed, 0 insertions, 516 deletions
diff --git a/esp8266/modules/_boot.py b/esp8266/modules/_boot.py deleted file mode 100644 index 81eb20dd63..0000000000 --- a/esp8266/modules/_boot.py +++ /dev/null @@ -1,13 +0,0 @@ -import gc -gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4) -import uos -from flashbdev import bdev - -try: - if bdev: - uos.mount(bdev, '/') -except OSError: - import inisetup - inisetup.setup() - -gc.collect() diff --git a/esp8266/modules/apa102.py b/esp8266/modules/apa102.py deleted file mode 100644 index 41b7c0485c..0000000000 --- a/esp8266/modules/apa102.py +++ /dev/null @@ -1,17 +0,0 @@ -# APA102 driver for MicroPython on ESP8266 -# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch - -from esp import apa102_write -from neopixel import NeoPixel - - -class APA102(NeoPixel): - ORDER = (0, 1, 2, 3) - - def __init__(self, clock_pin, data_pin, n, bpp=4): - super().__init__(data_pin, n, bpp) - self.clock_pin = clock_pin - self.clock_pin.init(clock_pin.OUT) - - def write(self): - apa102_write(self.clock_pin, self.pin, self.buf) diff --git a/esp8266/modules/dht.py b/esp8266/modules/dht.py deleted file mode 100644 index 9a69e7e07e..0000000000 --- a/esp8266/modules/dht.py +++ /dev/null @@ -1,32 +0,0 @@ -# DHT11/DHT22 driver for MicroPython on ESP8266 -# MIT license; Copyright (c) 2016 Damien P. George - -import esp - -class DHTBase: - def __init__(self, pin): - self.pin = pin - self.buf = bytearray(5) - - def measure(self): - buf = self.buf - esp.dht_readinto(self.pin, buf) - if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]: - raise Exception("checksum error") - -class DHT11(DHTBase): - def humidity(self): - return self.buf[0] - - def temperature(self): - return self.buf[2] - -class DHT22(DHTBase): - def humidity(self): - return (self.buf[0] << 8 | self.buf[1]) * 0.1 - - def temperature(self): - t = ((self.buf[2] & 0x7f) << 8 | self.buf[3]) * 0.1 - if self.buf[2] & 0x80: - t = -t - return t diff --git a/esp8266/modules/ds18x20.py b/esp8266/modules/ds18x20.py deleted file mode 120000 index faae59d909..0000000000 --- a/esp8266/modules/ds18x20.py +++ /dev/null @@ -1 +0,0 @@ -../../drivers/onewire/ds18x20.py
\ No newline at end of file diff --git a/esp8266/modules/flashbdev.py b/esp8266/modules/flashbdev.py deleted file mode 100644 index 40ba655c64..0000000000 --- a/esp8266/modules/flashbdev.py +++ /dev/null @@ -1,35 +0,0 @@ -import esp - -class FlashBdev: - - SEC_SIZE = 4096 - RESERVED_SECS = 1 - START_SEC = esp.flash_user_start() // SEC_SIZE + RESERVED_SECS - NUM_BLK = 0x6b - RESERVED_SECS - - def __init__(self, blocks=NUM_BLK): - self.blocks = blocks - - def readblocks(self, n, buf): - #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) - esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) - - def writeblocks(self, n, buf): - #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) - #assert len(buf) <= self.SEC_SIZE, len(buf) - esp.flash_erase(n + self.START_SEC) - esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) - - def ioctl(self, op, arg): - #print("ioctl(%d, %r)" % (op, arg)) - if op == 4: # BP_IOCTL_SEC_COUNT - return self.blocks - if op == 5: # BP_IOCTL_SEC_SIZE - return self.SEC_SIZE - -size = esp.flash_size() -if size < 1024*1024: - bdev = None -else: - # 20K at the flash end is reserved for SDK params storage - bdev = FlashBdev((size - 20480) // FlashBdev.SEC_SIZE - FlashBdev.START_SEC) diff --git a/esp8266/modules/inisetup.py b/esp8266/modules/inisetup.py deleted file mode 100644 index af78dfad51..0000000000 --- a/esp8266/modules/inisetup.py +++ /dev/null @@ -1,52 +0,0 @@ -import uos -import network -from flashbdev import bdev - -def wifi(): - import ubinascii - ap_if = network.WLAN(network.AP_IF) - essid = b"MicroPython-%s" % ubinascii.hexlify(ap_if.config("mac")[-3:]) - ap_if.config(essid=essid, authmode=network.AUTH_WPA_WPA2_PSK, password=b"micropythoN") - -def check_bootsec(): - buf = bytearray(bdev.SEC_SIZE) - bdev.readblocks(0, buf) - empty = True - for b in buf: - if b != 0xff: - empty = False - break - if empty: - return True - fs_corrupted() - -def fs_corrupted(): - import time - while 1: - print("""\ -The FAT filesystem starting at sector %d with size %d sectors appears to -be corrupted. If you had important data there, you may want to make a flash -snapshot to try to recover it. Otherwise, perform factory reprogramming -of MicroPython firmware (completely erase flash, followed by firmware -programming). -""" % (bdev.START_SEC, bdev.blocks)) - time.sleep(3) - -def setup(): - check_bootsec() - print("Performing initial setup") - wifi() - uos.VfsFat.mkfs(bdev) - vfs = uos.VfsFat(bdev) - uos.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 gc -#import webrepl -#webrepl.start() -gc.collect() -""") - return vfs diff --git a/esp8266/modules/neopixel.py b/esp8266/modules/neopixel.py deleted file mode 100644 index b13424d7d8..0000000000 --- a/esp8266/modules/neopixel.py +++ /dev/null @@ -1,32 +0,0 @@ -# NeoPixel driver for MicroPython on ESP8266 -# MIT license; Copyright (c) 2016 Damien P. George - -from esp import neopixel_write - - -class NeoPixel: - ORDER = (1, 0, 2, 3) - - def __init__(self, pin, n, bpp=3): - self.pin = pin - self.n = n - self.bpp = bpp - self.buf = bytearray(n * bpp) - self.pin.init(pin.OUT) - - def __setitem__(self, index, val): - offset = index * self.bpp - for i in range(self.bpp): - self.buf[offset + self.ORDER[i]] = val[i] - - def __getitem__(self, index): - offset = index * self.bpp - return tuple(self.buf[offset + self.ORDER[i]] - for i in range(self.bpp)) - - def fill(self, color): - for i in range(self.n): - self[i] = color - - def write(self): - neopixel_write(self.pin, self.buf, True) diff --git a/esp8266/modules/ntptime.py b/esp8266/modules/ntptime.py deleted file mode 100644 index a97e08e60d..0000000000 --- a/esp8266/modules/ntptime.py +++ /dev/null @@ -1,36 +0,0 @@ -try: - import usocket as socket -except: - import socket -try: - import ustruct as struct -except: - import struct - -# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 -NTP_DELTA = 3155673600 - -host = "pool.ntp.org" - -def time(): - NTP_QUERY = bytearray(48) - NTP_QUERY[0] = 0x1b - addr = socket.getaddrinfo(host, 123)[0][-1] - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.settimeout(1) - res = s.sendto(NTP_QUERY, addr) - msg = s.recv(48) - s.close() - val = struct.unpack("!I", msg[40:44])[0] - return val - NTP_DELTA - -# There's currently no timezone support in MicroPython, so -# utime.localtime() will return UTC time (as if it was .gmtime()) -def settime(): - t = time() - import machine - import utime - tm = utime.localtime(t) - tm = tm[0:3] + (0,) + tm[3:6] + (0,) - machine.RTC().datetime(tm) - print(utime.localtime()) diff --git a/esp8266/modules/onewire.py b/esp8266/modules/onewire.py deleted file mode 120000 index f6ec745e85..0000000000 --- a/esp8266/modules/onewire.py +++ /dev/null @@ -1 +0,0 @@ -../../drivers/onewire/onewire.py
\ No newline at end of file diff --git a/esp8266/modules/port_diag.py b/esp8266/modules/port_diag.py deleted file mode 100644 index ef8800355a..0000000000 --- a/esp8266/modules/port_diag.py +++ /dev/null @@ -1,33 +0,0 @@ -import esp -import uctypes -import network -import lwip - - -def main(): - - ROM = uctypes.bytearray_at(0x40200000, 16) - fid = esp.flash_id() - - print("FlashROM:") - print("Flash ID: %x (Vendor: %x Device: %x)" % (fid, fid & 0xff, fid & 0xff00 | fid >> 16)) - - print("Flash bootloader data:") - SZ_MAP = {0: "512KB", 1: "256KB", 2: "1MB", 3: "2MB", 4: "4MB"} - FREQ_MAP = {0: "40MHZ", 1: "26MHZ", 2: "20MHz", 0xf: "80MHz"} - print("Byte @2: %02x" % ROM[2]) - print("Byte @3: %02x (Flash size: %s Flash freq: %s)" % (ROM[3], SZ_MAP.get(ROM[3] >> 4, "?"), FREQ_MAP.get(ROM[3] & 0xf))) - print("Firmware checksum:") - print(esp.check_fw()) - - print("\nNetworking:") - print("STA ifconfig:", network.WLAN(network.STA_IF).ifconfig()) - print("AP ifconfig:", network.WLAN(network.AP_IF).ifconfig()) - print("Free WiFi driver buffers of type:") - for i, comm in enumerate(("1,2 TX", "4 Mngmt TX(len: 0x41-0x100)", "5 Mngmt TX (len: 0-0x40)", "7", "8 RX")): - print("%d: %d (%s)" % (i, esp.esf_free_bufs(i), comm)) - print("lwIP PCBs:") - lwip.print_pcbs() - - -main() diff --git a/esp8266/modules/upip.py b/esp8266/modules/upip.py deleted file mode 120000 index 20d52a4ab7..0000000000 --- a/esp8266/modules/upip.py +++ /dev/null @@ -1 +0,0 @@ -../../tools/upip.py
\ No newline at end of file diff --git a/esp8266/modules/upip_utarfile.py b/esp8266/modules/upip_utarfile.py deleted file mode 120000 index 1498862916..0000000000 --- a/esp8266/modules/upip_utarfile.py +++ /dev/null @@ -1 +0,0 @@ -../../tools/upip_utarfile.py
\ No newline at end of file diff --git a/esp8266/modules/webrepl.py b/esp8266/modules/webrepl.py deleted file mode 100644 index 5a76e9b26d..0000000000 --- a/esp8266/modules/webrepl.py +++ /dev/null @@ -1,77 +0,0 @@ -# This module should be imported from REPL, not run from command line. -import socket -import uos -import network -import websocket -import websocket_helper -import _webrepl - -listen_s = None -client_s = None - -def setup_conn(port, accept_handler): - global listen_s - listen_s = socket.socket() - listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - ai = socket.getaddrinfo("0.0.0.0", port) - addr = ai[0][4] - - listen_s.bind(addr) - listen_s.listen(1) - if accept_handler: - listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler) - for i in (network.AP_IF, network.STA_IF): - iface = network.WLAN(i) - if iface.active(): - print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port)) - return listen_s - - -def accept_conn(listen_sock): - global client_s - cl, remote_addr = listen_sock.accept() - if uos.dupterm(): - print("\nConcurrent WebREPL connection from", remote_addr, "rejected") - cl.close() - return - print("\nWebREPL connection from:", remote_addr) - client_s = cl - websocket_helper.server_handshake(cl) - ws = websocket.websocket(cl, True) - ws = _webrepl._webrepl(ws) - cl.setblocking(False) - # notify REPL on socket incoming data - cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify) - uos.dupterm(ws) - - -def stop(): - global listen_s, client_s - uos.dupterm(None) - if client_s: - client_s.close() - if listen_s: - listen_s.close() - - -def start(port=8266, password=None): - stop() - if password is None: - try: - import webrepl_cfg - _webrepl.password(webrepl_cfg.PASS) - setup_conn(port, accept_conn) - print("Started webrepl in normal mode") - except: - print("WebREPL is not configured, run 'import webrepl_setup'") - else: - _webrepl.password(password) - setup_conn(port, accept_conn) - print("Started webrepl in manual override mode") - - -def start_foreground(port=8266): - stop() - s = setup_conn(port, None) - accept_conn(s) diff --git a/esp8266/modules/webrepl_setup.py b/esp8266/modules/webrepl_setup.py deleted file mode 100644 index d91600e6ec..0000000000 --- a/esp8266/modules/webrepl_setup.py +++ /dev/null @@ -1,111 +0,0 @@ -import sys -#import uos as os -import os -import machine - -RC = "./boot.py" -CONFIG = "./webrepl_cfg.py" - -def input_choice(prompt, choices): - while 1: - resp = input(prompt) - if resp in choices: - return resp - -def getpass(prompt): - return input(prompt) - -def input_pass(): - while 1: - passwd1 = getpass("New password: ") - if len(passwd1) < 4: - print("Password too short") - continue - elif len(passwd1) > 9: - print("Password too long") - continue - passwd2 = getpass("Confirm password: ") - if passwd1 == passwd2: - return passwd1 - print("Passwords do not match") - - -def exists(fname): - try: - with open(fname): - pass - return True - except OSError: - return False - -def copy_stream(s_in, s_out): - buf = bytearray(64) - while 1: - sz = s_in.readinto(buf) - s_out.write(buf, sz) - - -def get_daemon_status(): - with open(RC) as f: - for l in f: - if "webrepl" in l: - if l.startswith("#"): - return False - return True - return None - -def add_daemon(): - with open(RC) as old_f, open(RC + ".tmp", "w") as new_f: - new_f.write("import webrepl\nwebrepl.start()\n") - copy_stream(old_f, new_f) - -def change_daemon(action): - LINES = ("import webrepl", "webrepl.start()") - with open(RC) as old_f, open(RC + ".tmp", "w") as new_f: - for l in old_f: - for patt in LINES: - if patt in l: - if action and l.startswith("#"): - l = l[1:] - elif not action and not l.startswith("#"): - l = "#" + l - new_f.write(l) - # FatFs rename() is not POSIX compliant, will raise OSError if - # dest file exists. - os.remove(RC) - os.rename(RC + ".tmp", RC) - - -def main(): - status = get_daemon_status() - - print("WebREPL daemon auto-start status:", "enabled" if status else "disabled") - print("\nWould you like to (E)nable or (D)isable it running on boot?") - print("(Empty line to quit)") - resp = input("> ").upper() - - if resp == "E": - if exists(CONFIG): - resp2 = input_choice("Would you like to change WebREPL password? (y/n) ", ("y", "n", "")) - else: - print("To enable WebREPL, you must set password for it") - resp2 = "y" - - if resp2 == "y": - passwd = input_pass() - with open(CONFIG, "w") as f: - f.write("PASS = %r\n" % passwd) - - - if resp not in ("D", "E") or (resp == "D" and not status) or (resp == "E" and status): - print("No further action required") - sys.exit() - - change_daemon(resp == "E") - - print("Changes will be activated after reboot") - resp = input_choice("Would you like to reboot now? (y/n) ", ("y", "n", "")) - if resp == "y": - machine.reset() - -main() diff --git a/esp8266/modules/websocket_helper.py b/esp8266/modules/websocket_helper.py deleted file mode 100644 index 9c06db5023..0000000000 --- a/esp8266/modules/websocket_helper.py +++ /dev/null @@ -1,74 +0,0 @@ -import sys -try: - import ubinascii as binascii -except: - import binascii -try: - import uhashlib as hashlib -except: - import hashlib - -DEBUG = 0 - -def server_handshake(sock): - clr = sock.makefile("rwb", 0) - l = clr.readline() - #sys.stdout.write(repr(l)) - - webkey = None - - while 1: - l = clr.readline() - if not l: - raise OSError("EOF in headers") - if l == b"\r\n": - break - # sys.stdout.write(l) - h, v = [x.strip() for x in l.split(b":", 1)] - if DEBUG: - print((h, v)) - if h == b'Sec-WebSocket-Key': - webkey = v - - if not webkey: - raise OSError("Not a websocket request") - - if DEBUG: - print("Sec-WebSocket-Key:", webkey, len(webkey)) - - d = hashlib.sha1(webkey) - d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") - respkey = d.digest() - respkey = binascii.b2a_base64(respkey)[:-1] - if DEBUG: - print("respkey:", respkey) - - sock.send(b"""\ -HTTP/1.1 101 Switching Protocols\r -Upgrade: websocket\r -Connection: Upgrade\r -Sec-WebSocket-Accept: """) - sock.send(respkey) - sock.send("\r\n\r\n") - - -# Very simplified client handshake, works for MicroPython's -# websocket server implementation, but probably not for other -# servers. -def client_handshake(sock): - cl = sock.makefile("rwb", 0) - cl.write(b"""\ -GET / HTTP/1.1\r -Host: echo.websocket.org\r -Connection: Upgrade\r -Upgrade: websocket\r -Sec-WebSocket-Key: foo\r -\r -""") - l = cl.readline() -# print(l) - while 1: - l = cl.readline() - if l == b"\r\n": - break -# sys.stdout.write(l) |