leds.c 7.07 KB
Newer Older
schneider's avatar
schneider committed
1
#include "gpio.h"
2
#include "portexpander.h"
fleur's avatar
fleur committed
3
#include "max32665.h"
schneider's avatar
schneider committed
4
5
#include <stdint.h>
#include <string.h>
fleur's avatar
fleur committed
6
7
#include <stdbool.h>
#include <stdio.h>
Rahix's avatar
Rahix committed
8
#define NUM_LEDS 15
fleur's avatar
fleur committed
9
10
11
#define DEFAULT_DIM_TOP 1
#define DEFAULT_DIM_BOTTOM 8
#define MAX_DIM 8
Rahix's avatar
Rahix committed
12
13
14
15
16
17
18

static const gpio_cfg_t rgb_dat_pin = {
	PORT_1, PIN_14, GPIO_FUNC_OUT, GPIO_PAD_NONE
};
static const gpio_cfg_t rgb_clk_pin = {
	PORT_1, PIN_15, GPIO_FUNC_OUT, GPIO_PAD_NONE
};
fleur's avatar
fleur committed
19
20
21
22
23
24
25
static uint8_t leds[NUM_LEDS][3];
static uint8_t gamma_table[3][256];
static uint8_t active_groups;
static uint8_t bottom_dim; //index 11-14
static uint8_t top_dim;    //index 0-10
static bool powersave;
static long powerup_wait_cycles = 500;
schneider's avatar
schneider committed
26
27
28
29
30

/***** Functions *****/
// *****************************************************************************

typedef struct {
Rahix's avatar
Rahix committed
31
32
33
	float r; // a fraction between 0 and 1
	float g; // a fraction between 0 and 1
	float b; // a fraction between 0 and 1
schneider's avatar
schneider committed
34
35
36
} rgb;

typedef struct {
Rahix's avatar
Rahix committed
37
38
39
	float h; // angle in degrees
	float s; // a fraction between 0 and 1
	float v; // a fraction between 0 and 1
schneider's avatar
schneider committed
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
} hsv;

#if 0
static hsv rgb2hsv(rgb in)
{
    hsv         out;
    float      min, max, delta;

    min = in.r < in.g ? in.r : in.g;
    min = min  < in.b ? min  : in.b;

    max = in.r > in.g ? in.r : in.g;
    max = max  > in.b ? max  : in.b;

    out.v = max;                                // v
    delta = max - min;
    if (delta < 0.00001)
    {
        out.s = 0;
        out.h = 0; // undefined, maybe nan?
        return out;
    }
    if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
        out.s = (delta / max);                  // s
    } else {
        // if max is 0, then r = g = b = 0
        // s = 0, h is undefined
        out.s = 0.0;
        out.h = 0.0;                            // its now undefined
        return out;
    }
    if( in.r >= max )                           // > is bogus, just keeps compilor happy
        out.h = ( in.g - in.b ) / delta;        // between yellow & magenta
    else
    if( in.g >= max )
        out.h = 2.0 + ( in.b - in.r ) / delta;  // between cyan & yellow
    else
        out.h = 4.0 + ( in.r - in.g ) / delta;  // between magenta & cyan

    out.h *= 60.0;                              // degrees

    if( out.h < 0.0 )
        out.h += 360.0;

    return out;
}
#endif

static rgb hsv2rgb(hsv in)
{
Rahix's avatar
Rahix committed
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
	float hh, p, q, t, ff;
	long i;
	rgb out;

	if (in.s <= 0.0) { // < is bogus, just shuts up warnings
		out.r = in.v;
		out.g = in.v;
		out.b = in.v;
		return out;
	}
	hh = in.h;
	if (hh >= 360.0)
		hh = 0.0;
	hh /= 60.0;
	i  = (long)hh;
	ff = hh - i;
	p  = in.v * (1.0 - in.s);
	q  = in.v * (1.0 - (in.s * ff));
	t  = in.v * (1.0 - (in.s * (1.0 - ff)));

	switch (i) {
	case 0:
		out.r = in.v;
		out.g = t;
		out.b = p;
		break;
	case 1:
		out.r = q;
		out.g = in.v;
		out.b = p;
		break;
	case 2:
		out.r = p;
		out.g = in.v;
		out.b = t;
		break;

	case 3:
		out.r = p;
		out.g = q;
		out.b = in.v;
		break;
	case 4:
		out.r = t;
		out.g = p;
		out.b = in.v;
		break;
	case 5:
	default:
		out.r = in.v;
		out.g = p;
		out.b = q;
		break;
	}
	return out;
schneider's avatar
schneider committed
145
146
147
148
}

static void shift(uint8_t data)
{
Rahix's avatar
Rahix committed
149
150
151
152
153
154
155
156
157
158
159
160
	for (int i = 0; i < 8; i++) {
		if (data & 0x80) {
			GPIO_OutSet(&rgb_dat_pin);
		} else {
			GPIO_OutClr(&rgb_dat_pin);
		}
		GPIO_OutClr(&rgb_clk_pin);
		//TMR_Delay(MXC_TMR0, MSEC(1), 0);
		GPIO_OutSet(&rgb_clk_pin);
		//TMR_Delay(MXC_TMR0, MSEC(1), 0);
		data <<= 1;
	}
schneider's avatar
schneider committed
161
162
163
164
}

static void leds_start(void)
{
Rahix's avatar
Rahix committed
165
166
167
168
	shift(0x00);
	shift(0x00);
	shift(0x00);
	shift(0x00);
schneider's avatar
schneider committed
169
170
171
172
}

static void leds_shift(uint8_t r, uint8_t g, uint8_t b, uint8_t dim)
{
Rahix's avatar
Rahix committed
173
174
175
176
	shift(0xE0 | (dim & 0x1F));
	shift(b);
	shift(g);
	shift(r);
schneider's avatar
schneider committed
177
178
179
180
}

static void leds_stop(void)
{
Rahix's avatar
Rahix committed
181
182
183
184
	shift(0xFF);
	shift(0xFF);
	shift(0xFF);
	shift(0xFF);
schneider's avatar
schneider committed
185
186
}

fleur's avatar
fleur committed
187
188
189
190
191
192
193
194
195
196
197
static uint8_t led_to_dim_value(uint8_t led)
{
	return (led < 11) ? top_dim : bottom_dim;
}

void leds_set_dim_top(uint8_t value)
{
	top_dim = (value > MAX_DIM) ? MAX_DIM : value;
}

void leds_set_dim_bottom(uint8_t value)
schneider's avatar
schneider committed
198
{
fleur's avatar
fleur committed
199
	bottom_dim = (value > MAX_DIM) ? MAX_DIM : value;
schneider's avatar
schneider committed
200
201
}

fleur's avatar
fleur committed
202
void leds_prep(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
schneider's avatar
schneider committed
203
{
Rahix's avatar
Rahix committed
204
205
206
	leds[led][0] = r;
	leds[led][1] = g;
	leds[led][2] = b;
schneider's avatar
schneider committed
207
208
}

fleur's avatar
fleur committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#if 0
//don't use, is buggy
void leds_set_autodim(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
    if(led==NUM_LEDS){
        leds_set(led,r,g,b);
        return;
    }
    leds[led][3] = max(r,max(g,b));
    float gain = (float)255/leds[led][3]; //might cause rounding->overflow errors might debug later idk~
    leds[led][0] = (uint8_t)(r*gain);
    leds[led][1] = (uint8_t)(g*gain);
    leds[led][2] = (uint8_t)(b*gain);
}
#endif

void leds_prep_hsv(uint8_t led, float h, float s, float v)
schneider's avatar
schneider committed
226
{
Rahix's avatar
Rahix committed
227
228
229
230
231
	hsv in       = { h, s, v };
	rgb out      = hsv2rgb(in);
	leds[led][0] = out.r * 255;
	leds[led][1] = out.g * 255;
	leds[led][2] = out.b * 255;
schneider's avatar
schneider committed
232
233
}

fleur's avatar
fleur committed
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
static bool is_led_on(uint8_t led) // scheduled to be on after next update
{
	if (!led_to_dim_value(led)) {
		return false;
	}
	for (int i = 0; i < 3; i++) {
		if (leds[led][i] != 0) {
			return true;
		}
	}
	return false;
}

static uint8_t led_to_group(uint8_t led)
{
	if (led == 14) {
		return 1;
	} else if (led >= 11) {
		return 2;
	}
	return 3;
}

static uint8_t
check_privilege(void) //returns number of hierarchical groups with power
{
	for (int i = 0; i < NUM_LEDS; i++) {
		if (is_led_on(i)) {
			return led_to_group(i);
		}
	}
	return 0;
}

static uint8_t power_pin_conversion(uint8_t group)
{
	if (group == 2) {
		return 1;
	}
	if (group == 1) {
		return 2;
	}
	return 0;
}

static void power_all(void)
{
	for (int i = 0; i < 3; i++) {
		portexpander_prep(i, 0);
	}
	portexpander_update();
}

void leds_update_power(void)
{
	if (!powersave) {
		return;
	}
	uint8_t new_groups =
		check_privilege(); //there must be a prettier way to do this but meh
	if (new_groups == active_groups) {
		return;
	}
	for (int i = 0; i < 3; i++) {
		if (i < new_groups) {
			portexpander_prep(power_pin_conversion(i), 0);
		} else {
			portexpander_prep(power_pin_conversion(i), 1);
		}
	}
	portexpander_update();
	if (active_groups < new_groups) {
		for (int i = 0; i < powerup_wait_cycles; i++) {
			__NOP();
		}
	}
	active_groups = new_groups;
}

void leds_powersave(bool eco)
{
	powersave = eco;
	if (!powersave) {
		power_all();
	} else {
		leds_update_power();
	}
}

schneider's avatar
schneider committed
323
324
void leds_update(void)
{
Rahix's avatar
Rahix committed
325
326
	leds_start();
	for (int i = NUM_LEDS - 1; i >= 0; i--) {
fleur's avatar
fleur committed
327
328
329
330
331
332
		leds_shift(
			gamma_table[0][leds[i][0]],
			gamma_table[1][leds[i][1]],
			gamma_table[2][leds[i][2]],
			led_to_dim_value(i)
		);
Rahix's avatar
Rahix committed
333
334
	}
	leds_stop();
schneider's avatar
schneider committed
335
336
}

fleur's avatar
fleur committed
337
338
339
340
341
342
343
344
345
346
347
348
void leds_flashlight(bool power)
{
	portexpander_set(7, (power) ? 0 : 1);
}

void leds_set_gamma_table(uint8_t rgb_channel, uint8_t table[256])
{
	for (int i = 0; i < 256; i++) {
		gamma_table[rgb_channel][i] = table[i];
	}
}

schneider's avatar
schneider committed
349
350
void leds_init(void)
{
Rahix's avatar
Rahix committed
351
352
	GPIO_Config(&rgb_clk_pin);
	GPIO_Config(&rgb_dat_pin);
schneider's avatar
schneider committed
353

Rahix's avatar
Rahix committed
354
355
	GPIO_OutSet(&rgb_clk_pin);
	GPIO_OutClr(&rgb_dat_pin);
schneider's avatar
schneider committed
356

Rahix's avatar
Rahix committed
357
	memset(leds, 0, sizeof(leds));
358

fleur's avatar
fleur committed
359
360
361
	powersave  = TRUE;
	top_dim    = DEFAULT_DIM_TOP;
	bottom_dim = DEFAULT_DIM_BOTTOM;
Rahix's avatar
Rahix committed
362
	for (int i = 0; i < NUM_LEDS; i++) {
fleur's avatar
fleur committed
363
364
365
		for (int j = 0; j < 3; j++) {
			leds[i][j] = 0;
		}
Rahix's avatar
Rahix committed
366
	}
fleur's avatar
fleur committed
367
368
369
370
371
	for (int i = 0; i < 256; i++) {
		for (int j = 0; j < 3; j++) {
			int k             = (i * (1 + i) + 255) >> 8;
			gamma_table[j][i] = (k * (k + 1) + 255) >> 8;
		}
Rahix's avatar
Rahix committed
372
373
	}
	leds_update();
schneider's avatar
schneider committed
374
}