Debian Codegraph @

examples/analysis/tracking/Smacking.ck

raw
1 //--
2 // name: Smacking.ck
3 // desc: class and process for tracking smack events from sudden motion sensor
4 // only works with SMS-enabled laptops
5 //
6 // author: Rebecca Fiebrink + Ge Wang
7 // date: 2007
8 //--
9
10
11 // public class
12 public class Smacking
13 {
14 static Event @ the_event;
15 }
16
17 // initialize separately (due to a bug)
18 new Event @=> Smacking.the_event;
19
20 // smack detection threshold (less -> more sensitive)
21 15 => int threshold;
22 // pol rate
23 .01::second => dur poll_rate;
24 // recover time (after smack detection)
25 .10::second => dur recover_time;
26
27 // instantiate a Hid object
28 Hid hi;
29 HidMsg msg;
30
31 // open tilt sensor
32 if( !hi.openTiltSensor() )
33 {
34 <<< "tilt sensor unavailable", "" >>>;
35 me.exit();
36 }
37
38 // "doh"
39 <<< "hit me!", "" >>>;
40
41 // remember
42 HidMsg last;
43 // initialize
44 hi.read( 9, 0, last );
45
46 // infinite event loop
47 while( true )
48 {
49 // poll the tilt sensor, expect to get back 3 element array of ints
50 hi.read( 9, 0, msg );
51
52 // compute difference
53 (Math.abs(msg.x - last.x) + Math.abs(msg.y - last.y) + Math.abs(msg.z - last.z)) => int diff;
54 // check threshold
55 if (diff > threshold) {
56 // smack detected!
57 // <<< "ouch!" >>>;
58 // fire!
59 Smacking.the_event.broadcast();
60 // go
61 recover_time => now;
62 } else {
63 // uncomment the following to dynamically calibrate the smack sensor
64 // msg.x => last.x;
65 // msg.y => last.y;
66 // msg.z => last.z;
67
68 // advance
69 poll_rate => now;
70 }
71 }