-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHydroSphereUtil.cpp
302 lines (279 loc) · 7.75 KB
/
HydroSphereUtil.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
/*
* HydroSphereUtil.cpp
*
* Created: 9/19/2014 10:02:11 AM
* Author: Neve
*/
#include <avr/wdt.h> // Watchdog timer
#include "HydroSphereRTC.h" // DO and Conductivity sensors
#include <RTClib.h> // RTC Clock
RTC_DS3234 HSU_RTClock(CLOCK_SS_PIN);
DateTime HSU_time; // Structure to hold a DateTime
float getDensity(float temp_C, float salinity) {
// salinity is in kg/m^3
// Density is on kg/m^3
float density_kgm3 = ( ( 999.842594 +
( 0.06793952 * temp_C ) +
( 0.009095290 * pow(temp_C,2) ) +
( 0.0001001685 * pow(temp_C,3) ) -
( 0.000001120083 * pow(temp_C,4) ) +
( 0.000000006536332 + pow(temp_C,5) ) ) +
( ( 0.824493 -
( 0.0040899 * temp_C ) +
( 0.000076438 * pow(temp_C,2) ) -
( 0.00000082467 * pow(temp_C,3) ) +
( 0.0000000053875 * pow(temp_C,4) ) ) * salinity ) +
( ( -0.00572466 +
( 0.00010227 * temp_C) -
( 0.0000016546 * pow(temp_C,2) ) *
pow(salinity,1.5) ) ) +
( 0.00048314 * pow(salinity,2) ) );
return density_kgm3;
}
float getDepth(float pressure_kpa, float density_kgm3) {
// returns depth in meters
// Standard atmospheric pressure 101.325 kPa
float density_lbft = density_kgm3 * 0.0624279606;
float pressure_psi = pressure_kpa * 0.1450377;
float pressure_lbft = pressure_psi * 144.0;
float depth_m = (0.3048 * pressure_lbft ) / ( density_lbft );
return depth_m;
}
uint16_t freeRam() {
extern int16_t __heap_start, *__brkval;
int16_t v;
return (int16_t) &v - (__brkval == 0 ? (int16_t) &__heap_start : (int16_t) __brkval);
}
/*
// lowPowerDelay(seconds) - Low Power Delay. Drops the system clock
// to its lowest setting and sleeps for (256*quarterSeconds) milliseconds.
*/
void lowPowerDelay(uint16_t delay_secs) {
const uint16_t MAX_DELAY = 4; // Longest we will delay without resetting watchdog
uint16_t delay_portion;
wdt_reset(); // Reset watchdog timer, needs to be reset at least every 8 seconds.
Serial.print("delaying "); Serial.print(delay_secs);
if ( delay_secs <= 2 ) return ;
delay_secs -= 2;
uint16_t oldClkPr = CLKPR; // save old system clock prescale
//Logger_SD::Instance()->msgL(DEBUG,F("Delaying about %u seconds. CLKPR = %X"),delay_secs,oldClkPr);
delay(1000); // Allow time for things to flush and write?
while ( delay_secs >= 1){
wdt_reset(); // Reset watchdog timer, needs to be reset at least every 8 seconds.
CLKPR = 0x80; // Tell the AtMega we want to change the system clock
CLKPR = 0x08; // 1/256 prescaler = 60KHz for a 16MHz crystal
if ( delay_secs > MAX_DELAY ) {
delay_portion = MAX_DELAY;
delay_secs -= MAX_DELAY;
}
else {
delay_portion = delay_secs;
delay_secs = 0;
}
delay((delay_portion)* 4); // since the clock is slowed way down, delay(n) now acts like delay(n*256)
CLKPR = 0x80; // Tell the AtMega we want to change the system clock
CLKPR = oldClkPr; // Restore old system clock prescale
}
delay(1000);
}
void deepSleep(uint16_t delay_secs){
/* See
https://www.sparkfun.com/tutorials/309
for much more
*/
// Turn off all peripherals?
Serial.flush();
lowPowerDelay(delay_secs);
// Turn on Peripherals
}
float get_analog_temperature(uint8_t analog_pin) {
float adc_val = analogRead(analog_pin);
adc_val *= .0048; //volts
adc_val *= 1000; //millivolts
float temp_C = ( 0.0512 * adc_val ) - 20.5128;
return temp_C;
}
void hs_console(){
/* HydroSphere console.
Functions include:
Download data files
clear data files
exit
*/
char read_byte;
do {
// Prompt
Serial.println(F("HydroSphere Console"));
Serial.println(F("1 - Download Data Files"));
Serial.println(F("2 - Delete Data Files"));
Serial.println(F("3 - Card Utilities"));
Serial.println(F("0 - Exit console mode"));
// Look for and process input
read_byte = getByte0(true);
switch (read_byte){
case '1':
HSconsoleDownloadData();
break;
case '2':
HSconsoleDeleteData();
break;
case '3':
HSconsoleUtilities();
break;
case '0':
Serial.println(F("Exiting console mode"));
return;
break;
default:
continue;
}
} while(1);
}
void HSconsoleDownloadData(){
char read_byte;
do
{
// Prompt for which file to download
Serial.println(F("Select file to download:"));
Serial.print("1 - "); Serial.println(SCHED1_FILE);
Serial.print("2 - "); Serial.println(SCHED2_FILE);
Serial.print("3 - "); Serial.println(SCHED3_FILE);
Serial.print("9 - "); Serial.println(LOG_FILE);
Serial.println(F("0 - Exit download mode"));
read_byte = getByte0(true);
switch (read_byte) {
case '1':
Logger_SD::Instance()->setSampleFile(SCHED1_FILE);
downloadSampleFile();
break;
case '2':
Logger_SD::Instance()->setSampleFile(SCHED2_FILE);
downloadSampleFile();
break;
case '3':
Logger_SD::Instance()->setSampleFile(SCHED3_FILE);
downloadSampleFile();
break;
case '9':
Logger_SD::Instance()->dumpLogFile();
break;
case '0':
return;
default:
continue;
}
} while (true);
}
void downloadSampleFile(){
const uint16_t DUMP_DELAY = 5;
Serial.print(F("Prepare to Capture file: "));
for ( uint16_t i = DUMP_DELAY; i > 0 ; i-- ) {
delay(1000);
Serial.print(i); Serial.print(' ');
}
Serial.println();
Logger_SD::Instance()->dumpSampleFile();
Serial.println("DONE");
delay(5000); // Time to end capture
}
void HSconsoleDeleteData(){
char read_byte;
do {
// Prompt for which file to download
Serial.println(F("Select file to delete:"));
Serial.print("1 - "); Serial.println(SCHED1_FILE);
Serial.print("2 - "); Serial.println(SCHED2_FILE);
Serial.print("3 - "); Serial.println(SCHED3_FILE);
Serial.print("9 - "); Serial.println(LOG_FILE);
Serial.println(F("0 - Exit delete mode"));
read_byte = getByte0(true);
switch (read_byte) {
case '1':
Logger_SD::Instance()->setSampleFile(SCHED1_FILE);
Logger_SD::Instance()->deleteSampleFile();
break;
case '2':
Logger_SD::Instance()->setSampleFile(SCHED2_FILE);
Logger_SD::Instance()->deleteSampleFile();
break;
case '3':
Logger_SD::Instance()->setSampleFile(SCHED3_FILE);
Logger_SD::Instance()->deleteSampleFile();
//break;
case '9':
Logger_SD::Instance()->deleteLogFile();
break;
case '0':
return;
default:
continue;
}
} while (true);
}
void HSconsoleUtilities(){
char read_byte;
do {
// Prompt for which file to download
Serial.println(F("Select function:"));
Serial.println("1 - Card Info");
Serial.println("2 - Upload ini file");
Serial.println("3 - View ini file");
Serial.println("9 - FORMAT CARD");
Serial.println(F("0 - Exit utility mode"));
read_byte = getByte0(true);
switch (read_byte) {
case '1':
Logger_SD::Instance()->cardInfo();
break;
case '2':
Serial.println("Not Implemented");
break;
case '3':
Serial.println("Not Implemented");
break;
case '9':
Serial.println("Not Implemented");
break;
case '0':
return;
default:
continue;
}
} while (true);
}
uint16_t clearSerial0() {
uint16_t chars = 0;
while ( Serial.available() ) {
Serial.read();
chars++;
}
return chars;
}
char getByte0(bool ignore_EOL){
char read_byte = 0;
clearSerial0();
do {
if ( Serial.available() ) {
read_byte = Serial.read();
if ( ignore_EOL && ( read_byte == 13 || read_byte == 10 )) read_byte = 0;
}
else delay(100);
} while ( !read_byte );
clearSerial0();
return read_byte;
}
void watchdogSetup(){
cli(); // Disable all interrupts
wdt_reset();
// Enter Watchdog configuration mode WatchDogTimerControlRegister
WDTCSR |= (1<<WDCE) | (1<<WDE);
// Set Watchdog settings
// WDIE is interrupt on timeout
// WDE is reset on timeout
// WDPx: 1001 = 8 seconds
WDTCSR = (1<<WDIE) | (1<<WDE) | (1<<WDP3) | (0<<WDP2) | (0<<WDP1) | (1<<WDP0);
sei(); // enable interrupts
}
ISR(WDT_vect){
// Watchdog timer interrupt
}