matrixSelector.cpp
6.1 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
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
#include "matrixSelector.h"
#include "config.h"
#include <arduino.h>
#include <Streaming.h>
#include <EEPROM.h>
matrixSelector::matrixSelector(int _threshold)
{
// Serial.begin(115200);
// threshold = 60;
}
void matrixSelector::initial()
{
for (int i = 0; i < 10; i++)
{
pinMode(select_pin[i], OUTPUT);
}
pinMode(35, OUTPUT);
pinMode(34, OUTPUT);
// digitalWrite(35 , HIGH );
// digitalWrite(34 , HIGH );
EEPROM.begin(4000);
// EEPROM.setMaxAllowedWrites(maxAllowedWrites);
// start reading from position memBase (address 0) of the EEPROM. Set maximumSize to EEPROMSizeUno
// Writes before membase or beyond EEPROMSizeUno will only give errors when _EEPROMEX_DEBUG is set
// EEPROM.setMemPool(memBase, EEPROMSizeMega);
// addressInt = EEPROM.getAddress(sizeof(int));
// Serial.println(addressInt);
//
int x = readIntFromEEPROM(3000);
if (x > 0)
threshold = x;
else
threshold = 20;
Serial.println("matrix scanner starts ");
// Serial.print (threshold);
}
long matrixSelector::getadc(int pin, boolean txEn = true)
{
pin--;
int a, b;
a = (pin / 10);
b = (pin % 10);
if (txEn)
{
if (b % 2 == 1)
{
digitalWrite(35, LOW);
digitalWrite(34, HIGH);
}
else
{
digitalWrite(35, HIGH);
digitalWrite(34, LOW);
}
}
for (int i = 0; i < 10; i++)
{
// delay(1);
if (i == a)//||i-1==a)
{
pinMode(select_pin[i], OUTPUT);
digitalWrite(select_pin[i], HIGH); // Serial.print("h");Serial.print(select_pin[i]);
delayMicroseconds(40*1000/ledcount);
}
else
{
pinMode(select_pin[i], OUTPUT);
digitalWrite(select_pin[i], LOW); // Serial.print("L");Serial.print(select_pin[i]);
}
}
// Serial.println(" ");
long results = 0;
for (int j = 0; j < 10; j++)
{
results += analogRead(sense_pin[b]);
// delayMicroseconds(100);
}
results = results / 10;
digitalWrite(select_pin[a], LOW);
digitalWrite(35, LOW);
digitalWrite(34, LOW);
//if (results == 0 && txEn)
// results = 9999;
return results;
}
bool matrixSelector::getstatus(int pin)
{
float max = getmax(pin);
float min = getmin(pin);
float sample = getadc(pin);
float result = ((max - sample) / (max - min)) * 100;
if (max <100)
result = 0;
if (diagnosMode)
Serial << "\r\npin:" << pin << " max:" << max << " min: " << min << " sample : " << sample << " sen% : " << result << "";
//if (factoryMode && sample<2)
// return false;
if (result > getthreshold(pin))
return true;
else
return false;
}
void matrixSelector::calibrate(int pin, int mode)
{
// mode 1 is max
// mode -1 is min
long sample = 0;
int maxadr = addressInt + (pin * 2);
int minadr = addressInt + (pin * 2) + 300;
// while (!EEPROM.isReady()){}
if (mode == 1)
{
sample = getadc(pin, true);
// if (sample<9999)
writeIntIntoEEPROM(maxadr, sample, true);
}
if (mode == -1)
{
sample = getadc(pin, false);
//if (sample < 9999)
writeIntIntoEEPROM(minadr, sample, true);
}
}
int matrixSelector::getmax(int pin)
{
int adr = addressInt + (pin * 2);
// while (!EEPROM.isReady()){}
int max = readIntFromEEPROM(adr);
return max;
}
int matrixSelector::getmin(int pin)
{
int adr = addressInt + (pin * 2) + 300;
// while (!EEPROM.isReady()){}
int min = readIntFromEEPROM(adr);
return min;
}
int matrixSelector::getthreshold(int pin)
{
int adr = addressInt + (pin * 2) + 600;
int threshold = readIntFromEEPROM(adr);
if (threshold == 0 || threshold > 100)
threshold = 20;
return threshold;
}
void matrixSelector::setcalnum(HardwareSerial *sl, int startpin, int endpin, int max, int min, int threshold)
{
// 0 mean no change
for (int pin = startpin; pin <= endpin; pin++)
{
int maxadr = addressInt + (pin * 2);
int minadr = addressInt + (pin * 2) + 300;
int thresholdadr = addressInt + (pin * 2) + 600;
// while (!EEPROM.isReady()){}
*sl << pin <<","<<max<<","<<min<<","<<threshold<<"\r\n";
if (max > 0)
writeIntIntoEEPROM(maxadr, max, true);
if (min >= 0)
writeIntIntoEEPROM(minadr, min, true);
if (threshold > 0)
writeIntIntoEEPROM(thresholdadr, threshold, true);
}
EEPROMcommit();
}
void matrixSelector::getcalnum(HardwareSerial *sl, int startpin, int endpin, int max, int min, int threshold)
{
// 0 mean no change
for (int pin = startpin; pin <= endpin; pin++)
{
int maxadr = addressInt + (pin * 2);
int minadr = addressInt + (pin * 2) + 300;
int thresholdadr = addressInt + (pin * 2) + 600;
sl->print("(");
sl->print(pin);
sl->print(",");
sl->print(readIntFromEEPROM(maxadr));
sl->print(",");
sl->print(readIntFromEEPROM(minadr));
sl->print(",");
sl->print(readIntFromEEPROM(thresholdadr));
sl->print(",");
sl->print(getadc(pin));
sl->print(") ");
}
}
void matrixSelector::writeIntIntoEEPROM(int address, int number, bool nocommit)
{
EEPROM.write(address, number >> 8);
EEPROM.write(address + 1, number & 0xFF);
if (!nocommit)
EEPROM.commit();
}
void matrixSelector::writeShortIntoEEPROM(int address, short number, bool nocommit)
{
EEPROM.write(address, number);
if (!nocommit)
EEPROM.commit();
}
void matrixSelector::writeIntIntoEEPROM(int address, int number)
{
writeIntIntoEEPROM(address, number, false);
}
void matrixSelector::writeShortIntoEEPROM(int address, short number)
{
writeShortIntoEEPROM(address, number, false);
}
void matrixSelector::EEPROMcommit()
{
EEPROM.commit();
}
int matrixSelector::readIntFromEEPROM(int address)
{
return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
short matrixSelector::readShortFromEEPROM(int address)
{
return EEPROM.read(address);
}