Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 5ac9f18

Browse files
author
4nthonylin
committed
Added files for Joystick control Processing/Arduino
1 parent af3905a commit 5ac9f18

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Joystick.h - Library for reading and parsing Serial data from Processing.
3+
Created by Anthony Lin, November 11, 2013.
4+
*/
5+
6+
#include <Arduino.h>
7+
#include "Joystick.h"
8+
9+
Joystick::Joystick()
10+
{
11+
sync = '!';
12+
}
13+
14+
int Joystick::getX1(){
15+
return x1;
16+
}
17+
int Joystick::getX2(){
18+
return x2;
19+
}
20+
int Joystick::getY1(){
21+
return y1;
22+
}
23+
24+
int Joystick::getY2(){
25+
return y2;
26+
}
27+
28+
int Joystick::getButton1(){
29+
return button1;
30+
}
31+
32+
int Joystick::getButton2(){
33+
return button2;
34+
}
35+
36+
boolean Joystick::buttonPressed(int bn){
37+
bn = bn-1;
38+
constrain(bn, 0, 15);
39+
if(bn < 8){
40+
if(bitRead(button1, map(bn, 0, 7, 7, 0)) == 1) return true;
41+
else return false;
42+
}
43+
else{
44+
if(bitRead(button2, map(bn, 8, 15, 7, 0)) == 1) return true;
45+
else return false;
46+
}
47+
}
48+
49+
void Joystick::update(){
50+
byte temp[PACKETLENGTH];
51+
if(Serial.available() > PACKETLENGTH && Serial.read() == sync){
52+
for(int x = 0; x < PACKETLENGTH; x++) temp[x] = Serial.read();
53+
y1 = temp[0];
54+
x1 = temp[1];
55+
y2 = temp[2];
56+
x2 = temp[3];
57+
button1 = temp[4];
58+
button2 = temp[5];
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Joystick.h - Library for reading and parsing Serial data from Processing.
3+
Created by Anthony Lin, November 11, 2013.
4+
*/
5+
6+
#ifndef Joystick_h
7+
#define Joystick_h
8+
#include <Arduino.h>
9+
10+
11+
#define PACKETLENGTH 6
12+
13+
class Joystick{
14+
public:
15+
Joystick();
16+
int getX1();
17+
int getX2();
18+
int getY1();
19+
int getY2();
20+
int getButton1();
21+
int getButton2();
22+
boolean buttonPressed(int bn);
23+
void update();
24+
public:
25+
int x1;
26+
int x2;
27+
int y1;
28+
int y2;
29+
byte button1;
30+
byte button2;
31+
private:
32+
byte sync;
33+
};
34+
35+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Example sketch for Joystick library, used in conjunction with Processing Sketch
3+
4+
Author: Anthony Lin
5+
*/
6+
7+
#include <Joystick.h> //import Joystick library
8+
9+
Joystick controller; //initialized a new joystick object
10+
11+
int x1, x2, y1, y2; //Variables for holding stick values
12+
byte button1, button2; //Variables for holding button bytes
13+
14+
void setup(){
15+
Serial.begin(9600); //Have to call Serial.begin()
16+
Serial.print("Initialized");
17+
pinMode(13, OUTPUT);
18+
}
19+
20+
void loop(){
21+
controller.update(); //Must call this in void loop() to update Joystick values
22+
y1 = controller.getY1(); //Gets stick values
23+
x1 = controller.getX1();
24+
y2 = controller.getY2();
25+
x2 = controller.getX2();
26+
button1 = controller.getButton1(); //Gets button bytes
27+
button2 = controller.getButton2();
28+
29+
if(controller.buttonPressed(0)) digitalWrite(13, HIGH); //Returns true of button is pressed
30+
else digitalWrite(13, LOW);
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#######################################
2+
# Syntax Coloring Map Servo
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Joystick KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
getX1 KEYWORD2
15+
getX2 KEYWORD2
16+
getY1 KEYWORD2
17+
getY2 KEYWORD2
18+
getButton1 KEYWORD2
19+
getButton2 KEYWORD2
20+
buttonPressed KEYWORD2
21+
update KEYWORD2
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################

0 commit comments

Comments
 (0)