Editing functions
Here is an example of how to edit a function for a 12-position switch to match a 16-position switch. Most of the functions are found under 64_RotaryAnalog.ino and 65_QuickRotary.ino
Here isrotaryAnalog2Mode()
void rotaryAnalog2Mode(int analogChannel, int fieldPlacement, int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int pos10, int pos11, int pos12, bool reverse)
{
int N = analogChannel - 1;
int Number = analogButtonNumber[N];
int FieldPlacement = fieldPlacement;
int maxPos = 12;
#if(USING_ADS1115 == 1 || USING_CB1 == 1 || ENABLE_OVERSAMPLING == 1)
int value;
if (analogPins[N] > 49)
{
value = ADS1115value[analogPins[N] - ADC_CORR];
}
else
{
value = analogRead(analogPins[N]);
}
#else
int value = analogRead(analogPins[N]);
#endif
int positions[12] = { pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12 };
int differ = 0;
int result = 0;
for (int i = 0; i < 12; i++)
{
if (i == 0 || abs(positions[i] - value) < differ)
{
result++;
differ = abs(positions[i] - value);
}
}
result--;
if (reverse)
{
result = 11 - result;
}
//Short debouncer on switch rotation
if (analogLastCounter[N] != result)
{
if (globalClock - analogTimer1[N] > analogPulse)
{
analogTimer1[N] = globalClock;
}
else if (globalClock - analogTimer1[N] > analogWait)
{
//----------------------------------------------
//----------------MODE CHANGE-------------------
//----------------------------------------------
//Due to placement of this scope, mode change will only occur on switch rotation.
//If you want to avoid switching mode, set fieldPlacement to 0.
if (pushState[modButtonRow - 1][modButtonCol - 1] == 1 && FieldPlacement != 0)
{
for (int i = 0; i < maxPos + 1; i++) //Remove the remnants from SWITCH MODE 1
{
Joystick.releaseButton(i - 1 + Number);
}
analogSwitchMode1[N] = !analogSwitchMode1[N]; //SWAP MODE
}
//Engage encoder pulse timer
analogTimer2[N] = globalClock;
//Update difference, storing the value in pushState on pin 2
analogTempState[N] = result - analogLastCounter[N];
//Give new value to pushState
analogLastCounter[N] = result;
}
}
//SWITCH MODE 1: 12 - position switch
if (!analogSwitchMode1[N])
{
analogTempState[N] = 0; //Refreshing encoder mode difference
for (int i = 0; i < 12; i++)
{
if (i == analogLastCounter[N])
{
Joystick.pressButton(i + Number);
}
else
{
Joystick.releaseButton(i + Number);
}
}
}
//SWITCH MODE 2/4: Incremental encoder or closed hybrid
else if (analogSwitchMode1[N])
{
Number = analogButtonNumberIncMode[N];
int difference = analogTempState[N];
if (difference != 0)
{
if (globalClock - analogTimer2[N] < encoderPulse)
{
if ((difference > 0 && difference < 5) || difference < -5)
{
Joystick.setButton(Number, 1);
Joystick.setButton(Number + 1, 0);
}
else
{
Joystick.setButton(Number, 0);
Joystick.setButton(Number + 1, 1);
}
}
else
{
analogTempState[N] = 0;
Joystick.setButton(Number, 0);
Joystick.setButton(Number + 1, 0);
}
}
}
//Push switch mode
long push = 0;
push = push | analogSwitchMode1[N];
push = push << (FieldPlacement - 1);
rotaryField = rotaryField | push;
}
Copy the whole function, paste in same file. Renaming it to rotaryAnalog2Mode16(), but you can name it whatever you like. Add the extra switch position values in the
positions
array and edit the limits:
void rotaryAnalog2Mode(int analogChannel, int fieldPlacement, int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int pos10, int pos11, int pos12, bool reverse)
{
////ADDED pos13 - pos16 above!
int N = analogChannel - 1;
int Number = analogButtonNumber[N];
int FieldPlacement = fieldPlacement;
int maxPos = 16; //<<<--- EDITED
#if(USING_ADS1115 == 1 || USING_CB1 == 1 || ENABLE_OVERSAMPLING == 1)
int value;
if (analogPins[N] > 49)
{
value = ADS1115value[analogPins[N] - ADC_CORR];
}
else
{
value = analogRead(analogPins[N]);
}
#else
int value = analogRead(analogPins[N]);
#endif
//EDITED ARRAY SIZE AND ADDED pos13-pos16 BELOW
int positions[16] = { pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, pos13, pos14, pos15, pos16 };
int differ = 0;
int result = 0;
for (int i = 0; i < 16; i++) //Edited
{
if (i == 0 || abs(positions[i] - value) < differ)
{
result++;
differ = abs(positions[i] - value);
}
}
result--;
if (Reverse)
{
result = 15 - result; //Edited
}
Edit limits in the 12-position mode
//SWITCH MODE 1: 16 - position switch (edited)
if (!analogSwitchMode1[N] && !biteButtonBit1 && !biteButtonBit2)
{
analogTempState[N] = 0; //Refreshing encoder mode difference
for (int i = 0; i < 16; i++) //Edited
{
if (i == analogLastCounter[N])
{
Joystick.pressButton(i + Number);
}
else
{
Joystick.releaseButton(i + Number);
}
}
}
Last updated