From e9308c189af388525becbcd42b81ea6d37738135 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 12 May 2017 17:57:23 +0300 Subject: esp8266/scripts: Move drivers/modules to modules/ (frozen bytecode). --- esp8266/modules/apa102.py | 17 +++++++++++++++++ esp8266/modules/dht.py | 32 ++++++++++++++++++++++++++++++++ esp8266/modules/neopixel.py | 32 ++++++++++++++++++++++++++++++++ esp8266/modules/ntptime.py | 36 ++++++++++++++++++++++++++++++++++++ esp8266/scripts/apa102.py | 17 ----------------- esp8266/scripts/dht.py | 32 -------------------------------- esp8266/scripts/neopixel.py | 32 -------------------------------- esp8266/scripts/ntptime.py | 36 ------------------------------------ 8 files changed, 117 insertions(+), 117 deletions(-) create mode 100644 esp8266/modules/apa102.py create mode 100644 esp8266/modules/dht.py create mode 100644 esp8266/modules/neopixel.py create mode 100644 esp8266/modules/ntptime.py delete mode 100644 esp8266/scripts/apa102.py delete mode 100644 esp8266/scripts/dht.py delete mode 100644 esp8266/scripts/neopixel.py delete mode 100644 esp8266/scripts/ntptime.py diff --git a/esp8266/modules/apa102.py b/esp8266/modules/apa102.py new file mode 100644 index 0000000000..41b7c0485c --- /dev/null +++ b/esp8266/modules/apa102.py @@ -0,0 +1,17 @@ +# 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 new file mode 100644 index 0000000000..9a69e7e07e --- /dev/null +++ b/esp8266/modules/dht.py @@ -0,0 +1,32 @@ +# 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/neopixel.py b/esp8266/modules/neopixel.py new file mode 100644 index 0000000000..b13424d7d8 --- /dev/null +++ b/esp8266/modules/neopixel.py @@ -0,0 +1,32 @@ +# 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 new file mode 100644 index 0000000000..a97e08e60d --- /dev/null +++ b/esp8266/modules/ntptime.py @@ -0,0 +1,36 @@ +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/scripts/apa102.py b/esp8266/scripts/apa102.py deleted file mode 100644 index 41b7c0485c..0000000000 --- a/esp8266/scripts/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/scripts/dht.py b/esp8266/scripts/dht.py deleted file mode 100644 index 9a69e7e07e..0000000000 --- a/esp8266/scripts/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/scripts/neopixel.py b/esp8266/scripts/neopixel.py deleted file mode 100644 index b13424d7d8..0000000000 --- a/esp8266/scripts/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/scripts/ntptime.py b/esp8266/scripts/ntptime.py deleted file mode 100644 index a97e08e60d..0000000000 --- a/esp8266/scripts/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()) -- cgit v1.2.3