Saturday, 2 December 2023

ESP32-2432S024R "nixie" clock with micropython

Working on a (simulated) nixie tube clock on a "Cheap Yellow Display" in micropython. I used the nixie images from macsbug's M5Stack clock ( https://macsbug.wordpress.com/2019/06/16/m5stack-nixie-tube-clock/ ). Converted with the img2rgb565.py utility from rdagger's ILI9341 library  (https://github.com/rdagger/micropython-ili9341). The library had a RST pin input that the CYD did not need, so I just commented that out, is this the "right" way to fix it ? who knows ? it works. Update: added a "beats" internet time and date display in little round "nixie" tubes... 



The board has lots of built in stuff - SD card, battery charge circuit, 2W audio amp, an LDR and a RGB led

 

"""ILI9341 nixie clock"""
#from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI, Timer
from xglcd_font import XglcdFont
from beats import Beats

OldNews= XglcdFont('fonts/OldNewspaperTypes24x26.c',24,26)

spi = SPI(1, baudrate=2000000, sck=Pin(14), mosi=Pin(13))

display = Display(spi, dc=Pin(2), cs=Pin(15),width=320, height=240, rotation=270)
display.display_on()

from machine import RTC
rtc=RTC()


def nixieclock():
    """clock display"""
    days=('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
    display.draw_text8x8(0, 0, 'Time is an illusion...', color565(30, 33, 0))
    display.draw_text(25, 145,(days[rtc.datetime()[3]]) ,OldNews,color565(255, 165, 0))
    x=0
    
    if (rtc.datetime()[4] < 10 ) :
        fn='/images/raw/nix10.raw'
        display.draw_image(fn, x, 10, 70, 134)
        x=x+69
        fn='/images/raw/nix'+str(rtc.datetime()[4])+'.raw'     
        display.draw_image(fn, x, 10, 70, 134)
    else:    
        fn='/images/raw/nix'+str(rtc.datetime()[4])[0]+'.raw'
        display.draw_image(fn, x, 10, 70, 134)
        x=x+69
        fn='/images/raw/nix'+str(rtc.datetime()[4])[1]+'.raw'
        display.draw_image(fn, x, 10, 70, 134)
        
    x=x+71
    display.draw_image('/images/raw/nixc.raw', x, 10, 40, 134)
    x=x+40
    
    if (rtc.datetime()[5] < 10 ) :
        fn='/images/raw/nix0.raw'
        display.draw_image(fn, x, 10, 70, 134)
        x=x+69
        fn='/images/raw/nix'+str(str(rtc.datetime()[5]))+'.raw'
        display.draw_image(fn, x, 10, 70, 134)
    else: 
        fn='/images/raw/nix'+str(rtc.datetime()[5])[0]+'.raw'
        display.draw_image(fn, x, 10, 70, 134)
        x=x+69
        fn='/images/raw/nix'+str(str(rtc.datetime()[5])[1])+'.raw'
        display.draw_image(fn, x, 10, 70, 134)

def ndate() :    
    (year,month,day,h,m,s,dow,doy)=rtc.datetime()
    fn='/50px/'+str("%02d"%day)[0] +'_50px.raw'
    x=5
    display.draw_image(fn,1, 190, 50, 48)
    fn='/50px/'+str("%02d"%day)[1] +'_50px.raw'
    x=x+48
    display.draw_image(fn, x, 190, 50, 48)
    x=x+60
 
    fn='/50px/'+str("%02d"%month)[0] +'_50px.raw'   
    display.draw_image(fn,x, 190, 50, 48)
    x=x+48
    fn='/50px/'+str("%02d"%month)[1] +'_50px.raw'
    display.draw_image(fn, x, 190, 50, 48)
    x=x+60 
    fn='/50px/'+str(year)[2] +'_50px.raw'   
    display.draw_image(fn,x, 190, 50, 48)
    x=x+48
    fn='/50px/'+str(year)[3] +'_50px.raw'
    display.draw_image(fn, x, 190, 50, 48)

def nixbeats():
    x=150;y=140
    fn='/50px/'+Beats()[1] +'_50px.raw'
    display.draw_image(fn,x, y, 50, 48)

    x+=48
    if (Beats()[2] != '.'):
        fn='/50px/'+Beats()[2] +'_50px.raw'
        display.draw_image(fn, x, y, 50, 48)

    x+=48
    if (Beats()[3] != '.'):
        fn='/50px/'+Beats()[3] +'_50px.raw'   
        display.draw_image(fn,x, y, 50, 48)
    


nixieclock()
ndate()
tim0 = Timer(0)
tim0.init(period=30000, mode=Timer.PERIODIC, callback=lambda t:nixieclock())

nixbeats()
tim1 = Timer(1)
#attempt to sychronise with beat change
while (Beats()[-2:] != "01" ):
    pass     
tim1.init(period=86400, mode=Timer.PERIODIC, callback=lambda t:nixbeats())
nixbeats()

No comments: