Skip to content

Commit

Permalink
Expanded to full feature set.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtnorthcott committed Aug 21, 2016
1 parent 120a936 commit 360dde9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
20 changes: 16 additions & 4 deletions interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ID_LED_GREEN = 9
ID_LED_ORANGE = 10
ID_LED_RED = 11
ID_SWITCH = 27
ID_BUTTON = 27

FILE_CONFIG = "/home/pi/SpotpriceMonitor/spotprice.cfg"

Expand All @@ -50,19 +50,23 @@ def __init__(self):
self.is_green = False
self.is_orange = False
self.is_red = False
self.is_red_buzzer = False

self.lcd = LCD()

# Initialise all components to off
self.reset_all()

self.listener_thread = threading.Thread(target=self._button_listener)
self.listener_thread.start()

def reset_all(self):
self.buzz()
for id in GPIO_OUT.values():
GPIO.output(id, OFF)
self.is_green = False
self.is_orange = False
self.is_red = False
self.is_red_buzzer = False
self.buzz()

def set_green(self):
if self.is_green: return
Expand All @@ -78,14 +82,16 @@ def set_red(self):
if self.is_red: return
self.reset_all()
self.is_red = True
self.is_red_buzzer = True
thr = threading.Thread(target=self._red_subroutine, args=(conf.TIME_BUZZER_DURATION, ))
thr.start()

def _red_subroutine(self, period):
while self.is_red:
time.sleep(period)
GPIO.output(ID_LED_RED, ON)
GPIO.output(ID_BUZZER, ON)
if self.is_red_buzzer:
GPIO.output(ID_BUZZER, ON)
time.sleep(period)
GPIO.output(ID_LED_RED, OFF)
GPIO.output(ID_BUZZER, OFF)
Expand All @@ -99,5 +105,11 @@ def _buzz(self, duration):
time.sleep(duration)
GPIO.output(ID_BUZZER, OFF)

def _button_listener(self):
while True:
if GPIO.input(ID_BUTTON):
self.is_red_buzzer = False
time.sleep(0.1)

def lcd_out(self, string, line, justify="left"):
self.lcd.string_out(string, line, justify)
3 changes: 1 addition & 2 deletions lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self):
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable

# Initialise display
self._byte(0x33, LCD_CMD) # 110011 Initialise
self._byte(0x32, LCD_CMD) # 110010 Initialise
Expand All @@ -57,7 +57,6 @@ def __init__(self):
self._byte(0x01, LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)


def _byte(self, bits, mode):
"""Send byte to data pins. Mode is true for character, false for a command """

Expand Down
25 changes: 16 additions & 9 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
REGEX_SPOTPRICE = "(?<=ISL2201 \$)\d{1,4}\.\d{2}"
REGEX_SPOTPRICE_TIME = "(?<=Last updated at )[\d\/]{10} [\d\:]{8}"

STRING_TIME = "%d %b %H:%M"
STRING_PRICE = "{0:<10}{1:>10}"
STRING_TEMPERATURE = "{:<9}{:<5}{:>6}"
STRING_WIND = "{:<9}{:<7}{:>4}"

LEFT = "left"
CENTRE = "centre"
RIGHT = "right"
Expand All @@ -39,7 +44,6 @@
class Monitor(object):
def __init__(self, url):
self.webpage = webpage.Webpage(url)
self.webpage.open()
self.update_values()

def update_values(self):
Expand Down Expand Up @@ -98,19 +102,22 @@ def status(self):
return s

def time_string(self):
return self.time.strftime("%d %b %H:%M")
return self.time.strftime(STRING_TIME)

def price_string(self):
str_price = "${:.2f}".format(self.spotprice)
str_network = "{:.2f}c".format(self.network_charge)

return "{0:<10}{1:>10}".format(str_price, str_network)
return STRING_PRICE.format(str_price, str_network)


class WeatherMonitor(Monitor):
def __init__(self):
super().__init__(WEBPAGE_WEATHER)

def update_values(self):
if not self.webpage.open(): return

weather_data = pickle.loads(self.webpage.response, encoding="latin1")

self.temperature = weather_data[2]
Expand All @@ -130,13 +137,13 @@ def temp_string(self):
humidity = "{}%".format(self.humidity)
rainfall = "{}mm".format(self.rainfall)

return "{:<6} {:<4} {:>6}".format(temp, humidity, rainfall)
return STRING_TEMPERATURE.format(temp, humidity, rainfall)

def wind_string(self):
gust = "{}km/h".format(self.wind_speed_gust)
mean = "{}km/h".format(self.wind_speed_mean)
gust = "{}km/h".format(int(self.wind_speed_gust))
mean = "{}km/h".format(int(self.wind_speed_mean))

return "{:<7} {:<7} {:>3}".format(gust, mean, self.wind_dir)
return STRING_WIND.format(gust, mean, self.wind_dir)


class MonitorInterface(object):
Expand All @@ -147,7 +154,6 @@ def __init__(self):

self.spotprice_mon.update_values()
self.weather_mon.update_values()

self.update_interface()

def update_interface(self):
Expand All @@ -160,7 +166,7 @@ def update_interface(self):
self.interface.lcd_out(self.weather_mon.temp_string(), 2, "centre")
self.interface.lcd_out(self.weather_mon.wind_string(), 3, "centre")

def mainloop(self, length):
def mainloop(self):
""" Create a loop in which to perform tasks, updating weather information at every 11-minute point and the
spotprice every 5 minutes from start (possibly every 6 and 11 minute point)
"""
Expand Down Expand Up @@ -188,6 +194,7 @@ def mainloop(self, length):

def main():
mon = MonitorInterface()
mon.mainloop()

if __name__ == '__main__':
try:
Expand Down

0 comments on commit 360dde9

Please sign in to comment.