-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_functions.cpp
155 lines (148 loc) · 4.95 KB
/
test_functions.cpp
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* © 2023, Peter Cole. All rights reserved.
*
* This file is part of EX-IOExpander.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "globals.h"
#include "test_functions.h"
#include "i2c_functions.h"
#include "pin_io_functions.h"
bool analogueTesting = false; // Flag that analogue input testing is enabled/disabled
bool inputTesting = false; // Flag that digital input testing without pullups is enabled/disabled
bool outputTesting = false; // Flag that digital output testing is enabled/disabled
bool pullupTesting = false; // Flag that digital input testing with pullups is enabled/disabled
/*
* Testing functions below, just pass true/false to enable/disable the appropriate testing
* Note enabling any of these will disable Wire() (providing the library supports it) so the
* device will need to be rebooted once testing is completed to enable it again.
*/
void testAnalogue(bool enable) {
if (enable) {
USB_SERIAL.println(F("Analogue input testing enabled, I2C connection disabled, diags enabled, reboot once testing complete"));
setupComplete = true;
disableWire();
testInput(false);
testOutput(false);
testPullup(false);
diag = true;
analogueTesting = true;
for (uint8_t pin = 0; pin < numPins; pin++) {
if (bitRead(pinMap[pin].capability, ANALOGUE_INPUT)) {
exioPins[pin].enable = 1;
exioPins[pin].mode = MODE_ANALOGUE;
exioPins[pin].direction = 1;
}
}
} else {
analogueTesting = false;
diag = false;
initialisePins();
}
}
void testInput(bool enable) {
if (enable) {
USB_SERIAL.println(F("Input testing (no pullups) enabled, I2C connection disabled, diags enabled, reboot once testing complete"));
setupComplete = true;
disableWire();
testAnalogue(false);
testOutput(false);
testPullup(false);
diag = true;
inputTesting = true;
for (uint8_t pin = 0; pin < numPins; pin++) {
if (bitRead(pinMap[pin].capability, DIGITAL_INPUT)) {
exioPins[pin].enable = 1;
exioPins[pin].mode = MODE_DIGITAL;
exioPins[pin].pullup = 0;
exioPins[pin].direction = 1;
}
}
} else {
inputTesting = false;
diag = false;
initialisePins();
}
}
void testOutput(bool enable) {
if (enable) {
USB_SERIAL.println(F("Output testing enabled, I2C connection disabled, diags enabled, reboot once testing complete"));
setupComplete = true;
disableWire();
testAnalogue(false);
testInput(false);
testPullup(false);
diag = true;
outputTesting = true;
for (uint8_t pin = 0; pin < numPins; pin++) {
if (bitRead(pinMap[pin].capability, DIGITAL_OUTPUT)) {
exioPins[pin].enable = 1;
exioPins[pin].mode = DIGITAL_OUTPUT;
exioPins[pin].direction = 0;
}
}
} else {
outputTesting = false;
diag = false;
initialisePins();
}
}
void testPullup(bool enable) {
if (enable) {
USB_SERIAL.println(F("Pullup input testing enabled, I2C connection disabled, diags enabled, reboot once testing complete"));
setupComplete = true;
disableWire();
testAnalogue(false);
testOutput(false);
testInput(false);
diag = true;
pullupTesting = true;
for (uint8_t pin = 0; pin < numPins; pin++) {
if (bitRead(pinMap[pin].capability, DIGITAL_INPUT)) {
exioPins[pin].enable = 1;
exioPins[pin].mode = MODE_DIGITAL;
exioPins[pin].pullup = 1;
exioPins[pin].direction = 1;
}
}
} else {
pullupTesting = false;
diag = false;
initialisePins();
}
}
void testServo(uint8_t vpin, uint16_t value, uint8_t profile) {
if (firstVpin > 0) {
USB_SERIAL.println(F("EX-IOExpander has been connected and configured, please disconnect from EX-CommandStation and reboot"));
} else if (analogueTesting || inputTesting || outputTesting || pullupTesting) {
USB_SERIAL.println(F("Please disable all other testing first"));
} else {
String pinLabel = pinNameMap[vpin].pinLabel;
USB_SERIAL.print(F("Test move servo or dim LED - vpin|physicalPin|value|profile:"));
USB_SERIAL.print(vpin);
USB_SERIAL.print(F("|"));
USB_SERIAL.print(pinLabel);
USB_SERIAL.print(F("|"));
USB_SERIAL.print(value);
USB_SERIAL.print(F("|"));
USB_SERIAL.println(profile);
if (!setupComplete) {
setupComplete = true;
}
disableWire();
writeAnalogue(vpin, value, profile);
}
}