Class: MoodCube::MoodCubeReader

Inherits:
Object
  • Object
show all
Defined in:
lib/moodcube.rb

Overview

TODO handle different moodcubes!

Instance Method Summary collapse

Constructor Details

#initialize(port = 2343, inet = '') ⇒ MoodCubeReader




68
69
70
71
# File 'lib/moodcube.rb', line 68

def initialize port=2343, inet=''
    @server = UDPSocket.open
    @server.bind(inet, port)
end

Instance Method Details

#listen(&side_switch) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/moodcube.rb', line 73

def listen &side_switch
    moodcube = MoodCube.new(side_switch)
    ip = nil
    oldside = -1
    loop do
        data, sender = @server.recvfrom(6) #, Socket::SO_BROADCAST)
        moodcube.ip_addr = sender[3] # XXX remove this hack if reader
                                     # can handle multiple moodcubes!
        if ip != sender[3]
            ip = sender[3]
            puts "\n--- [#{ip}] --- [#{sender[2]}] ---" if $verbose
        end
        begin
            values = [OpenStruct.new, OpenStruct.new, OpenStruct.new]
            values.each_with_index do |v,n|

                v.raw = data[n*2]<<8|data[n*2+1]
                v.norm = Float(v.raw)/512.0 - 1.0

                moodcube.min[n] = v.norm if moodcube.min[n] > v.norm
                moodcube.max[n] = v.norm if moodcube.max[n] < v.norm
            end

            if $verbose
                printf "%+1.3f %+1.3f %+1.3f  [%+1.3f %+1.3f] [%+1.3f %+1.3f] [%+1.3f %+1.3f]  ",
                    values[0].norm, values[1].norm, values[2].norm, 
                    moodcube.min[0], moodcube.max[0],
                    moodcube.min[1], moodcube.max[1],
                    moodcube.min[2], moodcube.max[2]
            end

            side = identify_side(values)
            moodcube.side_pulse(side)

            printf "%i\n", side if $verbose
        rescue => e
            p e if $verbose
        end
    end
end