1+ #include <math.h>
12#include "leapio/leapio.h"
23#include "log.h"
34
4- static BOOL _test_mode = false;
5+ #define CONFIG L".\\chunitouch.ini"
6+
7+ static BOOL _test_mode = FALSE;
58static uint32_t _n_hands = 0 ;
6- float _x , _y , _z ;
9+ static float _x , _y , _z ;
10+ static BOOL use_leap ;
11+ static UINT leap_trigger ;
12+ static UINT leap_step ;
13+ static UINT leap_orientation ;
14+ static BOOL leap_inverted ;
15+
16+ #define LEAP_X 0
17+ #define LEAP_Y 1
18+ #define LEAP_Z 2
719
8- void log_tracks (const LEAP_TRACKING_EVENT * ev ) {
9- log_debug ("saw %u hands.\n" , ev -> nHands );
20+ void handle_track (const LEAP_TRACKING_EVENT * ev ) {
21+ // log_debug("saw %u hands.\n", ev->nHands);
22+ static int8_t last_id = -1 ;
1023 _n_hands = ev -> nHands ;
1124 for (uint32_t h = 0 ; h < ev -> nHands ; h ++ ) {
1225 const LEAP_HAND * hand = & (ev -> pHands [h ]);
13- log_debug ("hand %u is a %s hand. location (%f, %f, %f).\n" ,
14- hand -> id , hand -> type == eLeapHandType_Left ? "left" : "right" ,
15- hand -> palm .position .x , hand -> palm .position .y , hand -> palm .position .z );
26+ _x = hand -> palm .position .x ;
27+ _y = hand -> palm .position .y ;
28+ _z = hand -> palm .position .z ;
29+
30+ if (_test_mode ) {
31+ int8_t id = -1 ;
32+ float pos = 0 ;
33+ if (leap_orientation == LEAP_X ) pos = _x ;
34+ if (leap_orientation == LEAP_Y ) pos = _y ;
35+ if (leap_orientation == LEAP_Z ) pos = _z ;
36+ if ((!leap_inverted && pos > leap_trigger ) || (leap_inverted && leap_trigger > pos )) {
37+ id = (leap_inverted ? -1 : 1 ) * (pos - leap_trigger ) / leap_step - 1 ;
38+ if (id > 5 ) id = 5 ;
39+ if (id < 0 ) id = 0 ;
40+ }
41+
42+ if (last_id != id ) {
43+ if (id >= 0 ) log_info ("IR %d triggered.\n" , id + 1 );
44+ else log_info ("No IR triggered.\n" );
45+ last_id = id ;
46+ }
47+ }
48+
49+ //log_debug("hand %u is a %s hand. location (%f, %f, %f).\n",
50+ // hand->id, hand->type == eLeapHandType_Left ? "left" : "right",
51+ // hand->palm.position.x, hand->palm.position.y, hand->palm.position.z);
1652 }
1753}
1854
@@ -32,11 +68,26 @@ char prompt(const char *p, const char *s, uint8_t n) {
3268}
3369
3470void configure () {
35- bool low_ok = false, high_ok = false;
36- float low_x , low_y , low_z , high_x ;
71+ float low_x , low_y , low_z , high_x , high_y , high_z ;
72+
73+ while (TRUE) {
74+ prompt ("Put your hand at the location you want the bottommost sensor to be activated, press [ENTER] when you are ready." , NULL , 0 );
75+ if (_n_hands == 0 ) {
76+ printf ("I can't see any hands.\n" );
77+ continue ;
78+ }
79+ if (_n_hands > 1 ) {
80+ printf ("I saw more than one hand.\n" );
81+ continue ;
82+ }
83+ low_x = _x ;
84+ low_y = _y ;
85+ low_z = _z ;
86+ break ;
87+ }
3788
38- while (! low_ok ) {
39- prompt ("Put your hand at the lowest location you want the IR sensor to be triggered, then press [ENTER]" , NULL , 0 );
89+ while (TRUE ) {
90+ prompt ("Put your hand at the location you want the topmost sensor to be activated, press [ENTER] when you are ready. " , NULL , 0 );
4091 if (_n_hands == 0 ) {
4192 printf ("I can't see any hands.\n" );
4293 continue ;
@@ -45,20 +96,87 @@ void configure() {
4596 printf ("I saw more than one hand.\n" );
4697 continue ;
4798 }
99+ high_x = _x ;
100+ high_y = _y ;
101+ high_z = _z ;
102+ break ;
48103 }
49104
105+ log_info ("low: (%f, %f, %f), high: (%f, %f, %f).\n" , low_x , low_y , low_z , high_x , high_y , high_z );
106+ float dx = high_x - low_x ;
107+ float dy = high_y - low_y ;
108+ float dz = high_z - low_z ;
109+ float dmax = max (max (fabs (dx ), fabs (dy )), fabs (dz ));
110+ WCHAR leap_orientation_char ;
111+
112+ if (dmax == fabs (dx )) {
113+ leap_orientation_char = 'x' ;
114+ leap_orientation = LEAP_X ;
115+ leap_trigger = low_x ;
116+ }
117+ if (dmax == fabs (dy )) {
118+ leap_orientation_char = 'y' ;
119+ leap_orientation = LEAP_Y ;
120+ leap_trigger = low_y ;
121+ }
122+ if (dmax == fabs (dz )) {
123+ leap_orientation_char = 'z' ;
124+ leap_orientation = LEAP_Z ;
125+ leap_trigger = low_z ;
126+ }
127+ if (leap_orientation == LEAP_X && dx < 0 ) {
128+ leap_inverted = TRUE;
129+ }
130+ if (leap_orientation == LEAP_Y && dy < 0 ) {
131+ leap_inverted = TRUE;
132+ }
133+ if (leap_orientation == LEAP_Z && dz < 0 ) {
134+ leap_inverted = TRUE;
135+ }
136+ leap_step = dmax /6 ;
137+ use_leap = TRUE;
138+
139+ WCHAR leap_trigger_str [16 ];
140+ WCHAR leap_step_str [16 ];
141+ WCHAR leap_orientation_str [16 ];
142+
143+ swprintf_s (leap_trigger_str , 16 , L"%d" , leap_trigger );
144+ swprintf_s (leap_step_str , 16 , L"%d" , leap_step );
145+ swprintf_s (leap_orientation_str , 16 , L"%s%c" , leap_inverted ? L"-" : L"" , leap_orientation_char );
146+
147+ WritePrivateProfileStringW (L"ir" , L"leap_trigger" , leap_trigger_str , CONFIG );
148+ WritePrivateProfileStringW (L"ir" , L"leap_step" , leap_step_str , CONFIG );
149+ WritePrivateProfileStringW (L"ir" , L"leap_orientation" , leap_orientation_str , CONFIG );
50150}
51151
52152void test () {
53153 printf ("Move your hand around. Configurator will print out which IR sensor is being activated.\n" );
54154 prompt ("Press [ENTER] to begin test, press [ENTER] again to end." , NULL , 0 );
55155 _test_mode = true;
56156 (void ) getchar ();
157+ _test_mode = false;
57158}
58159
59160int main () {
161+ leap_trigger = GetPrivateProfileIntW (L"ir" , L"leap_trigger" , 50 , CONFIG );
162+ leap_step = GetPrivateProfileIntW (L"ir" , L"leap_step" , 30 , CONFIG );
163+
164+ WCHAR str_control_src [16 ];
165+ WCHAR str_leap_orientation [16 ];
166+
167+ GetPrivateProfileStringW (L"ir" , L"control_source" , L"touch" , str_control_src , 16 , CONFIG );
168+ GetPrivateProfileStringW (L"ir" , L"leap_orientation" , L"y" , str_leap_orientation , 16 , CONFIG );
169+ use_leap = wcscmp (str_control_src , L"leap" ) == 0 ;
170+
171+ /**/ if (wcscmp (str_leap_orientation , L"x" ) == 0 ) leap_orientation = LEAP_X ;
172+ else if (wcscmp (str_leap_orientation , L"y" ) == 0 ) leap_orientation = LEAP_Y ;
173+ else if (wcscmp (str_leap_orientation , L"z" ) == 0 ) leap_orientation = LEAP_Z ;
174+ else if (wcscmp (str_leap_orientation , L"-x" ) == 0 ) { leap_orientation = LEAP_X ; leap_inverted = TRUE; }
175+ else if (wcscmp (str_leap_orientation , L"-y" ) == 0 ) { leap_orientation = LEAP_Y ; leap_inverted = TRUE; }
176+ else if (wcscmp (str_leap_orientation , L"-z" ) == 0 ) { leap_orientation = LEAP_Z ; leap_inverted = TRUE; }
177+
60178 log_info ("connecting to leap service...\n" );
61- leap_set_tracking_handler (log_tracks ); // debug
179+ leap_set_tracking_handler (handle_track ); // debug
62180
63181 leap_connect (NULL );
64182 while (!leap_is_connected ()) {
@@ -68,6 +186,7 @@ int main () {
68186
69187 while (TRUE) {
70188 printf ("chuni-touch: leap configurator\n" );
189+ printf ("current configured values: enabled: %s, trigger: %d, step: %d, orientation: %s%d.\n" , use_leap ? "true" : "false" , leap_trigger , leap_step , leap_inverted ? "-" : "" , leap_orientation );
71190 printf (" c) configure\n" );
72191 printf (" t) test\n" );
73192 printf ("\n" );
0 commit comments