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_ULTRASONIC_READ =
7- CMD_READ_FIRMWARE_VERSION =
8- CMD_READ_TEMP_HUM =
40- CMD_4DIG_INIT =
70- CMD_4DIG_BRIGHTNESS =
71- CMD_4DIG_VALUE =
72- CMD_4DIG_VALUE_ZEROS =
73- CMD_4DIG_DIGIT =
74- CMD_4DIG_LEDS =
75- CMD_4DIG_CLOCK =
76- CMD_4DIG_ON =
78- 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 =
added A2 to test using analog port as digital
[D2, D3, D4, D5, D6, D7, D8, A2]
- PIN_MODE_IN =
Pin modes.
0- PIN_MODE_OUT =
1- CONFIG_RETRIES =
Configuration settings.
10- GROVE_PI_I2C_SLAVE_ADDRESS =
4- DISPLAY_TEXT_ADDRESS =
LCD RGB display - I2C addresses
0x3e- DISPLAY_RGB_ADDRESS =
0x3e
0x62- LOCK_FILE_PATH =
Initialize the lock file path.
"/tmp/grove"- @@_i2c_device_file =
Initialize i2c file path
nil
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
-
.fourDigit_displayClock(pin, string) ⇒ Object
Display digits and center colon string can contain digits (0-9).
-
.fourDigit_displayDec(pin, string) ⇒ Object
Display a decimal value without leading zeros value = 0 - 9999.
-
.fourDigit_displayOn(pin) ⇒ Object
Turn on entire display (88:88).
-
.fourDigit_displayString(pin, string) ⇒ Object
Display a string string can consist of 0-9.
-
.fourDigit_init(pin) ⇒ Object
Initialize four-digit display.
-
.fourDigit_setBrightness(pin, brightness) ⇒ Object
Set brightness of display brightness = 0-7.
-
.fourDigit_writeDigit(pin, segment, value) ⇒ Object
Set individual digit segment = 0-3 value = 0-15 or 0-F.
- .get_lock ⇒ 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.
-
.read_temp_humidity(pin, sensor_version) ⇒ Object
Read temperature and humidity values from sensor.
- .release_lock ⇒ Object
-
.setRGB(red, green, blue) ⇒ Object
Set the color of the LCD RGB display red = 0-255 green = 0-255 blue = 0-255.
-
.setText(text) ⇒ Object
Set the text on the LCD RGB display.
-
.ultrasonic_read(pin) ⇒ Object
Read distance from ultrasonic ranger.
-
.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
-
.write_lcd_rgb_i2c(i2c_slave_address, *data) ⇒ Object
Helper function for communicating with LCD RGB display.
Class Method Details
._ensure_init ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 166 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 GrovePi.get_lock 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 81 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
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 155 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
183 184 185 186 187 188 189 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 183 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 GrovePi.release_lock return (bytes[1].ord * 256) + bytes[2].ord end |
._read_digital(pin) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 197 def self._read_digital(pin) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_READ_DIGITAL, pin, 0, 0 val = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 2) GrovePi.release_lock return val.chars[0].ord end |
._set_pin_mode(pin, mode) ⇒ Object
177 178 179 180 181 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 177 def self._set_pin_mode(pin, mode) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_PIN_MODE, pin, mode, 0 GrovePi.release_lock end |
._write_analog(pin, value) ⇒ Object
191 192 193 194 195 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 191 def self._write_analog(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_ANALOG, pin, value, 0 GrovePi.release_lock end |
._write_digital(pin, value) ⇒ Object
205 206 207 208 209 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 205 def self._write_digital(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_DIGITAL, pin, value, 0 GrovePi.release_lock end |
.fourDigit_displayClock(pin, string) ⇒ Object
Display digits and center colon
string can contain digits (0-9)
490 491 492 493 494 495 496 497 498 499 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 490 def self.fourDigit_displayClock(pin, string) self._ensure_init string.delete! ":" # make sure there is no colon in the string value = string.to_i left = value/100 right = value % 100 @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_CLOCK, pin, left, right) self.release_lock return left, right end |
.fourDigit_displayDec(pin, string) ⇒ Object
Display a decimal value without leading zeros
value = 0 - 9999
434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 434 def self.fourDigit_displayDec(pin, string) self._ensure_init value = string.to_i # split into 2 bytes byte1 = value & 255 byte2 = value >> 8 @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_VALUE, pin, byte1, byte2) sleep(0.05) GrovePi.release_lock end |
.fourDigit_displayOn(pin) ⇒ Object
Turn on entire display (88:88)
425 426 427 428 429 430 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 425 def self.fourDigit_displayOn(pin) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_ON, pin, 0, 0) sleep(0.05) GrovePi.release_lock end |
.fourDigit_displayString(pin, string) ⇒ Object
Display a string
string can consist of 0-9
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 450 def self.fourDigit_displayString(pin, string) self._ensure_init if string.include? ":" string.delete ":" end digits = string.chars i = 0 while (digits.length < 4) digits.unshift(" ") end #for each space added or each unaccepted character, display an empty space #otherwise display the character digits.each do |digit| if (digit == " ") | ((digit.to_i(16) == 0) & (digit != "0")) @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_LEDS, pin, i, 0) else @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_DIGIT, pin, i, digit.to_i(16)) end i += 1 sleep(0.05) end GrovePi.release_lock end |
.fourDigit_init(pin) ⇒ Object
Initialize four-digit display
407 408 409 410 411 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 407 def self.fourDigit_init(pin) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_INIT, pin, 0, 0) GrovePi.release_lock end |
.fourDigit_setBrightness(pin, brightness) ⇒ Object
Set brightness of display
brightness = 0-7
480 481 482 483 484 485 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 480 def self.fourDigit_setBrightness(pin, brightness) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_BRIGHTNESS, pin, brightness.to_i, 0) sleep(0.05) GrovePi.release_lock end |
.fourDigit_writeDigit(pin, segment, value) ⇒ Object
Set individual digit
segment = 0-3
value = 0-15 or 0-F
417 418 419 420 421 422 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 417 def self.fourDigit_writeDigit(pin, segment, value) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_DIGIT, pin, segment, value) sleep(0.05) GrovePi.release_lock end |
.get_lock ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 130 def self.get_lock cycle = true while cycle == true begin puts "Getting lock." @@_i2c_device_file = File.open(LOCK_FILE_PATH, File::WRONLY|File::CREAT|File::EXCL) cycle = false puts "Lock acquired." rescue puts "Could not get lock." end end end |
.read_analog(pin) ⇒ Object
Analog read functions.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 214 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.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 247 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 => e puts e. next end end end |
.read_firmware_version ⇒ Object
Miscellaneous functions.
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 321 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 self.release_lock 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 282 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 release_lock return bytes end |
.read_temp_humidity(pin, sensor_version) ⇒ Object
Read temperature and humidity values from sensor
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 361 def self.read_temp_humidity(pin, sensor_version) self._ensure_init # if sensor is white: module_type = 1, if sensor is blue: module_type = 0 if sensor_version == "white" module_type = 1 else module_type = 0 end error = false begin @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_READ_TEMP_HUM, pin, module_type, 0) rescue => e puts e. error = true end if error == false for i in 0..CONFIG_RETRIES - 1 begin # read one byte @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 1) # read 10 more bytes number = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 9) temp_value = number[1..4].unpack('f')[0].round(2) * 9/5 + 32 #convert to F humidity_value = number[5..8].unpack('f')[0].round(2) return temp_value, humidity_value rescue Errno::EREMOTEIO next end end end GrovePi.release_lock end |
.release_lock ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 144 def self.release_lock puts "Releasing lock." if @@_i2c_device_file.is_a?(File) @@_i2c_device_file.close File.delete(LOCK_FILE_PATH) @@_i2c_device_file = nil puts "Lock released." sleep 0.1 end end |
.setRGB(red, green, blue) ⇒ Object
Set the color of the LCD RGB display
red = 0-255
green = 0-255
blue = 0-255
526 527 528 529 530 531 532 533 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 526 def self.setRGB(red,green,blue) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 0, 0) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 1, 0) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 0x08, 0xaa) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 4, red.to_i) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 3, green.to_i) write_lcd_rgb_i2c(DISPLAY_RGB_ADDRESS, 2, blue.to_i) end |
.setText(text) ⇒ Object
Set the text on the LCD RGB display
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 536 def self.setText(text) write_lcd_rgb_i2c(DISPLAY_TEXT_ADDRESS, 0x80, 0x01) sleep(0.05) write_lcd_rgb_i2c(DISPLAY_TEXT_ADDRESS, 0x80, 0x08 | 0x04) write_lcd_rgb_i2c(DISPLAY_TEXT_ADDRESS, 0x80, 0x28) sleep(0.05) count = 0 row = 0 text.chars.each do |c| if c == '\n' or count == 16 count = 0 row += 1 if row == 2 break end write_lcd_rgb_i2c(DISPLAY_TEXT_ADDRESS, 0x80, 0xc0) if c == '\n' next end end count += 1 write_lcd_rgb_i2c(DISPLAY_TEXT_ADDRESS, 0x40, c.ord) end end |
.ultrasonic_read(pin) ⇒ Object
Read distance from ultrasonic ranger
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 344 def self.ultrasonic_read(pin) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_ULTRASONIC_READ, pin, 0, 0) sleep(0.06) #firmware has a time of 50ms so wait for more than that @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 1) numbers = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 3).chars GrovePi.release_lock return (numbers[1].ord) * 256 + (numbers[2].ord) end |
.write_analog(pin, value) ⇒ Object
Analog write functions (PWM on digital pins D3, D5 and D6).
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 230 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.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 264 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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 304 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 GrovePi.release_lock end |
.write_lcd_rgb_i2c(i2c_slave_address, *data) ⇒ Object
Helper function for communicating with LCD RGB display
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 506 def self.write_lcd_rgb_i2c(i2c_slave_address, *data) self._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 *data GrovePi.release_lock end |