atomic.c 7.5 KB

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