Skip to content

Commit 695f429

Browse files
Merge pull request #18 from talbotmcinnis/IntegrateMatrixSwitches
Integrated Dehuman's Matrix2Pos and Matrix3Pos
2 parents 2a34e3b + 1701402 commit 695f429

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

examples/Button_Matrix_Example/Button_Matrix_Example.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Name: Button_Matrix_Example.ino
2+
Name: Button_matrix_Example.ino
33
Created: 10/06/2019 2:27:18 PM
44
Author: Azza276
55
Button Matrix by: ChronoZoggt
@@ -18,6 +18,7 @@
1818

1919
#include <Arduino.h>
2020
#define DCSBIOS_DEFAULT_SERIAL //Use DCSBIOS_DEFAULT_SERIAL is IRQ does not work.
21+
#define USE_MATRIX_SWITCHES
2122
#include "DcsBios.h"
2223

2324

@@ -74,6 +75,8 @@ DcsBios::MatActionButton ufcClr("UFC_CLR", "TOGGLE", &in_mat[3][0]);
7475
DcsBios::MatActionButton ufc0("UFC_0", "TOGGLE", &in_mat[3][1]);
7576
DcsBios::MatActionButton ufcEnt("UFC_ENT", "TOGGLE", &in_mat[3][2]);*/
7677

78+
DcsBios::Matrix2Pos aapCdupwr("AAP_CDUPWR", 2, 2);
79+
DcsBios::Matrix3Pos ahcpAltSce("AHCP_ALT_SCE", 1, 2, 1, 2);
7780

7881
// The setup() function runs once each time the micro-controller starts
7982
void setup() {
@@ -101,4 +104,4 @@ void loop() {
101104
digitalWrite(colPins[x], HIGH); //set the current column output to a high +V source to so it is not recognised as a press when the other columns are LOW.
102105
}
103106
DcsBios::loop(); //DcsBios loop is here.
104-
}
107+
}

src/DcsBios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ do not come with their own build system, we are just putting everything into the
128128
#include "internal/BcdWheels.h"
129129
#include "internal/AnalogMultiPos.h"
130130
#include "internal/RotarySwitch.h"
131+
#include "internal/MatrixSwitches.h"
131132

132133
namespace DcsBios {
133134
template<unsigned int first, unsigned int second>

src/internal/MatrixSwitches.h

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#ifndef __DCSBIOS_MATRIX_SWITCHES_H
2+
#define __DCSBIOS_MATRIX_SWITCHES_H
3+
4+
#ifdef USE_MATRIX_SWITCHES
5+
6+
#include <math.h>
7+
#include "Arduino.h"
8+
#include "SwitchMatrix.h" // https://github.com/dagoston93/SwitchMatrix
9+
10+
SwitchMatrix swPanel = SwitchMatrix();
11+
12+
namespace DcsBios {
13+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
14+
class Matrix2PosT : PollingInput, public ResettableInput {
15+
private:
16+
const char* msg_;
17+
char row_;
18+
char col_;
19+
char lastState_;
20+
bool reverse_;
21+
void init_(const char* msg, char row, char col, bool reverse) {
22+
msg_ = msg;
23+
row_ = row;
24+
col_ = col;
25+
lastState_ = swPanel.GetSwitchState(row_, col_);
26+
reverse_ = reverse;
27+
}
28+
void resetState()
29+
{
30+
lastState_ = (lastState_==0)?-1:0;
31+
}
32+
void pollInput() {
33+
char state = swPanel.GetSwitchState(row_, col_);
34+
if (reverse_) state = !state;
35+
if (state != lastState_) {
36+
if (tryToSendDcsBiosMessage(msg_, state == false ? "0" : "1")) {
37+
lastState_ = state;
38+
}
39+
}
40+
}
41+
public:
42+
Matrix2PosT(const char* msg, char row, char col, bool reverse) : PollingInput(pollIntervalMs)
43+
{
44+
init_(msg, row, col, reverse);
45+
}
46+
47+
Matrix2PosT(const char* msg, char row, char col) : PollingInput(pollIntervalMs)
48+
{
49+
init_(msg, row, col, false);
50+
}
51+
52+
void resetThisState()
53+
{
54+
this->resetState();
55+
}
56+
};
57+
typedef Matrix2PosT<> Matrix2Pos;
58+
59+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
60+
class Matrix3PosT : PollingInput, public ResettableInput {
61+
private:
62+
const char* msg_;
63+
char rowA_;
64+
char colA_;
65+
char rowB_;
66+
char colB_;
67+
char lastState_;
68+
char readState() {
69+
if (swPanel.GetSwitchState(rowA_, colA_) == true) return 0;
70+
if (swPanel.GetSwitchState(rowB_, colB_) == true) return 2;
71+
return 1;
72+
}
73+
void resetState()
74+
{
75+
lastState_ = (lastState_==0)?-1:0;
76+
}
77+
void pollInput() {
78+
char state = readState();
79+
if (state != lastState_) {
80+
if (state == 0) {
81+
if (tryToSendDcsBiosMessage(msg_, "0")) {
82+
lastState_ = state;
83+
}
84+
}
85+
else if (state == 1) {
86+
if (tryToSendDcsBiosMessage(msg_, "1")) {
87+
lastState_ = state;
88+
}
89+
}
90+
else if (state == 2) {
91+
if(tryToSendDcsBiosMessage(msg_, "2")){
92+
lastState_ = state;
93+
}
94+
}
95+
}
96+
}
97+
public:
98+
Matrix3PosT(const char* msg, char rowA, char colA, char rowB, char colB) : PollingInput(pollIntervalMs)
99+
{
100+
msg_ = msg;
101+
colA_ = colA;
102+
rowA_ = rowA;
103+
colB_ = colB;
104+
rowB_ = rowB;
105+
lastState_ = readState();
106+
}
107+
108+
void resetThisState()
109+
{
110+
this->resetState();
111+
}
112+
};
113+
typedef Matrix3PosT<> Matrix3Pos;
114+
}
115+
#endif
116+
#endif

0 commit comments

Comments
 (0)