Now with weather showing capabilities

This commit is contained in:
Remy Moll 2020-10-15 11:27:54 +02:00
parent ce6c6bbe9a
commit 4ee2f78e48
8 changed files with 80 additions and 94 deletions

View File

@ -88,18 +88,6 @@ class ChatBot():
self.telegram = telegram.TelegramIO(self.persistence, self.commands) self.telegram = telegram.TelegramIO(self.persistence, self.commands)
# self.message_loop()
# def message_loop(self):
# """Calls the telegram entity regularly to check for activity"""
# while(True):
# result = self.telegram.fetch_updates()
# if len(result) != 0:
# command, params = self.telegram.handle_result(result)
# if command != "nothing":
# self.commands[command](params)
# time.sleep(5)
def react_command(self, command, params): def react_command(self, command, params):
"""""" """"""

View File

@ -3,17 +3,17 @@ import datetime
from threading import Thread from threading import Thread
import clock.main import clock.main
import bot.main
class ModuleWrapper(): class ModuleWrapper():
"""Wrapper for the CLOCK-functionality""" """Wrapper for the CLOCK-functionality"""
def __init__(self, module_name): def __init__(self, module_name):
"""""" """"""
self.clock = clock.main.ClockFace() self.clock = clock.main.ClockFace()
#import bot.main
self.time_thread = Thread(target=self.mainloop) self.time_thread = Thread(target=self.mainloop)
self.time_thread.start() self.time_thread.start()
def mainloop(self): def mainloop(self):
"""Runs the showing of the clock-face periodically (better way?)""" """Runs the showing of the clock-face periodically (better way?)"""
prev_time = 0 prev_time = 0
@ -23,4 +23,7 @@ class ModuleWrapper():
else: else:
prev_time = datetime.datetime.now().strftime("%H:%M") prev_time = datetime.datetime.now().strftime("%H:%M")
self.clock.set_face() self.clock.set_face("sun")
test = ModuleWrapper("clock")

View File

@ -1,11 +0,0 @@
P1
# Cloud - non-standard p1!
16 8
0000000000000000
0000001111001000
0000001001000000
0000010000100000
0000010000100000
0000001001000000
0000001111001000
0000000000000000

View File

@ -63,30 +63,25 @@ def date_converter():
pixels += lrow pixels += lrow
return pixels return pixels
def weather_converter(name): def weather_converter(name):
result = np.zeros((16,16)) result = np.zeros((16,16))
return result cwd = __file__.replace("\\","/") # for windows
equiv = { cwd = cwd.rsplit("/", 1)[0] # the current working directory (where this file is)
"clouds" : "clouds.pbm", if len(cwd) == 0:
"sun" : "sun.pbm", cwd = "."
"mix" : "mix.pbm", icon_spritesheet = cwd + "/weather-icons.bmp"
"rain" : "rain.pbm",
"snow" : "snow.pbm",
}
if name in equiv:
fname = equiv[name]
else:
return np.zeros((8,16))
f = open(fname,"r") icons = Image.open(icon_spritesheet)
f.readline() icons_full = np.array(icons)
f.readline() print(name)
f.readline() icon_loc = ["sun","moon","sun and clouds", "moon and clouds", "cloud","fog and clouds","2 clouds", "3 clouds", "rain and cloud", "rain and clouds", "thunder and cloud", "thunder and cloud and moon", "snow and cloud", "snow and cloud and moon", "fog","fog night"]
#ordered 1 2 \n 3 4 \ 5 5 ...
try:
iy, ix = int(icon_loc.index(name)/2), icon_loc.index(name)%2
# x and y coords
except:
return np.zeros((16,16,3))
result = np.zeros((16,16))#should be 8x16 icon_single = icons_full[16*iy:16*(iy + 1),16*ix:16*(ix + 1),...]
for i in range(8): return icon_single
l = f.readline()[:-1]
for ind,bit in enumerate(l):
result[i][ind] = bit
return result

View File

@ -61,6 +61,7 @@ class UnicornHat(object):
self.clear() self.clear()
self.draw() self.draw()
pygame.display.flip() pygame.display.flip()
pygame.event.pump()
#time.sleep(5) #time.sleep(5)

View File

@ -19,11 +19,10 @@ class OutputHandler():
self.primary = [230, 230, 230] self.primary = [230, 230, 230]
self.secondary = [10, 200, 10] self.secondary = [10, 200, 10]
self.red = [200, 10, 10] self.red = [200, 10, 10]
self.weather_string = ""
self.weather_matrix = []
def set_matrix(self, matrix, quadrant, colors = []):
def set_matrix(self, matrix, quadrant = 1, colors = []):
"""assumes 1 for primary, 2 for secondary color (everything beyond is treated as an error) """assumes 1 for primary, 2 for secondary color (everything beyond is treated as an error)
quadrant: 1,2,3,4 : 4|1 quadrant: 1,2,3,4 : 4|1
___ ___
@ -32,6 +31,15 @@ class OutputHandler():
# reshape to the main size: (eg 32x16) (always aligns the given matrix on top left.) # reshape to the main size: (eg 32x16) (always aligns the given matrix on top left.)
# add depth (rgb values)
r3 = self.matrix_add_depth(matrix,colors)
self.set_matrix_rgb(r3,quadrant)
def matrix_add_depth(self, matrix, colors = []):
"""transforms a 2d-array with 0,1,2 to a 3d-array with the rgb values for primary and secondary color"""
c1 = self.primary c1 = self.primary
c2 = self.secondary c2 = self.secondary
c3 = self.red c3 = self.red
@ -44,21 +52,11 @@ class OutputHandler():
if len(colors) > 3: if len(colors) > 3:
print("Too many colors") print("Too many colors")
result = np.zeros((self.height, self.width))
if quadrant == 1:
result[:matrix.shape[0], self.width-matrix.shape[1]:] = matrix
elif quadrant == 2:
result[self.height-matrix.shape[0]:, self.width-matrix.shape[1]:] = matrix
elif quadrant == 3:
result[self.height-matrix.shape[0]:, :matrix.shape[1]] = matrix
else: # 4 or more
result[:matrix.shape[0], :matrix.shape[1]] = matrix
# add depth (rgb values) r3 = np.zeros((matrix.shape[0],matrix.shape[1],3),dtype=int)
r3 = np.zeros((self.height,self.width,3),dtype=int) for i in range(matrix.shape[0]):
for i in range(self.height): for j in range(matrix.shape[1]):
for j in range(self.width): t = int(matrix[i, j])
t = int(result[i, j])
if t == 0: if t == 0:
r3[i, j, :] = [0,0,0] r3[i, j, :] = [0,0,0]
elif t == 1: elif t == 1:
@ -67,24 +65,38 @@ class OutputHandler():
r3[i, j, :] = c2 r3[i, j, :] = c2
else: else:
r3[i, j, :] = c3 r3[i, j, :] = c3
self.output.set_matrix(r3) return r3
def set_matrix_rgb(self, matrix, quadrant=1):
result = np.zeros((self.height, self.width,3))
if quadrant == 1:
result[:matrix.shape[0], self.width-matrix.shape[1]:,...] = matrix
elif quadrant == 2:
result[self.height-matrix.shape[0]:, self.width-matrix.shape[1]:,...] = matrix
elif quadrant == 3:
result[self.height-matrix.shape[0]:, :matrix.shape[1],...] = matrix
else: # 4 or more
result[:matrix.shape[0], :matrix.shape[1],...] = matrix
self.output.set_matrix(result)
def clock_face(self,weather): def clock_face(self,weather):
hour = converter.time_converter() hour = converter.time_converter()
day = converter.date_converter() day = converter.date_converter()
face1 = hour + day face1 = hour + day
face1_3d = self.matrix_add_depth(face1)
if self.weather_matrix == [] or weather != self.weather_string:
self.weather_matrix = converter.weather_converter("clouds")
self.weather_string = weather
face2 = self.weather_matrix face2_3d = converter.weather_converter(weather)
print("WEATHER",face2_3d.shape)
face = np.zeros((max(face1_3d.shape[0],face2_3d.shape[0]),face1_3d.shape[1]+face2_3d.shape[1],3))
face = np.zeros((max(face1.shape[0],face2.shape[0]),face1.shape[1]+face2.shape[1])) face[:face1_3d.shape[0],:face1_3d.shape[1],...] = face1_3d
face[:face1.shape[0],:face1.shape[1]] = face1 face[:face2_3d.shape[0],face1_3d.shape[1]:,...] = face2_3d
face[:face2.shape[0],face1.shape[1]:] = face2 self.set_matrix_rgb(face)
self.set_matrix(face, 4)
def text_scroll(self, text, speed, color): def text_scroll(self, text, speed, color):

BIN
clock/api/weather-icons.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

@ -1,18 +1,21 @@
import datetime import datetime
import time import time
import json import json
from clock.api import led_out
from threading import Thread from threading import Thread
import numpy import numpy
from clock.api import led
import persistence.rw import persistence.rw
################################################################################ ################################################################################
#start of actual programm. #start of actual programm.
class ClockFace(object): class ClockFace(object):
"""Actual functions one might need for a clock""" """Actual functions one might need for a clock"""
def __init__(self, text_speed=10): def __init__(self, text_speed=10):
self.IO = led_out.OutputHandler(32,16) self.IO = led.OutputHandler(32,16)
self.tspeed = text_speed self.tspeed = text_speed
self.output_thread = "" self.output_thread = ""
@ -42,9 +45,10 @@ class ClockFace(object):
def set_face(self): def set_face(self, weather):
"""""" """"""
self.run(self.IO.clock_face,("C")) self.weather = weather
self.run(self.IO.clock_face,(weather,))
def text_scroll(self, text, color=""): def text_scroll(self, text, color=""):
@ -66,7 +70,7 @@ class ClockFace(object):
for i in range(20): for i in range(20):
ct = i/20 * gradient ct = i/20 * gradient
col = [int(x) for x in ct+start_color] col = [int(x) for x in ct+start_color]
self.IO.set_matrix(ones,5,colors=[col]) self.IO.set_matrix(ones,colors=[col])
time.sleep(duration / 20) time.sleep(duration / 20)
@ -83,24 +87,18 @@ class ClockFace(object):
red = empty.copy() red = empty.copy()
red[red == 0] = 3 red[red == 0] = 3
for i in range(int(n)): for i in range(int(n)):
self.IO.set_matrix(red,5) self.IO.set_matrix(red)
time.sleep(1/frequency) time.sleep(1/frequency)
self.IO.set_matrix(empty,5) self.IO.set_matrix(empty)
time.sleep(1/frequency) time.sleep(1/frequency)
self.run(output,(duration, frequency)) self.run(output,(duration, frequency))
def image_show(self, image): def image_show(self, image, duratation):
def output(): """Shows a 16x32 image for duration seconds"""
self.output_threads.append("Showing: " + text) def output(image, duratation):
self.IO.text_scroll(text, self.tspeed, color) self.IO.set_matrix_rgb(red)
time.sleep(2)
self.output_threads.remove("Showing: " + text)
self.set_face()
scroll_thread = Thread(target = output)
scroll_thread.start() self.run(output,(image, duration))