-
Notifications
You must be signed in to change notification settings - Fork 0
/
phAdjuster.cpp
334 lines (258 loc) · 5.95 KB
/
phAdjuster.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "phAdjuster.h"
#include "globalMtx.h"
#include "i2c_iO.h"
#include "HabitatConfiguration.h"
#include <wiringPiI2C.h>
#include <cmath>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <fstream>
#define DEFAULT_ADDR 0x60
using namespace std;
std::vector<phAdjuster*> phAdjusters;
phAdjuster::phAdjuster(int slotPosition)
{
this->slotPosition = slotPosition;
disconnected = false;
busMtx.lock();
deviceID = switchToSlot(slotPosition, DEFAULT_ADDR);
if (wiringPiI2CWriteReg8(deviceID, PCA9685_MODE1, 0x0) == -1)
{
disconnected = true;
}
busMtx.unlock();
initPHAdjuster();
stopPump(ADJUSTER_ONE, PH_UP);
stopPump(ADJUSTER_ONE, PH_DOWN);
stopPump(ADJUSTER_TWO, PH_UP);
stopPump(ADJUSTER_TWO, PH_DOWN);
for (int i = 0; i < 2; i++)
{
adjOperating[i] = false;
targetPHVal[i] = 0;
adjMode[i] = 0;
linkedPHSensorSlot[i] = -1;
}
}
phAdjuster::~phAdjuster()
{
}
void phAdjuster::initPHAdjuster()
{
busMtx.lock();
deviceID = switchToSlot(slotPosition, DEFAULT_ADDR);
int oldmode = wiringPiI2CReadReg8(deviceID, PCA9685_MODE1);
wiringPiI2CWriteReg8(deviceID, PCA9685_MODE1, oldmode | 0xa1); //turn on register auto increment (e.g. write 16 Bits at once instead of 8 Bits twice)
busMtx.unlock();
}
//void phAdjuster::setPWMFreq()
//{
// float freq = 1600 * 0.9;
// float osciClock = 25000000;
// int prescale;
// int oldmode;
// int newmode;
//
// osciClock /= 4096;
// osciClock /= freq;
// osciClock -= 1;
//
// prescale = floor(osciClock + 0.5);
//
// busMtx.lock();
// oldmode = wiringPiI2CReadReg8(deviceID, PCA9685_MODE1);
// newmode = (oldmode & 0x7F) | 0x10;
//
// wiringPiI2CWriteReg8(deviceID, PCA9685_MODE1, newmode); //sleep or else prescale register is blocked
// wiringPiI2CWriteReg8(deviceID, PCA9685_PRESCALE, prescale); //write the prescale
// busMtx.unlock();
//}
void phAdjuster::getMotor(int adjusterID, int motorID)
{
if (adjusterID == ADJUSTER_ONE)
{
if (motorID == PH_DOWN)
{
PWMPin = 8;
IN1Pin = 10;
IN2Pin = 9;
}
else if (motorID == PH_UP)
{
PWMPin = 13;
IN1Pin = 12;
IN2Pin = 11;
}
}
else if (adjusterID == ADJUSTER_TWO)
{
if (motorID == PH_DOWN)
{
PWMPin = 2;
IN1Pin = 4;
IN2Pin = 3;
}
else if (motorID == PH_UP)
{
PWMPin = 7;
IN1Pin = 5;
IN2Pin = 6;
}
}
}
bool phAdjuster::startPump(int adjusterID, int motorID, int speedVal)
{
getMotor(adjusterID, motorID);
setSpeed(speedVal * 16);
//vorwärts (anders rum rückwärts)
setPin(IN2Pin, LOW);
setPin(IN1Pin, HIGH);
}
bool phAdjuster::stopPump(int adjusterID, int motorID)
{
getMotor(adjusterID, motorID);
setPin(IN1Pin, LOW);
setPin(IN2Pin, LOW);
}
void phAdjuster::setPWM(int pin, int on, int off)
{
busMtx.lock();
deviceID = switchToSlot(slotPosition, DEFAULT_ADDR);
wiringPiI2CWriteReg16(deviceID, LED0_ON + 4*pin, on);
wiringPiI2CWriteReg16(deviceID, LED0_OFF + 4*pin, off);
busMtx.unlock();
}
void phAdjuster::setSpeed(int speedVal)
{
if (speedVal > 4095)
{
setPWM(PWMPin, 4096, 0);
}
else
{
setPWM(PWMPin, 0, speedVal);
}
}
void phAdjuster::setPin(int pin, bool val)
{
if (val == LOW)
{
setPWM(pin, 0, 0);
}
else
{
setPWM(pin, 4096, 0);
}
}
void phAdjuster::loadSettingsIfExist(int deviceObjectID)
{
ifstream inFile(getFilepath(), ios::in | ios::binary);
if (inFile.good())
{
inFile.read((char *) phAdjusters[deviceObjectID], sizeof(phAdjuster));
inFile.close();
}
}
//simple dump of the object variables(settings) as a binary file. Fine because the object just holds primitiv data types.
//Not working, if you add pointer variables, but not needed for now
void phAdjuster::saveSettings()
{
int deviceObjectID = deviceObjectIdList[slotPosition - 1];
ofstream outFile(getFilepath(), ios::out | ios::binary);
outFile.write((char *) phAdjusters[deviceObjectID], sizeof(phAdjuster));
outFile.close();
}
std::string phAdjuster::getFilepath()
{
char buff[50];
snprintf(buff, sizeof(buff), "device_settings/slot_%d_settings.bin", slotPosition);
string path = buff;
return path;
}
void phAdjuster::setOperatingStatus(int adjusterID, bool active)
{
adjOperating[adjusterID - 1] = active;
saveSettings();
}
bool phAdjuster::getOperatingStatus(int adjusterID)
{
return adjOperating[adjusterID - 1];
}
void phAdjuster::setTargetPHVal(float phVal, int adjusterID)
{
targetPHVal[adjusterID - 1] = phVal;
saveSettings();
}
float phAdjuster::getTargetPHVal(int adjusterID)
{
return targetPHVal[adjusterID - 1];
}
void phAdjuster::assignToPHSlot(int slotPosition, int adjusterID)
{
linkedPHSensorSlot[adjusterID - 1] = slotPosition;
saveSettings();
}
int phAdjuster::getAssignedPHSlot(int adjusterID)
{
return linkedPHSensorSlot[adjusterID - 1];
}
void phAdjuster::setMode(int adjusterID, int mode)
{
if (mode == INTERVAL)
{
dateTillNextTick = time(NULL);
struct tm *date = localtime(&dateTillNextTick);
date->tm_mday += 1;
date->tm_isdst = -1;
dateTillNextTick = mktime(date);
targetPHVal[adjusterID] = upperPHLimit[adjusterID];
increaseFlag[adjusterID] = false;
}
adjMode[adjusterID - 1] = mode;
saveSettings();
}
int phAdjuster::getMode(int adjusterID)
{
return adjMode[adjusterID - 1];
}
bool phAdjuster::checkConnection()
{
return disconnected;
}
int phAdjuster::getSlotPosition()
{
return slotPosition;
}
float phAdjuster::getLowerPHLimit(int adjusterID)
{
return lowerPHLimit[adjusterID];
}
void phAdjuster::setLowerPHLimit(int adjusterID, float phVal)
{
lowerPHLimit[adjusterID] = phVal;
}
float phAdjuster::getUpperPHLimit(int adjusterID)
{
return upperPHLimit[adjusterID];
}
void phAdjuster::setUpperPHLimit(int adjusterID, float phVal)
{
upperPHLimit[adjusterID] = phVal;
}
bool phAdjuster::getIncreaseFlag(int adjusterID)
{
return increaseFlag[adjusterID];
}
void phAdjuster::setIncreaseFlag(int adjusterID, bool increase)
{
increaseFlag[adjusterID] = increase;
}
time_t phAdjuster::getDateForNextTick()
{
return dateTillNextTick;
}
void phAdjuster::setDateForNextTick(time_t nextDate)
{
dateTillNextTick = nextDate;
}