Skip to content

Commit 6b2f868

Browse files
committed
add zhiwu idas system
1 parent 9ceae2b commit 6b2f868

4 files changed

Lines changed: 424 additions & 1 deletion

File tree

07.sensors/dht11.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# DHT11/DHT22 driver for MicroPython on ESP8266
2+
# MIT license; Copyright (c) 2016 Damien P. George
3+
4+
try:
5+
from esp import dht_readinto
6+
except:
7+
from pyb import dht_readinto
8+
9+
class DHTBase:
10+
def __init__(self, pin):
11+
self.pin = pin
12+
self.buf = bytearray(5)
13+
14+
def measure(self):
15+
buf = self.buf
16+
dht_readinto(self.pin, buf)
17+
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
18+
raise Exception("checksum error")
19+
20+
class DHT11(DHTBase):
21+
def humidity(self):
22+
return self.buf[0]
23+
24+
def temperature(self):
25+
return self.buf[2]
26+
27+
class DHT22(DHTBase):
28+
def humidity(self):
29+
return (self.buf[0] << 8 | self.buf[1]) * 0.1
30+
31+
def temperature(self):
32+
t = ((self.buf[2] & 0x7f) << 8 | self.buf[3]) * 0.1
33+
if self.buf[2] & 0x80:
34+
t = -t
35+
return t
36+
37+
if __name__ == "__main__":
38+
from machine import Pin
39+
import DHTsensor
40+
41+
dht = DHTsensor.DHT11(Pin(18))
42+
43+
dht.measure()
44+
print("temperature:",dht.temperature())
45+
print("humidity:",dht.humidity())

07.sensors/sh1106.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#
2+
# MicroPython SH1106 OLED driver, I2C and SPI interfaces
3+
#
4+
# The MIT License (MIT)
5+
#
6+
# Copyright (c) 2016 Radomir Dopieralski (@deshipu),
7+
# 2017 Robert Hammelrath (@robert-hh)
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in
17+
# all copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
# THE SOFTWARE.
26+
#
27+
# Sample code sections
28+
# ------------ SPI ------------------
29+
# Pin Map SPI
30+
# - 3v - xxxxxx - Vcc
31+
# - G - xxxxxx - Gnd
32+
# - D7 - GPIO 13 - Din / MOSI fixed
33+
# - D5 - GPIO 14 - Clk / Sck fixed
34+
# - D8 - GPIO 4 - CS (optional, if the only connected device)
35+
# - D2 - GPIO 5 - D/C
36+
# - D1 - GPIO 2 - Res
37+
#
38+
# for CS, D/C and Res other ports may be chosen.
39+
#
40+
# from machine import Pin, SPI
41+
# import sh1106
42+
43+
# spi = SPI(1, baudrate=1000000)
44+
# display = sh1106.SH1106_SPI(128, 64, spi, Pin(5), Pin(2), Pin(4))
45+
# display.sleep(False)
46+
# display.fill(0)
47+
# display.text('Testing 1', 0, 0, 1)
48+
# display.show()
49+
#
50+
# --------------- I2C ------------------
51+
#
52+
# Pin Map I2C
53+
# - 3v - xxxxxx - Vcc
54+
# - G - xxxxxx - Gnd
55+
# - D2 - GPIO 5 - SCK / SCL
56+
# - D1 - GPIO 4 - DIN / SDA
57+
# - D0 - GPIO 16 - Res
58+
# - G - xxxxxx CS
59+
# - G - xxxxxx D/C
60+
#
61+
# Pin's for I2C can be set almost arbitrary
62+
#
63+
# from machine import Pin, I2C
64+
# import sh1106
65+
#
66+
# i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
67+
# display = sh1106.SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)
68+
# display.sleep(False)
69+
# display.fill(0)
70+
# display.text('Testing 1', 0, 0, 1)
71+
# display.show()
72+
73+
from micropython import const
74+
import utime as time
75+
import framebuf
76+
77+
# a few register definitions
78+
_SET_CONTRAST = const(0x81)
79+
_SET_NORM_INV = const(0xa6)
80+
_SET_DISP = const(0xae)
81+
_SET_SCAN_DIR = const(0xc0)
82+
_SET_SEG_REMAP = const(0xa0)
83+
_LOW_COLUMN_ADDRESS = const(0x00)
84+
_HIGH_COLUMN_ADDRESS = const(0x10)
85+
_SET_PAGE_ADDRESS = const(0xB0)
86+
87+
88+
class SH1106:
89+
def __init__(self, width, height, external_vcc):
90+
self.width = width
91+
self.height = height
92+
self.external_vcc = external_vcc
93+
self.pages = self.height // 8
94+
self.buffer = bytearray(self.pages * self.width)
95+
fb = framebuf.FrameBuffer(self.buffer, self.width, self.height,
96+
framebuf.MVLSB)
97+
self.framebuf = fb
98+
# set shortcuts for the methods of framebuf
99+
self.fill = fb.fill
100+
self.fill_rect = fb.fill_rect
101+
self.hline = fb.hline
102+
self.vline = fb.vline
103+
self.line = fb.line
104+
self.rect = fb.rect
105+
self.pixel = fb.pixel
106+
self.scroll = fb.scroll
107+
self.text = fb.text
108+
self.blit = fb.blit
109+
110+
self.init_display()
111+
112+
def init_display(self):
113+
self.reset()
114+
self.fill(0)
115+
self.poweron()
116+
self.show()
117+
118+
def poweroff(self):
119+
self.write_cmd(_SET_DISP | 0x00)
120+
121+
def poweron(self):
122+
self.write_cmd(_SET_DISP | 0x01)
123+
124+
def rotate(self, flag, update=True):
125+
if flag:
126+
self.write_cmd(_SET_SEG_REMAP | 0x01) # mirror display vertically
127+
self.write_cmd(_SET_SCAN_DIR | 0x08) # mirror display hor.
128+
else:
129+
self.write_cmd(_SET_SEG_REMAP | 0x00)
130+
self.write_cmd(_SET_SCAN_DIR | 0x00)
131+
if update:
132+
self.show()
133+
134+
def sleep(self, value):
135+
self.write_cmd(_SET_DISP | (not value))
136+
137+
def contrast(self, contrast):
138+
self.write_cmd(_SET_CONTRAST)
139+
self.write_cmd(contrast)
140+
141+
def invert(self, invert):
142+
self.write_cmd(_SET_NORM_INV | (invert & 1))
143+
144+
def show(self):
145+
for page in range(self.height // 8):
146+
self.write_cmd(_SET_PAGE_ADDRESS | page)
147+
self.write_cmd(_LOW_COLUMN_ADDRESS | 2)
148+
self.write_cmd(_HIGH_COLUMN_ADDRESS | 0)
149+
self.write_data(self.buffer[
150+
self.width * page:self.width * page + self.width
151+
])
152+
153+
def reset(self, res):
154+
if res is not None:
155+
res(1)
156+
time.sleep_ms(1)
157+
res(0)
158+
time.sleep_ms(20)
159+
res(1)
160+
time.sleep_ms(20)
161+
162+
163+
class SH1106_I2C(SH1106):
164+
def __init__(self, width, height, i2c, res=None, addr=0x3c,
165+
external_vcc=False):
166+
self.i2c = i2c
167+
self.addr = addr
168+
self.res = res
169+
self.temp = bytearray(2)
170+
if hasattr(self.i2c, "start"):
171+
self.write_data = self.sw_write_data
172+
else:
173+
self.write_data = self.hw_write_data
174+
if res is not None:
175+
res.init(res.OUT, value=1)
176+
super().__init__(width, height, external_vcc)
177+
178+
def write_cmd(self, cmd):
179+
self.temp[0] = 0x80 # Co=1, D/C#=0
180+
self.temp[1] = cmd
181+
self.i2c.writeto(self.addr, self.temp)
182+
183+
def hw_write_data(self, buf):
184+
self.i2c.writeto(self.addr, b'\x40' + buf)
185+
186+
def sw_write_data(self, buf):
187+
self.temp[0] = self.addr << 1
188+
self.temp[1] = 0x40 # Co=0, D/C#=1
189+
self.i2c.start()
190+
self.i2c.write(self.temp)
191+
self.i2c.write(buf)
192+
self.i2c.stop()
193+
194+
def reset(self):
195+
super().reset(self.res)
196+
197+
198+
class SH1106_SPI(SH1106):
199+
def __init__(self, width, height, spi, dc, res=None, cs=None,
200+
external_vcc=False):
201+
self.rate = 10 * 1000 * 1000
202+
dc.init(dc.OUT, value=0)
203+
if res is not None:
204+
res.init(res.OUT, value=0)
205+
if cs is not None:
206+
cs.init(cs.OUT, value=1)
207+
self.spi = spi
208+
self.dc = dc
209+
self.res = res
210+
self.cs = cs
211+
super().__init__(width, height, external_vcc)
212+
213+
def write_cmd(self, cmd):
214+
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
215+
if self.cs is not None:
216+
self.cs(1)
217+
self.dc(0)
218+
self.cs(0)
219+
self.spi.write(bytearray([cmd]))
220+
self.cs(1)
221+
else:
222+
self.dc(0)
223+
self.spi.write(bytearray([cmd]))
224+
225+
def write_data(self, buf):
226+
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
227+
if self.cs is not None:
228+
self.cs(1)
229+
self.dc(1)
230+
self.cs(0)
231+
self.spi.write(buf)
232+
self.cs(1)
233+
else:
234+
self.dc(1)
235+
self.spi.write(buf)
236+
237+
def reset(self):
238+
super().reset(self.res)

0 commit comments

Comments
 (0)