summaryrefslogtreecommitdiff
path: root/src/hal/components/multiclick.comp
blob: 92d27c13e14dead0fda2ccf58566de3adb6ef7ad (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

//
//   This is a single-, double-, triple-, and quadruple-click detector
//   component for LinuxCNC.
//
//   Copyright 2012 Sebastian Kuzminsky <seb@highlab.com>
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

component multiclick "Single-, double-, triple-, and quadruple-click detector";
license "GPL";

pin in  bit in "The input line, this is where we look for clicks.";

pin out bit single_click
"Goes high briefly when a single-click is detected on the 'in' pin.";

pin out bit single_click_only
"""Goes high briefly when a single-click is detected on the 'in' pin
and no second click followed it.""";

pin out bit double_click
"Goes high briefly when a double-click is detected on the 'in' pin.";

pin out bit double_click_only
"""Goes high briefly when a double-click is detected on the 'in' pin
and no third click followed it.""";

pin out bit triple_click
"Goes high briefly when a triple-click is detected on the 'in' pin.";

pin out bit triple_click_only
"""Goes high briefly when a triple-click is detected on the 'in' pin
and no fourth click followed it.""";

pin out bit quadruple_click
"Goes high briefly when a quadruple-click is detected on the 'in' pin.";

pin out bit quadruple_click_only
"""Goes high briefly when a quadruple-click is detected on the 'in'
pin and no fifth click followed it.""";


// for debugging
pin out s32 state;


param rw bit invert_input = FALSE
"""If FALSE (the default), clicks start with rising edges.  If TRUE,
clicks start with falling edges.""";

param rw u32 max_hold_ns  = 250000000
"""If the input is held down longer than this, it's not part of a
multi-click.  (Default 250,000,000 ns, 250 ms.)""";

param rw u32 max_space_ns = 250000000
"""If the input is released longer than this, it's not part of a
multi-click.  (Default 250,000,000 ns, 250 ms.)""";

param rw u32 output_hold_ns = 100000000
"""Positive pulses on the output pins last this long.  (Default
100,000,000 ns, 100 ms.)""";


variable int click_state = 0;  // wish i could have an enum type here


//
// When entering a new non-IDLE state in the state machine, timer is set to
// 0 and timeout is set to the timeout for the new state.
//
// Each time the component's function runs and the state machine is not
// IDLE, the timer is incremented by the thread period.  When the timer
// exceeds the timeout, the state machine resets.
//

variable unsigned timer;
variable unsigned timeout;


//
// When an "X"-click (for X={single,double,triple,quadruple}) is detected,
// the X-click pin goes high and the X_click_hold_timer is set to the
// output_hold_ns param.  Each invocation of the function, the
// X_click_hold_timer is decremented by the thread period, and when the
// timer falls below 0, the X-click output pin goes  back to low again.
//
// Similarly for the X_click_only_hold_timer, but it doesnt start until
// max_space_ns after the click is detected (to ensure that this is not an
// X+1 click).
//

variable unsigned single_click_hold_timer;
variable unsigned single_click_only_hold_timer;

variable unsigned double_click_hold_timer;
variable unsigned double_click_only_hold_timer;

variable unsigned triple_click_hold_timer;
variable unsigned triple_click_only_hold_timer;

variable unsigned quadruple_click_hold_timer;
variable unsigned quadruple_click_only_hold_timer;


description """A click is defined as a rising edge on the 'in' pin,
followed by the 'in' pin being True for at most 'max-hold-ns' nanoseconds,
followed by a falling edge.

A double-click is defined as two clicks, separated by at
most 'max-space-ns' nanoseconds with the 'in' pin in the False state.

I bet you can guess the definition of triple- and quadruple-click.

You probably want to run the input signal through a debounce component
before feeding it to the multiclick detector, if the input is at all
noisy.

The '*-click' pins go high as soon as the input detects the correct
number of clicks.

The '*-click-only' pins go high a short while after the click, after
the click separator space timeout has expired to show that no further
click is coming.  This is useful for triggering halui MDI commands.""";


function _ nofp "Detect single-, double-, triple-, and quadruple-clicks";

;;

typedef enum {
    IDLE = 0,
    SAW_FIRST_RISING_EDGE,
    SAW_FIRST_CLICK,
    SAW_SECOND_RISING_EDGE,
    SAW_SECOND_CLICK,
    SAW_THIRD_RISING_EDGE,
    SAW_THIRD_CLICK,
    SAW_FOURTH_RISING_EDGE,
    SAW_FOURTH_CLICK,
    HELD_TOO_LONG
} state_t;

FUNCTION(_) {
    int new_in = in;

    if (1 == invert_input) {
        new_in = !new_in;
    }

    if (click_state != IDLE) {
        timer += period;
    }


    //
    // update the output pins
    //

    if (single_click_hold_timer > 0) {
        if (single_click_hold_timer > period) {
            single_click_hold_timer -= period;
        } else {
            single_click_hold_timer = 0;
            single_click = 0;
        }
    }

    if (single_click_only_hold_timer > 0) {
        if (single_click_only_hold_timer > period) {
            single_click_only_hold_timer -= period;
        } else {
            single_click_only_hold_timer = 0;
            single_click_only = 0;
        }
    }

    if (double_click_hold_timer > 0) {
        if (double_click_hold_timer > period) {
            double_click_hold_timer -= period;
        } else {
            double_click_hold_timer = 0;
            double_click = 0;
        }
    }

    if (double_click_only_hold_timer > 0) {
        if (double_click_only_hold_timer > period) {
            double_click_only_hold_timer -= period;
        } else {
            double_click_only_hold_timer = 0;
            double_click_only = 0;
        }
    }

    if (triple_click_hold_timer > 0) {
        if (triple_click_hold_timer > period) {
            triple_click_hold_timer -= period;
        } else {
            triple_click_hold_timer = 0;
            triple_click = 0;
        }
    }

    if (triple_click_only_hold_timer > 0) {
        if (triple_click_only_hold_timer > period) {
            triple_click_only_hold_timer -= period;
        } else {
            triple_click_only_hold_timer = 0;
            triple_click_only = 0;
        }
    }

    if (quadruple_click_hold_timer > 0) {
        if (quadruple_click_hold_timer > period) {
            quadruple_click_hold_timer -= period;
        } else {
            quadruple_click_hold_timer = 0;
            quadruple_click = 0;
        }
    }

    if (quadruple_click_only_hold_timer > 0) {
        if (quadruple_click_only_hold_timer > period) {
            quadruple_click_only_hold_timer -= period;
        } else {
            quadruple_click_only_hold_timer = 0;
            quadruple_click_only = 0;
        }
    }


    //
    // update state, if needed
    //

    switch (click_state) {
        case IDLE: {
            if (1 == new_in) {
                click_state = SAW_FIRST_RISING_EDGE;
                timer = 0;
                timeout = max_hold_ns;
            }
            break;
        }

        case SAW_FIRST_RISING_EDGE: {
            if (0 == new_in) {
                click_state = SAW_FIRST_CLICK;
                timer = 0;
                timeout = max_space_ns;
                single_click = 1;
                single_click_hold_timer = output_hold_ns;
            }
            break;
        }

        case SAW_FIRST_CLICK: {
            if (1 == new_in) {
                click_state = SAW_SECOND_RISING_EDGE;
                timer = 0;
                timeout = max_hold_ns;
            }
            break;
        }

        case SAW_SECOND_RISING_EDGE: {
            if (0 == new_in) {
                click_state = SAW_SECOND_CLICK;
                timer = 0;
                timeout = max_space_ns;
                double_click = 1;
                double_click_hold_timer = output_hold_ns;
            }
            break;
        }

        case SAW_SECOND_CLICK: {
            if (1 == new_in) {
                click_state = SAW_THIRD_RISING_EDGE;
                timer = 0;
                timeout = max_hold_ns;
            }
            break;
        }

        case SAW_THIRD_RISING_EDGE: {
            if (0 == new_in) {
                click_state = SAW_THIRD_CLICK;
                timer = 0;
                timeout = max_space_ns;
                triple_click = 1;
                triple_click_hold_timer = output_hold_ns;
            }
            break;
        }

        case SAW_THIRD_CLICK: {
            if (1 == new_in) {
                click_state = SAW_FOURTH_RISING_EDGE;
                timer = 0;
                timeout = max_hold_ns;
            }
            break;
        }

        case SAW_FOURTH_RISING_EDGE: {
            if (0 == new_in) {
                // four clicks is the most we look for, and we just saw it,
                // so we're done now
                click_state = SAW_FOURTH_CLICK;
                timer = 0;
                timeout = max_space_ns;
                quadruple_click = 1;
                quadruple_click_hold_timer = output_hold_ns;
            }
            break;
        }

        case SAW_FOURTH_CLICK: {
            if (1 == new_in) {
                click_state = HELD_TOO_LONG;
            }
            break;
        }

        case HELD_TOO_LONG: {
            if (0 == new_in) {
                click_state = IDLE;
            }
            break;
        }

        default: {
            // invalid click_state!
            click_state = IDLE;
        }
    }

    if ((click_state != IDLE) && (click_state != HELD_TOO_LONG) && (timer > timeout)) {
        if (1 == new_in) {
            click_state = HELD_TOO_LONG;
        } else {
            // timeout after some activity on the input line, trigger one
            // of the "only" outputs if appropriate
            switch (click_state) {
                case SAW_FIRST_CLICK: {
                    single_click_only = 1;
                    single_click_only_hold_timer = output_hold_ns;
                    break;
                }
                case SAW_SECOND_CLICK: {
                    double_click_only = 1;
                    double_click_only_hold_timer = output_hold_ns;
                    break;
                }
                case SAW_THIRD_CLICK: {
                    triple_click_only = 1;
                    triple_click_only_hold_timer = output_hold_ns;
                    break;
                }
                case SAW_FOURTH_CLICK: {
                    quadruple_click_only = 1;
                    quadruple_click_only_hold_timer = output_hold_ns;
                    break;
                }
            }
            click_state = IDLE;
        }
    }

    state = click_state;
}