planck.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "planck.h"
  2. __attribute__ ((weak))
  3. void matrix_init_user(void) {}
  4. __attribute__ ((weak))
  5. void matrix_scan_user(void) {}
  6. __attribute__ ((weak))
  7. bool process_action_user(keyrecord_t *record) {
  8. return true;
  9. }
  10. __attribute__ ((weak))
  11. void led_set_user(uint8_t usb_led) {}
  12. void matrix_init_kb(void) {
  13. #ifdef BACKLIGHT_ENABLE
  14. backlight_init_ports();
  15. #endif
  16. #ifdef RGBLIGHT_ENABLE
  17. rgblight_init();
  18. #endif
  19. // Turn status LED on
  20. DDRE |= (1<<6);
  21. PORTE |= (1<<6);
  22. matrix_init_user();
  23. }
  24. void matrix_scan_kb(void) {
  25. matrix_scan_user();
  26. }
  27. bool process_action_kb(keyrecord_t *record) {
  28. return process_action_user(record);
  29. }
  30. void led_set_kb(uint8_t usb_led) {
  31. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  32. led_set_user(usb_led);
  33. }
  34. #ifdef BACKLIGHT_ENABLE
  35. #define CHANNEL OCR1C
  36. #define BREATHING_NO_HALT 0
  37. #define BREATHING_HALT_OFF 1
  38. #define BREATHING_HALT_ON 2
  39. static uint8_t breath_intensity;
  40. static uint8_t breath_speed;
  41. static uint16_t breathing_index;
  42. static uint8_t breathing_halt;
  43. void backlight_init_ports()
  44. {
  45. // Setup PB7 as output and output low.
  46. DDRB |= (1<<7);
  47. PORTB &= ~(1<<7);
  48. // Use full 16-bit resolution.
  49. ICR1 = 0xFFFF;
  50. // I could write a wall of text here to explain... but TL;DW
  51. // Go read the ATmega32u4 datasheet.
  52. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  53. // Pin PB7 = OCR1C (Timer 1, Channel C)
  54. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  55. // (i.e. start high, go low when counter matches.)
  56. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  57. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  58. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  59. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  60. backlight_init();
  61. breathing_defaults();
  62. }
  63. void backlight_set(uint8_t level)
  64. {
  65. // Prevent backlight blink on lowest level
  66. PORTB &= ~(_BV(PORTB7));
  67. if ( level == 0 )
  68. {
  69. // Turn off PWM control on PB7, revert to output low.
  70. TCCR1A &= ~(_BV(COM1C1));
  71. CHANNEL = 0x0;
  72. }
  73. else if ( level == BACKLIGHT_LEVELS )
  74. {
  75. // Turn on PWM control of PB7
  76. TCCR1A |= _BV(COM1C1);
  77. // Set the brightness
  78. CHANNEL = 0xFFFF;
  79. }
  80. else
  81. {
  82. // Turn on PWM control of PB7
  83. TCCR1A |= _BV(COM1C1);
  84. // Set the brightness
  85. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  86. }
  87. breathing_intensity_default();
  88. }
  89. void breathing_enable(void)
  90. {
  91. if (get_backlight_level() == 0)
  92. {
  93. breathing_index = 0;
  94. }
  95. else
  96. {
  97. // Set breathing_index to be at the midpoint (brightest point)
  98. breathing_index = 0x20 << breath_speed;
  99. }
  100. breathing_halt = BREATHING_NO_HALT;
  101. // Enable breathing interrupt
  102. TIMSK1 |= _BV(OCIE1A);
  103. }
  104. void breathing_pulse(void)
  105. {
  106. if (get_backlight_level() == 0)
  107. {
  108. breathing_index = 0;
  109. }
  110. else
  111. {
  112. // Set breathing_index to be at the midpoint + 1 (brightest point)
  113. breathing_index = 0x21 << breath_speed;
  114. }
  115. breathing_halt = BREATHING_HALT_ON;
  116. // Enable breathing interrupt
  117. TIMSK1 |= _BV(OCIE1A);
  118. }
  119. void breathing_disable(void)
  120. {
  121. // Disable breathing interrupt
  122. TIMSK1 &= ~_BV(OCIE1A);
  123. backlight_set(get_backlight_level());
  124. }
  125. void breathing_self_disable(void)
  126. {
  127. if (get_backlight_level() == 0)
  128. {
  129. breathing_halt = BREATHING_HALT_OFF;
  130. }
  131. else
  132. {
  133. breathing_halt = BREATHING_HALT_ON;
  134. }
  135. //backlight_set(get_backlight_level());
  136. }
  137. void breathing_toggle(void)
  138. {
  139. if (!is_breathing())
  140. {
  141. if (get_backlight_level() == 0)
  142. {
  143. breathing_index = 0;
  144. }
  145. else
  146. {
  147. // Set breathing_index to be at the midpoint + 1 (brightest point)
  148. breathing_index = 0x21 << breath_speed;
  149. }
  150. breathing_halt = BREATHING_NO_HALT;
  151. }
  152. // Toggle breathing interrupt
  153. TIMSK1 ^= _BV(OCIE1A);
  154. // Restore backlight level
  155. if (!is_breathing())
  156. {
  157. backlight_set(get_backlight_level());
  158. }
  159. }
  160. bool is_breathing(void)
  161. {
  162. return (TIMSK1 && _BV(OCIE1A));
  163. }
  164. void breathing_intensity_default(void)
  165. {
  166. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  167. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  168. }
  169. void breathing_intensity_set(uint8_t value)
  170. {
  171. breath_intensity = value;
  172. }
  173. void breathing_speed_default(void)
  174. {
  175. breath_speed = 4;
  176. }
  177. void breathing_speed_set(uint8_t value)
  178. {
  179. bool is_breathing_now = is_breathing();
  180. uint8_t old_breath_speed = breath_speed;
  181. if (is_breathing_now)
  182. {
  183. // Disable breathing interrupt
  184. TIMSK1 &= ~_BV(OCIE1A);
  185. }
  186. breath_speed = value;
  187. if (is_breathing_now)
  188. {
  189. // Adjust index to account for new speed
  190. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  191. // Enable breathing interrupt
  192. TIMSK1 |= _BV(OCIE1A);
  193. }
  194. }
  195. void breathing_speed_inc(uint8_t value)
  196. {
  197. if ((uint16_t)(breath_speed - value) > 10 )
  198. {
  199. breathing_speed_set(0);
  200. }
  201. else
  202. {
  203. breathing_speed_set(breath_speed - value);
  204. }
  205. }
  206. void breathing_speed_dec(uint8_t value)
  207. {
  208. if ((uint16_t)(breath_speed + value) > 10 )
  209. {
  210. breathing_speed_set(10);
  211. }
  212. else
  213. {
  214. breathing_speed_set(breath_speed + value);
  215. }
  216. }
  217. void breathing_defaults(void)
  218. {
  219. breathing_intensity_default();
  220. breathing_speed_default();
  221. breathing_halt = BREATHING_NO_HALT;
  222. }
  223. /* Breathing Sleep LED brighness(PWM On period) table
  224. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  225. *
  226. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  227. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  228. */
  229. static const uint8_t breathing_table[64] PROGMEM = {
  230. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  231. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  232. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  233. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  234. };
  235. ISR(TIMER1_COMPA_vect)
  236. {
  237. // CHANNEL = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  238. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  239. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  240. {
  241. // Disable breathing interrupt
  242. TIMSK1 &= ~_BV(OCIE1A);
  243. }
  244. CHANNEL = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  245. }
  246. #endif