Class: Dino::PiBoard

Inherits:
Object
  • Object
show all
Defined in:
lib/dino/piboard.rb,
lib/dino/piboard_version.rb

Constant Summary collapse

VERSION =
'0.13.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePiBoard

Returns a new instance of PiBoard.



8
9
10
11
12
13
14
15
16
17
# File 'lib/dino/piboard.rb', line 8

def initialize
  @components = []

  # Sample every 10us, using PCM peripheral. Keeps CPU usage down.
  PiGPIO.gpioCfgClock(10, 1, 0)    
  PiGPIO.gpioInitialise

  @low  = 0
  @high = 1
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



6
7
8
# File 'lib/dino/piboard.rb', line 6

def components
  @components
end

#highObject (readonly)

Returns the value of attribute high.



6
7
8
# File 'lib/dino/piboard.rb', line 6

def high
  @high
end

#lowObject (readonly)

Returns the value of attribute low.



6
7
8
# File 'lib/dino/piboard.rb', line 6

def low
  @low
end

Instance Method Details

#add_component(component) ⇒ Object



33
34
35
# File 'lib/dino/piboard.rb', line 33

def add_component(component)
  @components << component
end

#digital_listen(pin, divider = 4) ⇒ Object



91
92
93
# File 'lib/dino/piboard.rb', line 91

def digital_listen(pin, divider=4)
  set_listener(pin, :on, {})
end

#digital_read(pin) ⇒ Object

CMD = 2



71
72
73
# File 'lib/dino/piboard.rb', line 71

def digital_read(pin)
  update(pin, PiGPIO.gpioRead(pin))
end

#digital_write(pin, value) ⇒ Object

CMD = 1



66
67
68
# File 'lib/dino/piboard.rb', line 66

def digital_write(pin, value)
  PiGPIO.gpioWrite(pin, value)
end

#finish_writeObject



19
20
21
# File 'lib/dino/piboard.rb', line 19

def finish_write
  PiGPIO.gpioTerminate
end

#pwm_write(pin, value) ⇒ Object

CMD = 3



76
77
78
# File 'lib/dino/piboard.rb', line 76

def pwm_write(pin, value)
  PiGPIO.gpioPWM(pin, value)
end

#remove_component(component) ⇒ Object



37
38
39
40
# File 'lib/dino/piboard.rb', line 37

def remove_component(component)
  # component.stop if component.methods.include? :stop
  @components.delete(component)
end

#set_listener(set_pin, state = :off, options = {}) ⇒ Object

CMD = 6



81
82
83
84
85
86
87
88
89
# File 'lib/dino/piboard.rb', line 81

def set_listener(set_pin, state=:off, options={})
  if state == :on
    PiGPIO.gpioSetAlertFunc(set_pin) do |pin, message, time|
      update(pin, message, time)
    end
  else
    PiGPIO._gpioSetAlertFunc(pin, nil)
  end
end

#set_pin_mode(pin, mode = :input) ⇒ Object

CMD = 0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dino/piboard.rb', line 43

def set_pin_mode(pin, mode=:input)
  # Output
  if mode.to_s.match /output/
    PiGPIO.gpioSetMode(pin, 1)

  # Input
  else  
    PiGPIO.gpioSetMode(pin, 0)
    # Only respond if level has been stable for 90us.
    PiGPIO.gpioGlitchFilter(pin, 90)      

    # Pull down/up/none
    if mode.to_s.match /pulldown/
      PiGPIO.gpioSetPullUpDown(pin, 1)
    elsif mode.to_s.match /pullup/
      PiGPIO.gpioSetPullUpDown(pin, 2)
    else
      PiGPIO.gpioSetPullUpDown(pin, 0)
    end
  end
end

#stop_listener(pin) ⇒ Object



95
96
97
# File 'lib/dino/piboard.rb', line 95

def stop_listener(pin)
  set_listener(pin, :off)
end

#update(pin, message, time) ⇒ Object



23
24
25
# File 'lib/dino/piboard.rb', line 23

def update(pin, message, time)
  update_component(pin, message)
end

#update_component(pin, message) ⇒ Object



27
28
29
30
31
# File 'lib/dino/piboard.rb', line 27

def update_component(pin, message)
  @components.each do |part|
    part.update(message) if part.pin.to_i == pin
  end
end