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

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

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

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

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

	leds_update_power();
	leds_update();

	hwlock_release(HWLOCK_LED);
24
25
}

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

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

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

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

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

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

52
53
54
55
56
57
58
59
60
61
62
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
63
64
void epic_leds_prep_hsv(int led, float h, float s, float v)
{
65
66
67
	if (led == PERSONAL_STATE_LED && personal_state_enabled())
		return;

fleur's avatar
fleur committed
68
69
70
71
72
73
74
	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++) {
75
76
77
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
			continue;

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

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++) {
87
88
89
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
			continue;

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

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

103
		leds_update();
104
105
106

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

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

117
		leds_update();
118
119
120

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

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

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

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

135
136
137
138
139
140
141
142
143
144
145
146
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;
}

fleur's avatar
fleur committed
147
148
void epic_set_flashlight(bool power)
{
149
150
151
152
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

fleur's avatar
fleur committed
153
	leds_flashlight(power);
154
155

	hwlock_release(HWLOCK_I2C);
fleur's avatar
fleur committed
156
157
158
159
}

void epic_leds_update(void)
{
160
	do_update();
fleur's avatar
fleur committed
161
162
163
164
}

void epic_leds_set_powersave(bool eco)
{
165
166
167
168
	while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) {
		vTaskDelay(pdMS_TO_TICKS(1));
	}

fleur's avatar
fleur committed
169
	leds_powersave(eco);
170
171

	hwlock_release(HWLOCK_I2C);
fleur's avatar
fleur committed
172
173
174
175
176
177
}

void epic_leds_set_gamma_table(uint8_t rgb_channel, uint8_t gamma_table[256])
{
	leds_set_gamma_table(rgb_channel, gamma_table);
}
178
179
180
181

void epic_leds_clear_all(uint8_t r, uint8_t g, uint8_t b)
{
	for (int i = 0; i < NUM_LEDS; i++) {
182
		if (i == PERSONAL_STATE_LED && personal_state_enabled())
183
184
185
186
187
			continue;

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

188
	do_update();
189
}