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, A0, A1, 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"
- LOCK_REQUEST_PATH =
"/tmp/lock_request"
- @@_i2c_device_file =
Initialize i2c file path
nil
- @@_lock_request =
Initialize lock request flag
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
190 191 192 193 194 195 196 197 198 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 190 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 GrovePi.get_lock 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.
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 129 130 131 132 133 134 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 87 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
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 179 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
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 206 def self._read_analog(pin) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_READ_ANALOG, pin, 0, 0 read_value = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 3) GrovePi.release_lock if read_value != nil bytes = read_value.chars return (bytes[1].ord * 256) + bytes[2].ord else return 65535 end end |
._read_digital(pin) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 225 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 if val != nil return val.chars[0].ord else return 0 end end |
._set_pin_mode(pin, mode) ⇒ Object
200 201 202 203 204 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 200 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
219 220 221 222 223 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 219 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
237 238 239 240 241 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 237 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)
543 544 545 546 547 548 549 550 551 552 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 543 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
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 487 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)
478 479 480 481 482 483 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 478 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
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 503 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
460 461 462 463 464 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 460 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
533 534 535 536 537 538 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 533 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
470 471 472 473 474 475 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 470 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 136 def self.get_lock cycle = true wait = false while cycle == true begin #puts "Getting lock." unless @@_lock_request == false && File.exist?(LOCK_REQUEST_PATH) @@_i2c_device_file = File.open(LOCK_FILE_PATH, File::WRONLY|File::CREAT|File::EXCL) if @@_lock_request == true File.delete(LOCK_REQUEST_PATH) end @@_lock_request = false end if wait == true i = 0 while i < 1000000 i += 1 end wait = false end cycle = false #puts "Lock acquired." rescue #puts "Could not get lock." unless File.exist?(LOCK_REQUEST_PATH) File.open(LOCK_REQUEST_PATH, File::WRONLY|File::CREAT) end @@_lock_request = true wait = true end end end |
.read_analog(pin) ⇒ Object
Analog read functions.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 246 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.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 279 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.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 359 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 read_value = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 4) GrovePi.release_lock if read_value != nil bytes = read_value.chars return [bytes[1].ord, bytes[2].ord, bytes[3].ord] else raise "Firmware version could not be read." end 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.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 314 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 = [] read_value = @_i2c_slave_addresses[i2c_slave_address].i2cget(i2c_slave_address, length) GrovePi.release_lock if read_value != nil _bytes = read_value.chars for b in _bytes bytes << b.ord end return bytes else return ["0".ord] end end |
.read_temp_humidity(pin, sensor_version) ⇒ Object
Read temperature and humidity values from sensor
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 410 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) GrovePi.release_lock if !number.nil? 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 else return 0.0, -100.0 end rescue Errno::EREMOTEIO next end end end end |
.release_lock ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 169 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." end end |
.setRGB(red, green, blue) ⇒ Object
Set the color of the LCD RGB display
red = 0-255
green = 0-255
blue = 0-255
579 580 581 582 583 584 585 586 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 579 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
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 589 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
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 387 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) GrovePi.release_lock read_value = @_i2c_grove_pi.i2cget(@_i2c_grove_pi.address, 3) if read_value != nil numbers = read_value.chars return (numbers[1].ord) * 256 + (numbers[2].ord) else return 0 end end |
.write_analog(pin, value) ⇒ Object
Analog write functions (PWM on digital pins D3, D5 and D6).
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 262 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.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 296 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
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 342 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
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 559 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 |