Skip to content

Commit 678f714

Browse files
maffeCopilotdepklyon
authored
Add time handling (#17)
* Add time handling * Show time usage in readme * fix references * fix code style in readme * code normalization --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Depklyon <7872736+depklyon@users.noreply.github.com>
1 parent fcf71ce commit 678f714

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ Display 2 independent numbers on either side of the (optional) colon, with leadi
156156
numbers(num1, num2, colon=True)
157157
```
158158

159+
Display time with colon, without leading zero in the hour value.
160+
```python
161+
t = datetime.datetime.now().time()
162+
time(t, colon=True, leading_zero=False)
163+
```
164+
159165
Display a temperature -9 through 99 followed by degrees C.
160166
```python
161167
temperature(num)

demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
# Raspberry Pi Python 3 TM1637 quad 7-segment LED display driver examples
4+
from datetime import datetime
45
from time import sleep
56

67
import tm1637
@@ -246,6 +247,11 @@
246247
tm.numbers(12, 59)
247248
sleep(DELAY)
248249

250+
# show "8:08"
251+
demo_time = datetime(2025, 1, 1, 8, 8)
252+
tm.time(demo_time, colon=True)
253+
sleep(DELAY)
254+
249255
# show temperature '24*C'
250256
tm.temperature(24)
251257
sleep(DELAY)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
setup(
1313
name='raspberrypi-tm1637',
1414
py_modules=['tm1637'],
15-
version='1.3.7',
15+
version='1.3.8',
1616
description='Raspberry Pi Python port from MicroPython library for TM1637 LED driver.',
1717
long_description=long_description,
1818
long_description_content_type="text/markdown",

tm1637.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ def numbers(self, num1, num2, colon=True):
186186
segments[1] |= 0x80 # colon on
187187
self.write(segments)
188188

189+
def time(self, time, colon=True, leading_zero=False):
190+
h0, h1 = divmod(time.hour, 10)
191+
m0, m1 = divmod(time.minute, 10)
192+
self.write([
193+
self.encode_digit(h0) if leading_zero or h0 != 0 else 0,
194+
self.encode_digit(h1) | 0x80 if colon else self.encode_digit(h1),
195+
self.encode_digit(m0),
196+
self.encode_digit(m1)])
197+
189198
def temperature(self, num):
190199
if num < -9:
191200
self.show('lo') # low

0 commit comments

Comments
 (0)