Skip to content

Instantly share code, notes, and snippets.

@SuperRoach
Created April 23, 2013 10:26
Show Gist options
  • Select an option

  • Save SuperRoach/5442454 to your computer and use it in GitHub Desktop.

Select an option

Save SuperRoach/5442454 to your computer and use it in GitHub Desktop.
This is a class that eventually talks to a light :) Looking in KickClass.py The code is fairly commented to the problems I'm running into, but... Line 71: I'm trying to set the length as "2 byte, high byte and low byte". Would the hardcoded example be correct for 3? Line 77: How can I correctly get the length of self.Data ? If I put two hex char…
# The Kick light - Python interface to control it via the Wireless api.
#
# Before using this class, it's currently required for you to connect
# your kick to the device running this, using AD-HOC. If the connection drops,
# you have Wi-Fi drivers that do not support AD-HOC mode.
#
# Changing colour will have no effect unless the light is visible.
import binascii
import socket
import struct
import sys
# HOW IT WORKS:
# A packet is sent containing:
# Marker Address Length Command Data
# All of these are fixed except the last three.
# - The length should be split into a "high and low byte"
# - The command is always one byte.
# - The data is usually one or two bytes.
# Source of knowledge: Page 2 of the Kick API:
# https://docs.google.com/document/d/1TnYrs8QB-Gi5ReVWuMrgrOhmeV3k2KEGuIERLUjjRqI/edit?pli=1
class KickLight:
def __init__(self):
# The following data does not change between each command.
self.Marker = 'RL'
self.SlaveAddress = '\x00\x00\x00\x00'
self.IpAddress = "169.254.255.255"
self.live = False # Change to true if actually connecting.
if self.live:
self.Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.ServerAddress = ('169.254.255.255', 8080)
self.Sock.connect(self.ServerAddress)
print "Live, connected."
else:
print "Not live - Just pretending to send data."
def __del__(self):
print >>sys.stderr, 'closing socket'
self.Sock.close()
def colortemp(self, temp):
# Sets the color temperature in Kelvin.
# Typical values are between 2500 and 10000.
# This is the data command to change the colour temp.
self.Command = '\x05'
#self.Data = '\x1000' # 200 in int
self.Data = hex(temp)
self.SendCommand()
return 'color temp %s is %r in hex.' % (temp, self.Data)
def brightness(self, intensity):
# Set's the brightness level
self.Command = '\x06'
self.Data = hex(intensity)
self.SendCommand()
def SendCommand(self):
# You need to set data before this at the moment.
# Hardcoded to test.
self.Length = Length = '\x00\x03' # High byte, Low byte. Should be Length of data +1 for the command.
#self.Length = Length = len(self.Data) # It should be high byte and low byte. Data Length + 1 for command
#self.Length = '\x00', str(len(self.Data) + 1)
#self.Length = '\x00', str(1+1)
print 'The length of the data is apparently %s' % (str(len(self.Data) + 1))
values = (self.Marker, self.SlaveAddress, self.Length, self.Command, self.Data)
# Am I correctly setting self.length? (third value should be two bytes.)
Packer = struct.Struct('2s 4s 2s 2s 2s')
PackedData = Packer.pack(*values)
try:
# Send data
print >>sys.stderr, 'sending "%s"' % binascii.hexlify(PackedData), values
if self.live:
self.Sock.sendall(PackedData)
finally:
print >>sys.stderr, 'Sent.'
#self.Sock.close()
# This is to actually run the class. See the class itself, but I am looking to correctly use the struct.
import random
import time
import KickClass as k
light = k.KickLight()
# Give it a new number each time to show it changing. Needs to be two bytes.
light.colortemp(random.randrange(1000,4000))
# Being Sent: sending "524c00000000000305003078" ('RL', '\x00\x00\x00\x00', '\x00\x03', '\x05', '0x826')
time.sleep(2)
# I'm not correctly sending this as one byte I don't think. Range is 0-255 (one byte)
light.brightness(155)
# Being sent: sending "524c00000000000306003078" ('RL', '\x00\x00\x00\x00', '\x00\x03', '\x06', '0x9b')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment