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  prepStr(unit_x, STR_LEN_SHORT);
150  prepStr(desc_x, STR_LEN_LARGE);
151  prepStr(unit_y, STR_LEN_SHORT);
152  prepStr(desc_y, STR_LEN_LARGE);
153  prepStr(unit_para, STR_LEN_SHORT);
154  prepStr(desc_para, STR_LEN_LARGE);
155 
156  std::ofstream file(filename);
157 
158  // write file header
159  file.write(reinterpret_cast<char*>(&command), 1);
160  file.write(head.c_str(), head.length());
161  file.write(filetype.c_str(), filetype.length());
162  file.write(reinterpret_cast<char*>(&version), 2);
163  file.write(reinterpret_cast<char*>(&subversion), 2);
164  file.put(static_cast<uint8_t>(function_type));
165  file.write(reinterpret_cast<char*>(&quadrant), 1);
166  file.write(reinterpret_cast<char*>(&ref_x), 2);
167  file.write(reinterpret_cast<char*>(&ref_y), 2);
168  file.write(reinterpret_cast<char*>(&para_first), 2);
169  file.write(reinterpret_cast<char*>(&para_stepwidth), 2);
170  file.write(unit_x.c_str(), unit_x.length());
171  file.write(desc_x.c_str(), desc_x.length());
172  file.write(unit_y.c_str(), unit_y.length());
173  file.write(desc_y.c_str(), desc_y.length());
174  file.write(unit_para.c_str(), unit_para.length());
175  file.write(desc_para.c_str(), desc_para.length());
176  file.write(reinterpret_cast<const char*>(&eof), 1);
177 
178  // make sure header size is 256 Byte
179  while(file.tellp() < 256)
180  file.put(0);
181 
182  for(Dot& dot : dots)
183  {
184  file.put((dot.getX() >> 8) | (static_cast<uint8_t>(dot.getCurve()) << 2));
185  file.put(dot.getX() & 0xFF);
186  file.put(dot.getY() >> 8);
187  file.put(dot.getY() & 0xFF);
188  }
189 
190  file.close();
191 }
192 
193 void PlottyFile::startPlotty(std::string filename)
194 {
195  int code = system(("plotty --in " + filename).c_str());
196  if(code)
197  throw std::runtime_error("Fehler beim Aufruf von plotty");
198 }
Dot
Definition: dot.h:7