aboutsummaryrefslogtreecommitdiff
path: root/behaviors/ControllerOSC.py
blob: 0dd1b1260f4c087daae590b6d3699b44a74b5eb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
from operationscore.Behavior import * 
from logger import main_log
#import util.ColorOps as color 
import colorsys
from numpy import array
import pdb
import util.ComponentRegistry as compReg

speedfactor = 15
vel_decay = .01

def constrainLocation(v,c):
    if v[0] > c[0]:
        v[0] = c[0]
    elif v[0]<0:
        v[0] = 0
    
    if v[1] > c[1]:
        v[1] = c[1]
    elif v[1]<0:
        v[1] = 0

    return v

class ControllerOSC(Behavior):   
    def behaviorInit(self):
        self.xy = array((0,0))
        self.v_xy = array((0,0))
        self.v_decay = vel_decay

        self.start_hsv = [0,1,1] 
        self.dest_hsv = [0,1,1] 
        self.ssize = compReg.getComponent('Screen').getSize()[-2:] #896 x 310
    
    def processResponse(self, sensorInputs, recursiveInputs):
        ret = []
        if sensorInputs:
            data = sensorInputs[-1]#for data in sensorInputs:
            if data['Path'] == '/sixaxis/xy':
                #try:
                    x = data['Value'][0]
                    y = data['Value'][1]
                    if y < 0:
                        self.start_hsv[1] = 1.0+y #s
                    else:
                        self.start_hsv[2] = 1.0-y
                    self.start_hsv[0] = (x+1) * 180.0  
#                    if self.start_hsv[0] >= 360:
#                        self.start_hsv[0] = 0
#                    if self.start_hsv[0] <=0:
#                        self.start_hsv[0] = 360
#self.h = x * 360.
                    
                #except(e):
                #    pdb.set_trace()
            elif data['Path'] == '/sixaxis/lrud':
                val=data['Value']
                vy = val[3] if val[3] else -val[2]
                vx = -val[0] if val[0] else val[1]
                #pdb.set_trace()
                #self.v_xy = (val[1]*ssize[0], (1.0-val[0])*ssize[1])
                self.v_xy = array((vx, vy)) * speedfactor
            else:
                main_log.error('Sensor Inputs: ' + str(sensorInputs))
        self.xy = self.xy + self.v_xy
        constrainLocation(self.xy,self.ssize)
        self.v_xy -= self.v_decay
        if self.v_xy[0] < 0:
            self.v_xy[0] = 0
        if self.v_xy[1] < 0:
            self.v_xy[1] = 0
        ret.append({'Color':[i*256 for i in
        colorsys.hsv_to_rgb(*self.start_hsv)],'Location':(int(self.xy[0]), int(self.xy[1]))})
    
        return (ret, [])