-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
functions = [] | ||
errorFunction = ["funcNotFound", "print Function not found"] | ||
|
||
def getFunction(name): | ||
for function in functions: | ||
if function[0] == name: | ||
return function | ||
return errorFunction | ||
|
||
def addFunction(name, code): | ||
functions.append([name, code]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import asyncio | ||
import time | ||
import threading | ||
import config | ||
import os | ||
from pywizlight import wizlight, PilotBuilder, discovery | ||
bulbs = [] | ||
|
||
async def initialize(broadcastSpace): | ||
unsortedIPs = [] | ||
|
||
#Add all bulbs to an unsorted list | ||
bulbsUnsorted = await discovery.discover_lights(broadcast_space=broadcastSpace) | ||
|
||
#Sorts all the bulbs alphabetically and appends them to a new list | ||
for x in bulbsUnsorted: | ||
unsortedIPs.append(x.ip) | ||
|
||
unsortedIPs = sorted(unsortedIPs) | ||
|
||
for x in unsortedIPs: | ||
for y in bulbsUnsorted: | ||
if x == y.ip: bulbs.append(y) | ||
|
||
async def changeColor(bulbIndex, color): | ||
if bulbIndex == "all": | ||
for bulb in bulbs: | ||
await bulb.turn_on(PilotBuilder(rgb = (color))) | ||
else: | ||
await bulbs[int(bulbIndex)-1].turn_on(PilotBuilder(rgb = (color))) | ||
|
||
async def setBrightness(bulbIndex, newBrightness): | ||
newBrightness = int(newBrightness) | ||
if bulbIndex == "all": | ||
for bulb in bulbs: | ||
await bulb.turn_on(PilotBuilder(brightness = newBrightness)) | ||
else: | ||
await bulbs[int(bulbIndex)-1].turn_on(PilotBuilder(brightness = newBrightness)) | ||
|
||
async def setScene(bulbIndex, scene): | ||
scene = int(scene) | ||
if bulbIndex == "all": | ||
for bulb in bulbs: | ||
await bulb.turn_on(PilotBuilder(scene = scene)) | ||
else: | ||
await bulbs[int(bulbIndex)-1].turn_on(PilotBuilder(scene = scene)) | ||
|
||
async def setOff(bulbIndex): | ||
if bulbIndex == "all": | ||
for bulb in bulbs: | ||
await bulb.turn_off() | ||
else: | ||
await bulbs[int(bulbIndex)-1].turn_off() | ||
|
||
async def setWhite(value, brightness, bulbIndex): | ||
brightness = int(brightness) | ||
newColor = PilotBuilder(cold_white = 255) #Defaults to cold white | ||
if value == "cold": newColor = PilotBuilder(cold_white = brightness) | ||
if value == "warm": newColor = PilotBuilder(warm_white = brightness) | ||
if bulbIndex == "all": | ||
for bulb in bulbs: | ||
await bulb.turn_on(newColor) | ||
else: | ||
await bulbs[int(bulbIndex)-1].turn_on(newColor) | ||
|
||
async def setRgb(value, bulbIndex): | ||
await changeColor(bulbIndex, (int(value[0]),int(value[1]),int(value[2]))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
variables = [["returned", 0]] | ||
|
||
def doesVariableExist(varName): | ||
for variable in variables: | ||
if variable[0] == varName: return True | ||
return False | ||
|
||
def getVariable(varName, returnInput=False, returnValue=False): | ||
for index, variable in enumerate(variables): | ||
if variable[0] == varName: | ||
if returnValue: return variable[1] | ||
return variable, index | ||
if not returnInput: return None | ||
return varName | ||
|
||
def getAllVariablesFromRgbInput(cmd): | ||
newValues = [] | ||
values = cmd[cmd.find("(")+1:cmd.find(")")] | ||
values = values.replace(" ", "") | ||
values = values.replace("*", "") | ||
values = values.split(",") | ||
for value in values: | ||
if getVariable(value) != None: | ||
newValues.append(getVariable(value)[0][1]) | ||
else: newValues.append(value) | ||
return newValues | ||
|
||
def addVariable(varName, varValue): | ||
if doesVariableExist(varName): return | ||
try: | ||
varValue = int(varValue) | ||
except: | ||
1+1 | ||
variables.append([varName, varValue]) | ||
|
||
#Math functions | ||
def setVariable(varName, newValue): | ||
varIndex = getVariable(varName)[1] | ||
try: | ||
newValue = int(newValue) | ||
except: | ||
1+1 | ||
if getVariable(newValue) == None: | ||
variables[varIndex][1] = newValue | ||
else: variables[varIndex][1] = getVariable(newValue)[0][1] | ||
|
||
def addToVariable(varName, newValue): | ||
varType = "num" | ||
varIndex = getVariable(varName)[1] | ||
varValue = variables[varIndex][1] | ||
try: | ||
varValue = int(varValue) | ||
except: | ||
return; | ||
if doesVariableExist(newValue): | ||
toAddIndex = getVariable(newValue)[1] | ||
variables[varIndex][1] = varValue + int(variables[toAddIndex][1]) | ||
return | ||
variables[varIndex][1] = varValue + int(newValue) | ||
|
||
def subFromVariable(varName, newValue): | ||
varType = "num" | ||
varIndex = getVariable(varName)[1] | ||
varValue = variables[varIndex][1] | ||
try: | ||
varValue = int(varValue) | ||
except: | ||
return; | ||
if doesVariableExist(newValue): | ||
toAddIndex = getVariable(newValue)[1] | ||
variables[varIndex][1] = varValue - int(variables[toAddIndex][1]) | ||
return | ||
variables[varIndex][1] = varValue - int(newValue) | ||
|
||
def compareVariable(varName, compareTo, operator): | ||
if getVariable(varName) == None: return | ||
try: | ||
compareTo = int(compareTo) | ||
except: | ||
1+1 | ||
varValue = getVariable(varName)[0][1] | ||
if getVariable(compareTo) != None: compareTo = getVariable(compareTo)[0][1] | ||
if operator == "==": return varValue == compareTo | ||
if operator == ">=": return varValue >= compareTo | ||
if operator == "<=": return varValue <= compareTo | ||
if operator == ">": return varValue > compareTo | ||
if operator == "<": return varValue < compareTo | ||
if operator == "!=": return varValue != compareTo | ||
|
||
|
||
#Printing | ||
def formatAndPrint(toPrint): | ||
finalString = "" | ||
variableToAdd = "" | ||
recordVariable = False | ||
|
||
for x in toPrint: | ||
if not recordVariable: | ||
if x == "*": | ||
recordVariable = True | ||
else: finalString += x | ||
else: | ||
if x == "*": | ||
recordVariable = False | ||
finalString += str(getVariable(variableToAdd)[0][1]) | ||
else: variableToAdd += x; | ||
|
||
print(finalString) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
broadcastSpace = "192.168.1.255" #Your lights' broadcast space | ||
loadedScript = "DefaultScript.bulb" #The bulb script to load |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import asyncio | ||
import sys | ||
import time | ||
import config | ||
import random | ||
from bulbScript_functions import bulbSet, bulbVariables, bulbFunctions | ||
from user_functions import customFunctions | ||
|
||
funcName = "" | ||
funcCode = [] | ||
recordCode = False | ||
skip = False | ||
|
||
async def initialize(script): | ||
await bulbSet.initialize(config.broadcastSpace) | ||
await parseCode(script) | ||
|
||
async def parseCommand(cmd): | ||
global funcName, funcCode, recordCode, skip | ||
cmdUnchanged = cmd | ||
cmd = cmd.split("//")[0] | ||
cmd = cmd.lower() | ||
splitCmd = cmd.split(" ") | ||
if splitCmd[0] == "end" and splitCmd[1] == "if" and skip == True: | ||
skip = False | ||
if skip: return | ||
if recordCode: funcCode.append(cmd) | ||
if splitCmd[0] == "func": | ||
funcName = splitCmd[1] | ||
recordCode = True | ||
if splitCmd[0] == "end" and splitCmd[1] == "func": | ||
funcCode.pop(-1) | ||
bulbFunctions.addFunction(funcName, funcCode) | ||
funcCode = [] | ||
recordCode = False | ||
if splitCmd[0] == "call": | ||
bulbFunction = bulbFunctions.getFunction(splitCmd[1])[1] | ||
if len(splitCmd) == 3 and splitCmd[2] != "": | ||
for i in range(0, int(splitCmd[2])): | ||
await parseCode(bulbFunction, True) | ||
return | ||
await parseCode(bulbFunction, True) | ||
|
||
if not recordCode: | ||
if splitCmd[0] == "setrgb": | ||
await bulbSet.setRgb(bulbVariables.getAllVariablesFromRgbInput(cmd), bulbVariables.getVariable(splitCmd[1], True, True)); | ||
if splitCmd[0] == "setbrightness": | ||
await bulbSet.setBrightness(bulbVariables.getVariable(splitCmd[1], True, True), bulbVariables.getVariable(splitCmd[2], True, True)); | ||
if splitCmd[0] == "setscene": | ||
await bulbSet.setScene(bulbVariables.getVariable(splitCmd[1], True, True), bulbVariables.getVariable(splitCmd[2], True, True)); | ||
if splitCmd[0] == "setwhite": | ||
await bulbSet.setWhite(splitCmd[2], bulbVariables.getVariable(splitCmd[3], True, True), bulbVariables.getVariable(splitCmd[1], True, True)); | ||
if splitCmd[0] == "setoff": | ||
await bulbSet.setOff(bulbVariables.getVariable(splitCmd[1], True, True)); | ||
if splitCmd[0] == "slp": | ||
await asyncio.sleep(float(splitCmd[1])); | ||
if splitCmd[0] == "var": | ||
varName = splitCmd[1] | ||
if len(splitCmd) > 2: | ||
varValue = cmd.split("=")[1] | ||
varValue = varValue.replace(" ", "") | ||
else: varValue = "" | ||
bulbVariables.addVariable(varName, varValue) | ||
if splitCmd[0] == "mov": | ||
bulbVariables.setVariable(splitCmd[1], splitCmd[2]) | ||
if splitCmd[0] == "add": | ||
bulbVariables.addToVariable(splitCmd[1], splitCmd[2]) | ||
if splitCmd[0] == "sub": | ||
bulbVariables.subFromVariable(splitCmd[1], splitCmd[2]) | ||
if splitCmd[0] == "exec": | ||
bulbVariables.setVariable("returned", customFunctions.runCustomFunction(cmdUnchanged[5:])) | ||
if splitCmd[0] == "print": | ||
bulbVariables.formatAndPrint(cmd[6:]) | ||
if splitCmd[0] == "rnd": | ||
rnd = random.randint(int(splitCmd[2]), int(splitCmd[3])) | ||
bulbVariables.setVariable(splitCmd[1], rnd) | ||
if splitCmd[0] == "stop": | ||
return "stop" | ||
if splitCmd[0] == "if": | ||
skip = not (bulbVariables.compareVariable(splitCmd[1], splitCmd[3], splitCmd[2])) | ||
|
||
async def parseCode(codeToParse, runOnce=False): | ||
loop = True | ||
lines = [] | ||
for line in codeToParse: | ||
line = line.format(line.strip()) | ||
line = line.rstrip() | ||
lines.append(line) | ||
|
||
if runOnce: | ||
for cmd in lines: | ||
if (await parseCommand(cmd)) == "stop": return | ||
return | ||
|
||
while loop: | ||
for cmd in lines: | ||
if (await parseCommand(cmd)) == "stop": loop = False; return | ||
|
||
sys.path.append('/bulbScript_functions') | ||
sys.path.append('/user_functions') | ||
asyncio.run(initialize(open("bulbScript_scripts/" + config.loadedScript, "r"))) |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from pywizlight import wizlight, PilotBuilder, discovery | ||
from bulbScript_functions import bulbVariables, bulbSet | ||
|
||
def runCustomFunction(func): | ||
variables = func[func.find("(")+1:func.find(")")] | ||
variables = variables.replace(" ", "") | ||
variables = variables.split(",") | ||
for i, variable in enumerate(variables): | ||
variables[i] = bulbVariables.getVariable(str(variable).lower(), True, True) | ||
addedVars = False | ||
func = func.split("(")[0] | ||
func += "(" | ||
for x in variables: | ||
func += str(x).lower() + ", " | ||
addedVars = True | ||
if addedVars: | ||
func = func[:-2] | ||
func += ")" | ||
print(func) | ||
return(eval(func)) #Returns the value which will be assigned to the default variable "returned" | ||
|
||
######ADD YOUR FUNCTIONS BELOW###### | ||
|
||
def exampleFunction(value): #See this function in use in the "Custom Function Example.bulb" script | ||
return value * 5 |