Skip to content

Commit

Permalink
Merge pull request malcolmholmes#6 from the-it/feature/singletons
Browse files Browse the repository at this point in the history
Make all hardware classes singletons
  • Loading branch information
malcolmholmes authored Oct 6, 2021
2 parents 75acb71 + e62df2e commit a5a57aa
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 150 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/05**: Unified interface for the app classes
* **2021/10/02**: Add basic time-set app
* **2021/10/01**: Improved 'app' functionality
Top button switches between apps
Expand Down
20 changes: 12 additions & 8 deletions apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from buttons import Buttons
from display import Display


class App:
def __init__(self, name, label):
self.name = name
Expand All @@ -8,32 +12,32 @@ def __init__(self, name, label):
def top_button(self, t):
print("top_button not implemented for " + self.name)

class Apps:

def __init__(self, display, buttons):
self.display = display
self.buttons = buttons
class Apps:
def __init__(self, scheduler):
self.display = Display(scheduler)
self.buttons = Buttons(scheduler)
self.apps = []
self.current_app = 0
self.buttons.add_callback(1, self.next, max=500)
self.buttons.add_callback(1, self.previous, min=500)
self.buttons.add_callback(1, self.exit, min=500)

def add(self, app):
if len(self.apps)==0:
if len(self.apps) == 0:
app.enable()
self.apps.append(app)

def next(self, t):
print("NEXT")
if len(self.apps) == 0:
return

app = self.apps[self.current_app]
if app.active and app.grab_top_button:
app.top_button(t)
return

self.apps[self.current_app].disable()
self.buttons.clear_callbacks(2)
self.buttons.clear_callbacks(3)
Expand All @@ -50,7 +54,7 @@ def previous(self, t):
self.apps[self.current_app].enable()

def exit(self, t):
if len(self.apps) >0:
if len(self.apps) > 0:
self.apps[self.current_app].disable()
self.current_app = 0
self.apps[self.current_app].enable()
33 changes: 19 additions & 14 deletions buttons.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
from machine import Pin
import time

STATE_UNPRESSED=1
STATE_PRESSED=2
from util import singleton

class Buttons:
PINS = {
STATE_UNPRESSED = 1
STATE_PRESSED = 2

PINS = {
1: 2,
2: 17,
3: 15,
}
class Button:


@singleton
class Buttons:
class Button:
class Callback:
def __init__(self, callback, min=0, max=-1):
self.callback = callback
self.min = min
self.max = max

def __init__(self, number):
self.pin = Pin(Buttons.PINS[number], Pin.IN, Pin.PULL_UP)
self.pin = Pin(PINS[number], Pin.IN, Pin.PULL_UP)
self.number = number
self.state = STATE_UNPRESSED
self.callbacks = []
Expand All @@ -41,35 +45,36 @@ def clear_callbacks(self):

def __init__(self, scheduler):
self.buttons = [
Buttons.Button(number) for number in (1,2,3)
self.Button(number) for number in (1, 2, 3)
]
scheduler.schedule("button-press", 1, self.millis_callback)

def add_callback(self, number, callback, min=0, max=-1):
self.buttons[number-1].add_callback(callback, min, max)
self.buttons[number - 1].add_callback(callback, min, max)

def remove_callback(self, number, callback, min=0, max=-1):
self.buttons[number-1].remove_callback(callback, min, max)
self.buttons[number - 1].remove_callback(callback, min, max)

def clear_callbacks(self, number):
self.buttons[number-1].clear_callbacks()
self.buttons[number - 1].clear_callbacks()

def get_button(self, number):
return self.buttons[number-1]
return self.buttons[number - 1]

def millis_callback(self, t):
for button in self.buttons:
if len(button.callbacks)>0:
if len(button.callbacks) > 0:
if button.state == STATE_UNPRESSED and button.pin.value() == 0:
button.state = STATE_PRESSED
button.pressed = time.ticks_ms()
elif button.state == STATE_PRESSED and button.pin.value() == 1:
button.state = STATE_UNPRESSED
tm = time.ticks_ms()
press_duration = time.ticks_diff(tm, button.pressed)
print("Button %d pressed for %dms" %(button.number, press_duration))
print("Button %d pressed for %dms" % (button.number, press_duration))
for callback in button.callbacks:
if callback.min < press_duration and (callback.max==-1 or press_duration <= callback.max):
if callback.min < press_duration and (
callback.max == -1 or press_duration <= callback.max):
callback.callback(t)
break
button.pressed = None
14 changes: 8 additions & 6 deletions clock.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import time
from apps import App
from display import Display
from rtc import RTC


class Clock(App):
def __init__(self, scheduler, display, rtc):
def __init__(self, scheduler):
App.__init__(self, "Clock", "clock")
self.display = display
self.rtc = rtc
self.display = Display(scheduler)
self.rtc = RTC()
self.enabled = True
self.scheduler = scheduler
scheduler.schedule("clock-second", 1000, self.secs_callback)
scheduler.schedule("clock-minute", 60000, self.mins_callback)

Expand All @@ -21,15 +23,15 @@ def disable(self):
def secs_callback(self, t):
if self.enabled:
t = time.time()
if t%2==0:
if t % 2 == 0:
self.display.show_char(":", pos=10)
else:
self.display.show_char(" :", pos=10)

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

def update_time(self):
t = self.rtc.get_time()
now = "%02d:%02d" % (t[3], t[4])
Expand Down
Loading

0 comments on commit a5a57aa

Please sign in to comment.