forked from tumugin/WiringPi-Ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up Ruby class for easier maintenance
- Loading branch information
Showing
10 changed files
with
262 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.gem | ||
test.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "ext/wiringpi/WiringPi"] | ||
path = ext/wiringpi/WiringPi | ||
url = https://github.com/WiringPi/WiringPi.git | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module WiringPi | ||
class GPIO | ||
def interrupt(pin, edge, &block) | ||
Thread.new do | ||
@value = digital_read(pin) | ||
@last_value = value | ||
loop do | ||
@last_value = @value | ||
@value = digital_read(pin) | ||
|
||
if @value != @last_value | ||
next if @value == 0 and edge == :falling_edge | ||
next if @value == 1 and edge == :rising_edge | ||
break | ||
end | ||
block.call @value | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
module WiringPi | ||
class GPIO | ||
|
||
@modules = Array.new | ||
@pins = Array.new | ||
|
||
attr_reader :modules, :pins | ||
|
||
def initialize(&block) | ||
Wiringpi2.wiringPiSetup | ||
@pins = Array.new | ||
instance_eval &block | ||
end | ||
|
||
def read_byte(starting_pin) | ||
bits = Array.new | ||
8.times do |time| | ||
bits << Wiringpi2.digitalRead(starting_pin + time) | ||
end | ||
bits.join | ||
end | ||
|
||
def write_byte(starting_pin, byte) | ||
byte = byte.to_s(2) unless byte.length = 8 | ||
offset = starting_pin | ||
byte.each_char do |bit| | ||
Wiringpi2.digitalWrite(offset, bit) | ||
offset += 1 | ||
end | ||
end | ||
|
||
def digital_read(pin) | ||
if pin.respond_to?(:each) | ||
pin.collect do |pin| | ||
Wiringpi2.digitalRead(pin) | ||
end | ||
else | ||
Wiringpi2.digitalRead(pin) | ||
end | ||
end | ||
|
||
def digital_write(pin, value) | ||
if pin.respond_to?(:each) | ||
pin.each do |pin| | ||
Wiringpi2.digitalWrite(pin, value) | ||
end | ||
else | ||
Wiringpi2.digitalWrite(pin, value) | ||
end | ||
end | ||
|
||
def pin_mode(pin, mode) | ||
Wiringpi2.pinMode(pin, mode) | ||
end | ||
|
||
def delay(ms) | ||
Wiringpi2.delay(ms) | ||
end | ||
|
||
def delay_microseconds(ms) | ||
Wiringpi2.delayMicroseconds(ms) | ||
end | ||
|
||
def millis() | ||
return Wiringpi2.millis() | ||
end | ||
|
||
def micros() | ||
return Wiringpi2.micros() | ||
end | ||
|
||
def pwm_set_mode(mode) | ||
return Wiringpi2.pwmSetMode(mode) | ||
end | ||
|
||
def pwm_set_range(range) | ||
return Wiringpi2.pwmSetRange(range) | ||
end | ||
|
||
def pwm_set_clock(divisor) | ||
return Wiringpi2.pwmSetClock(divisor) | ||
end | ||
|
||
def gpio_clock_set(pin, freq) | ||
return Wiringpi2.gpioClockSet(pin, freq) | ||
end | ||
|
||
def wait_for_interrupt(pin, ms) | ||
Wiringpi2.waitForInterrupt(pin, ms) | ||
end | ||
|
||
def wiringpi_isr(pin, mode, fn) | ||
Wiringpi.wiringPiISR(pin, mode, fn) | ||
end | ||
|
||
def shift_out(dpin, cpin, order, val ) | ||
Wiringpi.shiftOut(dpin,cpin,order,val) | ||
end | ||
|
||
def shift_in(dpin, cpin, order) | ||
Wiringpi.shiftIn(dpin,cpin,order) | ||
end | ||
|
||
def add_module(module_instance) | ||
@modules = Array.new if @modules.nil? | ||
|
||
@modules << module_instance | ||
puts 'Added module ' + module_instance.name.to_s | ||
|
||
module_instance.pin_count.times do |offset| | ||
@pins[offset + module_instance.pin_base] = 'ENABLED' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module WiringPi | ||
class I2C | ||
@device_id = 0 | ||
def initialize(device_address) | ||
@device_id = Wiringpi.wiringPiI2CSetup(device_address) | ||
end | ||
def read() | ||
return Wiringpi.wiringPiI2CRead(@device_id) | ||
end | ||
def write(data) | ||
return Wiringpi.wiringPiI2CWrite(@device_id,data) | ||
end | ||
def read_reg_8(reg) | ||
return Wiringpi.wiringPiI2CReadReg8(@device_id,reg) | ||
end | ||
def write_reg_8(reg,data) | ||
return Wiringpi.wiringPiI2CWriteReg8(@device_id,reg,data) | ||
end | ||
def read_reg_16(reg) | ||
return Wiringpi.wiringPiI2CReadReg16(@device_id,reg) | ||
end | ||
def write_reg_16(reg,data) | ||
return Wiringpi.wiringPiI2CWriteReg16(@device_id,reg,data) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module WiringPi | ||
class Modules | ||
class Mcp23017 < ModuleBase | ||
|
||
@i2c_address = 0x0 | ||
|
||
def initialize(pin_base, i2c_address) | ||
@pin_base = pin_base | ||
@pin_count = 16 | ||
@i2c_address = i2c_address | ||
Wiringpi.mcp23017Setup( pin_base, i2c_address ) | ||
super() | ||
end | ||
end | ||
class Mcp23s17 < ModuleBase | ||
|
||
@spi_port = 0 | ||
@device_id = 0 | ||
|
||
def initialize(pin_base, spi_port, device_id) | ||
@pin_base = pin_base | ||
@pin_count = 16 | ||
@spi_port = spi_port | ||
@device_id = device_id | ||
Wiringpi.mcp23s17Setup( pin_base, spi_port, device_id ) | ||
super() | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module WiringPi | ||
class Serial | ||
|
||
@id = 0 | ||
@device = '/dev/ttyAMA0' | ||
@baud = 9600 | ||
|
||
def initialize(device='/dev/ttyAMA0', baud=9600) | ||
|
||
@device = device | ||
@baud = baud | ||
|
||
@id = Wiringpi2.serialOpen(@device, @baud) | ||
|
||
end | ||
|
||
def serial_close | ||
|
||
Wiringpi2.serialClose(@id) | ||
@id = 0 | ||
|
||
end | ||
|
||
def serial_put_char(char) | ||
|
||
Wiringpi2.serialPutchar(@id, char) | ||
|
||
end | ||
|
||
def serial_puts(string) | ||
|
||
Wiringpi2.serialPuts(@id, string) | ||
|
||
end | ||
|
||
def serial_data_avail | ||
|
||
Wiringpi2.serialDataAvail(@id) | ||
|
||
end | ||
|
||
def serial_get_char | ||
|
||
Wiringpi2.serialGetchar(@id) | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module WiringPi | ||
class SPI | ||
@channel = 0 | ||
def initialize(channel,speed) | ||
@channel = channel | ||
Wiringpi.wiringPiSPISetup(channel,speed) | ||
end | ||
def wiringPiSPIGetFd() | ||
return Wiringpi.wiringPiSPIGetFd(@channel) | ||
end | ||
def wiringPiSPIDataRW(data) | ||
return Wiringpi.wiringPiSPIDataRW(@channel,data) | ||
end | ||
end | ||
end |
Oops, something went wrong.