ez.c 3.3 KB

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