rpi_pwm 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fae368fef9c49ada9c7b9f1343b27027a67195da
4
+ data.tar.gz: aa7eba1f5eb885bb2145789069108fed821008d0
5
+ SHA512:
6
+ metadata.gz: c6ee3b07e5e9fff98804974b5fc1e83a17b633851fd672def37579ec0ab9d2d2c3cdb0f626794e468c764e3e09b24c6602164dd01f808ef121fd4a007084b17e
7
+ data.tar.gz: 8f5ce0fd45deb33ef2ed5bba5eebcff6a6e736d75cc341ed509db8f4420ccdec4fc111a7c78ac5e54ccaa6738000f50e7a133077742998a0c5873572030bc9ee
Binary file
Binary file
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: rpi_pwm.rb
4
+
5
+ require 'rpi_gpio'
6
+
7
+
8
+ class RPiPwm
9
+
10
+
11
+ class PinX
12
+
13
+ HIGH = true
14
+ LOW = false
15
+
16
+ attr_accessor :duty_cycle, :freq
17
+
18
+
19
+ def initialize(pin_num, duty_cycle: 100, freq: 100)
20
+
21
+ @id, @duty_cycle, @freq = pin_num, duty_cycle, freq
22
+
23
+ RPi::GPIO.set_numbering :bcm
24
+ RPi::GPIO.setup pin_num, :as => :output
25
+
26
+ @pwm = RPi::GPIO::PWM.new(pin_num, freq)
27
+ @pwm.frequency = 40
28
+
29
+ @on, @blinking = false, false
30
+
31
+ at_exit { RPi::GPIO.clean_up pin_num}
32
+
33
+ end
34
+
35
+ def duty_cycle=(val)
36
+ @duty_cycle = val
37
+ @pwm.duty_cycle = val
38
+ end
39
+
40
+ def on(durationx=nil, duration: nil)
41
+
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
48
+
49
+ end
50
+
51
+ def off(durationx=nil, duration: nil)
52
+
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
61
+
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
95
+
96
+ def on?() @on end
97
+ def off?() !@on end
98
+
99
+ # set val with 0 (off) or 1 (on)
100
+ #
101
+ def set_pin(val)
102
+
103
+ if val then
104
+ @pwm.start @duty_cycle
105
+ else
106
+ @pwm.stop
107
+ end
108
+
109
+ end
110
+
111
+ def to_s()
112
+ @id
113
+ end
114
+ end
115
+
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
122
+
123
+
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
134
+
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
148
+
149
+ end
150
+
151
+ def pin() @pins.first end
152
+ def pins() @pins end
153
+
154
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpi_pwm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
+ YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
+ 8ixkARkWAmV1MB4XDTE2MDQwMzA5NDYzNFoXDTE3MDQwMzA5NDYzNFowSDESMBAG
16
+ A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
17
+ EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
18
+ ggEBAKj5REqSzIytdTgKxZPFAr2pZz4B64rcEHM6eoo2iCZsxU9+DzolCF6Rl/sY
19
+ AjuNcdgkHFjbVF5mxr1nBvmz2Uec657SDlqIBl/p0vL3nUss8Odu9O6oDRmc3X4u
20
+ 3ES0qppUZNGMWouSIyDIKs2g62JovNufqRUhhWgWKJGtBKU2zCtnFh1i4S/jOA8E
21
+ ki20nNJ4HN+t1SQ/9DJzu/yHd2vUCbrubYOFust/kKAmdc0o5G4qzO3pLwxCatWV
22
+ pvmSG3uies2drDV2y2SSxKOPFUHih4CC4M2QSTDi0+dVDlczeqIZXDi/DiUDDPZQ
23
+ OrbLeWt1trBNS3DgtUUNZunpfSUCAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
24
+ DwQEAwIEsDAdBgNVHQ4EFgQUphSVISSFJ/YGjFyQkoXOWObthAowJgYDVR0RBB8w
25
+ HYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1h
26
+ c3RlckBqYW1lc3JvYmVydHNvbi5ldTANBgkqhkiG9w0BAQUFAAOCAQEAk1mmrZbP
27
+ TcdyAkfr5/I6rtqKxsFRj6SHg5l2h6WvazudCdRCAz04Uz+WrGu0fUWd8+cKO4zY
28
+ vsh7s5E7SlB/rpli/TZDFaUIt12gN3ASx0cqwKTGWgK07XmAlf+6Dub/tHiq7Yt9
29
+ dxkf/5UJoCBxX8HGqiWTtw5896Nz1SgIm5Xi/2z45mpoYjNvaHX9x4e3B2iQvagj
30
+ UA50rR8ryFtvMyqumGPCHKoIQNsYEGlTJgckzR/0Ta5DuaTUlMZBJ1E4nyOIr4Bg
31
+ Lg+1iD44mOhKo89xZ5h1Puu6JavsbMTmeFCxBH7BbacQ3NHjj3CSG0pI0YWlDSys
32
+ SKOkTuZCEwtqtw==
33
+ -----END CERTIFICATE-----
34
+ date: 2016-04-03 00:00:00.000000000 Z
35
+ dependencies: []
36
+ description:
37
+ email: james@r0bertson.co.uk
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - lib/rpi_pwm.rb
43
+ homepage: https://github.com/jrobertson/rpi_pwm
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.4.8
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A wrapper of the rpi_gpio gem with similar function to the rpi gem, but features
67
+ PWM.
68
+ test_files: []
Binary file