atomic.c 7.5 KB

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