lcd_backlight_keyframes.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright 2017 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "lcd_backlight_keyframes.h"
  17. bool lcd_backlight_keyframe_animate_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  18. int frame_length = animation->frame_lengths[animation->current_frame];
  19. int current_pos = frame_length - animation->time_left_in_frame;
  20. uint8_t t_h = LCD_HUE(state->target_lcd_color);
  21. uint8_t t_s = LCD_SAT(state->target_lcd_color);
  22. uint8_t t_i = LCD_INT(state->target_lcd_color);
  23. uint8_t p_h = LCD_HUE(state->prev_lcd_color);
  24. uint8_t p_s = LCD_SAT(state->prev_lcd_color);
  25. uint8_t p_i = LCD_INT(state->prev_lcd_color);
  26. uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
  27. int d_h2 = t_h - p_h;
  28. // Chose the shortest way around
  29. int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
  30. int d_s = t_s - p_s;
  31. int d_i = t_i - p_i;
  32. int hue = (d_h * current_pos) / frame_length;
  33. int sat = (d_s * current_pos) / frame_length;
  34. int intensity = (d_i * current_pos) / frame_length;
  35. //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
  36. hue += p_h;
  37. sat += p_s;
  38. intensity += p_i;
  39. state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
  40. lcd_backlight_color(
  41. LCD_HUE(state->current_lcd_color),
  42. LCD_SAT(state->current_lcd_color),
  43. LCD_INT(state->current_lcd_color));
  44. return true;
  45. }
  46. bool lcd_backlight_keyframe_set_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  47. (void)animation;
  48. state->prev_lcd_color = state->target_lcd_color;
  49. state->current_lcd_color = state->target_lcd_color;
  50. lcd_backlight_color(
  51. LCD_HUE(state->current_lcd_color),
  52. LCD_SAT(state->current_lcd_color),
  53. LCD_INT(state->current_lcd_color));
  54. return false;
  55. }
  56. bool lcd_backlight_keyframe_disable(keyframe_animation_t* animation, visualizer_state_t* state) {
  57. (void)animation;
  58. (void)state;
  59. lcd_backlight_hal_color(0, 0, 0);
  60. return false;
  61. }
  62. bool lcd_backlight_keyframe_enable(keyframe_animation_t* animation, visualizer_state_t* state) {
  63. (void)animation;
  64. (void)state;
  65. lcd_backlight_color(LCD_HUE(state->current_lcd_color),
  66. LCD_SAT(state->current_lcd_color),
  67. LCD_INT(state->current_lcd_color));
  68. return false;
  69. }