nRF5 SDK for Mesh v5.0.0
light_ctl_utils.h
1 /* Copyright (c) 2010 - 2020, Nordic Semiconductor ASA
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form, except as embedded into a Nordic
11  * Semiconductor ASA integrated circuit in a product or a software update for
12  * such product, must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution.
15  *
16  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * 4. This software, with or without modification, must only be used with a
21  * Nordic Semiconductor ASA integrated circuit.
22  *
23  * 5. Any software provided in binary form under this license must not be reverse
24  * engineered, decompiled, modified and/or disassembled.
25  *
26  * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
27  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef LIGHT_CTL_UTILS_H__
39 #define LIGHT_CTL_UTILS_H__
40 #include <stdint.h>
41 
42 #include "nordic_common.h"
43 
85 #define T32_SCALE_FACTOR (65535LL)
86 
88 #define SCALE_DOWN(_input, _scale_factor) ((_input)/(_scale_factor))
89 
91 #define SCALE_UP(_input, _scale_factor) ((_input)*(_scale_factor))
92 
99 static inline uint16_t light_ctl_utils_temperature32_to_temperature(uint32_t temperature32)
100 {
101  return ((uint32_t)SCALE_DOWN(temperature32, T32_SCALE_FACTOR));
102 }
103 
110 static inline uint32_t light_ctl_utils_temperature_to_temperature32(uint16_t temperature)
111 {
112  return SCALE_UP((uint32_t)temperature, T32_SCALE_FACTOR);
113 }
114 
125 static inline uint32_t light_ctl_utils_level_to_temperature32(int16_t level, uint32_t t32_min,
126  uint32_t t32_max)
127 {
128  return (t32_min + SCALE_DOWN((uint64_t) (level + 32768) * (uint64_t)(t32_max - t32_min), T32_SCALE_FACTOR));
129 }
130 
141 static inline int16_t light_ctl_utils_temperature32_to_level(uint32_t temperature32, uint32_t t32_min,
142  uint32_t t32_max)
143 {
144  if (t32_max == t32_min)
145  {
146  return 0;
147  }
148 
149  return ((SCALE_UP((uint64_t)(temperature32 - t32_min), T32_SCALE_FACTOR)/(t32_max - t32_min)) - 32768);
150 }
151 
164 static inline int32_t light_ctl_utils_level_delta_to_temperature_delta(int32_t generic_level_delta,
165  uint16_t t_min, uint16_t t_max)
166 {
167  return (generic_level_delta * ((int32_t)t_max - (int32_t)t_min))/65535L;
168 }
169 
178 static inline uint32_t light_ctl_utils_temperature32_range_restrict(uint32_t temperature32,
179  uint32_t t32_min, uint32_t t32_max)
180 {
181  /* 6.1.3.1.3 binding with CTL temperature range state */
182  return MAX(t32_min, MIN(t32_max, temperature32));
183 }
185 #endif /* LIGHT_CTL_UTILS_H__ */
static int16_t light_ctl_utils_temperature32_to_level(uint32_t temperature32, uint32_t t32_min, uint32_t t32_max)
Converts internal, scaled up, temperature32 value to Generic Level state.
#define SCALE_UP(_input, _scale_factor)
Macro for performing scaling up.
static uint32_t light_ctl_utils_temperature_to_temperature32(uint16_t temperature)
Converts netural temperature value to internal, scaled up, temperature32 value.
static uint32_t light_ctl_utils_temperature32_range_restrict(uint32_t temperature32, uint32_t t32_min, uint32_t t32_max)
Restricts the given temperature32 value to given minimum and maximum (scaled up) range values...
#define SCALE_DOWN(_input, _scale_factor)
Macro for performing scaling down.
static int32_t light_ctl_utils_level_delta_to_temperature_delta(int32_t generic_level_delta, uint16_t t_min, uint16_t t_max)
Converts Generic Level Delta value to internal temperature value.
static uint16_t light_ctl_utils_temperature32_to_temperature(uint32_t temperature32)
Converts the scaled up temperature32 value back to neutral value.
static uint32_t light_ctl_utils_level_to_temperature32(int16_t level, uint32_t t32_min, uint32_t t32_max)
Converts Generic Level state to internal, scaled up, temperature32 value.
#define T32_SCALE_FACTOR
Scaling factor used by this module to scale given temperature value so as not to loose precision...

Documentation feedback | Developer Zone | Subscribe | Updated