andy speed boost

This commit is contained in:
Elektrospy 2019-01-18 20:22:13 +01:00
parent 5f101d776b
commit 740e3520f5

View File

@ -14,6 +14,7 @@ import png
import numpy as np import numpy as np
import os import os
import sys import sys
import time
# get screen size from parameters # get screen size from parameters
Nx = 80 Nx = 80
@ -55,7 +56,7 @@ class Layer:
self.rgb_buffer_length = (self.png_width * self.png_height) * self.color_values self.rgb_buffer_length = (self.png_width * self.png_height) * self.color_values
self.layer_buffer_current_color = np.zeros(self.color_values, np.uint8) self.layer_buffer_current_color = np.zeros(self.color_values, np.uint8)
self.layer_buffer_array = np.zeros(self.rgb_buffer_length, np.uint8) self.layer_buffer_array = np.zeros(self.rgb_buffer_length, np.uint8)
self.png_image_data_flatten = self.flatten_rgb_array_from_png(self.png_image_data) self.png_image_data_flatten = np.array(self.flatten_rgb_array_from_png(self.png_image_data))
# internal event states # internal event states
self.layer_current_event_value = 0 self.layer_current_event_value = 0
self.animation_finished = True self.animation_finished = True
@ -124,19 +125,15 @@ class Layer:
return output_rgb_array return output_rgb_array
def render_rgb_layer(self, r=0, g=0, b=0): def render_rgb_layer(self, r=0, g=0, b=0):
# reset layer buffer, step by number of color values (default 3, rgb) def dolayer(color, channel):
for i in range(0, self.rgb_buffer_length): if color == 0:
self.layer_buffer_array[i] = 0 self.layer_buffer_array[channel::3] *= 0
# set new rgb values, step by number of color values (default 3, rgb) else:
for i in range(0, self.rgb_buffer_length, self.color_values): im = (color * (self.png_image_data_flatten[channel::3].astype(np.uint16)))
# check for every r g b value if current brightness map value is not equal to zero self.layer_buffer_array[channel::3] = (im/255).astype(np.uint8)
# if true to mapping, else ignore and keep it at zero (black) dolayer(r, 0)
if self.png_image_data_flatten[i] != 0: dolayer(g, 1)
self.layer_buffer_array[i] = self.get_new_color_value(r, self.png_image_data_flatten[i]) dolayer(b, 2)
if self.png_image_data_flatten[i + 1] != 0:
self.layer_buffer_array[i + 1] = self.get_new_color_value(g, self.png_image_data_flatten[i + 1])
if self.png_image_data_flatten[i + 2] != 0:
self.layer_buffer_array[i + 2] = self.get_new_color_value(b, self.png_image_data_flatten[i + 2])
# helper method to calculate new color value # helper method to calculate new color value
# based on current value and target mapping value # based on current value and target mapping value
@ -302,10 +299,11 @@ def get_higher_color_value(value1, value2):
def merge_layer_arrays(layer_first, layer_second): def merge_layer_arrays(layer_first, layer_second):
output_array = np.zeros(buffer_length, np.uint8) return np.maximum(layer_first, layer_second)
for idx in range(0, buffer_length): # output_array = np.zeros(buffer_length, np.uint8)
output_array[idx] = get_higher_color_value(layer_first[idx], layer_second[idx]) # for idx in range(0, buffer_length):
return output_array # output_array[idx] = get_higher_color_value(layer_first[idx], layer_second[idx])
# return output_array
def get_merged_layers_array(): def get_merged_layers_array():
@ -344,13 +342,13 @@ if __name__ == "__main__":
wst.start() wst.start()
# frame refresh time limit, default ~16ms for about ~60fps # frame refresh time limit, default ~16ms for about ~60fps
frame_update_limit_ms = 16 frame_update_limit_ms = 0.016
frame_last_update_ms = millis() frame_last_update_ms = time.time()
frame_current_millis = millis() frame_current_millis = time.time()
# Start layer logic loop # Start layer logic loop
while True: while True:
frame_current_millis = millis() frame_current_millis = time.time()
if frame_current_millis - frame_last_update_ms >= frame_update_limit_ms: if frame_current_millis - frame_last_update_ms >= frame_update_limit_ms:
# run layer render logic # run layer render logic
layer_small.run() layer_small.run()
@ -363,4 +361,7 @@ if __name__ == "__main__":
# convert frame buffer to byte string and write to stdout # convert frame buffer to byte string and write to stdout
os.write(1, output_buffer.tobytes()) os.write(1, output_buffer.tobytes())
# refresh last frame update time # refresh last frame update time
frame_last_update_ms = millis() frame_last_update_ms = time.time()
else:
time.sleep((frame_last_update_ms+frame_update_limit_ms)-frame_current_millis)