rpi_pwm 0.1.1 → 0.2.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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/rpi_pwm.rb +71 -115
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13c9033952c730a81a3bc76e0033d3f3389b0a09
|
4
|
+
data.tar.gz: b375773d796520cad8d1b4171cbc2156eddfe8a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
13
|
+
attr_accessor :duty_cycle, :freq
|
12
14
|
|
13
|
-
HIGH = true
|
14
|
-
LOW = false
|
15
15
|
|
16
|
-
|
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
|
-
|
20
|
+
RPi::GPIO.set_numbering :bcm
|
21
|
+
RPi::GPIO.setup pin_num, :as => :output
|
20
22
|
|
21
|
-
|
23
|
+
@pwm = RPi::GPIO::PWM.new(pin_num, freq)
|
22
24
|
|
23
|
-
|
24
|
-
RPi::GPIO.setup pin_num, :as => :output
|
25
|
+
@on, @blinking = false, false
|
25
26
|
|
26
|
-
|
27
|
-
@pwm.frequency = 40
|
27
|
+
at_exit { RPi::GPIO.clean_up pin_num}
|
28
28
|
|
29
|
-
|
29
|
+
end
|
30
30
|
|
31
|
-
|
31
|
+
def duty_cycle=(val)
|
32
|
+
@duty_cycle = val
|
33
|
+
@pwm.duty_cycle = val
|
34
|
+
end
|
32
35
|
|
33
|
-
|
36
|
+
def on(durationx=nil, duration: nil)
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
45
|
+
end
|
41
46
|
|
42
|
-
|
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
|
-
|
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
|
-
|
58
|
+
end
|
59
|
+
|
60
|
+
alias stop off
|
61
|
+
alias start on
|
52
62
|
|
53
|
-
|
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
|
-
|
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
|
-
|
97
|
-
|
68
|
+
Thread.new do
|
69
|
+
while @blinking do
|
98
70
|
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
104
|
-
@pwm.start @duty_cycle
|
105
|
-
else
|
106
|
-
@pwm.stop
|
107
|
-
end
|
82
|
+
break if !@blinking
|
108
83
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
@id
|
84
|
+
self.off if duration and Time.now >= t2
|
85
|
+
end
|
86
|
+
|
113
87
|
end
|
114
88
|
end
|
115
89
|
|
116
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
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
|
152
|
-
|
153
|
-
|
154
|
-
end
|
106
|
+
|
107
|
+
def to_s()
|
108
|
+
@id
|
109
|
+
end
|
110
|
+
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|