Initial commit

This commit is contained in:
Diogo Cordeiro
2018-04-19 21:26:42 +01:00
commit 723f64f8b8
14 changed files with 1457 additions and 0 deletions

34
driver/dist/README.TXT vendored Normal file
View File

@@ -0,0 +1,34 @@
sudo apt install librxtx-java
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "basicJoystickInputDeviceDriver.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.

BIN
driver/dist/basicJoystickInputDeviceDriver.jar vendored Executable file

Binary file not shown.

BIN
driver/dist/lib/RXTXcomm.jar vendored Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,93 @@
/*
* Basic Joystick Input Device - a very simple and cool way of getting to know how a joystick works underneath the hood.
* Copyright (C) 2018, Diogo Cordeiro.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Driver
* @package Basic Joystick Input Device
* @author Diogo Cordeiro <up201705417@fc.up.pt>
* @copyright 2018 Diogo Cordeiro.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://www.diogo.site/projects/basic-joystick-input-device/
*/
package basicJoystickInputDeviceDriver;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
/**
* serialPort.java
*
* Diogo Cordeiro
* up201705417@fc.up.pt
*
* Allows a Java Application to interface with an arduino board by means of
* the serial port.
*/
public class
Handle
{
public
Handle(String button)
{
try
{
Robot robot = new Robot();
// Interfacing with the app
switch (button)
{
case "up":
System.out.println("Para cima: " + button);
robot.keyPress(KeyEvent.VK_KP_UP);
robot.keyRelease(KeyEvent.VK_KP_UP);
break;
case "right":
System.out.println("Para a direita: " + button);
robot.keyPress(KeyEvent.VK_KP_RIGHT);
robot.keyRelease(KeyEvent.VK_KP_RIGHT);
break;
case "down":
System.out.println("Para baixo: " + button);
robot.keyPress(KeyEvent.VK_KP_DOWN);
robot.keyRelease(KeyEvent.VK_KP_DOWN);
break;
case "left":
System.out.println("Para a esquerda: " + button);
robot.keyPress(KeyEvent.VK_KP_LEFT);
robot.keyRelease(KeyEvent.VK_KP_LEFT);
break;
case "click":
System.out.println("Clique: " + button);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
break;
default:
System.out.println("Leitura do input analogico atual: " + button);
}
}
catch(AWTException e)
{
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,205 @@
/*
* Basic Joystick Input Device - a very simple and cool way of getting to know how a joystick works underneath the hood.
* Copyright (C) 2018, Diogo Cordeiro.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Driver
* @package Basic Joystick Input Device
* @author Diogo Cordeiro <up201705417@fc.up.pt>
* @copyright 2018 Diogo Cordeiro.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://www.diogo.site/projects/basic-joystick-input-device/
*/
/*****************************************************************************
* NOTICE *
* This file was originally written for a IR Controller project I did, thus *
* the different indent style from the remaining codebase *
*****************************************************************************/
package basicJoystickInputDeviceDriver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.util.Enumeration;
/**
* serialPort.java
*
* Diogo Cordeiro
* diogo@fc.up.pt
*
* Allows a Java Application to interface with an arduino board by means of
* the serial port.
*/
public class Main implements SerialPortEventListener
{
SerialPort serialPort;
// The port we're normally going to use
private static final String PORT_NAMES[] =
{
"/dev/ttyACM0", // Raspberry Pi
"/dev/ttyUSB0", // Linux
"COM3", // Windows
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/cu.usbmodem1411",
"/dev/cu.usbmodem1451",
};
// A BufferedReader which will be fed by a InputStreamReader
// converting the bytes into characters
// making the displayed results codepage independent
private BufferedReader input;
// The output stream to the port
private OutputStream output;
// Milliseconds to block while waiting for port open
private static final int TIME_OUT = 2000;
// Default bits per second for COM port
private static final int DATA_RATE = 9600;
public void initialize()
{
System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// First, Find an instance of serial port as set in PORT_NAMES
while (portEnum.hasMoreElements())
{
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES)
{
if (currPortId.getName().equals(portName))
{
portId = currPortId;
break;
}
}
}
if (portId == null)
{
System.out.println("Could not find COM port.");
return;
}
try
{
// open serial port, and use class name for the appName
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE, //Transfer rate of the serial port
SerialPort.DATABITS_8, //rate of 10 bits 8 (sending)
SerialPort.STOPBITS_1, //rate of 10 bits 1 (receiver)
SerialPort.PARITY_NONE); //receive and send data
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close()
{
if (serialPort != null)
{
serialPort.removeEventListener();
serialPort.close();
}
}
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent)
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try
{
String inputLine=null;
if (input.ready())
{
inputLine = input.readLine();
Handle handle = new Handle(inputLine);
}
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones
}
/**
* Sends data to the serial port.
* @param data - Value to be sent
*/
public void sendData(int data)
{
try
{
output.write(data); // writes value on the serial port to be sent
}
catch (IOException e)
{
System.err.println(e.toString());
}
}
public static void main(String[] args) throws Exception
{
Main main = new Main();
main.initialize();
Thread t=new Thread()
{
public void run()
{
// the following line will keep this app alive for
// 1000 seconds, waiting for events to occur and
// responding to them (printing incoming messages
// to console)
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}

Binary file not shown.