Also ich komme hier leider nicht wirklich weiter..
Ich würde jetzt den Part:
1 |
// Button inputs
|
2 |
else if (assignedInput > 12 + 31)
|
3 |
{
|
4 |
// first 11 inputs are reserved for axes
|
5 |
// button1 is stored as 1+12 but reported HID as 1
|
6 |
Joystick.setButton(assignedInput - 12 - 32, value);
|
7 |
}
|
8 |
else
|
ersetzen durch:
1 |
// Button inputs
|
2 |
else if (assignedInput > 12 + 31)
|
3 |
{
|
4 |
// first 11 inputs are reserved for axes
|
5 |
// button1 is stored as 1+12 but reported HID as 1
|
6 |
int buttonIndex = assignedInput - 12 - 32;
|
7 |
|
8 |
if (value != lastButtonValue[buttonIndex]) {
|
9 |
// Indicate to the PC the Joystick button has been pressed if the switch has changed value
|
10 |
lastButtonValue[buttonIndex] = value;
|
11 |
Joystick.setButton(buttonIndex, 1);
|
12 |
} else {
|
13 |
// Indicate to the PC the Joystick button has been released if the switch value has not changed
|
14 |
Joystick.setButton(buttonIndex, 0);
|
15 |
}
|
16 |
}
|
17 |
else
|
Zusätzlich müsste ich ja noch den letzten Zustand iwo speichern..
long lastHatChange = 0;
int lastButtonValue[128];
void InitInputs()
Das funktioniert aber nicht.. Sobald ich den array einfüge und hochlade verabschiedet sich der Arduino.. Iwas ist da falsch..
Die original Inputs.ino:
#define HAT_CENTER 8
#define HAT_N 0
#define HAT_NE 1
#define HAT_E 2
#define HAT_SE 3
#define HAT_S 4
#define HAT_SW 5
#define HAT_W 6
#define HAT_NW 7
#define PIN_RESERVED 3
Encoder encoders[4];
// input pins autofilled based on which mpu
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
int inputPins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 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};
Input inputs[66];
int memoryLocationEncoder = 2300;
int memoryLocationMatrix = 2000;
int memoryLocationDeviceID = 4000;
#elif defined(AVR_ATmega328P)
int inputPins[] = {A0, A1, A2, A3, A4, A5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
Input inputs[18];
int memoryLocationEncoder = 800;
int memoryLocationMatrix = 512;
int memoryLocationDeviceID = 1000;
#elif defined(AVR_ATmega32U4)
int inputPins[] = {18, 19, 20, 21, 4, 6, 8, 9, 10, 2, 3, 5, 7, 14, 15, 16};
Input inputs[16];
int memoryLocationEncoder = 800;
int memoryLocationMatrix = 512;
int memoryLocationDeviceID = 1000;
#include "Joystick.h"
Joystick_ Joystick(0x03,
JOYSTICK_TYPE_JOYSTICK, 128, 2,
true, true, true, true, true, true,
true, true);
#elif defined(ESP32)
int inputPins[] = {4, 5, 13, 14, 15, 16, 17, 18, 19, 23, 25, 26, 27, 32, 33, 34}; //, 35, 36, 39};
Input inputs[16];
int memoryLocationEncoder = 800;
int memoryLocationMatrix = 512;
int memoryLocationDeviceID = 1000;
#include "rr_bleHID.h"
#else
int inputPins[] = {2};
Input inputs[1];
int memoryLocationEncoder = 800;
int memoryLocationMatrix = 512;
int memoryLocationDeviceID = 1000;
#endif
#define FORCE_DEFAULTS false
uint8_t matrixRowCount = 0;
uint8_t matrixColCount = 0;
uint8_t matrixRowPins[16];
uint8_t matrixColPins[16];
uint8_t matrixButtonAssignments[256];
uint8_t matrixState[32];
uint8_t hat1_state = HAT_CENTER;
uint8_t hat2_state = HAT_CENTER;
uint8_t hat3_state = HAT_CENTER;
uint8_t hat4_state = HAT_CENTER;
long lastHatChange = 0;
void InitInputs()
{
1 |
delay(1000);
|
2 |
// Reset EEPROM
|
3 |
// for (int i = 0; i < 1024; i++)
|
4 |
// {
|
5 |
// Store8BitValue(i, 0);
|
6 |
// }
|
7 |
|
8 |
// 100 written in EEPROM address 0 indicates device has been used more than once
|
9 |
// if not 100, initialize variables and eeprom with default values.
|
10 |
int firstRun = Read8BitValue(memoryLocationDeviceID + 23);
|
11 |
//Serial.println(firstRun);
|
12 |
|
13 |
if (EncodersInited()){
|
14 |
SetEncodersToDefault();
|
15 |
}
|
16 |
|
17 |
|
18 |
if (FORCE_DEFAULTS)
|
19 |
{
|
20 |
for (int i = 0; i < InputCount(); i++)
|
21 |
{
|
22 |
inputs[i].SetPin(inputPins[i]);
|
23 |
if (i == 8)
|
24 |
{
|
25 |
inputs[i].SetPinMode(OUTPUT);
|
26 |
inputs[i].SetIsAnalog(3);
|
27 |
inputs[i].SetMinVal(50);
|
28 |
}
|
29 |
else
|
30 |
{
|
31 |
inputs[i].SetIsAnalog(false);
|
32 |
}
|
33 |
inputs[i].InitInput();
|
34 |
}
|
35 |
SaveCurrentInputConfig();
|
36 |
SetEncodersToDefault();
|
37 |
SetButtonMatrixToDefault();
|
38 |
CommitButtonMatrixToEEPROM();
|
39 |
}
|
40 |
else if (firstRun != 100)
|
41 |
{
|
42 |
OnFirstRun();
|
43 |
}
|
44 |
else
|
45 |
{
|
46 |
|
47 |
// Load deviceName from EEPROM
|
48 |
for (int i = 0; i < 16; i++)
|
49 |
{
|
50 |
deviceName[i] = char(Read8BitValue(memoryLocationDeviceID + i));
|
51 |
}
|
52 |
|
53 |
// Load i2cAddress from EEPROM
|
54 |
// delay(2000);
|
55 |
// Serial.println("Loading i2caddress...");
|
56 |
// delay(2000);
|
57 |
i2c_address = Read8BitValue(memoryLocationDeviceID + 22);
|
58 |
|
59 |
//Serial.println(i2c_address);
|
60 |
|
61 |
// Load Inputs from EEPROM
|
62 |
for (int i = 0; i < InputCount(); i++)
|
63 |
{
|
64 |
inputs[i].SetPin(inputPins[i]);
|
65 |
inputs[i].ReadFromEEPROM(i);
|
66 |
inputs[i].InitInput();
|
67 |
}
|
68 |
|
69 |
// Load Encoders from EEPROM
|
70 |
for (int i = 0; i < 4; i++)
|
71 |
{
|
72 |
encoders[i].ReadFromEEPROM(memoryLocationEncoder, i);
|
73 |
inputs[encoders[i].pinAidx].SetPinMode(PIN_RESERVED);
|
74 |
inputs[encoders[i].pinBidx].SetPinMode(PIN_RESERVED);
|
75 |
}
|
76 |
|
77 |
LoadButtonMatrixFromEEPROM();
|
78 |
}
|
79 |
|
80 |
UpdateMatrixRowColCount();
|
81 |
|
82 |
// encoders[0].SetPins(4, 11, inputPins[4], inputPins[11]);
|
83 |
// encoders[0].leftAssignment = 65;
|
84 |
// encoders[0].rightAssignment = 66;
|
85 |
|
86 |
if (IsMaster())
|
87 |
{
|
#if defined(AVR_ATmega32U4)
Joystick.begin();
Joystick.setXAxisRange(-32767, 32767);
Joystick.setYAxisRange(-32767, 32767);
Joystick.setZAxisRange(-32767, 32767);
1 |
Joystick.setRxAxisRange(-32767, 32767);
|
2 |
Joystick.setRyAxisRange(-32767, 32767);
|
3 |
Joystick.setRzAxisRange(-32767, 32767);
|
4 |
|
5 |
Joystick.setRudderRange(-32767, 32767);
|
6 |
Joystick.setThrottleRange(-32767, 32767);
|
#elif defined(ESP32)
delay(100);
initBLEDevice();
delay(100);
#endif
}
}
void OnFirstRun()
{
// Write default deviceName to EEPROM
char defaultName[] = "NEW_DEVICE ";
for (int i = 0; i < 16; i++)
{
Store8BitValue(memoryLocationDeviceID + i, defaultName[i]);
}
1 |
// Write default i2c_address to EEPROM
|
2 |
Store8BitValue(memoryLocationDeviceID + 22, 0);
|
3 |
|
4 |
// Write Input defaults to EEPROM
|
5 |
Store8BitValue(memoryLocationDeviceID + 23, 100); // mark first run byte
|
6 |
|
7 |
for (int i = 0; i < InputCount(); i++)
|
8 |
{
|
9 |
inputs[i].SetPin(inputPins[i]);
|
10 |
inputs[i].SetToDefaults();
|
11 |
inputs[i].WriteToEEPROM(i);
|
12 |
}
|
13 |
SetButtonMatrixToDefault();
|
14 |
SetEncodersToDefault();
|
15 |
CommitButtonMatrixToEEPROM();
|
}
void ResetToDefaults()
{
uint8_t currentI2CAddress = i2c_address;
OnFirstRun();
1 |
// rescue i2c address from reset to defaults
|
2 |
i2c_address = currentI2CAddress;
|
3 |
Store8BitValue(memoryLocationDeviceID + 22, i2c_address);
|
}
// Forces encoder EEPROM initiation for devices upgrading from earlier versions
bool EncodersInited(){
// 804 == first key assignment, never should be 255 if inited
if (Read8BitValue(memoryLocationEncoder + 4) == 255){
return true;
}
return false;
}
uint8_t matrixCounter = 0;
void UpdateInputs()
{
1 |
for (int i = 0; i < InputCount(); i++)
|
2 |
{
|
3 |
if (i != 4 && i != 11)
|
4 |
inputs[i].UpdateInput();
|
5 |
// if (i == 12){
|
6 |
// Serial.println(inputs[i].GetAssignedInput());
|
7 |
// }
|
8 |
}
|
9 |
for (int i = 0; i < 4; i++)
|
10 |
{
|
11 |
encoders[i].UpdateInput();
|
12 |
}
|
13 |
//Serial.println(encoders[0].GetPosition());
|
14 |
//Serial.print("Encoder0: ");
|
15 |
|
16 |
RecordMatrixState(0, matrixColCount);
|
17 |
|
18 |
// for (int i = 0; i < 8; i++){
|
19 |
// Serial.print(matrixRowPins[i]);
|
20 |
// Serial.print(" ");
|
21 |
// }
|
22 |
// Serial.println();
|
#if defined(AVR_ATmega32U4)
if (hat1_state == HAT_CENTER)
{
Joystick.setHatSwitch(0, -1);
}
else
{
Joystick.setHatSwitch(0, hat1_state * 45);
}
if (hat2_state == HAT_CENTER)
{
Joystick.setHatSwitch(1, -1);
}
else
{
Joystick.setHatSwitch(1, hat2_state * 45);
// Serial.print("hat2_state:");
// Serial.println(hat2_state);
}
#elif defined(ESP32)
SetHat(0, hat1_state);
SetHat(1, hat2_state);
SetHat(2, hat3_state);
SetHat(3, hat4_state);
updateHIDDevice();
#endif
1 |
if (IsMaster())
|
2 |
{
|
3 |
SendLocalJoystickStatus();
|
4 |
}
|
}
void SetInput(int idx, int property, int value)
{
inputs[idx].Set(property, value);
}
void SetEncoder(int idx, int property, int value)
{
encoders[idx].Set(property, value);
if (property == 1)
{
inputs[value].SetPinMode(PIN_RESERVED);
encoders[idx].Set(3, inputPins[value]);
}
else if (property == 2)
{
inputs[value].SetPinMode(PIN_RESERVED);
encoders[idx].Set(4, inputPins[value]);
}
}
Input GetInput(int idx)
{
return inputs[idx];
}
Encoder GetEncoder(int idx)
{
return encoders[idx];
}
void UpdateMatrixRowColCount()
{
matrixRowCount = 16;
matrixColCount = 16;
for (int i = 0; i < 16; i++)
{
if (matrixRowPins[i] == 254)
{
matrixRowCount = i;
break;
}
}
for (int i = 0; i < 16; i++)
{
if (matrixColPins[i] == 254)
{
matrixColCount = i;
break;
}
}
}
uint8_t GetMatrixRowCount()
{
return matrixRowCount;
}
uint8_t GetMatrixColCount()
{
return matrixColCount;
}
void SetMatrixRowPin(int idx, int pin)
{
matrixRowPins[idx] = pin;
inputs[pin].SetPinMode(PIN_RESERVED);
UpdateMatrixRowColCount();
}
void SetMatrixColPin(int idx, int pin)
{
matrixColPins[idx] = pin;
inputs[pin].SetPinMode(PIN_RESERVED);
UpdateMatrixRowColCount();
}
void SetMatrixInputAssignment(int idx, int assignment)
{
if (idx < 256)
{
matrixButtonAssignments[idx] = assignment;
}
}
uint8_t GetMatrixRowPin(int idx)
{
return matrixRowPins[idx];
}
uint8_t GetMatrixColPin(int idx)
{
return matrixColPins[idx];
}
uint8_t GetMatrixInputAssignment(int idx)
{
return matrixButtonAssignments[idx];
}
int GetInputIdxByPin(int pin)
{
//Serial.print("Looking for pin: ");
//Serial.println(pin);
for (int i = 0; i < InputCount(); i++)
{
if (inputs[i].GetPin() == pin)
{
return i;
}
}
return -1;
}
void ReportInputConfig(int subDeviceIndex)
{
if (subDeviceIndex == 0)
{
// Master device config
Serial.write(InputCount()); // input count
for (int i = 0; i < InputCount(); i++)
{
inputs[i].ReportConfig();
}
1 |
for (int i = 0; i < 4; i++)
|
2 |
{
|
3 |
encoders[i].ReportConfig();
|
4 |
}
|
5 |
|
6 |
ReportButtonMatrixConfig();
|
7 |
}
|
8 |
else
|
9 |
{
|
10 |
PrintSubdeviceInputConfig(subDeviceIndex - 1);
|
11 |
}
|
}
void SaveCurrentInputConfig()
{
for (int i = 0; i < 16; i++)
{
Store8BitValue(memoryLocationDeviceID + i, deviceName[i]);
}
1 |
// Write default i2c_address to EEPROM
|
2 |
Store8BitValue(memoryLocationDeviceID + 22, i2c_address);
|
3 |
|
4 |
for (int i = 0; i < InputCount(); i++)
|
5 |
{
|
6 |
inputs[i].WriteToEEPROM(i);
|
7 |
}
|
8 |
|
9 |
for (int i = 0; i < 4; i++)
|
10 |
{
|
11 |
encoders[i].WriteToEEPROM(memoryLocationEncoder, i);
|
12 |
}
|
13 |
|
14 |
CommitButtonMatrixToEEPROM();
|
}
void PrintInputs()
{
for (int i = 0; i < InputCount(); i++)
{
Serial.print(inputPins[i]);
Serial.print("\t");
}
Serial.println();
}
uint8_t InputCount()
{
return sizeof(inputPins) / sizeof(int);
}
void ReportConfigReadable(int idx)
{
inputs[idx].ReportConfigReadable();
}
void ReportValues()
{
Serial.write("V");
for (int i = 0; i < InputCount(); i++)
{
Serial.write(highByte(inputs[i].GetRawVal()));
Serial.write(lowByte(inputs[i].GetRawVal()));
Serial.write(highByte(inputs[i].GetVal()));
Serial.write(lowByte(inputs[i].GetVal()));
}
1 |
int deviceCount = SubDeviceCount();
|
2 |
Serial.write(deviceCount);
|
3 |
|
4 |
for (int i = 0; i < deviceCount; i++)
|
5 |
{
|
6 |
Serial.write(i); // SubDevice index
|
7 |
//RequestInputInfo(1, i); // SubDevice config
|
8 |
}
|
9 |
|
10 |
Serial.write("\r\n");
|
}
void JoystickInput(uint8_t assignedInput, int value)
{
#if defined(AVR_ATmega32U4)
// HatSwitch Inputs
if (assignedInput >= 12 && assignedInput < 44)
{
// Hat 1
if (assignedInput < 20)
{
if (value == 0 && assignedInput - 12 == hat1_state)
{
hat1_state = HAT_CENTER;
//Serial.println("CENTER");
}
else if (value > 0)
{
hat1_state = assignedInput - 12;
//Serial.println(hat1_state);
}
}
else
{
if (value == 0 && assignedInput - 12 - 8 == hat2_state)
{
hat2_state = HAT_CENTER;
//Serial.println("CENTER");
}
else if (value > 0)
{
hat2_state = assignedInput - 12 - 8;
//Serial.println(hat2_state);
}
//Serial.println(hat2_state);
}
}
// Button inputs
else if (assignedInput > 12 + 31)
{
// first 11 inputs are reserved for axes
// button1 is stored as 1+12 but reported HID as 1
Joystick.setButton(assignedInput - 12 - 32, value);
}
else
{
switch (assignedInput)
{
case XAXIS:
Joystick.setXAxis(value);
break;
case YAXIS:
Joystick.setYAxis(value);
break;
case ZAXIS:
Joystick.setZAxis(value);
break;
case XROTATION:
Joystick.setRxAxis(value);
break;
case YROTATION:
Joystick.setRyAxis(value);
break;
case ZROTATION:
Joystick.setRzAxis(value);
break;
case RUDDERAXIS:
Joystick.setRudder(value);
break;
case THROTTLEAXIS:
Joystick.setThrottle(value);
break;
case ACCELERATORAXIS:
Joystick.setAccelerator(value);
break;
case BRAKEAXIS:
Joystick.setBrake(value);
break;
case STEERINGAXIS:
Joystick.setSteering(value);
break;
}
}
#elif defined(ESP32)
1 |
// HatSwitch Inputs
|
2 |
if (assignedInput > 11 && assignedInput < 12 + 32)
|
3 |
{
|
4 |
if (assignedInput < 12 + 8)
|
5 |
{
|
6 |
if (value == 0 && assignedInput - 12 == hat1_state)
|
7 |
{
|
8 |
hat1_state = HAT_CENTER;
|
9 |
}
|
10 |
else if (value > 0 && assignedInput - 12 != hat1_state)
|
11 |
{
|
12 |
hat1_state = assignedInput - 12;
|
13 |
}
|
14 |
}
|
15 |
else if (assignedInput < 12 + (8 * 2))
|
16 |
{
|
17 |
if (value == 0 && assignedInput - (12 + 8) == hat2_state)
|
18 |
{
|
19 |
hat2_state = HAT_CENTER;
|
20 |
}
|
21 |
else if (value > 0 && assignedInput - (12 + 8) != hat2_state)
|
22 |
{
|
23 |
hat2_state = assignedInput - (12 + 8);
|
24 |
}
|
25 |
}
|
26 |
else if (assignedInput < 12 + (8 * 3))
|
27 |
{
|
28 |
if (value == 0 && assignedInput - (12 + 16) == hat3_state)
|
29 |
{
|
30 |
hat3_state = HAT_CENTER;
|
31 |
}
|
32 |
else if (value > 0 && assignedInput - (12 + 16) != hat3_state)
|
33 |
{
|
34 |
hat3_state = assignedInput - (12 + 16);
|
35 |
}
|
36 |
}
|
37 |
else if (assignedInput < 12 + (8 * 4))
|
38 |
{
|
39 |
if (value == 0 && assignedInput - (12 + 24) == hat4_state)
|
40 |
{
|
41 |
hat4_state = HAT_CENTER;
|
42 |
}
|
43 |
else if (value > 0 && assignedInput - (12 + 24) != hat4_state)
|
44 |
{
|
45 |
hat4_state = assignedInput - (12 + 24);
|
46 |
}
|
47 |
}
|
48 |
}
|
49 |
// Button inputs
|
50 |
else if (assignedInput > 12 + 31)
|
51 |
{
|
52 |
// first 11 inputs are reserved for axes
|
53 |
// button1 is stored as 1+12 but reported HID as 1
|
54 |
//Joystick.setButton(assignedInput - 12 - 16, value);
|
55 |
SetButton(assignedInput - 12 - 32, value);
|
56 |
}
|
57 |
else
|
58 |
{
|
59 |
SetAxis(assignedInput - 1, value);
|
60 |
}
|
#endif
}
void SendLocalJoystickStatus()
{
uint8_t assignedInput = 0;
for (int i = 0; i < InputCount(); i++)
{
if (inputs[i].IsInput())
{
JoystickInput(inputs[i].GetAssignedInput(), inputs[i].GetVal());
}
}
1 |
// Send Encoder Inputs
|
2 |
for (int i = 0; i < 4; i++)
|
3 |
{
|
4 |
if (encoders[i].pinA == -1 || encoders[i].pinB == -1)
|
5 |
{
|
6 |
continue;
|
7 |
}
|
8 |
if (encoders[i].ButtonADown())
|
9 |
{
|
10 |
JoystickInput(encoders[i].leftAssignment, 1);
|
11 |
}
|
12 |
else
|
13 |
{
|
14 |
if (millis() - encoders[i].leftPushedTime < encoders[i].holdTime)
|
15 |
{
|
16 |
JoystickInput(encoders[i].leftAssignment, 1);
|
17 |
} else {
|
18 |
JoystickInput(encoders[i].leftAssignment, 0);
|
19 |
}
|
20 |
}
|
21 |
if (encoders[i].ButtonBDown())
|
22 |
{
|
23 |
JoystickInput(encoders[i].rightAssignment, 1);
|
24 |
}
|
25 |
else
|
26 |
{
|
27 |
if (millis() - encoders[i].rightPushedTime < encoders[i].holdTime)
|
28 |
{
|
29 |
JoystickInput(encoders[i].rightAssignment, 1);
|
30 |
} else {
|
31 |
JoystickInput(encoders[i].rightAssignment, 0);
|
32 |
}
|
33 |
}
|
34 |
}
|
35 |
|
36 |
SendMatrixHIDReport();
|
}
void SetEncodersToDefault()
{
for (int i = 0; i < 4; i++)
{
encoders[i].ToDefault();
encoders[i].WriteToEEPROM(memoryLocationEncoder, i);
}
}
void SetButtonMatrixToDefault()
{
for (int i = 0; i < 16; i++)
{
matrixRowPins[i] = 254; // 254 stands in for null
matrixColPins[i] = 254;
}
1 |
for (int i = 0; i < 256; i++)
|
2 |
{
|
3 |
matrixButtonAssignments[i] = 0;
|
4 |
}
|
}
void ReportButtonMatrixConfig()
{
for (int x = 0; x < 16; x++)
{
Serial.write(matrixRowPins[x]);
}
for (int x = 0; x < 16; x++)
{
Serial.write(matrixColPins[x]);
}
for (int x = 0; x < 256; x++)
{
Serial.write(matrixButtonAssignments[x]);
}
}
void CommitButtonMatrixToEEPROM()
{
for (int x = 0; x < 16; x++)
{
Store8BitValue(memoryLocationMatrix + x, matrixRowPins[x]);
}
for (int x = 0; x < 16; x++)
{
Store8BitValue(memoryLocationMatrix + 16 + x, matrixColPins[x]);
}
for (int x = 0; x < 256; x++)
{
Store8BitValue(memoryLocationMatrix + 32 + x, matrixButtonAssignments[x]);
}
}
void LoadButtonMatrixFromEEPROM()
{
for (int x = 0; x < 16; x++)
{
matrixRowPins[x] = Read8BitValue(memoryLocationMatrix + x);
}
for (int x = 0; x < 16; x++)
{
matrixColPins[x] = Read8BitValue(memoryLocationMatrix + 16 + x);
}
for (int x = 0; x < 256; x++)
{
matrixButtonAssignments[x] = Read8BitValue(memoryLocationMatrix + 32 + x);
}
}
// Report current buttn states to serial
void ReportMatrixState()
{
// iterate the columns
for (int colIndex = 0; colIndex < 16; colIndex++)
{
pinMode(inputPins[matrixRowPins[colIndex]], OUTPUT);
digitalWrite(inputPins[matrixRowPins[colIndex]], LOW);
1 |
byte rowByte0 = 0;
|
2 |
byte rowByte1 = 0;
|
3 |
|
4 |
for (int rowIndex = 0; rowIndex < 16; rowIndex++)
|
5 |
{
|
6 |
|
7 |
pinMode(inputPins[matrixColPins[rowIndex]], INPUT_PULLUP);
|
8 |
|
9 |
if (colIndex < 8)
|
10 |
{
|
11 |
if (!digitalRead(inputPins[matrixColPins[rowIndex]]))
|
12 |
{
|
13 |
bitSet(rowByte0, rowIndex);
|
14 |
}
|
15 |
}
|
16 |
else
|
17 |
{
|
18 |
if (!digitalRead(inputPins[matrixColPins[rowIndex]]))
|
19 |
{
|
20 |
bitSet(rowByte1, rowIndex - 8);
|
21 |
}
|
22 |
}
|
23 |
|
24 |
pinMode(inputPins[matrixColPins[rowIndex]], INPUT);
|
25 |
}
|
26 |
Serial.write(rowByte0);
|
27 |
Serial.write(rowByte1);
|
28 |
|
29 |
// disable the column
|
30 |
pinMode(inputPins[matrixRowPins[colIndex]], INPUT);
|
31 |
}
|
}
// Report matrix state over i2c
void ReportMatrixStateI2cOLD(int fromCol, int colCount)
{
// iterate the columns
for (int colIndex = fromCol; colIndex < fromCol + colCount; colIndex++)
{
pinMode(inputPins[matrixColPins[colIndex]], OUTPUT);
digitalWrite(inputPins[matrixColPins[colIndex]], LOW);
1 |
byte rowByte0 = 0;
|
2 |
byte rowByte1 = 0;
|
3 |
|
4 |
for (int rowIndex = 0; rowIndex < 16; rowIndex++)
|
5 |
{
|
6 |
pinMode(inputPins[matrixRowPins[rowIndex]], INPUT_PULLUP);
|
7 |
|
8 |
if (colIndex < 8)
|
9 |
{
|
10 |
if (!digitalRead(inputPins[matrixRowPins[rowIndex]]))
|
11 |
{
|
12 |
bitSet(rowByte0, rowIndex);
|
13 |
}
|
14 |
}
|
15 |
else
|
16 |
{
|
17 |
if (!digitalRead(inputPins[matrixRowPins[rowIndex]]))
|
18 |
{
|
19 |
bitSet(rowByte1, rowIndex - 8);
|
20 |
}
|
21 |
}
|
22 |
|
23 |
pinMode(inputPins[matrixRowPins[rowIndex]], INPUT);
|
24 |
}
|
25 |
i2cWrite(rowByte0);
|
26 |
i2cWrite(rowByte1);
|
27 |
|
28 |
// disable the column
|
29 |
pinMode(inputPins[matrixColPins[colIndex]], INPUT);
|
30 |
}
|
}
// Report matrix state over i2c
void RecordMatrixState(int fromCol, int colCount)
{
// iterate the columns
for (int colIndex = fromCol; colIndex < fromCol + colCount; colIndex++)
{
pinMode(inputPins[matrixRowPins[colIndex]], OUTPUT);
digitalWrite(inputPins[matrixRowPins[colIndex]], LOW);
1 |
for (int rowIndex = 0; rowIndex < 16; rowIndex++)
|
2 |
{
|
3 |
pinMode(inputPins[matrixColPins[rowIndex]], INPUT_PULLUP);
|
4 |
|
5 |
if (colIndex < 8)
|
6 |
{
|
7 |
bitWrite(matrixState[colIndex * 2], rowIndex, !digitalRead(inputPins[matrixColPins[rowIndex]]));
|
8 |
}
|
9 |
else
|
10 |
{
|
11 |
bitWrite(matrixState[colIndex * 2 + 1], rowIndex, !digitalRead(inputPins[matrixColPins[rowIndex]]));
|
12 |
}
|
13 |
|
14 |
pinMode(inputPins[matrixColPins[rowIndex]], INPUT);
|
15 |
}
|
16 |
|
17 |
// disable the column
|
18 |
pinMode(inputPins[matrixRowPins[colIndex]], INPUT);
|
19 |
}
|
}
void ReportMatrixStateI2c(int fromCol, int colCount)
{
for (int colIndex = fromCol; colIndex < fromCol + colCount; colIndex++)
{
i2cWrite(matrixState[colIndex * 2]);
i2cWrite(matrixState[colIndex * 2 + 1]);
}
}
void SendMatrixHIDReport()
{
1 |
// for (int i = 0; i < 32; i++)
|
2 |
// {
|
3 |
// Serial.print(matrixState[i]);
|
4 |
// Serial.print(" ");
|
5 |
// }
|
6 |
// Serial.println();
|
7 |
|
8 |
for (int colIndex = 0; colIndex < matrixColCount; colIndex++)
|
9 |
{
|
10 |
for (int rowIndex = 0; rowIndex < matrixRowCount; rowIndex++)
|
11 |
{
|
12 |
if (rowIndex < 8)
|
13 |
{
|
14 |
//Serial.print(bitRead(matrixState[colIndex*2], rowIndex));
|
15 |
JoystickInput(matrixButtonAssignments[colIndex * 16 + rowIndex], bitRead(matrixState[colIndex * 2], rowIndex));
|
16 |
}
|
17 |
else
|
18 |
{
|
19 |
//Serial.print(bitRead(matrixState[colIndex*2+1], rowIndex));
|
20 |
JoystickInput(matrixButtonAssignments[colIndex * 16 + rowIndex], bitRead(matrixState[colIndex * 2 + 1], rowIndex));
|
21 |
}
|
22 |
//
|
23 |
}
|
24 |
}
|
25 |
//Serial.println();
|
}