Fixed a byte encoding error that occurred in Python 3

Fixed an error that caused strange byte encoding in Python 3. This error
was caused by a mistake in porting code from Python 2.7 to Python 3 and
would cause strange LED flickering behaviour. The LED update code should
now work properly in Python 2.7 and Python 3.
This commit is contained in:
Scott Lawson 2016-12-31 11:06:09 -07:00
parent 3504d0b9c0
commit 0f5c05339c

View File

@ -16,15 +16,18 @@ pixels = np.tile(1, (3, config.N_PIXELS))
def update(): def update():
global pixels, _prev_pixels global pixels, _prev_pixels
pixels = np.clip(pixels, 0, 255).astype(int) pixels = np.clip(pixels, 0, 255).astype(int)
m = ''
p = _gamma[pixels] if config.GAMMA_CORRECTION else np.copy(pixels) p = _gamma[pixels] if config.GAMMA_CORRECTION else np.copy(pixels)
m = []
for i in range(config.N_PIXELS): for i in range(config.N_PIXELS):
# Ignore pixels if they haven't changed (saves bandwidth) # Ignore pixels if they haven't changed (saves bandwidth)
if np.array_equal(p[:, i], _prev_pixels[:, i]): if np.array_equal(p[:, i], _prev_pixels[:, i]):
continue continue
m += chr(i) + chr(p[0][i]) + chr(p[1][i]) + chr(p[2][i]) m.append(i) # Index of pixel to change
m.append(p[0][i]) # Pixel red value
m.append(p[1][i]) # Pixel green value
m.append(p[2][i]) # Pixel blue value
_prev_pixels = np.copy(p) _prev_pixels = np.copy(p)
_sock.sendto(m.encode(), (config.UDP_IP, config.UDP_PORT)) _sock.sendto(bytes(m), (config.UDP_IP, config.UDP_PORT))
# Execute this file to run a LED strand test # Execute this file to run a LED strand test
@ -42,4 +45,3 @@ if __name__ == '__main__':
pixels = np.roll(pixels, 1, axis=1) pixels = np.roll(pixels, 1, axis=1)
update() update()
time.sleep(0.2) time.sleep(0.2)