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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

backlight.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright 2013,2014 Kai Ryu <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/pgmspace.h>
  17. #include "backlight.h"
  18. #include "breathing_led.h"
  19. #ifdef GH60_REV_CHN
  20. #else
  21. #define SOFTPWM_TIMER_TOP F_CPU/(256*64)
  22. uint8_t softpwm_ocr = 0;
  23. uint8_t softpwm_ocr_buff = 0;
  24. #endif
  25. static const uint8_t backlight_table[] PROGMEM = {
  26. 0, 16, 128, 255
  27. };
  28. /* Backlight pin configuration
  29. * PWM: PB6 (Rev.CHN)
  30. * GPIO: PF7 PF6 PF5 PF4 (Rev.B)
  31. */
  32. void backlight_enable(void)
  33. {
  34. #if defined(GH60_REV_CHN)
  35. // Turn on PWM
  36. DDRB |= (1<<PB6);
  37. cli();
  38. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  39. TCCR1B |= ((1<<CS11) | (1<<CS10));
  40. sei();
  41. #else
  42. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  43. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  44. cli();
  45. TCCR1B |= (1<<WGM12);
  46. TCCR1B |= (1<<CS10);
  47. OCR1AH = (SOFTPWM_TIMER_TOP>>8)&0xff;
  48. OCR1AL = SOFTPWM_TIMER_TOP&0xff;
  49. TIMSK1 |= (1<<OCIE1A);
  50. sei();
  51. #endif
  52. }
  53. void backlight_disable(void)
  54. {
  55. #if defined(GH60_REV_CHN)
  56. // Turn off PWM
  57. cli();
  58. DDRB &= ~(1<<PB6);
  59. TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
  60. TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
  61. sei();
  62. OCR1B = 0;
  63. #else
  64. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  65. cli();
  66. TCCR1B &= ~(1<<WGM12);
  67. TCCR1B &= ~(1<<CS10);
  68. TIMSK1 &= ~(1<<OCIE1A);
  69. sei();
  70. OCR1A = 0;
  71. #endif
  72. }
  73. void backlight_set(uint8_t level)
  74. {
  75. #ifdef BREATHING_LED_ENABLE
  76. switch (level) {
  77. case 1:
  78. case 2:
  79. case 3:
  80. backlight_enable();
  81. breathing_led_disable();
  82. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  83. break;
  84. case 4:
  85. case 5:
  86. case 6:
  87. backlight_enable();
  88. breathing_led_enable();
  89. breathing_led_set_duration(6 - level);
  90. break;
  91. case 0:
  92. default:
  93. breathing_led_disable();
  94. backlight_disable();
  95. break;
  96. }
  97. #else
  98. if (level > 0) {
  99. backlight_enable();
  100. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  101. }
  102. else {
  103. backlight_disable();
  104. }
  105. #endif
  106. }
  107. #ifdef BREATHING_LED_ENABLE
  108. void breathing_led_set_raw(uint8_t raw)
  109. {
  110. backlight_set_raw(raw);
  111. }
  112. #endif
  113. inline void backlight_set_raw(uint8_t raw)
  114. {
  115. #if defined(GH60_REV_CHN)
  116. OCR1B = raw;
  117. #else
  118. softpwm_ocr_buff = raw;
  119. #endif
  120. }
  121. #if defined(GH60_REV_CHN)
  122. #else
  123. ISR(TIMER1_COMPA_vect)
  124. {
  125. static uint8_t pwm = 0;
  126. pwm++;
  127. // LED on
  128. if (pwm == 0) {
  129. //PORTB |= (1<<PB6);
  130. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  131. softpwm_ocr = softpwm_ocr_buff;
  132. }
  133. // LED off
  134. if (pwm == softpwm_ocr) {
  135. //PORTB &= ~(1<<PB6);
  136. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  137. }
  138. }
  139. #endif