kopia lustrzana https://github.com/peterhinch/micropython-samples
DS3231/ds3231_port fixed for official ESP32 firmware.
rodzic
4d2a52d109
commit
e2fc55740e
|
@ -9,17 +9,10 @@ import machine
|
||||||
import sys
|
import sys
|
||||||
DS3231_I2C_ADDR = 104
|
DS3231_I2C_ADDR = 104
|
||||||
|
|
||||||
class DS3231Exception(OSError):
|
try:
|
||||||
pass
|
|
||||||
|
|
||||||
if sys.platform == 'pyboard':
|
|
||||||
import pyb
|
|
||||||
rtc = pyb.RTC()
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
rtc = machine.RTC()
|
rtc = machine.RTC()
|
||||||
except: # Official ESP32 port
|
except:
|
||||||
print('warning: machine module does not support the RTC.')
|
print('Warning: machine module does not support the RTC.')
|
||||||
rtc = None
|
rtc = None
|
||||||
|
|
||||||
def bcd2dec(bcd):
|
def bcd2dec(bcd):
|
||||||
|
@ -37,9 +30,9 @@ class DS3231:
|
||||||
self.ds3231 = i2c
|
self.ds3231 = i2c
|
||||||
self.timebuf = bytearray(7)
|
self.timebuf = bytearray(7)
|
||||||
if DS3231_I2C_ADDR not in self.ds3231.scan():
|
if DS3231_I2C_ADDR not in self.ds3231.scan():
|
||||||
raise DS3231Exception("DS3231 not found on I2C bus at %d" % DS3231_I2C_ADDR)
|
raise RuntimeError("DS3231 not found on I2C bus at %d" % DS3231_I2C_ADDR)
|
||||||
|
|
||||||
def get_time(self, set_rtc = False):
|
def get_time(self, set_rtc=False):
|
||||||
if set_rtc:
|
if set_rtc:
|
||||||
self.await_transition() # For accuracy set RTC immediately after a seconds transition
|
self.await_transition() # For accuracy set RTC immediately after a seconds transition
|
||||||
else:
|
else:
|
||||||
|
@ -72,10 +65,7 @@ class DS3231:
|
||||||
secs = utime.mktime(result)
|
secs = utime.mktime(result)
|
||||||
utime.localtime(secs)
|
utime.localtime(secs)
|
||||||
else:
|
else:
|
||||||
if sys.platform == 'pyboard' or sys.platform == 'esp8266':
|
|
||||||
rtc.datetime((YY, MM, DD, wday, hh, mm, ss, 0))
|
rtc.datetime((YY, MM, DD, wday, hh, mm, ss, 0))
|
||||||
else:
|
|
||||||
rtc.init((YY, MM, DD, hh, mm, ss))
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def save_time(self):
|
def save_time(self):
|
||||||
|
|
Ładowanie…
Reference in New Issue