ws_light 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbf069c0977c0d9f8df28c61681c404780820a38
4
- data.tar.gz: 5119cbe7ce8d5014ff8b9b63c299a68ef266dcb9
3
+ metadata.gz: c759d2c4fc062deccebc4190aa531af5f388cdd1
4
+ data.tar.gz: 0d471ef1d1f9b3988187f26e3efa57283df1731b
5
5
  SHA512:
6
- metadata.gz: 9792d608a3f9859c70ff2c5319d6abb4480f21a4b39bbb7385c735fdbbe16e274184e1d45e347c2880ea38be1e6557ca4c9128d56a672b88f6fb2b652c9f9e5e
7
- data.tar.gz: 2adef99d7dee1b1f081d510189535ab4b813697252e19bf9f1cd48e9a97971a9ef1e3428e8e9fa0da5d68e94a3d38f26a9b15b5f23e0aff3d542f52e7f6e9d0f
6
+ metadata.gz: 5c643c0a4992cb7310469cc5c7070d791906836267b0b24d1f2b3b5e1269902f6e626d4c687ad0a5cf0d0f05241504df7a8ad7872cb1cce5283fcb30dba71062
7
+ data.tar.gz: 78f81988a236233f7e64ad735f61a6edba4d9ddf952b2e564f3904bc9103ae910db20788acd12bee5aa2d9c22a921f7d9b23b18ec31637769ba0abb0fd047deb
data/.gitignore CHANGED
@@ -2,4 +2,7 @@
2
2
  *.log
3
3
 
4
4
  # Ignore built gems
5
- *.gem
5
+ *.gem
6
+
7
+ # Ignore the lock file
8
+ Gemfile.lock
data/bin/ws_light CHANGED
@@ -24,15 +24,25 @@ require 'bundler'
24
24
  require 'ws_light/strip'
25
25
  require 'ws_light/sd_logger'
26
26
 
27
+ PIN_RIGHT = 23
28
+ PIN_LEFT = 24
29
+
27
30
  logger = WSLight::SDLogger.new
28
31
  logger.debug = DEBUG
29
- logger.filename = 'motion.log'
32
+ logger.filename = '/var/log/motion.log'
30
33
  logger.log 'Starting up'
31
34
 
35
+ def unregister_pins
36
+ [PIN_RIGHT, PIN_LEFT].each do |pin|
37
+ File.open('/sys/class/gpio/unexport', 'w') { |f| f.write("#{pin}") }
38
+ end
39
+ end
40
+
32
41
  # Trap ^C
33
42
  Signal.trap('INT') {
34
43
  logger.log 'Shutting down'
35
44
  logger.write_log
45
+ unregister_pins
36
46
  exit
37
47
  }
38
48
 
@@ -40,31 +50,21 @@ Signal.trap('INT') {
40
50
  Signal.trap('TERM') {
41
51
  logger.log 'Shutting down'
42
52
  logger.write_log
53
+ unregister_pins
43
54
  exit
44
55
  }
45
56
 
46
57
  strip = WSLight::Strip.new
47
58
  strip.debug = DEBUG
48
59
 
49
- pin_right = PiPiper::Pin.new(:pin => 22, :direction => :in)
50
- pin_left = PiPiper::Pin.new(:pin => 23, :direction => :in)
51
-
52
- after :pin => 23, :goes => :high do
60
+ after :pin => PIN_RIGHT, :goes => :high do
53
61
  logger.log('Motion detected: RIGHT')
54
- strip.on(WSLight::Strip::DIRECTION_RIGHT)
55
- while pin_right.on?
56
- strip.on(WSLight::Strip::DIRECTION_RIGHT)
57
- sleep 1
58
- end
62
+ strip.on(:direction_right)
59
63
  end
60
64
 
61
- after :pin => 24, :goes => :high do
65
+ after :pin => PIN_LEFT, :goes => :high do
62
66
  logger.log('Motion detected: LEFT')
63
- strip.on(WSLight::Strip::DIRECTION_LEFT)
64
- while pin_left.on?
65
- strip.on(WSLight::Strip::DIRECTION_LEFT)
66
- sleep 1
67
- end
67
+ strip.on(:direction_left)
68
68
  end
69
69
 
70
70
  PiPiper.wait
@@ -5,7 +5,7 @@ module WSLight
5
5
  attr_accessor :entries, :interval, :entries, :debug, :filename
6
6
 
7
7
  def initialize
8
- @filename = 'log.txt'
8
+ @filename = '/var/log/motion.log'
9
9
  @interval = 1800 # log interval in seconds
10
10
  @entries = []
11
11
  @last_write = Time.now
@@ -26,6 +26,8 @@ module WSLight
26
26
 
27
27
  set = sprinkle_nuts(set)
28
28
 
29
+ set.reverse! if rand(2) == 0
30
+
29
31
  type == :double ? set + set.reverse : set
30
32
  end
31
33
 
@@ -18,7 +18,7 @@ module WSLight
18
18
  if i < length_red
19
19
  set << Color.new((rand(25) < 1 ? 0 : 255), 0, 0)
20
20
  elsif i < length_red + length_red_to_white
21
- ratio = (i - length_red) / (length_red + length_red_to_white)
21
+ ratio = (i - length_red) / length_red_to_white.to_f
22
22
  set << red.mix(white, ratio)
23
23
  elsif i < length_red + length_red_to_white + length_white
24
24
  set << white
@@ -27,6 +27,8 @@ module WSLight
27
27
  end
28
28
  end
29
29
 
30
+ set.reverse! if rand(2) == 0
31
+
30
32
  type == :double ? set + set.reverse : set
31
33
  end
32
34
 
@@ -32,16 +32,8 @@ module WSLight
32
32
  LENGTH = 160
33
33
  TYPE = :double
34
34
 
35
- DIRECTION_NONE = 0
36
- DIRECTION_LEFT = 1
37
- DIRECTION_RIGHT = 2
38
35
  TIMEOUT = 12
39
-
40
- STATE_OFF = :state_off
41
- STATE_ON = :state_on
42
- STATE_STARTING_UP = :state_starting_up
43
- STATE_SHUTTING_DOWN = :state_shutting_down
44
-
36
+
45
37
  WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather?q=Hannover,de'
46
38
 
47
39
  FRAMES_PER_SECOND = 25
@@ -49,11 +41,10 @@ module WSLight
49
41
  def initialize
50
42
  WS2801.length(Strip::TYPE == :double ? Strip::LENGTH * 2 : Strip::LENGTH)
51
43
  WS2801.autowrite(true)
52
- update_daylight
53
44
  self_test
54
45
  @listen_thread = Thread.new { while true do check_timer; sleep 0.5; end }
55
46
  @last_event = Time.now - 3600 # set last event to a longer time ago
56
- @state = STATE_OFF
47
+ @state = :state_off
57
48
  @debug = false
58
49
  @current_set = Set::ColorSet.new
59
50
  @current_set.color = Color.new(0,0,0)
@@ -62,13 +53,13 @@ module WSLight
62
53
  def on(direction)
63
54
  @last_event = Time.now
64
55
  puts "triggered event 'on': #{last_event.to_f} from state #{@state}" if @debug
65
- @state = STATE_STARTING_UP if @state == STATE_SHUTTING_DOWN
66
- return if @state != STATE_OFF
56
+ @state = :state_starting_up if @state == :state_shutting_down
57
+ return if @state != :state_off
67
58
 
68
59
  puts 'Loading a new set...' if @debug
69
60
 
70
61
  @direction = direction
71
- @state = STATE_STARTING_UP
62
+ @state = :state_starting_up
72
63
 
73
64
  case rand(100)
74
65
  when 0..3
@@ -96,7 +87,7 @@ module WSLight
96
87
  animate(animation)
97
88
  @current_set = set
98
89
 
99
- @state = STATE_ON
90
+ @state = :state_on
100
91
 
101
92
  # Move show() into background, so we can accept new events on the main thread
102
93
  Thread.new { show(@current_set, animation.frames) }
@@ -104,9 +95,9 @@ module WSLight
104
95
 
105
96
  def off(direction = nil)
106
97
  puts "triggered event 'off': #{Time.now.to_f} during state #{@state}" if @debug
107
- return if @state != STATE_ON
98
+ return if @state != :state_on
108
99
 
109
- @state = STATE_SHUTTING_DOWN
100
+ @state = :state_shutting_down
110
101
  sleep 0.2
111
102
  @direction = direction if direction
112
103
 
@@ -116,10 +107,10 @@ module WSLight
116
107
  animation = animation_for(@direction).new(@current_set, set)
117
108
 
118
109
  if animate(animation)
119
- @state = STATE_OFF
110
+ @state = :state_off
120
111
  @current_set = set
121
112
  else
122
- @state = STATE_ON
113
+ @state = :state_on
123
114
  Thread.new { show(@current_set, animation.frames) }
124
115
  end
125
116
 
@@ -129,7 +120,7 @@ module WSLight
129
120
  def animation_for(direction)
130
121
  return Animation::FadeAnimation if night?
131
122
 
132
- if direction == DIRECTION_LEFT
123
+ if direction == :direction_left
133
124
  Animation::SlideLeftAnimation
134
125
  else
135
126
  Animation::SlideRightAnimation
@@ -173,20 +164,7 @@ module WSLight
173
164
 
174
165
  def night?
175
166
  time = Time.now
176
- time.to_i < (@daylight[:start] - 3600) || time.to_i > (@daylight[:end] + 3600)
177
- end
178
-
179
- # Gets the sunset/sunrise data
180
- # this might get out of sync when the clock is not set correctly
181
- # anyway, one day off is not a problem :)
182
- def update_daylight
183
- data = JSON.parse(open(WEATHER_URL).read)
184
- @daylight = {
185
- start: data['sys']['sunrise'].to_i,
186
- end: data['sys']['sunset'].to_i,
187
- day: Time.now.day
188
- }
189
- pp @daylight
167
+ time.hour > 22 || time.hour < 6
190
168
  end
191
169
 
192
170
  def shutdown
@@ -204,10 +182,7 @@ module WSLight
204
182
  end
205
183
 
206
184
  def check_timer
207
- WS2801.set(r: 0, g: 0, b: 0) if @state == STATE_OFF
208
- # Test after 2 a.m. to make sure we can the correct date even if our time is slightly off
209
- time = Time.now
210
- update_daylight if @daylight[:day] != time.day && time.hour > 1
185
+ WS2801.set(r: 0, g: 0, b: 0) if @state == :state_off
211
186
  off if timeout?
212
187
  end
213
188
 
@@ -1,4 +1,4 @@
1
1
  # Provides a global version number
2
2
  module WSLight
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ws_light
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerrit Visscher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-06 00:00:00.000000000 Z
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.4.5
110
+ rubygems_version: 2.4.5.1
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: A lighting gem for WS2801 led strips.