-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTruthTableEngineClass.cpp
More file actions
193 lines (183 loc) · 5.63 KB
/
TruthTableEngineClass.cpp
File metadata and controls
193 lines (183 loc) · 5.63 KB
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
/**
* TruthTableEngineClass.cpp
*
* Created on : 7 nov. 2013
* Author : Nico Verduin
* Email : info@verelec.com
* Website : www.verelec.nl
*
* Revision Control
*
* Latest Revsion
* ____________________
*
* Revision : $Revision: 3 $
* Date : $Date: 2013-11-11 14:08:57 +0100 (Mon, 11 Nov 2013) $
* Author : $Author: Nico $
*
*/
#include "TruthTableEngineClass.h"
/**
* @name TruthTableEngine()
* constructor
*/
TruthTableEngine::TruthTableEngine() {
}
/**
* @name setup(EngineData *inp)
* @parameter inp pointer to EngineData structure
* sets the input and output ports
*/
void TruthTableEngine::setup(EngineData *inp) {
//
// setup digital input and output pins
//
for (uint8_t i = 0; i < inp->numberOfDigitalPins; i++) {
pinMode(inp->inputArray[i], INPUT);
}
for (uint8_t i = 0; i < inp->numberOfOutputPins; i++) {
pinMode(inp->outputArray[i], OUTPUT);
}
}
/**
* @name readInputs(EngineData *inp)
* @parameter inp pointer to EngineData structure
* reads the inputs from the digital and analog pins
*/
void TruthTableEngine::readInputs(EngineData *inp) {
//
// read the digital inputs
//
for (uint8_t i = 0; i < inp->numberOfDigitalPins; i++) {
inp->digitalInputArray[i] = digitalRead(inp->inputArray[i]);
}
//
// read the analog inputs
//
for (uint8_t i = 0; i < inp->numberOfAnalogPins; i++) {
inp->analogInputArray[i] = analogRead(inp->analogArray[i]);
}
}
/**
* @name preProcess(EngineData *inp)
* @param inp pointer to EngineData structure
* preprocesses the data by validating the input pins against the active logic levels
* and analog values if they fit within the bandwidth
*/
void TruthTableEngine::preProcess(EngineData *inp) {
inp->testByte = 0; // no inputs processed yet
//
// process digital inputs
//
for (uint8_t i = 0; i < inp->numberOfDigitalPins; i++) {
//
// shift the bits in testByte one bit left first and then add the digital reading of the pin
//
inp->testByte = inp->testByte << 1; // shift left one bit
if(inp->digitalInputArray[i] == inp->activeInputArray[i]) { // set right bit setting in test byte
inp->testByte += 1;
} else {
inp->testByte += 0;
}
}
//
// preprocessing analog inputs
//
for (uint8_t i = 0; i < inp->numberOfAnalogPins; i++){
//
// check if within bandwidth
//
inp->testByte = inp->testByte << 1; // shift one bit left
uint8_t j = (i * 2 * sizeof(unsigned int)); // determine offsets
if (inp->analogInputArray[i] >= inp->bandwidthArray[j]) { // value larger/equal than minimum value
if (inp->analogInputArray[i] <= inp->bandwidthArray[j+1]) { // value smaller/equal than maximum value
//
// fits in the bandwidth
//
inp->testByte = inp->testByte + 1; // set lowest bit to 1
}
}
}
}
/**
* @name process(EngineData *inp)
* @param inp pointer to EngineData structure
* searches the condition table for a valid (= equal) entry like testByte and
* puts the result in output byte
*/
void TruthTableEngine::process(EngineData *inp) {
//
// Check if there is an entry with a corresponding match in the engine array
//
inp->outputByte = 0L;
for (uint8_t i = 0; i < inp->numberOfConditions; i++){
if (inp->engineArray[i].inputs == inp->testByte) {
//
// we found a corresponding entry
//
inp->outputByte = inp->engineArray[i].outputs;
//
// and we can break out of the loop
//
break;
}
}
}
/**
* @name postProcess(EngineData *inp)
* @param inp pointer to EngineData structure
* Breaks the output byte into the individual array entries
*/
void TruthTableEngine::postProcess(EngineData *inp) {
uint32_t wOutputByte; // work value so outputByte stays intact
wOutputByte = inp->outputByte;
//
// now the post processing. copy each bit value from outputByte into the output array
//
for (int i = inp->numberOfOutputPins - 1; i >=0; i--) {
//
// mask all but the least significant bit and set the digitalOutput Array
//
if ((wOutputByte & 0x0001) == 1) {
inp->digitalOutputArray[i] = 1;
} else {
inp->digitalOutputArray[i] = 0;
}
//
// right shift outputByte one bit
//
wOutputByte = wOutputByte >> 1;
}
}
/**
* @name writeOutputs(EngineData *inp)
* @param inp pointer to EngineData structure
* copies the digital output array to the corresponding pin outputs
*/
void TruthTableEngine::writeOutputs(EngineData *inp) {
//
// now the post processing. copy each digital output to the correct pin and set the active level
//
for (uint8_t i = 0; i < inp->numberOfOutputPins; i++) {
//
// test each bit
//
if (inp->digitalOutputArray[i] == 1) {
digitalWrite(inp->outputArray[i], inp->activeOutputArray[i]);
} else {
digitalWrite(inp->outputArray[i], !inp->activeOutputArray[i]);
}
}
}
/**
* @name processAutomatic(EngineData *inp)
* @param inp pointer to EngineData structure
* processes the whole sequence from reading inputs to writing outputs
*/
void TruthTableEngine::processAutomatic(EngineData *inp) {
readInputs(inp); // read inputs
preProcess(inp); // pre process into testbyte
process(inp); // search for valid combination
postProcess(inp); // copy result to output array
writeOutputs(inp); // write digital pins based on active levels
}