#!/usr/bin/env python3 import random import time import busio import digitalio import pulseio from board import SCK, MOSI, MISO, D8, D24, D23, D18 from adafruit_rgb_display import color565 import adafruit_rgb_display.ili9341 as ili9341 from raspyrfm import * import sensors from sensors import rawsensor import sys import threading from PIL import Image, ImageDraw, ImageFont, ImageColor class TextBox: def __init__(self, pos, size, max_jitter, color, background, fitting_text): self.color = color self.background = background self.pos = pos self.bounds = (size[0]+1, size[1]+1) font_size = 100 while (self.bounds[0] > size[0]-max_jitter) or (self.bounds[1] > size[1]-max_jitter): font_size-=1 self.font = ImageFont.truetype("/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf", font_size) self.bounds = self.font.getsize(fitting_text) self.size = size self.img = Image.new("RGB", self.bounds) self.draw = ImageDraw.Draw(self.img) self.offset = (int((self.size[0]-self.bounds[0])/2), int((self.size[1]-self.bounds[1])/2)) def draw_text(self, display, text, jitter_pos): self.draw.rectangle((0, 0, self.img.width-1, self.img.height-1), self.background, self.background, 1) self.draw.text((jitter_pos[0],0), text, font=self.font, fill=self.color) y=self.pos[0]+self.offset[0] x=display.width-self.pos[1]-self.size[1]+self.offset[1] display.image(self.img, x=x, y=y) ### # CONFIGURATION ### BAUDRATE = 1000000 DISPLAY_SIZE=(320,240) CLOCK_FORMAT = "%H:%M" TEMP_FORMAT = "% 3.1f°C" BKGND_COLOR=(0,0,0) LINE_COLOR=(255,255,255) JITTER=5 DELAY=1 now_parts=time.localtime() time_str = time.strftime(CLOCK_FORMAT, now_parts) temp_str = TEMP_FORMAT % -10.0 clock_box=TextBox( (10,10), (300,120), JITTER, (255,255,255), BKGND_COLOR, time_str) temp_box =TextBox( (120,180), (190,50), JITTER, (255,0,0), BKGND_COLOR, temp_str) # Configuration for CS and DC pins: CS_PIN = D8 DC_PIN = D24 RES_PIN = D23 BL_PIN = D18 # Setup SPI bus using hardware SPI: spi = busio.SPI(clock=SCK, MOSI=MOSI, MISO=MISO) # Create the ILI9341 display: display = ili9341.ILI9341(spi, cs=digitalio.DigitalInOut(CS_PIN), dc=digitalio.DigitalInOut(DC_PIN), rst=digitalio.DigitalInOut(RES_PIN), baudrate=BAUDRATE, width=DISPLAY_SIZE[1], height=DISPLAY_SIZE[0], rotation=270) #backlight = pulseio.PWMOut(BL_PIN, duty_cycle=NIGHT_BRIGHT) backlight = digitalio.DigitalInOut(BL_PIN) backlight.direction = digitalio.Direction.OUTPUT backlight.value = True if raspyrfm_test(5, RFM69): print("Found RaspyRFM mod") rfm = RaspyRFM(5, RFM69) #when using the RaspyRFM twin else: print("No RFM69 module found!") exit() rfm.set_params( Freq = 868.30, #MHz center frequency Datarate = 17.241, #kbit/s baudrate ModulationType = rfm69.FSK, #modulation Deviation = 30, #kHz frequency deviation SyncPattern = [0x2d, 0xd4], #syncword Bandwidth = 150, #kHz bandwidth RssiThresh = -105, #dBm RSSI threshold ) mean_temp = 0 shutdown = False def update_temp(): global mean_temp temps = [] while not shutdown: data = rfm.receive(12) if data: obj = rawsensor.CreateSensor(data).GetData() if 'ID' in obj and 'T' in obj: temps.append(float(obj['T'][0])) if len(temps)>20: temps = temps[1:] if temps: mean_temp = sum(temps) / len(temps) temp_thread = threading.Thread(target=update_temp, name="Temp Thread") temp_thread.start() #display.vline(0,clock_box.size[1],1,color565(LINE_COLOR)) def jitter(): return (random.randint(0,JITTER), random.randint(0,JITTER)) # Main loop: jitter_pos = jitter() old_time="" old_temp="" display.fill(color565(BKGND_COLOR)) try: while True: check_pixel = display.read((0,0) #display.reset() #time.sleep(0.5) now = time.time() now_parts = time.localtime(now) time_str = time.strftime(CLOCK_FORMAT, now_parts) temp_str = TEMP_FORMAT % mean_temp if time_str != old_time: clock_box.draw_text(display, time_str, jitter_pos) old_time = time_str if now_parts.tm_sec == 0: jitter_pos = jitter() # if (now_parts.tm_min == 0) and (now_parts.tm_hour > 7 and now_parts.tm_hour < 19): # backlight.duty_cycle = DAY_BRIGHT # else: # backlight.duty_cycle = NIGHT_BRIGHT if temp_str != old_temp: time.sleep(0.1) temp_box.draw_text(display, temp_str, jitter_pos) old_temp = temp_str while time.time() - now < DELAY: time.sleep(0.1*DELAY) except KeyboardInterrupt: clock_box.draw_text(display, "off" ,(0,0)) temp_box.draw_text(display,"",(0,0)) shutdown=True temp_thread.join()