planck.c 6.9 KB

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