lib/ws_light/strip.rb in ws_light-0.2.0 vs lib/ws_light/strip.rb in ws_light-0.3.0

- old
+ new

@@ -30,47 +30,38 @@ attr_accessor :direction, :last_event, :state, :current_set, :debug LENGTH = 160 TYPE = :double - DIRECTION_NONE = 0 - DIRECTION_LEFT = 1 - DIRECTION_RIGHT = 2 TIMEOUT = 12 - - STATE_OFF = :state_off - STATE_ON = :state_on - STATE_STARTING_UP = :state_starting_up - STATE_SHUTTING_DOWN = :state_shutting_down - + WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather?q=Hannover,de' FRAMES_PER_SECOND = 25 def initialize WS2801.length(Strip::TYPE == :double ? Strip::LENGTH * 2 : Strip::LENGTH) WS2801.autowrite(true) - update_daylight self_test @listen_thread = Thread.new { while true do check_timer; sleep 0.5; end } @last_event = Time.now - 3600 # set last event to a longer time ago - @state = STATE_OFF + @state = :state_off @debug = false @current_set = Set::ColorSet.new @current_set.color = Color.new(0,0,0) end def on(direction) @last_event = Time.now puts "triggered event 'on': #{last_event.to_f} from state #{@state}" if @debug - @state = STATE_STARTING_UP if @state == STATE_SHUTTING_DOWN - return if @state != STATE_OFF + @state = :state_starting_up if @state == :state_shutting_down + return if @state != :state_off puts 'Loading a new set...' if @debug @direction = direction - @state = STATE_STARTING_UP + @state = :state_starting_up case rand(100) when 0..3 set = Set::RainbowSet.new when 4..6 @@ -94,44 +85,44 @@ animation = animation_for(direction).new(@current_set, set) animate(animation) @current_set = set - @state = STATE_ON + @state = :state_on # Move show() into background, so we can accept new events on the main thread Thread.new { show(@current_set, animation.frames) } end def off(direction = nil) puts "triggered event 'off': #{Time.now.to_f} during state #{@state}" if @debug - return if @state != STATE_ON + return if @state != :state_on - @state = STATE_SHUTTING_DOWN + @state = :state_shutting_down sleep 0.2 @direction = direction if direction set = Set::ColorSet.new set.color = Color.by_name :black animation = animation_for(@direction).new(@current_set, set) if animate(animation) - @state = STATE_OFF + @state = :state_off @current_set = set else - @state = STATE_ON + @state = :state_on Thread.new { show(@current_set, animation.frames) } end puts "finished shutting off: #{Time.now.to_f}" if @debug end def animation_for(direction) return Animation::FadeAnimation if night? - if direction == DIRECTION_LEFT + if direction == :direction_left Animation::SlideLeftAnimation else Animation::SlideRightAnimation end end @@ -171,26 +162,13 @@ end end def night? time = Time.now - time.to_i < (@daylight[:start] - 3600) || time.to_i > (@daylight[:end] + 3600) + time.hour > 22 || time.hour < 6 end - # Gets the sunset/sunrise data - # this might get out of sync when the clock is not set correctly - # anyway, one day off is not a problem :) - def update_daylight - data = JSON.parse(open(WEATHER_URL).read) - @daylight = { - start: data['sys']['sunrise'].to_i, - end: data['sys']['sunset'].to_i, - day: Time.now.day - } - pp @daylight - end - def shutdown WS2801.set(r: 0, g: 0, b: 0) end def self_test @@ -202,13 +180,10 @@ sleep 1 WS2801.set(r: 0, g: 0, b: 0) end def check_timer - WS2801.set(r: 0, g: 0, b: 0) if @state == STATE_OFF - # Test after 2 a.m. to make sure we can the correct date even if our time is slightly off - time = Time.now - update_daylight if @daylight[:day] != time.day && time.hour > 1 + WS2801.set(r: 0, g: 0, b: 0) if @state == :state_off off if timeout? end def timeout? @last_event < (Time.now - TIMEOUT)