Module: PiPiper::Bcm2835

Extended by:
FFI::Library
Defined in:
lib/pi_piper/bcm2835.rb

Overview

The Bcm2835 module is not intended to be directly called. It serves as an FFI library for PiPiper::SPI & PiPiper::I2C.

Constant Summary collapse

SPI_MODE0 =
0
SPI_MODE1 =
1
SPI_MODE2 =
2
SPI_MODE3 =
3
I2C_REASON_OK =

Success

0
I2C_REASON_ERROR_NACK =

Received a NACK

1
I2C_REASON_ERROR_CLKT =

Received Clock Stretch Timeout

2
I2C_REASON_ERROR_DATA =

Not all data is sent / received

3

Class Method Summary collapse

Class Method Details

.export(pin) ⇒ Object



64
65
66
67
# File 'lib/pi_piper/bcm2835.rb', line 64

def self.export(pin)
  File.write('/sys/class/gpio/export', pin)
  @pins << pin unless @pins.include?(pin)
end

.exported?(pin) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/pi_piper/bcm2835.rb', line 82

def self.exported?(pin)
  @pins.include?(pin)
end

.exported_pinsObject



78
79
80
# File 'lib/pi_piper/bcm2835.rb', line 78

def self.exported_pins
  @pins
end

.i2c_allowed_clocksObject



135
136
137
138
139
140
# File 'lib/pi_piper/bcm2835.rb', line 135

def self.i2c_allowed_clocks
  [100.kilohertz,
   399.3610.kilohertz,
   1.666.megahertz,
   1.689.megahertz]
end

.i2c_read_bytes(bytes) ⇒ Object



159
160
161
162
163
164
# File 'lib/pi_piper/bcm2835.rb', line 159

def self.i2c_read_bytes(bytes)
  data_in = FFI::MemoryPointer.new(bytes)
  i2c_read(data_in, bytes) #TODO reason codes 

  (0..bytes-1).map { |i| data_in.get_uint8(i) } 
end

.i2c_transfer_bytes(data) ⇒ Object



152
153
154
155
156
157
# File 'lib/pi_piper/bcm2835.rb', line 152

def self.i2c_transfer_bytes(data)
  data_out = FFI::MemoryPointer.new(data.count)
  (0..data.count-1).each{ |i| data_out.put_uint8(i, data[i]) } 

  i2c_write data_out, data.count
end

.pin_direction(pin, direction) ⇒ Object

Raises:



59
60
61
62
# File 'lib/pi_piper/bcm2835.rb', line 59

def self.pin_direction(pin, direction)
  raise PiPiper::PinError, "Pin #{pin} not exported" unless exported?(pin)
  File.write("/sys/class/gpio/gpio#{pin}/direction", direction)
end

.pin_input(pin) ⇒ Object



33
34
35
36
# File 'lib/pi_piper/bcm2835.rb', line 33

def self.pin_input(pin)
  export(pin)
  pin_direction(pin, 'in')
end

.pin_output(pin) ⇒ Object



43
44
45
46
# File 'lib/pi_piper/bcm2835.rb', line 43

def self.pin_output(pin)
  export(pin)
  pin_direction(pin, 'out')
end

.pin_read(pin) ⇒ Object

Raises:



48
49
50
51
# File 'lib/pi_piper/bcm2835.rb', line 48

def self.pin_read(pin)
  raise PiPiper::PinError, "Pin #{pin} not exported" unless exported?(pin)
  File.read("/sys/class/gpio/gpio#{pin}/value").to_i
end

.pin_set(pin, value) ⇒ Object

Raises:



38
39
40
41
# File 'lib/pi_piper/bcm2835.rb', line 38

def self.pin_set(pin, value)
  raise PiPiper::PinError, "Pin #{pin} not exported" unless exported?(pin)
  File.write("/sys/class/gpio/gpio#{pin}/value", value)
end

.pin_set_edge(pin, trigger) ⇒ Object

Support "none", "rising", "falling", or "both"

Raises:



87
88
89
90
# File 'lib/pi_piper/bcm2835.rb', line 87

def self.pin_set_edge(pin, trigger)
  raise PiPiper::PinError, "Pin #{pin} not exported" unless exported?(pin)
  File.write("/sys/class/gpio/gpio#{pin}/edge", trigger)
end

.pin_wait_for(pin, trigger) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pi_piper/bcm2835.rb', line 92

def self.pin_wait_for(pin, trigger)
  pin_set_edge(pin, trigger)

  fd = File.open("/sys/class/gpio/gpio#{pin}/value", "r")
  value = nil
  loop do
    fd.read
    IO.select(nil, nil, [fd], nil)
    last_value = value
    value = self.pin_read(pin)
    if last_value != value
      next if trigger == :rising and value == 0
      next if trigger == :falling and value == 1
      break
    end
  end
end

.spi_transfer_bytes(data) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/pi_piper/bcm2835.rb', line 142

def self.spi_transfer_bytes(data)
  data_out = FFI::MemoryPointer.new(data.count)
  data_in = FFI::MemoryPointer.new(data.count)
  (0..data.count-1).each { |i| data_out.put_uint8(i, data[i]) }

  spi_transfernb(data_out, data_in, data.count)

  (0..data.count-1).map { |i| data_in.get_uint8(i) }
end

.spidev_out(array) ⇒ Object

NOTE to use: chmod 666 /dev/spidev0.0



111
112
113
# File 'lib/pi_piper/bcm2835.rb', line 111

def self.spidev_out(array)
  File.open('/dev/spidev0.0', 'wb'){|f| f.write(array.pack('C*')) }
end

.unexport_allObject



74
75
76
# File 'lib/pi_piper/bcm2835.rb', line 74

def self.unexport_all
  @pins.dup.each { |pin| unexport_pin(pin) }
end

.unexport_pin(pin) ⇒ Object



69
70
71
72
# File 'lib/pi_piper/bcm2835.rb', line 69

def self.unexport_pin(pin)
  File.write('/sys/class/gpio/unexport', pin)
  @pins.delete(pin)
end