B15F Board 15 Famulus Edition

plottyfile.cpp
1 #include "plottyfile.h"
2
3 void PlottyFile::addDot(Dot& dot)
4 {
5 dots.push_back(dot);
6 }
7
8 void PlottyFile::addDot(Dot dot)
9 {
10 dots.push_back(dot);
11 }
12
13 void PlottyFile::setFunctionType(FunctionType function_type)
14 {
15 this->function_type = function_type;
16 }
17
18 void PlottyFile::setQuadrant(uint8_t quadrant)
19 {
20 if(quadrant < 1 || quadrant > 4)
21 throw std::range_error("Ungueltiger Quadrant");
22 this->quadrant = quadrant;
23 }
24
25 void PlottyFile::setRefX(uint16_t ref_x)
26 {
27 this->ref_x = ref_x;
28 }
29
30 void PlottyFile::setRefY(uint16_t ref_y)
31 {
32 this->ref_y = ref_y;
33 }
34
35 void PlottyFile::setParaFirstCurve(uint16_t para_first)
36 {
37 this->para_first = para_first;
38 }
39
40 void PlottyFile::setParaStepWidth(uint16_t para_stepwidth)
41 {
42 this->para_stepwidth = para_stepwidth;
43 }
44
45 void PlottyFile::setUnitX(std::string unit_x)
46 {
47 this->unit_x = unit_x;
48 }
49
50 void PlottyFile::setDescX(std::string desc_x)
51 {
52 this->desc_x = desc_x;
53 }
54
55 void PlottyFile::setUnitY(std::string unit_y)
56 {
57 this->unit_y = unit_y;
58 }
59
60 void PlottyFile::setDescY(std::string desc_y)
61 {
62 this->desc_y = desc_y;
63 }
64
65 void PlottyFile::setUnitPara(std::string unit_para)
66 {
67 this->unit_para = unit_para;
68 }
69
70 void PlottyFile::setDescPara(std::string desc_para)
71 {
72 this->desc_para = desc_para;
73 }
74
75 FunctionType PlottyFile::getFunctionType() const
76 {
77 return function_type;
78 }
79
80 uint8_t PlottyFile::getQuadrant() const
81 {
82 return quadrant;
83 }
84
85 uint16_t PlottyFile::getRefX() const
86 {
87 return ref_x;
88 }
89
90 uint16_t PlottyFile::getRefY() const
91 {
92 return ref_y;
93 }
94
95 uint16_t PlottyFile::getParaFirstCurve() const
96 {
97 return para_first;
98 }
99
100 uint16_t PlottyFile::getParaStepWidth() const
101 {
102 return para_stepwidth;
103 }
104
105 std::string PlottyFile::getUnitX() const
106 {
107 return unit_x;
108 }
109
110 std::string PlottyFile::getDescX() const
111 {
112 return desc_x;
113 }
114
115 std::string PlottyFile::getUnitY() const
116 {
117 return unit_y;
118 }
119
120 std::string PlottyFile::getDescY() const
121 {
122 return desc_y;
123 }
124
125 std::string PlottyFile::getUnitPara() const
126 {
127 return unit_para;
128 }
129
130 std::string PlottyFile::getDescPara() const
131 {
132 return desc_para;
133 }
134
135 void PlottyFile::prepStr(std::string& str, uint8_t len)
136 {
137 if(str.length() > len)
138 throw std::runtime_error("Zu grosser String.");
139
140 if(str.length() != len)
141 str += '\n';
142
143 while(str.length() < len)
144 str += '\0';
145 }
146
147 void PlottyFile::writeToFile(std::string filename)
148 {
149 if(dots.empty())
150 throw std::length_error("Es wurden keine Punkte gespeichert.");
151
152 prepStr(unit_x, STR_LEN_SHORT);
153 prepStr(desc_x, STR_LEN_LARGE);
154 prepStr(unit_y, STR_LEN_SHORT);
155 prepStr(desc_y, STR_LEN_LARGE);
156 prepStr(unit_para, STR_LEN_SHORT);
157 prepStr(desc_para, STR_LEN_LARGE);
158
159 std::ofstream file(filename);
160
161 // write file header
162 file.write(reinterpret_cast<char*>(&command), 1);
163 file.write(head.c_str(), head.length());
164 file.write(filetype.c_str(), filetype.length());
165 file.write(reinterpret_cast<char*>(&version), 2);
166 file.write(reinterpret_cast<char*>(&subversion), 2);
167 file.put(static_cast<uint8_t>(function_type));
168 file.write(reinterpret_cast<char*>(&quadrant), 1);
169 file.write(reinterpret_cast<char*>(&ref_x), 2);
170 file.write(reinterpret_cast<char*>(&ref_y), 2);
171 file.write(reinterpret_cast<char*>(&para_first), 2);
172 file.write(reinterpret_cast<char*>(&para_stepwidth), 2);
173 file.write(unit_x.c_str(), unit_x.length());
174 file.write(desc_x.c_str(), desc_x.length());
175 file.write(unit_y.c_str(), unit_y.length());
176 file.write(desc_y.c_str(), desc_y.length());
177 file.write(unit_para.c_str(), unit_para.length());
178 file.write(desc_para.c_str(), desc_para.length());
179 file.write(reinterpret_cast<const char*>(&eof), 1);
180
181 // make sure header size is 256 Byte
182 while(file.tellp() < 256)
183 file.put(0);
184
185 for(Dot& dot : dots)
186 {
187 file.put((dot.getX() >> 8) | (static_cast<uint8_t>(dot.getCurve()) << 2));
188 file.put(dot.getX() & 0xFF);
189 file.put(dot.getY() >> 8);
190 file.put(dot.getY() & 0xFF);
191 }
192
193 file.close();
194 }
195
196 void PlottyFile::startPlotty(std::string filename)
197 {
198 int code = system(("plotty --in " + filename).c_str());
199 if(code)
200 throw std::runtime_error("Fehler beim Aufruf von plotty");
201 }
PlottyFile::setParaStepWidth
void setParaStepWidth(uint16_t para_stepwidth)
Definition: plottyfile.cpp:40
PlottyFile::getQuadrant
uint8_t getQuadrant(void) const
Definition: plottyfile.cpp:80
PlottyFile::startPlotty
void startPlotty(std::string filename)
Definition: plottyfile.cpp:196
PlottyFile::writeToFile
void writeToFile(std::string filename)
Definition: plottyfile.cpp:147
PlottyFile::setUnitX
void setUnitX(std::string unit_x)
Definition: plottyfile.cpp:45
PlottyFile::setUnitPara
void setUnitPara(std::string unit_para)
Definition: plottyfile.cpp:65
PlottyFile::setDescY
void setDescY(std::string desc_y)
Definition: plottyfile.cpp:60
PlottyFile::setQuadrant
void setQuadrant(uint8_t quadrant)
Definition: plottyfile.cpp:18
PlottyFile::getDescY
std::string getDescY(void) const
Definition: plottyfile.cpp:120
PlottyFile::setRefY
void setRefY(uint16_t ref_y)
Definition: plottyfile.cpp:30
PlottyFile::getDescX
std::string getDescX(void) const
Definition: plottyfile.cpp:110
PlottyFile::setFunctionType
void setFunctionType(FunctionType function_type)
Definition: plottyfile.cpp:13
PlottyFile::setDescX
void setDescX(std::string desc_x)
Definition: plottyfile.cpp:50
Dot
Definition: dot.h:12
PlottyFile::setRefX
void setRefX(uint16_t ref_x)
Definition: plottyfile.cpp:25
PlottyFile::setUnitY
void setUnitY(std::string unit_y)
Definition: plottyfile.cpp:55
PlottyFile::addDot
void addDot(Dot &dot)
Definition: plottyfile.cpp:3
PlottyFile::setDescPara
void setDescPara(std::string desc_para)
Definition: plottyfile.cpp:70
PlottyFile::getParaStepWidth
uint16_t getParaStepWidth(void) const
Definition: plottyfile.cpp:100
PlottyFile::getDescPara
std::string getDescPara(void) const
Definition: plottyfile.cpp:130
PlottyFile::setParaFirstCurve
void setParaFirstCurve(uint16_t para_first)
Definition: plottyfile.cpp:35
PlottyFile::getUnitY
std::string getUnitY(void) const
Definition: plottyfile.cpp:115
PlottyFile::getParaFirstCurve
uint16_t getParaFirstCurve(void) const
Definition: plottyfile.cpp:95
PlottyFile::getRefX
uint16_t getRefX(void) const
Definition: plottyfile.cpp:85
PlottyFile::getUnitPara
std::string getUnitPara(void) const
Definition: plottyfile.cpp:125
PlottyFile::getFunctionType
FunctionType getFunctionType(void) const
Definition: plottyfile.cpp:75
PlottyFile::getRefY
uint16_t getRefY(void) const
Definition: plottyfile.cpp:90
PlottyFile::getUnitX
std::string getUnitX(void) const
Definition: plottyfile.cpp:105