Skip to content

Commit

Permalink
Basic time set app
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmholmes committed Oct 2, 2021
1 parent 4157eea commit a4f26a5
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change Log

* **2021/10/02**: Add basic time-set app
* **2021/10/01**: Improved 'app' functionality
Top button switches between apps
* **2021/09/19**: Added 'Pomodoro timer' app
Expand Down
2 changes: 1 addition & 1 deletion display.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def initialise_fonts(self):
".": self.Character(width=1, rows=[0x00,0x00,0x00,0x00,0x00,0x00,0x01]), # 1×7
"-": self.Character(width=2, rows=[0x00,0x00,0x00,0x03,0x00,0x00,0x00]), # 2×7
"M": self.Character(width=4, rows=[0x00,0x11,0x1B,0x15,0x11,0x11,0x11,0x11]), # 5×7
"/": self.Character(width=4, rows=[0x00,0x04,0x04,0x02,0x02,0x02,0x01,0x01]), # 3×7
"/": self.Character(width=2, rows=[0x02,0x02,0x02,0x01,0x01,0x01,0x01,0x01]), # 3×7
"°C2": self.Character(width=4, rows=[0x00,0x01,0x0C,0x12,0x02,0x02,0x12,0x0C]), # 5×7
"°F2": self.Character(width=4, rows=[0x00,0x01,0x1E,0x02,0x1E,0x02,0x02,0x02]),
"V": self.Character(width=4, rows=[0x11,0x11,0x11,0x11,0x11,0x0A,0x04]), # 5×7
Expand Down
4 changes: 2 additions & 2 deletions ds3231_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def convert(self, set_rtc=False): # Return a tuple in localtime() format (less
rtc.datetime((YY, MM, DD, wday, hh, mm, ss, 0))
return result

def save_time(self):
(YY, MM, mday, hh, mm, ss, wday, yday) = utime.localtime() # Based on RTC
def save_time(self, t):
(YY, MM, mday, hh, mm, ss, wday, yday) = t
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh))) # Sets to 24hr mode
Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rtc import RTC
from apps import Apps
from pomodoro import Pomodoro
from time_set import TimeSet

scheduler = Scheduler()
display = Display(scheduler)
Expand All @@ -16,6 +17,7 @@

apps = Apps(display, buttons)
apps.add(Clock(scheduler, display, rtc))
apps.add(TimeSet(scheduler, display, buttons, rtc))
apps.add(Pomodoro(scheduler, display, speaker, buttons))

print("STARTING...")
Expand Down
4 changes: 2 additions & 2 deletions rtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def __init__(self):
def get_time(self):
return self.ds.get_time()

def save_time(self):
return self.ds.save_time()
def save_time(self, t):
return self.ds.save_time(t)
145 changes: 145 additions & 0 deletions time_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import time
import machine
from apps import App

month_max = {
1: 31, # January
2: 29, # February
3: 31, # March
4: 30, # April
5: 31, # May
6: 30, # June
7: 31, # July
8: 31, # August
9: 30, # September
10: 31, # October
11: 30, # November
12: 31, # December
}

class TimeSet(App):
class State:
def __init__(self, name, position, panel, index, max, length=2, offset=0):
self.name = name
self.position = position
self.panel = panel
self.index = index
self.max = max
self.length = length
self.offset = offset

def __init__(self, scheduler, display, buttons, rtc):
App.__init__(self, "Time Set", "timeset")

self.display = display
self.scheduler = scheduler
self.buttons = buttons
self.rtc = rtc
self.grab_top_button = True
self.enabled = False
self.state = None
self.state_index = -1
self.flash_count = 0
self.flash_state = False
scheduler.schedule("time-set-half-second", 500, self.half_secs_callback)
scheduler.schedule("time-set-minute", 60000, self.mins_callback)
self.initialise_states()

def initialise_states(self):
self.states = [
TimeSet.State("hours", 0, "time", 3, 24),
TimeSet.State("minutes", 13, "time", 4, 60),
TimeSet.State("year", 0, "year", 0, 3000, length=4),
TimeSet.State("month", 0, "date", 1, 12, offset=1),
TimeSet.State("day", 13, "date", 2, -1, offset=1),
]

def enable(self):
self.enabled = True
self.state_index = 0
self.state = self.states[self.state_index]
self.update_display()
self.buttons.add_callback(2, self.up_callback, max=500)
self.buttons.add_callback(3, self.down_callback, max=500)

def disable(self):
self.active = False
self.enabled = False
self.state = None

def half_secs_callback(self, t):
if self.enabled:
self.flash_count = (self.flash_count+1)%3
if self.flash_count == 2:
if self.state.length == 2:
self.display.show_text(" ", pos=self.state.position)
elif self.state.length == 4:
self.display.show_text(" ", pos=self.state.position)
self.flash_state = False
else:
if not self.flash_state:
self.flash_state = True
if self.state.length == 2:
self.display.show_text("%02d" % self.time[self.state.index], pos=self.state.position)
elif self.state.length == 4:
self.display.show_text("%04d" % self.time[self.state.index], pos=self.state.position)

def mins_callback(self, t):
if self.enabled:
self.update_display()

def update_display(self):
self.time = self.rtc.get_time()
self.display.clear()
if self.state.panel == "time":
t = self.rtc.get_time()
now = "%02d:%02d" % (t[3], t[4])
self.display.show_text(now)

elif self.state.panel == "year":
t = self.rtc.get_time()
now = "%04d" % (t[0])
self.display.show_text(now)

elif self.state.panel == "date":
t = self.rtc.get_time()
now = "%02d/%02d" % (t[1], t[2])
self.display.show_text(now)

def up_callback(self, t):
self.active = True
t = list(self.rtc.get_time())
max = self.state.max
if max ==-1:
# This is "day of month", which varies
month = t[1]
max = month_max[month]

t[self.state.index] = (t[self.state.index]+1-self.state.offset) % max + self.state.offset
self.rtc.save_time(tuple(t))
self.flash_count = 0
self.update_display()

def down_callback(self, t):
self.active = True
t = list(self.rtc.get_time())
max = self.state.max
if max ==-1:
# This is "day of month", which varies
month = t[1]
max = month_max[month]

t[self.state.index] = (t[self.state.index]-1-self.state.offset) % max + self.state.offset
self.rtc.save_time(tuple(t))
self.flash_count = 0
self.update_display()

def stop_callback(self, t):
self.scheduler.disable_app(self.name)

def top_button(self, t):
self.flash_count = 0
self.state_index = (self.state_index + 1) % len(self.states)
self.state = self.states[self.state_index]
self.display.clear()
self.update_display()

0 comments on commit a4f26a5

Please sign in to comment.