giuliomoro@153
|
1 /*
|
giuliomoro@153
|
2 * WriteMFile.h
|
giuliomoro@153
|
3 *
|
giuliomoro@153
|
4 * Created on: 5 Oct 2015
|
giuliomoro@153
|
5 * Author: giulio
|
giuliomoro@153
|
6 */
|
giuliomoro@153
|
7
|
giuliomoro@153
|
8 #ifndef WRITEMFILE_H_
|
giuliomoro@153
|
9 #define WRITEMFILE_H_
|
giuliomoro@301
|
10 #include <Bela.h>
|
giuliomoro@153
|
11 #include <vector>
|
giuliomoro@153
|
12 #include <stdio.h>
|
giuliomoro@153
|
13 #include <stdlib.h>
|
giuliomoro@153
|
14 #include <string.h>
|
giuliomoro@153
|
15 #include <unistd.h>
|
giuliomoro@153
|
16
|
giuliomoro@157
|
17 typedef enum {
|
giuliomoro@157
|
18 kBinary,
|
giuliomoro@157
|
19 kText
|
giuliomoro@157
|
20 } WriteFileType;
|
giuliomoro@157
|
21
|
giuliomoro@153
|
22 class WriteFile {
|
giuliomoro@153
|
23 private:
|
giuliomoro@153
|
24 static AuxiliaryTask writeAllFilesTask;
|
giuliomoro@153
|
25 bool echo;
|
giuliomoro@157
|
26 int echoedLines;
|
giuliomoro@157
|
27 int echoPeriod;
|
giuliomoro@153
|
28 char *header;
|
giuliomoro@153
|
29 char *footer;
|
giuliomoro@153
|
30 char *stringBuffer;
|
giuliomoro@153
|
31 int stringBufferLength;
|
giuliomoro@153
|
32 int bufferLength;
|
giuliomoro@153
|
33 float* buffer;
|
giuliomoro@157
|
34 int textReadPointer;
|
giuliomoro@157
|
35 int binaryReadPointer;
|
giuliomoro@153
|
36 int writePointer;
|
giuliomoro@153
|
37 bool variableOpen;
|
giuliomoro@153
|
38 char* format;
|
giuliomoro@153
|
39 int lineLength;
|
giuliomoro@157
|
40 WriteFileType fileType;
|
giuliomoro@153
|
41 static int sleepTimeMs;
|
giuliomoro@153
|
42 FILE *file;
|
giuliomoro@153
|
43 void writeLine();
|
giuliomoro@153
|
44 void writeHeader();
|
giuliomoro@153
|
45 void writeFooter();
|
giuliomoro@153
|
46 void allocateAndCopyString(const char* source, char** destination);
|
giuliomoro@153
|
47 void print(const char* string);
|
giuliomoro@157
|
48 void printBinary(const char* string);
|
giuliomoro@153
|
49 void setLineLength(int newLineLength);
|
giuliomoro@157
|
50 int getOffsetFromPointer(int aPointer);
|
giuliomoro@153
|
51 std::vector<char *> formatTokens;
|
giuliomoro@153
|
52 static void sanitizeString(char* string);
|
giuliomoro@153
|
53 static void sanitizeString(char* string, int numberOfArguments);
|
giuliomoro@153
|
54 static bool isThreadRunning();
|
giuliomoro@153
|
55 static bool auxiliaryTaskRunning;
|
giuliomoro@153
|
56 static bool threadShouldExit();
|
giuliomoro@153
|
57 static bool threadIsExiting;
|
giuliomoro@153
|
58 static bool threadRunning;
|
giuliomoro@153
|
59 static bool staticConstructed;
|
giuliomoro@153
|
60 static void staticConstructor();
|
giuliomoro@153
|
61 static std::vector<WriteFile *> objAddrs;
|
giuliomoro@159
|
62 void writeOutput(bool flush);
|
giuliomoro@153
|
63 public:
|
giuliomoro@153
|
64 WriteFile();
|
giuliomoro@157
|
65 /**
|
giuliomoro@157
|
66 * Set the type of file to write, can be either kText or kBinary.
|
giuliomoro@157
|
67 * Binary files can be imported e.g. in Matlab:
|
giuliomoro@157
|
68 * fid=fopen('out','r');
|
giuliomoro@157
|
69 * A = fread(fid, 'float');
|
giuliomoro@157
|
70 * */
|
giuliomoro@157
|
71 void setFileType(WriteFileType newFileType);
|
giuliomoro@153
|
72 void setEcho(bool newEcho);
|
giuliomoro@157
|
73 void setEchoInterval(int newPeriod);
|
giuliomoro@157
|
74 /**
|
giuliomoro@157
|
75 * Set the format that you want to use for your output.
|
giuliomoro@157
|
76 *
|
giuliomoro@157
|
77 * Only %f is allowed (with modifiers). When in binary mode,
|
giuliomoro@157
|
78 * the specified format is used only for echoing to console.
|
giuliomoro@157
|
79 */
|
giuliomoro@153
|
80 void setFormat(const char* newFormat);
|
giuliomoro@157
|
81 /**
|
giuliomoro@157
|
82 * Set one or more lines to be printed at the beginning of the file.
|
giuliomoro@157
|
83 *
|
giuliomoro@157
|
84 * This is ignored in binary mode.
|
giuliomoro@157
|
85 */
|
giuliomoro@153
|
86 void setHeader(const char* newHeader);
|
giuliomoro@157
|
87 /**
|
giuliomoro@157
|
88 * Set one or more lines to be printed at the end of the file.
|
giuliomoro@157
|
89 *
|
giuliomoro@157
|
90 * This is ignored in binary mode.
|
giuliomoro@157
|
91 */
|
giuliomoro@153
|
92 void setFooter(const char* newFooter);
|
giuliomoro@153
|
93 void log(float* array, int length);
|
giuliomoro@153
|
94 void log(float value);
|
giuliomoro@153
|
95 void init(const char* filename);
|
giuliomoro@153
|
96
|
giuliomoro@153
|
97 /**
|
giuliomoro@153
|
98 * Gets the distance between the write and read pointers of
|
giuliomoro@153
|
99 * the buffer that holds data to be written to disk.
|
giuliomoro@153
|
100 */
|
giuliomoro@153
|
101 int getOffset();
|
giuliomoro@153
|
102
|
giuliomoro@153
|
103 /**
|
giuliomoro@153
|
104 * Inquiries the status of the buffer that holds data to be written to disk.
|
giuliomoro@153
|
105 *
|
giuliomoro@153
|
106 * @return a value between 0 and 1, with 0 being buffer full (writing to disk not fast enough)
|
giuliomoro@153
|
107 * and 1 being buffer empty (writing to disk is fast enough).
|
giuliomoro@153
|
108 */
|
giuliomoro@153
|
109 float getBufferStatus();
|
giuliomoro@153
|
110 ~WriteFile();
|
giuliomoro@153
|
111 static int getNumInstances();
|
giuliomoro@153
|
112 static void writeAllHeaders();
|
giuliomoro@153
|
113 static void writeAllFooters();
|
giuliomoro@159
|
114 static void writeAllOutputs(bool flush);
|
giuliomoro@153
|
115 static void startThread();
|
giuliomoro@153
|
116 static void stopThread();
|
giuliomoro@153
|
117 static void run();
|
giuliomoro@153
|
118 };
|
giuliomoro@153
|
119
|
giuliomoro@153
|
120 #endif /* WRITEMFILE_H_ */
|