Moved Blinking logic to the ProotState class.
Made ProotState class a singleton.
This commit is contained in:
parent
8dab8805e6
commit
25c9b953bf
1 changed files with 68 additions and 60 deletions
128
rpi/antRender.py
128
rpi/antRender.py
|
@ -5,28 +5,66 @@ from PIL import Image
|
|||
import numpy as np
|
||||
import math
|
||||
from scipy.optimize import linear_sum_assignment
|
||||
import threading
|
||||
|
||||
|
||||
print("start configuring matrix")
|
||||
startT = curr_time = round(time.time()*1000)
|
||||
|
||||
# Configuration for the matrix screens
|
||||
options = RGBMatrixOptions()
|
||||
options.rows = 32
|
||||
options.cols = 64
|
||||
options.chain_length = 2
|
||||
options.parallel = 1
|
||||
options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat'
|
||||
matrix = RGBMatrix(options=options)
|
||||
|
||||
endT = curr_time = round(time.time()*1000)
|
||||
print("configuring matrix took: " + str(endT - startT) + " ms")
|
||||
|
||||
|
||||
# array to hold the blink animation frames
|
||||
blinkFrameCanvases = []
|
||||
|
||||
|
||||
|
||||
class ProotState:
|
||||
def __init__(self):
|
||||
self.current_blink_state = 0
|
||||
self.desired_blink_state = 0
|
||||
|
||||
def startBlink(self):
|
||||
self.desired_blink_state = 10
|
||||
|
||||
def next_blink_state(self) -> int:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
cls._instance.current_blink_state = 0
|
||||
cls._instance.desired_blink_state = 0
|
||||
cls._instance.blinks_frames_ready = False
|
||||
return cls._instance
|
||||
|
||||
def next_blink_frame_number(self) -> int:
|
||||
if self.current_blink_state == self.desired_blink_state and self.current_blink_state == 10:
|
||||
self.desired_blink_state = 0
|
||||
return 10
|
||||
|
||||
|
||||
if self.current_blink_state < self.desired_blink_state:
|
||||
self.current_blink_state += 1
|
||||
else:
|
||||
self.current_blink_state -= 1
|
||||
|
||||
|
||||
return self.current_blink_state
|
||||
|
||||
def set_desired_blink_state(self, state: int):
|
||||
self.desired_blink_state = state
|
||||
|
||||
def set_blinks_frames_ready(self, ready: bool):
|
||||
self.blinks_frames_ready = ready
|
||||
|
||||
def get_desired_blink_state(self) -> int:
|
||||
return self.desired_blink_state
|
||||
|
||||
def get_blinks_frames_ready(self) -> bool:
|
||||
return self.blinks_frames_ready
|
||||
|
||||
|
||||
class Point2D:
|
||||
x = 0
|
||||
y = 0
|
||||
|
@ -156,24 +194,24 @@ def interpolate_point_pairs(pairs: list[tuple[Point2D, Point2D]], percentage: fl
|
|||
return interpolated_points
|
||||
|
||||
|
||||
def update_screen():
|
||||
# TODO move blinking animation logic to the ProotState class
|
||||
global blinkFrameCanvases, matrix
|
||||
|
||||
proot_state = ProotState()
|
||||
|
||||
if proot_state.get_blinks_frames_ready():
|
||||
matrix.SwapOnVSync(blinkFrameCanvases[proot_state.next_blink_frame_number()])
|
||||
|
||||
|
||||
def interrupt_timer():
|
||||
while True:
|
||||
update_screen()
|
||||
time.sleep(0.01)
|
||||
|
||||
|
||||
print("start configuring matrix")
|
||||
startT = curr_time = round(time.time()*1000)
|
||||
|
||||
# Configuration for the matrix
|
||||
options = RGBMatrixOptions()
|
||||
options.rows = 32
|
||||
options.cols = 64
|
||||
options.chain_length = 2
|
||||
options.parallel = 1
|
||||
options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat'
|
||||
matrix = RGBMatrix(options=options)
|
||||
|
||||
endT = curr_time = round(time.time()*1000)
|
||||
print("configuring matrix took: " + str(endT - startT) + " ms")
|
||||
# Create and start the interrupt thread
|
||||
screen_update_thread = threading.Thread(target=interrupt_timer)
|
||||
screen_update_thread.start()
|
||||
|
||||
|
||||
print("start setting ProotScreen")
|
||||
|
@ -215,8 +253,6 @@ print("generating pixel array took: " + str(endT - startT) + " ms")
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
print("start pairing points for one eye")
|
||||
startT = curr_time = round(time.time()*1000)
|
||||
|
||||
|
@ -228,11 +264,6 @@ endT = curr_time = round(time.time()*1000)
|
|||
print("pairing points for one eye took: " + str(endT - startT) + " ms")
|
||||
|
||||
|
||||
DesiredBlinkState = 10
|
||||
currentBlinkState = 0
|
||||
|
||||
blinkFrameCanvases = []
|
||||
|
||||
|
||||
print("start populating matrices for each blink frame")
|
||||
startT = curr_time = round(time.time()*1000)
|
||||
|
@ -254,30 +285,9 @@ for alpha in range(0,11):
|
|||
endT = curr_time = round(time.time()*1000)
|
||||
print("populating matrices for each blink frame took: " + str(endT - startT) + " ms")
|
||||
|
||||
proot_state = ProotState()
|
||||
|
||||
def update_screen():
|
||||
# TODO move blinking animation logic to the ProotState class
|
||||
global DesiredBlinkState, currentBlinkState, blinkFrameCanvases, matrix
|
||||
|
||||
# open eye again after blink
|
||||
if currentBlinkState == 10:
|
||||
DesiredBlinkState = 0
|
||||
|
||||
if currentBlinkState == DesiredBlinkState:
|
||||
next_canvas = blinkFrameCanvases[currentBlinkState]
|
||||
next_canvas = matrix.SwapOnVSync(next_canvas)
|
||||
return
|
||||
|
||||
|
||||
next_canvas = blinkFrameCanvases[currentBlinkState]
|
||||
|
||||
if currentBlinkState < DesiredBlinkState:
|
||||
currentBlinkState += 1
|
||||
else:
|
||||
currentBlinkState -= 1
|
||||
|
||||
next_canvas = matrix.SwapOnVSync(next_canvas)
|
||||
|
||||
proot_state.set_blinks_frames_ready(True)
|
||||
|
||||
|
||||
|
||||
|
@ -290,8 +300,8 @@ def on_connect(client, userdata, flags, response_code):
|
|||
def on_message(client, userdata, message):
|
||||
print("Received message '" + str(message.payload) + "' on topic '"
|
||||
+ message.topic + "' with QoS " + str(message.qos))
|
||||
global DesiredBlinkState
|
||||
DesiredBlinkState = 10
|
||||
proot_state = ProotState()
|
||||
proot_state.set_desired_blink_state(10)
|
||||
|
||||
# MQTT broker configuration
|
||||
broker_address = "10.1.13.173" # Replace with your MQTT broker's address
|
||||
|
@ -309,9 +319,7 @@ client.loop_start()
|
|||
|
||||
while True:
|
||||
# this sleep sets the time between finishing one screen update and the next starting
|
||||
# TODO replace this mechanism with an interupt to use the cpu time between frame updates.
|
||||
time.sleep(0.01)
|
||||
update_screen()
|
||||
pass
|
||||
|
||||
|
||||
# TODO create a splash screen to display super quick before the rest of the assets are loading
|
||||
|
|
Loading…
Add table
Reference in a new issue