1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * Generic GPIO led
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci#include "private-lib-core.h"
25d4afb5ceSopenharmony_ci
26d4afb5ceSopenharmony_cistatic const lws_led_intensity_t sineq16[] = {
27d4afb5ceSopenharmony_ci
28d4afb5ceSopenharmony_ci	/*
29d4afb5ceSopenharmony_ci	 * Quadrant at sin(270) in 16 samples, normalized so
30d4afb5ceSopenharmony_ci	 * -1 == 0 and 0 == 32767
31d4afb5ceSopenharmony_ci	 */
32d4afb5ceSopenharmony_ci
33d4afb5ceSopenharmony_ci	    0,   158,   630,  1411,  2494,  3869,  5522,  7437,
34d4afb5ceSopenharmony_ci	 9597, 11980, 14562, 17321, 20228, 23225, 26374, 29555,
35d4afb5ceSopenharmony_ci	32767 /* to interpolate against */
36d4afb5ceSopenharmony_ci};
37d4afb5ceSopenharmony_ci
38d4afb5ceSopenharmony_ci/*
39d4afb5ceSopenharmony_ci * Elaborate the 90 degree phase table to 360 degrees and offset to +32768,
40d4afb5ceSopenharmony_ci * notice for the last sample we have to interpolate against a 17th sample
41d4afb5ceSopenharmony_ci * reflecting full scale to avoid clipping due to interpolation against the
42d4afb5ceSopenharmony_ci * 16th sample again
43d4afb5ceSopenharmony_ci */
44d4afb5ceSopenharmony_ci
45d4afb5ceSopenharmony_cistatic lws_led_intensity_t
46d4afb5ceSopenharmony_cisine_lu(int n, int next)
47d4afb5ceSopenharmony_ci{
48d4afb5ceSopenharmony_ci        switch ((n >> 4) & 3) {
49d4afb5ceSopenharmony_ci        case 1:
50d4afb5ceSopenharmony_ci        	/* forwards */
51d4afb5ceSopenharmony_ci                return 32768 + sineq16[(n & 15) + next];
52d4afb5ceSopenharmony_ci        case 2:
53d4afb5ceSopenharmony_ci        	/* scan it backwards */
54d4afb5ceSopenharmony_ci                return 32768 + sineq16[15 - (n & 15) + (!next)];
55d4afb5ceSopenharmony_ci        case 3:
56d4afb5ceSopenharmony_ci        	/* forwards */
57d4afb5ceSopenharmony_ci                return 32768 - sineq16[(n & 15) + next];
58d4afb5ceSopenharmony_ci        default:
59d4afb5ceSopenharmony_ci        	/* scan it backwards */
60d4afb5ceSopenharmony_ci                return 32768 - sineq16[15 - (n & 15) + (!next)];
61d4afb5ceSopenharmony_ci        }
62d4afb5ceSopenharmony_ci}
63d4afb5ceSopenharmony_ci
64d4afb5ceSopenharmony_ci/*
65d4afb5ceSopenharmony_ci * The normalized phase resolution is 16-bit, however much table you decide to
66d4afb5ceSopenharmony_ci * have needs interpolating or indexing in a reduced number of significant
67d4afb5ceSopenharmony_ci * phase bits if it doesn't have the same phase resolution.
68d4afb5ceSopenharmony_ci *
69d4afb5ceSopenharmony_ci * In this sine table we have a 16 x 15-bit sample quadrant reflected 4 times
70d4afb5ceSopenharmony_ci * to make 360 degrees, so 64 accurate sample points, with the rest of the
71d4afb5ceSopenharmony_ci * intermediate phases generated by linear interpolation.  That probably would
72d4afb5ceSopenharmony_ci * sound a bit funky, but for modulating light dynamically it's more than
73d4afb5ceSopenharmony_ci * enough.
74d4afb5ceSopenharmony_ci */
75d4afb5ceSopenharmony_ci
76d4afb5ceSopenharmony_cilws_led_intensity_t
77d4afb5ceSopenharmony_cilws_led_func_sine(lws_led_seq_phase_t n)
78d4afb5ceSopenharmony_ci{
79d4afb5ceSopenharmony_ci        /*
80d4afb5ceSopenharmony_ci         * 2: quadrant
81d4afb5ceSopenharmony_ci         * 4: table entry in quadrant
82d4afb5ceSopenharmony_ci         * 10: interp (LSB)
83d4afb5ceSopenharmony_ci         */
84d4afb5ceSopenharmony_ci
85d4afb5ceSopenharmony_ci        return (sine_lu(n >> 10, 0) * (0x3ff - (n & 0x3ff)) +
86d4afb5ceSopenharmony_ci        	sine_lu(n >> 10, 1) * (n & 0x3ff)) / 0x3ff;
87d4afb5ceSopenharmony_ci}
88d4afb5ceSopenharmony_ci
89d4afb5ceSopenharmony_cilws_led_intensity_t
90d4afb5ceSopenharmony_cilws_led_func_linear(lws_led_seq_phase_t n)
91d4afb5ceSopenharmony_ci{
92d4afb5ceSopenharmony_ci	return (lws_led_intensity_t)n;
93d4afb5ceSopenharmony_ci}
94d4afb5ceSopenharmony_ci
95d4afb5ceSopenharmony_ci
96d4afb5ceSopenharmony_cistatic lws_led_intensity_t
97d4afb5ceSopenharmony_cilws_led_func_static(lws_led_seq_phase_t n)
98d4afb5ceSopenharmony_ci{
99d4afb5ceSopenharmony_ci	return ((int)n * LWS_LED_MAX_INTENSITY) / 2;
100d4afb5ceSopenharmony_ci}
101d4afb5ceSopenharmony_ci
102d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_static_off = {
103d4afb5ceSopenharmony_ci	.func			= lws_led_func_static,
104d4afb5ceSopenharmony_ci	.ledphase_offset	= 0,
105d4afb5ceSopenharmony_ci	.ledphase_total		= 0,
106d4afb5ceSopenharmony_ci	.ms			= 0
107d4afb5ceSopenharmony_ci};
108d4afb5ceSopenharmony_ci
109d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_static_half = {
110d4afb5ceSopenharmony_ci	.func			= lws_led_func_static,
111d4afb5ceSopenharmony_ci	.ledphase_offset	= 1,
112d4afb5ceSopenharmony_ci	.ledphase_total		= 0,
113d4afb5ceSopenharmony_ci	.ms			= 0
114d4afb5ceSopenharmony_ci};
115d4afb5ceSopenharmony_ci
116d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_static_on = {
117d4afb5ceSopenharmony_ci	.func			= lws_led_func_static,
118d4afb5ceSopenharmony_ci	.ledphase_offset	= 2,
119d4afb5ceSopenharmony_ci	.ledphase_total		= 0,
120d4afb5ceSopenharmony_ci	.ms			= 0
121d4afb5ceSopenharmony_ci};
122d4afb5ceSopenharmony_ci
123d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_sine_up = {
124d4afb5ceSopenharmony_ci	.func			= lws_led_func_sine,
125d4afb5ceSopenharmony_ci	.ledphase_offset	= 0, /* already at 0 amp at 0 phase */
126d4afb5ceSopenharmony_ci	.ledphase_total		= LWS_LED_FUNC_PHASE / 2, /* 180 degree ./^ */
127d4afb5ceSopenharmony_ci	.ms			= 300
128d4afb5ceSopenharmony_ci};
129d4afb5ceSopenharmony_ci
130d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_sine_down = {
131d4afb5ceSopenharmony_ci	.func			= lws_led_func_sine,
132d4afb5ceSopenharmony_ci	.ledphase_offset	= LWS_LED_FUNC_PHASE / 2, /* start at peak */
133d4afb5ceSopenharmony_ci	.ledphase_total		= LWS_LED_FUNC_PHASE / 2, /* 180 degree ./^ */
134d4afb5ceSopenharmony_ci	.ms			= 300
135d4afb5ceSopenharmony_ci};
136d4afb5ceSopenharmony_ci
137d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_linear_wipe = {
138d4afb5ceSopenharmony_ci	.func			= lws_led_func_linear,
139d4afb5ceSopenharmony_ci	.ledphase_offset	= 0,
140d4afb5ceSopenharmony_ci	.ledphase_total		= LWS_LED_FUNC_PHASE - 1,
141d4afb5ceSopenharmony_ci	.ms			= 300
142d4afb5ceSopenharmony_ci};
143d4afb5ceSopenharmony_ci
144d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_sine_endless_slow = {
145d4afb5ceSopenharmony_ci	.func			= lws_led_func_sine,
146d4afb5ceSopenharmony_ci	.ledphase_offset	= 0, /* already at 0 amp at 0 phase */
147d4afb5ceSopenharmony_ci	.ledphase_total		= LWS_SEQ_LEDPHASE_TOTAL_ENDLESS,
148d4afb5ceSopenharmony_ci	.ms			= 1500
149d4afb5ceSopenharmony_ci};
150d4afb5ceSopenharmony_ci
151d4afb5ceSopenharmony_ciconst lws_led_sequence_def_t lws_pwmseq_sine_endless_fast = {
152d4afb5ceSopenharmony_ci	.func			= lws_led_func_sine,
153d4afb5ceSopenharmony_ci	.ledphase_offset	= 0, /* already at 0 amp at 0 phase */
154d4afb5ceSopenharmony_ci	.ledphase_total		= LWS_SEQ_LEDPHASE_TOTAL_ENDLESS,
155d4afb5ceSopenharmony_ci	.ms			= 750
156d4afb5ceSopenharmony_ci};
157