diff --git a/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/m5stack.py b/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/m5stack.py index c7c92b7..cdbab89 100644 --- a/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/m5stack.py +++ b/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/m5stack.py @@ -1,7 +1,9 @@ -import uos as os import utime as time import display as lcd -import machine, ubinascii +import machine +import ubinascii +import axp192 +import rtc from ubutton import Button from micropython import const @@ -25,15 +27,17 @@ print('\nDevice ID:' + node_id) print('LCD initializing...') # pin Analog and digital -import axp192 axp = axp192.Axp192() axp.powerAll() +# RTC +rtc = rtc.RTC() + # LCD lcd = lcd.TFT() lcd.init(lcd.M5STICK, speed=30000000, bgr=True, mosi=15, miso=36, clk=13, cs=5, dc=23, rst_pin=18, width=80, height=160) -#BUTTON +# BUTTON m5button = Button() buttonA = m5button.register(_BUTTON_A_PIN) buttonB = m5button.register(_BUTTON_B_PIN) \ No newline at end of file diff --git a/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/rtc.py b/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/rtc.py new file mode 100644 index 0000000..25c54db --- /dev/null +++ b/MicroPython_BUILD/components/micropython/esp32/modules/m5flow/rtc.py @@ -0,0 +1,79 @@ +import ustruct +import i2c_bus + + +class RTC: + def __init__(self): + self.addr = 0x51 + self.i2c = i2c_bus.get(i2c_bus.M_BUS) + + def getTime(self): + buf = self._regChar(0x02, buf=bytearray(3)) + + seconds = self.bcd2ToByte(buf[0] & 0x7f) + minutes = self.bcd2ToByte(buf[1] & 0x7f) + hours = self.bcd2ToByte(buf[2] & 0x3f) + + return hours, minutes, seconds + + def setTime(self, hours, minutes, seconds): + seconds = self.byteToBcd2(seconds) + minutes = self.byteToBcd2(minutes) + hours = self.byteToBcd2(hours) + + self._regChar(0x02, (seconds, minutes, hours), buf=bytearray(3)) + + def getDate(self): + buf = self._regChar(0x05, buf=bytearray(4)) + + date = self.bcd2ToByte(buf[0] & 0x3f) + week_day = self.bcd2ToByte(buf[1] & 0x07) + month = self.bcd2ToByte(buf[2] & 0x1f) + + if buf[2] & 0x80: + year = 1900 + self.bcd2ToByte(buf[3] & 0xff) + else: + year = 2000 + self.bcd2ToByte(buf[3] & 0xff) + + return year, month, date, week_day + + def setDate(self, year, month, date, week_day): + date = self.byteToBcd2(date) + week_day = self.byteToBcd2(week_day) + + if year < 2000: + month = self.byteToBcd2(month | 0x80) + else: + month = self.byteToBcd2(month | 0x00) + + year = self.byteToBcd2(year % 100) + + self._regChar(0x05, (date, week_day, month, year), buf=bytearray(4)) + + def _regChar(self, reg, value=None, buf=bytearray(1)): + if value is None: + self.i2c.readfrom_mem_into(self.addr, reg, buf) + if len(buf) == 1: + return buf[0] + else: + return buf + + if type(value) is int: + ustruct.pack_into('> 0x4) * 10 + return tmp + (value & 0x0F) + + def byteToBcd2(self, value): + bcdhigh = 0 + + while value >= 10: + bcdhigh += 1 + value -= 10 + + return (bcdhigh << 4) | value