added get/set controller functions

This commit is contained in:
Giovanni Harting
2015-07-14 15:44:59 +02:00
parent 7656f7a39c
commit cd4de5f9aa
2 changed files with 38 additions and 6 deletions

View File

@@ -1,3 +1,25 @@
import smbus
PCA9685_SUBADR1 = 0x2
PCA9685_SUBADR2 = 0x3
PCA9685_SUBADR3 = 0x4
PCA9685_MODE1 = 0x00
PCA9685_MODE2 = 0x01
PCA9685_PRESCALE = 0xFE
PCA9685_RESET = 0xFE
LED0_ON_L = 0x06
LED0_ON_H = 0x07
LED0_OFF_L = 0x08
LED0_OFF_H = 0x09
ALLLED_ON_L = 0xFA
ALLLED_ON_H = 0xFB
ALLLED_OFF_L = 0xFC
ALLLED_OFF_H = 0xFD
class Controller:
"""
A controller controls a number of stripes.
@@ -22,6 +44,7 @@ class Controller:
self.pwm_freq = pwm_freq
self.channels = channels
self.i2c_device = i2c_device
self.bus = smbus.SMBus(i2c_device)
self.address = address
self.id = cid
self.db = db
@@ -36,6 +59,13 @@ class Controller:
def __repr__(self):
return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id)
def set_channel(self, channel, val):
self.bus.write_word_data(self.address, LED0_OFF_L + 4 * channel, val*4095)
self.bus.write_word_data(self.address, LED0_ON_L + 4 * channel, 0)
def get_channel(self, channel):
return self.bus.read_word_data(self.address, LED0_OFF_L + 4 * channel)
class Stripe:
"""

View File

@@ -90,9 +90,10 @@ class Daemon:
def handle_read(self):
data = self.recv(5120)
if data:
print(data)
try:
json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
json_decoded = json.loads(data)
json_decoded = json.loads(data.decode())
print(json.dumps(json_decoded, sort_keys=True, indent=4, separators=(',', ': ')))
if "action" in json_decoded:
if json_decoded['action'] == "set_color":
@@ -104,10 +105,11 @@ class Daemon:
elif json_decoded['action'] == "get_color":
# TODO: add stripe color get logic
print("recieved action: {}".format(json_decoded['action']))
except TypeError:
print("No JSON found!")
else:
print("no action found, ignoring")
else:
print("no action found, ignoring")
except (TypeError, ValueError):
print("No valid JSON found!")
class SocketServer(asyncore.dispatcher):
def __init__(self, host, port):