-
Notifications
You must be signed in to change notification settings - Fork 2
/
filestore.cpp
80 lines (73 loc) · 1.91 KB
/
filestore.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
#include <Arduino.h>
#include <Fat16.h>
#include <Time.h>
#include "vivarium.h"
#include "filestore.h"
#include "vivtime.h"
#include "lcd.h"
SdCard card;
Fat16 file;
void initSD() {
if(card.init() && Fat16::init(&card)) {
sdPresent=true;
}
}
// Sensor ID's are 16 characters in Hex.
// We just use a sub-set, skipping the first two characters
// This will change when we get the persistent registration of the
// sensor Ids implemented
bool selectSensorFile(char *sensorIdStr) {
memcpy(displayBuf,sensorIdStr+2,8);
strcpy_P(displayBuf+8,PSTR(".dat"));
displayBuf[12]=0;
if(file.open(displayBuf,O_RDWR | O_CREAT | O_APPEND)==0) return false;
}
void truncateFile(int idx) {
char sensorId[17];
sensorIdToBuffer(idx,sensorId);
if(!selectSensorFile(sensorId)) return;
file.truncate(0);
file.close();
}
void dumpFile(int idx) {
char sensorId[17];
sensorIdToBuffer(idx,sensorId);
if(!selectSensorFile(sensorId)) return;
file.rewind();
int d;
while((d=file.read())!=-1) {
Serial.write((byte)d);
}
Serial.println("");
file.close();
}
void logData(int idx,int viv,float target) {
if(!sdPresent || timeStatus()==timeNotSet) return;
time_t current=now();
if(sensors[idx].valid==0 || (sensors[idx].lastWrite!=0 && (current-sensors[idx].lastWrite)<60)) return;
sensors[idx].lastWrite=current;
char sensorId[17];
sensorIdToBuffer(idx,sensorId);
if(!selectSensorFile(sensorId)) return;
dtostrf(sensors[idx].value,3,1,displayBuf);
displayBuf[5]=0;
file.write(displayBuf);
file.write(',');
fmt2Digits(displayBuf,viv);
displayBuf[2]=0;
file.write(displayBuf);
file.write(',');
dtostrf(target,3,1,displayBuf);
displayBuf[5]=0;
file.write(displayBuf);
file.write(',');
file.write(sensors[idx].relay_on+'0');
file.write(',');
displayBuf[14]=0;
time_t tval=lastRead;
fmtDateTime(displayBuf,tval);
file.write(displayBuf);
file.write("\n");
file.sync();
file.close();
}