# HG changeset patch # User ark # Date 1485388122 -3600 # Node ID f31cf317518900c0d0ed62bfdec0f640c0f3ca9d v1.0 diff -r 000000000000 -r f31cf3175189 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Jan 26 00:48:42 2017 +0100 @@ -0,0 +1,1 @@ +.DS_Store diff -r 000000000000 -r f31cf3175189 Joystick.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Joystick.cpp Thu Jan 26 00:48:42 2017 +0100 @@ -0,0 +1,93 @@ +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#include "Joystick.h" + + +/** + see Joystick.h for description. +*/ + +Joystick::Joystick() {} + +/** + setup joystick instance + + @param {byte} pinX input pin for x axis + @param {byte} pinY input pin for y axis + @param {byte} pinB input pin for button +*/ +void Joystick::setup(byte pinX, byte pinY, byte pinB) { + this->setup(pinX, pinY, pinB, true); +} + + +/** + setup joystick instance + + @param {byte} pinX input pin for x axis + @param {byte} pinY input pin for y axis + @param {byte} pinB input pin for button + @param {bool} flipY mirror y axis output +*/ +void Joystick::setup(byte pinX, byte pinY, byte pinB, bool flipY) { + this->x.setup(pinX); + this->y.setup(pinY); + this->b.setup(pinB, true); + this->centerX = 512; + this->centerY = 512; + this->flipY = flipY; +} + + +void Joystick::update() { + this->update(millis()); +} + + +void Joystick::update(unsigned long ms) { + this->x.update(ms); + this->y.update(ms); + this->b.update(ms); + float vx = 0; + float vy = 0; + // if not calibrated yet, and stick is stopped within the center deadzone, + // auto-calibrate to that point + if(!this->isCalibrated) { + bool inDeadzone = abs(512-this->x.value) < Joystick::CENTER_DEADZONE && + abs(512-this->y.value) < Joystick::CENTER_DEADZONE; + if(inDeadzone) { + if(this->x.isStopped && this->y.isStopped) { + this->isCalibrated = true; + this->centerX = this->x.value; + this->centerY = this->y.value; + #ifdef DEBUG + Serial.print("Joystick calibrated, center="); + Serial.print(this->centerX); + Serial.print(","); + Serial.println(this->centerY); + #endif + } + else return; + } + } + vx = this->x.value - this->centerX; + if(vx < 0) + this->vx = vx*100/this->centerX; + else + this->vx = vx*100/(1023-this->centerX); + vy = this->y.value - this->centerY; + if(vy < 0) + this->vy = vy*100/this->centerY; + else + this->vy = vy*100/(1023-this->centerY); + // flip vy to cartesian logic + if(this->flipY) + this->vy = -this->vy; +} + + + diff -r 000000000000 -r f31cf3175189 Joystick.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Joystick.h Thu Jan 26 00:48:42 2017 +0100 @@ -0,0 +1,86 @@ +#ifndef Joystick_h +#define Joystick_h + +#include +#include + + +/** + utility class for interpreting joystick values. + combines two potentiometer and calibration information to provide a vector + of user selected direction with 0,0 in the center and +x,+y in top-right. + + (c) 2017 arkadiusz@wobblyhead.net +*/ +class Joystick { + public: + // x-axis pot + Potentiometer x; + // y-axis pot + Potentiometer y; + // optional button + Button b; + + // if calibrated, current vector, x component + float vx; + // if calibrated, current vector, y component + float vy; + + // flag, if true joystick is calibrated. + bool isCalibrated; + + // calibrated center of x axis + int centerX; + // calibrated center of y axis + int centerY; + + /** center deadzone radius. if the joystick is not calibrated, + * positions within this distance from the center are considered in the + * center. + */ + const int CENTER_DEADZONE = 50; + + /** + create an uninitialised Joystick. + setup() must be called after this to make it usable. + */ + Joystick(); + + /** + setup joystick instance with flipped y axis + + @param {byte} pinX input pin for x axis + @param {byte} pinY input pin for y axis + @param {byte} pinB input pin for button + */ + void setup(byte pinX, byte pinY, byte pinB); + + /** + setup joystick instance + + @param {byte} pinX input pin for x axis + @param {byte} pinY input pin for y axis + @param {byte} pinB input pin for button + @param {bool} flipY flip y axis + */ + void setup(byte pinX, byte pinY, byte pinB, bool flipY); + + /** + check inputs. + implicitely calls millis(); + */ + void update(); + + /** + check inputs, update values. + if calibrated, calculate vector. + @param {unsigned long} ms current millis() value + */ + void update(unsigned long ms); + + private: + bool flipY; + +}; + +#endif diff -r 000000000000 -r f31cf3175189 library.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/library.properties Thu Jan 26 00:48:42 2017 +0100 @@ -0,0 +1,9 @@ +name=Joystick +version=1.0 +author=Arkadiusz Juszczyk +maintainer=Arkadiusz Juszczyk +sentence=Joystick for Arduino +paragraph=Self-calibrating joystick +category=Signal Input/Output +url=https://hg.wobblyhead.net/arduino/Joystick +architectures=*