Created
December 15, 2017 23:58
-
-
Save moozer/96d9118c03d1742b3e18ad1e15edb346 to your computer and use it in GitHub Desktop.
Setting colors on a Cosna LED speaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import bluetooth | |
import serial | |
import time | |
import binascii | |
# bytes: | |
# 1 length: 0x0a | |
# 2 random: r | |
# 3 c_-94 xor r | |
# 4 source xor r (8 for picker, 18 for something else?) | |
# 5 red xor r | |
# 6 green xor r | |
# 7 blue xor r | |
# 8 0 xor r | |
# 9 0 xor r | |
# 10 checksum: 0- (length+r+b3+b4+b6+b7+b8+b9+b10) | |
def generate_hexstring( r, g, b, random = 0xff, source = 8 ): | |
length, rc94, rsource = 0x0a, (256-94)^random, source^random | |
rred, rblue, rgreen = r^random, g^random, b^random | |
rzero = 0^random | |
checksum = (length+random+rc94+rsource+rred+rgreen+rblue+rzero+rzero)%256 | |
return bytearray( [length, random, rc94, rsource, rred, | |
rgreen, rblue, rzero, rzero, 256-checksum ] ) | |
bt_remote_addr = "00:E0:4C:XX:XX:XX" | |
print "connecting and sending switch commands" | |
sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM) | |
sockfd.connect((bt_remote_addr, 1)) # BT Address | |
print "color" | |
hex = generate_hexstring( 0xf3, 0xf6, 0xf9 ) | |
sockfd.send( binascii.hexlify(hex).decode('hex') ) | |
sockfd.send('\r') | |
time.sleep( 3) | |
sockfd.send(chr(26)) # CTRL+Z | |
sockfd.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment