#!/usr/bin/python3 import spidev from time import sleep, time, clock, struct_time, localtime, ctime, strftime import os import signal import random import subprocess from PIL import Image, ImageDraw import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) bgColor = (0,0,0) # https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix gamma8 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 ]; ordinal = lambda n: str(n) + {1:'st', 2:'nd', 3:'rd'}.get(10<=n%100<=20 and n or n % 10, 'th') def my_button(chan): print('button') t = localtime() wday = strftime('%A', t) month = strftime('%B', t) dd = ordinal(t.tm_mday) h = strftime('%I', t).lstrip('0') m = strftime('%M', t).lstrip('0') if (m==''): m="o'clock" am = strftime('%p', t) tt = "%s %s %s" % (h,m,am) str = "%s, %s %s, %s" % (wday, month, dd, tt) cmd = ['espeak', str] subprocess.call(cmd) pinButton=16 GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(pinButton, GPIO.RISING, callback=my_button, bouncetime=9000) spi = spidev.SpiDev() spi.open(0, 0) #-- /dev/spidev-X.Y spi.max_speed_hz = 2100000 nextImg=False ledCountOut=36 ledCountIn=25 oversample=16 w0 = ledCountOut * oversample w1 = ledCountIn * oversample h = 1 def black(): data = [] for i in range(0, ledCountOut + ledCountIn): data.extend([0,0,0]) Display(data) def Display(data): #LED: GRB chan=[1,-1,0] out = [] for i in range(0, len(data)): dat = gamma8[ data[i + chan[i%3]] ] for b in range(7,-1,-2): bit1 = dat >> b & 0x01 bit2 = dat >> (b-1) & 0x01 byte = 0x44 | (bit1 << 5) | (bit2 << 1) out.append(byte) #out.extend([0,0,0,0,0,0,0,0,0,0,0,0,0]) #out.extend([0,0,0,0]) #print(":".join(['%0X' % x for x in out])) #print(len(out)) spi.writebytes(out) #bin: LED0=100 LED1=110 #hex: LED0=4 LED1=6 def signal_interrupt(signal, frame): global nextImg nextImg=True signal.signal(signal.SIGINT, signal_interrupt) black() xw = [x * oversample for x in [1, 2, 2, 1.5]] color = [(0x40,0x30,0x20), (0,0,0x80), (0,0x80,0), (0x80,0,0)] im0 = Image.new("RGB", (w0,h), color=0) draw0 = ImageDraw.Draw(im0) im1 = Image.new("RGB", (w1,h), color=0) draw1 = ImageDraw.Draw(im1) loop=0 while True: #-erase draw0.line([0,0,w0,0], bgColor) draw1.line([0,0,w1,0], bgColor) now = time() #now = time() + ((time() * 59) % 100000) tt = localtime(now) (h,m,s,fs) = (tt.tm_hour % 12, tt.tm_min, tt.tm_sec % 60, now % 1) h += float(m)/60 m += float(s+fs)/60 s = float(s) + fs x = [ fs * w1, #< frac sec h/12 * w1, m/60 * w0, s/60 * w0, ] #if loop % 30==0: #print("%02d:%02d:%02d.%f" % (h,m,s,fs)) #print(x[2], x[2]+xw[2]) for i in range(0, 2): x2 = x[i]+xw[i] draw1.line([x[i], 0, x2, 0], color[i]) if x2 > w1: draw1.line([x[i]-w1, 0, x2-w1, 0], color[i]) for i in range(2, 4): x2 = x[i]+xw[i] draw0.line([x[i], 0, x2, 0], color[i]) if x2 > w0: draw0.line([x[i]-w0, 0, x2-w0, 0], color[i]) disp0 = im0.resize((ledCountOut, 1), Image.BILINEAR) disp1 = im1.resize((ledCountIn, 1), Image.BILINEAR) data0 = disp0.tobytes() data1 = disp1.tobytes() #--rotate data0 180? mid = len(data0)//2 d0 = data0[mid:] + data0[0:mid] Display(d0 + data1) sleep(0.025) if nextImg==True: break loop+=1 black()