-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSensorControl.sc
107 lines (92 loc) · 2.29 KB
/
SensorControl.sc
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
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
// SensorControl is a class to deal with a set of SensorData streams, to trigger and control something above a certain threshold, and turn off and switch to another action below the threshold.
// Created for Schwelle, www.schwelle.org
// (c) 2007-8, Marije Baalman
SensorControl{
var <>threshold, <>fadeout, <>topamp, <>density, <>ts, <>scale;
var <>updateValues;
var <ison,<turn,<turningoff,<trig;
var <updateFunc;
var <name;
var <>onAction, <>offonAction, <>onoffAction, <>offAction;
var <>defaultRetAction;
var <>weights;
var <>dataArray;
var <task, <>waitTime = 0.05;
*new{ |data,name|
^super.new.init(data,name);
}
longshort_{ |long|
if ( long, {
updateValues = { var val;
val = 0;
dataArray.do{ |it,i|
val = val + (weights[i]*it.longStdDev);
};
val;
};
},{
updateValues = { var val;
val = 0;
dataArray.do{ |it,i|
val = val + (weights[i]*it.shortStdDev);
};
val;
};
});
}
init{ |data,nm|
name = nm ? \defaultSensorControl;
dataArray = data; // must be an array of SensorData
ison = 0;
turn = 0;
trig = 0;
defaultRetAction = {};
turningoff = Task({
turn = 1; (name+"turning off").postln;
fadeout.wait;
ison = 0; (name+"turned off").postln;
turn = 0 });
weights = Array.fill( dataArray.size, 1 );
updateValues = { var val;
val = 0;
dataArray.do{ |it,i|
val = val + (weights[i]*it.longStdDev);
};
val;
};
updateFunc = {
var returnAction;
var val = updateValues.value ;
if ( ison == 0, {
if ( val > threshold,
{
(name+"turning on").postln;
ison = 1;
if ( offonAction.notNil, { returnAction = offonAction; })
},{ // while off
if ( offAction.notNil, { returnAction = offAction; } )
});
},{
if ( val > threshold,
{ // while on
if ( onAction.notNil, { returnAction = onAction; });
},{ // turning off
if ( turn == 0,
{
turningoff.reset.play;
if ( onoffAction.notNil, { returnAction = onoffAction; } );
});
});
});
if ( returnAction.isNil, { returnAction = defaultRetAction; } );
returnAction.value( val );
};
task = Task.new( { loop{ (this.updateFunc).value; this.waitTime.wait; } } );
}
start{
task.start;
}
stop{
task.stop;
}
}