Linux preempt-rt
Check our new training course
Real-Time Linux with PREEMPT_RT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models * * Copyright (c) 2018 Vikrant More * * SPDX-License-Identifier: Apache-2.0 */ #include <drivers/gpio.h> #include "ble_mesh.h" #include "device_composition.h" #include "state_binding.h" #include "storage.h" #include "transition.h" #define MINDIFF 2.25e-308 static float sqrt(float square) { float root, last, diff; root = square / 3.0; diff = 1; if (square <= 0) { return 0; } do { last = root; root = (root + square / root) / 2.0; diff = root - last; } while (diff > MINDIFF || diff < -MINDIFF); return root; } static int32_t ceiling(float num) { int32_t inum; inum = (int32_t) num; if (num == (float) inum) { return inum; } return inum + 1; } static uint16_t actual_to_linear(uint16_t val) { float tmp; tmp = ((float) val / UINT16_MAX); return (uint16_t) ceiling(UINT16_MAX * tmp * tmp); } static uint16_t linear_to_actual(uint16_t val) { return (uint16_t) (UINT16_MAX * sqrt(((float) val / UINT16_MAX))); } uint16_t constrain_lightness(uint16_t light) { if (light > 0 && light < ctl->light->range_min) { light = ctl->light->range_min; } else if (light > ctl->light->range_max) { light = ctl->light->range_max; } return light; } static void constrain_target_lightness2(void) { /* This is as per Mesh Model Specification 3.3.2.2.3 */ if (ctl->light->target > 0 && ctl->light->target < ctl->light->range_min) { if (ctl->light->delta < 0) { ctl->light->target = 0U; } else { ctl->light->target = ctl->light->range_min; } } else if (ctl->light->target > ctl->light->range_max) { ctl->light->target = ctl->light->range_max; } } uint16_t constrain_temperature(uint16_t temp) { if (temp < ctl->temp->range_min) { temp = ctl->temp->range_min; } else if (temp > ctl->temp->range_max) { temp = ctl->temp->range_max; } return temp; } static int16_t light_ctl_temp_to_level(uint16_t temp) { float tmp; /* Mesh Model Specification 6.1.3.1.1 2nd formula start */ tmp = (temp - ctl->temp->range_min) * UINT16_MAX; tmp = tmp / (ctl->temp->range_max - ctl->temp->range_min); return (int16_t) (tmp + INT16_MIN); /* 6.1.3.1.1 2nd formula end */ } uint16_t level_to_light_ctl_temp(int16_t level) { uint16_t tmp; float diff; /* Mesh Model Specification 6.1.3.1.1 1st formula start */ diff = (float) (ctl->temp->range_max - ctl->temp->range_min) / UINT16_MAX; tmp = (uint16_t) ((level - INT16_MIN) * diff); return (ctl->temp->range_min + tmp); /* 6.1.3.1.1 1st formula end */ } void set_target(uint8_t type, void *dptr) { switch (type) { case ONOFF: { uint8_t onoff; onoff = *((uint8_t *) dptr); if (onoff == STATE_OFF) { ctl->light->target = 0U; } else if (onoff == STATE_ON) { if (ctl->light->def == 0U) { ctl->light->target = ctl->light->last; } else { ctl->light->target = ctl->light->def; } } } break; case LEVEL_LIGHT: ctl->light->target = *((int16_t *) dptr) - INT16_MIN; ctl->light->target = constrain_lightness(ctl->light->target); break; case DELTA_LEVEL_LIGHT: ctl->light->target = *((int16_t *) dptr) - INT16_MIN; constrain_target_lightness2(); break; case MOVE_LIGHT: ctl->light->target = *((uint16_t *) dptr); break; case ACTUAL: ctl->light->target = *((uint16_t *) dptr); ctl->light->target = constrain_lightness(ctl->light->target); break; case LINEAR: ctl->light->target = linear_to_actual(*((uint16_t *) dptr)); ctl->light->target = constrain_lightness(ctl->light->target); break; case CTL_LIGHT: ctl->light->target = *((uint16_t *) dptr); ctl->light->target = constrain_lightness(ctl->light->target); break; case LEVEL_TEMP: ctl->temp->target = level_to_light_ctl_temp(*((int16_t *) dptr)); break; case MOVE_TEMP: ctl->temp->target = *((uint16_t *) dptr); break; case CTL_TEMP: ctl->temp->target = *((uint16_t *) dptr); ctl->temp->target = constrain_temperature(ctl->temp->target); break; case CTL_DELTA_UV: ctl->duv->target = *((int16_t *) dptr); break; default: return; } if (ctl->onpowerup == STATE_RESTORE) { save_on_flash(LAST_TARGET_STATES); } } int get_current(uint8_t type) { switch (type) { case ONOFF: if (ctl->light->current != 0U) { return STATE_ON; } else { if (ctl->light->target) { return STATE_ON; } else { return STATE_OFF; } } case LEVEL_LIGHT: return (int16_t) (ctl->light->current + INT16_MIN); case ACTUAL: return ctl->light->current; case LINEAR: return actual_to_linear(ctl->light->current); case CTL_LIGHT: return ctl->light->current; case LEVEL_TEMP: return light_ctl_temp_to_level(ctl->temp->current); case CTL_TEMP: return ctl->temp->current; case CTL_DELTA_UV: return ctl->duv->current; default: return 0U; } } int get_target(uint8_t type) { switch (type) { case ONOFF: if (ctl->light->target != 0U) { return STATE_ON; } else { return STATE_OFF; } case LEVEL_LIGHT: return (int16_t) (ctl->light->target + INT16_MIN); case ACTUAL: return ctl->light->target; case LINEAR: return actual_to_linear(ctl->light->target); case CTL_LIGHT: return ctl->light->target; case LEVEL_TEMP: return light_ctl_temp_to_level(ctl->temp->target); case CTL_TEMP: return ctl->temp->target; case CTL_DELTA_UV: return ctl->duv->target; default: return 0U; } }