This commit is contained in:
0x1abin
2018-01-02 19:19:31 +08:00
parent 19e1e88fb6
commit 1f1941ee1b
16 changed files with 178 additions and 13 deletions
+30
View File
@@ -360,3 +360,33 @@ import machine
adc = machine.ADC(35)
adc.read()
```
## **I2C**
---
```python
from machine import I2C
i2c = I2C(freq=400000) # create I2C peripheral at frequency of 400kHz
# depending on the port, extra parameters may be required
# to select the peripheral and/or pins to use
i2c.scan() # scan for slaves, returning a list of 7-bit addresses
i2c.writeto(42, b'123') # write 3 bytes to slave with 7-bit address 42
i2c.readfrom(42, 4) # read 4 bytes from slave with 7-bit address 42
i2c.readfrom_mem(42, 8, 3) # read 3 bytes from memory of slave 42,
# starting at memory-address 8 in the slave
i2c.writeto_mem(42, 2, b'\x10') # write 1 byte to memory of slave 42
# starting at address 2 in the slave
```
## **SPI**
---
## **UART**
---
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 89 KiB

+28
View File
@@ -0,0 +1,28 @@
"""
MicroPython Aosong DHT12 I2C driver
"""
class DHTBaseI2C:
def __init__(self, i2c=None, addr=0x5c):
if i2c == None:
from machine import I2C
self.i2c = I2C()
else:
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
+8
View File
@@ -0,0 +1,8 @@
import dht12, time
sensor = dht12.DHT12()
while True:
sensor.measure()
print("Temperature:%.2f" % (sensor.temperature()))
print("Humidity:%.2f \n" % (sensor.humidity()))
time.sleep(1)
+16
View File
@@ -0,0 +1,16 @@
from m5stack import *
import time, _thread
def clock():
lcd.clear()
lcd.font(lcd.FONT_7seg, fixedwidth=True, dist=16, width=2)
lcd.setBrightness(200)
while True:
d = time.strftime("%Y-%m-%d", time.localtime())
t = time.strftime("%H:%M:%S", time.localtime())
lcd.print(d, lcd.CENTER, 50, lcd.ORANGE)
lcd.print(t, lcd.CENTER, 130, lcd.ORANGE)
time.sleep(1)
_thread.start_new_thread('Clock', clock, ())
+2
View File
@@ -0,0 +1,2 @@
from m5stack import *
import clock
+25
View File
@@ -0,0 +1,25 @@
"""
M5Stack MicroPython FACES keyboard I2C driver
"""
class Faces:
def __init__(self):
from machine import I2C
self.i2c = I2C()
self.addr = 0x08
self.cb = None
def read(self):
return self.i2c.readfrom(self.addr, 1)
def _callback(self, pin):
from machine import Pin
if pin == Pin(5):
self.cb(self.read())
def callback(self, cb):
from machine import Pin
self.pin = Pin(5)
self.pin.init(Pin.IN)
self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self._callback)
self.cb = cb
+16
View File
@@ -0,0 +1,16 @@
from m5stack import *
import faces
keyboard = faces.Faces()
# read once
print("Key value:", end='')
print(keyboard.read())
# callback
def keyboard_cb(value):
print("Key value:", end='')
print(value)
lcd.print(value)
keyboard.callback(keyboard_cb)
-12
View File
@@ -1,12 +0,0 @@
from m5stack import *
import time
lcd.font(lcd.FONT_7seg, fixedwidth=True, dist=16, width=2)
lcd.setBrightness(200)
while True:
d = time.strftime("%Y-%m-%d", time.localtime())
t = time.strftime("%H:%M:%S", time.localtime())
lcd.print(d, lcd.CENTER, 50, lcd.ORANGE)
lcd.print(t, lcd.CENTER, 130, lcd.ORANGE)
time.sleep(1)
-1
View File
@@ -1 +0,0 @@
import clock
Binary file not shown.
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
"""
MicroPython Aosong DHT12 I2C driver
"""
class DHTBaseI2C:
def __init__(self, i2c=None, addr=0x5c):
if i2c == None:
from machine import I2C
self.i2c = I2C()
else:
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
+25
View File
@@ -0,0 +1,25 @@
"""
M5Stack MicroPython FACES keyboard I2C driver
"""
class Faces:
def __init__(self):
from machine import I2C
self.i2c = I2C()
self.addr = 0x08
self.cb = None
def read(self):
return self.i2c.readfrom(self.addr, 1)
def _callback(self, pin):
from machine import Pin
if pin == Pin(5):
self.cb(self.read())
def callback(self, cb):
from machine import Pin
self.pin = Pin(5)
self.pin.init(Pin.IN)
self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self._callback)
self.cb = cb