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