Module: GrovePi
- Defined in:
- lib/templates/grove_pi/grove_pi.rb
Constant Summary collapse
- CMD_READ_DIGITAL =
Commands.
1- CMD_WRITE_DIGITAL =
2- CMD_READ_ANALOG =
3- CMD_WRITE_ANALOG =
4- CMD_PIN_MODE =
5- CMD_READ_FIRMWARE_VERSION =
8- A0 =
Arduino pin mappings.
14- A1 =
15- A2 =
16- PINS_ANALOG =
[A0, A1, A2]
- D2 =
2- D3 =
3- D4 =
4- D5 =
5- D6 =
6- D7 =
7- D8 =
8- PINS_DIGITAL =
[D2, D3, D4, D5, D6, D7, D8]
- PIN_MODE_IN =
Pin modes.
0- PIN_MODE_OUT =
1- CONFIG_RETRIES =
Configuration settings.
10- GROVE_PI_I2C_SLAVE_ADDRESS =
4
Class Method Summary collapse
- ._ensure_init ⇒ Object
-
._grove_pi_discover ⇒ Object
Internal functions.
- ._grove_pi_init ⇒ Object
- ._read_analog(pin) ⇒ Object
- ._read_digital(pin) ⇒ Object
- ._set_pin_mode(pin, mode) ⇒ Object
- ._write_analog(pin, value) ⇒ Object
- ._write_digital(pin, value) ⇒ Object
-
.read_analog(pin) ⇒ Object
Analog read functions.
-
.read_digital(pin) ⇒ Object
Digital read function.
-
.read_firmware_version ⇒ Object
Miscellaneous functions.
-
.read_grove_pi_i2c(i2c_slave_address, length) ⇒ Object
Functions for reading and writing I2C slaves connected to I2C-1, I2C-2 or I2C-3 ports of the GrovePi.
-
.write_analog(pin, value) ⇒ Object
Analog write functions (PWM on digital pins D3, D5 and D6).
-
.write_digital(pin, value) ⇒ Object
Digital write function.
- .write_grove_pi_i2c(i2c_slave_address, *data) ⇒ Object
Class Method Details
._ensure_init ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 110 def self._ensure_init() if @_i2c_grove_pi == nil @_i2c_grove_pi = self._grove_pi_init if @_i2c_grove_pi == nil raise 'No GrovePi found.' end end end |
._grove_pi_discover ⇒ Object
Internal functions.
These functions are not intended to be used directly by the user but by the functions which are exposed to the user.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 50 def self._grove_pi_discover() begin i2c_device_files = Dir['/dev/i2c-*'] if i2c_device_files.length < 1 return false, nil end # Iterate over I2C device files. for f in i2c_device_files device_file = f f = f.strip f.slice! '/dev/i2c-' lines = %x(#{'i2cdetect -y ' + Integer(f).to_s}).split("\n") # Get rid of the first line. lines.shift if lines.length < 1 next end # Process i2cdetect command output for the I2C device file. for i in 0..lines.length - 1 line = lines[i].strip line = line.split ':' if line.length != 2 next end for i2c_address in line[1].split(' ') begin if Integer(i2c_address) == GROVE_PI_I2C_SLAVE_ADDRESS return true, device_file.strip end rescue next end end end end rescue return false, nil end return false, nil end |
._grove_pi_init ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 99 def self._grove_pi_init() status, i2c_device_file = self._grove_pi_discover if status && i2c_device_file != nil return I2CDevice.new address: GROVE_PI_I2C_SLAVE_ADDRESS, driver: I2CDevice::Driver::I2CDev.new(i2c_device_file) else return nil end end |
._read_analog(pin) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 125 def self._read_analog(pin) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_READ_ANALOG, pin, 0, 0 bytes = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 3).chars return (bytes[1].ord * 256) + bytes[2].ord end |
._read_digital(pin) ⇒ Object
137 138 139 140 141 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 137 def self._read_digital(pin) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_READ_DIGITAL, pin, 0, 0 return @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 2).chars[0].ord end |
._set_pin_mode(pin, mode) ⇒ Object
120 121 122 123 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 120 def self._set_pin_mode(pin, mode) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_PIN_MODE, pin, mode, 0 end |
._write_analog(pin, value) ⇒ Object
132 133 134 135 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 132 def self._write_analog(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_ANALOG, pin, value, 0 end |
._write_digital(pin, value) ⇒ Object
143 144 145 146 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 143 def self._write_digital(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_DIGITAL, pin, value, 0 end |
.read_analog(pin) ⇒ Object
Analog read functions.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 151 def self.read_analog(pin) if !PINS_ANALOG.include? pin raise 'Invalid analog pin.' end for i in 0..CONFIG_RETRIES - 1 begin self._set_pin_mode pin, PIN_MODE_IN return self._read_analog pin rescue Errno::EREMOTEIO next end end end |
.read_digital(pin) ⇒ Object
Digital read function.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 184 def self.read_digital(pin) if !PINS_DIGITAL.include? pin raise 'Invalid digital pin.' end for i in 0..CONFIG_RETRIES - 1 begin self._set_pin_mode pin, PIN_MODE_IN return self._read_digital pin rescue Errno::EREMOTEIO next end end end |
.read_firmware_version ⇒ Object
Miscellaneous functions.
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 256 def self.read_firmware_version() for i in 0..CONFIG_RETRIES - 1 begin self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_READ_FIRMWARE_VERSION, 0, 0, 0 bytes = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 4).chars return [bytes[1].ord, bytes[2].ord, bytes[3].ord] rescue Errno::EREMOTEIO next end end end |
.read_grove_pi_i2c(i2c_slave_address, length) ⇒ Object
Functions for reading and writing I2C slaves connected to I2C-1, I2C-2 or I2C-3 ports of the GrovePi.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 218 def self.read_grove_pi_i2c(i2c_slave_address, length) _ensure_init if !@_i2c_slave_addresses.key?(i2c_slave_address) path = @_i2c_grove_pi.instance_variable_get(:@driver) .instance_variable_get(:@path) @_i2c_slave_addresses[i2c_slave_address] = I2CDevice.new address: i2c_slave_address, driver: I2CDevice::Driver::I2CDev.new(path) end bytes = [] _bytes = @_i2c_slave_addresses[i2c_slave_address].i2cget(i2c_slave_address, length).chars for b in _bytes bytes << b.ord end return bytes end |
.write_analog(pin, value) ⇒ Object
Analog write functions (PWM on digital pins D3, D5 and D6).
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 167 def self.write_analog(pin, value) if !PINS_DIGITAL.include? pin raise 'Invalid analog pin. Note: PWM based analog write is applicable on digital ports.' end for i in 0..CONFIG_RETRIES - 1 begin self._set_pin_mode pin, PIN_MODE_OUT self._write_analog pin, value return rescue Errno::EREMOTEIO next end end end |
.write_digital(pin, value) ⇒ Object
Digital write function.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 200 def self.write_digital(pin, value) if !PINS_DIGITAL.include? pin raise 'Invalid digital pin.' end for i in 0..CONFIG_RETRIES - 1 begin self._set_pin_mode pin, PIN_MODE_OUT self._write_digital pin, value return rescue Errno::EREMOTEIO next end end end |
.write_grove_pi_i2c(i2c_slave_address, *data) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 240 def self.write_grove_pi_i2c(i2c_slave_address, *data) _ensure_init if !@_i2c_slave_addresses.key?(i2c_slave_address) path = @_i2c_grove_pi.instance_variable_get(:@driver) .instance_variable_get(:@path) @_i2c_slave_addresses[i2c_slave_address] = I2CDevice.new address: i2c_slave_address, driver: I2CDevice::Driver::I2CDev.new(path) end @_i2c_slave_addresses[i2c_slave_address].i2cset i2c_slave_address, *data end |