Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. /*
  2. * tinkerBOY's usb_gamepad v1.0
  3. *
  4. */
  5.  
  6. #include <Joystick.h>
  7.  
  8. #define PINS 5
  9. #define ENABLE_ANALOG1 true
  10.  
  11.  
  12. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, PINS, 0,
  13. true, true, false, false, false, false, false, false, false, false, false);
  14.  
  15. class CButton {
  16. public:
  17. int pin = NULL;
  18. int lastState = 0;
  19.  
  20. CButton(int p) {
  21. pin = p;
  22. }
  23. };
  24.  
  25. CButton Buttons[PINS] ={0,1,2,3,4};
  26.  
  27. void setup() {
  28. for(int i=0 ; i<PINS ;i++) {
  29. pinMode(Buttons[i].pin, INPUT_PULLUP);
  30. }
  31.  
  32. Joystick.begin();
  33. Joystick.setXAxisRange(-512, 512);
  34. Joystick.setYAxisRange(-512, 512);
  35. }
  36.  
  37. void JButtonStates() {
  38.  
  39. // Read the state of the 4 joystick direction switches and present as analog stick movement to the host
  40. // I don't know how to incorporate these into the loop below :(
  41. // Joystick.setXAxis(digitalRead(1) * 512 - 512);
  42. // Joystick.setXAxis(!digitalRead(2) * 512);
  43. // Joystick.setYAxis(digitalRead(3) * 512 - 512);
  44. // Joystick.setYAxis(!digitalRead(4) * 512);
  45.  
  46.  
  47. // for (int i = 0; i < PINS; i++) {
  48. int currentState = !digitalRead(Buttons[0].pin);
  49. if (currentState != Buttons[0].lastState) {
  50. Joystick.setButton(0, currentState);
  51. Buttons[0].lastState = currentState;
  52. }
  53. }
  54. //}
  55.  
  56. void loop() {
  57. JButtonStates();
  58. delay(50);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement