Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Control of brightness and color temperature in 10 steps (https://www.instagram.com/p/Bt8dhKcAxaJ/)
def initialize_state
$color_state_file = "#{__dir__}/color_state_file"
$brightness_state_file = "#{__dir__}/brightness_state_file"
update_color_state(10) unless FileTest.exist?($color_state_file)
update_brightness_state(10) unless FileTest.exist?($brightness_state_file)
$color_state = File.read($color_state_file).to_i
$brightness_state = File.read($brightness_state_file).to_i
end
def update_color_state(state)
File.open($color_state_file,"w") {|f|f.print state }
end
def update_brightness_state(state)
File.open($brightness_state_file,"w") {|f|f.print state }
end
def check_color_temp
ct = ARGV[0].to_i
unless ct >= 50 && ct <= 400
puts ("ERROR!: color temperature range.")
return
end
$target_color_temp = (ct - 50)/35
end
def check_brightness
bn = ARGV[1].to_i
unless bn >= 0 && bn <= 100
puts ("ERROR!: brightness range.")
return
end
$target_brightness = bn/10
end
def change_brightness
p diff = $target_brightness - $brightness_state
brightness_up(diff) if diff.positive?
brightness_down(diff) if diff.negative?
update_brightness_state($target_brightness)
end
def change_color_temp
p diff = $target_color_temp - $color_state
orange_up(diff) if diff.positive?
white_up(diff) if diff.negative?
update_color_state($target_color_temp)
end
def brightness_up(n)
`irsend -##{n} send_once 'light1' 'light_up'`
sleep 2
end
def brightness_down(n)
`irsend -##{n.abs} send_once 'light1' 'light_down'`
sleep 2
end
def orange_up(n)
`irsend -##{n} send_once 'light1' 'orange_up'`
sleep 2
end
def white_up(n)
`irsend -##{n.abs} send_once 'light1' 'white_up'`
sleep 2
end
check_color_temp
check_brightness
initialize_state
change_brightness unless $target_brightness.nil?
change_color_temp unless $target_color_temp.nil?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.