Skip to content

Commit

Permalink
Merge pull request #7 from aschuma/current
Browse files Browse the repository at this point in the history
3rd party lib update
  • Loading branch information
aschuma authored Nov 12, 2022
2 parents 7c9fbf1 + 951a36e commit 03f9c5c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 34 deletions.
91 changes: 67 additions & 24 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
# -*- coding: utf-8 -*-

import time
from datetime import date
from datetime import datetime
from datetime import timedelta

from luma.core.interface.serial import spi, noop
from luma.core.legacy import text, show_message
Expand All @@ -15,30 +12,56 @@

import config
from messageprovider import MessageProvider
from timestamp import Timestamp, now

LONG_MSG_LEN = 11

class ClockDigit:

class Timestamp:
def __init__(self):
self.ts = datetime.now()
self._set_hm()

def next(self):
self.ts = self.ts + timedelta(seconds=1)
self._set_hm()
def __init__(self, position, transition_delay =0, height=10):
self.position = position
self.transitions_delay = transition_delay
self.height = height

def _set_hm(self):
self.hours = self.ts.strftime("%H")
self.minutes = self.ts.strftime("%M")
self.date = self.ts.strftime("%d.%m.%Y")
self.day_of_week = ["MO", "DI", "MI", "DO", "FR", "SA", "SO"][
self.ts.today().weekday()
]
def transition(self, painter, digit, digit_next, offset = 0):
if digit == digit_next:
painter(self.position, digit)
else:
delayed_offset = min(self.height, max(0, offset - self.transitions_delay))
painter((self.position[0], self.position[1] - delayed_offset), digit)
painter((self.position[0], self.position[1] - delayed_offset + self.height), digit_next)


def now():
return Timestamp()
class Clock:
def __init__(self):
self.digits = [
ClockDigit(position=(0,1),transition_delay=12),
ClockDigit(position=(8,1),transition_delay=8),
ClockDigit(position=(17,1),transition_delay=4),
ClockDigit(position=(25,1),transition_delay=0)]

def max_tick(self): return 20

def transition(self, painter, ts0, ts1, tick ):
self.digits[0].transition(
painter=painter,
digit=ts0.hours[0],
digit_next=ts1.hours[0],
offset=tick)
self.digits[1].transition(
painter=painter,
digit=ts0.hours[1],
digit_next=ts1.hours[1],
offset=tick)
self.digits[2].transition(
painter=painter,
digit=ts0.minutes[0],
digit_next=ts1.minutes[0],
offset=tick)
self.digits[3].transition(
painter=painter,
digit=ts0.minutes[1],
digit_next=ts1.minutes[1],
offset=tick)


def draw_time(draw, ts, x_offset=0, y_offset=0, minute_y_offset=0, toggle=True):
Expand All @@ -62,11 +85,31 @@ def helper(current_y):

for current_y in range(0, 9):
helper(current_y)
timestamp.next()
timestamp = timestamp.next()
for current_y in range(9, 0, -1):
helper(current_y)


def minute_change_v2(device):
"""When we reach a minute change, animate it."""
ts = now()
ts_next = ts.next()

clock = Clock()
for tick in range(clock.max_tick()):
with canvas(device) as draw:

text(draw, (48,1), ts.day_of_week, fill="white", font=proportional(CP437_FONT))

def painter(position,digit):
text(draw, position, digit, fill="white", font=CP437_FONT)

clock.transition(painter,ts, ts_next,tick)
time.sleep(0.025)




def animation(device, from_y, to_y):
"""Animate the whole thing, moving it into/out of the abyss."""
timestamp = now()
Expand Down Expand Up @@ -110,7 +153,7 @@ def cp437_encode(str):
cp437_encode(msg),
fill="white",
font=proportional(CP437_FONT),
scroll_delay=0.024,
scroll_delay=0.022,
)
animation(device, 8, 1)

Expand Down Expand Up @@ -143,7 +186,7 @@ def main():
sec = timestamp.ts.second
if sec == 59:
# When we change minutes, animate the minute change
minute_change(device)
minute_change_v2(device)
elif sec == 10:
messages = [timestamp.date] + msg_provider.short_messages(LONG_MSG_LEN)
vertical_scroll(device, messages)
Expand Down
21 changes: 11 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
cbor2==5.2.0
Deprecated==1.2.10
luma.core==2.2.0
luma.led-matrix==1.5.0
paho-mqtt==1.5.1
Pillow==8.0.1
pyftdi==0.52.0
cbor2==5.4.3
luma.core==2.4.0
luma.led-matrix==1.7.0
paho-mqtt==1.6.1
Pillow==9.3.0
pkg-resources==0.0.0
pyftdi==0.54.0
pyserial==3.5
pyusb==1.1.0
smbus2==0.4.0
wrapt==1.12.1
pyusb==1.2.1
RPi.GPIO==0.7.1
smbus2==0.4.2
spidev==3.6
24 changes: 24 additions & 0 deletions timestamp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re
from datetime import datetime
from datetime import timedelta

class Timestamp:
def __init__(self, ts):
self.ts = ts
self._set_hm()

def next(self):
return Timestamp(ts = self.ts + timedelta(seconds=1))

def _set_hm(self):
self.hours = re.sub(r"^(0)(.)$", r" \2", self.ts.strftime("%H"))
self.minutes = self.ts.strftime("%M")
self.date = self.ts.strftime("%d.%m.%Y")
self.day_of_week = ["MO", "DI", "MI", "DO", "FR", "SA", "SO"][
self.ts.today().weekday()
]


def now():
return Timestamp(datetime.now())

0 comments on commit 03f9c5c

Please sign in to comment.