Доброго времени суток!
Соединил два куска разных скетчей после чего ардуино начала определяться как два игровых устройства. И то и то устройство исправно работает, но как из объединить в одно?
Скетч:
#include <Keypad.h>
#include <Joystick.h>
#include "./src/G27PedalsShifter.h"
#define USE_PEDALS
#define ENABLE_PULLUPS
#define NUMBUTTONS 32
#define NUMROWS 9
#define NUMCOLS 4
#define CLUTCH_PIN 9
#define MIN_CLUTCH 45
#define MAX_CLUTCH 932
// MISC.
#define MAX_AXIS 1023
//#define SIGNAL_SETTLE_DELAY 10
#if defined(DEBUG)
#define DEBUG_PEDALS true
#endif
// PEDAL CODE
typedef struct pedal {
byte pin;
int min, max, cur, axis;
};
typedef struct pedal Pedal;
void* clutchPedal;
int axisValue(void* in) {
Pedal* input = (Pedal*)in;
int physicalRange = input->max - input->min;
if (physicalRange == 0) {
return 0;
}
int result = map(input->cur, input->min, input->max, 0, MAX_AXIS);
if (result < 0) {
return 0;
}
if (result > MAX_AXIS) {
return MAX_AXIS;
}
return result;
}
void processPedal(void* in) {
Pedal* input = (Pedal*)in;
input->cur = analogRead(input->pin);
#if !defined(STATIC_THRESHOLDS)
// calibrate, we want the highest this pedal has been
input->max = input->cur > input->max ? input->cur : input->max;
// same for lowest, but bottom out at current value rather than 0
input->min = input->min == 0 || input->cur < input->min ? input->cur : input->min;
#endif
input->axis = axisValue(input);
}
void setZAxis(void* in) {
Pedal* input = (Pedal*)in;
G27.setZAxis(input->axis);
}
void waitForSignalToSettle() {
// delayMicroseconds(SIGNAL_SETTLE_DELAY);
}
byte buttons[NUMROWS][NUMCOLS] = {
{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}
};
byte rowPins[NUMROWS] = {21,20,19,18,15,14,16,10};
byte colPins[NUMCOLS] = {8,7,6};
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK, 32, 0, false, false,
false,false,false,false,false,false,false,false,false);
void setup() {
Joystick.begin();
Serial.begin(38400);
#if !defined(DEBUG_PEDALS)
G27.begin(false);
#endif
// pedal
Pedal* clutch = new Pedal();
clutch->pin = CLUTCH_PIN;
#if defined(STATIC_THRESHOLDS)
clutch->min = MIN_CLUTCH;
clutch->max = MAX_CLUTCH;
#endif
clutchPedal = clutch;
}
void loop() {
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;
}
}
}
}
// pedals
processPedal(clutchPedal);
#if defined(DEBUG_PEDALS)
describePedal("CLUTCH", "Z", clutchPedal);
#elif defined(USE_PEDALS)
setZAxis(clutchPedal);
#endif
#if !defined(DEBUG_PEDALS)
G27.sendState();
#endif
#if defined(DEBUG_PEDALS)
Serial.print("\n----------------------------------------------------------------------------");
delay(500);
#endif
}