summaryrefslogtreecommitdiffstatshomepage
path: root/esp8266/modules
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-05-12 17:57:23 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-05-12 17:57:23 +0300
commite9308c189af388525becbcd42b81ea6d37738135 (patch)
tree9976c0b87b77e06ae40c52443f2c0d1e0acce7ad /esp8266/modules
parent5f7ce2a1ca65840ef95d0c044088bc38453b7471 (diff)
downloadmicropython-e9308c189af388525becbcd42b81ea6d37738135.tar.gz
micropython-e9308c189af388525becbcd42b81ea6d37738135.zip
esp8266/scripts: Move drivers/modules to modules/ (frozen bytecode).
Diffstat (limited to 'esp8266/modules')
-rw-r--r--esp8266/modules/apa102.py17
-rw-r--r--esp8266/modules/dht.py32
-rw-r--r--esp8266/modules/neopixel.py32
-rw-r--r--esp8266/modules/ntptime.py36
4 files changed, 117 insertions, 0 deletions
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())