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 #include "drivers/led/private-lib-drivers-led.h"
26
27 #if defined(LWS_PLAT_TIMER_CB)
LWS_PLAT_TIMER_CB(lws_led_timer_cb,th)28 static LWS_PLAT_TIMER_CB(lws_led_timer_cb, th)
29 {
30 lws_led_state_t *lcs = LWS_PLAT_TIMER_CB_GET_OPAQUE(th);
31
32 lws_seq_timer_handle(lcs);
33 }
34 #endif
35
36 struct lws_led_state *
lws_led_gpio_create(const lws_led_ops_t * led_ops)37 lws_led_gpio_create(const lws_led_ops_t *led_ops)
38 {
39 lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)led_ops;
40 /*
41 * We allocate the main state object, and a 3 x seq dynamic footprint
42 * for each led, since it may be sequencing the transition between two
43 * other sequences.
44 */
45
46 lws_led_state_t *lcs = lws_zalloc(sizeof(lws_led_state_t) +
47 (lgc->count_leds * sizeof(lws_led_state_chs_t)),
48 __func__);
49 int n;
50
51 if (!lcs)
52 return NULL;
53
54 lcs->controller = lgc;
55
56 #if defined(LWS_PLAT_TIMER_CREATE)
57 lcs->timer = LWS_PLAT_TIMER_CREATE("leds",
58 LWS_LED_SEQUENCER_UPDATE_INTERVAL_MS, 1, lcs,
59 (TimerCallbackFunction_t)lws_led_timer_cb);
60 if (!lcs->timer)
61 return NULL;
62 #endif
63
64 for (n = 0; n < lgc->count_leds; n++) {
65 const lws_led_gpio_map_t *map = &lgc->led_map[n];
66
67 if (map->pwm_ops) {
68 lgc->gpio_ops->mode(map->gpio, LWSGGPIO_FL_READ);
69 lgc->gpio_ops->set(map->gpio, 0);
70 } else {
71 lgc->gpio_ops->mode(map->gpio, LWSGGPIO_FL_WRITE);
72 lgc->gpio_ops->set(map->gpio,
73 !lgc->led_map[n].active_level);
74 }
75 }
76
77 return lcs;
78 }
79
80 void
lws_led_gpio_destroy(struct lws_led_state * lcs)81 lws_led_gpio_destroy(struct lws_led_state *lcs)
82 {
83 #if defined(LWS_PLAT_TIMER_DELETE)
84 LWS_PLAT_TIMER_DELETE(lcs->timer);
85 #endif
86 lws_free(lcs);
87 }
88
89 int
lws_led_gpio_lookup(const struct lws_led_ops * lo,const char * name)90 lws_led_gpio_lookup(const struct lws_led_ops *lo, const char *name)
91 {
92 const lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)lo;
93 int n;
94
95 for (n = 0; n < lgc->count_leds; n++)
96 if (!strcmp(name, lgc->led_map[n].name))
97 return n;
98
99 return -1;
100 }
101
102 void
lws_led_gpio_intensity(const struct lws_led_ops * lo,const char * name,lws_led_intensity_t inten)103 lws_led_gpio_intensity(const struct lws_led_ops *lo, const char *name,
104 lws_led_intensity_t inten)
105 {
106 const lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)lo;
107 int idx = lws_led_gpio_lookup(lo, name);
108 const lws_led_gpio_map_t *map;
109
110 if (idx < 0)
111 return;
112
113 map = &lgc->led_map[idx];
114
115 if (map->pwm_ops)
116 map->pwm_ops->intensity(map->pwm_ops, map->gpio, inten);
117 else
118 lgc->gpio_ops->set(map->gpio,
119 (!!map->active_level) ^ !(inten & 0x8000));
120 }
121