Author: Sander
Script Version: 1
Minimum CoyoteMIDI Build: 562
script default hidden
return
// --- Change the on state of the light. ---
script Change_On
$id = (runscript default get_light_from_name name:($parameters change_on_light))
$on = ($parameters change_on_state)
if $on == toggle
$lights = (runscript default get_lights)
$on = (lowercase ($lights $id state on) != true)
else
$on = (lowercase ($on == On))
runscript default change_light_state id:$id,on:$on
// --- Change the colour of the light. ---
script Change_Color
$id = (runscript default get_light_from_name name:($parameters change_color_light))
$hue = ($parameters change_color_hue)
$sat = ($parameters change_color_sat)
$bri = ($parameters change_color_bri)
$time = ($parameters change_color_time)
$time = (round ($time * 10))
$rgb = (runscript default get_rgb hue:$hue,sat:$sat,bri:$bri)
indicator true $rgb 1 5000
runscript default change_light_state id:$id,hue:$hue,sat:$sat,bri:$bri,time:$time
// --- Change the brightness of the light, taking the brightness directly from the MIDI data. ---
script Brightness_From_MIDI
if $trigger !contains rawvalue
messagebox "This subscript should only be ran by a translation triggered by MIDI!" "Invalid usage!"
return
$id = (runscript default get_light_from_name name:($parameters brightness_from_midi_light))
$bri = ($trigger rawvalue) * 2
runscript default change_light_state id:$id,hue:$hue,sat:$sat,bri:$bri,time:$time
event uirefreshed
configurationparameter reset_bridge (checkbox "Forget Pairing" "When selected, the script will delete any existing information of its pairing with a Philips Hue Bridge" false)
// only register the translation parameters when the script is paired because we need the light information.
if !(runscript default is_paired)
return
runscript default apply_parameters
event configchanged
// Allow for the bridge data to be reset, effectively removing the pairing to the bridge.
if ($trigger parameters reset_bridge) == true && (messagebox "Are you sure you want to forget your bridge pairing? All information of your Philips Hue bridge will be removed and the script will need to be paired again in order to work." "Reset Pairing?" yes,cancel) == yes
%phillips_hue_bridge_ip = ""
%phillips_hue_bridge_api_key = ""
event scriptsloaded
// Check if the script is paired with the bridge or not, and offer to pair if necessary.
if !(runscript default is_paired)
if (messagebox "CoyoteMIDI is not paired with your Philips Hue bridge. Pair now?" "Pair Philips Hue bridge?" yes,no) == yes
runscript default pair_bridge
// Update the light data.
if (runscript default is_paired)
runscript default get_lights
// --- Provides the parameters shown in translation settings. ---
script apply_parameters private
$light_selector = (combobox "Light" "Selects the light to change." (mapkeys %philips_hue_light_name_map) default 0.8)
translationparameter change_on_light $light_selector Change_On
translationparameter change_on_state (combobox "State" "The state to apply to the light" "On,Off,Toggle" "Toggle" 0.5) Change_On
translationparameter change_color_light $light_selector Change_Color
translationparameter change_color_hue (numberbox Hue "The hue of the color to apply to the light" 1 default false 0 65535 250) Change_Color
translationparameter change_color_sat (numberbox Saturation "The saturation of the color to apply to the light" 255 default false 0 255 1) Change_Color
translationparameter change_color_bri (numberbox Brightness "The brightness of the color to apply to the light" 255 default false 0 255 1) Change_Color
translationparameter change_color_time (numberbox "Transition Time" "The duration of the transition from the previous colour to the next, in seconds." 1 default true 0 default 1) Change_Color
translationparameter brightness_from_midi_light $light_selector Brightness_From_MIDI
// --- starts the bridge pairing procedure. ---
script pair_bridge private
// Get the bridge IP address.
$discover = (parsejson (web get "https://discovery.meethue.com"))
if $discover !contains 1 internalipaddress
messagebox "Failed to find a Philips Hue bridge." "Bridge not found!" ok
return
%phillips_hue_bridge_ip = ($discover 1 internalipaddress)
// create an API access key.
$bridge_api = "http://%{phillips_hue_bridge_ip}/api"
$device_info = (tojson devicetype:coyotescript#coyotemidi)
$register_json = (web post $bridge_api $device_info)
messagebox "Please press the button on your Philips Hue bridge to confirm pairing with CoyoteMIDI. Click OK after pairing." "Pair with bridge" ok
$register_json = (web post $bridge_api $device_info)
$register = (parsejson $register_json)
if $register !contains 1 success username
messagebox "Failed to pair CoyoteMIDI with your Philips Hue bridge." ok
%phillips_hue_bridge_ip = ""
%phillips_hue_bridge_api_key = ""
return
%phillips_hue_bridge_api_key = (encrypt ($register 1 success username))
messagebox "You've successfully paired CoyoteMIDI with your Philips Hue bridge." "Pairing success!" ok
runscript default get_lights
runscript default apply_parameters
// --- Returns whether the script is currently paired with the bridge. ---
script is_paired hidden
return %phillips_hue_bridge_ip != "" && %phillips_hue_bridge_api_key != ""
// --- Returns the available lights and their data, also caches them in a global variable. ---
script get_lights hidden
$apiKey = (decrypt %phillips_hue_bridge_api_key)
%philips_hue_light_data = (parsejson (web get "http://%{phillips_hue_bridge_ip}/api/${apiKey}/lights"))
%philips_hue_light_name_map new map
foreach $light_id : $light_data in %philips_hue_light_data
%philips_hue_light_name_map + ($light_data name):$light_id
return %philips_hue_light_data
// --- Returns the ID of a given light by name. ---
script get_light_from_name hidden
if !$name
error "No light name specified!"
if %philips_hue_light_name_map !contains $name
return -1
return (%philips_hue_light_name_map $name)
// --- converts Philips Hue's Hue/Sat/Bri into RGB. ---
script get_rgb hidden
if !$hue || !$sat || !$bri
error "Invalid arguments specified!"
$h = $hue / 65535 * 360
$s = $sat / 255
$v = $bri / 255
$c = $v * $s
$modh = $h / 60 - (floor ($h / 60))
$x = $c * (1 - (abs $modh - 1))
$m = $v - $c
$r1 = 0
$g1 = 0
$b1 = 0
if $h < 60
$r1 = $c
$g1 = $x
else if $h < 120
$r1 = $x
$g1 = $c
else if $h < 180
$g1 = $c
$b1 = $x
else if $h < 240
$g1 = $x
$b1 = $c
else if $h < 300
$r1 = $x
$b1 = $c
else
$r1 = $c
$b1 = $x
$result new map
$result + R: (($r1 + $m) * 255)
$result + G: (($g1 + $m) * 255)
$result + B: (($b1 + $m) * 255)
return $result
// --- Changes the state of a given light. ---
script change_light_state hidden
if !$id
error "No light ID specified!"
$body new map
if $on != ""
$body + on: $on
if $hue
$body + hue: $hue
if $sat
$body + sat: $sat
if $bri
$body + bri: $bri
if $time
$body + transitiontime:$time
$apiKey = (decrypt %phillips_hue_bridge_api_key)
web put "http://%{phillips_hue_bridge_ip}/api/${apiKey}/lights/${id}/state" (tojson $body)
7 downloads