/ adafruit_register / i2c_bcd_datetime.py
i2c_bcd_datetime.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  # pylint: disable=too-few-public-methods
 23  """
 24  `adafruit_register.i2c_bcd_datetime`
 25  ====================================================
 26  
 27  Binary Coded Decimal date and time register
 28  
 29  * Author(s): Scott Shawcroft
 30  """
 31  
 32  __version__ = "0.0.0-auto.0"
 33  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
 34  
 35  import time
 36  
 37  
 38  def _bcd2bin(value):
 39      """Convert binary coded decimal to Binary
 40  
 41      :param value: the BCD value to convert to binary (required, no default)
 42      """
 43      return value - 6 * (value >> 4)
 44  
 45  
 46  def _bin2bcd(value):
 47      """Convert a binary value to binary coded decimal.
 48  
 49      :param value: the binary value to convert to BCD. (required, no default)
 50      """
 51      return value + 6 * (value // 10)
 52  
 53  
 54  class BCDDateTimeRegister:
 55      """
 56      Date and time register using binary coded decimal structure.
 57  
 58      The byte order of the register must* be: second, minute, hour, weekday, day (1-31), month, year
 59      (in years after 2000).
 60  
 61      * Setting weekday_first=False will flip the weekday/day order so that day comes first.
 62  
 63      Values are `time.struct_time`
 64  
 65      :param int register_address: The register address to start the read
 66      :param bool weekday_first: True if weekday is in a lower register than the day of the month
 67          (1-31)
 68      :param int weekday_start: 0 or 1 depending on the RTC's representation of the first day of the
 69          week
 70      """
 71  
 72      def __init__(self, register_address, weekday_first=True, weekday_start=1):
 73          self.buffer = bytearray(8)
 74          self.buffer[0] = register_address
 75          if weekday_first:
 76              self.weekday_offset = 0
 77          else:
 78              self.weekday_offset = 1
 79          self.weekday_start = weekday_start
 80  
 81      def __get__(self, obj, objtype=None):
 82          # Read and return the date and time.
 83          with obj.i2c_device as i2c:
 84              i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
 85          return time.struct_time(
 86              (
 87                  _bcd2bin(self.buffer[7]) + 2000,
 88                  _bcd2bin(self.buffer[6]),
 89                  _bcd2bin(self.buffer[5 - self.weekday_offset]),
 90                  _bcd2bin(self.buffer[3]),
 91                  _bcd2bin(self.buffer[2]),
 92                  _bcd2bin(self.buffer[1] & 0x7F),
 93                  _bcd2bin(self.buffer[4 + self.weekday_offset] - self.weekday_start),
 94                  -1,
 95                  -1,
 96              )
 97          )
 98  
 99      def __set__(self, obj, value):
100          self.buffer[1] = _bin2bcd(value.tm_sec) & 0x7F  # format conversions
101          self.buffer[2] = _bin2bcd(value.tm_min)
102          self.buffer[3] = _bin2bcd(value.tm_hour)
103          self.buffer[4 + self.weekday_offset] = _bin2bcd(
104              value.tm_wday + self.weekday_start
105          )
106          self.buffer[5 - self.weekday_offset] = _bin2bcd(value.tm_mday)
107          self.buffer[6] = _bin2bcd(value.tm_mon)
108          self.buffer[7] = _bin2bcd(value.tm_year - 2000)
109          with obj.i2c_device:
110              obj.i2c_device.write(self.buffer)