Rewrite to reduce memory usage and add more features
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
This commit is contained in:
54
demoscene.py
54
demoscene.py
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from pixelfont import PixelFont
|
||||
|
||||
class DemoScene:
|
||||
"""This module implements an example scene with a traveling pixel"""
|
||||
|
||||
@@ -9,8 +10,13 @@ class DemoScene:
|
||||
update the display via self.display.put_pixel() and .render()
|
||||
"""
|
||||
self.display = display
|
||||
self.x_pos = 0 # ..just an example
|
||||
print('DemoScene: yay, initialized')
|
||||
self.intensity = 32
|
||||
self.x_pos = 0
|
||||
self.text = 'example'
|
||||
if not config:
|
||||
return
|
||||
if 'intensity' in config:
|
||||
self.intensity = int(round(config['intensity']*255))
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
@@ -20,12 +26,19 @@ class DemoScene:
|
||||
self.x_pos = 0
|
||||
print('DemoScene: here we go')
|
||||
|
||||
def input(self, button_id, button_state):
|
||||
def input(self, button_state):
|
||||
"""
|
||||
Handle button input
|
||||
"""
|
||||
print('DemoScene: button {} pressed: {}'.format(button_id, button_state))
|
||||
return False # signal that we did not handle the input
|
||||
print('DemoScene: button state: {}'.format(button_state))
|
||||
return 0 # signal that we did not handle the input
|
||||
|
||||
def set_intensity(self, value=None):
|
||||
if value is not None:
|
||||
self.intensity -= 1
|
||||
if not self.intensity:
|
||||
self.intensity = 16
|
||||
return self.intensity
|
||||
|
||||
def render(self, frame, dropped_frames, fps):
|
||||
"""
|
||||
@@ -35,26 +48,23 @@ class DemoScene:
|
||||
requested frames per second (FPS).
|
||||
"""
|
||||
|
||||
time_in_seconds = frame * fps
|
||||
if not time_in_seconds.is_integer():
|
||||
if (frame % fps) == 0:
|
||||
# Only update pixel once every second
|
||||
return True
|
||||
|
||||
y = 3
|
||||
color = 64
|
||||
self.display.clear()
|
||||
self.display.put_pixel(self.x_pos, y, color, color, color >> 1)
|
||||
self.display.render()
|
||||
print('DemoScene: rendered a pixel at ({},{})'.format(self.x_pos, y))
|
||||
display = self.display
|
||||
intensity = self.intensity
|
||||
|
||||
dot_x, dot_y = self.x_pos, 0
|
||||
text_x, text_y = 2, 2
|
||||
color = intensity
|
||||
display.clear()
|
||||
display.put_pixel(dot_x, dot_y, color, color, color >> 1)
|
||||
display.render_text(PixelFont, self.text, text_x, text_y, self.intensity)
|
||||
display.render()
|
||||
|
||||
self.x_pos += 1
|
||||
if self.x_pos == self.display.columns:
|
||||
return False # our work is done!
|
||||
if self.x_pos == display.columns:
|
||||
return False # signal that our work is done
|
||||
|
||||
return True # we want to be called again
|
||||
|
||||
if __name__ == '__main__':
|
||||
display = None
|
||||
config = None
|
||||
scene = DemoScene(display, config)
|
||||
scene.reset()
|
||||
|
||||
Reference in New Issue
Block a user