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.

216 lines
5.1 KiB

ps2avrgb: fix incorrect avr ports specified (for numlock and capslock LEDs) (#3453) * Add M6-A keymap * Update XD60 keymap * Update XD60 keymap readme * Update JJ40 and Let's Split keymaps * Add readme for M6-A * Fix typo, update JJ40 README * Update jj40 readme * Cleanup jj40 keymap * Revert Let's Split QWERTY layer to default before #2010 * Update numpad layers * Fix: Let's Split keymap getting stuck mods due to having keycodes assigned on the Raise layer * Keep ASCII art consistent with keymap * Staryu: initial port * Add personal keymap * Added and updated READMEs * Fix: default keymap for staryu * Rudimentary backlight support. * Enabled mousekeys for default keymap * use QMK_KEYBOARD_H and LAYOUT * Update readme.md for NIU mini: flash using avrdude * Fix missing linebreaks for Staryu README * Update readme.md * Update PS2AVRGB boards with new matrix.c * Update canoe matrix.c; untested * Fix canoe.c for building (needs matrix_scan_user and matrix_init_user) * Add personal Iris keymap * Update keymap * Update keymap * Update keymap, disable backlighting and underglow * Move PrintScreen button * Add README * Update personal keymaps * Add INS key * Limit USB max power consumption, change Fn to MENU * Remove Numpad layer (easy to accidentally toggle) * Fix backlighting for ps2avrgb * Update comments to refer to actual pin naming * Possible fix for xyverz ortho keymap: define RGBLED_NUM * Make led_set_user in backlight.c overridable * Add changes to address points raised in code review, untested (don't have build env right now)
5 years ago
ps2avrgb: fix incorrect avr ports specified (for numlock and capslock LEDs) (#3453) * Add M6-A keymap * Update XD60 keymap * Update XD60 keymap readme * Update JJ40 and Let's Split keymaps * Add readme for M6-A * Fix typo, update JJ40 README * Update jj40 readme * Cleanup jj40 keymap * Revert Let's Split QWERTY layer to default before #2010 * Update numpad layers * Fix: Let's Split keymap getting stuck mods due to having keycodes assigned on the Raise layer * Keep ASCII art consistent with keymap * Staryu: initial port * Add personal keymap * Added and updated READMEs * Fix: default keymap for staryu * Rudimentary backlight support. * Enabled mousekeys for default keymap * use QMK_KEYBOARD_H and LAYOUT * Update readme.md for NIU mini: flash using avrdude * Fix missing linebreaks for Staryu README * Update readme.md * Update PS2AVRGB boards with new matrix.c * Update canoe matrix.c; untested * Fix canoe.c for building (needs matrix_scan_user and matrix_init_user) * Add personal Iris keymap * Update keymap * Update keymap * Update keymap, disable backlighting and underglow * Move PrintScreen button * Add README * Update personal keymaps * Add INS key * Limit USB max power consumption, change Fn to MENU * Remove Numpad layer (easy to accidentally toggle) * Fix backlighting for ps2avrgb * Update comments to refer to actual pin naming * Possible fix for xyverz ortho keymap: define RGBLED_NUM * Make led_set_user in backlight.c overridable * Add changes to address points raised in code review, untested (don't have build env right now)
5 years ago
  1. /**
  2. * Backlighting code for PS2AVRGB boards (ATMEGA32A)
  3. * Kenneth A. (github.com/krusli | krusli.me)
  4. Modified by Wayne K Jones (github.com/WarmCatUK) 2018
  5. */
  6. #include "backlight.h"
  7. #include "quantum.h"
  8. #include <avr/pgmspace.h>
  9. #include <avr/interrupt.h>
  10. #include "backlight_custom.h"
  11. #include "breathing_custom.h"
  12. // DEBUG
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. // Port D: digital pins of the AVR chipset
  16. //#define NUMLOCK_PORT (1 << 2) // 2nd pin of Port D (digital)
  17. #define CAPSLOCK_PORT (1 << 1) // 1st pin
  18. #define BACKLIGHT_PORT (1 << 4) // D4
  19. //#define SCROLLLOCK_PORT (1 << 6) // D6
  20. #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64
  21. #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default
  22. #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask
  23. #define PWM_MAX 0xFF
  24. #define TIMER_TOP 255 // 8 bit PWM
  25. extern backlight_config_t backlight_config;
  26. /**
  27. * References
  28. * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation
  29. * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b
  30. * Timers: http://www.avrbeginners.net/architecture/timers/timers.html
  31. * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/
  32. * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware
  33. */
  34. // @Override
  35. // turn LEDs on and off depending on USB caps/num/scroll lock states.
  36. __attribute__ ((weak))
  37. void led_set_user(uint8_t usb_led) {
  38. /*
  39. if (usb_led & (1 << USB_LED_NUM_LOCK)) {
  40. // turn on
  41. DDRD |= NUMLOCK_PORT;
  42. PORTD |= NUMLOCK_PORT;
  43. } else {
  44. // turn off
  45. DDRD &= ~NUMLOCK_PORT;
  46. PORTD &= ~NUMLOCK_PORT;
  47. }
  48. */
  49. if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
  50. DDRD |= CAPSLOCK_PORT;
  51. PORTD |= CAPSLOCK_PORT;
  52. } else {
  53. DDRD &= ~CAPSLOCK_PORT;
  54. PORTD &= ~CAPSLOCK_PORT;
  55. }
  56. /*
  57. if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
  58. DDRD |= SCROLLLOCK_PORT;
  59. PORTD |= SCROLLLOCK_PORT;
  60. } else {
  61. DDRD &= ~SCROLLLOCK_PORT;
  62. PORTD &= ~SCROLLLOCK_PORT;
  63. }
  64. */
  65. }
  66. #ifdef BACKLIGHT_ENABLE
  67. // sets up Timer 1 for 8-bit PWM
  68. void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE
  69. // default 8 bit mode
  70. TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH
  71. TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW
  72. // clear output compare value A
  73. // outb(OCR1AH, 0);
  74. // outb(OCR1AL, 0);
  75. // clear output comparator registers for B
  76. OCR1BH = 0; // outb(OCR1BH, 0);
  77. OCR1BL = 0; // outb(OCR1BL, 0);
  78. }
  79. bool is_init = false;
  80. void timer1Init(void) {
  81. // timer1SetPrescaler(TIMER1PRESCALE)
  82. // set to DIV/64
  83. (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
  84. // reset TCNT1
  85. TCNT1H = 0; // outb(TCNT1H, 0);
  86. TCNT1L = 0; // outb(TCNT1L, 0);
  87. // TOIE1: Timer Overflow Interrupt Enable (Timer 1);
  88. TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1);
  89. is_init = true;
  90. }
  91. void timer1UnInit(void) {
  92. // set prescaler back to NONE
  93. (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP
  94. // disable timer overflow interrupt
  95. TIMSK &= ~_BV(TOIE1); // overflow bit?
  96. setPWM(0);
  97. is_init = false;
  98. }
  99. // handle TCNT1 overflow
  100. //! Interrupt handler for tcnt1 overflow interrupt
  101. ISR(TIMER1_OVF_vect, ISR_NOBLOCK)
  102. {
  103. // sei();
  104. // handle breathing here
  105. #ifdef BACKLIGHT_BREATHING
  106. if (is_breathing()) {
  107. custom_breathing_handler();
  108. }
  109. #endif
  110. // TODO call user defined function
  111. }
  112. // enable timer 1 PWM
  113. // timer1PWMBOn()
  114. void timer1PWMBEnable(void) {
  115. // turn on channel B (OC1B) PWM output
  116. // set OC1B as non-inverted PWM
  117. TCCR1A |= _BV(COM1B1);
  118. TCCR1A &= ~_BV(COM1B0);
  119. }
  120. // disable timer 1 PWM
  121. // timer1PWMBOff()
  122. void timer1PWMBDisable(void) {
  123. TCCR1A &= ~_BV(COM1B1);
  124. TCCR1A &= ~_BV(COM1B0);
  125. }
  126. void enableBacklight(void) {
  127. DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
  128. PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
  129. }
  130. void disableBacklight(void) {
  131. // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
  132. PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
  133. }
  134. void startPWM(void) {
  135. timer1Init();
  136. timer1PWMBEnable();
  137. enableBacklight();
  138. }
  139. void stopPWM(void) {
  140. timer1UnInit();
  141. disableBacklight();
  142. timer1PWMBDisable();
  143. }
  144. void b_led_init_ports(void) {
  145. /* turn backlight on/off depending on user preference */
  146. #if BACKLIGHT_ON_STATE == 0
  147. // DDRx register: sets the direction of Port D
  148. // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
  149. PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
  150. #else
  151. DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
  152. PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
  153. #endif
  154. timer1PWMSetup();
  155. startPWM();
  156. #ifdef BACKLIGHT_BREATHING
  157. breathing_enable();
  158. #endif
  159. }
  160. void b_led_set(uint8_t level) {
  161. if (level > BACKLIGHT_LEVELS) {
  162. level = BACKLIGHT_LEVELS;
  163. }
  164. setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS));
  165. }
  166. // called every matrix scan
  167. void b_led_task(void) {
  168. // do nothing for now
  169. }
  170. void setPWM(uint16_t xValue) {
  171. if (xValue > TIMER_TOP) {
  172. xValue = TIMER_TOP;
  173. }
  174. OCR1B = xValue; // timer1PWMBSet(xValue);
  175. }
  176. #endif // BACKLIGHT_ENABLE