ergodox_ez.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include QMK_KEYBOARD_H
  2. #include "i2cmaster.h"
  3. extern inline void ergodox_board_led_on(void);
  4. extern inline void ergodox_right_led_1_on(void);
  5. extern inline void ergodox_right_led_2_on(void);
  6. extern inline void ergodox_right_led_3_on(void);
  7. extern inline void ergodox_right_led_on(uint8_t led);
  8. extern inline void ergodox_board_led_off(void);
  9. extern inline void ergodox_right_led_1_off(void);
  10. extern inline void ergodox_right_led_2_off(void);
  11. extern inline void ergodox_right_led_3_off(void);
  12. extern inline void ergodox_right_led_off(uint8_t led);
  13. extern inline void ergodox_led_all_on(void);
  14. extern inline void ergodox_led_all_off(void);
  15. extern inline void ergodox_right_led_1_set(uint8_t n);
  16. extern inline void ergodox_right_led_2_set(uint8_t n);
  17. extern inline void ergodox_right_led_3_set(uint8_t n);
  18. extern inline void ergodox_right_led_set(uint8_t led, uint8_t n);
  19. extern inline void ergodox_led_all_set(uint8_t n);
  20. bool i2c_initialized = 0;
  21. uint8_t mcp23018_status = 0x20;
  22. void matrix_init_kb(void) {
  23. // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
  24. TCCR1A = 0b10101001; // set and configure fast PWM
  25. TCCR1B = 0b00001001; // set and configure fast PWM
  26. // (tied to Vcc for hardware convenience)
  27. DDRB &= ~(1<<4); // set B(4) as input
  28. PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
  29. // unused pins - C7, D4, D5, D7, E6
  30. // set as input with internal pull-ip enabled
  31. DDRC &= ~(1<<7);
  32. DDRD &= ~(1<<5 | 1<<4);
  33. DDRE &= ~(1<<6);
  34. PORTC |= (1<<7);
  35. PORTD |= (1<<5 | 1<<4);
  36. PORTE |= (1<<6);
  37. ergodox_blink_all_leds();
  38. matrix_init_user();
  39. }
  40. void ergodox_blink_all_leds(void)
  41. {
  42. ergodox_led_all_off();
  43. ergodox_led_all_set(LED_BRIGHTNESS_HI);
  44. ergodox_right_led_1_on();
  45. _delay_ms(50);
  46. ergodox_right_led_2_on();
  47. _delay_ms(50);
  48. ergodox_right_led_3_on();
  49. _delay_ms(50);
  50. ergodox_right_led_1_off();
  51. _delay_ms(50);
  52. ergodox_right_led_2_off();
  53. _delay_ms(50);
  54. ergodox_right_led_3_off();
  55. //ergodox_led_all_on();
  56. //_delay_ms(333);
  57. ergodox_led_all_off();
  58. }
  59. uint8_t init_mcp23018(void) {
  60. mcp23018_status = 0x20;
  61. // I2C subsystem
  62. // uint8_t sreg_prev;
  63. // sreg_prev=SREG;
  64. // cli();
  65. if (i2c_initialized == 0) {
  66. i2c_init(); // on pins D(1,0)
  67. i2c_initialized = true;
  68. _delay_ms(1000);
  69. }
  70. // set pin direction
  71. // - unused : input : 1
  72. // - input : input : 1
  73. // - driving : output : 0
  74. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  75. mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out;
  76. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  77. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  78. i2c_stop();
  79. // set pull-up
  80. // - unused : on : 1
  81. // - input : on : 1
  82. // - driving : off : 0
  83. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  84. mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out;
  85. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  86. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  87. out:
  88. i2c_stop();
  89. // SREG=sreg_prev;
  90. return mcp23018_status;
  91. }
  92. #ifdef ONEHAND_ENABLE
  93. __attribute__ ((weak))
  94. // swap-hands action needs a matrix to define the swap
  95. const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  96. /* Left hand, matrix positions */
  97. {{0,13}, {1,13}, {2,13}, {3,13}, {4,13}, {5,13}},
  98. {{0,12}, {1,12}, {2,12}, {3,12}, {4,12}, {5,12}},
  99. {{0,11}, {1,11}, {2,11}, {3,11}, {4,11}, {5,11}},
  100. {{0,10}, {1,10}, {2,10}, {3,10}, {4,10}, {5,10}},
  101. {{0,9}, {1,9}, {2,9}, {3,9}, {4,9}, {5,9}},
  102. {{0,8}, {1,8}, {2,8}, {3,8}, {4,8}, {5,8}},
  103. {{0,7}, {1,7}, {2,7}, {3,7}, {4,7}, {5,7}},
  104. /* Right hand, matrix positions */
  105. {{0,6}, {1,6}, {2,6}, {3,6}, {4,6}, {5,6}},
  106. {{0,5}, {1,5}, {2,5}, {3,5}, {4,5}, {5,5}},
  107. {{0,4}, {1,4}, {2,4}, {3,4}, {4,4}, {5,4}},
  108. {{0,3}, {1,3}, {2,3}, {3,3}, {4,3}, {5,3}},
  109. {{0,2}, {1,2}, {2,2}, {3,2}, {4,2}, {5,2}},
  110. {{0,1}, {1,1}, {2,1}, {3,1}, {4,1}, {5,1}},
  111. {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}},
  112. };
  113. #endif