RCUSB4.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * kekse23.de RCUSB4 v1.1
  3. * Copyright (c) 2020, Nicholas Regitz
  4. *
  5. * Diese Datei ist Lizensiert unter der Creative Commons 4.0 CC BY-NC-SA
  6. * https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
  7. */
  8. #include <Joystick.h>
  9. #include "AVRPort23.h"
  10. #define CHAN1 D,0
  11. #define _INT1 0
  12. #define CHAN2 D,1
  13. #define _INT2 1
  14. #define CHAN3 D,3
  15. #define _INT3 3
  16. #define CHAN4 D,2
  17. #define _INT4 2
  18. #define RXLED B,0
  19. #define TXLED D,5
  20. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  21. 0, 0, // Button Count, Hat Switch Count
  22. true, true, false, // X, Y, Z
  23. true, true, false, // Rx, Ry, Rz
  24. false, false, // Rudder, Throttle
  25. false, false, false); // Accelerator, Brake, Steering
  26. void setup()
  27. {
  28. portMode(CHAN1, INPUT, HIGH);
  29. portMode(CHAN2, INPUT, HIGH);
  30. portMode(CHAN3, INPUT, HIGH);
  31. portMode(CHAN4, INPUT, HIGH);
  32. portMode(RXLED, OUTPUT, LOW);
  33. portMode(TXLED, OUTPUT, LOW);
  34. Joystick.begin();
  35. Joystick.setXAxisRange(2250, 750);
  36. Joystick.setYAxisRange(2250, 750);
  37. Joystick.setRxAxisRange(2250, 750);
  38. Joystick.setRyAxisRange(2250, 750);
  39. attachInterrupt(_INT1, isr1, CHANGE);
  40. attachInterrupt(_INT2, isr2, CHANGE);
  41. attachInterrupt(_INT3, isr3, CHANGE);
  42. attachInterrupt(_INT4, isr4, CHANGE);
  43. }
  44. volatile unsigned long Time[4];
  45. volatile unsigned int Value[4];
  46. volatile bool ValChanged[4];
  47. unsigned int NewValue[4];
  48. void loop()
  49. {
  50. if (ValChanged[0])
  51. {
  52. NewValue[0] = (NewValue[0]+Value[0])/2;
  53. Joystick.setXAxis(NewValue[0]);
  54. ValChanged[0] = false;
  55. }
  56. if (ValChanged[1])
  57. {
  58. NewValue[1] = (NewValue[1]+Value[1])/2;
  59. Joystick.setYAxis(NewValue[1]);
  60. ValChanged[1] = false;
  61. }
  62. if (ValChanged[2])
  63. {
  64. NewValue[2] = (NewValue[2]+Value[2])/2;
  65. Joystick.setRxAxis(NewValue[2]);
  66. ValChanged[2] = false;
  67. }
  68. if (ValChanged[3])
  69. {
  70. NewValue[3] = (NewValue[3]+Value[3])/2;
  71. Joystick.setRyAxis(NewValue[3]);
  72. ValChanged[3] = false;
  73. }
  74. delay(1);
  75. }
  76. void isr1()
  77. {
  78. if (portRead(CHAN1)) Time[0] = micros();
  79. else if (micros() > Time[0])
  80. {
  81. Value[0] = (Value[0]+(micros()-Time[0]))/2;
  82. ValChanged[0] = true;
  83. }
  84. }
  85. void isr2()
  86. {
  87. if (portRead(CHAN2)) Time[1] = micros();
  88. else if (micros() > Time[1])
  89. {
  90. Value[1] = (Value[1]+(micros()-Time[1]))/2;
  91. ValChanged[1] = true;
  92. }
  93. }
  94. void isr3()
  95. {
  96. if (portRead(CHAN3)) Time[2] = micros();
  97. else if (micros() > Time[2])
  98. {
  99. Value[2] = (Value[2]+(micros()-Time[2]))/2;
  100. ValChanged[2] = true;
  101. }
  102. }
  103. void isr4()
  104. {
  105. if (portRead(CHAN4)) Time[3] = micros();
  106. else if (micros() > Time[3])
  107. {
  108. Value[3] = (Value[3]+(micros()-Time[3]))/2;
  109. ValChanged[3] = true;
  110. }
  111. }