aboutsummaryrefslogtreecommitdiff
path: root/gem_counter.c
blob: c3c823651be04d108eb0f7126e1754e73d7cdf6e (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (c) 1997-1999 Mark Danks.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
 */

#include "m_pd.h"

/* -------------------------- gem_counter ------------------------------ */

/* instance structure */
static t_class *gem_counter_class;

typedef struct _gem_counter
{
	t_object    x_obj;	    /* obligatory object header */
	int	        c_current;	/* current number */
	int	        c_high;	    /* highest number */
	int	        c_low;	    /* lowest number */
	int	        c_updown;	/* 0 = going up, 1 = going down */
	int	        c_dir;	    /* gem_counter dir. 1=up, 2=down, 3=up/down */
	t_outlet    *t_out1;	/* the outlet */
	t_outlet    *t_out2;	/* the outlet */
} t_gem_counter;

void gem_counter_bang(t_gem_counter *x)
{
	int sendBang = 0;
    switch(x->c_dir)
    {
		// count up
	    case 1:
            x->c_current++;
	        if (x->c_current > x->c_high)
				x->c_current = x->c_low;
	        else if (x->c_current < x->c_low)
				x->c_current = x->c_low;
			else if (x->c_current == x->c_high)
				sendBang = 1;
	        break;
	    // count down
		case 2:
	        x->c_current--;
	        if (x->c_current < x->c_low)
				x->c_current = x->c_high;
	        else if (x->c_current > x->c_high)
				x->c_current = x->c_high;
			else if (x->c_current == x->c_low)
				sendBang = 1;
	        break;
	    // count up and down
		case 3:
	        // going up
			if (x->c_updown == 0)
	        {
                x->c_current++;
		        if (x->c_current > x->c_high)
		        {
		            x->c_current = x->c_high - 1;
		            x->c_updown = 1;
		        }
		        else if (x->c_current < x->c_low)
					x->c_current = x->c_low;
				else if (x->c_current == x->c_high)
					sendBang = 1;
	        }
	        // going down
			else if (x->c_updown == 1)
	        {
                x->c_current--;
		        if (x->c_current < x->c_low)
		        {
		            x->c_current = x->c_low + 1;
		            x->c_updown = 0;
		        }
		        else if (x->c_current > x->c_high)
					x->c_current = x->c_high;
				else if (x->c_current == x->c_low)
					sendBang = 1;
	        }
	        else
	        {
		        error("up/down wrong");
		        return;
	        }
	        break;
	    default:
	        error("dir wrong");
	        return;
    }
    outlet_float(x->t_out1, (float)x->c_current);
	if (sendBang)
		outlet_bang(x->t_out2);
}

void gem_counter_dir(t_gem_counter *x, t_floatarg n)
{
    if (n == 1 || n == 2 || n == 3) x->c_dir = (int)n;
    else error("bad dir");
}

void gem_counter_high(t_gem_counter *x, t_floatarg n)
{
    x->c_high = (int)n;
}

void gem_counter_low(t_gem_counter *x, t_floatarg n)
{
    x->c_low = (int)n;
}

void gem_counter_reset(t_gem_counter *x, t_symbol *s, int argc, t_atom *argv)
{
    if (!argc)
    {
	    switch(x->c_dir)
	    {
	        case 1:
		        x->c_current = x->c_low;
		        break;
	        case 2:
		        x->c_current = x->c_high;
		        break;
	        case 3:
		        if (x->c_updown == 0) x->c_current = x->c_low;
		        else if (x->c_updown == 1) x->c_current = x->c_high;
		        break;
	        default:
		        return;
	    }
    }
    else
    {
	    switch(argv[0].a_type)
	    {
	        case A_FLOAT :
		        x->c_current = (int)argv[0].a_w.w_float;
		        break;
	        default :
		        error ("gem_counter: reset not float");
		        break;
	    }
    }
    outlet_float(x->t_out1, (float)x->c_current);
}

void gem_counter_clear(t_gem_counter *x, t_symbol *s, int argc, t_atom *argv)
{
    if (!argc)
    {
	    switch(x->c_dir)
	    {
	        case 1:
		        x->c_current = x->c_low - 1;
		        break;
	        case 2:
		        x->c_current = x->c_high + 1;
		        break;
	        case 3:
		        if (x->c_updown == 0) x->c_current = x->c_low - 1;
		        else if (x->c_updown == 1) x->c_current = x->c_high + 1;
		        break;
	        default:
		        break;
	    }
    }
    else
    {
	    switch(argv[0].a_type)
	    {
	        case A_FLOAT :
		        x->c_current = (int)argv[0].a_w.w_float - 1;
		        break;
	        default :
		        error ("gem_counter: reset not float");
		        break;
	    }
    }
}

void *gem_counter_new(t_floatarg f, t_floatarg g, t_floatarg h) /* init vals in struc */
{
    t_gem_counter *x = (t_gem_counter *)pd_new(gem_counter_class);
    x->t_out1 = outlet_new(&x->x_obj, 0);
    x->t_out2 = outlet_new(&x->x_obj, 0);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("fl1"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("fl2"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("fl3"));
    x->c_current = 0;
    x->c_updown = 0;
    if (h)
    {
	    gem_counter_low(x, f);
	    gem_counter_high(x, g);
	    gem_counter_dir(x, h);
    }
    else if (g)
    {
	    x->c_dir = 1;
	    gem_counter_low(x, f);
	    gem_counter_high(x, g);
    }
    else if (f)
    {
	    x->c_dir = x->c_low = 1;
	    gem_counter_high(x, f);
    }
    else
    {
	    x->c_dir = x->c_low = 1;
	    x->c_high = 10;
    }
    return (x);
}

void gem_counter_setup(void)
{
    gem_counter_class = class_new(gensym("gem_counter"), (t_newmethod)gem_counter_new, 0,
    	    sizeof(t_gem_counter), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addbang(gem_counter_class, (t_method)gem_counter_bang);
    class_addmethod(gem_counter_class, (t_method)gem_counter_dir, gensym("fl1"), A_FLOAT, 0);
    class_addmethod(gem_counter_class, (t_method)gem_counter_low, gensym("fl2"), A_FLOAT, 0);
    class_addmethod(gem_counter_class, (t_method)gem_counter_high, gensym("fl3"), A_FLOAT, 0);
    class_addmethod(gem_counter_class, (t_method)gem_counter_reset, gensym("reset"), A_GIMME, 0);
    class_addmethod(gem_counter_class, (t_method)gem_counter_clear, gensym("clear"), A_GIMME, 0);

#if PD_MINOR_VERSION < 37 
	class_sethelpsymbol(gem_counter_class, gensym("gem_counter-help.pd"));
#endif
}