I'm trying to impress my step son as he is a huge American Trucking Simulator and Farming Simulator fan. He discovered they sell premade button boxes by Saitek (Logitech) and EvolvPC for both games. With a little googling I found a site that has code to make a 32 function box which is nice but I wanted to combine the best of both retail boxes. Soooo, along came the idea of a 56 button, 5 rotary and 2 3-axis joystick game controller. I researched high and low and thought it would be possible with a Leonardo, some code changes and the use of a couple mcp23017 expanders. After almost 2 weeks of beating my head against my keyboard I'm at a loss and could really use some help.
So what I made was an 8x7 matrix and I've varified all buttons do work. I haven't even attempted to get the rotaries / joys going as I wanted to tackle the buttons first. Unfortunately I have since discovered DirectX only likes you to have 32 buttons max. So then I thought well, the Joystick.h library can make it look like there is multiple HID devices so why not make it look like I have 2 multibutton joysticks.
And that's where I've hit a brick wall. I could really use some help / guidance with this huge undertaking. The most programming experience I ever had was programming in basic on a C64 so bear with me.
The code I'm tinkering with. Also Is it possible to have the rotaries / joys on the i/o expanders using the included libraries or do I just need to buy 2 more leonardo's and divide everything up ![]()
#include <Keypad.h>
#include <Joystick.h>
#include <Wire.h>
#include "Adafruit_MCP23017.h"
#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS0 56
#define NUMBUTTONS1 56
#define NUMROWS0 8
#define NUMCOLS0 7
#define NUMROWS1 8
#define NUMCOLS1 7
#define JOYSTICK_COUNT 2
byte buttons[NUMROWS0][NUMCOLS0] = {
{0,1,2,3,4,5,6},
{7,8,9,10,11,12,13},
{14,15,16,17,18,19,20},
{21,22,23,24,25,26,27},
{28,29,30,31,32,33,34},
{35,36,37,38,39,40,41},
{42,43,44,45,46,47,48},
{49,50,51,52,53,54,55}
};
byte buttons[NUMROWS1][NUMCOLS1] = {
{56,57,58,59,60,61,62},
{63,64,65,66,67,68,69},
{70,71,72,73,74,75,76},
{77,78,79,80,81,82,83},
{84,85,86,87,88,89,90},
{91,92,93,94,95,96,97},
{98,99,100,101,102,103,104}
};
struct rotariesdef {
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
rotariesdef rotaries[NUMROTARIES] {
{0,1,24,25,0},
{2,3,26,27,0},
{4,5,28,29,0},
{6,7,30,31,0},
};
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#ifdef HALF_STEP
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttable[6][4] = {
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
};
#else
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttable[7][4] = {
// R_START
{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CW_FINAL
{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
// R_CW_BEGIN
{R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
// R_CW_NEXT
{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
// R_CCW_BEGIN
{R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
// R_CCW_FINAL
{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
// R_CCW_NEXT
{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};
#endif
byte rowPins[NUMROWS0] = {1,0,A5,A4,A3,A2,A1,A0};
byte colPins[NUMCOLS0] = {8,7,6,5,4,3,2};
byte rowPins[NUMROWS1] = {A0,A1,A2,A3,A4,A5,0,1};
byte colPins[NUMCOLS1] = {2,3,4,5,6,7,8};
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS0, NUMCOLS0);
Keypad buttbx1 = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS1, NUMCOLS1);
Joystick_ Joystick(0x05, JOYSTICK_TYPE_MULTI_AXIS, 32, 2, true, true, true, true, true, true, false, false, false, false, false);
Joystick_ Joystick(0x06, JOYSTICK_TYPE_MULTI_AXIS, 32, 2, true, true, true, true, true, true, false, false, false, false, false);
void setup() {
Joystick.begin();
rotary_init();}
void loop() {
CheckAllEncoders();
CheckAllButtons();
}
void CheckAllButtons(void) {
if (buttbx.getKeys())
{
for (int i=0; i<LIST_MAX; i++)
{
if ( buttbx.key[i].stateChanged )
{
switch (buttbx.key[i].kstate) {
case PRESSED:
case HOLD:
Joystick.setButton(buttbx.key[i].kchar, 1);
break;
case RELEASED:
case IDLE:
Joystick.setButton(buttbx.key[i].kchar, 0);
break;
}
}
}
}
}
void rotary_init() {
for (int i=0;i<NUMROTARIES;i++) {
pinMode(rotaries[i].pin1, INPUT);
pinMode(rotaries[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotaries[i].pin1, HIGH);
digitalWrite(rotaries[i].pin2, HIGH);
#endif
}
}
unsigned char rotary_process(int _i) {
unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
return (rotaries[_i].state & 0x30);
}
void CheckAllEncoders(void) {
for (int i=0;i<NUMROTARIES;i++) {
unsigned char result = rotary_process(i);
if (result == DIR_CCW) {
Joystick.setButton(rotaries[i].ccwchar, 1); delay(50); Joystick.setButton(rotaries[i].ccwchar, 0);
};
if (result == DIR_CW) {
Joystick.setButton(rotaries[i].cwchar, 1); delay(50); Joystick.setButton(rotaries[i].cwchar, 0);
};
}
}
