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

Class Method Details

._ensure_initObject



165
166
167
168
169
170
171
172
173
# File 'lib/templates/grove_pi/grove_pi.rb', line 165

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_discoverObject

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_initObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/templates/grove_pi/grove_pi.rb', line 154

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



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/templates/grove_pi/grove_pi.rb', line 181

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



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/templates/grove_pi/grove_pi.rb', line 200

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



175
176
177
178
179
# File 'lib/templates/grove_pi/grove_pi.rb', line 175

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



194
195
196
197
198
# File 'lib/templates/grove_pi/grove_pi.rb', line 194

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



212
213
214
215
216
# File 'lib/templates/grove_pi/grove_pi.rb', line 212

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)


518
519
520
521
522
523
524
525
526
527
# File 'lib/templates/grove_pi/grove_pi.rb', line 518

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


462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/templates/grove_pi/grove_pi.rb', line 462

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)



453
454
455
456
457
458
# File 'lib/templates/grove_pi/grove_pi.rb', line 453

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


478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/templates/grove_pi/grove_pi.rb', line 478

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



435
436
437
438
439
# File 'lib/templates/grove_pi/grove_pi.rb', line 435

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


508
509
510
511
512
513
# File 'lib/templates/grove_pi/grove_pi.rb', line 508

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


445
446
447
448
449
450
# File 'lib/templates/grove_pi/grove_pi.rb', line 445

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_lockObject



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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/templates/grove_pi/grove_pi.rb', line 221

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.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/templates/grove_pi/grove_pi.rb', line 254

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.message
      next
    end
  end
end

.read_firmware_versionObject

Miscellaneous functions.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/templates/grove_pi/grove_pi.rb', line 334

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.



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/templates/grove_pi/grove_pi.rb', line 289

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



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/templates/grove_pi/grove_pi.rb', line 385

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.message
    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_lockObject



144
145
146
147
148
149
150
151
152
# 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."
  end
end

.setRGB(red, green, blue) ⇒ Object

Set the color of the LCD RGB display

red = 0-255
green = 0-255
blue = 0-255


554
555
556
557
558
559
560
561
# File 'lib/templates/grove_pi/grove_pi.rb', line 554

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



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/templates/grove_pi/grove_pi.rb', line 564

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



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/templates/grove_pi/grove_pi.rb', line 362

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).



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/templates/grove_pi/grove_pi.rb', line 237

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.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/templates/grove_pi/grove_pi.rb', line 271

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/templates/grove_pi/grove_pi.rb', line 317

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



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/templates/grove_pi/grove_pi.rb', line 534

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