Units.h 942 B

123456789101112131415161718192021222324252627282930313233
  1. #include <Arduino.h>
  2. #include <inttypes.h>
  3. #ifndef _UNITS_H
  4. #define _UNITS_H
  5. // MiLight CCT bulbs range from 2700K-6500K, or ~370.3-153.8 mireds.
  6. #define COLOR_TEMP_MAX_MIREDS 370
  7. #define COLOR_TEMP_MIN_MIREDS 153
  8. class Units {
  9. public:
  10. template <typename T, typename V>
  11. static T rescale(T value, V newMax, float oldMax = 255.0) {
  12. return round(value * (newMax / oldMax));
  13. }
  14. static uint8_t miredsToWhiteVal(uint16_t mireds, uint8_t maxValue = 255) {
  15. return rescale<uint16_t, uint16_t>(
  16. constrain(mireds, COLOR_TEMP_MIN_MIREDS, COLOR_TEMP_MAX_MIREDS) - COLOR_TEMP_MIN_MIREDS,
  17. maxValue,
  18. (COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS)
  19. );
  20. }
  21. static uint16_t whiteValToMireds(uint8_t value, uint8_t maxValue = 255) {
  22. uint16_t scaled = rescale<uint16_t, uint16_t>(value, (COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS), maxValue);
  23. return COLOR_TEMP_MIN_MIREDS + scaled;
  24. }
  25. };
  26. #endif