Class: GardenMessenger::AtlasScientific::I2CDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/garden_messenger/atlas_scientific/i2c_device.rb

Direct Known Subclasses

EC, PH, Temperature

Defined Under Namespace

Classes: CommandSyntaxError, NoDataToSendError, StillProcessingError, UnknownStatusCodeError

Constant Summary collapse

STILL_PROCESSING =
254
I2C_SLAVE =
0x0703

Instance Method Summary collapse

Constructor Details

#initialize(address, path = Dir.glob('/dev/i2c-*').first) ⇒ I2CDevice

Returns a new instance of I2CDevice.



12
13
14
15
# 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

Instance Method Details

#calibrated?Boolean

Returns:

  • (Boolean)


29
30
31
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#readingObject Also known as: take_reading, read

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



19
20
21
# File 'lib/garden_messenger/atlas_scientific/i2c_device.rb', line 19

def reading
  r.to_f
end

#take_reading_with_temperature_compensation(temperature) ⇒ Object



25
26
27
# 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