diff --git a/README.md b/README.md index fcd094c..1c44510 100644 --- a/README.md +++ b/README.md @@ -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** + +--- \ No newline at end of file diff --git a/docs/img/_img_conncet-suc.JPG b/docs/img/_img_conncet-suc.JPG new file mode 100644 index 0000000..1fec97a Binary files /dev/null and b/docs/img/_img_conncet-suc.JPG differ diff --git a/docs/img/checkcode.jpg b/docs/img/checkcode.jpg index 0a3f5ab..751badf 100644 Binary files a/docs/img/checkcode.jpg and b/docs/img/checkcode.jpg differ diff --git a/docs/img/img_conncet-suc.JPG b/docs/img/img_conncet-suc.JPG index 1fec97a..2d4ef63 100644 Binary files a/docs/img/img_conncet-suc.JPG and b/docs/img/img_conncet-suc.JPG differ diff --git a/examples/DHT12/dht12.py b/examples/DHT12/dht12.py new file mode 100644 index 0000000..7a5e925 --- /dev/null +++ b/examples/DHT12/dht12.py @@ -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 \ No newline at end of file diff --git a/examples/DHT12/main.py b/examples/DHT12/main.py new file mode 100644 index 0000000..78bbf40 --- /dev/null +++ b/examples/DHT12/main.py @@ -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) \ No newline at end of file diff --git a/examples/DigitalClock/clock.py b/examples/DigitalClock/clock.py new file mode 100644 index 0000000..f3f36cf --- /dev/null +++ b/examples/DigitalClock/clock.py @@ -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, ()) \ No newline at end of file diff --git a/examples/DigitalClock/main.py b/examples/DigitalClock/main.py new file mode 100644 index 0000000..aaf9059 --- /dev/null +++ b/examples/DigitalClock/main.py @@ -0,0 +1,2 @@ +from m5stack import * +import clock \ No newline at end of file diff --git a/examples/FACES/faces.py b/examples/FACES/faces.py new file mode 100644 index 0000000..ef644e1 --- /dev/null +++ b/examples/FACES/faces.py @@ -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 diff --git a/examples/FACES/main.py b/examples/FACES/main.py new file mode 100644 index 0000000..77fe317 --- /dev/null +++ b/examples/FACES/main.py @@ -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) \ No newline at end of file diff --git a/examples/clock.py b/examples/clock.py deleted file mode 100644 index dd32689..0000000 --- a/examples/clock.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/examples/main.py b/examples/main.py deleted file mode 100644 index 694e2fa..0000000 --- a/examples/main.py +++ /dev/null @@ -1 +0,0 @@ -import clock diff --git a/firmwares/m5cloud-20180102-v0.2.5.bin b/firmwares/m5cloud-20180102-v0.2.5.bin new file mode 100644 index 0000000..3bf2e51 Binary files /dev/null and b/firmwares/m5cloud-20180102-v0.2.5.bin differ diff --git a/firmwares/m5cloud-psram-20180102-v0.2.5.bin b/firmwares/m5cloud-psram-20180102-v0.2.5.bin new file mode 100644 index 0000000..2988724 Binary files /dev/null and b/firmwares/m5cloud-psram-20180102-v0.2.5.bin differ diff --git a/lib/dht12.py b/lib/dht12.py new file mode 100644 index 0000000..7a5e925 --- /dev/null +++ b/lib/dht12.py @@ -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 \ No newline at end of file diff --git a/lib/faces.py b/lib/faces.py new file mode 100644 index 0000000..ef644e1 --- /dev/null +++ b/lib/faces.py @@ -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