joystiic.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. enum {
  33. JOYSTIIC_LEFT,
  34. JOYSTIIC_RIGHT,
  35. JOYSTIIC_UP,
  36. JOYSTIIC_DOWN,
  37. JOYSTIIC_PRESS
  38. };
  39. bool joystiic_triggered[5] = {0};
  40. void joystiic_init(void) {
  41. i2c_init();
  42. i2c_start(JOYSTIIC_DEFAULT_ADDR);
  43. }
  44. void joystiic_update(uint16_t horizontal, uint16_t vertical, bool button) {
  45. joystiic_update_kb(horizontal, vertical, button);
  46. joystiic_update_user(horizontal, vertical, button);
  47. }
  48. void joystiic_update_kb(uint16_t horizontal, uint16_t vertical, bool button) {
  49. }
  50. void joystiic_update_user(uint16_t horizontal, uint16_t vertical, bool button) {
  51. }
  52. void joystiic_trigger(uint8_t trigger, bool active) {
  53. joystiic_trigger_kb(trigger, active);
  54. joystiic_trigger_user(trigger, active);
  55. }
  56. void joystiic_trigger_kb(uint8_t trigger, bool active) {
  57. switch (trigger) {
  58. case JOYSTIIC_LEFT: active ? register_code(KC_L) : unregister_code(KC_L); break;
  59. }
  60. }
  61. void joystiic_trigger_user(uint8_t trigger, bool active) {
  62. }
  63. void joystiic_task(void) {
  64. // get horizontal axis
  65. joystiic_tx[0] = JOYSTIIC_COMMAND_HORIZONTAL;
  66. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  67. joystiic_tx, 1,
  68. joystiic_rx_horizontal, 2
  69. )) {
  70. printf("error hori\n");
  71. }
  72. joystiic_horizontal = ((uint16_t)joystiic_rx_horizontal[0] << 8) | joystiic_rx_horizontal[1];
  73. if (joystiic_horizontal > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)) {
  74. if (!joystiic_triggered[JOYSTIIC_LEFT]) {
  75. joystiic_triggered[JOYSTIIC_LEFT] = true;
  76. joystiic_trigger(JOYSTIIC_LEFT, true);
  77. }
  78. } else {
  79. if (joystiic_triggered[JOYSTIIC_LEFT]) {
  80. joystiic_triggered[JOYSTIIC_LEFT] = false;
  81. joystiic_trigger(JOYSTIIC_LEFT, false);
  82. }
  83. }
  84. if (joystiic_horizontal < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)) {
  85. if (!joystiic_triggered[JOYSTIIC_RIGHT]) {
  86. joystiic_triggered[JOYSTIIC_RIGHT] = true;
  87. joystiic_trigger(JOYSTIIC_RIGHT, true);
  88. }
  89. } else {
  90. if (joystiic_triggered[JOYSTIIC_RIGHT]) {
  91. joystiic_triggered[JOYSTIIC_RIGHT] = false;
  92. joystiic_trigger(JOYSTIIC_RIGHT, false);
  93. }
  94. }
  95. // get vertical axis
  96. joystiic_tx[0] = JOYSTIIC_COMMAND_VERTICAL;
  97. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  98. joystiic_tx, 1,
  99. joystiic_rx_vertical, 2
  100. )) {
  101. printf("error vert\n");
  102. }
  103. joystiic_vertical = ((uint16_t)joystiic_rx_vertical[0] << 8) | joystiic_rx_vertical[1];
  104. if (joystiic_vertical > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)) {
  105. if (!joystiic_triggered[JOYSTIIC_UP]) {
  106. joystiic_triggered[JOYSTIIC_UP] = true;
  107. joystiic_trigger(JOYSTIIC_UP, true);
  108. }
  109. } else {
  110. if (joystiic_triggered[JOYSTIIC_UP]) {
  111. joystiic_triggered[JOYSTIIC_UP] = false;
  112. joystiic_trigger(JOYSTIIC_UP, false);
  113. }
  114. }
  115. if (joystiic_vertical < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)) {
  116. if (!joystiic_triggered[JOYSTIIC_DOWN]) {
  117. joystiic_triggered[JOYSTIIC_DOWN] = true;
  118. joystiic_trigger(JOYSTIIC_DOWN, true);
  119. }
  120. } else {
  121. if (joystiic_triggered[JOYSTIIC_DOWN]) {
  122. joystiic_triggered[JOYSTIIC_DOWN] = false;
  123. joystiic_trigger(JOYSTIIC_DOWN, false);
  124. }
  125. }
  126. // get button press
  127. joystiic_tx[0] = JOYSTIIC_COMMAND_BUTTON;
  128. if (MSG_OK != i2c_transmit_receive(JOYSTIIC_DEFAULT_ADDR << 1,
  129. joystiic_tx, 1,
  130. joystiic_rx_button, 1
  131. )) {
  132. printf("error vert\n");
  133. }
  134. joystiic_button = joystiic_rx_button[0];
  135. if (joystiic_button) {
  136. if (!joystiic_triggered[JOYSTIIC_PRESS]) {
  137. joystiic_triggered[JOYSTIIC_PRESS] = true;
  138. joystiic_trigger(JOYSTIIC_PRESS, true);
  139. }
  140. } else {
  141. if (joystiic_triggered[JOYSTIIC_PRESS]) {
  142. joystiic_triggered[JOYSTIIC_PRESS] = false;
  143. joystiic_trigger(JOYSTIIC_PRESS, false);
  144. }
  145. }
  146. joystiic_update(joystiic_horizontal, joystiic_vertical, joystiic_button);
  147. //printf("%d\n", joystiic[0]);
  148. // SEND_STRING("H: ");
  149. // send_word(joystiic_rx_horizontal[0]);
  150. // tap_code(KC_SPACE);
  151. // send_word(joystiic_rx_horizontal[1]);
  152. // tap_code(KC_SPACE);
  153. }