I'm trying to create a controller for Wiz Light Bulbs that use the sliders for different colors using a raspberry pi. My issue is that I can't get the MCP3008 module to read more than one channel without throwing an error in it's backend programs.
Here is the entire code, complete with snarky comments:
Code: Select all
#This little test allows me to change the color of a thing using the sliders.
import colour #This is the module that defines Colo(u)r objects allowing me to store them. Here's where this gets stupid. So, the module I want isn't a built-in, which is okay. But python has a module built-in called "color," which does a completely different thing than "colour." The built-in module influences colors on the terminal, while this one allows colors to be defined as objects. I hate everything.
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import asyncio
from pywizlight import wizlight, PilotBuilder, discovery
#I want to put this in a function. But scope is weird.
#CHAPTER 1: INITIALIZE THE POT
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
#Start the Channels. This is where I encounter the problem.
redChan = MCP.MCP3008(mcp,MCP.P0) #Pot 0 controls red
greenChan = MCP.MCP3008(mcp,MCP.P1) #Pot 1 controls green
blueChan = MCP.MCP3008(mcp,MCP.P2) #Pot 2 controls hamsters
#CHAPTER 2: C(O/U)L(O(U/E)/(U/E))R
#Initiate the color object to hold our color.
col = Color(redChan,greenChan,blueChan) #NOW WAIT A SECOND! I THOUGHT THE MODULE WAS CALLED COLOUR! WHY IS THE OBJECT NAME FROM THE COLOUR PACKAGE SPELLED COLOR!? WHAT IS THIS MADNESS!?
#CHAPTER 3: GRAB OUR COLORS!!
def remap_range(value, left_min, left_max, right_min, right_max):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
left_span = left_max - left_min
right_span = right_max - right_min
# Convert the left range into a 0-1 range (int)
valueScaled = int(value - left_min) / int(left_span)
# Convert the 0-1 range into a value in the right range.
return int(right_min + (valueScaled * right_span))
def getColorPot():
col.red = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.
col.green = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.
col.blue = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.
#CHAPTER 4: THE LIGHTS LMFAO
bulbs = [] #Make the bulbs a global variable so all coroutines can access it.
async def discover():
global bulbs
#Use UDP to find all the bulbs.
bulbs = await discovery.discover_lights(broadcast_space="192.168.1.255")
#read the bulbs
for bulb in bulbs:
print(bulb.__dict__)
async def set_color(r,g,b): #Subroutine for setting light colors. Must be passed RGB values.
print("setting lights")
await asyncio.gather(bulbs[0].turn_on(PilotBuilder(rgb = (r,g,b))),bulbs[1].turn_on(PilotBuilder(rgb = (r,g,b))), loop = loop)
#RUNTIME PART
loop = asyncio.get_event_loop()
loop.run_until_complete(discover())
colorLoop = asyncio.get_event_loop()
while(True):
getColorPot()
set_color(col.red,col.green,col.blue)
Code: Select all
File "PiLight/color.py", line 25, in <module>
greenChan = MCP.MCP3008(mcp,MCP.P1) #Pot 1 controls green
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/mcp3008.py", line 61, in __init__
super().__init__(spi_bus, cs, ref_voltage=ref_voltage)
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/mcp3xxx.py", line 57, in __init__
self._spi_device = SPIDevice(spi_bus, cs)
File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/spi_device.py", line 70, in __init__
self.chip_select.switch_to_output(value=True)
AttributeError: 'int' object has no attribute 'switch_to_output'