2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-02-03 20:12:04 +01:00
|
|
|
""" Real Time Clock based on the nRF-family low power counter """
|
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
import time
|
|
|
|
|
2020-02-03 20:12:04 +01:00
|
|
|
#class Stim(object):
|
|
|
|
# def __init__(self):
|
|
|
|
# self(0)
|
|
|
|
#
|
|
|
|
# def __call__(self, v):
|
|
|
|
# self.c = v
|
|
|
|
#
|
|
|
|
# def counter(self):
|
|
|
|
# return self.c
|
|
|
|
|
|
|
|
class RTC(object):
|
2020-02-19 20:32:06 +01:00
|
|
|
"""Real Time Clock based on the nRF-family low power counter."""
|
2020-02-03 20:12:04 +01:00
|
|
|
|
|
|
|
def __init__(self, counter):
|
|
|
|
self.counter = counter
|
2020-04-18 16:34:49 +02:00
|
|
|
self._uptime = 0
|
|
|
|
self.set_localtime((2020, 3, 1, 3, 0, 0, 0, 0))
|
2020-02-03 20:12:04 +01:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
newcount = self.counter.counter()
|
|
|
|
split = newcount - self.lastcount
|
|
|
|
if split == 0:
|
|
|
|
return False
|
|
|
|
if split < 0:
|
|
|
|
split += (1 << 24)
|
2020-04-18 16:34:49 +02:00
|
|
|
|
|
|
|
self.lastcount += split
|
2020-02-03 20:12:04 +01:00
|
|
|
self.lastcount &= (1 << 24) - 1
|
2020-04-18 16:34:49 +02:00
|
|
|
self._uptime += split
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
return True
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
def set_localtime(self, t):
|
|
|
|
self.lastcount = self.counter.counter()
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
if len(t) < 8:
|
|
|
|
yyyy = t[0]
|
|
|
|
mm = t[1]
|
|
|
|
dd = t[2]
|
|
|
|
HH = t[3]
|
|
|
|
MM = t[4]
|
|
|
|
SS = t[5]
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
t = (yyyy, mm, dd, HH, MM, SS, 0, 0)
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
lt = time.mktime(t)
|
2020-04-18 16:34:49 +02:00
|
|
|
self.offset = lt - self._uptime
|
2020-02-03 20:12:04 +01:00
|
|
|
|
2020-02-19 20:32:06 +01:00
|
|
|
def get_localtime(self):
|
2020-02-03 20:12:04 +01:00
|
|
|
self.update()
|
2020-04-18 16:34:49 +02:00
|
|
|
return time.localtime(self.offset + (self._uptime >> 3))
|
2020-02-19 20:32:06 +01:00
|
|
|
|
|
|
|
def get_time(self):
|
|
|
|
localtime = self.get_localtime()
|
|
|
|
return localtime[3:6]
|
2020-03-07 12:49:35 +01:00
|
|
|
|
2020-04-18 16:34:49 +02:00
|
|
|
@property
|
|
|
|
def uptime(self):
|
|
|
|
"""Provide the current uptime in seconds."""
|
|
|
|
return self._uptime // 8
|
|
|
|
|
2020-03-07 12:49:35 +01:00
|
|
|
def get_uptime_ms(self):
|
|
|
|
"""Return the current uptime in milliseconds."""
|
2020-04-18 16:34:49 +02:00
|
|
|
return self._uptime * 125
|