Class: GPIO
- Inherits:
-
Object
- Object
- GPIO
- Defined in:
- lib/mruby/gpio/sysfs/gpio_sysfs.rb,
lib/mruby/gpio/sysfs/version.rb
Overview
class GPIO
Constant Summary collapse
- VERSION =
"1.0.0"- DRIVER =
"sysfs"- UNUSED =
Constants
0b0000_0000- IN =
option
0b0000_0001- OUT =
0b0000_0010- HIGH_Z =
0b0000_0100- PULL_UP =
0b0000_1000- PULL_DOWN =
0b0001_0000- OPEN_DRAIN =
0b0010_0000- EDGE_RISE =
0b0001_0000_0000- EDGE_FALL =
0b0010_0000_0000- PATH_SYSFS =
"/sys/class/gpio"
Class Method Summary collapse
-
.high_at?(pin) ⇒ Boolean
Return true If the value read from the specified pin is high (==1).
-
.low_at?(pin) ⇒ Boolean
Return true If the value read from the specified pin is low (==0).
-
.read_at(pin) ⇒ Integer
Returns the value read from the specified pin as either 0 or 1.
-
.setmode(pin, params) ⇒ nil
Specify the physical pin indicated by “pin” and change the mode of the GPIO.
-
.write_at(pin, value) ⇒ void
Output a value to the specified pin.
Instance Method Summary collapse
-
#high? ⇒ Boolean
If the loaded value is high level (==1), it returns true.
-
#initialize(pin, params) ⇒ GPIO
constructor
constructor.
-
#irq(cond, bounce_ms: 50, &block) ⇒ void
(option) IRQ event handling.
-
#low? ⇒ Boolean
If the loaded value is low-level (==0), return true.
-
#read ⇒ Integer
Return the loaded value as 0 or 1.
-
#setmode(params) ⇒ nil
Change the GPIO mode at any timing.
-
#write(value) ⇒ void
Specify the value to output to the pin as either 0 or 1.
Constructor Details
#initialize(pin, params) ⇒ GPIO
constructor
186 187 188 189 190 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 186 def initialize( pin, params ) @pin = pin GPIO.setmode( pin, params ) @value = File.open("#{PATH_SYSFS}/gpio#{pin}/value", "r+:ASCII-8BIT") end |
Class Method Details
.high_at?(pin) ⇒ Boolean
Return true If the value read from the specified pin is high (==1)
147 148 149 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 147 def self.high_at?( pin ) return read_at(pin) == 1 end |
.low_at?(pin) ⇒ Boolean
Return true If the value read from the specified pin is low (==0)
158 159 160 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 158 def self.low_at?( pin ) return read_at(pin) == 0 end |
.read_at(pin) ⇒ Integer
Returns the value read from the specified pin as either 0 or 1.
136 137 138 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 136 def self.read_at( pin ) return File.binread("#{PATH_SYSFS}/gpio#{pin}/value", 10).to_i end |
.setmode(pin, params) ⇒ nil
Specify the physical pin indicated by “pin” and change the mode of the GPIO.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 114 def self.setmode( pin, params ) if params == UNUSED _set_unused( pin ) return nil end if ! _set_dir( pin, params ) raise ArgumentError, "You must specify one of IN, OUT and HIGH_Z" end _set_pull( pin, params ) return nil end |
.write_at(pin, value) ⇒ void
This method returns an undefined value.
Output a value to the specified pin.
170 171 172 173 174 175 176 177 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 170 def self.write_at( pin, value ) case value when 0,1 File.binwrite("#{PATH_SYSFS}/gpio#{pin}/value", value.to_s) else raise RangeError end end |
Instance Method Details
#high? ⇒ Boolean
If the loaded value is high level (==1), it returns true.
209 210 211 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 209 def high?() return read() == 1 end |
#irq(cond, bounce_ms: 50, &block) ⇒ void
This method returns an undefined value.
(option) IRQ event handling
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 264 def irq( cond, bounce_ms:50, &block ) if !@irq_thread File.binwrite("#{PATH_SYSFS}/gpio#{@pin}/edge", "both") @bounce_time = bounce_ms / 1000.0 @value.sysseek( 0 ) v1 = @value.sysread(10).to_i @irq_thread = Thread.new { while true @value.sysseek(0) rs,ws,es = IO.select(nil, nil, [@value], 1) sleep @bounce_time v2 = @value.sysread(10).to_i if v1 == 0 && v2 == 1 @handler_rise && @handler_rise.call( EDGE_RISE ) elsif v1 == 1 && v2 == 0 @handler_fall && @handler_fall.call( EDGE_FALL ) end v1 = v2 end } end @handler_rise = block if (cond & EDGE_RISE) != 0 @handler_fall = block if (cond & EDGE_FALL) != 0 return nil end |
#low? ⇒ Boolean
If the loaded value is low-level (==0), return true.
219 220 221 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 219 def low?() return read() == 0 end |
#read ⇒ Integer
Return the loaded value as 0 or 1.
198 199 200 201 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 198 def read() @value.sysseek( 0 ) return @value.sysread(10).to_i end |
#setmode(params) ⇒ nil
Change the GPIO mode at any timing.
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 241 def setmode( params ) if params == UNUSED GPIO._set_unused( @pin ) return nil end GPIO._set_dir( @pin, params ) GPIO._set_pull( @pin, params ) return nil end |
#write(value) ⇒ void
This method returns an undefined value.
Specify the value to output to the pin as either 0 or 1.
230 231 232 |
# File 'lib/mruby/gpio/sysfs/gpio_sysfs.rb', line 230 def write( value ) @value.syswrite( value == 0 ? "0" : "1" ) end |