planck.c 6.9 KB

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