Skip to content

Instantly share code, notes, and snippets.

@Coro365 Coro365/air_purifier.rb
Last active Jan 13, 2019

Embed
What would you like to do?
Control air purifier by servo motor (Panasonic F-VX501)
def initialize_state
$mode_state_file = "#{__dir__}/mode_state_file"
$power_state_file = "#{__dir__}/power_state_file"
update_mode_state(1) unless FileTest.exist?($mode_state_file)
update_power_state(1) unless FileTest.exist?($power_state_file)
$mode_state = File.read($mode_state_file).to_i
$power_state = File.read($power_state_file).to_i
$mode_name = ["low", "mid", "high", "auto"]
end
def change_air_flow(target_mode)
n = target_mode - $mode_state
n = n + $mode_name.size if n.negative?
puts "#{$mode_name[$mode_state]} => #{$mode_name[target_mode]}"
puts n
mode_push(n)
update_mode_state(target_mode)
end
def power_change(t)
power_push unless $power_state == t
update_power_state(t)
end
def mode_push(n)
n.times do
puts "mode push!"
left_turn
sleep 1
init_position
sleep 1
end
end
def power_push
puts "power push!"
right_turn
sleep 1
init_position
sleep 1
end
def update_mode_state(state)
File.open($mode_state_file,"w") {|f|f.print state }
end
def update_power_state(state)
File.open($power_state_file,"w") {|f|f.print state }
end
def right_turn
`echo 0=70% > /dev/servoblaster`
end
def left_turn
`echo 0=35% > /dev/servoblaster`
end
def init_position
`echo 0=50% > /dev/servoblaster`
end
function = ARGV[0]
target = ARGV[1]
initialize_state
unless target.match(/[0123]/)
puts "ERROR: target number is 0-3"
exit
end
target = target.to_i
if function == "power"
power_change(target)
elsif function == "mode"
power_change(1) if $power_state == 0
change_air_flow(target)
else
puts "ERROR: function is power or mdoe"
end
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.