Re-implemented PSP joystick as dedicated class
This commit is contained in:
parent
59eeabc785
commit
c83891d765
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Serial Adapter Project: Dynamic serial TTY passthroughs
|
||||
Serial Adapter Project: Dynamic serial TTY passthroughs
|
||||
by: Mike Crosson
|
||||
Nusku Networks
|
||||
date: 2013/03/09
|
||||
|
@ -8,3 +8,58 @@
|
|||
attribute.
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Project.h"
|
||||
#include "UIJoystickPSP.h"
|
||||
|
||||
UIJoystickPSP::UIJoystickPSP(int xAxisPin, int yAxisPin) {
|
||||
this->xAxisPin = xAxisPin;
|
||||
this->yAxisPin = yAxisPin;
|
||||
|
||||
upCount = 0;
|
||||
downCount = 0;
|
||||
leftCount = 0;
|
||||
rightCount = 0;
|
||||
|
||||
previousDirection = joyNone;
|
||||
}
|
||||
|
||||
joyDirection UIJoystickPSP::direction() {
|
||||
// Read the x/y values from the joystick
|
||||
xAxisValue = map(analogRead(xAxisPin), 0, 1023, 0, 10);
|
||||
yAxisValue = map(analogRead(yAxisPin), 0, 1023, 0, 10);
|
||||
|
||||
if (yAxisValue > 6 ) {
|
||||
upCount++;
|
||||
if (upCount > 768) {
|
||||
upCount = 0;
|
||||
return joyUp;
|
||||
}
|
||||
}
|
||||
|
||||
if (yAxisValue < 4 ) {
|
||||
downCount++;
|
||||
if (downCount > 768) {
|
||||
downCount = 0;
|
||||
return joyDown;
|
||||
}
|
||||
}
|
||||
|
||||
if (xAxisValue > 6) {
|
||||
leftCount++;
|
||||
if (leftCount > 768) {
|
||||
leftCount = 0;
|
||||
return joyLeft;
|
||||
}
|
||||
}
|
||||
|
||||
if (xAxisValue < 4) {
|
||||
rightCount++;
|
||||
if (rightCount > 768) {
|
||||
rightCount = 0;
|
||||
return joyRight;
|
||||
}
|
||||
}
|
||||
|
||||
return joyNone;
|
||||
}
|
|
@ -8,46 +8,28 @@
|
|||
attribute.
|
||||
*/
|
||||
|
||||
// PSP joystick tracking
|
||||
int pspUpCount = 0; // Used to slow down how fast we repeat up movement
|
||||
int pspDownCount = 0; // Used to slow down how fast we repeat down movement
|
||||
int pspXAxisValue;
|
||||
int pspYAxisValue;
|
||||
#include "Project.h"
|
||||
|
||||
// void readPSPValues() {
|
||||
// // Read the x/y values from the joystick
|
||||
// pspXAxisValue=map(analogRead(pspXPin), 0, 1023, 0, 10);
|
||||
// pspYAxisValue=map(analogRead(pspYPin), 0, 1023, 0, 10);
|
||||
enum joyDirection {
|
||||
joyUp,
|
||||
joyDown,
|
||||
joyLeft,
|
||||
joyRight,
|
||||
joyNone=-1
|
||||
};
|
||||
|
||||
// // Move cursor Up
|
||||
// if (pspYAxisValue > 6 ) {
|
||||
// pspUpCount++;
|
||||
// if (pspUpCount > 768) {
|
||||
// pspUpCount = 0;
|
||||
// serialmode newMode = (serialmode)(selectedMode - 1);
|
||||
// if (newMode >= 0) {
|
||||
// //FIXME: Re-enable once new LCD is online
|
||||
// //setSelection(newMode);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// pspUpCount = 0;
|
||||
// }
|
||||
class UIJoystickPSP {
|
||||
private:
|
||||
int xAxisPin, yAxisPin; // Pins in use
|
||||
int xAxisValue, yAxisValue; // Values read from pins
|
||||
int upCount, downCount, leftCount, rightCount; // Counts to slow down repeat
|
||||
|
||||
// // Move cursor Down
|
||||
// if (pspYAxisValue < 4 ) {
|
||||
// pspDownCount++;
|
||||
// if (pspDownCount > 768) {
|
||||
// serialmode newMode = (serialmode)(selectedMode + 1);
|
||||
// if (newMode <= modelinespeed) {
|
||||
// //FIXME: Re-enable once new LCD is online
|
||||
// //setSelection(newMode);
|
||||
// }
|
||||
// pspDownCount = 0;
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// pspDownCount = 0;
|
||||
// }
|
||||
// }
|
||||
joyDirection previousDirection;
|
||||
|
||||
// int readXAxisValue();
|
||||
// int readYAxisValue();
|
||||
|
||||
public:
|
||||
UIJoystickPSP(int xAxisPin, int yAxisPin);
|
||||
joyDirection direction();
|
||||
};
|
|
@ -9,9 +9,12 @@
|
|||
*/
|
||||
|
||||
#include "Project.h"
|
||||
#include "UIButton.h"
|
||||
#include "UIJoystickPSP.h"
|
||||
|
||||
UIButton* okButton;
|
||||
UIButton* cancelButton;
|
||||
UIJoystickPSP* pspJoystick;
|
||||
|
||||
// Defaults
|
||||
void setDefaults() {
|
||||
|
@ -30,14 +33,13 @@ void setup() {
|
|||
|
||||
okButton = new UIButton(okButtonPin, okButtonLed);
|
||||
cancelButton = new UIButton(cancelButtonPin, cancelButtonLed);
|
||||
pspJoystick = new UIJoystickPSP(pspXPin, pspYPin);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (okButton->isPressed()) {
|
||||
Serial.println("OK Button Pressed");
|
||||
}
|
||||
|
||||
if (cancelButton->isPressed()) {
|
||||
Serial.println("Cancel button pressed");
|
||||
int direction = pspJoystick->direction();
|
||||
if (direction != joyNone) {
|
||||
Serial.print("Joystick Direction: ");
|
||||
Serial.println(direction);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue