SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
LogData.h
Go to the documentation of this file.
1
2#ifndef LOGDATA_H
3#define LOGDATA_H
4
5#include <string>
6#include <vector>
7#include<stdio.h>
8#include<stdlib.h>
9#include "File.h"
10#include "Timepp.h"
11
19template<typename T>
20class LogData {
21public:
22
23 //variables
24 size_t num_samples;
25 size_t num_signals;
27 std::vector<std::vector<T>> data;
28 std::vector<size_t> samples;
29 std::vector<std::string> signals;
30 std::string name;
31 std::string path;
32 std::string author;
34 int id = -1;
35 bool saved;
45 LogData(std::string str_name, std::string str_path,
46 std::vector<std::string> signal_names, int ID,
47 size_t num_of_signals, size_t num_of_samples,
48 Time* p_time = nullptr, std::string theAuthor = "unknown") {
49 name = str_name;
50 path = str_path;
51 saved = false;
52 signals = signal_names;
53 id = ID;
54 time = p_time;
55 author = theAuthor;
56 num_samples = 0;
57 max_num_samples = num_of_samples;
58 num_signals = num_of_signals;
59 data.resize(num_signals);
60 for (int i = 0; i < num_signals; i++) data[i].reserve(max_num_samples);
61 samples.reserve(max_num_samples);
62 };
63
68 data.clear();
69 };
70
77 T get(int i, int j) { return data[i][j]; } //return data (i,j), i.e. one element (i-th signal and j-th sample)
78
84 std::vector<T> get(int i) { return data[i]; } //return data(i,:), i.e. all samples of i-th signal
85
91 int push_back(T* packet) { //append packet data to buffer
92 if (num_samples > (max_num_samples - 1)) { return 0; } //return 0 if max_num_samples reached
93 for (int i = 0; i < num_signals; ++i) data[i].push_back(packet[i]);
94 samples.push_back(++num_samples);
95 return num_signals;
96 };
97
102 std::string getAuthor() {
103 return author;
104 }
105
110 std::string getDate() {
111 if (time) {
112 return time->getData(false);
113 }
114 else {
115 return (std::string) "0000-00-00 00:00:00";
116 }
117 }
118
124 size_t save(File* p_file) { //save to file, return the number of samples saved
125 if (p_file == nullptr) { return 0; }
126 T* buf = (T*)malloc(sizeof(T) * num_signals);
127 if (buf == NULL) { free(buf); return 0; }
128 for (size_t j = 0; j < num_samples; ++j) {
129 for (int i = 0; i < num_signals; ++i) buf[i] = get(i, j);
130 if (num_signals * sizeof(T) != p_file->write((unsigned char*)buf, num_signals * sizeof(T))) {
131 free(buf);
132 return j;
133 }
134 }
135 free(buf);
136 saved = true;
137 return num_samples;
138 }
139};
140
141#endif
A class for file reading and writing.
Definition: File.h:30
size_t write(void *buf, size_t len)
Write a buffer.
A templated-class for managing logged-data.
Definition: LogData.h:20
Time * time
Variable to store the initial time.
Definition: LogData.h:33
T get(int i, int j)
Get a single sample of a signal.
Definition: LogData.h:77
std::string author
Author of the log.
Definition: LogData.h:32
std::vector< T > get(int i)
Get all samples of a signal.
Definition: LogData.h:84
std::string getDate()
Get the date.
Definition: LogData.h:110
size_t save(File *p_file)
Save the log.
Definition: LogData.h:124
size_t num_samples
Number of logged samples.
Definition: LogData.h:24
std::vector< size_t > samples
Vector of samples.
Definition: LogData.h:28
std::vector< std::vector< T > > data
Vector of vector of type T (i.e. a matrix with num_signals rows and max_num_samples columns)
Definition: LogData.h:27
std::vector< std::string > signals
Vector of signal names.
Definition: LogData.h:29
std::string name
Name of the log.
Definition: LogData.h:30
std::string path
Path of the corresponding log file.
Definition: LogData.h:31
size_t num_signals
Number of logged signals.
Definition: LogData.h:25
bool saved
True if the log has been saved.
Definition: LogData.h:35
LogData(std::string str_name, std::string str_path, std::vector< std::string > signal_names, int ID, size_t num_of_signals, size_t num_of_samples, Time *p_time=nullptr, std::string theAuthor="unknown")
Constructor.
Definition: LogData.h:45
size_t max_num_samples
Max number of logged samples (reserved for vectors).
Definition: LogData.h:26
~LogData()
Destructor.
Definition: LogData.h:67
int push_back(T *packet)
Push data to the log buffer.
Definition: LogData.h:91
std::string getAuthor()
Get author name.
Definition: LogData.h:102
A class to get system time.
Definition: Timepp.h:37
int getData(char *str, size_t len, bool update=true)
Get the current date-time.
Definition: Timepp.h:110