1
2
3
4 import smbus
5
7 '''
8 Converts byte interpreted as two digit bcd to integer
9 (e.g. 88 = b01011000->bcd0101'1000 = 58)
10 '''
11 out = 0
12 for d in (bcd >> 4, bcd):
13 for p in (1, 2, 4 ,8):
14 if d & 1:
15 out += p
16 d >>= 1
17 out *= 10
18 return out // 10
19
21 '''
22 Converts integer 0..99 to byte interpreted in two digit bcd format.
23 (e.g. 58 = bcd0101'1000->b01011000 = 88)
24 '''
25 bcd = 0
26 for i in (n // 10, n % 10):
27 for p in (8, 4, 2, 1):
28 if i >= p:
29 bcd += 1
30 i -= p
31 bcd <<= 1
32 return bcd >> 1
33
35 _REG_SECONDS = 0x00
36 _REG_MINUTES = 0x01
37 _REG_HOURS = 0x02
38 _REG_DOW = 0x03
39 _REG_DAY = 0x04
40 _REG_MONTH = 0x05
41 _REG_YEAR = 0x06
42 _REG_CONTROL = 0x07
43
44 - def __init__(self, type = 1, addr = 0x68):
45 '''
46 Creates a Real Time Clock abstraction using given SMBus type and I2C address
47 @param type: 0 for RPi model A, 1 for higher versions (default: 1)
48 @param addr: I2C address (default: 0x68)
49 '''
50 self._bus = smbus.SMBus(type)
51 self._addr = addr
52
53 - def _write(self, register, data):
54 self._bus.write_byte_data(self._addr, register, data)
55
56
58 returndata = self._bus.read_byte_data(self._addr, data)
59 return returndata
60
62 '''
63 Returns current seconds.
64 @return: seconds of current date/time (0..59)
65 '''
66 return bcdToInt(self._read(self._REG_SECONDS))
67
69 '''
70 Returns current minutes.
71 @return: minutes of current date/time (0..59)
72 '''
73 return bcdToInt(self._read(self._REG_MINUTES))
74
76 '''
77 Returns current hours.
78 @return: hours of current date/time (0..23)
79 '''
80 d = self._read(self._REG_HOURS)
81 if (d == 0x64):
82 d = 0x40
83 return bcdToInt(d & 0x3F)
84
86 '''
87 Returns current day of week.
88 @return: day number of current date/time (1..7, 1 for Monday)
89 '''
90 return bcdToInt(self._read(self._REG_DOW))
91
93 '''
94 Returns current day of month.
95 @return: day of current date/time (1..31)
96 '''
97 return bcdToInt(self._read(self._REG_DAY))
98
100 '''
101 Returns current month.
102 @return: month of current date/time (1..12)
103 '''
104 return bcdToInt(self._read(self._REG_MONTH))
105
107 '''
108 Returns current year.
109 @return: year of current date/time (0..99)
110 '''
111 return bcdToInt(self._read(self._REG_YEAR))
112
113 - def setDate(self, seconds = None, minutes = None, hours = None, dow = None,
114 day = None, month = None, year = None):
115 '''
116 Sets the current date/time.
117 Range: seconds [0,59], minutes [0,59], hours [0,23],
118 day_of_week [1,7], day [1-31], month [1-12], year [0-99].
119 If a parameter is None (default), the current value is unchanged
120 '''
121 if seconds is not None:
122 if seconds < 0 or seconds > 59:
123 raise ValueError('Seconds is out of range [0,59].')
124 self._write(self._REG_SECONDS, intToBcd(seconds))
125
126 if minutes is not None:
127 if minutes < 0 or minutes > 59:
128 raise ValueError('Minutes is out of range [0,59].')
129 self._write(self._REG_MINUTES, intToBcd(minutes))
130
131 if hours is not None:
132 if hours < 0 or hours > 23:
133 raise ValueError('Hours is out of range [0,23].')
134 self._write(self._REG_HOURS, intToBcd(hours))
135
136 if year is not None:
137 if year < 0 or year > 99:
138 raise ValueError('Years is out of range [0,99].')
139 self._write(self._REG_YEAR, intToBcd(year))
140
141 if month is not None:
142 if month < 1 or month > 12:
143 raise ValueError('Month is out of range [1,12].')
144 self._write(self._REG_MONTH, intToBcd(month))
145
146 if day is not None:
147 if day < 1 or day > 31:
148 raise ValueError('Day is out of range [1,31].')
149 self._write(self._REG_DAY, intToBcd(day))
150
151 if dow is not None:
152 if dow < 1 or dow > 7:
153 raise ValueError('Day Of Week is out of range [1,7].')
154 self._write(self._REG_DOW, intToBcd(dow))
155
157 '''
158 Returns the current date/time.
159 @return: date/time in a tuple with order
160 (year, month, day, dow, hours, minutes, seconds)
161 '''
162 return (self.getYear(), self.getMonth(), self.getDay(),
163 self.getDow(), self.getHours(), self.getMinutes(),
164 self.getSeconds())
165
167 '''
168 Returns the current date/time.
169 @return: date/time in a string with format
170 year-month-day hours:minutes:seconds
171 '''
172 return "20%02d-%02d-%02d %02d:%02d:%02d" %(self.getYear(), self.getMonth(), self.getDay(), self.getHours(), self.getMinutes(), self.getSeconds())
173