Skip to content

Instantly share code, notes, and snippets.

@anishathalye
Created November 30, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anishathalye/81e54f4016651f9b67b0e16476ce70e3 to your computer and use it in GitHub Desktop.
Save anishathalye/81e54f4016651f9b67b0e16476ce70e3 to your computer and use it in GitHub Desktop.
import serial
import time
DEVICE = '/dev/ttyUSB0'
SPEED = '115200'
PWM_MAX = 20
def get_data(color, value):
assert color in 'RGB'
assert 0 <= value <= PWM_MAX
cbit = 'RGB'.index(color)
data = cbit << 6 | value
return chr(data)
def write_color(ser, color, value):
data = get_data(color, value)
ser.write(data)
def main():
ser = serial.Serial(DEVICE, SPEED)
ser.setDTR()
ser.flushInput()
ser.flushOutput()
write_color(ser, 'R', 0x0f)
time.sleep(0.5)
write_color(ser, 'B', 0x0f)
time.sleep(0.5)
write_color(ser, 'R', 0x00)
time.sleep(0.5)
write_color(ser, 'B', 0x00)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment