Galactic Unicorn - Small numeric characters

With smaller numeric characters we can get more information on the GU matrix at one time. This project provides the 5x3 characters and demonstrates how we can show the current time, temperature and humidity on the screen at the same time.

It moves on to demonstrate colour mixing, with the primary values displayed, their current colours and resulting mixed colour. The buttons A, B, C, D, Vol+ and Vol- used to adjust the values.

# Pimoroni Galactic Unicorn and AHT20 sensor
# Small numeric characters, colour mixing, buttons, time, temperature & humidity
# Tony Goodhew - TonyGo2 - 19th November 2022

from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
import time
import machine

import ahtx0
i2c = machine.I2C(id=0,scl=machine.Pin(5), sda=machine.Pin(4))
# Create the sensor object using I2C
sensor = ahtx0.AHT20(i2c)

# create the rtc object
rtc = machine.RTC()
# create a PicoGraphics framebuffer to draw into

graphics = PicoGraphics(display=DISPLAY_GALACTIC_UNICORN)

# create our GalacticUnicorn object
gu = GalacticUnicorn()

#Define some colours
BLACK = graphics.create_pen(0, 0, 0)
RED =  graphics.create_pen(255, 0, 0)
RED2 =  graphics.create_pen(128, 0, 0)
YELLOW = graphics.create_pen(255, 255, 0)
YELLOW2 = graphics.create_pen(100, 100, 0)
GREEN = graphics.create_pen(0, 255, 0)
GREEN2 = graphics.create_pen(0, 128, 0)
CYAN =  graphics.create_pen(0, 255, 255)
BLUE =  graphics.create_pen(0, 0, 255)
BLUE2 =  graphics.create_pen(0, 0, 128)
MAGENTA =  graphics.create_pen(255, 0, 255)
WHITE =  graphics.create_pen(200, 200, 200)
GREY =  graphics.create_pen(100, 100, 100)
DRKGRY =  graphics.create_pen(20, 20, 20)

# Coded small digits
nums =[
    [1,1,1,1,0,1,1,0,1,1,0,1,1,1,1], # 0
    [0,1,0,0,1,0,0,1,0,0,1,0,0,1,0],
    [1,1,1,0,0,1,1,1,1,1,0,0,1,1,1],
    [1,1,1,0,0,1,1,1,1,0,0,1,1,1,1],
    [1,0,0,1,0,1,1,1,1,0,0,1,0,0,1],
    [1,1,1,1,0,0,1,1,1,0,0,1,1,1,1], # 5
    [1,1,1,1,0,0,1,1,1,1,0,1,1,1,1],
    [1,1,1,0,0,1,0,0,1,0,1,0,1,0,0],
    [1,1,1,1,0,1,1,1,1,1,0,1,1,1,1],
    [1,1,1,1,0,1,1,1,1,0,0,1,0,0,1], # 9
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], # empty
    [1,1,1,1,0,0,1,0,0,1,0,0,1,1,1] # C
    ]

def blk():
    graphics.set_pen(BLACK)
    graphics.clear()
    gu.update(graphics)
    
def pressed():
    if gu.is_pressed(GalacticUnicorn.SWITCH_A):
        return GalacticUnicorn.SWITCH_A
    if gu.is_pressed(GalacticUnicorn.SWITCH_B):
        return GalacticUnicorn.SWITCH_B
    if gu.is_pressed(GalacticUnicorn.SWITCH_C):
        return GalacticUnicorn.SWITCH_C
    if gu.is_pressed(GalacticUnicorn.SWITCH_D):
        return GalacticUnicorn.SWITCH_D
    if gu.is_pressed(GalacticUnicorn.SWITCH_VOLUME_UP):
        return GalacticUnicorn.SWITCH_VOLUME_UP
    if gu.is_pressed(GalacticUnicorn.SWITCH_VOLUME_DOWN):
        return GalacticUnicorn.SWITCH_VOLUME_DOWN
    return None

def show_digit(dig,xx,yy,c):
    p = 0
    colours = [BLACK,c]
    for row in range(5):
        for col in range(3):
            nn = nums[dig]
            v = nn[p]
            graphics.set_pen(colours[v])
            graphics.pixel(xx+col,yy+row)
            p = p + 1

def show_number(val,xx,yy,colour):  #Base 10
    digits = [0,0,0,0]
    digits[0] = val // 1000
    temp_val = val - digits[0] * 1000
    if val < 1000: digits[0] = 10 # Blank digit
    digits[1] = temp_val // 100
    temp_val = temp_val - digits[1] * 100
    if val < 100: digits[1] = 10
    digits[2] = temp_val // 10
    if val < 10: digits[2] = 10
    digits[3] = temp_val % 10
    for p in range(4):
        show_digit(digits[p],xx+p*4,yy,colour)

def show_number2(val,xx,yy,colour):  #Base 10
    digits = [0,0]
    if val < 100: digits[1] = 0
    digits[0] = val // 10
    if val < 10: digits[1] = 0
    digits[1] = val % 10
    for p in range(2):
        show_digit(digits[p],xx+p*4,yy,colour)

def blob(xx,yy,r,g,b):               # Small primary rectangle
    t = graphics.create_pen(r,g,b)
    graphics.set_pen(t)    
    for x in range(6):
        for y in range(4):
            graphics.pixel(xx+x,yy +y)           

def blob2(xx,yy,r,g,b):             # Large mixed colour rectangle
    t = graphics.create_pen(r,g,b)
    graphics.set_pen(t)    
    for x in range(20):
        for y in range(3):
            graphics.pixel(xx+x,yy +y)
 
for i in range(100):
    graphics.set_pen(BLACK)
    graphics.clear()

    year, month, day, wd, hour, minute, second, _ = rtc.datetime()

    show_number2(second,33,0,RED2)   # Time
    show_number2(minute,23,0,RED2)
    show_number2(hour,13,0,RED2)
    
    graphics.set_pen(BLUE2)          # Colons
    graphics.pixel(21,1)
    graphics.pixel(21,3)
    graphics.pixel(31,1)
    graphics.pixel(31,3)

    t = int(sensor.temperature)
    h = int(sensor.relative_humidity)
    graphics.set_pen(YELLOW2)
    show_number2(t,13,6,YELLOW2)
    show_digit(11,23,6,YELLOW2)
    graphics.pixel(21,6)
    show_number2(h,33,6,GREEN2)
    gu.update(graphics)

blk()
for d in range(10):
    show_digit(+d,6+d*4,0, BLUE2)
    show_digit(d,6+d*4,6, RED2)
gu.update(graphics)
time.sleep(3)
blk()

#Colour mixing
# Buttons A+B Red U & D, C+D Gren U & D, Vol +/- Blue U & D
r = 100 # Start primary colour values
g = 128
b = 20

while True:
    if pressed() == GalacticUnicorn.SWITCH_A: # RED UP
        r = r + 1
        if r > 255: r = 255
        
    if pressed() == GalacticUnicorn.SWITCH_B: # RED DOWN
        r = r - 1
        if r < 0: r = 0
        
    if pressed() == GalacticUnicorn.SWITCH_C: # GREEN UP
        g = g + 1
        if g > 255: g = 255
        
    if pressed() == GalacticUnicorn.SWITCH_D: # GREEN DOWN
        g = g - 1
        if g < 0: g = 0
        
    if pressed() == GalacticUnicorn.SWITCH_VOLUME_UP: # BLUE UP
        b = b + 1
        if b > 255: b = 255
        
    if pressed() == GalacticUnicorn.SWITCH_VOLUME_DOWN: # BLUE DOWN
        b = b - 1
        if b < 0: b = 0

    show_number(r,0,0,RED2)
    show_number(g,18,0,GREEN2)
    show_number(b,37,0,BLUE2)
    blob(1,7,r,0,0)
    blob(10,7,0,g,0)
    blob(19,7,0,0,b)
    blob2(30,7,r,g,b)
    gu.update(graphics)

I hope you find it useful.

2 Likes