#####################  syringe.rb -- brent@mbari.org  #######################
# $Source: /home/cvs/ESP/gen2/software/esp/lib/syringe.rb,v $
#    Copyright (C) 2003 MBARI
#    MBARI Proprietary Information. All rights reserved.
# $Id: syringe.rb,v 1.16 2005/06/09 18:43:42 brent Exp $
#
#     Defines high-level operations on Syringes
#
############################################################################

require 'scale'

class Axis
  class Position  #need to skip 'round Scale::Position#to_s method
    alias_method :axisTo_s, :to_s
  end
end

class Syringe < Scale
#A Syringe is a Scale with a position mapping to 
#input and display positions in some linear unit
  
  alias_method :volume, :amount
  alias_method :rawVolume, :rawAmount
  alias_method :maxVolume, :maxPosition
    
  alias_method :pull, :moveUp
  alias_method :push, :moveDown
  
  def fill tmpCfg=nil, maxDuration=@maxDuration
    moveTo maxPosition, tmpCfg, maxDuration
  end
  def empty tmpCfg=nil, maxDuration=@maxDuration
    moveTo minPosition, tmpCfg, maxDuration
  end

  def rawId counts, slop=@slop
  #given raw position in counts, return:
  # corresponding volume if >= 0,
  # otherwise, return location relative to the first (empty) position
    vol = rawVolume counts
    emptyCounts = @zero-minRaw
    if vol >= 0 or emptyCounts <= 0
      at vol
    else
      maxZero = @zero - emptyCounts/2  #between empty and 0
      if counts < maxZero  #call it empty
        offset = counts - minRaw
        at (minPosition, (offset if offset.abs > slop))
      else
        at 0  #close enough to zero that we report it as such
      end
    end
  end
  
  def position slop=@slop
  #return the named position the syringe is in
    rawId rawPosition, slop
  end

  class Position < Scale::Position
    alias_method :volume, :amount
  end

end #Syringe class


