forked from noisymime/speeduino
/
utilities.ino
284 lines (265 loc) · 11.5 KB
/
utilities.ino
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright (C) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
/** @file
* Custom Programmable I/O.
* The config related to Programmable I/O is found on configPage13 (of type @ref config13).
*/
#include <avr/pgmspace.h>
#include "globals.h"
#include "utilities.h"
#include "decoders.h"
#include "comms.h"
#include "logger.h"
#include "scheduler.h"
#include "scheduledIO.h"
#include "speeduino.h"
uint8_t ioDelay[sizeof(configPage13.outputPin)];
uint8_t ioOutDelay[sizeof(configPage13.outputPin)];
uint8_t pinIsValid = 0;
uint8_t currentRuleStatus = 0;
/** Translate between the pin list that appears in TS and the actual pin numbers.
For the **digital IO**, this will simply return the same number as the rawPin value as those are mapped directly.
For **analog pins**, it will translate them into the correct internal pin number.
* @param rawPin - High level pin number
* @return Translated / usable pin number
*/
byte pinTranslate(byte rawPin)
{
byte outputPin = rawPin;
if(rawPin > BOARD_MAX_DIGITAL_PINS) { outputPin = A8 + (outputPin - BOARD_MAX_DIGITAL_PINS - 1); }
return outputPin;
}
/** Translate a pin number (0 - 22) to the relevant Ax (analog) pin reference.
* This is required as some ARM chips do not have all analog pins in order (EG pin A15 != A14 + 1).
* */
byte pinTranslateAnalog(byte rawPin)
{
byte outputPin = rawPin;
switch(rawPin)
{
case 0: outputPin = A0; break;
case 1: outputPin = A1; break;
case 2: outputPin = A2; break;
case 3: outputPin = A3; break;
case 4: outputPin = A4; break;
case 5: outputPin = A5; break;
case 6: outputPin = A6; break;
case 7: outputPin = A7; break;
case 8: outputPin = A8; break;
case 9: outputPin = A9; break;
case 10: outputPin = A10; break;
case 11: outputPin = A11; break;
case 12: outputPin = A12; break;
case 13: outputPin = A13; break;
#if BOARD_MAX_ADC_PINS >= 14
case 14: outputPin = A14; break;
#endif
#if BOARD_MAX_ADC_PINS >= 15
case 15: outputPin = A15; break;
#endif
#if BOARD_MAX_ADC_PINS >= 16
case 16: outputPin = A16; break;
#endif
#if BOARD_MAX_ADC_PINS >= 17
case 17: outputPin = A17; break;
#endif
#if BOARD_MAX_ADC_PINS >= 18
case 18: outputPin = A18; break;
#endif
#if BOARD_MAX_ADC_PINS >= 19
case 19: outputPin = A19; break;
#endif
#if BOARD_MAX_ADC_PINS >= 20
case 20: outputPin = A20; break;
#endif
#if BOARD_MAX_ADC_PINS >= 21
case 21: outputPin = A21; break;
#endif
#if BOARD_MAX_ADC_PINS >= 22
case 22: outputPin = A22; break;
#endif
}
return outputPin;
}
void setResetControlPinState()
{
BIT_CLEAR(currentStatus.status3, BIT_STATUS3_RESET_PREVENT);
/* Setup reset control initial state */
switch (resetControl)
{
case RESET_CONTROL_PREVENT_WHEN_RUNNING:
/* Set the reset control pin LOW and change it to HIGH later when we get sync. */
digitalWrite(pinResetControl, LOW);
BIT_CLEAR(currentStatus.status3, BIT_STATUS3_RESET_PREVENT);
break;
case RESET_CONTROL_PREVENT_ALWAYS:
/* Set the reset control pin HIGH and never touch it again. */
digitalWrite(pinResetControl, HIGH);
BIT_SET(currentStatus.status3, BIT_STATUS3_RESET_PREVENT);
break;
case RESET_CONTROL_SERIAL_COMMAND:
/* Set the reset control pin HIGH. There currently isn't any practical difference
between this and PREVENT_ALWAYS but it doesn't hurt anything to have them separate. */
digitalWrite(pinResetControl, HIGH);
BIT_CLEAR(currentStatus.status3, BIT_STATUS3_RESET_PREVENT);
break;
}
}
//*********************************************************************************************************************************************************************************
void initialiseProgrammableIO()
{
uint8_t outputPin;
for (uint8_t y = 0; y < sizeof(configPage13.outputPin); y++)
{
ioDelay[y] = 0;
ioOutDelay[y] = 0;
outputPin = configPage13.outputPin[y];
if (outputPin > 0)
{
if ( outputPin >= 128 ) //Cascate rule usage
{
BIT_WRITE(currentStatus.outputsStatus, y, BIT_CHECK(configPage13.outputInverted, y));
BIT_SET(pinIsValid, y);
}
else if ( !pinIsUsed(outputPin) )
{
pinMode(outputPin, OUTPUT);
digitalWrite(outputPin, BIT_CHECK(configPage13.outputInverted, y));
BIT_WRITE(currentStatus.outputsStatus, y, BIT_CHECK(configPage13.outputInverted, y));
BIT_SET(pinIsValid, y);
}
else { BIT_CLEAR(pinIsValid, y); }
}
}
}
/** Check all (8) programmable I/O:s and carry out action on output pin as needed.
* Compare 2 (16 bit) vars in a way configured by @ref cmpOperation (see also @ref config13.operation).
* Use ProgrammableIOGetData() to get 2 vars to compare.
* Skip all programmable I/O:s where output pin is set 0 (meaning: not programmed).
*/
void checkProgrammableIO()
{
int16_t data, data2;
uint8_t dataRequested;
bool firstCheck, secondCheck;
for (uint8_t y = 0; y < sizeof(configPage13.outputPin); y++)
{
firstCheck = false;
secondCheck = false;
if ( BIT_CHECK(pinIsValid, y) ) //if outputPin == 0 it is disabled
{
dataRequested = configPage13.firstDataIn[y];
if ( dataRequested > 239U ) //Somehow using 239 uses 9 bytes of RAM, why??
{
dataRequested -= REUSE_RULES;
if ( dataRequested <= sizeof(configPage13.outputPin) ) { data = BIT_CHECK(currentRuleStatus, dataRequested); }
else { data = 0; }
}
else { data = ProgrammableIOGetData(dataRequested); }
data2 = configPage13.firstTarget[y];
if ( (configPage13.operation[y].firstCompType == COMPARATOR_EQUAL) && (data == data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_NOT_EQUAL) && (data != data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_GREATER) && (data > data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_GREATER_EQUAL) && (data >= data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_LESS) && (data < data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_LESS_EQUAL) && (data <= data2) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_AND) && ((data & data2) != 0) ) { firstCheck = true; }
else if ( (configPage13.operation[y].firstCompType == COMPARATOR_XOR) && ((data ^ data2) != 0) ) { firstCheck = true; }
if (configPage13.operation[y].bitwise != BITWISE_DISABLED)
{
dataRequested = configPage13.secondDataIn[y];
if ( dataRequested <= (REUSE_RULES + sizeof(configPage13.outputPin)) ) //Failsafe check
{
if ( dataRequested > 239U ) //Somehow using 239 uses 9 bytes of RAM, why??
{
dataRequested -= REUSE_RULES;
data = BIT_CHECK(currentRuleStatus, dataRequested);
}
else { data = ProgrammableIOGetData(dataRequested); }
data2 = configPage13.secondTarget[y];
if ( (configPage13.operation[y].secondCompType == COMPARATOR_EQUAL) && (data == data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_NOT_EQUAL) && (data != data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_GREATER) && (data > data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_GREATER_EQUAL) && (data >= data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_LESS) && (data < data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_LESS_EQUAL) && (data <= data2) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_AND) && ((data & data2) != 0) ) { secondCheck = true; }
else if ( (configPage13.operation[y].secondCompType == COMPARATOR_XOR) && ((data ^ data2) != 0) ) { secondCheck = true; }
if (configPage13.operation[y].bitwise == BITWISE_AND) { firstCheck &= secondCheck; }
if (configPage13.operation[y].bitwise == BITWISE_OR) { firstCheck |= secondCheck; }
if (configPage13.operation[y].bitwise == BITWISE_XOR) { firstCheck ^= secondCheck; }
}
}
//If the limiting time is active(>0) and using maximum time
if (BIT_CHECK(configPage13.kindOfLimiting, y))
{
if(firstCheck)
{
if ((configPage13.outputTimeLimit[y] != 0) && (ioOutDelay[y] >= configPage13.outputTimeLimit[y])) { firstCheck = false; } //Time has counted, disable the output
}
else
{
//Released before Maximum time, set delay to maximum to flip the output next
if(BIT_CHECK(currentStatus.outputsStatus, y)) { ioOutDelay[y] = configPage13.outputTimeLimit[y]; }
else { ioOutDelay[y] = 0; } //Reset the counter for next time
}
}
if ( (firstCheck == true) && (configPage13.outputDelay[y] < 255) )
{
if (ioDelay[y] >= configPage13.outputDelay[y])
{
bool bitStatus = BIT_CHECK(configPage13.outputInverted, y) ^ firstCheck;
if (BIT_CHECK(currentStatus.outputsStatus, y) && (ioOutDelay[y] < configPage13.outputTimeLimit[y])) { ioOutDelay[y]++; }
if (configPage13.outputPin[y] < 128) { digitalWrite(configPage13.outputPin[y], bitStatus); }
else { BIT_WRITE(currentRuleStatus, y, bitStatus); }
BIT_WRITE(currentStatus.outputsStatus, y, bitStatus);
}
else { ioDelay[y]++; }
}
else
{
if (ioOutDelay[y] >= configPage13.outputTimeLimit[y])
{
bool bitStatus = BIT_CHECK(configPage13.outputInverted, y) ^ firstCheck;
if (configPage13.outputPin[y] < 128) { digitalWrite(configPage13.outputPin[y], bitStatus); }
else { BIT_WRITE(currentRuleStatus, y, bitStatus); }
BIT_WRITE(currentStatus.outputsStatus, y, bitStatus);
if(!BIT_CHECK(configPage13.kindOfLimiting, y)) { ioOutDelay[y] = 0; }
}
else { ioOutDelay[y]++; }
ioDelay[y] = 0;
}
}
}
}
/** Get single I/O data var (from currentStatus) for comparison.
* Uses member offset index @ref fsIntIndex to lookup realtime 'live' data from @ref currentStatus.
* @param index - Field index/number (?)
* @return 16 bit (int) result
*/
int16_t ProgrammableIOGetData(uint16_t index)
{
int16_t result;
if ( index < LOG_ENTRY_SIZE )
{
/*
for(uint8_t x = 0; x<sizeof(fsIntIndex); x++)
{
// Stop at desired field
if (pgm_read_byte(&(fsIntIndex[x])) == index) { break; }
}
if (x >= sizeof(fsIntIndex)) { result = getTSLogEntry(index); } // 8-bit, coerce to 16 bit result
else { result = word(getTSLogEntry(index+1), getTSLogEntry(index)); } // Assemble 2 bytes to word of 16 bit result
*/
if(is2ByteEntry(index)) { result = word(getTSLogEntry(index+1), getTSLogEntry(index)); }
else { result = getTSLogEntry(index); }
//Special cases for temperatures
if( (index == 6) || (index == 7) ) { result -= CALIBRATION_TEMPERATURE_OFFSET; }
}
else if ( index == 239U ) { result = (int16_t)max((uint32_t)runSecsX10, (uint32_t)32768); } //STM32 used std lib
else { result = -1; } //Index is bigger than fullStatus array
return result;
}