backlight_ps2avrGB.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright 2017 Sebastian Kaim
  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. #ifdef BACKLIGHT_ENABLE
  17. #include "backlight_ps2avrGB.h"
  18. #define sbi(reg,bit) reg |= (_BV(bit))
  19. #define cbi(reg,bit) reg &= (~_BV(bit))
  20. #define PWM10 WGM10
  21. #define PWM11 WGM11
  22. #define COM1x1 COM1B1
  23. #define OCR1x OCR1B
  24. void backlight_init_ports(void)
  25. {
  26. #if BACKLIGHT_ON_STATE == 0
  27. backlight_off();
  28. #else
  29. backlight_on();
  30. #endif
  31. // setup pwm
  32. // this bitmagic is sourced from the original firmware
  33. /*TCCR1B = ((TCCR1B & ~0x07) | 0x03);
  34. TCNT1H = 0;
  35. TCNT1L = 0;
  36. sbi(TIMSK, TOIE1);
  37. OCR1BH = 0;
  38. OCR1BL = 0;
  39. cbi(TCCR1A,PWM11);
  40. sbi(TCCR1A,PWM10);
  41. sbi(TCCR1A,COM1B1);
  42. cbi(TCCR1A,COM1B0);*/
  43. ICR1 = 0xFFFF;
  44. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  45. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  46. backlight_init();
  47. }
  48. void backlight_set(uint8_t level)
  49. {
  50. if( level == 0 ) {
  51. backlight_off();
  52. }
  53. else {
  54. backlight_on();
  55. /*uint8_t pwm = get_pwm_for_brightness(level);
  56. set_backlight_pwm(pwm);
  57. TCCR1A |= _BV(COM1x1);
  58. OCR1x = (level >= 2) ? 0xFFFF : 0x00FF;*/
  59. }
  60. }
  61. #define PWM_MAX 0xFF
  62. uint8_t get_pwm_for_brightness(uint8_t level)
  63. {
  64. // we need to cast up here to allow multiplication with 0xFF. We could also use floats, but this is probably a lot faster.
  65. uint16_t brightness = (uint16_t)level * (uint16_t)PWM_MAX / (uint16_t)BACKLIGHT_LEVELS;
  66. return (uint8_t)brightness;
  67. }
  68. void backlight_on(void)
  69. {
  70. //_SFR_IO8(0x12) |= _BV(0x4);
  71. LED_PIN |= BACKLIGHT_PORT_NUM;
  72. }
  73. void backlight_off(void)
  74. {
  75. //_SFR_IO8(0x12) &= ~_BV(0x4);
  76. LED_PIN &= ~BACKLIGHT_PORT_NUM;
  77. }
  78. void set_backlight_pwm(uint8_t level) {
  79. // this does not work (yet)
  80. //OCR1B = level;
  81. }
  82. #endif // BACKLIGHT_ENABLE