Class: PCF8591

Inherits:
Object show all
Extended by:
Assertions::ClassMethods
Includes:
Assertions, Constants
Defined in:
app/models/pcf8591.rb

Overview

Copyright (C) 2013 by Greg Lawson

<GregLawson123@gmail.com>

Copyright: See COPYING file that comes with this distribution

Defined Under Namespace

Modules: Assertions, Constants, Examples

Constant Summary

Constants included from Constants

Constants::CB_Mask, Constants::Default_control_byte, Constants::MAX_OSC_WARM_UP

Instance Method Summary collapse

Methods included from Assertions

#assert_constant, #assert_disconnected, #assert_loopback, #assert_pre_conditions, #assert_range

Constructor Details

#initialize(bus = 1, address = 0x48) ⇒ PCF8591

Returns a new instance of PCF8591.


26
27
28
29
30
31
# File 'app/models/pcf8591.rb', line 26

def initialize(bus=1, address=0x48)
  @bus=bus.to_s
  @address='0x'+address.to_s(16)
  @last_value=[] # detect disconnected lines and power on
  @last_control_byte=nil
end

Instance Method Details

#adc_read(control_byte = Default_control_byte, burst_length = 1, analog_output_enable = CB::Analog_output_enable) ⇒ Object

set analog_output_enable to zero to force internal oscillator delay


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/pcf8591.rb', line 50

def adc_read(control_byte=Default_control_byte, burst_length=1, analog_output_enable=CB::Analog_output_enable)
  control_byte=control_byte|CB::Analog_output_enable #default to analog out enabled
  if @last_control_byte!=control_byte then
    sysout=`#{select_channel_string(control_byte)}` # set control_byte
    @last_control_byte=control_byte
  end #if
  @read_behind=`#{get_value_string}`.chomp.hex #clear latched value
  ret=[] #no data collected yet
  if analog_output_enable then
    iterations=burst_length
  else
    iterations=burst_length+MAX_OSC_WARM_UP
  end #if
  iterations.times do |index| # extra
    sysout=`#{get_value_string}`
#   puts "get_value_string="+get_value_string
#   puts sysout
    value=sysout.chomp.hex
    if analog_output_enable then
      ret.push(value)
    else
      if value!=@read_behind then 
        ret.push(value)
      end #if
    end #if
    break if ret.size>=burst_length #
  end #Array.new
  sysout=`#{select_channel_string(control_byte&~CB::Analog_output_enable)}` # set control_byte
  @last_value[control_byte]=ret[-1]
  return ret
end

#command_string(command = :get, *args) ⇒ Object

initialize


32
33
34
35
36
# File 'app/models/pcf8591.rb', line 32

def command_string(command=:get, *args)
# puts "in command_string, args="+args.inspect
  command_start="sudo i2c#{command.to_s} -y #{@bus} #{@address}"
  ([command_start]+args).map{|a| a.to_s}.join(' ')
end

#dac_set(output_value = 0, control_byte = 0x41) ⇒ Object

dac_set_value_string


46
47
48
# File 'app/models/pcf8591.rb', line 46

def dac_set(output_value=0, control_byte=0x41)
  @last_aout=`#{dac_set_value_string(output_value, control_byte)}`
end

#dac_set_value_string(output_value = 0, control_byte = 0x41) ⇒ Object

get_value_string


43
44
45
# File 'app/models/pcf8591.rb', line 43

def dac_set_value_string(output_value=0, control_byte=0x41)
  command_string(command=:set, '0x'+control_byte.to_s(16), '0x'+output_value.to_s(16))
end

#get_value_stringObject

select_channel_string


40
41
42
# File 'app/models/pcf8591.rb', line 40

def get_value_string
  command_string(:get)
end

#select_channel_string(control_byte = Default_control_byte) ⇒ Object

command_string


37
38
39
# File 'app/models/pcf8591.rb', line 37

def select_channel_string(control_byte=Default_control_byte)
  command_string(:set, '0x'+control_byte.to_s(16)) # set control_byte
end