In addition to the clock scene, both the animation scene and the weather scene should now work under MicroPython on devices with 520kBytes of RAM (e.g. LoPy 1, WiPy 2) after: - combating heap fragmentation during initialization by temporarily allocating a large chunk of RAM in the beginning of main.py and freeing it after all modules have been imported and initialized - stream parsing the JSON response from the weather API - converting animations to binary and streaming them from the flash file system (additionally, older ESP8266 modules with 4MB flash have been found working under some circumstances with MicroPython 1.9.4 and an 8x8 LED matrix) - 3D parts: add diffuser grid and frame for square LED matrix displays - Arduino projects needs to be in a folder with the same name as the .ino file - config: allow multiple WiFi networks to be configured - config: add support for debug flags - config: add intensity configuration - HAL: unify serial input processing for Arduino and Pycom devices - HAL: handle UART write failures on Pycom devices - HAL: drop garbage collection from .update_display() because it takes several hundred milliseconds on 4MB devices - MCU: clear display when enabling/disabling MCU independence from host - PixelFont: move data to class attributes to reduce memory usage - PixelFont: add more characters - PixelFont: move data generation to scripts/generate-pixelfont.py - LedMatrix: support LED matrixes with strides other than 8 (e.g. as 16x16 matrices) - LedMatrix: add method to render text - LedMatrix: let consumers handle brightness themselves - AnimationScene: MicroPython does not implement bytearray.find - AnimationScene: ensure minimum on-screen time - BootScene: wifi connection and RTC sync progress for Pycom devices - ClockScene: delete unused code, switch to generic text rendering method - FireScene: classical fire effect - WeatherScene: bug fixes, switch to generic text rendering method - WeatherScene: ensure minimum on-screen time - WeatherScene: use custom JSON parsing to reduce memory usage
106 lines
3.9 KiB
Python
Executable File
106 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# This file provides a small 4x5 (width and height) font primarily designed to
|
|
# present the current date and time.
|
|
#
|
|
# The .data property provides the bits for each character available in .alphabet.
|
|
# For each character, the consumer must extract the bits (4x5 == 20 bits) from
|
|
# the offset given by the the character's position in .alphabet * 20.
|
|
#
|
|
# Example:
|
|
#
|
|
# font = PixelFont
|
|
# digit = '2' # Extract and plot the digit '2'
|
|
#
|
|
# start_bit = font.alphabet.find(digit) * font.width * font.height
|
|
# font_byte = start_bit // 8
|
|
# font_bit = start_bit % 8
|
|
# for y in range(font.height):
|
|
# for x in range(font.width):
|
|
# is_lit = font.data[font_byte] & (1<<font_bit)
|
|
# put_pixel(x, y, is_lit)
|
|
# font_bit +=1
|
|
# if font_bit == 8:
|
|
# font_byte += 1
|
|
# font_bit = 0
|
|
#
|
|
# To add new symbols to the font, edit the `font` variable in to_bytearray(),
|
|
# add a new 4x5 block representing the symbol, update the .alphabet property
|
|
# and then run this file to generate updated data for Python and C:
|
|
#
|
|
# $ ./generate-pixelfont.py
|
|
#
|
|
# Finally update the instance variables in pixelfont.py with the new data.
|
|
#
|
|
|
|
import sys
|
|
|
|
font_width = 4
|
|
font_height = 5
|
|
font_alphabet = ' %\'-./0123456789:?acdefgiklmnoprstwxy'
|
|
|
|
def font_to_bytearray(width, height, alphabet):
|
|
###|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|
|
|
font = """
|
|
# # ## # ### # ### ### # # ### ### ### ### ### ## # # ## ### ### ### ### # # # # # # # ### ### ### ### ### # # # # # #
|
|
# ## # # # ## # # # # # # # # # # # # # # # # # # # # # # # # # # ### ### # # # # # # # # # # # # # #
|
|
# ## # # # # ### ## ### ### ### # ### ### # ### # # # ## ## ### # ## # ### ### # # ### ## ### # ### # ###
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### # # # # # # # ### # # #
|
|
# # # # ### ### ### ### # ### ### # ### ### # # # # ## ### # ### ### # # ### # # # # ### # # # ### # # # # # #
|
|
""".strip('\n').replace('\n', '')
|
|
###|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|123|
|
|
|
|
data = bytearray()
|
|
byte = bits = 0
|
|
num_digits = len(alphabet)
|
|
for i in range(num_digits):
|
|
pixels = bytearray()
|
|
for row in range(height):
|
|
for col in range(width):
|
|
pos = row * num_digits * width
|
|
pos += i * width + col
|
|
is_lit = int(font[pos] != ' ')
|
|
byte |= is_lit << bits
|
|
bits += 1
|
|
if bits == 8:
|
|
data.append(byte)
|
|
bits = byte = 0
|
|
if bits:
|
|
data.append(byte)
|
|
return data
|
|
|
|
if __name__ == '__main__':
|
|
data = font_to_bytearray(font_width, font_height, font_alphabet)
|
|
debugstr = '12:30 1.8\'c'
|
|
print('Rendering string: {}'.format(debugstr))
|
|
for j in range(len(debugstr)):
|
|
digit = debugstr[j]
|
|
i = font_alphabet.find(digit)
|
|
if i < 0:
|
|
print('WARN: digit {} not found in alphabet'.format(digit))
|
|
font_byte = (i * font_height * font_width) // 8
|
|
font_bit = (i * font_height * font_width) % 8
|
|
for row in range(font_height):
|
|
for col in range(font_width):
|
|
val = 0
|
|
if data[font_byte] & (1 << font_bit):
|
|
val = 255
|
|
sys.stdout.write('#' if val else ' ')
|
|
font_bit += 1
|
|
if font_bit == 8:
|
|
font_byte += 1
|
|
font_bit = 0
|
|
sys.stdout.write('\n')
|
|
print('')
|
|
print('# Computed with pixelfont.py')
|
|
print('width = {}'.format(font_width))
|
|
print('height = {}'.format(font_height))
|
|
print('alphabet = "{}"'.format(font_alphabet))
|
|
print('data = bytearray("{}")'.format("".join("\\x{:02x}".format(x) for x in data)))
|
|
print('')
|
|
print('/* Computed with pixelfont.py */')
|
|
print('static int font_width = {};'.format(font_width))
|
|
print('static int font_height = {};'.format(font_height))
|
|
print('static char font_alphabet[] = "{}";'.format(font_alphabet))
|
|
print('static unsigned char font_data[] = "{}";'.format("".join("\\x{:02x}".format(x) for x in data)))
|