utils.h 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2021 Victor Toni (@vitoni)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. /**
  7. * @brief Changes `*value` to `new_value`.
  8. * @param[in,out] value Pointer to variable to be changed.
  9. * @param[in] new_value Value to be changed.
  10. * @param[in,out] changed Flag indicating `*value` and `new_value` were different.
  11. */
  12. void update_value(uint8_t *value, const uint8_t new_value, bool *changed);
  13. /**
  14. * @brief Checks whether a value is in the given range.
  15. * @param[in] value Value to be checked.
  16. * @param[in] range_min Lower bound of range (inclusive).
  17. * @param[in] range_max Upper bound of range (inclusive).
  18. * @return `true` if (range_min <= value <= range_max), `false` otherwise
  19. */
  20. bool in_range(const uint8_t value, const uint8_t range_min, const uint8_t range_max);
  21. /**
  22. * @brief Calculates the sine value based on sin8() and scales it to the given range (unsigned).
  23. *
  24. * Table of values for unscaled sin8() eg. a theta of 0 results to 128 and a theta of 255 (240+15) results to 125.
  25. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  26. +----------------------------------------------------------------
  27. 0: 128 131 134 137 140 143 146 149 152 155 158 161 164 167 170 173
  28. 16: 177 179 182 184 187 189 192 194 197 200 202 205 207 210 212 215
  29. 32: 218 219 221 223 224 226 228 229 231 233 234 236 238 239 241 243
  30. 48: 245 245 246 246 247 248 248 249 250 250 251 251 252 253 253 254
  31. 64: 255 254 253 253 252 251 251 250 250 249 248 248 247 246 246 245
  32. 80: 245 243 241 239 238 236 234 233 231 229 228 226 224 223 221 219
  33. 96: 218 215 212 210 207 205 202 200 197 194 192 189 187 184 182 179
  34. 112: 177 173 170 167 164 161 158 155 152 149 146 143 140 137 134 131
  35. 128: 128 125 122 119 116 113 110 107 104 101 98 95 92 89 86 83
  36. 144: 79 77 74 72 69 67 64 62 59 56 54 51 49 46 44 41
  37. 160: 38 37 35 33 32 30 28 27 25 23 22 20 18 17 15 13
  38. 176: 11 11 10 10 9 8 8 7 6 6 5 5 4 3 3 2
  39. 192: 1 2 3 3 4 5 5 6 6 7 8 8 9 10 10 11
  40. 208: 11 13 15 17 18 20 22 23 25 27 28 30 32 33 35 37
  41. 224: 38 41 44 46 49 51 54 56 59 62 64 67 69 72 74 77
  42. 240: 79 83 86 89 92 95 98 101 104 107 110 113 116 119 122 125
  43. *
  44. * @param[in] theta Angle (a full circle mapped to 0-255) used as input for sine calculation.
  45. * @param[in] range_min Lower bound of range (inclusive).
  46. * @param[in] range_max Upper bound of range (inclusive).
  47. * @return Calculated sine value mapped to the given range.
  48. */
  49. uint8_t scaled_sin(const uint8_t theta, const uint8_t range_min, const uint8_t range_max);
  50. /**
  51. * @brief Increases the given value until reaching range_max.
  52. * The increments occur following an upwards sine wave (scaled from range_min to range_max).
  53. * @param[in] theta Angle (a full circle mapped to 0-255) used as input for sine calculation.
  54. * @param[in] range_min Lower bound of range (inclusive).
  55. * @param[in] range_max Upper bound of range (inclusive).
  56. * @param[in] max_delta Maximum delta between value and range_max (due to values being integers and eventually not fully matching).
  57. * @param[in,out] value Reference of variable to be increased
  58. * @return `true` if value and range_max are within a delta of 3 (chosen by fair dice rolling), `false` otherwise
  59. * @see scaled_sin()
  60. */
  61. bool scaled_sin_up(const uint8_t thea, const uint8_t range_min, const uint8_t range_max, const uint8_t max_delta, uint8_t *value);
  62. /**
  63. * @brief Decreases the given value until reaching range_min.
  64. * The decrements occur following an downwards sinus wave (scaled from range_min to range_max).
  65. * @param[in] theta Angle (a full circle mapped to 0-255) used as input for sinus calculation.
  66. * @param[in] range_min Lower bound of range (inclusive).
  67. * @param[in] range_max Upper bound of range (inclusive).
  68. * @param[in] max_delta Maximum delta between value and range_min (due to values being integers and eventually not fully matching).
  69. * @param[in,out] value Reference of variable to be decreased
  70. * @return `true` if value and range_max are within a delta of 3 (chosen by fair dice rolling), `false` otherwise
  71. * @see scaled_sin()
  72. */
  73. bool scaled_sin_down(const uint8_t theta, const uint8_t range_min, const uint8_t range_max, const uint8_t max_delta, uint8_t *value);