class GardenMessenger::AtlasScientific::I2CDevice

Constants

I2C_SLAVE
STILL_PROCESSING

Public Class Methods

new(address, path = Dir.glob('/dev/i2c-*').first) click to toggle source
# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 12
def initialize(address, path = Dir.glob('/dev/i2c-*').first)
  @address = address
  @path = path
end

Public Instance Methods

calibrated?() click to toggle source
# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 29
def calibrated?
  !execute('cal', '?').casecmp?('?cal,0')
end
check_for_status_code(code, ezo_command) click to toggle source
# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 84
def check_for_status_code(code, ezo_command)
  return if code == 1

  case code
  when 255
    raise NoDataToSendError
  when 2
    raise CommandSyntaxError, "There was an error executing the following command: #{ezo_command}\n" \
                              'Please check the documentation of your circuit.'
  else
    raise UnknownStatusCodeError, "Unkown Response code: #{code} received from device.\n" \
                                  'Check the latest datasheet of atlas scientific.'
  end
end
execute(command, *args) click to toggle source
# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 60
def execute(command, *args)
  io = File.open(@path, 'r+')
  # Set i2c address and mode
  io.ioctl(I2C_SLAVE, @address)

  # Prepare EZO command like
  ezo_command = [command, *args].join(',')

  io.syswrite(ezo_command)
  return if command == 'sleep'

  # Wait for the command to finish
  result, status = loop do
    result = io.read(31)
    status = result[0].unpack1('C')
    break [result, status] unless status == STILL_PROCESSING
  end

  check_for_status_code(status, ezo_command)
  result[1..-1].strip
ensure
  io&.close
end
read()
Alias for: reading
reading() click to toggle source

Takes a reading using the r ezo command and casts it to a float

# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 19
def reading
  r.to_f
end
Also aliased as: take_reading, read
take_reading()
Alias for: reading
take_reading_with_temperature_compensation(temperature) click to toggle source
# File lib/garden_messenger/atlas_scientific/i2c_device.rb, line 25
def take_reading_with_temperature_compensation(temperature)
  rt(temperature.to_s).to_f
end