joystiic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2018 Jack Humbert <jack.humb@gmail.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 "joystiic.h"
  17. #include "print.h"
  18. #include "action.h"
  19. #define JOYSTIIC_DEFAULT_ADDR 0x20
  20. #define JOYSTIIC_COMMAND_HORIZONTAL 0x00
  21. #define JOYSTIIC_COMMAND_VERTICAL 0x02
  22. #define JOYSTIIC_COMMAND_BUTTON 0x04
  23. #define JOYSTIIC_CENTER 512
  24. #define JOYSTIIC_DEADZONE 200
  25. uint16_t joystiic_horizontal;
  26. uint16_t joystiic_vertical;
  27. bool joystiic_button;
  28. uint8_t joystiic_tx[1];
  29. uint8_t joystiic_rx_horizontal[2];
  30. uint8_t joystiic_rx_vertical[2];
  31. uint8_t joystiic_rx_button[1];
  32. bool joystiic_triggered[5] = {0};
  33. void joystiic_init(void) {
  34. i2c_init();
  35. i2c_start(JOYSTIIC_DEFAULT_ADDR);
  36. }
  37. void joystiic_update(uint16_t horizontal, uint16_t vertical, bool button) {
  38. joystiic_update_kb(horizontal, vertical, button);
  39. joystiic_update_user(horizontal, vertical, button);
  40. }
  41. __attribute__ ((weak))
  42. void joystiic_update_kb(uint16_t horizontal, uint16_t vertical, bool button) { }
  43. __attribute__ ((weak))
  44. void joystiic_update_user(uint16_t horizontal, uint16_t vertical, bool button) { }
  45. void joystiic_trigger(uint8_t trigger, bool active) {
  46. joystiic_trigger_kb(trigger, active);
  47. joystiic_trigger_user(trigger, active);
  48. }
  49. __attribute__ ((weak))
  50. void joystiic_trigger_kb(uint8_t trigger, bool active) { }
  51. __attribute__ ((weak))
  52. void joystiic_trigger_user(uint8_t trigger, bool active) { }
  53. void joystiic_trigger_if_not(uint8_t trigger, bool active) {
  54. if (joystiic_triggered[trigger] != active) {
  55. joystiic_triggered[trigger] = active;
  56. joystiic_trigger(trigger, active);
  57. }
  58. }
  59. void joystiic_task(void) {
  60. // get horizontal axis
  61. joystiic_tx[0] = JOYSTIIC_COMMAND_HORIZONTAL;
  62. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  63. joystiic_tx, 1,
  64. joystiic_rx_horizontal, 2
  65. )) {
  66. printf("error hori\n");
  67. }
  68. joystiic_horizontal = ((uint16_t)joystiic_rx_horizontal[0] << 8) | joystiic_rx_horizontal[1];
  69. joystiic_trigger_if_not(JOYSTIIC_LEFT, joystiic_horizontal > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE));
  70. joystiic_trigger_if_not(JOYSTIIC_RIGHT, joystiic_horizontal < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE));
  71. // get vertical axis
  72. joystiic_tx[0] = JOYSTIIC_COMMAND_VERTICAL;
  73. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  74. joystiic_tx, 1,
  75. joystiic_rx_vertical, 2
  76. )) {
  77. printf("error vert\n");
  78. }
  79. joystiic_vertical = ((uint16_t)joystiic_rx_vertical[0] << 8) | joystiic_rx_vertical[1];
  80. joystiic_trigger_if_not(JOYSTIIC_UP, joystiic_vertical > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE));
  81. joystiic_trigger_if_not(JOYSTIIC_DOWN, joystiic_vertical < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE));
  82. // get button press
  83. joystiic_tx[0] = JOYSTIIC_COMMAND_BUTTON;
  84. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  85. joystiic_tx, 1,
  86. joystiic_rx_button, 1
  87. )) {
  88. printf("error vert\n");
  89. }
  90. joystiic_button = !joystiic_rx_button[0];
  91. joystiic_trigger_if_not(JOYSTIIC_PRESS, joystiic_button);
  92. joystiic_update(joystiic_horizontal, joystiic_vertical, joystiic_button);
  93. //printf("%d\n", joystiic[0]);
  94. // SEND_STRING("H: ");
  95. // send_word(joystiic_rx_horizontal[0]);
  96. // tap_code(KC_SPACE);
  97. // send_word(joystiic_rx_horizontal[1]);
  98. // tap_code(KC_SPACE);
  99. }