rpi_pwm 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d75263e424241d178c35a9a771cef5082671bd9a
4
- data.tar.gz: ac93b7747099b04d4a545289e89a248673830c3c
3
+ metadata.gz: 13c9033952c730a81a3bc76e0033d3f3389b0a09
4
+ data.tar.gz: b375773d796520cad8d1b4171cbc2156eddfe8a3
5
5
  SHA512:
6
- metadata.gz: bbcc2b9cb6798fd9696e92cd9b0c50f2e4ce11a7fd90456afaaec287b46e033f3481e03304385cf29ae5bdcb25d71f827b560e0092ef265a366f7cbee6ce4106
7
- data.tar.gz: 95900204ff252d0cab623860d7dcd1499b862e2a745078f3801406307d7281ebb0f162167ad783ddff1f261bdae164bd76b22be70812f409807e5ef8bae2b74d
6
+ metadata.gz: aa648dfe4a33da3ab53a2d98d913dfa0b761b27d396b2c37ea2f946f10562890eca6f4e2dc7e17ff06c1d42b5b227961e641972217ac7139373f76caf486f881
7
+ data.tar.gz: 6462cbfb7a1f4dd27d9701bbf1173e9c104962d6f11d53977886f72f55baeef1cff4a62e8d3cba8a810073f52e748fa663207d9840c544b9232dc0d97a7965b2
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/rpi_pwm.rb CHANGED
@@ -7,148 +7,104 @@ require 'rpi_gpio'
7
7
 
8
8
  class RPiPwm
9
9
 
10
+ HIGH = true
11
+ LOW = false
10
12
 
11
- class PinX
13
+ attr_accessor :duty_cycle, :freq
12
14
 
13
- HIGH = true
14
- LOW = false
15
15
 
16
- attr_accessor :duty_cycle, :freq
16
+ def initialize(pin_num, duty_cycle: 100, freq: 100)
17
17
 
18
+ @id, @duty_cycle, @freq = pin_num, duty_cycle, freq
18
19
 
19
- def initialize(pin_num, duty_cycle: 100, freq: 100)
20
+ RPi::GPIO.set_numbering :bcm
21
+ RPi::GPIO.setup pin_num, :as => :output
20
22
 
21
- @id, @duty_cycle, @freq = pin_num, duty_cycle, freq
23
+ @pwm = RPi::GPIO::PWM.new(pin_num, freq)
22
24
 
23
- RPi::GPIO.set_numbering :bcm
24
- RPi::GPIO.setup pin_num, :as => :output
25
+ @on, @blinking = false, false
25
26
 
26
- @pwm = RPi::GPIO::PWM.new(pin_num, freq)
27
- @pwm.frequency = 40
27
+ at_exit { RPi::GPIO.clean_up pin_num}
28
28
 
29
- @on, @blinking = false, false
29
+ end
30
30
 
31
- at_exit { RPi::GPIO.clean_up pin_num}
31
+ def duty_cycle=(val)
32
+ @duty_cycle = val
33
+ @pwm.duty_cycle = val
34
+ end
32
35
 
33
- end
36
+ def on(durationx=nil, duration: nil)
34
37
 
35
- def duty_cycle=(val)
36
- @duty_cycle = val
37
- @pwm.duty_cycle = val
38
- end
38
+ set_pin HIGH
39
+ @on = true
40
+ duration ||= durationx
41
+
42
+ @off_thread.exit if @off_thread
43
+ @on_thread = Thread.new {(sleep duration; self.off()) } if duration
39
44
 
40
- def on(durationx=nil, duration: nil)
45
+ end
41
46
 
42
- set_pin HIGH
43
- @on = true
44
- duration ||= durationx
45
-
46
- @off_thread.exit if @off_thread
47
- @on_thread = Thread.new {(sleep duration; self.off()) } if duration
47
+ def off(durationx=nil, duration: nil)
48
48
 
49
- end
49
+ set_pin LOW
50
+ return if @internal_call
51
+
52
+ @on, @blinking = false, false
53
+ duration ||= durationx
54
+
55
+ @on_thread.exit if @on_thread
56
+ @off_thread = Thread.new { (sleep duration; self.on()) } if duration
50
57
 
51
- def off(durationx=nil, duration: nil)
58
+ end
59
+
60
+ alias stop off
61
+ alias start on
52
62
 
53
- set_pin LOW
54
- return if @internal_call
55
-
56
- @on, @blinking = false, false
57
- duration ||= durationx
58
-
59
- @on_thread.exit if @on_thread
60
- @off_thread = Thread.new { (sleep duration; self.on()) } if duration
63
+ def blink(seconds=0.5, duration: nil)
61
64
 
62
- end
63
-
64
- alias stop off
65
- alias start on
66
-
67
- def blink(seconds=0.5, duration: nil)
68
-
69
- @blinking = true
70
- t2 = Time.now + duration if duration
71
-
72
- Thread.new do
73
- while @blinking do
74
-
75
- set_pin HIGH
76
- sleep seconds / 2.0
77
- break if !@blinking
78
- sleep seconds / 2.0
79
- break if !@blinking
80
-
81
- set_pin LOW;
82
- sleep seconds / 2.0
83
- break if !@blinking
84
- sleep seconds / 2.0
85
-
86
- break if !@blinking
87
-
88
- self.off if duration and Time.now >= t2
89
- end
90
-
91
- end
92
- end
93
-
94
- alias oscillate blink
65
+ @blinking = true
66
+ t2 = Time.now + duration if duration
95
67
 
96
- def on?() @on end
97
- def off?() !@on end
68
+ Thread.new do
69
+ while @blinking do
98
70
 
99
- # set val with 0 (off) or 1 (on)
100
- #
101
- def set_pin(val)
71
+ set_pin HIGH
72
+ sleep seconds / 2.0
73
+ break if !@blinking
74
+ sleep seconds / 2.0
75
+ break if !@blinking
76
+
77
+ set_pin LOW;
78
+ sleep seconds / 2.0
79
+ break if !@blinking
80
+ sleep seconds / 2.0
102
81
 
103
- if val then
104
- @pwm.start @duty_cycle
105
- else
106
- @pwm.stop
107
- end
82
+ break if !@blinking
108
83
 
109
- end
110
-
111
- def to_s()
112
- @id
84
+ self.off if duration and Time.now >= t2
85
+ end
86
+
113
87
  end
114
88
  end
115
89
 
116
- class Void
117
- def on(duration=nil) end
118
- def off() end
119
- def blink(seconds=0, duration=nil) end
120
- alias stop off
121
- end
90
+ alias oscillate blink
122
91
 
92
+ def on?() @on end
93
+ def off?() !@on end
123
94
 
124
- def initialize(x=[], duty_cycle: 100, freq: 100)
125
-
126
- a = case x
127
- when Fixnum
128
- [x]
129
- when String
130
- [x]
131
- when Array
132
- x
133
- end
95
+ # set val with 0 (off) or 1 (on)
96
+ #
97
+ def set_pin(val)
134
98
 
135
- RPi::GPIO.reset
136
-
137
- @pins = a.map {|pin| PinX.new pin, duty_cycle: duty_cycle, freq: freq }
138
-
139
- def @pins.[](i)
140
-
141
- if i.respond_to? :to_i and i.to_i >= self.length then
142
- puts "RPiPwm warning: PinX instance #{i.inspect} not found"
143
- Void.new
144
- else
145
- super(i)
146
- end
147
- end
99
+ if val then
100
+ @pwm.start @duty_cycle
101
+ else
102
+ @pwm.stop
103
+ end
148
104
 
149
105
  end
150
-
151
- def pin() @pins.first end
152
- def pins() @pins end
153
-
154
- end
106
+
107
+ def to_s()
108
+ @id
109
+ end
110
+ end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpi_pwm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file