| Nordic Thingy:52 v2.0.0

drv_ext_light.h
1 /*
2 Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form, except as embedded into a Nordic
12 Semiconductor ASA integrated circuit in a product or a software update for
13 such product, must reproduce the above copyright notice, this list of
14 conditions and the following disclaimer in the documentation and/or other
15 materials provided with the distribution.
16
17 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18 contributors may be used to endorse or promote products derived from this
19 software without specific prior written permission.
20
21 4. This software, with or without modification, must only be used with a
22 Nordic Semiconductor ASA integrated circuit.
23
24 5. Any software provided in binary form under this license must not be reverse
25 engineered, decompiled, modified and/or disassembled.
26
27 THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
28 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
33 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
48 #ifndef __DRV_EXT_LIGHT_H__
49 #define __DRV_EXT_LIGHT_H__
50
51 #include <stdint.h>
52 #include "app_error.h"
53 #include "drv_sx1509.h"
54 #include "sx150x_led_drv_regs.h"
55 #include "sx150x_led_drv_calc.h"
56 #include "app_timer.h"
57
58 #define DRV_EXT_LIGHT_INTENSITY_MAX ((uint8_t)(0xFF))
59 #define DRV_EXT_LIGHT_INTENSITY_OFF (0)
60
61 #define DRV_EXT_LIGHT_MAX_PIN_ID_ALLOWED 16
62 #define DRV_EXT_LIGHT_NUM_LIGHTS_MAX (((DRV_SX1509_HIGHINPMODE_PIN15_Pos + 1) < (16)) ? \
63 (DRV_SX1509_HIGHINPMODE_PIN15_Pos + 1) : (16))
64 #define DRV_EXT_LIGHT_IOEXT_CLKX_DIV_MAX (DRV_SX1509_MISC_CLKX_Msk >> DRV_SX1509_MISC_CLKX_Pos)
65 #define DRV_EXT_LIGHT_INVALID_RESYNC_PIN 255
66
68 enum
69 {
70 DRV_EXT_LIGHT_STATUS_CODE_SUCCESS,
71 DRV_EXT_LIGHT_STATUS_CODE_INVALID_PARAM,
72 DRV_EXT_LIGHT_STATUS_CODE_NOT_SUPPORTED,
73 DRV_EXT_LIGHT_STATUS_CODE_INVALID_LIGHT_TYPE,
74 DRV_EXT_LIGHT_STATUS_CODE_INVALID_TIMER_VALUE
75 };
76
80 typedef enum
81 {
82 DRV_EXT_LIGHT_CLKX_DIV_1 = 1,
83 DRV_EXT_LIGHT_CLKX_DIV_2 ,
84 DRV_EXT_LIGHT_CLKX_DIV_4 ,
85 DRV_EXT_LIGHT_CLKX_DIV_8 ,
86 DRV_EXT_LIGHT_CLKX_DIV_16,
87 DRV_EXT_LIGHT_CLKX_DIV_32,
88 DRV_EXT_LIGHT_CLKX_DIV_64,
89 }drv_ext_light_clkx_div_t;
90
93 typedef enum
94 {
95 DRV_EXT_LIGHT_COLOR_NONE,
96 DRV_EXT_LIGHT_COLOR_RED,
97 DRV_EXT_LIGHT_COLOR_GREEN,
98 DRV_EXT_LIGHT_COLOR_YELLOW,
99 DRV_EXT_LIGHT_COLOR_BLUE,
100 DRV_EXT_LIGHT_COLOR_PURPLE,
101 DRV_EXT_LIGHT_COLOR_CYAN,
102 DRV_EXT_LIGHT_COLOR_WHITE
103 }drv_ext_light_color_mix_t;
104
107 typedef enum
108 {
109 EXTENDER_OSC_UNUSED,
110 EXTENDER_OSC_USED_RUNNING,
111 EXTENDER_OSC_USED_PAUSED,
112 EXTENDER_OSC_USED_PERM,
113 IOEXT_OSC_STATUS_T_SIZE
114 }drv_ext_light_ioext_osc_status_t;
115
118 typedef struct
119 {
120 uint32_t active_time_ms;
121 uint32_t inactive_time_ms;
122 drv_ext_light_color_mix_t colors;
123 drv_ext_light_ioext_osc_status_t ioext_osc_status;
124 }drv_ext_light_status_t;
125
128 #define DRV_EXT_LIGHT_STATUS_INIT \
129 { \
130 .active_time_ms = 0, \
131 .inactive_time_ms = 0, \
132 .colors = DRV_EXT_LIGHT_COLOR_NONE, \
133 .ioext_osc_status = EXTENDER_OSC_UNUSED \
134 }
135
138 typedef struct
139 {
140 drv_ext_light_status_t * p_status;
141 app_timer_id_t timer;
142 }drv_ext_light_data_t;
143
146 #define DRV_EXT_LIGHT_DEF(light_id) \
147 static drv_ext_light_status_t light_id##_timings = DRV_EXT_LIGHT_STATUS_INIT; \
148 static app_timer_t light_id##_timer_data = { {0} }; \
149 static drv_ext_light_data_t light_id = {.p_status = &light_id##_timings, .timer = &light_id##_timer_data} \
150
151
153 typedef enum
154 {
155 DRV_EXT_LIGHT_TYPE_MONO,
156 DRV_EXT_LIGHT_TYPE_RGB
157 }drv_ext_light_reg_type_t;
158
161 typedef struct
162 {
163 drv_ext_light_reg_type_t type;
164 union
165 {
166 uint8_t mono;
167 struct
168 {
169 uint8_t r;
170 uint8_t g;
171 uint8_t b;
172 }rgb;
173 }pin;
174 drv_ext_light_data_t * p_data;
175 }drv_ext_light_conf_t;
176
179 typedef struct
180 {
181 drv_ext_light_conf_t const * p_light_conf;
182 uint8_t num_lights;
183 drv_ext_light_clkx_div_t clkx_div;
184 drv_sx1509_cfg_t const * p_twi_conf;
185 uint8_t resync_pin;
186 } drv_ext_light_init_t;
187
190 typedef struct
191 {
192 uint8_t r;
193 uint8_t g;
194 uint8_t b;
195 }drv_ext_light_rgb_intensity_t;
196
199 typedef struct
200 {
201 drv_ext_light_color_mix_t color;
202 drv_ext_light_sequence_t sequence_vals;
203 }drv_ext_light_rgb_sequence_t;
204
207 #define SEQUENCE_REAL_VAL_INIT(PARAM) drv_ext_light_sequence_t PARAM = SEQUENCE_DEFAULT_VAL
208
216 ret_code_t drv_ext_light_reset(void);
217
228 ret_code_t drv_ext_light_on(uint32_t id);
229
240 ret_code_t drv_ext_light_off(uint32_t id);
241
251 ret_code_t drv_ext_light_intensity_set(uint32_t id, uint8_t intensity);
252
262 ret_code_t drv_ext_light_rgb_intensity_set(uint32_t id, drv_ext_light_rgb_intensity_t const * const p_intensity);
263
281 ret_code_t drv_ext_light_sequence(uint32_t id, drv_ext_light_sequence_t * const p_sequence);
282
300 ret_code_t drv_ext_light_rgb_sequence(uint32_t id, drv_ext_light_rgb_sequence_t * const p_sequence);
301
316 ret_code_t drv_ext_light_init(drv_ext_light_init_t const * p_init, bool on_init_reset);
317
318 #endif
319
drv_ext_light_conf_t::mono
uint8_t mono
Monochrome pin.
Definition: drv_ext_light.h:166
drv_ext_light_conf_t::p_data
drv_ext_light_data_t * p_data
Associated data for this light (app timer and timings).
Definition: drv_ext_light.h:174
drv_ext_light_rgb_sequence_t
The color mix and sequence variables if a sequence (e.g. breathe) is used.
Definition: drv_ext_light.h:199
drv_ext_light_init_t::p_light_conf
drv_ext_light_conf_t const * p_light_conf
Array of lights, each with their specific configuraion.
Definition: drv_ext_light.h:181
drv_ext_light_init_t::p_twi_conf
drv_sx1509_cfg_t const * p_twi_conf
pointer to TWI (I2C) configuration for communication between master and IO extender.
Definition: drv_ext_light.h:184
drv_sx1509_cfg_t
SX1509 configuration.
Definition: drv_sx1509.h:70
DRV_EXT_LIGHT_STATUS_CODE_INVALID_LIGHT_TYPE
The type of light is not supported.
Definition: drv_ext_light.h:73
EXTENDER_OSC_UNUSED
Ext osc not used for this light.
Definition: drv_ext_light.h:109
drv_ext_light_clkx_div_t
drv_ext_light_clkx_div_t
Defines the frequency of the LED driver clock (ClkX) of the SX1509 according to ClkX = fOSC/2^(DRV_EX...
Definition: drv_ext_light.h:80
drv_ext_light_rgb_intensity_t::r
uint8_t r
Red intensity.
Definition: drv_ext_light.h:192
DRV_EXT_LIGHT_STATUS_CODE_NOT_SUPPORTED
Not supported.
Definition: drv_ext_light.h:72
drv_ext_light_rgb_sequence
ret_code_t drv_ext_light_rgb_sequence(uint32_t id, drv_ext_light_rgb_sequence_t *const p_sequence)
Function for setting color for a given light through the GPIO extender.
drv_ext_light_rgb_intensity_t
Simplified struct for setting RGB intensity directly.
Definition: drv_ext_light.h:190
drv_ext_light_rgb_sequence_t::color
drv_ext_light_color_mix_t color
Color mix used for the sequence.
Definition: drv_ext_light.h:201
drv_ext_light_init
ret_code_t drv_ext_light_init(drv_ext_light_init_t const *p_init, bool on_init_reset)
Function for initializing the drv_ext_light driver.
drv_ext_light_conf_t::g
uint8_t g
RGB G pin.
Definition: drv_ext_light.h:170
drv_ext_light_ioext_osc_status_t
drv_ext_light_ioext_osc_status_t
The state of the IO extender oscillator for each light.
Definition: drv_ext_light.h:107
drv_ext_light_conf_t::r
uint8_t r
RGB R pin.
Definition: drv_ext_light.h:169
EXTENDER_OSC_USED_PERM
Ext osc used on a permanent basis (light dimmed).
Definition: drv_ext_light.h:112
IOEXT_OSC_STATUS_T_SIZE
Number of different possible IO extender oscillator statuses.
Definition: drv_ext_light.h:113
drv_ext_light_rgb_sequence_t::sequence_vals
drv_ext_light_sequence_t sequence_vals
Defines sequence values: on/off/fade_in/fade_out times, and on/off intensities.
Definition: drv_ext_light.h:202
drv_ext_light_on
ret_code_t drv_ext_light_on(uint32_t id)
Function for turning a given light on.
drv_ext_light_init_t::resync_pin
uint8_t resync_pin
Pin used to resync IO extender counters. Use DRV_EXT_LIGHT_INVALID_RESYNC_PIN if unused.
Definition: drv_ext_light.h:185
drv_ext_light_sequence
ret_code_t drv_ext_light_sequence(uint32_t id, drv_ext_light_sequence_t *const p_sequence)
Function for setting color for a given light through the GPIO extender.
drv_ext_light_init_t
Struct for initializing the light driver.
Definition: drv_ext_light.h:179
drv_ext_light_rgb_intensity_set
ret_code_t drv_ext_light_rgb_intensity_set(uint32_t id, drv_ext_light_rgb_intensity_t const *const p_intensity)
Function for setting color for RGB LEDs.
drv_ext_light_conf_t::b
uint8_t b
RGB B pin.
Definition: drv_ext_light.h:171
EXTENDER_OSC_USED_RUNNING
Ext osc running (Light currently on).
Definition: drv_ext_light.h:110
drv_ext_light_intensity_set
ret_code_t drv_ext_light_intensity_set(uint32_t id, uint8_t intensity)
Function for setting intensity of monochrome light.
drv_ext_light_data_t
App timer and internal light timing struct.
Definition: drv_ext_light.h:138
drv_ext_light_data_t::timer
app_timer_id_t timer
App timer information.
Definition: drv_ext_light.h:141
drv_ext_light_data_t::p_status
drv_ext_light_status_t * p_status
Light status information.
Definition: drv_ext_light.h:140
DRV_EXT_LIGHT_TYPE_MONO
Monochrome (single) LED.
Definition: drv_ext_light.h:155
drv_ext_light_reset
ret_code_t drv_ext_light_reset(void)
Function for resetting all the registers of the IO extender to their default values.
drv_ext_light_conf_t::type
drv_ext_light_reg_type_t type
Monochrome or RGB.
Definition: drv_ext_light.h:163
drv_ext_light_status_t
Struct used by the app_timer handler, mainly for power saving.
Definition: drv_ext_light.h:118
drv_ext_light_status_t::inactive_time_ms
uint32_t inactive_time_ms
Total time for a light to be off.
Definition: drv_ext_light.h:121
EXTENDER_OSC_USED_PAUSED
Ext osc paused (Light currently off).
Definition: drv_ext_light.h:111
drv_ext_light_reg_type_t
drv_ext_light_reg_type_t
Monochrome (single) or RGB light.
Definition: drv_ext_light.h:153
drv_ext_light_off
ret_code_t drv_ext_light_off(uint32_t id)
Function for turning a given light off.
DRV_EXT_LIGHT_STATUS_CODE_INVALID_PARAM
Invalid parameters.
Definition: drv_ext_light.h:71
drv_ext_light_color_mix_t
drv_ext_light_color_mix_t
The following color combinations are possible when using drv_ext_light_rgb_sequence_t.
Definition: drv_ext_light.h:93
drv_ext_light_rgb_intensity_t::g
uint8_t g
Green intensity.
Definition: drv_ext_light.h:193
DRV_EXT_LIGHT_TYPE_RGB
RGB LED (three IOs used).
Definition: drv_ext_light.h:156
drv_ext_light_conf_t
Struct that defines the type of light (monochrome or RGB) and the corresponding connected pins...
Definition: drv_ext_light.h:161
drv_ext_light_status_t::ioext_osc_status
drv_ext_light_ioext_osc_status_t ioext_osc_status
Status, holds information on the current use of the IO extender oscillator.
Definition: drv_ext_light.h:123
drv_ext_light_status_t::active_time_ms
uint32_t active_time_ms
Total time for a light to be on or dimmed.
Definition: drv_ext_light.h:120
drv_ext_light_status_t::colors
drv_ext_light_color_mix_t colors
Color mix used for breathe, single-shot, or blink.
Definition: drv_ext_light.h:122
drv_ext_light_init_t::num_lights
uint8_t num_lights
Number of connected lights.
Definition: drv_ext_light.h:182
drv_ext_light_init_t::clkx_div
drv_ext_light_clkx_div_t clkx_div
IO extender clock divider.
Definition: drv_ext_light.h:183
DRV_EXT_LIGHT_STATUS_CODE_INVALID_TIMER_VALUE
The desired time is too long. Increase the ClkX divider.
Definition: drv_ext_light.h:74
DRV_EXT_LIGHT_STATUS_CODE_SUCCESS
Successful.
Definition: drv_ext_light.h:70
drv_ext_light_rgb_intensity_t::b
uint8_t b
Blue intensity.
Definition: drv_ext_light.h:194