-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.c
755 lines (649 loc) · 37.5 KB
/
example_test.c
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include <stdio.h>
#include <stdlib.h>
#include "windows.h"
#include "ebtn.h"
//
// Tests
//
const char *suite_name;
char suite_pass;
int suites_run = 0, suites_failed = 0, suites_empty = 0;
int tests_in_suite = 0, tests_run = 0, tests_failed = 0;
#define QUOTE(str) #str
#define ASSERT(x) \
{ \
tests_run++; \
tests_in_suite++; \
if (!(x)) \
{ \
printf("failed assert [%s:%i] %s\n", __FILE__, __LINE__, QUOTE(x)); \
suite_pass = 0; \
tests_failed++; \
} \
}
void SUITE_START(const char *name)
{
suite_pass = 1;
suite_name = name;
suites_run++;
tests_in_suite = 0;
}
void SUITE_END(void)
{
printf("Testing %s ", suite_name);
size_t suite_i;
for (suite_i = strlen(suite_name); suite_i < 80 - 8 - 5; suite_i++)
printf(".");
printf("%s\n", suite_pass ? " pass" : " fail");
if (!suite_pass)
suites_failed++;
if (!tests_in_suite)
suites_empty++;
}
typedef enum
{
USER_BUTTON_default = 0,
USER_BUTTON_onrelease_debounce,
USER_BUTTON_keepalive_with_click,
USER_BUTTON_max_click_3,
USER_BUTTON_click_multi_max_0,
USER_BUTTON_keep_alive_0,
USER_BUTTON_MAX,
USER_BUTTON_COMBO_0 = 0x100,
USER_BUTTON_COMBO_1,
USER_BUTTON_COMBO_2,
USER_BUTTON_COMBO_3,
USER_BUTTON_COMBO_MAX,
} user_button_t;
/**
* \brief Input state information
*/
typedef struct
{
uint16_t key_id; /*!< Select Key id */
uint8_t state; /*!< Input state -> 1 = active, 0 = inactive */
uint32_t duration; /*!< Time until this state is enabled */
} btn_test_time_t;
/**
* \brief Event sequence
*/
typedef struct
{
ebtn_evt_t evt; /*!< Event type */
uint8_t keepalive_cnt; /*!< Number of keep alive events while button is active */
uint8_t conseq_clicks; /*!< Number of consecutive clicks detected */
} btn_test_evt_t;
typedef struct
{
const char *test_name;
uint16_t test_key_id;
int test_sequence_cnt;
btn_test_time_t *test_sequence; /*!< Input state -> 1 = active, 0 = inactive */
int test_events_cnt;
const btn_test_evt_t *test_events; /*!< Time until this state is enabled */
} btn_test_arr_t;
#define TEST_ARRAY_DEFINE(_key_id, _seq, _evt) \
{ \
.test_key_id = _key_id, .test_name = #_seq, .test_sequence_cnt = EBTN_ARRAY_SIZE(_seq), .test_sequence = _seq, \
.test_events_cnt = EBTN_ARRAY_SIZE(_evt), .test_events = _evt \
}
/* Max number of ms to demonstrate */
#define MAX_TIME_MS 0x3FFFF
#define EBTN_PARAM_TIME_DEBOUNCE_PRESS(_param) _param.time_debounce
#define EBTN_PARAM_TIME_DEBOUNCE_RELEASE(_param) _param.time_debounce_release
#define EBTN_PARAM_TIME_CLICK_MIN(_param) _param.time_click_pressed_min
#define EBTN_PARAM_TIME_CLICK_MAX(_param) _param.time_click_pressed_max
#define EBTN_PARAM_TIME_CLICK_MULTI_MAX(_param) _param.time_click_multi_max
#define EBTN_PARAM_TIME_KEEPALIVE_PERIOD(_param) _param.time_keepalive_period
#define EBTN_PARAM_CLICK_MAX_CONSECUTIVE(_param) _param.max_consecutive
static const ebtn_btn_param_t param_default = EBTN_PARAMS_INIT(20, 0, 20, 300, 200, 500, 10);
static const ebtn_btn_param_t param_onrelease_debounce = EBTN_PARAMS_INIT(20, 80, 0, 300, 200, 500, 10);
static const ebtn_btn_param_t param_keepalive_with_click = EBTN_PARAMS_INIT(20, 80, 0, 400, 200, 100, 10);
static const ebtn_btn_param_t param_max_click_3 = EBTN_PARAMS_INIT(20, 80, 0, 400, 200, 100, 3);
static const ebtn_btn_param_t param_click_multi_max_0 = EBTN_PARAMS_INIT(20, 80, 0, 400, 0, 100, 3);
static const ebtn_btn_param_t param_keep_alive_0 = EBTN_PARAMS_INIT(20, 80, 0, 400, 200, 0, 3);
/* List of used buttons -> test case */
static ebtn_btn_t btns[] = {EBTN_BUTTON_INIT(USER_BUTTON_default, ¶m_default),
EBTN_BUTTON_INIT(USER_BUTTON_onrelease_debounce, ¶m_onrelease_debounce),
EBTN_BUTTON_INIT(USER_BUTTON_keepalive_with_click, ¶m_keepalive_with_click),
EBTN_BUTTON_INIT(USER_BUTTON_max_click_3, ¶m_max_click_3),
EBTN_BUTTON_INIT(USER_BUTTON_click_multi_max_0, ¶m_click_multi_max_0),
EBTN_BUTTON_INIT(USER_BUTTON_keep_alive_0, ¶m_keep_alive_0)};
static volatile uint32_t test_processed_time_current;
/* Set button state -> used for test purposes */
#define BTN_STATE_RAW(_key_id_, _state_, _duration_) \
{ \
.key_id = (_key_id_), .state = (_state_), .duration = (_duration_) \
}
#define BTN_STATE(_state_, _duration_) BTN_STATE_RAW(USER_BUTTON_default, _state_, _duration_)
/* On-Press event */
#define BTN_EVENT_ONPRESS() \
{ \
.evt = EBTN_EVT_ONPRESS \
}
/* On-Release event */
#define BTN_EVENT_ONRELEASE() \
{ \
.evt = EBTN_EVT_ONRELEASE \
}
/* On-Click event */
#define BTN_EVENT_ONCLICK(_conseq_clicks_) \
{ \
.evt = EBTN_EVT_ONCLICK, .conseq_clicks = (_conseq_clicks_) \
}
/* On-Click event */
#define BTN_EVENT_KEEPALIVE(_keepalive_cnt_) \
{ \
.evt = EBTN_EVT_KEEPALIVE, .keepalive_cnt = (_keepalive_cnt_) \
}
/*
* Simulate click event
*/
static btn_test_time_t test_sequence_single_click[] = {
/*
* Step 1:
* Go to active state and stay there for a period
* of minimum debounce time and minimum
* time input must be active to later detect click event
*
* Step 2:
* Go low and stay inactive until time to report click has elapsed.
* Include debounce timing for release event.
*
* Add +1 to the end, to force click event,
* and not to go to "consecutive clicks" if any further tests are added in this sequence
*/
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_single_click[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_double_click[] = {
/*
* Repeat above steps, this time w/o +1 at the end.
*
* Simulate "2" consecutive clicks and report final "click" event at the end of the
* sequence, with "2" consecutive clicks in the report info
*/
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_double_click[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(2),
};
static btn_test_time_t test_sequence_triple_click[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_triple_click[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(3),
};
static btn_test_time_t test_sequence_double_click_critical_time[] = {
/*
* Repeat above steps, this time w/o +1 at the end.
*
* Simulate "2" consecutive clicks and report final "click" event at the end of the
* sequence, with "2" consecutive clicks in the report info
*/
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
/* Hold button in release state for time that is max for 2 clicks - time that we will
indicate in the next press state -> this is the frequency between detected events */
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) +
EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)
/* Decrease by active time in next step */
- (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default)) - 2),
/* Active time */
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_double_click_critical_time[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(2),
};
static btn_test_time_t test_sequence_double_click_critical_time_over[] = {
/*
* This test shows how to handle case when 2 clicks are being executed,
* but time between 2 release events is larger than maximum
* allowed time for consecutive clicks.
*
* In this case, 2 onclick events are sent,
* both with consecutive clicks counter set to 1
*/
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) -
(EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default))),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_double_click_critical_time_over[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
/* This one is to handle click for first sequence */
BTN_EVENT_ONCLICK(1),
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
/* This one is to handle click for second sequence */
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_click_with_keepalive[] = {
/*
* Make a click event, followed by the longer press.
* Simulate "long press" w/ previous click, that has click counter set to 1
*/
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_default)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_default)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_click_with_keepalive[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONPRESS(),
/* This one is to handle click before long press */
BTN_EVENT_ONCLICK(1),
BTN_EVENT_KEEPALIVE(1),
BTN_EVENT_KEEPALIVE(2),
BTN_EVENT_KEEPALIVE(3),
BTN_EVENT_ONRELEASE(),
};
static btn_test_time_t test_sequence_click_with_short[] = {
/* Make with short press (shorter than minimum required) */
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) / 2),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_click_with_short[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
};
static btn_test_time_t test_sequence_click_with_short_with_multi[] = {
/* Make with short press (shorter than minimum required) */
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) / 2),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_click_with_short_with_multi[] = {
/* This one is short... */
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(2),
};
static btn_test_time_t test_sequence_multi_click_with_short[] = {
/* Make 2 clicks, and 3rd one with short press (shorter than minimum required) */
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) / 2),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default)),
};
static const btn_test_evt_t test_events_multi_click_with_short[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
/* This one is short... */
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(2),
};
static btn_test_time_t test_sequence_onpress_debounce[] = {
BTN_STATE(0, 0),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) / 2),
BTN_STATE(0, 1),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) / 2),
BTN_STATE(0, 1),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) / 2),
BTN_STATE(0, 1),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default) + 1),
};
static const btn_test_evt_t test_events_onpress_debounce[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
// for test overflow, make sure enable macro 'EBTN_CONFIG_TIMER_16' or compile with 'make all
// CFLAGS=-DEBTN_CONFIG_TIMER_16'
static btn_test_time_t test_sequence_time_overflow_onpress_debounce[] = {
BTN_STATE(0, 0x0ffff - (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) / 2)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_time_overflow_onpress_debounce[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_time_overflow_onpress[] = {
BTN_STATE(0, 0x0ffff - (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) / 2)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_time_overflow_onpress[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_time_overflow_onrelease_muti[] = {
BTN_STATE(0, 0x0ffff - (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) +
EBTN_PARAM_TIME_CLICK_MIN(param_default) / 2)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_time_overflow_onrelease_muti[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_time_overflow_keepalive[] = {
BTN_STATE(0, 0x0ffff - (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default) +
EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_default) / 2)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_default) + EBTN_PARAM_TIME_CLICK_MIN(param_default)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_default)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_default) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_default)),
};
static const btn_test_evt_t test_events_time_overflow_keepalive[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_KEEPALIVE(1),
BTN_EVENT_ONRELEASE(),
};
///
/// Test onrelease debounce
///
static btn_test_time_t test_sequence_onrelease_debounce[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MIN(param_onrelease_debounce)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_onrelease_debounce)),
};
static const btn_test_evt_t test_events_onrelease_debounce[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_onrelease_debounce_over[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MIN(param_onrelease_debounce)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MIN(param_onrelease_debounce)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_onrelease_debounce)),
};
static const btn_test_evt_t test_events_onrelease_debounce_over[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(2),
};
static btn_test_time_t test_sequence_onrelease_debounce_time_overflow[] = {
BTN_STATE(0, 0x0ffff - (EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MIN(param_onrelease_debounce) +
EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce) / 2)),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MIN(param_onrelease_debounce)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_onrelease_debounce) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_onrelease_debounce)),
};
static const btn_test_evt_t test_events_onrelease_debounce_time_overflow[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
///
/// Test keepalive with click debounce
///
static btn_test_time_t test_sequence_keepalive_with_click[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_keepalive_with_click) + EBTN_PARAM_TIME_CLICK_MIN(param_keepalive_with_click)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_keepalive_with_click)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_keepalive_with_click) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_keepalive_with_click)),
};
static const btn_test_evt_t test_events_keepalive_with_click[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_KEEPALIVE(1),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_time_t test_sequence_keepalive_with_click_double[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_keepalive_with_click) + EBTN_PARAM_TIME_CLICK_MIN(param_keepalive_with_click)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_keepalive_with_click)),
BTN_STATE(1, EBTN_PARAM_TIME_KEEPALIVE_PERIOD(param_keepalive_with_click)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_keepalive_with_click) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_keepalive_with_click)),
};
static const btn_test_evt_t test_events_keepalive_with_click_double[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_KEEPALIVE(1), BTN_EVENT_KEEPALIVE(2), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(1),
};
///
/// Test max multi click with 3
///
static btn_test_time_t test_sequence_max_click_3[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3)),
};
static const btn_test_evt_t test_events_max_click_3[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(3),
};
static btn_test_time_t test_sequence_max_click_3_over[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MIN(param_max_click_3)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_max_click_3) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_max_click_3)),
};
static const btn_test_evt_t test_events_max_click_3_over[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(3), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(1),
};
///
/// Test click multi max with 0
///
static btn_test_time_t test_sequence_click_multi_max_0[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MIN(param_click_multi_max_0)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_click_multi_max_0) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MIN(param_click_multi_max_0)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_click_multi_max_0) / 2),
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MIN(param_click_multi_max_0)),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_click_multi_max_0) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_click_multi_max_0)),
};
static const btn_test_evt_t test_events_click_multi_max_0[] = {
BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(1), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1), BTN_EVENT_ONPRESS(), BTN_EVENT_ONRELEASE(), BTN_EVENT_ONCLICK(1),
};
///
/// Test max keepalive with 0
///
static btn_test_time_t test_sequence_keep_alive_0[] = {
BTN_STATE(1, EBTN_PARAM_TIME_DEBOUNCE_PRESS(param_keep_alive_0) + EBTN_PARAM_TIME_CLICK_MIN(param_keep_alive_0)),
BTN_STATE(1, EBTN_PARAM_TIME_CLICK_MAX(param_keep_alive_0) / 2),
BTN_STATE(0, EBTN_PARAM_TIME_DEBOUNCE_RELEASE(param_keep_alive_0) + EBTN_PARAM_TIME_CLICK_MULTI_MAX(param_keep_alive_0)),
};
static const btn_test_evt_t test_events_keep_alive_0[] = {
BTN_EVENT_ONPRESS(),
BTN_EVENT_ONRELEASE(),
BTN_EVENT_ONCLICK(1),
};
static btn_test_arr_t test_list[] = {
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_single_click, test_events_single_click),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_double_click, test_events_double_click),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_triple_click, test_events_triple_click),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_double_click_critical_time, test_events_double_click_critical_time),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_double_click_critical_time_over, test_events_double_click_critical_time_over),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_click_with_keepalive, test_events_click_with_keepalive),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_click_with_short, test_events_click_with_short),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_click_with_short_with_multi, test_events_click_with_short_with_multi),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_multi_click_with_short, test_events_multi_click_with_short),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_onpress_debounce, test_events_onpress_debounce),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_time_overflow_onpress_debounce, test_events_time_overflow_onpress_debounce),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_time_overflow_onpress, test_events_time_overflow_onpress),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_time_overflow_onrelease_muti, test_events_time_overflow_onrelease_muti),
TEST_ARRAY_DEFINE(USER_BUTTON_default, test_sequence_time_overflow_keepalive, test_events_time_overflow_keepalive),
TEST_ARRAY_DEFINE(USER_BUTTON_onrelease_debounce, test_sequence_onrelease_debounce, test_events_onrelease_debounce),
TEST_ARRAY_DEFINE(USER_BUTTON_onrelease_debounce, test_sequence_onrelease_debounce_over, test_events_onrelease_debounce_over),
TEST_ARRAY_DEFINE(USER_BUTTON_onrelease_debounce, test_sequence_onrelease_debounce_time_overflow, test_events_onrelease_debounce_time_overflow),
TEST_ARRAY_DEFINE(USER_BUTTON_keepalive_with_click, test_sequence_keepalive_with_click, test_events_keepalive_with_click),
TEST_ARRAY_DEFINE(USER_BUTTON_keepalive_with_click, test_sequence_keepalive_with_click_double, test_events_keepalive_with_click_double),
TEST_ARRAY_DEFINE(USER_BUTTON_max_click_3, test_sequence_max_click_3, test_events_max_click_3),
TEST_ARRAY_DEFINE(USER_BUTTON_max_click_3, test_sequence_max_click_3_over, test_events_max_click_3_over),
TEST_ARRAY_DEFINE(USER_BUTTON_click_multi_max_0, test_sequence_click_multi_max_0, test_events_click_multi_max_0),
TEST_ARRAY_DEFINE(USER_BUTTON_keep_alive_0, test_sequence_keep_alive_0, test_events_keep_alive_0),
};
static btn_test_arr_t *select_test_item;
/* Get button state for given current time */
static uint8_t prv_get_state_for_time(uint16_t key_id, uint32_t time)
{
uint8_t state = 0;
uint32_t duration = 0;
if (select_test_item->test_key_id == key_id)
{
// printf("time: %d, key_id: %d\n", time, key_id);
for (size_t i = 0; i < select_test_item->test_sequence_cnt; ++i)
{
if (select_test_item->test_sequence[i].duration == 0)
{
continue;
}
duration += select_test_item->test_sequence[i].duration + 1; /* Advance time, need add 1 for state switch time. */
// printf("i: %d, duration: %d\n", i, duration);
if (time <= duration)
{
state = select_test_item->test_sequence[i].state;
// printf("i: %d, time: %d, duration: %d, state: %d\n", i, time, duration, state);
// printf("state: %d\n", state);
break;
}
}
}
return state;
}
static uint32_t test_get_state_total_duration(void)
{
uint32_t duration = 0;
for (size_t i = 0; i < select_test_item->test_sequence_cnt; ++i)
{
if (select_test_item->test_sequence[i].duration == 0)
{
continue;
}
duration += select_test_item->test_sequence[i].duration + 1; /* Advance time, need add 1 for state switch time. */
}
return duration;
}
/* Get button state */
static uint8_t prv_btn_get_state(struct ebtn_btn *btn)
{
uint8_t state = prv_get_state_for_time(btn->key_id, test_processed_time_current);
(void)btn;
return state;
}
static uint32_t test_processed_event_time_prev;
static uint32_t test_processed_array_index = 0;
/* Process button event */
static void prv_btn_event(struct ebtn_btn *btn, ebtn_evt_t evt)
{
const char *s;
uint32_t color, keepalive_cnt = 0, diff_time;
const btn_test_evt_t *test_evt_data = NULL;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (test_processed_array_index >= select_test_item->test_events_cnt)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
printf("[%7u] ERROR! Array index is out of bounds!\r\n", (unsigned)test_processed_time_current);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
{
test_evt_data = &select_test_item->test_events[test_processed_array_index];
}
/* Handle timing */
diff_time = test_processed_time_current - test_processed_event_time_prev;
test_processed_event_time_prev = test_processed_time_current;
keepalive_cnt = btn->keepalive_cnt;
/* Event type must match */
ASSERT((test_evt_data != NULL) && (test_evt_data->evt == evt));
/* Get event string */
if (evt == EBTN_EVT_KEEPALIVE)
{
s = "KEEPALIVE";
color = FOREGROUND_RED | FOREGROUND_BLUE;
ASSERT((test_evt_data != NULL) && (test_evt_data->keepalive_cnt == keepalive_cnt));
}
else if (evt == EBTN_EVT_ONPRESS)
{
s = "ONPRESS";
color = FOREGROUND_GREEN;
}
else if (evt == EBTN_EVT_ONRELEASE)
{
s = "ONRELEASE";
color = FOREGROUND_BLUE;
}
else if (evt == EBTN_EVT_ONCLICK)
{
s = "ONCLICK";
color = FOREGROUND_RED | FOREGROUND_GREEN;
ASSERT((test_evt_data != NULL) && (test_evt_data->conseq_clicks == btn->click_cnt));
}
else
{
s = "UNKNOWN";
color = FOREGROUND_RED;
}
SetConsoleTextAttribute(hConsole, color);
printf("[%7u][%6u] ID(hex):%4x, evt:%10s, keep-alive cnt: %3u, click cnt: %3u\r\n", (unsigned)test_processed_time_current, (unsigned)diff_time, btn->key_id,
s, (unsigned)keepalive_cnt, (unsigned)btn->click_cnt);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
++test_processed_array_index; /* Go to next step in next event */
}
/**
* \brief Test function
*/
int example_test(void)
{
printf("Test running\r\n");
for (int index = 0; index < EBTN_ARRAY_SIZE(test_list); index++)
{
select_test_item = &test_list[index];
SUITE_START(select_test_item->test_name);
// printf("\n");
// init variable
test_processed_event_time_prev = 0;
test_processed_array_index = 0;
/* Define buttons */
ebtn_init(btns, EBTN_ARRAY_SIZE(btns), NULL, 0, prv_btn_get_state, prv_btn_event);
/* Counter simulates ms tick */
for (uint32_t i = 0; i < MAX_TIME_MS; ++i)
{
test_processed_time_current = i; /* Set current time used in callback */
ebtn_process(i); /* Now run processing */
// printf("time: %d, end: %d, in_process(): %d/%d\n", i, test_processed_array_index >= select_test_item->test_events_cnt
// , ebtn_is_btn_in_process(ebtn_get_btn_by_key_id(select_test_item->test_key_id)), ebtn_is_in_process());
// check end
if (test_processed_array_index >= select_test_item->test_events_cnt)
{
uint32_t duration = test_get_state_total_duration();
if (i > duration + 1)
{
ASSERT(!ebtn_is_btn_in_process(ebtn_get_btn_by_key_id(select_test_item->test_key_id)));
ASSERT(!ebtn_is_in_process());
}
}
}
ASSERT(test_processed_array_index == select_test_item->test_events_cnt);
// printf("\n");
SUITE_END();
}
return 0;
}