wt_mono_backlight.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright 2018 Jason Williams (Wilba)
  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 "wt_mono_backlight.h"
  17. #include "drivers/avr/i2c_master.h"
  18. #include "drivers/issi/is31fl3736.h"
  19. #include <avr/interrupt.h>
  20. #define ISSI_ADDR_DEFAULT 0x50
  21. bool g_suspend_state = false;
  22. // Global tick at 20 Hz
  23. uint32_t g_tick = 0;
  24. uint8_t g_config_effect_speed = 0;
  25. uint8_t g_config_brightness = 255;
  26. void backlight_init_drivers(void)
  27. {
  28. // Initialize I2C
  29. i2c_init();
  30. IS31FL3736_init( ISSI_ADDR_DEFAULT );
  31. for ( uint8_t index = 0; index < 96; index++ ) {
  32. IS31FL3736_mono_set_led_control_register( index, true );
  33. }
  34. IS31FL3736_update_led_control_registers( ISSI_ADDR_DEFAULT, 0x00 );
  35. }
  36. // This is (F_CPU/1024) / 20 Hz
  37. // = 15625 Hz / 20 Hz
  38. // = 781
  39. #define TIMER3_TOP 781
  40. void backlight_timer_init(void)
  41. {
  42. static uint8_t backlight_timer_is_init = 0;
  43. if ( backlight_timer_is_init ) {
  44. return;
  45. }
  46. backlight_timer_is_init = 1;
  47. // Timer 3 setup
  48. TCCR3B = _BV(WGM32) | // CTC mode OCR3A as TOP
  49. _BV(CS32) | _BV(CS30); // prescale by /1024
  50. // Set TOP value
  51. uint8_t sreg = SREG;
  52. cli();
  53. OCR3AH = (TIMER3_TOP >> 8) & 0xff;
  54. OCR3AL = TIMER3_TOP & 0xff;
  55. SREG = sreg;
  56. }
  57. void backlight_timer_enable(void)
  58. {
  59. TIMSK3 |= _BV(OCIE3A);
  60. }
  61. void backlight_timer_disable(void)
  62. {
  63. TIMSK3 &= ~_BV(OCIE3A);
  64. }
  65. void backlight_set_suspend_state(bool state)
  66. {
  67. g_suspend_state = state;
  68. }
  69. void backlight_effect_cycle_all(void)
  70. {
  71. uint8_t offset = ( g_tick << g_config_effect_speed ) & 0xFF;
  72. backlight_set_brightness_all( offset );
  73. }
  74. ISR(TIMER3_COMPA_vect)
  75. {
  76. // delay 1 second before driving LEDs or doing anything else
  77. static uint8_t startup_tick = 0;
  78. if ( startup_tick < 20 ) {
  79. startup_tick++;
  80. return;
  81. }
  82. g_tick++;
  83. //backlight_effect_cycle_all();
  84. backlight_set_brightness_all( 255 );
  85. }
  86. void backlight_update_pwm_buffers(void)
  87. {
  88. IS31FL3736_update_pwm_buffers(ISSI_ADDR_DEFAULT,0x00);
  89. }
  90. void backlight_set_brightness_all( uint8_t value )
  91. {
  92. IS31FL3736_mono_set_brightness_all( value );
  93. }