Annotation of ruby/snd/snd.rb, revision 1.1
1.1 ! as 1: #!/usr/bin/env ruby
! 2: # $Paefchen$
! 3:
! 4: module SND
! 5:
! 6: class MB
! 7: def initialize addr, port=502, slaveaddr=0
! 8: @addr = addr
! 9: @port = port
! 10: @slaveaddr = slaveaddr
! 11: end
! 12:
! 13: #def read_input; end
! 14: #def read_inputs; end
! 15: #def read_coil; end
! 16: #def read_coils; end
! 17: #def write_coil; end
! 18: #def write_coils; end
! 19: #def read_input_register; end
! 20: #def read_input_registers; end
! 21: #def read_input_registers; end
! 22: #def write_registers; end
! 23:
! 24: def read_holding_register register
! 25: raw("read_holding_register #{register}").split('=')[1].to_i
! 26: end
! 27:
! 28: def write_register register, value
! 29: raw("write_register #{register} #{value.to_i}")
! 30: end
! 31:
! 32: private
! 33: def raw text
! 34: `../mb_tcpclient #{@addr} #{@port} #{@slaveaddr} #{text}`
! 35: end
! 36: end
! 37:
! 38: class Setting
! 39: def initialize addr, port=502, slaveaddr=0
! 40: @mb = MB.new addr, port, slaveaddr
! 41:
! 42: @volumen_min = 100
! 43: @volumen_max = 250
! 44: end
! 45:
! 46: def volumen_fadeto to
! 47: now = volumen_get
! 48:
! 49: return true if now == to
! 50:
! 51: return volumen_set(0) if to == 0
! 52:
! 53: to = @volumen_min if to < @volumen_min
! 54: to = @volumen_max if to > @volumen_max
! 55:
! 56: if now < to
! 57: (now+1).upto(to) { |i| volumen_set i }
! 58: else
! 59: (now-1).downto(to) { |i| volumen_set i }
! 60: end
! 61: end
! 62:
! 63: def volumen_get
! 64: @mb.read_holding_register 0
! 65: end
! 66:
! 67: def volumen_set value
! 68: @mb.write_register 0, value
! 69: end
! 70: end
! 71: end