Class: GPIO

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/hw-gpio/mrblib/gpio.rb

Constant Summary collapse

IN =
0x01
OUT =
0x02
HIGH_Z =
0x04
PULL_UP =
0x08
PULL_DOWN =
0x10
OPEN_DRAIN =
0x20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pin, flags) ⇒ GPIO

Returns a new instance of GPIO.



11
12
13
14
15
# File 'mrbgems/hw-gpio/mrblib/gpio.rb', line 11

def initialize(pin, flags)
  @pin = pin
  __init(pin)
  setmode(flags)
end

Instance Attribute Details

#pin ⇒ Object (readonly)

Returns the value of attribute pin.



9
10
11
# File 'mrbgems/hw-gpio/mrblib/gpio.rb', line 9

def pin
  @pin
end

Instance Method Details

#high? ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'mrbgems/hw-gpio/mrblib/gpio.rb', line 37

def high?
  read != 0
end

#low? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'mrbgems/hw-gpio/mrblib/gpio.rb', line 41

def low?
  read == 0
end

#setmode(flags) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'mrbgems/hw-gpio/mrblib/gpio.rb', line 17

def setmode(flags)
  dir = flags & (IN | OUT | HIGH_Z)
  n = (flags & IN != 0 ? 1 : 0) + (flags & OUT != 0 ? 1 : 0) + (flags & HIGH_Z != 0 ? 1 : 0)
  if n == 0
    raise ArgumentError, "specify one of IN, OUT, or HIGH_Z"
  elsif n > 1
    raise ArgumentError, "IN, OUT, and HIGH_Z are exclusive"
  end
  GPIO.set_dir_at(@pin, dir)

  pull = flags & (PULL_UP | PULL_DOWN)
  if pull == (PULL_UP | PULL_DOWN)
    raise ArgumentError, "PULL_UP and PULL_DOWN are exclusive"
  end
  GPIO.pull_up_at(@pin) if pull == PULL_UP
  GPIO.pull_down_at(@pin) if pull == PULL_DOWN
  GPIO.open_drain_at(@pin) if flags & OPEN_DRAIN != 0
  nil
end