Saturday, January 18, 2014

Arduino Esplora Joystick Mouse with Button function

This example work on last post "Auto center Arduino Esplora Joystick Mouse of Arduino Esplora", with button function added.

#include <TFT.h>
#include <SPI.h>
#include <Esplora.h>

const int MAX_W = 160;
const int MAX_H = 128;
int xCenter = MAX_W/2;
int yCenter = MAX_H/2;

int xPos = xCenter; 
int yPos = yCenter;
int xPrev = xCenter;
int yPrev = yCenter;
char printoutX[5];
char printoutY[5];
char printoutSlider[5];

int centerJoystickX, centerJoystickY;

int JoystickButton;
int prevJoystickButton;

void setup(){
  Serial.begin(9600);
  Mouse.begin();
  
  EsploraTFT.begin();  
  EsploraTFT.background(0,0,0);
  EsploraTFT.stroke(255,255,255);
  EsploraTFT.line(xCenter, 0, xCenter, MAX_H);
  EsploraTFT.line(0, yCenter, MAX_W, yCenter);

  //preset dummy reading to print
  String dummy = "0";
  dummy.toCharArray(printoutX,5);
  dummy.toCharArray(printoutY,5);
  dummy.toCharArray(printoutSlider,5);
  
  //Read Joystick center position at start-up
  centerJoystickX = Esplora.readJoystickX();
  centerJoystickY = Esplora.readJoystickY();
  
  //preset JoystickSwitch states
  JoystickButton = HIGH;
  prevJoystickButton = HIGH;
}

void loop(){
  int xValue = Esplora.readJoystickX();
  int yValue = Esplora.readJoystickY();
  int sliderValue = Esplora.readSlider();
  
  //map stick value to TFT screen position
  if(xValue >= centerJoystickX){
    xPos = map(xValue, 512, centerJoystickX, 0, xCenter);
  }else{
    xPos = map(xValue, centerJoystickX, -512, xCenter, MAX_W);
  }
  
  int mouseX, mouseY;
  
  if(xValue >= centerJoystickX){
    xPos = map(xValue, 512, centerJoystickX, 0, xCenter);
    
    //report mouse position only if position offset > slider
    if((xValue - centerJoystickX) >= sliderValue){
      mouseX = map( xValue, 512, centerJoystickX, -10, 0);
    }else{
      mouseX = 0;
    }
  }else{
    xPos = map(xValue, centerJoystickX, -512, xCenter, MAX_W);
    
    //report mouse position only if position offset > slider
    if((centerJoystickX - xValue) >= sliderValue){
      mouseX = map( xValue, centerJoystickX, 512, 0, -10);
    }else{
      mouseX = 0;
    }
  }
  
  if(yValue >= centerJoystickY){
    yPos = map(yValue, 512, centerJoystickY, MAX_H, yCenter);
    
    //report mouse position only if position offset > slider
    if((yValue - centerJoystickY) >= sliderValue){
      mouseY = map( yValue, 512, centerJoystickY, 10, 0);
    }else{
      mouseY = 0;
    }
  }else{
    yPos = map(yValue, centerJoystickY, -512, yCenter, 0);
    
    //report mouse position only if position offset > slider
    if((centerJoystickY - yValue) >= sliderValue){
      mouseY = map( yValue, centerJoystickY, 512, 0, 10);
    }else{
      mouseY = 0;
    }
  }
  
  Mouse.move(mouseX, mouseY, 0);
  
  //clear previous print of reading
  EsploraTFT.stroke(0,0,0);
  EsploraTFT.text(printoutX,0,10);
  EsploraTFT.text(printoutY,0,20);
  EsploraTFT.text(printoutSlider,0,30);
  
  String(xPos).toCharArray(printoutX,5);
  String(yPos).toCharArray(printoutY,5);
  String(sliderValue).toCharArray(printoutSlider,5);
  EsploraTFT.stroke(255,255,255);
  EsploraTFT.text(printoutX,0,10);
  EsploraTFT.text(printoutY,0,20);
  EsploraTFT.text(printoutSlider,0,30);
  
  if(xPos!=xPrev || yPos!=yPrev){
    
    EsploraTFT.line(xPrev, yPrev, xPos, yPos);
    EsploraTFT.stroke(0,0,255);
    EsploraTFT.point(xPos, yPos);
    
    xPrev=xPos;
    yPrev=yPos;
  }
  
  //Read JoystickButton
  //send Mouse.press()/.release() if JoystickButton changed
  JoystickButton = Esplora.readJoystickButton();
  if(JoystickButton != prevJoystickButton){

    if(JoystickButton == LOW){
      Mouse.press();
      Esplora.writeRed(100);
    }else{
      Mouse.release();
      Esplora.writeRed(0);
    }
    prevJoystickButton = JoystickButton;
  }
  
  delay(10);
}

No comments:

Post a Comment