Skip to content

Commit

Permalink
animation thread #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohni committed Mar 27, 2022
1 parent 2ed03a8 commit b36cfeb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
22 changes: 18 additions & 4 deletions ESP32/LEDUtils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import neopixel
import machine
import random

#
# 0 - 1 - 2
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(self, led_count: int):
self.rotation = 1
self.color = (10, 5, 6)

async def writeStringToMatrix(self, s: str):
def writeStringToMatrix(self, s: str):
self.np.fill((0, 0, 0))
for pos, char in enumerate(s):
seperator = True
Expand All @@ -74,7 +75,7 @@ async def writeStringToMatrix(self, s: str):

position = font.get(char)
self.writeCharToMatrix(position, pos, seperator)
self.np.write()
# self.np.write()

def writeCharToMatrix(self, char: [], char_position, seperator: bool):
# width of matrix : 32, char width : 3 + 1 padding
Expand Down Expand Up @@ -109,14 +110,27 @@ def writeCharToMatrix(self, char: [], char_position, seperator: bool):
elif pos < 6:
self.np[startIndex - (row_pos * 2 - 1) - (pos - 3) - 2] = txt_color
elif pos < 9:
self.np[startIndex - matrixWidth*2 + (pos - 6)] = txt_color
self.np[startIndex - matrixWidth * 2 + (pos - 6)] = txt_color
elif pos < 12:
self.np[startIndex - (row_pos + matrixWidth) * 2 - 1 - (pos - 9)] = txt_color
else:
self.np[startIndex - matrixWidth * 4 + (pos - 12)] = txt_color

return

def animateTxt(self):
for pos, val in enumerate(self.np):
col = self.color

i1 = random.randint(0, 2)
i2 = random.randint(0, 2)
i3 = random.randint(0, 2)

if val != (0, 0, 0):
a = (col[0] + i1, col[1] + i2, col[2] + i3)
self.np[pos] = a
self.np.write()

async def runningText(self, s, padding):
self.np.fill((0, 0, 0))
matrixWidth = 32
Expand Down Expand Up @@ -150,4 +164,4 @@ async def runningText(self, s, padding):
if index >= matrixWidth * 5:
index = matrixWidth * 4 + (index - matrixWidth * 5)

self.np[index] = (10, 10, 10)
self.np[index] = (10, 10, 10)
3 changes: 1 addition & 2 deletions ESP32/LedMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dht
from Persistence import Persistence
from LEDUtils import LEDUtils
import uasyncio
import MatrixSocket


Expand Down Expand Up @@ -75,7 +74,7 @@ def run(self):

print(tmp_color)
self.ledUtil.color = tmp_color
uasyncio.run(self.ledUtil.writeStringToMatrix(tmp))
self.ledUtil.writeStringToMatrix(tmp)

current_min = local_time[4]
print(str(current_min) + ' ... ' + str(self.persistence.last_persisted_minute))
Expand Down
28 changes: 16 additions & 12 deletions ESP32/Persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,25 @@ def decodeWholeFile(self):
dataFile = open('sensorData.txt', 'rb')

body = ''
while True:
line = dataFile.read(5).split(':'.encode())
line_start = line[0].decode()

if line_start == '':
break
try:
while True:
line = dataFile.read(5).split(':'.encode())
line_start = line[0].decode()

if line_start.startswith('yy') or line_start.startswith('mm') \
or line_start.startswith('dd') or line_start.startswith('hh'):
body = body + line_start + ':' + str(int.from_bytes(line[1], 'big')) + '\n'
else:
body = body + str(int.from_bytes(line[0], 'big')) + ':' + str(
self.persistedBytesToValues(line[1])) + '\n'
if line_start == '':
break

dataFile.close()
if line_start.startswith('yy') or line_start.startswith('mm') \
or line_start.startswith('dd') or line_start.startswith('hh'):
body = body + line_start + ':' + str(int.from_bytes(line[1], 'big')) + '\n'
else:
body = body + str(int.from_bytes(line[0], 'big')) + ':' + str(
self.persistedBytesToValues(line[1])) + '\n'

dataFile.close()
except Exception as e:
print(e)
return body

def overrideFile(self, data):
Expand Down
18 changes: 17 additions & 1 deletion ESP32/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import MatrixSocket
import LedMain
import uasyncio
import _thread
import time

from LEDUtils import LEDUtils

sLock = _thread.allocate_lock()

# load wlan config
matrixSocket = MatrixSocket.MatrixSocket()
main = LedMain.LedMain(matrixSocket)

def animateMatrixThread():
while True:
main.ledUtil.animateTxt()
time.sleep_ms(50)

_thread.start_new_thread(animateMatrixThread, ())

while True:
sLock.acquire()
main.run()
time.sleep_ms(1000)
sLock.release()
time.sleep(1)

0 comments on commit b36cfeb

Please sign in to comment.