ergodox_ez.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifdef LEFT_LEDS
  51. ergodox_left_led_1_on();
  52. _delay_ms(50);
  53. if (!mcp23018_status) {
  54. mcp23018_status = ergodox_left_leds_update();
  55. }
  56. ergodox_left_led_2_on();
  57. _delay_ms(50);
  58. if (!mcp23018_status) {
  59. mcp23018_status = ergodox_left_leds_update();
  60. }
  61. ergodox_left_led_3_on();
  62. _delay_ms(50);
  63. if (!mcp23018_status) {
  64. mcp23018_status = ergodox_left_leds_update();
  65. }
  66. #endif
  67. ergodox_right_led_1_off();
  68. _delay_ms(50);
  69. ergodox_right_led_2_off();
  70. _delay_ms(50);
  71. ergodox_right_led_3_off();
  72. #ifdef LEFT_LEDS
  73. _delay_ms(50);
  74. ergodox_left_led_1_off();
  75. if (!mcp23018_status) {
  76. mcp23018_status = ergodox_left_leds_update();
  77. }
  78. _delay_ms(50);
  79. ergodox_left_led_2_off();
  80. if (!mcp23018_status) {
  81. mcp23018_status = ergodox_left_leds_update();
  82. }
  83. _delay_ms(50);
  84. ergodox_left_led_3_off();
  85. if (!mcp23018_status) {
  86. mcp23018_status = ergodox_left_leds_update();
  87. }
  88. #endif
  89. //ergodox_led_all_on();
  90. //_delay_ms(333);
  91. ergodox_led_all_off();
  92. }
  93. uint8_t init_mcp23018(void) {
  94. mcp23018_status = 0x20;
  95. // I2C subsystem
  96. // uint8_t sreg_prev;
  97. // sreg_prev=SREG;
  98. // cli();
  99. if (i2c_initialized == 0) {
  100. i2c_init(); // on pins D(1,0)
  101. i2c_initialized = true;
  102. _delay_ms(1000);
  103. }
  104. // set pin direction
  105. // - unused : input : 1
  106. // - input : input : 1
  107. // - driving : output : 0
  108. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  109. mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out;
  110. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  111. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  112. i2c_stop();
  113. // set pull-up
  114. // - unused : on : 1
  115. // - input : on : 1
  116. // - driving : off : 0
  117. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  118. mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out;
  119. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  120. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  121. out:
  122. i2c_stop();
  123. #ifdef LEFT_LEDS
  124. if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
  125. #endif // LEFT_LEDS
  126. // SREG=sreg_prev;
  127. return mcp23018_status;
  128. }
  129. #ifdef LEFT_LEDS
  130. uint8_t ergodox_left_leds_update(void) {
  131. if (mcp23018_status) { // if there was an error
  132. return mcp23018_status;
  133. }
  134. #define LEFT_LED_1_SHIFT 7 // in MCP23018 port B
  135. #define LEFT_LED_2_SHIFT 6 // in MCP23018 port B
  136. #define LEFT_LED_3_SHIFT 7 // in MCP23018 port A
  137. // set logical value (doesn't matter on inputs)
  138. // - unused : hi-Z : 1
  139. // - input : hi-Z : 1
  140. // - driving : hi-Z : 1
  141. mcp23018_status = i2c_start(I2C_ADDR_WRITE);
  142. if (mcp23018_status) goto out;
  143. mcp23018_status = i2c_write(OLATA);
  144. if (mcp23018_status) goto out;
  145. mcp23018_status = i2c_write(0b11111111
  146. & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
  147. );
  148. if (mcp23018_status) goto out;
  149. mcp23018_status = i2c_write(0b11111111
  150. & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
  151. & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT)
  152. );
  153. if (mcp23018_status) goto out;
  154. out:
  155. i2c_stop();
  156. return mcp23018_status;
  157. }
  158. #endif
  159. #ifdef ONEHAND_ENABLE
  160. __attribute__ ((weak))
  161. // swap-hands action needs a matrix to define the swap
  162. const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  163. /* Left hand, matrix positions */
  164. {{0,13}, {1,13}, {2,13}, {3,13}, {4,13}, {5,13}},
  165. {{0,12}, {1,12}, {2,12}, {3,12}, {4,12}, {5,12}},
  166. {{0,11}, {1,11}, {2,11}, {3,11}, {4,11}, {5,11}},
  167. {{0,10}, {1,10}, {2,10}, {3,10}, {4,10}, {5,10}},
  168. {{0,9}, {1,9}, {2,9}, {3,9}, {4,9}, {5,9}},
  169. {{0,8}, {1,8}, {2,8}, {3,8}, {4,8}, {5,8}},
  170. {{0,7}, {1,7}, {2,7}, {3,7}, {4,7}, {5,7}},
  171. /* Right hand, matrix positions */
  172. {{0,6}, {1,6}, {2,6}, {3,6}, {4,6}, {5,6}},
  173. {{0,5}, {1,5}, {2,5}, {3,5}, {4,5}, {5,5}},
  174. {{0,4}, {1,4}, {2,4}, {3,4}, {4,4}, {5,4}},
  175. {{0,3}, {1,3}, {2,3}, {3,3}, {4,3}, {5,3}},
  176. {{0,2}, {1,2}, {2,2}, {3,2}, {4,2}, {5,2}},
  177. {{0,1}, {1,1}, {2,1}, {3,1}, {4,1}, {5,1}},
  178. {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}},
  179. };
  180. #endif