-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_user.c
153 lines (127 loc) · 3.99 KB
/
example_user.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
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#if 1
#include "eled.h"
#include "test_timer.h"
static LARGE_INTEGER freq, sys_start_time;
static uint32_t get_tick(void);
typedef enum
{
USER_LED_RED = 0,
USER_LED_GREEN,
USER_LED_BLUE,
USER_LED_MAX,
} user_led_t;
/* User defined settings */
static const eled_led_param_t test_param_0 = ELED_PARAMS_INIT(1, 200, 200, 5, 0, 0);
static const eled_led_param_t test_param_1 = ELED_PARAMS_INIT(2, 800, 200, 3, 0, 0);
static const eled_led_param_t test_param_2 = ELED_PARAMS_INIT(3, 3000, 2000, 1, 0, 0);
static void test_timer_timeout(void *arg)
{
// printf("test_timer_timeout()\n");
eled_process_next_state(arg);
}
static eled_led_t led_red;
static eled_led_t led_green;
static eled_led_t led_blue;
static struct test_timer timer_red = {NULL, 0, &led_red, test_timer_timeout};
static struct test_timer timer_green = {NULL, 0, &led_green, test_timer_timeout};
static struct test_timer timer_blue = {NULL, 0, &led_blue, test_timer_timeout};
static eled_led_t led_red = ELED_LED_INIT(USER_LED_RED, &timer_red);
static eled_led_t led_green = ELED_LED_INIT(USER_LED_GREEN, &timer_green);
static eled_led_t led_blue = ELED_LED_INIT(USER_LED_BLUE, &timer_blue);
uint32_t last_time_keys[USER_LED_MAX - USER_LED_RED] = {0};
void prv_led_set_state(struct eled_led *led, uint8_t state)
{
uint32_t color;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
uint32_t *diff_time_ptr = &last_time_keys[led->led_id - USER_LED_RED];
uint32_t diff_time = get_tick() - *diff_time_ptr;
/* This is for purpose of test and timing validation */
if (diff_time > 2000)
{
diff_time = 0;
}
*diff_time_ptr = get_tick(); /* Set current date as last one */
if (state)
{
if (led == &led_red)
{
color = FOREGROUND_RED;
}
else if (led == &led_green)
{
color = FOREGROUND_GREEN;
}
else if (led == &led_blue)
{
color = FOREGROUND_BLUE;
}
}
else
{
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
SetConsoleTextAttribute(hConsole, color);
printf("[%7u][%6u] ID(hex):%4x, state: %s, reserve-cnt: %3u\r\n", (unsigned)test_timer_get_ticks(), (unsigned)diff_time, led->led_id, state ? "ON" : "OFF",
led->blink_reserve_cnt);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
/* Process led event */
static void proc_led_end_event(struct eled_led *led)
{
printf("[%7u] LED end event. ID(hex):%4x, effect_id:0x%x\r\n", (unsigned)test_timer_get_ticks(), led->led_id, led->param.id);
}
void user_eled_start_timer(struct eled_led *led, uint16_t time)
{
struct test_timer *timer = led->timer_handle;
// printf("eled_start_timer(), time: %d\n", time);
test_timer_start(timer, time);
}
void user_eled_stop_timer(struct eled_led *led)
{
struct test_timer *timer = led->timer_handle;
// printf("eled_stop_timer()");
test_timer_stop(timer);
}
/**
* \brief Example function
*/
int example_user(void)
{
uint32_t time_last;
printf("Application running\r\n");
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&sys_start_time);
/* Define leds */
eled_init(prv_led_set_state, proc_led_end_event);
eled_start(&led_red, &test_param_0);
eled_start(&led_green, &test_param_1);
eled_start(&led_blue, &test_param_2);
while (1)
{
test_timer_polling();
/* Artificial sleep to offload win process */
Sleep(5);
}
return 0;
}
uint32_t user_test_timer_get_ticks(void)
{
return get_tick();
}
/**
* \brief Get current tick in ms from start of program
* \return uint32_t: Tick in ms
*/
static uint32_t get_tick(void)
{
LONGLONG ret;
LARGE_INTEGER now;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&now);
ret = now.QuadPart - sys_start_time.QuadPart;
return (uint32_t)((ret * 1000) / freq.QuadPart);
}
#endif