leds.c 4.47 KB
Newer Older
1
#include "leds.h"
fleur's avatar
fleur committed
2
#include "pmic.h"
3
#include "FreeRTOS.h"
zenox's avatar
zenox committed
4
#include "timers.h"
5
6
7
8
9
#include "task.h"
#include "epicardium.h"
#include "modules.h"

#include <stdbool.h>
fleur's avatar
fleur committed
10
11

//TODO: create smth like vTaskDelay(pdMS_TO_TICKS(//put ms here)) for us, remove blocking delay from /lib/leds.c to avoid process blocking
12

13
14
#define NUM_LEDS 15 /* Take from lib/card10/leds.c */

15
static void do_update()
16
{
17
18
	while (hwlock_acquire(HWLOCK_LED, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
19
	}
20
21
22
23
24

	leds_update_power();
	leds_update();

	hwlock_release(HWLOCK_LED);
25
26
}

27
28
void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)
{
29
30
31
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return;

fleur's avatar
fleur committed
32
	leds_prep(led, r, g, b);
33
	do_update();
fleur's avatar
fleur committed
34
35
36
37
}

void epic_leds_set_hsv(int led, float h, float s, float v)
{
38
39
40
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return;

fleur's avatar
fleur committed
41
	leds_prep_hsv(led, h, s, v);
42
	do_update();
fleur's avatar
fleur committed
43
44
45
46
}

void epic_leds_prep(int led, uint8_t r, uint8_t g, uint8_t b)
{
47
48
49
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return;

fleur's avatar
fleur committed
50
51
52
	leds_prep(led, r, g, b);
}

53
54
55
56
57
58
59
60
61
62
63
int epic_leds_get_rgb(int led, uint8_t *rgb)
{
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return -EPERM;
	if (led < 0 || led >= NUM_LEDS)
		return -EINVAL;

	leds_get_rgb(led, rgb);
	return 0;
}

fleur's avatar
fleur committed
64
65
void epic_leds_prep_hsv(int led, float h, float s, float v)
{
66
67
68
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return;

fleur's avatar
fleur committed
69
70
71
72
73
74
75
	leds_prep_hsv(led, h, s, v);
}

void epic_leds_set_all(uint8_t *pattern_ptr, uint8_t len)
{
	uint8_t(*pattern)[3] = (uint8_t(*)[3])pattern_ptr;
	for (int i = 0; i < len; i++) {
76
77
78
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
			continue;

fleur's avatar
fleur committed
79
80
		leds_prep(i, pattern[i][0], pattern[i][1], pattern[i][2]);
	}
81
	do_update();
fleur's avatar
fleur committed
82
83
84
85
86
87
}

void epic_leds_set_all_hsv(float *pattern_ptr, uint8_t len)
{
	float(*pattern)[3] = (float(*)[3])pattern_ptr;
	for (int i = 0; i < len; i++) {
88
89
90
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
			continue;

fleur's avatar
fleur committed
91
92
		leds_prep_hsv(i, pattern[i][0], pattern[i][1], pattern[i][2]);
	}
93
	do_update();
fleur's avatar
fleur committed
94
95
96
97
98
}

void epic_leds_dim_top(uint8_t value)
{
	leds_set_dim_top(value);
99
100
101
102
103
	if (personal_state_enabled() == 0) {
		while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
			vTaskDelay(pdMS_TO_TICKS(1));
		}

104
		leds_update();
105
106
107

		hwlock_release(HWLOCK_I2C);
	}
fleur's avatar
fleur committed
108
109
110
111
112
}

void epic_leds_dim_bottom(uint8_t value)
{
	leds_set_dim_bottom(value);
113
114
115
116
117
	if (personal_state_enabled() == 0) {
		while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
			vTaskDelay(pdMS_TO_TICKS(1));
		}

118
		leds_update();
119
120
121

		hwlock_release(HWLOCK_I2C);
	}
fleur's avatar
fleur committed
122
123
124
125
}

void epic_leds_set_rocket(int led, uint8_t value)
{
126
127
128
129
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

Rahix's avatar
Rahix committed
130
	value = value > 31 ? 31 : value;
fleur's avatar
fleur committed
131
	pmic_set_led(led, value);
132
133

	hwlock_release(HWLOCK_I2C);
fleur's avatar
fleur committed
134
135
}

136
137
138
139
140
141
142
143
144
145
146
147
int epic_leds_get_rocket(int led)
{
	int ret = 0;
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

	ret = pmic_get_led(led);
	hwlock_release(HWLOCK_I2C);
	return ret;
}

zenox's avatar
zenox committed
148
static TimerHandle_t blink_timer[] = {NULL, NULL ,NULL};
zenox's avatar
zenox committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

void bTimerCallback(TimerHandle_t blink_timer)
{
    uint32_t id = (uint32_t) pvTimerGetTimerID(blink_timer);
    epic_leds_set_rocket(id, 0);
}

void epic_leds_blink_rocket(int led, uint8_t value, int millis)
{
        int ticks = millis * (configTICK_RATE_HZ / 1000);
        int32_t id = led;
        if (blink_timer[id] == NULL) {
            blink_timer[id] = xTimerCreate(
                    "blinktimer",
                    ticks,
                    pdFALSE,
                    (void*) id,
                    bTimerCallback
            );
            epic_leds_set_rocket(led, value);
        }
zenox's avatar
zenox committed
170
171
        epic_leds_set_rocket(led, value);
        xTimerChangePeriod(blink_timer[id], ticks, 0);
zenox's avatar
zenox committed
172
}
fleur's avatar
fleur committed
173
174
void epic_set_flashlight(bool power)
{
175
176
177
178
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

fleur's avatar
fleur committed
179
	leds_flashlight(power);
180
181

	hwlock_release(HWLOCK_I2C);
fleur's avatar
fleur committed
182
183
184
185
}

void epic_leds_update(void)
{
186
	do_update();
fleur's avatar
fleur committed
187
188
189
190
}

void epic_leds_set_powersave(bool eco)
{
191
192
193
194
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

fleur's avatar
fleur committed
195
	leds_powersave(eco);
196
197

	hwlock_release(HWLOCK_I2C);
fleur's avatar
fleur committed
198
199
200
201
202
203
}

void epic_leds_set_gamma_table(uint8_t rgb_channel, uint8_t gamma_table[256])
{
	leds_set_gamma_table(rgb_channel, gamma_table);
}
204
205
206
207

void epic_leds_clear_all(uint8_t r, uint8_t g, uint8_t b)
{
	for (int i = 0; i < NUM_LEDS; i++) {
208
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
209
210
211
212
213
			continue;

		leds_prep(i, r, g, b);
	}

214
	do_update();
215
}