You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0 KiB

  1. #include <smoothled.h>
  2. static uint32_t sourceColor = 0x000000;
  3. static uint32_t currentColor = 0x000000;
  4. static uint32_t targetColor = 0x000000;
  5. static int32_t smoothledTimer = -1;
  6. void smoothled_set(uint32_t color) {
  7. smoothledTimer = timer_read32();
  8. sourceColor = currentColor;
  9. targetColor = color;
  10. }
  11. void smoothled_process(void) {
  12. if (smoothledTimer < 0) {
  13. return;
  14. }
  15. int32_t kb = timer_elapsed32(smoothledTimer);
  16. int32_t ka = SMOOTH_DURATION - kb;
  17. if (kb > SMOOTH_DURATION) {
  18. kb = SMOOTH_DURATION;
  19. ka = 0;
  20. smoothledTimer = -1;
  21. }
  22. currentColor = 0;
  23. for (int i = 2; i >= 0; i--) {
  24. uint32_t shift = i * 8;
  25. currentColor |= (ka * ((uint32_t)(sourceColor >> shift) & 0xFF) + kb * ((uint32_t)(targetColor >> shift) & 0xFF)) / SMOOTH_DURATION;
  26. /*currentColor |= ((targetColor >> shift) & 0xFF);*/
  27. currentColor <<= 8;
  28. }
  29. currentColor >>= 8;
  30. rgblight_setrgb((currentColor >> 16) & 0xFF, (currentColor >> 8) & 0xFF, currentColor & 0xFF);
  31. }