Skip to content

Commit

Permalink
readings now synched to clock time instead of relative to last boot
Browse files Browse the repository at this point in the history
  • Loading branch information
lowfatcode committed Sep 2, 2022
1 parent 2c6be5c commit f861605
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
35 changes: 25 additions & 10 deletions enviro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def stop_activity_led():
# intialise the pcf85063a real time clock chip
rtc = PCF85063A(i2c)
i2c.writeto_mem(0x51, 0x00, b'\x00') # ensure rtc is running (this should be default?)
rtc.enable_timer_interrupt(False)

t = rtc.datetime()
RTC().datetime((t[0], t[1], t[2], t[6], t[3], t[4], t[5], 0)) # synch PR2040 rtc too

Expand Down Expand Up @@ -156,7 +158,7 @@ def wake_reason():
def halt(message):
logging.error(message)
warn_led(WARN_LED_BLINK)
sleep(config.reading_frequency)
sleep()

# returns True if we've used up 90% of the internal filesystem
def low_disk_space():
Expand Down Expand Up @@ -310,19 +312,32 @@ def startup():
helpers.mkdir_safe("readings")
helpers.mkdir_safe("uploads")

def sleep(minutes = -1):
def sleep():
logging.info("> going to sleep")

# make sure the rtc flags are cleared before going back to sleep
logging.debug(" - clearing rtc alarm flags")
rtc.clear_timer_flag()
logging.debug(" - clearing and disabling timer and alarm")
rtc.clear_alarm_flag()

# if wake time supplied then set rtc timer
if minutes != -1:
logging.info(f" - setting timer to wake in {minutes} minutes")
rtc.enable_timer_interrupt(True)
rtc.set_timer(minutes, PCF85063A.TIMER_TICK_1_OVER_60HZ)
# set alarm to wake us up for next reading
dt = rtc.datetime()
hour, minute = dt[3:5]

# calculate how many minutes into the day we are
minute = math.floor(minute / config.reading_frequency) * config.reading_frequency
minute += config.reading_frequency
while minute >= 60:
minute -= 60
hour += 1
if hour >= 24:
hour -= 24
ampm = "am" if hour < 12 else "pm"

logging.info(f" - setting alarm to wake at {hour:02}:{minute:02}{ampm}")

# sleep until next scheduled reading
rtc.set_alarm(0, minute, hour)
rtc.enable_alarm_interrupt(True)

# disable the vsys hold, causing us to turn off
logging.info(" - shutting down")
Expand All @@ -339,7 +354,7 @@ def sleep(minutes = -1):

# we'll wait here until the rtc timer triggers and then reset the board
logging.debug(" - on usb power (so can't shutdown) halt and reset instead")
while not rtc.read_timer_flag():
while not rtc.read_alarm_flag():
time.sleep(0.25)

if button_pin.value(): # allow button to force reset
Expand Down
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#
# - the Pimoroni pirate crew

# import enviro firmware
# import enviro firmware, this will trigger provisioning if needed
import enviro

# initialise enviro, this will trigger provisioning if needed
# initialise enviro
enviro.startup()

# now that we know the device is provisioned import the config
Expand Down Expand Up @@ -65,4 +65,4 @@
enviro.halt("! reading upload failed")

# go to sleep until our next scheduled reading
enviro.sleep(config.reading_frequency)
enviro.sleep()

0 comments on commit f861605

Please sign in to comment.