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"
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
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 162 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 self.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.
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 129 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 82 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
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 151 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
179 180 181 182 183 184 185 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 179 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 self.release_lock return (bytes[1].ord * 256) + bytes[2].ord end |
._read_digital(pin) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 193 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) self.release_lock return val.chars[0].ord end |
._set_pin_mode(pin, mode) ⇒ Object
173 174 175 176 177 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 173 def self._set_pin_mode(pin, mode) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_PIN_MODE, pin, mode, 0 self.release_lock end |
._write_analog(pin, value) ⇒ Object
187 188 189 190 191 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 187 def self._write_analog(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_ANALOG, pin, value, 0 self.release_lock end |
._write_digital(pin, value) ⇒ Object
201 202 203 204 205 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 201 def self._write_digital(pin, value) self._ensure_init @_i2c_grove_pi.i2cset @_i2c_grove_pi.address, CMD_WRITE_DIGITAL, pin, value, 0 self.release_lock end |
.fourDigit_displayClock(pin, string) ⇒ Object
Display digits and center colon
string can contain digits (0-9)
486 487 488 489 490 491 492 493 494 495 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 486 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
430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 430 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) self.release_lock end |
.fourDigit_displayOn(pin) ⇒ Object
Turn on entire display (88:88)
421 422 423 424 425 426 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 421 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) self.release_lock end |
.fourDigit_displayString(pin, string) ⇒ Object
Display a string
string can consist of 0-9
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 446 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 self.release_lock end |
.fourDigit_init(pin) ⇒ Object
Initialize four-digit display
403 404 405 406 407 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 403 def self.fourDigit_init(pin) self._ensure_init @_i2c_grove_pi.i2cset(@_i2c_grove_pi.address, CMD_4DIG_INIT, pin, 0, 0) self.release_lock end |
.fourDigit_setBrightness(pin, brightness) ⇒ Object
Set brightness of display
brightness = 0-7
476 477 478 479 480 481 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 476 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) self.release_lock end |
.fourDigit_writeDigit(pin, segment, value) ⇒ Object
Set individual digit
segment = 0-3
value = 0-15 or 0-F
413 414 415 416 417 418 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 413 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) self.release_lock end |
.get_lock ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 131 def self.get_lock cycle = true while cycle == true begin @_i2c_device_file = File.open(LOCK_FILE_PATH, File::WRONLY|File::CREAT|File::EXCL) cycle = false rescue sleep 0.1 end end end |
.read_analog(pin) ⇒ Object
Analog read functions.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 210 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.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 243 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.
317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 317 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.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 278 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
357 358 359 360 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 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 357 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 self.release_lock end |
.release_lock ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 143 def self.release_lock if @_i2c_device_file.is_a(File) @_i2c_device_file.close File.delete(LOCK_FILE_PATH) @_i2c_device_file = nil end end |
.setRGB(red, green, blue) ⇒ Object
Set the color of the LCD RGB display
red = 0-255
green = 0-255
blue = 0-255
522 523 524 525 526 527 528 529 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 522 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
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 532 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
340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 340 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 self.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).
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 226 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.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 260 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
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 300 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 release_lock end |
.write_lcd_rgb_i2c(i2c_slave_address, *data) ⇒ Object
Helper function for communicating with LCD RGB display
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/templates/grove_pi/grove_pi.rb', line 502 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 self.release_lock end |