q11.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2023 @ Keychron (https://www.keychron.com)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef DIP_SWITCH_ENABLE
  18. bool dip_switch_update_kb(uint8_t index, bool active) {
  19. if (!dip_switch_update_user(index, active)) {
  20. return false;
  21. }
  22. if (index == 0) {
  23. default_layer_set(1UL << (active ? 0 : 2));
  24. }
  25. return true;
  26. }
  27. #endif
  28. #if defined(RGB_MATRIX_ENABLE) && defined(CAPS_LOCK_LED_INDEX)
  29. bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  30. if (!rgb_matrix_indicators_advanced_user(led_min, led_max)) {
  31. return false;
  32. }
  33. // RGB_MATRIX_INDICATOR_SET_COLOR(index, red, green, blue);
  34. # if defined(CAPS_LOCK_LED_INDEX)
  35. if (host_keyboard_led_state().caps_lock) {
  36. RGB_MATRIX_INDICATOR_SET_COLOR(CAPS_LOCK_LED_INDEX, 255, 255, 255);
  37. } else {
  38. if (!rgb_matrix_get_flags()) {
  39. RGB_MATRIX_INDICATOR_SET_COLOR(CAPS_LOCK_LED_INDEX, 0, 0, 0);
  40. }
  41. }
  42. # endif // CAPS_LOCK_LED_INDEX
  43. # if defined(NUM_LOCK_LED_INDEX)
  44. if (host_keyboard_led_state().num_lock) {
  45. RGB_MATRIX_INDICATOR_SET_COLOR(NUM_LOCK_LED_INDEX, 255, 255, 255);
  46. } else {
  47. if (!rgb_matrix_get_flags()) {
  48. RGB_MATRIX_INDICATOR_SET_COLOR(NUM_LOCK_LED_INDEX, 0, 0, 0);
  49. }
  50. }
  51. # endif // NUM_LOCK_LED_INDEX
  52. return true;
  53. }
  54. #endif
  55. #define ADC_BUFFER_DEPTH 1
  56. #define ADC_NUM_CHANNELS 1
  57. #define ADC_SAMPLING_RATE ADC_SMPR_SMP_12P5
  58. #define ADC_RESOLUTION ADC_CFGR_RES_10BITS
  59. static int16_t analogReadPin_my(pin_t pin) {
  60. ADCConfig adcCfg = {};
  61. adcsample_t sampleBuffer[ADC_NUM_CHANNELS * ADC_BUFFER_DEPTH];
  62. ADCDriver *targetDriver = &ADCD1;
  63. ADCConversionGroup adcConversionGroup = {
  64. .circular = FALSE,
  65. .num_channels = (uint16_t)(ADC_NUM_CHANNELS),
  66. .cfgr = ADC_RESOLUTION,
  67. };
  68. palSetLineMode(pin, PAL_MODE_INPUT_ANALOG);
  69. switch (pin) {
  70. case B0:
  71. adcConversionGroup.smpr[2] = ADC_SMPR2_SMP_AN15(ADC_SAMPLING_RATE);
  72. adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(ADC_CHANNEL_IN15);
  73. sampleBuffer[0] = 0;
  74. break;
  75. case B1:
  76. adcConversionGroup.smpr[2] = ADC_SMPR2_SMP_AN16(ADC_SAMPLING_RATE);
  77. adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(ADC_CHANNEL_IN16);
  78. sampleBuffer[0] = 0;
  79. break;
  80. default:
  81. return 0;
  82. }
  83. adcStart(targetDriver, &adcCfg);
  84. if (adcConvert(targetDriver, &adcConversionGroup, &sampleBuffer[0], ADC_BUFFER_DEPTH) != MSG_OK) {
  85. return 0;
  86. }
  87. return *sampleBuffer;
  88. }
  89. void keyboard_post_init_kb(void) {
  90. // 1. The pin A5/B5 of the USB C interface in the left hand is connected to the pin A0 of MCU,
  91. // A0 will be set to output and write high when keyboard initial.
  92. // 2. The same pin in the right hand is connected to the pin B0 and B1 of MCU respectively,
  93. // and the ADC function of B0 and B1 will be enabled when keyboard initial.
  94. // 3. because the serial usart RXD and TXD is multiplexed on USB's D+ and D- in the right hand.
  95. // So detect the voltage on the pin A5/B5 of the USB C interface by ADC,
  96. // and disable USB connectivity when the ADC value exceeds 1000,
  97. // to avoid affecting the serial usart communication between the left hand and the right hand.
  98. if (is_keyboard_left()) {
  99. gpio_set_pin_output(A0);
  100. gpio_write_pin_high(A0);
  101. } else {
  102. if ((analogReadPin_my(B0) > 1000) || (analogReadPin_my(B1) > 1000)) {
  103. gpio_set_pin_input(A11);
  104. gpio_set_pin_input(A12);
  105. }
  106. }
  107. keyboard_post_init_user();
  108. }