#!/usr/bin/ruby
#
# Indices into the parsed values array 'v'
# 
LC = 40
RG = 41
UP = 42
LP = 43
ST = 44

# Array of label/index pairs for output
# 
vals = [
   ["Load", LC],
   ["Range", RG],
   ["UpperPSI", UP],
   ["LowerPSI", LP],
   ["STAT", ST]
]

Signal.trap("INT") { STDOUT.flush(); puts("\nDone"); exit }

# Execute continuously or just once
# 
n = 0
while(1) do
   next unless (line = gets())

   # Examine every other line
   # 
   n += 1
   next unless (n >= 2) 

   n = 0
   # This parses the line using ',' as a separator.
   # Array 'v' contains the string values.
   # 
   v = line.split(',')

   # Write the labels and values
   # 
   vals.each do |i|
      STDOUT.write("#{i[0]}=#{v[i[1]].lstrip}  ")
   end

   # Now parse the bit field
   # 
   status = v[ST].to_i
   status = status & 0b11111100000000
   bits = status.to_s(2)
 
   ra = "#{bits[5..5]}-#{bits[4..4]}"
   ot = bits[3..3]
   tg = bits[2..2]
   ta = "#{bits[1..1]}-#{bits[0..0]}"

   # Print the labels and bit values
   # 
   # puts("PReq-PAct=#{ra}  OverTemp=#{ot}  Toggle=#{tg}  TxPReq-TxPAct=#{ta}")
   puts("PReq-PAct=#{ra}  OverTemp=#{ot}  Toggle=#{tg}")
   STDOUT.flush()

   # break  # Just once for now

end
