Skip to content

Commit

Permalink
For Python3 support: Always run strings through a Latin-1 converter t…
Browse files Browse the repository at this point in the history
…o bytes.
  • Loading branch information
cibomahto committed Jul 7, 2015
1 parent 9182c7d commit 657436f
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions BlinkyTape.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

import serial

# For Python3 support- always run strings through a bytes converter
import sys
if sys.version_info < (3,):
def b(x):
return x
else:
import codecs
def b(x):
return codecs.latin_1_encode(x)[0]


class BlinkyTape(object):
def __init__(self, port, ledCount=60, buffered=True):
Expand Down Expand Up @@ -60,7 +70,7 @@ def send_list(self, colors):
if b >= 255:
b = 254
data += chr(r) + chr(g) + chr(b)
self.serial.write(data)
self.serial.write(b(data))
self.show()

def sendPixel(self, r, g, b):
Expand Down Expand Up @@ -88,7 +98,7 @@ def sendPixel(self, r, g, b):
if self.buffered:
self.buf += data
else:
self.serial.write(data)
self.serial.write(b(data))
self.serial.flush()
self.position += 1
else:
Expand All @@ -102,10 +112,10 @@ def show(self):
"""
control = chr(0) + chr(0) + chr(255)
if self.buffered:
self.serial.write(self.buf + control)
self.serial.write(b(self.buf + control))
self.buf = ""
else:
self.serial.write(control)
self.serial.write(b(control))
self.serial.flush()
self.serial.flushInput() # Clear responses from BlinkyTape, if any
self.position = 0
Expand Down

0 comments on commit 657436f

Please sign in to comment.