Sto cercando di aggiungere un Rotary encoder ad una scheda Leonardo game controller.
utilizzo la libreria Joystick.h e Encoder.h.
Non so se esistono altre strade, io ho proceduto cosi'.
Associo al singolo step in avanti dell'encoder il pulsante 3.
Associo al singolo step indietro dell'encoder il pulsante 4.
Ad ogni rotazione dell'encoder in avanti invoco: Joystick.pressButton(3). Dopo 10 ms invoco Joystick.releaseButton(3);
Idem per lo step indietro:Joystick.pressButton(4). Dopo 10 ms invoco Joystick.releaseButton(4);
Nel monitor sorgente vedo tutto in realtime mentre la periferica di gioco va molto lentamente. Mi domando dove sbaglio. Grazie
Il codice che ho utilizzato è il seguente:
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Joystick.h>
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(2, 3);
// avoid using pins with LEDs attached
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
5, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
Serial.begin(9600);
// Initialize Button Pins
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
Joystick.begin();
}
bool initUp = false;
bool initDown = false;
long initTimeUp = 0;
long initTimeDown = 0;
long oldPosition = -999;
// Last state of the buttons
int lastButtonState[3] = {0,0,0};
int lastEncoderState=0;
void loop()
{
//Manage button 0, 1, 2
for (int index = 0; index < 3; index++)
{
int currentButtonState = !digitalRead(index + 8);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
}
lastButtonState[index] = currentButtonState;
}
//Manage Encoder: button 3(one step up) and 4(one step down)
long newPosition = myEnc.read();
newPosition = newPosition/4;
if (newPosition != oldPosition)
{
Serial.print("New Position= ");
Serial.println(newPosition);
if(newPosition > oldPosition)
{
Joystick.pressButton(3);
initTimeUp = millis();
initUp = true;
Serial.println("Start Up");
}
if (newPosition < oldPosition)
{
Joystick.pressButton(4);
initDown = true;
initTimeDown = millis();
Serial.println("Start Down");
}
oldPosition = newPosition;
}
if((millis() - initTimeUp > 10) and (initUp == true))
{
Joystick.releaseButton(3);
Serial.println("End Up");
initUp = false;
}
if((millis() - initTimeDown > 10) and (initDown == true))
{
Joystick.releaseButton(4);
Serial.println("End Down");
initDown = false;
}
}
This is the Monitor Output:
New Position= 246
Start Up
End Up
New Position= 247
Start Up
End Up
New Position= 248
Start Up
End Up
New Position= 249
Start Up
End Up
New Position= 248
Start Down
End Down
New Position= 249
Start Up
End Up
New Position= 250
Start Up
End Up
New Position= 251
Start Up
End Up