diff options
Diffstat (limited to 'vubars.c')
-rw-r--r-- | vubars.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/vubars.c b/vubars.c new file mode 100644 index 0000000..8fcf081 --- /dev/null +++ b/vubars.c | |||
@@ -0,0 +1,148 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <math.h> | ||
3 | |||
4 | #include "gm.h" | ||
5 | #include "vubars.h" | ||
6 | #include "heuristics.h" | ||
7 | |||
8 | // This is our current idea of how we want to influence the weather | ||
9 | // 128 means: do nothing at all | ||
10 | // | ||
11 | // We want to default to softening the weather, so our value always | ||
12 | // flows back towards 100 | ||
13 | static double vu_value_main = 100.0; | ||
14 | static double vu_value_softening = 0.0; | ||
15 | static double vu_value_activating = 0.0; | ||
16 | |||
17 | static int wii_last[WII_COUNT * 4]; | ||
18 | static int wii_bars[WII_COUNT * 4]; | ||
19 | |||
20 | int vubars_peak( int wii_id, int led, int is_on ) { | ||
21 | if( is_on ) { | ||
22 | wii_last[wii_id*4+led] = 0; | ||
23 | } else { | ||
24 | if( ++wii_last[wii_id*4+led] == 1 ) | ||
25 | wii_bars[wii_id*4+led] = VU_PEAK; | ||
26 | } | ||
27 | } | ||
28 | |||
29 | int vubars_getinfo( int wii_id, int led ) { | ||
30 | return wii_bars[wii_id*4+led]; | ||
31 | } | ||
32 | |||
33 | int vubars_getweather( ) { | ||
34 | return (int)vu_value_main; | ||
35 | } | ||
36 | |||
37 | static int GESTURE_A_dd = 0; | ||
38 | static int GESTURE_A_dx = 0; | ||
39 | static int GESTURE_A_dy = 0; | ||
40 | |||
41 | static int GESTURE_A_off = 0; | ||
42 | static int GESTURE_A_ttl = 0; | ||
43 | |||
44 | static int GESTURE_B_dd = 0; | ||
45 | static int GESTURE_B_dx = 0; | ||
46 | static int GESTURE_B_dy = 0; | ||
47 | |||
48 | static int GESTURE_B_off = 0; | ||
49 | static int GESTURE_B_ttl = 0; | ||
50 | |||
51 | static void match_gesture( int off, int dx, int dy, int dd ) { | ||
52 | if( ( GESTURE_A_dd == dd ) && ( GESTURE_A_off == off + dd ) ) { | ||
53 | printf( "...continued A\n" ); | ||
54 | |||
55 | GESTURE_A_off = off; | ||
56 | GESTURE_A_dx = dx; | ||
57 | GESTURE_A_dy = dy; | ||
58 | GESTURE_A_ttl = VU_PEAK; | ||
59 | |||
60 | } else if ( ( GESTURE_B_dd == dd ) && ( GESTURE_B_off == off + dd ) ) { | ||
61 | printf( "...continued B\n" ); | ||
62 | |||
63 | GESTURE_B_off = off; | ||
64 | GESTURE_B_dx = dx; | ||
65 | GESTURE_B_dy = dy; | ||
66 | GESTURE_B_ttl = VU_PEAK; | ||
67 | |||
68 | } else { | ||
69 | printf( "\n" ); | ||
70 | |||
71 | if( GESTURE_B_ttl >= GESTURE_A_ttl ) { | ||
72 | GESTURE_A_off = off; | ||
73 | GESTURE_A_dd = dd; | ||
74 | GESTURE_A_dx = dx; | ||
75 | GESTURE_A_dy = dy; | ||
76 | GESTURE_A_ttl = VU_PEAK; | ||
77 | } else { | ||
78 | GESTURE_B_off = off; | ||
79 | GESTURE_B_dd = dd; | ||
80 | GESTURE_B_dx = dx; | ||
81 | GESTURE_B_dy = dy; | ||
82 | GESTURE_B_ttl = VU_PEAK; | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | void vubars_reduce() { | ||
88 | int i; | ||
89 | double diff; | ||
90 | |||
91 | // First do some heuristics voodoo | ||
92 | int offa, offb, dxa, dxb, dya, dyb, dda, ddb; | ||
93 | |||
94 | heuristics_getGestures( 16, wii_bars, &offa, &dxa, &dya, &dda, &offb, &dxb, &dyb, &ddb ); | ||
95 | |||
96 | if( ( offa != -1 ) && ( dxa > 2 ) ) { | ||
97 | printf( "1mary gesture detected: offset: % 2d direction: %s len: % 2d speed: %lf", | ||
98 | offa, dda < 0 ? "left" : "rite", dxa, (double)dya / (double)dxa ); | ||
99 | match_gesture( offa, dxa, dya, dda ); | ||
100 | } | ||
101 | |||
102 | |||
103 | if( ( offb != -1 ) && ( dxb > 2 ) ) { | ||
104 | printf( "2ndry gesture detected: offset: % 2d direction: %s len: % 2d speed: %lf\n", | ||
105 | offb, ddb < 0 ? "left" : "rite", dxb, (double)dyb / (double)dxb ); | ||
106 | match_gesture( offb, dxb, dyb, ddb ); | ||
107 | } | ||
108 | |||
109 | // Have two gestures living at one | ||
110 | if( GESTURE_A_ttl && GESTURE_B_ttl ) { | ||
111 | if( GESTURE_A_dd == GESTURE_B_dd ) { | ||
112 | // printf( "Two gestures pointing into the same direction.\n" ); | ||
113 | } else { | ||
114 | // The more interesting case: hands move into opposite directions | ||
115 | if( GESTURE_A_dd == -1 ) { | ||
116 | if ( GESTURE_A_off < GESTURE_B_off ) | ||
117 | printf( "Slower! (Case 1)\n" ); | ||
118 | else | ||
119 | printf( "Faster! (Case 2)\n" ); | ||
120 | } else { | ||
121 | if ( GESTURE_A_off < GESTURE_B_off ) | ||
122 | printf( "Faster! (Case 3)\n" ); | ||
123 | else | ||
124 | printf( "Slower! (Case 4)\n" ); | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | // Now try to modify the weather by using values from our vu-bars | ||
130 | diff = (double)heuristics_getEnergy( 16, wii_bars ); | ||
131 | vu_value_main += diff / 40000.0; | ||
132 | |||
133 | diff = vu_value_main - 100.0; | ||
134 | if( diff<0 ) diff = -diff; | ||
135 | diff = sqrt( diff ); | ||
136 | |||
137 | if( vu_value_main > 100.0 ) diff = -diff; | ||
138 | |||
139 | vu_value_main += diff; | ||
140 | |||
141 | for( i=0; i<WII_COUNT*4; ++i ) | ||
142 | if( wii_bars[i] ) | ||
143 | wii_bars[i]-=VU_STEP; | ||
144 | |||
145 | if( GESTURE_A_ttl ) GESTURE_A_ttl -= VU_STEP; | ||
146 | if( GESTURE_B_ttl ) GESTURE_B_ttl -= VU_STEP; | ||
147 | |||
148 | } | ||