aboutsummaryrefslogtreecommitdiff
path: root/itrax2/itrax2.c
blob: 8c9a28b29bb60b16341a4c9219ef57c541167819 (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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

itrax2 written by Thomas Musil (c) IEM KUG Graz Austria 2000 - 2004 */

#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#include "m_pd.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "iemlib.h"
#include "isense.h"

typedef struct _itrax2
{
	t_object						x_obj;
	float								x_poll;
	ISD_TRACKER_HANDLE	x_handle;
	void								*x_out2;
	void								*x_out3;
	void								*x_clock;
} t_itrax2;

static t_class *itrax2_class;

static void itrax2_tick(t_itrax2 *x)
{
	if(x->x_handle > 0)
	{
		ISD_TRACKER_DATA_TYPE data;

		clock_delay(x->x_clock, x->x_poll);
		ISD_GetData(x->x_handle, &data);
		outlet_float(x->x_out3, (float)(data.Station[0].Orientation[2]));
		outlet_float(x->x_out2, (float)(data.Station[0].Orientation[1]));
		outlet_float(x->x_obj.ob_outlet, (float)(data.Station[0].Orientation[0]));
	}
	else
		clock_unset(x->x_clock);
}

static void itrax2_init(t_itrax2 *x)
{
	x->x_handle = ISD_OpenTracker((Hwnd)NULL, 0, FALSE, FALSE);
	if(x->x_handle <= 0)
		post("Tracker not found");
	else
		post("Intertrax2 dedected, OK");
}

static void itrax2_reset(t_itrax2 *x)
{
	ISD_ResetHeading(x->x_handle, 1);
}

static void itrax2_bang(t_itrax2 *x)
{
	clock_delay(x->x_clock, x->x_poll);
}

static void itrax2_start(t_itrax2 *x)
{
	itrax2_bang(x);
}

static void itrax2_stop(t_itrax2 *x)
{
	clock_unset(x->x_clock);
}

static void itrax2_float(t_itrax2 *x, t_float cmd)
{
	if(cmd == 0.0)
		itrax2_stop(x);
	else
		itrax2_bang(x);
}

static void itrax2_ft1(t_itrax2 *x, t_float polltime_ms)
{
	if(polltime_ms < 8.0)
	{
		polltime_ms = 8.0;
		post("serial polling-time clipped to 8 ms");
	}
	x->x_poll = polltime_ms;
}

static void *itrax2_new(t_floatarg polltime_ms)
{
	t_itrax2 *x = (t_itrax2 *)pd_new(itrax2_class);

	inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
	outlet_new(&x->x_obj, &s_float);
	x->x_out2 = outlet_new(&x->x_obj, &s_float);
	x->x_out3 = outlet_new(&x->x_obj, &s_float);
	x->x_clock = clock_new(x, (t_method)itrax2_tick);
	itrax2_ft1(x, polltime_ms);
	x->x_handle = 0;
	return(x);
}

static void itrax2_ff(t_itrax2 *x)
{
	clock_free(x->x_clock);
	if(x->x_handle > 0)
		ISD_CloseTracker(x->x_handle);
}

void itrax2_setup(void)
{
	itrax2_class = class_new(gensym("itrax2"), (t_newmethod)itrax2_new,
		(t_method)itrax2_ff, sizeof(t_itrax2), 0, A_DEFFLOAT, 0);
	class_addbang(itrax2_class, itrax2_bang);
	class_addfloat(itrax2_class, itrax2_float);
	class_addmethod(itrax2_class, (t_method)itrax2_start, gensym("start"), 0);
	class_addmethod(itrax2_class, (t_method)itrax2_stop, gensym("stop"), 0);
	class_addmethod(itrax2_class, (t_method)itrax2_init, gensym("init"), 0);
	class_addmethod(itrax2_class, (t_method)itrax2_reset, gensym("reset"), 0);
	class_addmethod(itrax2_class, (t_method)itrax2_ft1, gensym("ft1"), A_FLOAT, 0);
	class_sethelpsymbol(itrax2_class, gensym("iemhelp/help-itrax2"));

	post("itrax2 (R-1.15) library loaded!");
}