• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Generic GPIO led
3  *
4  * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 #include "private-lib-core.h"
25 
26 #include "drivers/led/private-lib-drivers-led.h"
27 
28 /*
29  * 64 entry interpolated CIE correction
30  * https://en.wikipedia.org/wiki/Lightness
31  */
32 
33 uint16_t cie[] = {
34 	0, 113, 227, 340, 454, 568, 688, 824, 976, 1146,
35 	1335, 1543, 1772, 2023, 2296, 2592, 2914, 3260, 3633, 4034,
36 	4463, 4921, 5409, 5929, 6482, 7067, 7687, 8341, 9032, 9761,
37 	10527, 11332, 12178, 13064, 13993, 14964, 15980, 17040, 18146, 19299,
38 	20500, 21750, 23049, 24400, 25802, 27256, 28765, 30328, 31946, 33622,
39 	35354, 37146, 38996, 40908, 42881, 44916, 47014, 49177, 51406, 53700,
40 	56062, 58492, 60992, 63561,
41 	65535 /* for interpolation */
42 };
43 
44 /*
45  * This is the default intensity correction function, it can be overridden
46  * per-led to eg, normalize intensity of different leds
47  */
48 
49 static lws_led_intensity_t
cie_antilog(lws_led_intensity_t lin)50 cie_antilog(lws_led_intensity_t lin)
51 {
52         return (cie[lin >> 10]       * (0x3ff - (lin & 0x3ff)) +
53         	cie[(lin >> 10) + 1] * (lin & 0x3ff)) / 0x3ff;
54 }
55 
56 static void
lws_seq_advance(lws_led_state_t * lcs,lws_led_state_ch_t * ch)57 lws_seq_advance(lws_led_state_t *lcs, lws_led_state_ch_t *ch)
58 {
59 	if (!ch->seq)
60 		return;
61 
62 	if (ch->phase_budget != LWS_SEQ_LEDPHASE_TOTAL_ENDLESS &&
63 	    (ch->phase_budget < ch->step || !ch->phase_budget)) {
64 
65 		/* we are done */
66 
67 		ch->seq = NULL;
68 		if (!(--lcs->timer_refcount)) {
69 #if defined(LWS_PLAT_TIMER_STOP)
70 			LWS_PLAT_TIMER_STOP(lcs->timer);
71 #endif
72 		}
73 
74 		return;
75 	}
76 
77 	ch->ph += ch->step;
78 	if (ch->phase_budget != LWS_SEQ_LEDPHASE_TOTAL_ENDLESS)
79 		ch->phase_budget -= ch->step;
80 }
81 
82 static lws_led_intensity_t
lws_seq_sample(const lws_led_gpio_map_t * map,lws_led_state_chs_t * chs)83 lws_seq_sample(const lws_led_gpio_map_t *map, lws_led_state_chs_t *chs)
84 {
85 	unsigned int i;
86 
87 	if (chs->seqs[LLSI_CURR].seq)
88 		chs->seqs[LLSI_CURR].last = chs->seqs[LLSI_CURR].seq->
89 						func(chs->seqs[LLSI_CURR].ph);
90 
91 	if (chs->seqs[LLSI_TRANS].seq) {
92 		/*
93 		 * If a transition is ongoing, we need to use the transition
94 		 * intensity as the mixing factor between the still-live current
95 		 * and newly-live next sequences
96 		 */
97 		chs->seqs[LLSI_TRANS].last = chs->seqs[LLSI_TRANS].seq->
98 						func(chs->seqs[LLSI_TRANS].ph);
99 
100 		if (chs->seqs[LLSI_NEXT].seq)
101 			chs->seqs[LLSI_NEXT].last = chs->seqs[LLSI_NEXT].seq->
102 						  func(chs->seqs[LLSI_NEXT].ph);
103 
104 		i = (lws_led_intensity_t)(((
105 				(unsigned int)chs->seqs[LLSI_CURR].last *
106 				(65535 - chs->seqs[LLSI_TRANS].last) >> 16) +
107 			(((unsigned int)chs->seqs[LLSI_NEXT].last *
108 			 (unsigned int)chs->seqs[LLSI_TRANS].last) >> 16)));
109 	} else
110 		i = chs->seqs[LLSI_CURR].last;
111 
112 	return map->intensity_correction ? map->intensity_correction(i) :
113 					   cie_antilog((lws_led_intensity_t)i);
114 }
115 
116 void
lws_seq_timer_handle(lws_led_state_t * lcs)117 lws_seq_timer_handle(lws_led_state_t *lcs)
118 {
119 	lws_led_gpio_controller_t *lgc = lcs->controller;
120 	lws_led_state_chs_t *chs = (lws_led_state_chs_t *)&lcs[1];
121 	const lws_led_gpio_map_t *map = &lgc->led_map[0];
122 	unsigned int n;
123 
124 	for (n = 0; n < lgc->count_leds; n++) {
125 
126 		lgc->led_ops.intensity(&lgc->led_ops, map->name,
127 				       lws_seq_sample(map, chs));
128 
129 		lws_seq_advance(lcs, &chs->seqs[LLSI_CURR]);
130 
131 		if (chs->seqs[LLSI_TRANS].seq) {
132 			lws_seq_advance(lcs, &chs->seqs[LLSI_NEXT]);
133 			lws_seq_advance(lcs, &chs->seqs[LLSI_TRANS]);
134 
135 			/*
136 			 * When we finished the transition, we can make the
137 			 * "next" sequence the current sequence and no need for
138 			 * a "next" or a transition any more.
139 			 */
140 
141 			if (!chs->seqs[LLSI_TRANS].seq) {
142 				chs->seqs[LLSI_CURR] = chs->seqs[LLSI_NEXT];
143 				chs->seqs[LLSI_NEXT].seq = NULL;
144 			}
145 		}
146 
147 		map++;
148 		chs++;
149 	}
150 }
151 
152 static int
lws_led_set_chs_seq(struct lws_led_state * lcs,lws_led_state_ch_t * dest,const lws_led_sequence_def_t * def)153 lws_led_set_chs_seq(struct lws_led_state *lcs, lws_led_state_ch_t *dest,
154 		    const lws_led_sequence_def_t *def)
155 {
156 	int steps;
157 
158 	dest->seq = def;
159 	dest->ph = def->ledphase_offset;
160 	dest->phase_budget = def->ledphase_total;
161 
162 	/*
163 	 * We need to compute the incremental phase angle step to cover the
164 	 * total number of phases in the indicated ms, incrementing at the
165 	 * timer rate of LWS_LED_SEQUENCER_UPDATE_RATE_HZ.  Eg,
166 	 *
167 	 * 65536 phase steps (one cycle) in 2000ms at 30Hz timer rate means we
168 	 * will update 2000ms / 33ms = 60 times, so we must step at at
169 	 * 65536 / 60 = 1092 phase angle resolution
170 	 */
171 
172 	steps = def->ms / LWS_LED_SEQUENCER_UPDATE_INTERVAL_MS;
173 	dest->step = (def->ledphase_total != LWS_SEQ_LEDPHASE_TOTAL_ENDLESS ?
174 		def->ledphase_total : LWS_LED_FUNC_PHASE) / (steps ? steps : 1);
175 
176 	if (!lcs->timer_refcount++) {
177 #if defined(LWS_PLAT_TIMER_START)
178 		LWS_PLAT_TIMER_START(lcs->timer);
179 #endif
180 	}
181 
182 	return steps;
183 }
184 
185 int
lws_led_transition(struct lws_led_state * lcs,const char * name,const lws_led_sequence_def_t * next,const lws_led_sequence_def_t * trans)186 lws_led_transition(struct lws_led_state *lcs, const char *name,
187 		   const lws_led_sequence_def_t *next,
188 		   const lws_led_sequence_def_t *trans)
189 {
190 	lws_led_state_chs_t *chs = (lws_led_state_chs_t *)&lcs[1];
191 	int index = lws_led_gpio_lookup(&lcs->controller->led_ops, name);
192 
193 	if (index < 0)
194 		return 1;
195 
196 	lws_led_set_chs_seq(lcs, &chs[index].seqs[LLSI_TRANS], trans);
197 	lws_led_set_chs_seq(lcs, &chs[index].seqs[LLSI_NEXT], next);
198 
199 	return 0;
200 }
201