bcm2835
bcm2835 copied to clipboard
Ruby bindings for libbcm2835
Ruby Bindings for libbcm2835
The BCM2835 is the ARM11 core used in the Raspberry PI. It exposes several harware peripherals:
- GPIO
- SPI
- I²C
- UART
This library is a wrapper on top of libbcm2835 that provides a high level interface for accessing the ARM's hardware features from Ruby
View the Full Documentation on rubydoc.info
GPIO
Using Inputs
pin = 17
# make pin an input
GPIO.input(pin)
# check if the input is HIGH i.e. true
puts "door is closed" if GPIO[pin]
Using Outputs
pin = 17
# make pin an output
GPIO.output(pin)
# blink
loop do
GPIO[pin] = true
sleep(1)
GPIO[pin] = false
sleep(1)
end
SPI
Reading from the SPI bus
SPI.begin do |spi|
puts spi.read # returns 1 byte
puts spi.read(1024) # returns an array of 1024 bytes
end
Writing to the SPI bus
SPI.begin do |spi|
spi.write(0x22) # write 1 byte
spi.write("hello") # write 5 bytes
spi.write(0x22,0x45,0x71) # write several bytes at once
end
I²C
Not yet implemented
UART
Universal Asynchronous Receiver/Transmitter
You can use the Ruby Standard Library to read and write from the UART
Echo Example
File.open('/dev/ttyAMA0', 'a+') do |file|
loop do
data = file.read
file.puts(data)
end
end
Installation
Add this line to your application's Gemfile:
gem 'bcm2835'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bcm2835
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request