ergodox_infinity.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include QMK_KEYBOARD_H
  2. #include <ch.h>
  3. #include <hal.h>
  4. #include "serial_link/system/serial_link.h"
  5. #ifdef VISUALIZER_ENABLE
  6. #include "lcd_backlight.h"
  7. #endif
  8. #ifdef WPM_ENABLE
  9. # include "serial_link/protocol/transport.h"
  10. # include "wpm.h"
  11. MASTER_TO_ALL_SLAVES_OBJECT(current_wpm, uint8_t);
  12. static remote_object_t* remote_objects[] = {
  13. REMOTE_OBJECT(current_wpm),
  14. };
  15. static uint8_t last_sent_wpm = 0;
  16. #endif
  17. void init_serial_link_hal(void) {
  18. PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
  19. PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
  20. PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3);
  21. PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3);
  22. }
  23. #define RED_PIN 1
  24. #define GREEN_PIN 2
  25. #define BLUE_PIN 3
  26. #define CHANNEL_RED FTM0->CHANNEL[0]
  27. #define CHANNEL_GREEN FTM0->CHANNEL[1]
  28. #define CHANNEL_BLUE FTM0->CHANNEL[2]
  29. #define RGB_PORT PORTC
  30. #define RGB_PORT_GPIO GPIOC
  31. // Base FTM clock selection (72 MHz system clock)
  32. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  33. // Higher pre-scalar will use the most power (also look the best)
  34. // Pre-scalar calculations
  35. // 0 - 72 MHz -> 549 Hz
  36. // 1 - 36 MHz -> 275 Hz
  37. // 2 - 18 MHz -> 137 Hz
  38. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  39. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  40. // 5 - 2 250 kHz -> 17 Hz
  41. // 6 - 1 125 kHz -> 9 Hz
  42. // 7 - 562 500 Hz -> 4 Hz
  43. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  44. // Which will reduce the brightness range
  45. #define PRESCALAR_DEFINE 0
  46. void lcd_backlight_hal_init(void) {
  47. // Setup Backlight
  48. SIM->SCGC6 |= SIM_SCGC6_FTM0;
  49. FTM0->CNT = 0; // Reset counter
  50. // PWM Period
  51. // 16-bit maximum
  52. FTM0->MOD = 0xFFFF;
  53. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  54. #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
  55. CHANNEL_RED.CnSC = CNSC_MODE;
  56. CHANNEL_GREEN.CnSC = CNSC_MODE;
  57. CHANNEL_BLUE.CnSC = CNSC_MODE;
  58. // System clock, /w prescalar setting
  59. FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
  60. CHANNEL_RED.CnV = 0;
  61. CHANNEL_GREEN.CnV = 0;
  62. CHANNEL_BLUE.CnV = 0;
  63. RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
  64. RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
  65. RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
  66. #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
  67. RGB_PORT->PCR[RED_PIN] = RGB_MODE;
  68. RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
  69. RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
  70. }
  71. static uint16_t cie_lightness(uint16_t v) {
  72. // The CIE 1931 formula for lightness
  73. // Y = luminance (output) 0-1
  74. // L = lightness input 0 - 100
  75. // Y = (L* / 902.3) if L* <= 8
  76. // Y = ((L* + 16) / 116)^3 if L* > 8
  77. float l = 100.0f * (v / 65535.0f);
  78. float y = 0.0f;
  79. if (l <= 8.0f) {
  80. y = l / 902.3;
  81. }
  82. else {
  83. y = ((l + 16.0f) / 116.0f);
  84. y = y * y * y;
  85. if (y > 1.0f) {
  86. y = 1.0f;
  87. }
  88. }
  89. return y * 65535.0f;
  90. }
  91. void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
  92. CHANNEL_RED.CnV = cie_lightness(r);
  93. CHANNEL_GREEN.CnV = cie_lightness(g);
  94. CHANNEL_BLUE.CnV = cie_lightness(b);
  95. }
  96. __attribute__ ((weak))
  97. void matrix_init_user(void) {
  98. }
  99. __attribute__ ((weak))
  100. void matrix_scan_user(void) {
  101. }
  102. void matrix_init_kb(void) {
  103. // put your keyboard start-up code here
  104. // runs once when the firmware starts up
  105. matrix_init_user();
  106. // The backlight always has to be initialized, otherwise it will stay lit
  107. #ifndef VISUALIZER_ENABLE
  108. lcd_backlight_hal_init();
  109. #endif
  110. #ifdef WPM_ENABLE
  111. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*));
  112. #endif
  113. }
  114. void matrix_scan_kb(void) {
  115. // put your looping keyboard code here
  116. // runs every cycle (a lot)
  117. #ifdef WPM_ENABLE
  118. if (is_serial_link_master()) {
  119. uint8_t current_wpm = get_current_wpm();
  120. if (current_wpm != last_sent_wpm) {
  121. *begin_write_current_wpm() = current_wpm;
  122. end_write_current_wpm();
  123. last_sent_wpm = current_wpm;
  124. }
  125. } else if (is_serial_link_connected()) {
  126. uint8_t* new_wpm = read_current_wpm();
  127. if (new_wpm) {
  128. set_current_wpm(*new_wpm);
  129. }
  130. }
  131. #endif
  132. matrix_scan_user();
  133. }
  134. bool is_keyboard_master(void) {
  135. return is_serial_link_master();
  136. }
  137. __attribute__ ((weak))
  138. void ergodox_board_led_on(void){
  139. }
  140. __attribute__ ((weak))
  141. void ergodox_right_led_1_on(void){
  142. }
  143. __attribute__ ((weak))
  144. void ergodox_right_led_2_on(void){
  145. }
  146. __attribute__ ((weak))
  147. void ergodox_right_led_3_on(void){
  148. }
  149. __attribute__ ((weak))
  150. void ergodox_board_led_off(void){
  151. }
  152. __attribute__ ((weak))
  153. void ergodox_right_led_1_off(void){
  154. }
  155. __attribute__ ((weak))
  156. void ergodox_right_led_2_off(void){
  157. }
  158. __attribute__ ((weak))
  159. void ergodox_right_led_3_off(void){
  160. }
  161. __attribute__ ((weak))
  162. void ergodox_right_led_1_set(uint8_t n) {
  163. }
  164. __attribute__ ((weak))
  165. void ergodox_right_led_2_set(uint8_t n) {
  166. }
  167. __attribute__ ((weak))
  168. void ergodox_right_led_3_set(uint8_t n) {
  169. }
  170. #ifdef SWAP_HANDS_ENABLE
  171. __attribute__ ((weak))
  172. const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  173. {{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}},
  174. {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}},
  175. {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}},
  176. {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}},
  177. {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}},
  178. {{0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}},
  179. {{0, 15}, {1, 15}, {2, 15}, {3, 15}, {4, 15}},
  180. {{0, 16}, {1, 16}, {2, 16}, {3, 16}, {4, 16}},
  181. {{0, 17}, {1, 17}, {2, 17}, {3, 17}, {4, 17}},
  182. {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
  183. {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
  184. {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
  185. {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
  186. {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
  187. {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}},
  188. {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}},
  189. {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}},
  190. {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}},
  191. };
  192. #endif