use the joystick library
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -3,6 +3,10 @@
 | 
				
			|||||||
BOARD_TAG    = leonardo
 | 
					BOARD_TAG    = leonardo
 | 
				
			||||||
MONITOR_PORT = /dev/ttyACM0
 | 
					MONITOR_PORT = /dev/ttyACM0
 | 
				
			||||||
ARDUINO_DIR = /home/dan/projects/arduino-1.8.9
 | 
					ARDUINO_DIR = /home/dan/projects/arduino-1.8.9
 | 
				
			||||||
ARDUINO_LIBS = HID Mouse
 | 
					ARDUINO_LIBS = HID Mouse Joystick
 | 
				
			||||||
 | 
					CTAGS_EXEC = /usr/bin/ctags
 | 
				
			||||||
 | 
					CTAGS_OPTS = -e
 | 
				
			||||||
 | 
					ARDMK_DIR = /home/dan/projects/avr/Arduino-Makefile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
include /home/dan/projects/avr/Arduino-Makefile/Arduino.mk
 | 
					include /home/dan/projects/avr/Arduino-Makefile/Arduino.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										57
									
								
								joystick.ino
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								joystick.ino
									
									
									
									
									
								
							@@ -1,21 +1,51 @@
 | 
				
			|||||||
 | 
					/* We use the Joystick library from:
 | 
				
			||||||
 | 
					   <https://github.com/MHeironimus/ArduinoJoystickLibrary> */
 | 
				
			||||||
 | 
					#include <Joystick.h>
 | 
				
			||||||
#include <Mouse.h>
 | 
					#include <Mouse.h>
 | 
				
			||||||
#define DELAY 5
 | 
					#define DELAY 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const int sw_pin = 8;
 | 
					const int sw_pin = 2;
 | 
				
			||||||
const int vrx_pin = A0;
 | 
					const int vrx_pin = A0;
 | 
				
			||||||
const int vry_pin = A1;
 | 
					const int vry_pin = A1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UP, RIGHT, DOWN, LEFT, additional left, additional right
 | 
				
			||||||
 | 
					const int btn_count = 6;
 | 
				
			||||||
 | 
					const int btn_pins[] = { 3,4,5,6,7,8 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const int base_x = 512;
 | 
					const int base_x = 512;
 | 
				
			||||||
const int base_y = 519;
 | 
					const int base_y = 519;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const int max_x = 1023;
 | 
					const int max_x = 1023;
 | 
				
			||||||
const int max_y = 1023;
 | 
					const int max_y = 1023;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const int rng = 14;
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const int rng = 500;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mouse_mode = 0;
 | 
				
			||||||
 | 
					int prev_readings[btn_count];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
 | 
				
			||||||
 | 
					                   JOYSTICK_TYPE_GAMEPAD,
 | 
				
			||||||
 | 
					                   btn_count, // buttons
 | 
				
			||||||
 | 
					                   1, // joystick hat switches
 | 
				
			||||||
 | 
					                   true, // X axis
 | 
				
			||||||
 | 
					                   true, // Y axis
 | 
				
			||||||
 | 
					                   false,  // Z axis
 | 
				
			||||||
 | 
					                   false, false, false, false, false, false, false, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void setup() {                
 | 
					void setup() {                
 | 
				
			||||||
  pinMode(sw_pin, INPUT_PULLUP);
 | 
					  pinMode(sw_pin, INPUT);
 | 
				
			||||||
 | 
					  for (int i = 0; i < btn_count; i++) {
 | 
				
			||||||
 | 
					    pinMode(btn_pins[i], INPUT);
 | 
				
			||||||
 | 
					    prev_readings[i] = 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  Mouse.begin();
 | 
					  Mouse.begin();
 | 
				
			||||||
 | 
					  Joystick.begin();
 | 
				
			||||||
 | 
					  Joystick.setXAxisRange(-rng, rng);
 | 
				
			||||||
 | 
					  Joystick.setYAxisRange(-rng, rng);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int prev_clicked = 0;
 | 
					int prev_clicked = 0;
 | 
				
			||||||
@@ -36,18 +66,33 @@ int read_direction(int analog_pin, int base, int max, int rng) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void loop() {
 | 
					void loop() {
 | 
				
			||||||
  int click = get_clicked();
 | 
					  /*int click = get_clicked();
 | 
				
			||||||
  if (click) {
 | 
					  if (click) {
 | 
				
			||||||
    Mouse.click();
 | 
					  Mouse.click();}*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  for (int i = 0; i < btn_count; i++) {
 | 
				
			||||||
 | 
					    int pin = btn_pins[i];
 | 
				
			||||||
 | 
					    int signal = digitalRead(pin);
 | 
				
			||||||
 | 
					    if (signal != prev_readings[i]) {
 | 
				
			||||||
 | 
					      Joystick.setButton(i, signal);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  int dirx = read_direction(vrx_pin, base_x, max_x, rng);
 | 
					    prev_readings[i] = signal;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Analogue input
 | 
				
			||||||
 | 
					  int dirx = -read_direction(vrx_pin, base_x, max_x, rng);
 | 
				
			||||||
  int diry = read_direction(vry_pin, base_y, max_y, rng);
 | 
					  int diry = read_direction(vry_pin, base_y, max_y, rng);
 | 
				
			||||||
 | 
					  if (mouse_mode) {
 | 
				
			||||||
    if (diry != 0) {
 | 
					    if (diry != 0) {
 | 
				
			||||||
      Mouse.move(0, diry, 0);
 | 
					      Mouse.move(0, diry, 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (dirx != 0) {
 | 
					    if (dirx != 0) {
 | 
				
			||||||
      Mouse.move(dirx, 0, 0);
 | 
					      Mouse.move(dirx, 0, 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    Joystick.setXAxis (dirx);
 | 
				
			||||||
 | 
					    Joystick.setYAxis (diry);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  delay(DELAY);
 | 
					  delay(DELAY);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user