4. Advanced Examples¶
4.1. OLEDrgb¶
Organic RGB LED module with a 96×64 pixel display capable of 16-bit color resolution.
4.1.1. Analogue clock¶
Display an analogue clock face with date and time.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-17 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK
"""
An analog clockface with date & time.
Ported from:
https://gist.github.com/TheRayTracer/dd12c498e3ecb9b8b47f#file-clock-py
"""
import math
import time
import datetime
from DesignSpark.Pmod.HAT import createPmod
from luma.core.render import canvas
def posn(angle, arm_length):
dx = int(math.cos(math.radians(angle)) * arm_length)
dy = int(math.sin(math.radians(angle)) * arm_length)
return (dx, dy)
def main():
today_last_time = "Unknown"
while True:
now = datetime.datetime.now()
today_date = now.strftime("%d %b %y")
today_time = now.strftime("%H:%M:%S")
if today_time != today_last_time:
today_last_time = today_time
with canvas(device) as draw:
now = datetime.datetime.now()
today_date = now.strftime("%d %b %y")
margin = 4
cx = 30
cy = min(device.height, 64) / 2
left = cx - cy
right = cx + cy
hrs_angle = 270 + (30 * (now.hour + (now.minute / 60.0)))
hrs = posn(hrs_angle, cy - margin - 7)
min_angle = 270 + (6 * now.minute)
mins = posn(min_angle, cy - margin - 2)
sec_angle = 270 + (6 * now.second)
secs = posn(sec_angle, cy - margin - 2)
draw.ellipse((left + margin, margin, right - margin, min(device.height, 64) - margin), outline="white")
draw.line((cx, cy, cx + hrs[0], cy + hrs[1]), fill="white")
draw.line((cx, cy, cx + mins[0], cy + mins[1]), fill="white")
draw.line((cx, cy, cx + secs[0], cy + secs[1]), fill="red")
draw.ellipse((cx - 2, cy - 2, cx + 2, cy + 2), fill="white", outline="white")
draw.text((2 * (cx + margin), cy - 8), today_date, fill="yellow")
draw.text((2 * (cx + margin), cy), today_time, fill="yellow")
time.sleep(0.1)
if __name__ == "__main__":
oled = createPmod('OLEDrgb','JA')
try:
device = oled.getDevice()
main()
except KeyboardInterrupt:
pass
finally:
oled.cleanup()
4.1.2. Game of Life¶
Conway’s Game of Life.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-17 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK
"""
Conway's game of life.
Adapted from:
http://codereview.stackexchange.com/a/108121
"""
import time
from random import randint
from DesignSpark.Pmod.HAT import createPmod
from luma.core.render import canvas
def neighbors(cell):
x, y = cell
yield x - 1, y - 1
yield x, y - 1
yield x + 1, y - 1
yield x - 1, y
yield x + 1, y
yield x - 1, y + 1
yield x, y + 1
yield x + 1, y + 1
def iterate(board):
new_board = set([])
candidates = board.union(set(n for cell in board for n in neighbors(cell)))
for cell in candidates:
count = sum((n in board) for n in neighbors(cell))
if count == 3 or (count == 2 and cell in board):
new_board.add(cell)
return new_board
def main():
scale = 3
cols = device.width // scale
rows = device.height // scale
initial_population = int(cols * rows * 0.33)
while True:
board = set((randint(0, cols), randint(0, rows)) for _ in range(initial_population))
for i in range(500):
with canvas(device, dither=True) as draw:
for x, y in board:
left = x * scale
top = y * scale
if scale == 1:
draw.point((left, top), fill="white")
else:
right = left + scale
bottom = top + scale
draw.rectangle((left, top, right, bottom), fill="white", outline="black")
if i == 0:
w, h = draw.textsize("Game of Life")
left = (device.width - w) // 2
top = (device.height - h) // 2
draw.rectangle((left - 1, top, left + w + 1, top + h), fill="black", outline="white")
draw.text((left + 1, top), text="Game of Life", fill="white")
if i == 0:
time.sleep(3)
board = iterate(board)
if __name__ == "__main__":
try:
oled = createPmod('OLEDrgb','JA')
device = oled.getDevice()
main()
except KeyboardInterrupt:
pass
Requirements
- PmodOLEDrgb connected to port JA