-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvutils.c
107 lines (95 loc) · 3.89 KB
/
csvutils.c
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
#include "csvutils.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int writeCSV(const VehicleData* csvdata, int numEntries, const char* filename){
FILE* file = fopen(filename,"w"); //opens 'filename' and assigns to 'file' the pointer to that file;
const char * fileheader = "timeMillis," // timestamp (millis)
"vehicleVelocity,angleAccPedal,angleBrakePedal,requestCarStart," // vehicle data
"batteryVoltage,batteryCurrent," // battery data
"bsgCurrentMode,bsgRPM,bsgVoltage,bsgCurrent," // bsg data
"engineRPM,velocity,gear\n"; // engine
fprintf(file,"%s", fileheader); //csv header
for(int i = 0; i < numEntries; i++){
char csvLine[512]; // Arbitrary size
writeCSVLine(csvdata[i], csvLine);
fprintf(file, "%s\n", csvLine);
}
fclose(file);
return 0;
}
void writeCSVLine(const VehicleData data, char* csvLine){
sprintf(csvLine, "%u," // timeMillis
"%hu," // vehicle.velocity
"%hu," // vehicle.angleAccPedal
"%hu," // vehicle.angleBrakePedal
"%hu," // vehicle.requestCarStart
"%hu," // battery.voltage
"%hhd," // battery.current
"%d," // bsg.currentMode
"%hu," // bsg.rpm
"%hu," // bsg.voltage
"%hu," // bsg.current
"%u," // engine.rpm
"%hu," // engine.velocity
"%hu", // engine.gear
data.timeMillis,
data.vehicle.velocity,
data.vehicle.angleAccPedal,
data.vehicle.angleBrakePedal,
data.vehicle.requestCarStart,
data.battery.voltage,
data.battery.current,
data.bsg.currentMode,
data.bsg.rpm,
data.bsg.voltage,
data.bsg.current,
data.engine.rpm,
data.engine.velocity,
data.engine.gear
);
}
int readCSV(VehicleData* csvdata, int numEntries, const char* filename){
FILE* file = fopen(filename,"r"); //opens 'filename' and assigns to 'file' the pointer to that file;
const int bufferSize = 512;
char csvLine[bufferSize];
fgets(csvLine, bufferSize, file); // Drop First Line with header
for( int i = 0; ( i < numEntries ) && fgets(csvLine, 512, file); i++ ) {
readCSVLine(&csvdata[i], csvLine);
}
fclose(file);
return 0;
}
void readCSVLine(VehicleData* data, const char* csvLine){
sscanf(csvLine, "%u," // Timestamp
"%hu," // vehicle.velocity
"%hu," // vehicle.angleAccPedal
"%hu," // vehicle.angleBrakePedal
"%hhu," // vehicle.requestCarStart
"%hhu," // battery.voltage
"%hhd," // battery.current
"%u," // bsg.currentMode
"%hu," // bsg.rpm
"%hu," // bsg.voltage
"%hu," // bsg.current
"%hu," // engine.rpm
"%hhu," // engine.velocity
"%hhu", // engine.gear
&(data->timeMillis),
&(data->vehicle.velocity),
&(data->vehicle.angleAccPedal),
&(data->vehicle.angleBrakePedal),
&(data->vehicle.requestCarStart),
&(data->battery.voltage),
&(data->battery.current),
&(data->bsg.currentMode),
&(data->bsg.rpm),
&(data->bsg.voltage),
&(data->bsg.current),
&(data->engine.rpm),
&(data->engine.velocity),
&(data->engine.gear)
);
}