diff --git a/control/bin/b15fcli b/control/bin/b15fcli new file mode 100755 index 0000000..864a09c Binary files /dev/null and b/control/bin/b15fcli differ diff --git a/control/examples/pwm/Makefile b/control/examples/pwm/Makefile new file mode 100644 index 0000000..8000d6e --- /dev/null +++ b/control/examples/pwm/Makefile @@ -0,0 +1,30 @@ +# Name: Makefile +# Project: B15F (board15 Famulus Edition) +# Author: Tristan Krause +# Creation Date: 2019-05-15 + +# Environment +COMPILER_PATH = g++ + +# Options +CFLAGS = -std=c++17 -O3 -Wall -Wextra +LDFLAGS = -lb15fdrv +OBJECTS = main.o +OUT = main.elf + +COMPILE = $(COMPILER_PATH) $(CFLAGS) + +main: $(OBJECTS) + $(COMPILE) $(OBJECTS) -o $(OUT) $(LDFLAGS) + +help: + @echo "This Makefile has the following targets:" + @echo "make main .... to compile" + @echo "make clean ... to delete objects and executables" + +clean: + @echo "Cleaning..." + rm -f $(OBJECTS) $(OUT) *.bin gnuplotscript.gp + +.cpp.o: + $(COMPILE) -c $< -o $@ diff --git a/control/examples/pwm/main.cpp b/control/examples/pwm/main.cpp new file mode 100644 index 0000000..dd8af86 --- /dev/null +++ b/control/examples/pwm/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +const char PLOT_FILE[] = "plot.bin"; + +int main() +{ + + B15F& drv = B15F::getInstance(); + std::cout << "TOP: " << (int) drv.pwmSetFrequency(100000) << std::endl; + drv.pwmSetValue(40); +} diff --git a/control/src/drv/b15f.cpp b/control/src/drv/b15f.cpp index 0204cc6..56cb2ed 100644 --- a/control/src/drv/b15f.cpp +++ b/control/src/drv/b15f.cpp @@ -289,6 +289,47 @@ void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset delay_us(10); } +uint8_t B15F::pwmSetFrequency(uint32_t freq) +{ + usart.clearInputBuffer(); + + uint8_t rq[] = + { + RQ_PWM_SET_FREQ, + static_cast((freq >> 0) & 0xFF), + static_cast((freq >> 8) & 0xFF), + static_cast((freq >> 16) & 0xFF), + static_cast((freq >> 24) & 0xFF) + }; + + int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000); + if(n_sent != sizeof(rq)) + abort("Sent failed"); + + uint8_t byte = usart.readByte(); + delay_us(10); + return byte; +} + +bool B15F::pwmSetValue(uint8_t value) +{ + usart.clearInputBuffer(); + + uint8_t rq[] = + { + RQ_PWM_SET_VALUE, + value + }; + + int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000); + if(n_sent != sizeof(rq)) + abort("Sent failed"); + + uint8_t aw = usart.readByte(); + delay_us(10); + return aw == MSG_OK; +} + void B15F::delay_ms(uint16_t ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); diff --git a/control/src/drv/b15f.h b/control/src/drv/b15f.h index 88172ba..1b011ea 100644 --- a/control/src/drv/b15f.h +++ b/control/src/drv/b15f.h @@ -194,6 +194,22 @@ public: */ void analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count); + /** + * Setzt die Register so, dass näherungsweise die gewünschte Frequenz erzeugt wird. + * Ist freq == 0 wird PWM deaktiviert. + * \param freq PWM Frequenz + * \return Top Wert des PWM Value für die gesetzte Frequenz + * \throws DriverException + */ + uint8_t pwmSetFrequency(uint32_t freq); + + /** + * Setzt den PWM Wert. + * \param value PWM Wert [0..0xFF] + * \throws DriverException + */ + bool pwmSetValue(uint8_t value); + /*************************/ @@ -233,6 +249,8 @@ private: constexpr static uint8_t RQ_AA1 = 11; constexpr static uint8_t RQ_ADC = 12; constexpr static uint8_t RQ_ADC_DAC_STROKE = 13; + constexpr static uint8_t RQ_PWM_SET_FREQ = 14; + constexpr static uint8_t RQ_PWM_SET_VALUE = 15; }; #endif // B15F_H diff --git a/control/src/drv/usart.cpp b/control/src/drv/usart.cpp index dcc7dd9..0717d41 100644 --- a/control/src/drv/usart.cpp +++ b/control/src/drv/usart.cpp @@ -85,7 +85,12 @@ void USART::writeInt(uint16_t d) throw USARTException("Fehler beim Senden: writeInt()"); } - +void USART::writeU32(uint32_t w) +{ + int sent = write(file_desc, reinterpret_cast(&w), 4); + if(sent != 4) + throw USARTException("Fehler beim Senden: writeU32()"); +} int USART::read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout) { diff --git a/control/src/drv/usart.h b/control/src/drv/usart.h index de2719f..762bed6 100644 --- a/control/src/drv/usart.h +++ b/control/src/drv/usart.h @@ -76,11 +76,18 @@ public: /** * Sendet ein Integer über die USART Schnittstelle - * \param b das zu sendende Byte + * \param b das zu sendende Int * \throws USARTException */ void writeInt(uint16_t d); + /** + * Sendet ein uint32_t über die USART Schnittstelle + * \param b das zu sendende uint32_t + * \throws USARTException + */ + void writeU32(uint32_t d); + /** * Empfängt ein Byte über die USART Schnittstelle * \throws USARTException diff --git a/docs/html/annotated.html b/docs/html/annotated.html index e8fa8b6..709b184 100644 --- a/docs/html/annotated.html +++ b/docs/html/annotated.html @@ -85,7 +85,7 @@ $(function() { diff --git a/docs/html/b15f_8cpp_source.html b/docs/html/b15f_8cpp_source.html index 5efca6f..dd5733f 100644 --- a/docs/html/b15f_8cpp_source.html +++ b/docs/html/b15f_8cpp_source.html @@ -70,46 +70,48 @@ $(function() {
b15f.cpp
-
1 #include "b15f.h"
2 
3 B15F* B15F::instance = nullptr;
4 errorhandler_t B15F::errorhandler = nullptr;
5 
6 B15F::B15F()
7 {
8  init();
9 }
10 
11 void B15F::init()
12 {
13 
14  std::string device = exec("bash -c 'ls /dev/ttyUSB*'");
15  while(device.find(' ') != std::string::npos || device.find('\n') != std::string::npos || device.find('\t') != std::string::npos)
16  device.pop_back();
17 
18  if(device.length() == 0)
19  abort("Adapter nicht gefunden");
20 
21  std::cout << PRE << "Verwende Adapter: " << device << std::endl;
22 
23 
24 
25  std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
26  usart.setBaudrate(BAUDRATE);
27  usart.openDevice(device);
28  std::cout << "OK" << std::endl;
29 
30 
31 
32  std::cout << PRE << "Teste Verbindung... " << std::flush;
33  uint8_t tries = 3;
34  while(tries--)
35  {
36  // verwerfe Daten, die µC noch hat
37  //discard();
38 
39  if(!testConnection())
40  continue;
41 
42  if(!testIntConv())
43  continue;
44 
45  break;
46  }
47  if(tries == 0)
48  abort("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
49  std::cout << "OK" << std::endl;
50 
51 
52  // Gib board info aus
53  std::vector<std::string> info = getBoardInfo();
54  std::cout << PRE << "AVR Firmware Version: " << info[0] << " um " << info[1] << " Uhr (" << info[2] << ")" << std::endl;
55 }
56 
58 {
59  uint8_t tries = RECONNECT_TRIES;
60  while(tries--)
61  {
63  discard();
64 
65  if(testConnection())
66  return;
67  }
68 
69  abort("Verbindung kann nicht repariert werden");
70 }
71 
72 void B15F::discard(void)
73 {
74  try
75  {
76  usart.clearOutputBuffer();
77  for(uint8_t i = 0; i < 16; i++)
78  {
79  usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
80  delay_ms(4);
81  }
82  usart.clearInputBuffer();
83  }
84  catch(std::exception& ex)
85  {
86  abort(ex);
87  }
88 }
89 
91 {
92  // erzeuge zufälliges Byte
93  srand(time(NULL));
94  uint8_t dummy = rand() % 256;
95 
96  usart.writeByte(RQ_TEST);
97  usart.writeByte(dummy);
98 
99  uint8_t aw = usart.readByte();
100  uint8_t mirror = usart.readByte();
101 
102  return aw == MSG_OK && mirror == dummy;
103 }
104 
106 {
107  srand(time(NULL));
108  uint16_t dummy = rand() % (0xFFFF / 3);
109 
110  usart.writeByte(RQ_INT);
111  usart.writeInt(dummy);
112 
113  uint16_t aw = usart.readInt();
114  return aw == dummy * 3;
115 }
116 
117 
118 std::vector<std::string> B15F::getBoardInfo(void)
119 {
120  std::vector<std::string> info;
121 
122  usart.writeByte(RQ_INFO);
123 
124  uint8_t n = usart.readByte();
125  while(n--)
126  {
127  uint8_t len = usart.readByte();
128  std::string str;
129 
130  while(len--)
131  {
132  str += static_cast<char>(usart.readByte());
133  }
134 
135  info.push_back(str);
136  }
137 
138  uint8_t aw = usart.readByte();
139  if(aw != MSG_OK)
140  abort("Board Info fehlerhalft: code " + std::to_string((int) aw));
141 
142  return info;
143 }
144 
146 {
147  usart.writeByte(RQ_ST);
148 
149  uint8_t aw = usart.readByte();
150  return aw == MSG_OK;
151 }
152 
153 bool B15F::digitalWrite0(uint8_t port)
154 {
155  usart.writeByte(RQ_BA0);
156  usart.writeByte(port);
157 
158  uint8_t aw = usart.readByte();
159  delay_us(10);
160  return aw == MSG_OK;
161 }
162 
163 bool B15F::digitalWrite1(uint8_t port)
164 {
165  usart.writeByte(RQ_BA1);
166  usart.writeByte(port);
167 
168  uint8_t aw = usart.readByte();
169  delay_us(10);
170  return aw == MSG_OK;
171 }
172 
174 {
175  usart.clearInputBuffer();
176  usart.writeByte(RQ_BE0);
177  uint8_t byte = usart.readByte();
178  delay_us(10);
179  return byte;
180 }
181 
183 {
184  usart.clearInputBuffer();
185  usart.writeByte(RQ_BE1);
186  uint8_t byte = usart.readByte();
187  delay_us(10);
188  return byte;
189 }
190 
192 {
193  usart.clearInputBuffer();
194  usart.writeByte(RQ_DSW);
195  uint8_t byte = usart.readByte();
196  delay_us(10);
197  return byte;
198 }
199 
200 bool B15F::analogWrite0(uint16_t value)
201 {
202  usart.writeByte(RQ_AA0);
203  usart.writeInt(value);
204 
205  uint8_t aw = usart.readByte();
206  delay_us(10);
207  return aw == MSG_OK;
208 }
209 
210 bool B15F::analogWrite1(uint16_t value)
211 {
212  usart.writeByte(RQ_AA1);
213  usart.writeInt(value);
214 
215  uint8_t aw = usart.readByte();
216  delay_us(10);
217  return aw == MSG_OK;
218 }
219 
220 uint16_t B15F::analogRead(uint8_t channel)
221 {
222  usart.clearInputBuffer();
223  if(channel > 7)
224  abort("Bad ADC channel: " + std::to_string(channel));
225 
226  uint8_t rq[] =
227  {
228  RQ_ADC,
229  channel
230  };
231 
232  int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
233  if(n_sent != sizeof(rq))
234  abort("Sent failed");
235 
236  uint16_t adc = usart.readInt();
237 
238  if(adc > 1023)
239  abort("Bad ADC data detected (1)");
240  return adc;
241 }
242 
243 void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
244 {
245  // check pointers
246  buffer_a += offset_a;
247  buffer_b += offset_b;
248 
249 
250  usart.clearInputBuffer();
251  usart.writeByte(RQ_ADC_DAC_STROKE);
252  usart.writeByte(channel_a);
253  usart.writeByte(channel_b);
254  usart.writeInt(start);
255  usart.writeInt(static_cast<uint16_t>(delta));
256  usart.writeInt(count);
257 
258  for(uint16_t i = 0; i < count; i++)
259  {
260  if(buffer_a)
261  {
262  buffer_a[i] = usart.readInt();
263 
264  if(buffer_a[i] > 1023) // check for broken usart connection
265  abort("Bad ADC data detected (2)");
266  }
267  else
268  {
269  usart.readInt();
270  }
271 
272  if(buffer_b)
273  {
274  buffer_b[i] = usart.readInt();
275 
276  if(buffer_b[i] > 1023) // check for broken usart connection
277  abort("Bad ADC data detected (3)");
278  }
279  else
280  {
281  usart.readInt();
282  }
283  }
284 
285  uint8_t aw = usart.readByte();
286  if(aw != MSG_OK)
287  abort("Sequenz unterbrochen");
288 
289  delay_us(10);
290 }
291 
292 void B15F::delay_ms(uint16_t ms)
293 {
294  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
295 }
296 
297 void B15F::delay_us(uint16_t us)
298 {
299  std::this_thread::sleep_for(std::chrono::microseconds(us));
300 }
301 
303 {
304  if(!instance)
305  instance = new B15F();
306 
307  return *instance;
308 }
309 
310 // https://stackoverflow.com/a/478960
311 std::string B15F::exec(std::string cmd)
312 {
313  std::array<char, 128> buffer;
314  std::string result;
315  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
316  if (!pipe)
317  {
318  throw std::runtime_error("popen() failed!");
319  }
320  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
321  {
322  result += buffer.data();
323  }
324  return result;
325 }
326 
327 void B15F::abort(std::string msg)
328 {
329  DriverException ex(msg);
330  abort(ex);
331 }
332 void B15F::abort(std::exception& ex)
333 {
334  if(errorhandler)
335  errorhandler(ex);
336  else
337  {
338  std::cerr << "NOTICE: B15F::errorhandler not set" << std::endl;
339  std::cout << ex.what() << std::endl;
340  throw DriverException(ex.what());
341  }
342 }
343 
344 void B15F::setAbortHandler(errorhandler_t func)
345 {
346  errorhandler = func;
347 }
-
static std::string exec(std::string cmd)
Definition: b15f.cpp:311
-
uint8_t readByte(void)
Definition: usart.cpp:211
-
void delay_us(uint16_t us)
Definition: b15f.cpp:297
+
1 #include "b15f.h"
2 
3 B15F* B15F::instance = nullptr;
4 errorhandler_t B15F::errorhandler = nullptr;
5 
6 B15F::B15F()
7 {
8  init();
9 }
10 
11 void B15F::init()
12 {
13 
14  std::string device = exec("bash -c 'ls /dev/ttyUSB*'");
15  while(device.find(' ') != std::string::npos || device.find('\n') != std::string::npos || device.find('\t') != std::string::npos)
16  device.pop_back();
17 
18  if(device.length() == 0)
19  abort("Adapter nicht gefunden");
20 
21  std::cout << PRE << "Verwende Adapter: " << device << std::endl;
22 
23 
24 
25  std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
26  usart.setBaudrate(BAUDRATE);
27  usart.openDevice(device);
28  std::cout << "OK" << std::endl;
29 
30 
31 
32  std::cout << PRE << "Teste Verbindung... " << std::flush;
33  uint8_t tries = 3;
34  while(tries--)
35  {
36  // verwerfe Daten, die µC noch hat
37  //discard();
38 
39  if(!testConnection())
40  continue;
41 
42  if(!testIntConv())
43  continue;
44 
45  break;
46  }
47  if(tries == 0)
48  abort("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
49  std::cout << "OK" << std::endl;
50 
51 
52  // Gib board info aus
53  std::vector<std::string> info = getBoardInfo();
54  std::cout << PRE << "AVR Firmware Version: " << info[0] << " um " << info[1] << " Uhr (" << info[2] << ")" << std::endl;
55 }
56 
58 {
59  uint8_t tries = RECONNECT_TRIES;
60  while(tries--)
61  {
63  discard();
64 
65  if(testConnection())
66  return;
67  }
68 
69  abort("Verbindung kann nicht repariert werden");
70 }
71 
72 void B15F::discard(void)
73 {
74  try
75  {
76  usart.clearOutputBuffer();
77  for(uint8_t i = 0; i < 16; i++)
78  {
79  usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
80  delay_ms(4);
81  }
82  usart.clearInputBuffer();
83  }
84  catch(std::exception& ex)
85  {
86  abort(ex);
87  }
88 }
89 
91 {
92  // erzeuge zufälliges Byte
93  srand(time(NULL));
94  uint8_t dummy = rand() % 256;
95 
96  usart.writeByte(RQ_TEST);
97  usart.writeByte(dummy);
98 
99  uint8_t aw = usart.readByte();
100  uint8_t mirror = usart.readByte();
101 
102  return aw == MSG_OK && mirror == dummy;
103 }
104 
106 {
107  srand(time(NULL));
108  uint16_t dummy = rand() % (0xFFFF / 3);
109 
110  usart.writeByte(RQ_INT);
111  usart.writeInt(dummy);
112 
113  uint16_t aw = usart.readInt();
114  return aw == dummy * 3;
115 }
116 
117 
118 std::vector<std::string> B15F::getBoardInfo(void)
119 {
120  std::vector<std::string> info;
121 
122  usart.writeByte(RQ_INFO);
123 
124  uint8_t n = usart.readByte();
125  while(n--)
126  {
127  uint8_t len = usart.readByte();
128  std::string str;
129 
130  while(len--)
131  {
132  str += static_cast<char>(usart.readByte());
133  }
134 
135  info.push_back(str);
136  }
137 
138  uint8_t aw = usart.readByte();
139  if(aw != MSG_OK)
140  abort("Board Info fehlerhalft: code " + std::to_string((int) aw));
141 
142  return info;
143 }
144 
146 {
147  usart.writeByte(RQ_ST);
148 
149  uint8_t aw = usart.readByte();
150  return aw == MSG_OK;
151 }
152 
153 bool B15F::digitalWrite0(uint8_t port)
154 {
155  usart.writeByte(RQ_BA0);
156  usart.writeByte(port);
157 
158  uint8_t aw = usart.readByte();
159  delay_us(10);
160  return aw == MSG_OK;
161 }
162 
163 bool B15F::digitalWrite1(uint8_t port)
164 {
165  usart.writeByte(RQ_BA1);
166  usart.writeByte(port);
167 
168  uint8_t aw = usart.readByte();
169  delay_us(10);
170  return aw == MSG_OK;
171 }
172 
174 {
175  usart.clearInputBuffer();
176  usart.writeByte(RQ_BE0);
177  uint8_t byte = usart.readByte();
178  delay_us(10);
179  return byte;
180 }
181 
183 {
184  usart.clearInputBuffer();
185  usart.writeByte(RQ_BE1);
186  uint8_t byte = usart.readByte();
187  delay_us(10);
188  return byte;
189 }
190 
192 {
193  usart.clearInputBuffer();
194  usart.writeByte(RQ_DSW);
195  uint8_t byte = usart.readByte();
196  delay_us(10);
197  return byte;
198 }
199 
200 bool B15F::analogWrite0(uint16_t value)
201 {
202  usart.writeByte(RQ_AA0);
203  usart.writeInt(value);
204 
205  uint8_t aw = usart.readByte();
206  delay_us(10);
207  return aw == MSG_OK;
208 }
209 
210 bool B15F::analogWrite1(uint16_t value)
211 {
212  usart.writeByte(RQ_AA1);
213  usart.writeInt(value);
214 
215  uint8_t aw = usart.readByte();
216  delay_us(10);
217  return aw == MSG_OK;
218 }
219 
220 uint16_t B15F::analogRead(uint8_t channel)
221 {
222  usart.clearInputBuffer();
223  if(channel > 7)
224  abort("Bad ADC channel: " + std::to_string(channel));
225 
226  uint8_t rq[] =
227  {
228  RQ_ADC,
229  channel
230  };
231 
232  int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
233  if(n_sent != sizeof(rq))
234  abort("Sent failed");
235 
236  uint16_t adc = usart.readInt();
237 
238  if(adc > 1023)
239  abort("Bad ADC data detected (1)");
240  return adc;
241 }
242 
243 void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
244 {
245  // check pointers
246  buffer_a += offset_a;
247  buffer_b += offset_b;
248 
249 
250  usart.clearInputBuffer();
251  usart.writeByte(RQ_ADC_DAC_STROKE);
252  usart.writeByte(channel_a);
253  usart.writeByte(channel_b);
254  usart.writeInt(start);
255  usart.writeInt(static_cast<uint16_t>(delta));
256  usart.writeInt(count);
257 
258  for(uint16_t i = 0; i < count; i++)
259  {
260  if(buffer_a)
261  {
262  buffer_a[i] = usart.readInt();
263 
264  if(buffer_a[i] > 1023) // check for broken usart connection
265  abort("Bad ADC data detected (2)");
266  }
267  else
268  {
269  usart.readInt();
270  }
271 
272  if(buffer_b)
273  {
274  buffer_b[i] = usart.readInt();
275 
276  if(buffer_b[i] > 1023) // check for broken usart connection
277  abort("Bad ADC data detected (3)");
278  }
279  else
280  {
281  usart.readInt();
282  }
283  }
284 
285  uint8_t aw = usart.readByte();
286  if(aw != MSG_OK)
287  abort("Sequenz unterbrochen");
288 
289  delay_us(10);
290 }
291 
292 uint8_t B15F::pwmSetFrequency(uint32_t freq)
293 {
294  usart.clearInputBuffer();
295 
296  uint8_t rq[] =
297  {
298  RQ_PWM_SET_FREQ,
299  static_cast<uint8_t>((freq >> 0) & 0xFF),
300  static_cast<uint8_t>((freq >> 8) & 0xFF),
301  static_cast<uint8_t>((freq >> 16) & 0xFF),
302  static_cast<uint8_t>((freq >> 24) & 0xFF)
303  };
304 
305  int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
306  if(n_sent != sizeof(rq))
307  abort("Sent failed");
308 
309  uint8_t byte = usart.readByte();
310  delay_us(10);
311  return byte;
312 }
313 
314 bool B15F::pwmSetValue(uint8_t value)
315 {
316  usart.clearInputBuffer();
317 
318  uint8_t rq[] =
319  {
320  RQ_PWM_SET_VALUE,
321  value
322  };
323 
324  int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
325  if(n_sent != sizeof(rq))
326  abort("Sent failed");
327 
328  uint8_t aw = usart.readByte();
329  delay_us(10);
330  return aw == MSG_OK;
331 }
332 
333 void B15F::delay_ms(uint16_t ms)
334 {
335  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
336 }
337 
338 void B15F::delay_us(uint16_t us)
339 {
340  std::this_thread::sleep_for(std::chrono::microseconds(us));
341 }
342 
344 {
345  if(!instance)
346  instance = new B15F();
347 
348  return *instance;
349 }
350 
351 // https://stackoverflow.com/a/478960
352 std::string B15F::exec(std::string cmd)
353 {
354  std::array<char, 128> buffer;
355  std::string result;
356  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
357  if (!pipe)
358  {
359  throw std::runtime_error("popen() failed!");
360  }
361  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
362  {
363  result += buffer.data();
364  }
365  return result;
366 }
367 
368 void B15F::abort(std::string msg)
369 {
370  DriverException ex(msg);
371  abort(ex);
372 }
373 void B15F::abort(std::exception& ex)
374 {
375  if(errorhandler)
376  errorhandler(ex);
377  else
378  {
379  std::cerr << "NOTICE: B15F::errorhandler not set" << std::endl;
380  std::cout << ex.what() << std::endl;
381  throw DriverException(ex.what());
382  }
383 }
384 
385 void B15F::setAbortHandler(errorhandler_t func)
386 {
387  errorhandler = func;
388 }
+
static std::string exec(std::string cmd)
Definition: b15f.cpp:352
+
uint8_t readByte(void)
Definition: usart.cpp:216
+
void delay_us(uint16_t us)
Definition: b15f.cpp:338
uint8_t digitalRead0(void)
Definition: b15f.cpp:173
+
uint8_t pwmSetFrequency(uint32_t freq)
Definition: b15f.cpp:292
void analogSequence(uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
Definition: b15f.cpp:243
bool testConnection(void)
Definition: b15f.cpp:90
uint8_t readDipSwitch(void)
Definition: b15f.cpp:191
-
void delay_ms(uint16_t ms)
Definition: b15f.cpp:292
-
static B15F & getInstance(void)
Definition: b15f.cpp:302
+
void delay_ms(uint16_t ms)
Definition: b15f.cpp:333
+
static B15F & getInstance(void)
Definition: b15f.cpp:343
Definition: b15f.h:26
-
static void abort(std::string msg)
Definition: b15f.cpp:327
+
static void abort(std::string msg)
Definition: b15f.cpp:368
void clearInputBuffer(void)
Definition: usart.cpp:39
void clearOutputBuffer(void)
Definition: usart.cpp:46
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:220
bool digitalWrite0(uint8_t)
Definition: b15f.cpp:153
-
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:199
-
void setBaudrate(uint32_t baudrate)
Definition: usart.cpp:317
+
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:215
+
void setBaudrate(uint32_t baudrate)
Definition: usart.cpp:322
bool activateSelfTestMode(void)
Definition: b15f.cpp:145
std::vector< std::string > getBoardInfo(void)
Definition: b15f.cpp:118
-
constexpr static uint16_t RECONNECT_TIMEOUT
Time in ms after which a reconnect attempt aborts.
Definition: b15f.h:202
+
constexpr static uint16_t RECONNECT_TIMEOUT
Time in ms after which a reconnect attempt aborts.
Definition: b15f.h:218
bool analogWrite1(uint16_t port)
Definition: b15f.cpp:210
void writeByte(uint8_t b)
Definition: usart.cpp:67
bool digitalWrite1(uint8_t)
Definition: b15f.cpp:163
+
bool pwmSetValue(uint8_t value)
Definition: b15f.cpp:314
void discard(void)
Definition: b15f.cpp:72
-
constexpr static uint8_t MSG_OK
Value to acknowledge a received command.
Definition: b15f.h:200
+
constexpr static uint8_t MSG_OK
Value to acknowledge a received command.
Definition: b15f.h:216
void openDevice(std::string device)
Definition: usart.cpp:3
-
uint16_t readInt(void)
Definition: usart.cpp:230
+
uint16_t readInt(void)
Definition: usart.cpp:235
uint8_t digitalRead1(void)
Definition: b15f.cpp:182
void reconnect(void)
Definition: b15f.cpp:57
-
constexpr static uint32_t BAUDRATE
USART baudrate for communication with the MCU.
Definition: b15f.h:205
-
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:344
+
constexpr static uint32_t BAUDRATE
USART baudrate for communication with the MCU.
Definition: b15f.h:221
+
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:385
bool analogWrite0(uint16_t port)
Definition: b15f.cpp:200
-
constexpr static uint8_t RECONNECT_TRIES
Maximum count of reconnect attempts after which the driver stops.
Definition: b15f.h:204
+
constexpr static uint8_t RECONNECT_TRIES
Maximum count of reconnect attempts after which the driver stops.
Definition: b15f.h:220
void writeInt(uint16_t d)
Definition: usart.cpp:81
bool testIntConv(void)
Definition: b15f.cpp:105
diff --git a/docs/html/b15f_8h_source.html b/docs/html/b15f_8h_source.html index 0f9eff8..346c23c 100644 --- a/docs/html/b15f_8h_source.html +++ b/docs/html/b15f_8h_source.html @@ -70,40 +70,42 @@ $(function() {
b15f.h
-
1 #ifndef B15F_H
2 #define B15F_H
3 
4 #include <iostream>
5 #include <bits/stdc++.h>
6 #include <string>
7 #include <fstream>
8 #include <cstdlib>
9 #include <chrono>
10 #include <cstdint>
11 #include <vector>
12 
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include <termios.h>
17 #include "usart.h"
18 #include "driverexception.h"
19 #include "timeoutexception.h"
20 
21 typedef std::function<void(std::exception&)> errorhandler_t;
22 
23 
26 class B15F
27 {
28 private:
29  // privater Konstruktor
30  B15F(void);
31 public:
32 
33  /*************************************
34  * Grundfunktionen des B15F Treibers *
35  *************************************/
36 
41  void reconnect(void);
42 
47  void discard(void);
48 
53  bool testConnection(void);
54 
59  bool testIntConv(void);
60 
65  std::vector<std::string> getBoardInfo(void);
66 
71  void delay_ms(uint16_t ms);
72 
77  void delay_us(uint16_t us);
78 
83  static B15F& getInstance(void);
84 
89  static std::string exec(std::string cmd);
90 
95  static void abort(std::string msg);
96 
101  static void abort(std::exception& ex);
102 
107  static void setAbortHandler(errorhandler_t func);
108 
109  /*************************************/
110 
111 
112 
113  /*************************
114  * Steuerbefehle für B15 *
115  *************************/
116 
122  bool activateSelfTestMode(void);
123 
129  bool digitalWrite0(uint8_t);
130 
136  bool digitalWrite1(uint8_t);
137 
143  uint8_t digitalRead0(void);
144 
150  uint8_t digitalRead1(void);
151 
157  uint8_t readDipSwitch(void);
158 
164  bool analogWrite0(uint16_t port);
165 
171  bool analogWrite1(uint16_t port);
172 
178  uint16_t analogRead(uint8_t channel);
179 
195  void analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count);
196 
197  /*************************/
198 
199 
200  // CONSTANTS
201  const std::string PRE = "[B15F] ";
202  constexpr static uint8_t MSG_OK = 0xFF;
203  constexpr static uint8_t MSG_FAIL = 0xFE;
204  constexpr static uint16_t RECONNECT_TIMEOUT = 64;
205  constexpr static uint16_t WDT_TIMEOUT = 15;
206  constexpr static uint8_t RECONNECT_TRIES = 3;
207  constexpr static uint32_t BAUDRATE = 57600;
208 
209 private:
210 
215  void init(void);
216 
217  USART usart;
218  static B15F* instance;
219  static errorhandler_t errorhandler;
220 
221  // REQUESTS
222  constexpr static uint8_t RQ_DISC = 0;
223  constexpr static uint8_t RQ_TEST = 1;
224  constexpr static uint8_t RQ_INFO = 2;
225  constexpr static uint8_t RQ_INT = 3;
226  constexpr static uint8_t RQ_ST = 4;
227  constexpr static uint8_t RQ_BA0 = 5;
228  constexpr static uint8_t RQ_BA1 = 6;
229  constexpr static uint8_t RQ_BE0 = 7;
230  constexpr static uint8_t RQ_BE1 = 8;
231  constexpr static uint8_t RQ_DSW = 9;
232  constexpr static uint8_t RQ_AA0 = 10;
233  constexpr static uint8_t RQ_AA1 = 11;
234  constexpr static uint8_t RQ_ADC = 12;
235  constexpr static uint8_t RQ_ADC_DAC_STROKE = 13;
236 };
237 
238 #endif // B15F_H
-
static std::string exec(std::string cmd)
Definition: b15f.cpp:311
-
constexpr static uint8_t MSG_FAIL
Value to reject a received command.
Definition: b15f.h:201
-
void delay_us(uint16_t us)
Definition: b15f.cpp:297
+
1 #ifndef B15F_H
2 #define B15F_H
3 
4 #include <iostream>
5 #include <bits/stdc++.h>
6 #include <string>
7 #include <fstream>
8 #include <cstdlib>
9 #include <chrono>
10 #include <cstdint>
11 #include <vector>
12 
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include <termios.h>
17 #include "usart.h"
18 #include "driverexception.h"
19 #include "timeoutexception.h"
20 
21 typedef std::function<void(std::exception&)> errorhandler_t;
22 
23 
26 class B15F
27 {
28 private:
29  // privater Konstruktor
30  B15F(void);
31 public:
32 
33  /*************************************
34  * Grundfunktionen des B15F Treibers *
35  *************************************/
36 
41  void reconnect(void);
42 
47  void discard(void);
48 
53  bool testConnection(void);
54 
59  bool testIntConv(void);
60 
65  std::vector<std::string> getBoardInfo(void);
66 
71  void delay_ms(uint16_t ms);
72 
77  void delay_us(uint16_t us);
78 
83  static B15F& getInstance(void);
84 
89  static std::string exec(std::string cmd);
90 
95  static void abort(std::string msg);
96 
101  static void abort(std::exception& ex);
102 
107  static void setAbortHandler(errorhandler_t func);
108 
109  /*************************************/
110 
111 
112 
113  /*************************
114  * Steuerbefehle für B15 *
115  *************************/
116 
122  bool activateSelfTestMode(void);
123 
129  bool digitalWrite0(uint8_t);
130 
136  bool digitalWrite1(uint8_t);
137 
143  uint8_t digitalRead0(void);
144 
150  uint8_t digitalRead1(void);
151 
157  uint8_t readDipSwitch(void);
158 
164  bool analogWrite0(uint16_t port);
165 
171  bool analogWrite1(uint16_t port);
172 
178  uint16_t analogRead(uint8_t channel);
179 
195  void analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count);
196 
204  uint8_t pwmSetFrequency(uint32_t freq);
205 
211  bool pwmSetValue(uint8_t value);
212 
213  /*************************/
214 
215 
216  // CONSTANTS
217  const std::string PRE = "[B15F] ";
218  constexpr static uint8_t MSG_OK = 0xFF;
219  constexpr static uint8_t MSG_FAIL = 0xFE;
220  constexpr static uint16_t RECONNECT_TIMEOUT = 64;
221  constexpr static uint16_t WDT_TIMEOUT = 15;
222  constexpr static uint8_t RECONNECT_TRIES = 3;
223  constexpr static uint32_t BAUDRATE = 57600;
224 
225 private:
226 
231  void init(void);
232 
233  USART usart;
234  static B15F* instance;
235  static errorhandler_t errorhandler;
236 
237  // REQUESTS
238  constexpr static uint8_t RQ_DISC = 0;
239  constexpr static uint8_t RQ_TEST = 1;
240  constexpr static uint8_t RQ_INFO = 2;
241  constexpr static uint8_t RQ_INT = 3;
242  constexpr static uint8_t RQ_ST = 4;
243  constexpr static uint8_t RQ_BA0 = 5;
244  constexpr static uint8_t RQ_BA1 = 6;
245  constexpr static uint8_t RQ_BE0 = 7;
246  constexpr static uint8_t RQ_BE1 = 8;
247  constexpr static uint8_t RQ_DSW = 9;
248  constexpr static uint8_t RQ_AA0 = 10;
249  constexpr static uint8_t RQ_AA1 = 11;
250  constexpr static uint8_t RQ_ADC = 12;
251  constexpr static uint8_t RQ_ADC_DAC_STROKE = 13;
252  constexpr static uint8_t RQ_PWM_SET_FREQ = 14;
253  constexpr static uint8_t RQ_PWM_SET_VALUE = 15;
254 };
255 
256 #endif // B15F_H
+
static std::string exec(std::string cmd)
Definition: b15f.cpp:352
+
constexpr static uint8_t MSG_FAIL
Value to reject a received command.
Definition: b15f.h:217
+
void delay_us(uint16_t us)
Definition: b15f.cpp:338
uint8_t digitalRead0(void)
Definition: b15f.cpp:173
+
uint8_t pwmSetFrequency(uint32_t freq)
Definition: b15f.cpp:292
void analogSequence(uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
Definition: b15f.cpp:243
bool testConnection(void)
Definition: b15f.cpp:90
uint8_t readDipSwitch(void)
Definition: b15f.cpp:191
-
void delay_ms(uint16_t ms)
Definition: b15f.cpp:292
-
static B15F & getInstance(void)
Definition: b15f.cpp:302
+
void delay_ms(uint16_t ms)
Definition: b15f.cpp:333
+
static B15F & getInstance(void)
Definition: b15f.cpp:343
Definition: b15f.h:26
-
static void abort(std::string msg)
Definition: b15f.cpp:327
+
static void abort(std::string msg)
Definition: b15f.cpp:368
Definition: usart.h:18
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:220
bool digitalWrite0(uint8_t)
Definition: b15f.cpp:153
-
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:199
+
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:215
bool activateSelfTestMode(void)
Definition: b15f.cpp:145
std::vector< std::string > getBoardInfo(void)
Definition: b15f.cpp:118
-
constexpr static uint16_t RECONNECT_TIMEOUT
Time in ms after which a reconnect attempt aborts.
Definition: b15f.h:202
+
constexpr static uint16_t RECONNECT_TIMEOUT
Time in ms after which a reconnect attempt aborts.
Definition: b15f.h:218
bool analogWrite1(uint16_t port)
Definition: b15f.cpp:210
bool digitalWrite1(uint8_t)
Definition: b15f.cpp:163
+
bool pwmSetValue(uint8_t value)
Definition: b15f.cpp:314
void discard(void)
Definition: b15f.cpp:72
-
constexpr static uint8_t MSG_OK
Value to acknowledge a received command.
Definition: b15f.h:200
+
constexpr static uint8_t MSG_OK
Value to acknowledge a received command.
Definition: b15f.h:216
uint8_t digitalRead1(void)
Definition: b15f.cpp:182
-
constexpr static uint16_t WDT_TIMEOUT
Time in ms after which the watch dog timer resets the MCU.
Definition: b15f.h:203
+
constexpr static uint16_t WDT_TIMEOUT
Time in ms after which the watch dog timer resets the MCU.
Definition: b15f.h:219
void reconnect(void)
Definition: b15f.cpp:57
-
constexpr static uint32_t BAUDRATE
USART baudrate for communication with the MCU.
Definition: b15f.h:205
-
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:344
+
constexpr static uint32_t BAUDRATE
USART baudrate for communication with the MCU.
Definition: b15f.h:221
+
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:385
bool analogWrite0(uint16_t port)
Definition: b15f.cpp:200
-
constexpr static uint8_t RECONNECT_TRIES
Maximum count of reconnect attempts after which the driver stops.
Definition: b15f.h:204
+
constexpr static uint8_t RECONNECT_TRIES
Maximum count of reconnect attempts after which the driver stops.
Definition: b15f.h:220
bool testIntConv(void)
Definition: b15f.cpp:105
diff --git a/docs/html/classB15F-members.html b/docs/html/classB15F-members.html index 1c32f74..119895f 100644 --- a/docs/html/classB15F-members.html +++ b/docs/html/classB15F-members.html @@ -90,6 +90,8 @@ $(function() { MSG_FAILB15Fstatic MSG_OKB15Fstatic PREB15F + pwmSetFrequency(uint32_t freq)B15F + pwmSetValue(uint8_t value)B15F readDipSwitch(void)B15F reconnect(void)B15F RECONNECT_TIMEOUTB15Fstatic @@ -101,7 +103,7 @@ $(function() { diff --git a/docs/html/classB15F.html b/docs/html/classB15F.html index 7271305..f22425d 100644 --- a/docs/html/classB15F.html +++ b/docs/html/classB15F.html @@ -111,6 +111,10 @@ Public Member Functions   void analogSequence (uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)   +uint8_t pwmSetFrequency (uint32_t freq) +  +bool pwmSetValue (uint8_t value) +  @@ -194,7 +198,7 @@ constexpr static uint32_t 

Static Public Member Functions

-

Definition at line 332 of file b15f.cpp.

+

Definition at line 373 of file b15f.cpp.

@@ -228,7 +232,7 @@ constexpr static uint32_t 
-

Definition at line 327 of file b15f.cpp.

+

Definition at line 368 of file b15f.cpp.

@@ -468,7 +472,7 @@ constexpr static uint32_t 
-

Definition at line 292 of file b15f.cpp.

+

Definition at line 333 of file b15f.cpp.

@@ -494,7 +498,7 @@ constexpr static uint32_t 
-

Definition at line 297 of file b15f.cpp.

+

Definition at line 338 of file b15f.cpp.

@@ -672,7 +676,7 @@ constexpr static uint32_t 
-

Definition at line 311 of file b15f.cpp.

+

Definition at line 352 of file b15f.cpp.

@@ -732,7 +736,72 @@ constexpr static uint32_t 
-

Definition at line 302 of file b15f.cpp.

+

Definition at line 343 of file b15f.cpp.

+ + + + +

◆ pwmSetFrequency()

+ +
+
+ + + + + + + + +
uint8_t B15F::pwmSetFrequency (uint32_t freq)
+
+

Setzt die Register so, dass näherungsweise die gewünschte Frequenz erzeugt wird. Ist freq == 0 wird PWM deaktiviert.

Parameters
+ + +
freqPWM Frequenz
+
+
+
Returns
Top Wert des PWM Value für die gesetzte Frequenz
+
Exceptions
+ + +
DriverException
+
+
+ +

Definition at line 292 of file b15f.cpp.

+ +
+
+ +

◆ pwmSetValue()

+ +
+
+ + + + + + + + +
bool B15F::pwmSetValue (uint8_t value)
+
+

Setzt den PWM Wert.

Parameters
+ + +
valuePWM Wert [0..0xFF]
+
+
+
Exceptions
+ + +
DriverException
+
+
+ +

Definition at line 314 of file b15f.cpp.

@@ -819,7 +888,7 @@ constexpr static uint32_t 
-

Definition at line 344 of file b15f.cpp.

+

Definition at line 385 of file b15f.cpp.

@@ -882,7 +951,7 @@ constexpr static uint32_t 
diff --git a/docs/html/classDot-members.html b/docs/html/classDot-members.html index c1464ba..fafd43d 100644 --- a/docs/html/classDot-members.html +++ b/docs/html/classDot-members.html @@ -76,7 +76,7 @@ $(function() {
diff --git a/docs/html/classDot.html b/docs/html/classDot.html index 318fa5d..b05a9bc 100644 --- a/docs/html/classDot.html +++ b/docs/html/classDot.html @@ -196,7 +196,7 @@ Public Member Functions diff --git a/docs/html/classDriverException-members.html b/docs/html/classDriverException-members.html index 848c1ce..5f85cd6 100644 --- a/docs/html/classDriverException-members.html +++ b/docs/html/classDriverException-members.html @@ -77,7 +77,7 @@ $(function() { diff --git a/docs/html/classDriverException.html b/docs/html/classDriverException.html index 5df8380..95ed51e 100644 --- a/docs/html/classDriverException.html +++ b/docs/html/classDriverException.html @@ -109,7 +109,7 @@ std::string msg_< diff --git a/docs/html/classPlottyFile-members.html b/docs/html/classPlottyFile-members.html index 3e7bb5c..b38eece 100644 --- a/docs/html/classPlottyFile-members.html +++ b/docs/html/classPlottyFile-members.html @@ -100,7 +100,7 @@ $(function() { diff --git a/docs/html/classPlottyFile.html b/docs/html/classPlottyFile.html index d68ad8e..de9db48 100644 --- a/docs/html/classPlottyFile.html +++ b/docs/html/classPlottyFile.html @@ -811,7 +811,7 @@ Public Member Functions diff --git a/docs/html/classTimeoutException-members.html b/docs/html/classTimeoutException-members.html index a656c56..8c3285a 100644 --- a/docs/html/classTimeoutException-members.html +++ b/docs/html/classTimeoutException-members.html @@ -78,7 +78,7 @@ $(function() { diff --git a/docs/html/classTimeoutException.html b/docs/html/classTimeoutException.html index 77727db..6aa40b1 100644 --- a/docs/html/classTimeoutException.html +++ b/docs/html/classTimeoutException.html @@ -112,7 +112,7 @@ int m_timeout diff --git a/docs/html/classUSART-members.html b/docs/html/classUSART-members.html index 4dc25df..8d603c6 100644 --- a/docs/html/classUSART-members.html +++ b/docs/html/classUSART-members.html @@ -90,10 +90,11 @@ $(function() { writeBlock(uint8_t *buffer, uint16_t offset, uint8_t len) (defined in USART)USART writeByte(uint8_t b)USART writeInt(uint16_t d)USART + writeU32(uint32_t d)USART diff --git a/docs/html/classUSART.html b/docs/html/classUSART.html index 89274cc..aa16cc1 100644 --- a/docs/html/classUSART.html +++ b/docs/html/classUSART.html @@ -91,6 +91,8 @@ Public Member Functions   void writeInt (uint16_t d)   +void writeU32 (uint32_t d) +  uint8_t readByte (void)   uint16_t readInt (void) @@ -254,7 +256,7 @@ constexpr static uint8_t B

Liefert die eingestellte Baudrate Änderungen werden erst nach einem open() wirksam

-

Definition at line 307 of file usart.cpp.

+

Definition at line 312 of file usart.cpp.

@@ -275,7 +277,7 @@ constexpr static uint8_t B

Liefert den eingestellten Timeout (in Dezisekunden) Änderungen werden erst nach einem open() wirksam

-

Definition at line 312 of file usart.cpp.

+

Definition at line 317 of file usart.cpp.

@@ -354,7 +356,7 @@ constexpr static uint8_t B -

Definition at line 211 of file usart.cpp.

+

Definition at line 216 of file usart.cpp.

@@ -380,7 +382,7 @@ constexpr static uint8_t B -

Definition at line 230 of file usart.cpp.

+

Definition at line 235 of file usart.cpp.

@@ -401,7 +403,7 @@ constexpr static uint8_t B

Setzt die Baudrate Änderungen werden erst nach einem open() wirksam

-

Definition at line 317 of file usart.cpp.

+

Definition at line 322 of file usart.cpp.

@@ -422,7 +424,7 @@ constexpr static uint8_t B

Setzt den Timeout (in Dezisekunden) Änderungen werden erst nach einem open() wirksam

-

Definition at line 322 of file usart.cpp.

+

Definition at line 327 of file usart.cpp.

@@ -475,7 +477,7 @@ constexpr static uint8_t B

Sendet ein Integer über die USART Schnittstelle

Parameters
- +
bdas zu sendende Byte
bdas zu sendende Int
@@ -488,6 +490,38 @@ constexpr static uint8_t B

Definition at line 81 of file usart.cpp.

+
+ + +

◆ writeU32()

+ +
+
+ + + + + + + + +
void USART::writeU32 (uint32_t d)
+
+

Sendet ein uint32_t über die USART Schnittstelle

Parameters
+ + +
bdas zu sendende uint32_t
+
+
+
Exceptions
+ + +
USARTException
+
+
+ +

Definition at line 88 of file usart.cpp.

+

The documentation for this class was generated from the following files:
    @@ -497,7 +531,7 @@ constexpr static uint8_t B diff --git a/docs/html/classUSARTException-members.html b/docs/html/classUSARTException-members.html index 03df1bd..fb0f7cc 100644 --- a/docs/html/classUSARTException-members.html +++ b/docs/html/classUSARTException-members.html @@ -77,7 +77,7 @@ $(function() { diff --git a/docs/html/classUSARTException.html b/docs/html/classUSARTException.html index 4b9bc80..96d050d 100644 --- a/docs/html/classUSARTException.html +++ b/docs/html/classUSARTException.html @@ -109,7 +109,7 @@ std::string msg diff --git a/docs/html/classView-members.html b/docs/html/classView-members.html index 94750c9..0816e70 100644 --- a/docs/html/classView-members.html +++ b/docs/html/classView-members.html @@ -89,7 +89,7 @@ $(function() { diff --git a/docs/html/classView.html b/docs/html/classView.html index 5c420fa..ba64d4a 100644 --- a/docs/html/classView.html +++ b/docs/html/classView.html @@ -157,7 +157,7 @@ constexpr static int KEY_E diff --git a/docs/html/classViewInfo-members.html b/docs/html/classViewInfo-members.html index 62d2103..11fecf3 100644 --- a/docs/html/classViewInfo-members.html +++ b/docs/html/classViewInfo-members.html @@ -99,7 +99,7 @@ $(function() { diff --git a/docs/html/classViewInfo.html b/docs/html/classViewInfo.html index c78f59d..090a6da 100644 --- a/docs/html/classViewInfo.html +++ b/docs/html/classViewInfo.html @@ -185,7 +185,7 @@ static std::vector< std::string >  diff --git a/docs/html/classViewMonitor-members.html b/docs/html/classViewMonitor-members.html index c2f4e01..f8b0248 100644 --- a/docs/html/classViewMonitor-members.html +++ b/docs/html/classViewMonitor-members.html @@ -103,7 +103,7 @@ $(function() { diff --git a/docs/html/classViewMonitor.html b/docs/html/classViewMonitor.html index 9c59752..7566433 100644 --- a/docs/html/classViewMonitor.html +++ b/docs/html/classViewMonitor.html @@ -197,7 +197,7 @@ constexpr static int KEY_E diff --git a/docs/html/classViewPromt-members.html b/docs/html/classViewPromt-members.html index b0cafed..0620da4 100644 --- a/docs/html/classViewPromt-members.html +++ b/docs/html/classViewPromt-members.html @@ -105,7 +105,7 @@ $(function() { diff --git a/docs/html/classViewPromt.html b/docs/html/classViewPromt.html index 22b82ad..e4f148d 100644 --- a/docs/html/classViewPromt.html +++ b/docs/html/classViewPromt.html @@ -205,7 +205,7 @@ static std::vector< std::string >  diff --git a/docs/html/classViewSelection-members.html b/docs/html/classViewSelection-members.html index 595ba75..7eb9d49 100644 --- a/docs/html/classViewSelection-members.html +++ b/docs/html/classViewSelection-members.html @@ -94,7 +94,7 @@ $(function() { diff --git a/docs/html/classViewSelection.html b/docs/html/classViewSelection.html index e92f205..3983a76 100644 --- a/docs/html/classViewSelection.html +++ b/docs/html/classViewSelection.html @@ -172,7 +172,7 @@ static std::vector< std::string >  diff --git a/docs/html/classes.html b/docs/html/classes.html index 8d0d0f8..54bd780 100644 --- a/docs/html/classes.html +++ b/docs/html/classes.html @@ -105,7 +105,7 @@ $(function() { diff --git a/docs/html/cli_8cpp_source.html b/docs/html/cli_8cpp_source.html index 801fcb9..941591f 100644 --- a/docs/html/cli_8cpp_source.html +++ b/docs/html/cli_8cpp_source.html @@ -68,11 +68,11 @@ $(function() {
    1 #define B15F_CLI_DEBUG
    2 
    3 #include <stdio.h>
    4 #include <ncurses.h> // sudo apt-get install libncurses5-dev
    5 #include <vector>
    6 #include <string>
    7 #include <iostream>
    8 #include <signal.h>
    9 #include <sys/ioctl.h>
    10 #include <unistd.h>
    11 #include <signal.h>
    12 #include <future>
    13 #include <thread>
    14 #include <chrono>
    15 #include "drv/b15f.h"
    16 #include "ui/ui.h"
    17 #include "ui/view_selection.h"
    18 #include "ui/view_info.h"
    19 #include "ui/view_monitor.h"
    20 #include "ui/view_promt.h"
    21 
    22 volatile int win_changed_cooldown = 0;
    23 volatile bool t_refresh_active = false;
    24 
    25 void signal_handler(int signal)
    26 {
    27  if(signal == SIGWINCH)
    28  {
    29  win_changed_cooldown = 10; // 100ms
    30 
    31  if (!t_refresh_active)
    32  {
    33  if(t_refresh.joinable())
    34  t_refresh.join();
    35  t_refresh_active = true;
    36  t_refresh = std::thread([]()
    37  {
    38 
    39  while(win_changed_cooldown--)
    40  std::this_thread::sleep_for(std::chrono::milliseconds(10));
    41 
    42  t_refresh_active = false;
    43 
    44  if(win_stack.size())
    45  win_stack.back()->repaint();
    46 
    47  });
    48  }
    49 
    50  }
    51  else if(signal == SIGINT)
    52  {
    53  cleanup();
    54  std::cout << "SIGINT - Abbruch." << std::endl;
    55  exit(EXIT_FAILURE);
    56  }
    57 }
    58 
    59 void abort_handler(std::exception& ex)
    60 {
    61  ViewInfo* view = new ViewInfo();
    62  view->setTitle("Fehler");
    63  std::string msg(ex.what());
    64  msg += "\n\nBeende in 5 Sekunden.";
    65  view->setText(msg.c_str());
    66  view->setLabelClose("");
    67  view->repaint();
    68 
    69  std::this_thread::sleep_for(std::chrono::milliseconds(5000));
    70 
    71  cleanup();
    72  std::cerr << std::endl << "*** EXCEPTION ***" << std::endl << ex.what() << std::endl;
    73  exit(EXIT_FAILURE);
    74 }
    75 
    76 void init()
    77 {
    78  // init b15 driver
    80 #ifndef B15F_CLI_DEBUG
    81  std::cout << std::endl << "Starte in 3s ..." << std::endl;
    82  sleep(3);
    83 #endif
    84  B15F::setAbortHandler(&abort_handler);
    85 
    86  // init all ncurses stuff
    87  initscr();
    88  start_color();
    89  curs_set(0); // 0: invisible, 1: normal, 2: very visible
    90  clear();
    91  noecho();
    92  cbreak(); // Line buffering disabled. pass on everything
    93  mousemask(ALL_MOUSE_EVENTS, NULL);
    94 
    95  // connect signals to handler
    96  signal(SIGWINCH, signal_handler);
    97  signal(SIGINT, signal_handler);
    98 
    99  // set view context
    100  View::setWinContext(newwin(25, 85, 0, 0));
    101 }
    102 
    103 
    104 int main()
    105 {
    106  init();
    107 
    108  int exit_code = EXIT_SUCCESS;
    109 
    110  show_main(0);
    111 
    112  cleanup();
    113 
    114  return exit_code;
    115 }
    -
    static B15F & getInstance(void)
    Definition: b15f.cpp:302
    -
    static void setAbortHandler(errorhandler_t func)
    Definition: b15f.cpp:344
    +
    static B15F & getInstance(void)
    Definition: b15f.cpp:343
    +
    static void setAbortHandler(errorhandler_t func)
    Definition: b15f.cpp:385
    diff --git a/docs/html/dir_1788f8309b1a812dcb800a185471cf6c.html b/docs/html/dir_1788f8309b1a812dcb800a185471cf6c.html index bebf28d..bd969af 100644 --- a/docs/html/dir_1788f8309b1a812dcb800a185471cf6c.html +++ b/docs/html/dir_1788f8309b1a812dcb800a185471cf6c.html @@ -73,7 +73,7 @@ $(function() { diff --git a/docs/html/dir_587c94d866dbb2f408f78cf41f9b2f8d.html b/docs/html/dir_587c94d866dbb2f408f78cf41f9b2f8d.html index 52fcf49..9d8eb0a 100644 --- a/docs/html/dir_587c94d866dbb2f408f78cf41f9b2f8d.html +++ b/docs/html/dir_587c94d866dbb2f408f78cf41f9b2f8d.html @@ -73,7 +73,7 @@ $(function() { diff --git a/docs/html/dot_8cpp_source.html b/docs/html/dot_8cpp_source.html index 853835b..dd83450 100644 --- a/docs/html/dot_8cpp_source.html +++ b/docs/html/dot_8cpp_source.html @@ -77,7 +77,7 @@ $(function() {
    Dot(uint16_t x, uint16_t y, uint8_t curve)
    Definition: dot.cpp:3
    diff --git a/docs/html/dot_8h_source.html b/docs/html/dot_8h_source.html index 46b7ae7..9c0bd19 100644 --- a/docs/html/dot_8h_source.html +++ b/docs/html/dot_8h_source.html @@ -78,7 +78,7 @@ $(function() {
    Dot(uint16_t x, uint16_t y, uint8_t curve)
    Definition: dot.cpp:3
    diff --git a/docs/html/driverexception_8h_source.html b/docs/html/driverexception_8h_source.html index 25897b6..015c2ad 100644 --- a/docs/html/driverexception_8h_source.html +++ b/docs/html/driverexception_8h_source.html @@ -74,7 +74,7 @@ $(function() { diff --git a/docs/html/files.html b/docs/html/files.html index 92b9ab6..e880b89 100644 --- a/docs/html/files.html +++ b/docs/html/files.html @@ -99,7 +99,7 @@ $(function() { diff --git a/docs/html/functions.html b/docs/html/functions.html index 1df7c12..26bae8d 100644 --- a/docs/html/functions.html +++ b/docs/html/functions.html @@ -236,6 +236,12 @@ $(function() {
  • printStatistics() : USART
  • +
  • pwmSetFrequency() +: B15F +
  • +
  • pwmSetValue() +: B15F +
@@ -336,11 +342,14 @@ $(function() {
  • writeToFile() : PlottyFile
  • +
  • writeU32() +: USART +
  • diff --git a/docs/html/functions_func.html b/docs/html/functions_func.html index 4fbff3f..1c2fd52 100644 --- a/docs/html/functions_func.html +++ b/docs/html/functions_func.html @@ -216,6 +216,12 @@ $(function() {
  • printStatistics() : USART
  • +
  • pwmSetFrequency() +: B15F +
  • +
  • pwmSetValue() +: B15F +
  • @@ -307,11 +313,14 @@ $(function() {
  • writeToFile() : PlottyFile
  • +
  • writeU32() +: USART +
  • diff --git a/docs/html/functions_vars.html b/docs/html/functions_vars.html index c646f33..3172ad7 100644 --- a/docs/html/functions_vars.html +++ b/docs/html/functions_vars.html @@ -88,7 +88,7 @@ $(function() { diff --git a/docs/html/hierarchy.html b/docs/html/hierarchy.html index c8df567..bf24851 100644 --- a/docs/html/hierarchy.html +++ b/docs/html/hierarchy.html @@ -86,7 +86,7 @@ $(function() { diff --git a/docs/html/index.html b/docs/html/index.html index aa89bbd..50426bf 100644 --- a/docs/html/index.html +++ b/docs/html/index.html @@ -72,7 +72,7 @@ Dort befindet sich auch eine Übersicht der verfügbaren Befehle.

    diff --git a/docs/html/plottyfile_8cpp_source.html b/docs/html/plottyfile_8cpp_source.html index c31ae5e..abd1e0b 100644 --- a/docs/html/plottyfile_8cpp_source.html +++ b/docs/html/plottyfile_8cpp_source.html @@ -101,7 +101,7 @@ $(function() {
    std::string getUnitX(void) const
    Definition: plottyfile.cpp:105
    diff --git a/docs/html/plottyfile_8h_source.html b/docs/html/plottyfile_8h_source.html index 4fdc771..dd26e80 100644 --- a/docs/html/plottyfile_8h_source.html +++ b/docs/html/plottyfile_8h_source.html @@ -102,7 +102,7 @@ $(function() {
    std::string getUnitX(void) const
    Definition: plottyfile.cpp:105
    diff --git a/docs/html/search/all_9.js b/docs/html/search/all_9.js index b1c5b0a..c3ed16c 100644 --- a/docs/html/search/all_9.js +++ b/docs/html/search/all_9.js @@ -2,5 +2,7 @@ var searchData= [ ['plottyfile',['PlottyFile',['../classPlottyFile.html',1,'']]], ['pre',['PRE',['../classB15F.html#a3b0fc1f85954b2d9c145af4a3af5b1ec',1,'B15F']]], - ['printstatistics',['printStatistics',['../classUSART.html#a33559bb8f0eda33a489d47b9c9227b59',1,'USART']]] + ['printstatistics',['printStatistics',['../classUSART.html#a33559bb8f0eda33a489d47b9c9227b59',1,'USART']]], + ['pwmsetfrequency',['pwmSetFrequency',['../classB15F.html#ac6f6532bb9550a0632c28b98c157d0a1',1,'B15F']]], + ['pwmsetvalue',['pwmSetValue',['../classB15F.html#af9aad3c0db5d5a8b37219d713e1977ee',1,'B15F']]] ]; diff --git a/docs/html/search/all_f.js b/docs/html/search/all_f.js index 021aad6..6a81098 100644 --- a/docs/html/search/all_f.js +++ b/docs/html/search/all_f.js @@ -3,5 +3,6 @@ var searchData= ['wdt_5ftimeout',['WDT_TIMEOUT',['../classB15F.html#a158d13bc84aed6430cdede1396384e06',1,'B15F']]], ['writebyte',['writeByte',['../classUSART.html#a60eadbe9956bab8144ee96d89eacd9f5',1,'USART']]], ['writeint',['writeInt',['../classUSART.html#a78b30d9aa863f38745e982860392599a',1,'USART']]], - ['writetofile',['writeToFile',['../classPlottyFile.html#a82c348e7fade2edcbc907e7c2bc2e305',1,'PlottyFile']]] + ['writetofile',['writeToFile',['../classPlottyFile.html#a82c348e7fade2edcbc907e7c2bc2e305',1,'PlottyFile']]], + ['writeu32',['writeU32',['../classUSART.html#a68c2d1cb7172813ae8ed61360fad47f6',1,'USART']]] ]; diff --git a/docs/html/search/functions_7.js b/docs/html/search/functions_7.js index cfb7a36..939b6a3 100644 --- a/docs/html/search/functions_7.js +++ b/docs/html/search/functions_7.js @@ -1,4 +1,6 @@ var searchData= [ - ['printstatistics',['printStatistics',['../classUSART.html#a33559bb8f0eda33a489d47b9c9227b59',1,'USART']]] + ['printstatistics',['printStatistics',['../classUSART.html#a33559bb8f0eda33a489d47b9c9227b59',1,'USART']]], + ['pwmsetfrequency',['pwmSetFrequency',['../classB15F.html#ac6f6532bb9550a0632c28b98c157d0a1',1,'B15F']]], + ['pwmsetvalue',['pwmSetValue',['../classB15F.html#af9aad3c0db5d5a8b37219d713e1977ee',1,'B15F']]] ]; diff --git a/docs/html/search/functions_b.js b/docs/html/search/functions_b.js index cbf06ef..59b6148 100644 --- a/docs/html/search/functions_b.js +++ b/docs/html/search/functions_b.js @@ -2,5 +2,6 @@ var searchData= [ ['writebyte',['writeByte',['../classUSART.html#a60eadbe9956bab8144ee96d89eacd9f5',1,'USART']]], ['writeint',['writeInt',['../classUSART.html#a78b30d9aa863f38745e982860392599a',1,'USART']]], - ['writetofile',['writeToFile',['../classPlottyFile.html#a82c348e7fade2edcbc907e7c2bc2e305',1,'PlottyFile']]] + ['writetofile',['writeToFile',['../classPlottyFile.html#a82c348e7fade2edcbc907e7c2bc2e305',1,'PlottyFile']]], + ['writeu32',['writeU32',['../classUSART.html#a68c2d1cb7172813ae8ed61360fad47f6',1,'USART']]] ]; diff --git a/docs/html/timeoutexception_8h_source.html b/docs/html/timeoutexception_8h_source.html index 84fffd2..9351c47 100644 --- a/docs/html/timeoutexception_8h_source.html +++ b/docs/html/timeoutexception_8h_source.html @@ -74,7 +74,7 @@ $(function() { diff --git a/docs/html/ui_8cpp_source.html b/docs/html/ui_8cpp_source.html index 760c88e..2012b32 100644 --- a/docs/html/ui_8cpp_source.html +++ b/docs/html/ui_8cpp_source.html @@ -73,8 +73,8 @@ $(function() {
    1 #include "ui.h"
    2 #include "../drv/b15f.h"
    3 
    4 std::vector<View*> win_stack;
    5 std::thread t_refresh;
    6 
    7 void show_main(int)
    8 {
    9  ViewSelection* view = new ViewSelection();
    10  view->setTitle("B15F - Command Line Interface");
    11  view->addChoice("[ Monitor - Eingaben beobachten ]", &show_monitor);
    12  view->addChoice("[ Digitale Ausgabe BE0 ]", &show_digital_output0);
    13  view->addChoice("[ Digitale Ausgabe BE1 ]", &show_digital_output1);
    14  view->addChoice("[ Analoge Ausgabe AA0 ]", &show_analog_output0);
    15  view->addChoice("[ Analoge Ausgabe AA1 ]", &show_analog_output1);
    16  view->addChoice("[ Selbsttest des B15 ]", &show_selftest_info);
    17  view->addChoice("[ Informationen ]", &show_info);
    18  view->addChoice("", nullptr);
    19  view->addChoice("[ Beenden ]", &finish);
    20  view->repaint();
    21 
    22  win_stack.push_back(view);
    23  input(0);
    24 }
    25 
    26 void input(int)
    27 {
    28  call_t nextCall;
    29  int key;
    30  do
    31  {
    32  key = wgetch(View::getWinContext());
    33  win_stack.back()->repaint();
    34  nextCall = win_stack.back()->keypress(key);
    35 
    36  if(key == -1)
    37  view_back(key);
    38 
    39  if(nextCall)
    40  nextCall(key);
    41  }
    42  while(win_stack.size());
    43 }
    44 
    45 void view_back(int)
    46 {
    47  if(win_stack.size())
    48  {
    49  delete win_stack.back();
    50  win_stack.pop_back();
    51  }
    52  if(win_stack.size())
    53  win_stack.back()->repaint();
    54 }
    55 
    56 void finish(int)
    57 {
    58  cleanup();
    59  exit(EXIT_SUCCESS);
    60 }
    61 
    62 void cleanup()
    63 {
    64  if(t_refresh.joinable())
    65  t_refresh.join();
    66  clrtoeol();
    67  refresh();
    68  endwin();
    69 }
    70 
    71 void show_info(int)
    72 {
    73  ViewInfo* view = new ViewInfo();
    74  view->setTitle("Info");
    75  view->setText("Informationen zu Board 15 Famulus Edition\nEs war einmal...");
    76  view->setLabelClose("[ Zurueck ]");
    77  view->repaint();
    78 
    79  win_stack.push_back(view);
    80  input(0);
    81 }
    82 
    83 void show_monitor(int)
    84 {
    85  ViewMonitor* view = new ViewMonitor();
    86  view->setTitle("Monitor");
    87  view->setText("\nErfasse Messwerte...");
    88  view->setLabelClose("[ Zurueck ]");
    89  view->repaint();
    90 
    91  win_stack.push_back(view);
    92  input(0);
    93 }
    94 
    95 void show_invalid_port_input(int)
    96 {
    97  ViewInfo* view = new ViewInfo();
    98  view->setTitle("Falsche Eingabe");
    99  view->setText("Bitte geben Sie einen Wert aus dem Intervall [0, FF] an.");
    100  view->setLabelClose("[ Schliessen ]");
    101  view->repaint();
    102 
    103  win_stack.push_back(view);
    104  input(0);
    105 }
    106 
    107 void show_invalid_dac_input(int)
    108 {
    109  ViewInfo* view = new ViewInfo();
    110  view->setTitle("Falsche Eingabe");
    111  view->setText("Bitte geben Sie einen Wert aus dem Intervall [0, 1023] an.");
    112  view->setLabelClose("[ Schliessen ]");
    113  view->repaint();
    114 
    115  win_stack.push_back(view);
    116  input(0);
    117 }
    118 
    119 void write_digital_output0(int)
    120 {
    121  try
    122  {
    123  int d = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput(), 0, 16);
    124  if(d > 255 || 0 > d)
    125  throw std::invalid_argument("bad value");
    126  uint8_t port = static_cast<uint8_t>(d);
    127 
    128  B15F& drv = B15F::getInstance();
    129  drv.digitalWrite0(port);
    130  view_back(0);
    131  }
    132  catch(std::invalid_argument& ex)
    133  {
    134  show_invalid_port_input(0);
    135  }
    136 }
    137 
    138 void write_digital_output1(int)
    139 {
    140  try
    141  {
    142  int d = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput(), 0, 16);
    143  if(d > 255 || 0 > d)
    144  throw std::invalid_argument("bad value");
    145  uint8_t port = static_cast<uint8_t>(d);
    146 
    147  B15F& drv = B15F::getInstance();
    148  drv.digitalWrite1(port);
    149  view_back(0);
    150  }
    151  catch(std::invalid_argument& ex)
    152  {
    153  show_invalid_port_input(0);
    154  }
    155 }
    156 
    157 void write_analog_output0(int)
    158 {
    159  try
    160  {
    161  uint16_t port = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput());
    162  if(port > 1023)
    163  throw std::invalid_argument("bad value");
    164 
    165  B15F& drv = B15F::getInstance();
    166  drv.analogWrite0(port);
    167  view_back(0);
    168  }
    169  catch(std::invalid_argument& ex)
    170  {
    171  show_invalid_dac_input(0);
    172  }
    173 }
    174 
    175 void write_analog_output1(int)
    176 {
    177  try
    178  {
    179  uint16_t port = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput());
    180  if(port > 1023)
    181  throw std::invalid_argument("bad value");
    182 
    183  B15F& drv = B15F::getInstance();
    184  drv.analogWrite1(port);
    185  view_back(0);
    186  }
    187  catch(std::invalid_argument& ex)
    188  {
    189  show_invalid_dac_input(0);
    190  }
    191 }
    192 
    193 void show_digital_output0(int)
    194 {
    195  ViewPromt* view = new ViewPromt();
    196  view->setTitle("Digitale Ausgabe BE0");
    197  view->setMessage("\nAusgabe Port-Wert (hex): 0x");
    198  view->setCancel("[ Zurueck ]", true);
    199  view->setConfirm("[ OK ]", &write_digital_output0);
    200  view->repaint();
    201 
    202  win_stack.push_back(view);
    203  input(0);
    204 }
    205 
    206 void show_digital_output1(int)
    207 {
    208  ViewPromt* view = new ViewPromt();
    209  view->setTitle("Digitale Ausgabe BE1");
    210  view->setMessage("\nAusgabe Port-Wert (hex): 0x");
    211  view->setCancel("[ Zurueck ]", true);
    212  view->setConfirm("[ OK ]", &write_digital_output1);
    213  view->repaint();
    214 
    215  win_stack.push_back(view);
    216  input(0);
    217 }
    218 
    219 void show_analog_output0(int)
    220 {
    221  ViewPromt* view = new ViewPromt();
    222  view->setTitle("Analoge Ausgabe AA0");
    223  view->setMessage("\nAusgabe 10-Bit-Wert (0...1023): ");
    224  view->setCancel("[ Zurueck ]", true);
    225  view->setConfirm("[ OK ]", &write_analog_output0);
    226  view->repaint();
    227 
    228  win_stack.push_back(view);
    229  input(0);
    230 }
    231 
    232 void show_analog_output1(int)
    233 {
    234  ViewPromt* view = new ViewPromt();
    235  view->setTitle("Analoge Ausgabe AA1");
    236  view->setMessage("\nAusgabe 10-Bit-Wert (0...1023): ");
    237  view->setCancel("[ Zurueck ]", true);
    238  view->setConfirm("[ OK ]", &write_analog_output1);
    239  view->repaint();
    240 
    241  win_stack.push_back(view);
    242  input(0);
    243 }
    244 
    245 void start_selftest(int)
    246 {
    247  B15F& drv = B15F::getInstance();
    248  drv.activateSelfTestMode();
    249 
    250  ViewInfo* view = new ViewInfo();
    251  view->setTitle("Selbsttest aktiv");
    252  view->setText("Das B15 befindet sich jetzt im Selbsttestmodus.\n \nSelbsttest:\nZu Beginn geht der Reihe nach jede LED von BA0 bis BA1 an.\nDanach leuchten die LEDs an AA0 und AA1 kurz auf.\nZum Schluss spiegelt in einer Endlosschleife:\n* BA0 Port BE0\n* BA1 die DIP-Schalter S7\n* AA0 ADC0\n* AA1 ADC1");
    253  view->setLabelClose("[ Selbsttest Beenden ]");
    254  view->setCall(&stop_selftest);
    255  view->repaint();
    256 
    257  win_stack.push_back(view);
    258  input(0);
    259 }
    260 
    261 void stop_selftest(int)
    262 {
    263  B15F& drv = B15F::getInstance();
    264  drv.discard();
    266  drv.reconnect();
    267  drv.digitalWrite0(0);
    268  drv.digitalWrite1(0);
    269 }
    270 
    271 void show_selftest_info(int)
    272 {
    273  ViewInfo* view = new ViewInfo();
    274  view->setTitle("Selbsttest");
    275  view->setText("Bitte entfernen Sie jetzt alle Draehte von den Anschlussklemmen und bestaetigen\nmit Enter.");
    276  view->setLabelClose("[ Weiter ]");
    277  view->setCall(&start_selftest);
    278  view->repaint();
    279 
    280  win_stack.push_back(view);
    281  input(0);
    282 }
    -
    void delay_ms(uint16_t ms)
    Definition: b15f.cpp:292
    -
    static B15F & getInstance(void)
    Definition: b15f.cpp:302
    +
    void delay_ms(uint16_t ms)
    Definition: b15f.cpp:333
    +
    static B15F & getInstance(void)
    Definition: b15f.cpp:343
    Definition: b15f.h:26
    bool digitalWrite0(uint8_t)
    Definition: b15f.cpp:153
    bool activateSelfTestMode(void)
    Definition: b15f.cpp:145
    @@ -83,12 +83,12 @@ $(function() {
    bool digitalWrite1(uint8_t)
    Definition: b15f.cpp:163
    void discard(void)
    Definition: b15f.cpp:72
    -
    constexpr static uint16_t WDT_TIMEOUT
    Time in ms after which the watch dog timer resets the MCU.
    Definition: b15f.h:203
    +
    constexpr static uint16_t WDT_TIMEOUT
    Time in ms after which the watch dog timer resets the MCU.
    Definition: b15f.h:219
    void reconnect(void)
    Definition: b15f.cpp:57
    bool analogWrite0(uint16_t port)
    Definition: b15f.cpp:200
    diff --git a/docs/html/ui_8h_source.html b/docs/html/ui_8h_source.html index 6b5ee6b..545b5cb 100644 --- a/docs/html/ui_8h_source.html +++ b/docs/html/ui_8h_source.html @@ -73,7 +73,7 @@ $(function() {
    1 #ifndef UI_H
    2 #define UI_H
    3 
    4 #include <vector>
    5 #include "view_selection.h"
    6 #include "view_info.h"
    7 #include "view_monitor.h"
    8 #include "view_promt.h"
    9 
    10 void show_main(int);
    11 void input(int);
    12 void view_back(int);
    13 void finish(int);
    14 void cleanup();
    15 
    16 void show_info(int);
    17 void show_monitor(int);
    18 void show_invalid_port_input(int);
    19 void show_invalid_dac_input(int);
    20 void write_digital_output0(int);
    21 void write_digital_output1(int);
    22 void write_analog_output0(int);
    23 void write_analog_output1(int);
    24 void show_digital_output0(int);
    25 void show_digital_output1(int);
    26 void show_analog_output0(int);
    27 void show_analog_output1(int);
    28 
    29 // selftest group
    30 void show_selftest_info(int);
    31 void start_selftest(int);
    32 void stop_selftest(int);
    33 
    34 
    35 extern std::vector<View*> win_stack;
    36 extern std::thread t_refresh;
    37 
    38 #endif // UI_H
    diff --git a/docs/html/usart_8cpp_source.html b/docs/html/usart_8cpp_source.html index 5456252..70b7d23 100644 --- a/docs/html/usart_8cpp_source.html +++ b/docs/html/usart_8cpp_source.html @@ -70,26 +70,27 @@ $(function() {
    usart.cpp
    -
    1 #include "usart.h"
    2 
    3 void USART::openDevice(std::string device)
    4 {
    5  file_desc = open(device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY /* | O_NONBLOCK*/);
    6  if(file_desc <= 0)
    7  throw USARTException("Fehler beim Öffnen des Gerätes");
    8 
    9  struct termios options;
    10  int code = tcgetattr(file_desc, &options);
    11  if(code)
    12  throw USARTException("Fehler beim Lesen der Geräteparameter");
    13 
    14  options.c_cflag = CS8 | CLOCAL | CREAD;
    15  options.c_iflag = IGNPAR;
    16  options.c_oflag = 0;
    17  options.c_lflag = 0;
    18  options.c_cc[VMIN] = 0; // #bytes read returns at least
    19  options.c_cc[VTIME] = timeout;
    20  code = cfsetspeed(&options, baudrate);
    21  if(code)
    22  throw USARTException("Fehler beim Setzen der Baudrate");
    23 
    24  code = tcsetattr(file_desc, TCSANOW, &options);
    25  if(code)
    26  throw USARTException("Fehler beim Setzen der Geräteparameter");
    27 
    30 }
    31 
    33 {
    34  int code = close(file_desc);
    35  if(code)
    36  throw USARTException("Fehler beim Schließen des Gerätes");
    37 }
    38 
    40 {
    41  int code = tcflush(file_desc, TCIFLUSH);
    42  if(code)
    43  throw USARTException("Fehler beim Leeren des Eingangspuffers");
    44 }
    45 
    47 {
    48  int code = tcflush(file_desc, TCOFLUSH);
    49  if(code)
    50  throw USARTException("Fehler beim Leeren des Ausgangspuffers");
    51 }
    52 
    54 {
    55  int code = tcdrain(file_desc);
    56  if(code)
    57  throw USARTException("Fehler beim Versenden des Ausgangspuffers");
    58 }
    59 
    61 {
    62  double pz = 1e2 * n_blocks_failed / n_blocks_total;
    63  pz = std::round(pz * 1e2) / 1e2;
    64  std::cout << "blocks total: " << n_blocks_total << " ok: " << (n_blocks_total - n_blocks_failed) << " failed: " << n_blocks_failed << " (" << pz << "%)" << std::endl;
    65 }
    66 
    67 void USART::writeByte(uint8_t b)
    68 {
    69  int sent = write(file_desc, &b, 1);
    70  if(sent != 1)
    71  {
    72  std::cout << "WARNUNG: Fehler beim Senden (" << sent << "): writeByte(), wiederhole..." << std::endl;
    73  usleep(100000);
    74  sent = write(file_desc, &b, 1);
    75  if(sent != 1)
    76  throw USARTException("Fehler beim Senden: writeByte()");
    77  }
    78 
    79 }
    80 
    81 void USART::writeInt(uint16_t d)
    82 {
    83  int sent = write(file_desc, reinterpret_cast<char*>(&d), 2);
    84  if(sent != 2)
    85  throw USARTException("Fehler beim Senden: writeInt()");
    86 }
    87 
    88 
    89 
    90 int USART::read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout)
    91 {
    92  uint32_t elapsed = 0;
    93  int n_read = -1;
    94  auto start = std::chrono::steady_clock::now();
    95  auto end = start;
    96  while(elapsed < timeout)
    97  {
    98  n_read = read(file_desc, buffer + offset, len);
    99  if (n_read == len)
    100  return n_read;
    101 
    102  end = std::chrono::steady_clock::now();
    103  elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    104  }
    105 
    106  return 0;
    107 }
    108 
    109 int USART::write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout)
    110 {
    111  uint32_t elapsed = 0;
    112  int n_sent = -1;
    113  auto start = std::chrono::steady_clock::now();
    114  auto end = start;
    115  while(elapsed < timeout)
    116  {
    117  n_sent = write(file_desc, buffer + offset, len);
    119  if (n_sent == len)
    120  return n_sent;
    121 
    122  end = std::chrono::steady_clock::now();
    123  elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    124  }
    125 
    126  return n_sent;
    127 }
    128 
    129 void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
    130 {
    131  uint8_t crc;
    132  uint8_t aw;
    133  const uint16_t us_per_bit = (1000000 / baudrate) * 16;
    134  const uint16_t n_total = len + 3;
    135 
    136  n_blocks_total++;
    137  bool failed = false;
    138 
    139  do
    140  {
    141  // calc crc
    142  crc = 0;
    143  for(uint8_t i = 0; i < len; i++)
    144  {
    145  crc ^= buffer[i];
    146  for (uint8_t k = 0; k < 8; k++)
    147  {
    148  if (crc & 1)
    149  crc ^= CRC7_POLY;
    150  crc >>= 1;
    151  }
    152  }
    153 
    154  // construct block
    155  block_buffer[0] = len;
    156  std::memcpy(&block_buffer[1], buffer + offset, len);
    157  block_buffer[len + 1] = crc;
    158  block_buffer[len + 2] = BLOCK_END;
    159 
    160  // send block
    163  int n_sent = write_timeout(&block_buffer[0], 0, len + 3, us_per_bit * n_total);
    164  if(n_sent != n_total)
    165  throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
    166 
    167  /*for(uint8_t i = 0; i < len + 3; i++)
    168  {
    169  write_timeout(&block_buffer[i], 0, 1, us_per_bit * n_total);
    170  //tcdrain(file_desc);
    171  //usleep(1000);
    172  }*/
    173 
    174  // flush output data
    175  tcdrain(file_desc);
    176 
    177  //usleep(us_per_bit * n_total * 10);
    178 
    179  // check response
    180  int n_read = read_timeout(&aw, 0, 1, us_per_bit * n_blocks_total * 10);
    181  for(uint16_t i = 0; i < 255 && n_read != 1; i++)
    182  {
    183  writeByte(0x80); // Stoppzeichen für Block
    184  if(tcdrain(file_desc))
    185  {
    186  std::cout << "drain failed" << std::endl;
    187  }
    188  std::cout << "WARNING: read error (" << n_read << "), retry #" << (int) i << std::endl;
    189  usleep(us_per_bit*100);
    190  n_read = read_timeout(&aw, 0, 1, us_per_bit);
    191  }
    192 
    193  if(n_read != 1)
    194  throw std::runtime_error("fatal: " + std::to_string(n_read));
    195 
    196  //clearInputBuffer();
    197 
    198  if(aw != 0xFF)
    199  {
    200  if(!failed)
    201  n_blocks_failed++;
    202  failed = true;
    203  std::cout << "block failed, retry" << std::endl;
    204  }
    205  }
    206  while(aw != 0xFF);
    207 
    208  //std::cout << "OK" << std::endl;
    209 }
    210 
    211 uint8_t USART::readByte(void)
    212 {
    213  char b;
    214  auto start = std::chrono::steady_clock::now();
    215  auto end = start;
    216  uint16_t elapsed = 0;
    217  while(elapsed < timeout * 100)
    218  {
    219  int code = read(file_desc, &b, 1);
    220  if (code > 0)
    221  return static_cast<uint8_t>(b);
    222 
    223  end = std::chrono::steady_clock::now();
    224  elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    225  }
    226 
    227  throw TimeoutException("Verbindung unterbrochen.", timeout);
    228 }
    229 
    230 uint16_t USART::readInt(void)
    231 {
    232  return readByte() | readByte() << 8;
    233 }
    234 
    235 bool USART::readBlock(uint8_t* buffer, uint16_t offset)
    236 {
    237  uint8_t len = readByte();
    238  uint8_t crc = 0;
    239  buffer += offset;
    240 
    241  uint32_t block_timeout = timeout / 10;
    242 
    243  // wait for block
    244  int n_ready;
    245  uint16_t elapsed = 0;
    246  auto start = std::chrono::steady_clock::now();
    247  auto end = start;
    248  while(elapsed < block_timeout)
    249  {
    250  int code = ioctl(file_desc, FIONREAD, &n_ready);
    251  if(code != 0)
    252  {
    253  std::cout << "n_ready code: " << code << std::endl;
    254  return false;
    255  }
    256 
    257  if(n_ready >= len + 1)
    258  break;
    259 
    260  end = std::chrono::steady_clock::now();
    261  elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    262  }
    263  if(elapsed >= timeout)
    264  {
    265  std::cout << "block timeout: " << std::endl;
    266  return false;
    267  }
    268 
    269  while(len--)
    270  {
    271  *buffer = readByte();
    272 
    273  crc ^= *buffer++;
    274  for (uint8_t i = 0; i < 8; i++)
    275  {
    276  if (crc & 1)
    277  crc ^= CRC7_POLY;
    278  crc >>= 1;
    279  }
    280  }
    281 
    282  crc ^= readByte();
    283  for (uint8_t i = 0; i < 8; i++)
    284  {
    285  if (crc & 1)
    286  crc ^= CRC7_POLY;
    287  crc >>= 1;
    288  }
    289 
    290  if(TEST == 1)
    291  crc = 1;
    292  if(TEST > 100)
    293  TEST = 0;
    294 
    295  if (crc == 0)
    296  {
    297  writeByte(0xFF);
    298  return true;
    299  }
    300  else
    301  {
    302  writeByte(0xFE);
    303  return false;
    304  }
    305 }
    306 
    308 {
    309  return baudrate;
    310 }
    311 
    313 {
    314  return timeout;
    315 }
    316 
    317 void USART::setBaudrate(uint32_t baudrate)
    318 {
    319  this->baudrate = baudrate;
    320 }
    321 
    322 void USART::setTimeout(uint8_t timeout)
    323 {
    324  this->timeout = timeout;
    325 }
    -
    uint32_t getBaudrate(void)
    Definition: usart.cpp:307
    -
    uint8_t readByte(void)
    Definition: usart.cpp:211
    +
    1 #include "usart.h"
    2 
    3 void USART::openDevice(std::string device)
    4 {
    5  file_desc = open(device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY /* | O_NONBLOCK*/);
    6  if(file_desc <= 0)
    7  throw USARTException("Fehler beim Öffnen des Gerätes");
    8 
    9  struct termios options;
    10  int code = tcgetattr(file_desc, &options);
    11  if(code)
    12  throw USARTException("Fehler beim Lesen der Geräteparameter");
    13 
    14  options.c_cflag = CS8 | CLOCAL | CREAD;
    15  options.c_iflag = IGNPAR;
    16  options.c_oflag = 0;
    17  options.c_lflag = 0;
    18  options.c_cc[VMIN] = 0; // #bytes read returns at least
    19  options.c_cc[VTIME] = timeout;
    20  code = cfsetspeed(&options, baudrate);
    21  if(code)
    22  throw USARTException("Fehler beim Setzen der Baudrate");
    23 
    24  code = tcsetattr(file_desc, TCSANOW, &options);
    25  if(code)
    26  throw USARTException("Fehler beim Setzen der Geräteparameter");
    27 
    30 }
    31 
    33 {
    34  int code = close(file_desc);
    35  if(code)
    36  throw USARTException("Fehler beim Schließen des Gerätes");
    37 }
    38 
    40 {
    41  int code = tcflush(file_desc, TCIFLUSH);
    42  if(code)
    43  throw USARTException("Fehler beim Leeren des Eingangspuffers");
    44 }
    45 
    47 {
    48  int code = tcflush(file_desc, TCOFLUSH);
    49  if(code)
    50  throw USARTException("Fehler beim Leeren des Ausgangspuffers");
    51 }
    52 
    54 {
    55  int code = tcdrain(file_desc);
    56  if(code)
    57  throw USARTException("Fehler beim Versenden des Ausgangspuffers");
    58 }
    59 
    61 {
    62  double pz = 1e2 * n_blocks_failed / n_blocks_total;
    63  pz = std::round(pz * 1e2) / 1e2;
    64  std::cout << "blocks total: " << n_blocks_total << " ok: " << (n_blocks_total - n_blocks_failed) << " failed: " << n_blocks_failed << " (" << pz << "%)" << std::endl;
    65 }
    66 
    67 void USART::writeByte(uint8_t b)
    68 {
    69  int sent = write(file_desc, &b, 1);
    70  if(sent != 1)
    71  {
    72  std::cout << "WARNUNG: Fehler beim Senden (" << sent << "): writeByte(), wiederhole..." << std::endl;
    73  usleep(100000);
    74  sent = write(file_desc, &b, 1);
    75  if(sent != 1)
    76  throw USARTException("Fehler beim Senden: writeByte()");
    77  }
    78 
    79 }
    80 
    81 void USART::writeInt(uint16_t d)
    82 {
    83  int sent = write(file_desc, reinterpret_cast<char*>(&d), 2);
    84  if(sent != 2)
    85  throw USARTException("Fehler beim Senden: writeInt()");
    86 }
    87 
    88 void USART::writeU32(uint32_t w)
    89 {
    90  int sent = write(file_desc, reinterpret_cast<char*>(&w), 4);
    91  if(sent != 4)
    92  throw USARTException("Fehler beim Senden: writeU32()");
    93 }
    94 
    95 int USART::read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout)
    96 {
    97  uint32_t elapsed = 0;
    98  int n_read = -1;
    99  auto start = std::chrono::steady_clock::now();
    100  auto end = start;
    101  while(elapsed < timeout)
    102  {
    103  n_read = read(file_desc, buffer + offset, len);
    104  if (n_read == len)
    105  return n_read;
    106 
    107  end = std::chrono::steady_clock::now();
    108  elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    109  }
    110 
    111  return 0;
    112 }
    113 
    114 int USART::write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout)
    115 {
    116  uint32_t elapsed = 0;
    117  int n_sent = -1;
    118  auto start = std::chrono::steady_clock::now();
    119  auto end = start;
    120  while(elapsed < timeout)
    121  {
    122  n_sent = write(file_desc, buffer + offset, len);
    124  if (n_sent == len)
    125  return n_sent;
    126 
    127  end = std::chrono::steady_clock::now();
    128  elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    129  }
    130 
    131  return n_sent;
    132 }
    133 
    134 void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
    135 {
    136  uint8_t crc;
    137  uint8_t aw;
    138  const uint16_t us_per_bit = (1000000 / baudrate) * 16;
    139  const uint16_t n_total = len + 3;
    140 
    141  n_blocks_total++;
    142  bool failed = false;
    143 
    144  do
    145  {
    146  // calc crc
    147  crc = 0;
    148  for(uint8_t i = 0; i < len; i++)
    149  {
    150  crc ^= buffer[i];
    151  for (uint8_t k = 0; k < 8; k++)
    152  {
    153  if (crc & 1)
    154  crc ^= CRC7_POLY;
    155  crc >>= 1;
    156  }
    157  }
    158 
    159  // construct block
    160  block_buffer[0] = len;
    161  std::memcpy(&block_buffer[1], buffer + offset, len);
    162  block_buffer[len + 1] = crc;
    163  block_buffer[len + 2] = BLOCK_END;
    164 
    165  // send block
    168  int n_sent = write_timeout(&block_buffer[0], 0, len + 3, us_per_bit * n_total);
    169  if(n_sent != n_total)
    170  throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
    171 
    172  /*for(uint8_t i = 0; i < len + 3; i++)
    173  {
    174  write_timeout(&block_buffer[i], 0, 1, us_per_bit * n_total);
    175  //tcdrain(file_desc);
    176  //usleep(1000);
    177  }*/
    178 
    179  // flush output data
    180  tcdrain(file_desc);
    181 
    182  //usleep(us_per_bit * n_total * 10);
    183 
    184  // check response
    185  int n_read = read_timeout(&aw, 0, 1, us_per_bit * n_blocks_total * 10);
    186  for(uint16_t i = 0; i < 255 && n_read != 1; i++)
    187  {
    188  writeByte(0x80); // Stoppzeichen für Block
    189  if(tcdrain(file_desc))
    190  {
    191  std::cout << "drain failed" << std::endl;
    192  }
    193  std::cout << "WARNING: read error (" << n_read << "), retry #" << (int) i << std::endl;
    194  usleep(us_per_bit*100);
    195  n_read = read_timeout(&aw, 0, 1, us_per_bit);
    196  }
    197 
    198  if(n_read != 1)
    199  throw std::runtime_error("fatal: " + std::to_string(n_read));
    200 
    201  //clearInputBuffer();
    202 
    203  if(aw != 0xFF)
    204  {
    205  if(!failed)
    206  n_blocks_failed++;
    207  failed = true;
    208  std::cout << "block failed, retry" << std::endl;
    209  }
    210  }
    211  while(aw != 0xFF);
    212 
    213  //std::cout << "OK" << std::endl;
    214 }
    215 
    216 uint8_t USART::readByte(void)
    217 {
    218  char b;
    219  auto start = std::chrono::steady_clock::now();
    220  auto end = start;
    221  uint16_t elapsed = 0;
    222  while(elapsed < timeout * 100)
    223  {
    224  int code = read(file_desc, &b, 1);
    225  if (code > 0)
    226  return static_cast<uint8_t>(b);
    227 
    228  end = std::chrono::steady_clock::now();
    229  elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    230  }
    231 
    232  throw TimeoutException("Verbindung unterbrochen.", timeout);
    233 }
    234 
    235 uint16_t USART::readInt(void)
    236 {
    237  return readByte() | readByte() << 8;
    238 }
    239 
    240 bool USART::readBlock(uint8_t* buffer, uint16_t offset)
    241 {
    242  uint8_t len = readByte();
    243  uint8_t crc = 0;
    244  buffer += offset;
    245 
    246  uint32_t block_timeout = timeout / 10;
    247 
    248  // wait for block
    249  int n_ready;
    250  uint16_t elapsed = 0;
    251  auto start = std::chrono::steady_clock::now();
    252  auto end = start;
    253  while(elapsed < block_timeout)
    254  {
    255  int code = ioctl(file_desc, FIONREAD, &n_ready);
    256  if(code != 0)
    257  {
    258  std::cout << "n_ready code: " << code << std::endl;
    259  return false;
    260  }
    261 
    262  if(n_ready >= len + 1)
    263  break;
    264 
    265  end = std::chrono::steady_clock::now();
    266  elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    267  }
    268  if(elapsed >= timeout)
    269  {
    270  std::cout << "block timeout: " << std::endl;
    271  return false;
    272  }
    273 
    274  while(len--)
    275  {
    276  *buffer = readByte();
    277 
    278  crc ^= *buffer++;
    279  for (uint8_t i = 0; i < 8; i++)
    280  {
    281  if (crc & 1)
    282  crc ^= CRC7_POLY;
    283  crc >>= 1;
    284  }
    285  }
    286 
    287  crc ^= readByte();
    288  for (uint8_t i = 0; i < 8; i++)
    289  {
    290  if (crc & 1)
    291  crc ^= CRC7_POLY;
    292  crc >>= 1;
    293  }
    294 
    295  if(TEST == 1)
    296  crc = 1;
    297  if(TEST > 100)
    298  TEST = 0;
    299 
    300  if (crc == 0)
    301  {
    302  writeByte(0xFF);
    303  return true;
    304  }
    305  else
    306  {
    307  writeByte(0xFE);
    308  return false;
    309  }
    310 }
    311 
    313 {
    314  return baudrate;
    315 }
    316 
    318 {
    319  return timeout;
    320 }
    321 
    322 void USART::setBaudrate(uint32_t baudrate)
    323 {
    324  this->baudrate = baudrate;
    325 }
    326 
    327 void USART::setTimeout(uint8_t timeout)
    328 {
    329  this->timeout = timeout;
    330 }
    +
    void writeU32(uint32_t d)
    Definition: usart.cpp:88
    +
    uint32_t getBaudrate(void)
    Definition: usart.cpp:312
    +
    uint8_t readByte(void)
    Definition: usart.cpp:216
    void printStatistics(void)
    Definition: usart.cpp:60
    void closeDevice(void)
    Definition: usart.cpp:32
    void clearInputBuffer(void)
    Definition: usart.cpp:39
    -
    uint8_t getTimeout(void)
    Definition: usart.cpp:312
    +
    uint8_t getTimeout(void)
    Definition: usart.cpp:317
    void clearOutputBuffer(void)
    Definition: usart.cpp:46
    -
    void setBaudrate(uint32_t baudrate)
    Definition: usart.cpp:317
    +
    void setBaudrate(uint32_t baudrate)
    Definition: usart.cpp:322
    void writeByte(uint8_t b)
    Definition: usart.cpp:67
    void openDevice(std::string device)
    Definition: usart.cpp:3
    -
    uint16_t readInt(void)
    Definition: usart.cpp:230
    -
    void setTimeout(uint8_t timeout)
    Definition: usart.cpp:322
    +
    uint16_t readInt(void)
    Definition: usart.cpp:235
    +
    void setTimeout(uint8_t timeout)
    Definition: usart.cpp:327
    void flushOutputBuffer(void)
    Definition: usart.cpp:53
    void writeInt(uint16_t d)
    Definition: usart.cpp:81
    diff --git a/docs/html/usart_8h_source.html b/docs/html/usart_8h_source.html index 31b8729..25eac7a 100644 --- a/docs/html/usart_8h_source.html +++ b/docs/html/usart_8h_source.html @@ -70,25 +70,26 @@ $(function() {
    usart.h
    -
    1 #ifndef USART_H
    2 #define USART_H
    3 
    4 #include <iostream>
    5 #include <cstdint>
    6 #include <chrono>
    7 #include <unistd.h>
    8 #include <cstring>
    9 #include <fcntl.h>
    10 #include <sys/ioctl.h>
    11 #include <termios.h>
    12 #include <cmath>
    13 #include "usartexception.h"
    14 #include "timeoutexception.h"
    15 
    18 class USART
    19 {
    20 public:
    21 
    22  /*************************************************
    23  * Methoden für die Verwaltung der Schnittstelle *
    24  *************************************************/
    25 
    31  void openDevice(std::string device);
    32 
    37  void closeDevice(void);
    38 
    43  void clearInputBuffer(void);
    44 
    49  void clearOutputBuffer(void);
    50 
    55  void flushOutputBuffer(void);
    56 
    60  void printStatistics(void);
    61 
    62  /*************************************************/
    63 
    64 
    65 
    66  /*************************************
    67  * Methoden für die Datenübertragung *
    68  *************************************/
    69 
    75  void writeByte(uint8_t b);
    76 
    82  void writeInt(uint16_t d);
    83 
    88  uint8_t readByte(void);
    89 
    94  uint16_t readInt(void);
    95 
    96  int read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout);
    97  int write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout);
    98  void writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len);
    99  bool readBlock(uint8_t* buffer, uint16_t offset);
    100 
    101  /*************************************/
    102 
    103 
    104 
    105  /***************************************
    106  * Methoden für einstellbare Parameter *
    107  ***************************************/
    108 
    113  uint32_t getBaudrate(void);
    114 
    119  uint8_t getTimeout(void);
    120 
    125  void setBaudrate(uint32_t baudrate);
    126 
    131  void setTimeout(uint8_t timeout);
    132 
    133  /***************************************/
    134 
    135  constexpr static uint8_t CRC7_POLY = 0x91;
    136  constexpr static uint8_t MAX_BLOCK_SIZE = 64;
    137  constexpr static uint8_t BLOCK_END = 0x80;
    138 private:
    139 
    140  int file_desc = -1; // Linux Dateideskriptor
    141  uint32_t baudrate = 9600; // Standard-Baudrate, sollte mit setBaudrate() überschrieben werden!
    142  int TEST = 0;
    143  uint8_t timeout = 10; // in Dezisekunden
    144  uint8_t block_buffer[MAX_BLOCK_SIZE + 3];
    145 
    146  // debug statistics
    147  uint32_t n_blocks_total = 0;
    148  uint32_t n_blocks_failed = 0;
    149 };
    150 
    151 
    152 #endif // USART_H
    -
    uint32_t getBaudrate(void)
    Definition: usart.cpp:307
    -
    uint8_t readByte(void)
    Definition: usart.cpp:211
    +
    1 #ifndef USART_H
    2 #define USART_H
    3 
    4 #include <iostream>
    5 #include <cstdint>
    6 #include <chrono>
    7 #include <unistd.h>
    8 #include <cstring>
    9 #include <fcntl.h>
    10 #include <sys/ioctl.h>
    11 #include <termios.h>
    12 #include <cmath>
    13 #include "usartexception.h"
    14 #include "timeoutexception.h"
    15 
    18 class USART
    19 {
    20 public:
    21 
    22  /*************************************************
    23  * Methoden für die Verwaltung der Schnittstelle *
    24  *************************************************/
    25 
    31  void openDevice(std::string device);
    32 
    37  void closeDevice(void);
    38 
    43  void clearInputBuffer(void);
    44 
    49  void clearOutputBuffer(void);
    50 
    55  void flushOutputBuffer(void);
    56 
    60  void printStatistics(void);
    61 
    62  /*************************************************/
    63 
    64 
    65 
    66  /*************************************
    67  * Methoden für die Datenübertragung *
    68  *************************************/
    69 
    75  void writeByte(uint8_t b);
    76 
    82  void writeInt(uint16_t d);
    83 
    89  void writeU32(uint32_t d);
    90 
    95  uint8_t readByte(void);
    96 
    101  uint16_t readInt(void);
    102 
    103  int read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout);
    104  int write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout);
    105  void writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len);
    106  bool readBlock(uint8_t* buffer, uint16_t offset);
    107 
    108  /*************************************/
    109 
    110 
    111 
    112  /***************************************
    113  * Methoden für einstellbare Parameter *
    114  ***************************************/
    115 
    120  uint32_t getBaudrate(void);
    121 
    126  uint8_t getTimeout(void);
    127 
    132  void setBaudrate(uint32_t baudrate);
    133 
    138  void setTimeout(uint8_t timeout);
    139 
    140  /***************************************/
    141 
    142  constexpr static uint8_t CRC7_POLY = 0x91;
    143  constexpr static uint8_t MAX_BLOCK_SIZE = 64;
    144  constexpr static uint8_t BLOCK_END = 0x80;
    145 private:
    146 
    147  int file_desc = -1; // Linux Dateideskriptor
    148  uint32_t baudrate = 9600; // Standard-Baudrate, sollte mit setBaudrate() überschrieben werden!
    149  int TEST = 0;
    150  uint8_t timeout = 10; // in Dezisekunden
    151  uint8_t block_buffer[MAX_BLOCK_SIZE + 3];
    152 
    153  // debug statistics
    154  uint32_t n_blocks_total = 0;
    155  uint32_t n_blocks_failed = 0;
    156 };
    157 
    158 
    159 #endif // USART_H
    +
    void writeU32(uint32_t d)
    Definition: usart.cpp:88
    +
    uint32_t getBaudrate(void)
    Definition: usart.cpp:312
    +
    uint8_t readByte(void)
    Definition: usart.cpp:216
    void printStatistics(void)
    Definition: usart.cpp:60
    void closeDevice(void)
    Definition: usart.cpp:32
    void clearInputBuffer(void)
    Definition: usart.cpp:39
    -
    uint8_t getTimeout(void)
    Definition: usart.cpp:312
    +
    uint8_t getTimeout(void)
    Definition: usart.cpp:317
    Definition: usart.h:18
    void clearOutputBuffer(void)
    Definition: usart.cpp:46
    -
    void setBaudrate(uint32_t baudrate)
    Definition: usart.cpp:317
    +
    void setBaudrate(uint32_t baudrate)
    Definition: usart.cpp:322
    void writeByte(uint8_t b)
    Definition: usart.cpp:67
    void openDevice(std::string device)
    Definition: usart.cpp:3
    -
    uint16_t readInt(void)
    Definition: usart.cpp:230
    -
    void setTimeout(uint8_t timeout)
    Definition: usart.cpp:322
    +
    uint16_t readInt(void)
    Definition: usart.cpp:235
    +
    void setTimeout(uint8_t timeout)
    Definition: usart.cpp:327
    void flushOutputBuffer(void)
    Definition: usart.cpp:53
    void writeInt(uint16_t d)
    Definition: usart.cpp:81
    diff --git a/docs/html/usartexception_8h_source.html b/docs/html/usartexception_8h_source.html index c067c44..d037b79 100644 --- a/docs/html/usartexception_8h_source.html +++ b/docs/html/usartexception_8h_source.html @@ -74,7 +74,7 @@ $(function() { diff --git a/docs/html/view_8cpp_source.html b/docs/html/view_8cpp_source.html index 0de8167..030d2a5 100644 --- a/docs/html/view_8cpp_source.html +++ b/docs/html/view_8cpp_source.html @@ -71,10 +71,10 @@ $(function() {
    1 #include "view.h"
    2 
    3 WINDOW* View::win = nullptr;
    4 
    5 View::View()
    6 {
    7  if(!win)
    8  {
    9  B15F::abort("View::win not initialized, missing context");
    10  }
    11  getmaxyx(win, height, width); // init width and height
    12  keypad(win, TRUE);
    13 }
    14 
    15 View::~View()
    16 {
    17 }
    18 
    19 void View::setWinContext(WINDOW* win)
    20 {
    21  View::win = win;
    22 }
    23 
    24 WINDOW* View::getWinContext()
    25 {
    26  return win;
    27 }
    28 
    29 // from: https://stackoverflow.com/a/37454181
    30 std::vector<std::string> View::str_split(const std::string& str, const std::string delim)
    31 {
    32  std::vector<std::string> tokens;
    33  size_t prev = 0, pos = 0;
    34  do
    35  {
    36  pos = str.find(delim, prev);
    37  if (pos == std::string::npos) pos = str.length();
    38  std::string token = str.substr(prev, pos-prev);
    39  if (!token.empty()) tokens.push_back(token);
    40  prev = pos + delim.length();
    41  }
    42  while (pos < str.length() && prev < str.length());
    43  return tokens;
    44 }
    45 
    46 
    47 void View::setTitle(std::string title)
    48 {
    49  this->title = title;
    50 }
    51 
    52 void View::repaint()
    53 {
    54  // get screen size
    55  struct winsize size;
    56  if (ioctl(0, TIOCGWINSZ, (char *) &size) < 0)
    57  throw std::runtime_error("TIOCGWINSZ error");
    58 
    59 
    60  start_x = floor((size.ws_col - width) / 2.);
    61  start_y = floor((size.ws_row - height) / 2.);
    62 
    63  curs_set(0); // hide cursor
    64  mvwin(win, start_y, start_x);
    65  clear();
    66  wclear(win);
    67 
    68  // generic draw
    69  box(win, 0, 0);
    70  int offset_x = (width - title.length()) / 2;
    71  mvwprintw(win, 1, offset_x, "%s", title.c_str());
    72 
    73  // specific draw
    74  draw();
    75 
    76  refresh();
    77  wrefresh(win);
    78 }
    -
    static void abort(std::string msg)
    Definition: b15f.cpp:327
    +
    static void abort(std::string msg)
    Definition: b15f.cpp:368
    diff --git a/docs/html/view_8h_source.html b/docs/html/view_8h_source.html index 92e566e..8978a3e 100644 --- a/docs/html/view_8h_source.html +++ b/docs/html/view_8h_source.html @@ -74,7 +74,7 @@ $(function() {
    Definition: view.h:19
    diff --git a/docs/html/view__info_8cpp_source.html b/docs/html/view__info_8cpp_source.html index 2370daf..c9d1a98 100644 --- a/docs/html/view__info_8cpp_source.html +++ b/docs/html/view__info_8cpp_source.html @@ -73,7 +73,7 @@ $(function() {
    1 #include "view_info.h"
    2 
    3 ViewInfo::ViewInfo()
    4 {
    5  calls.push_back(nullptr);
    6 }
    7 
    8 void ViewInfo::setText(std::string text)
    9 {
    10  this->text = text;
    11 }
    12 
    13 void ViewInfo::setLabelClose(std::string label)
    14 {
    15  this->label_close = label;
    16 }
    17 
    18 void ViewInfo::setCall(call_t call)
    19 {
    20  calls[0] = call;
    21 }
    22 
    23 void ViewInfo::draw()
    24 {
    25  int li = 0;
    26  for(std::string line : str_split(text, "\n"))
    27  mvwprintw(win, text_offset_y + li++, text_offset_x, "%s", line.c_str());
    28 
    29  close_offset_x = (width - label_close.length()) / 2;
    30  close_offset_y = height - 2;
    31 
    32  wattron(win, A_REVERSE);
    33  mvwprintw(win, close_offset_y, close_offset_x, "%s", label_close.c_str());
    34  wattroff(win, A_REVERSE);
    35 }
    36 
    37 call_t ViewInfo::keypress(int& key)
    38 {
    39  switch(key)
    40  {
    41 
    42  case KEY_MOUSE:
    43  {
    44  // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
    45  MEVENT event;
    46  if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
    47  {
    48  size_t column = start_x + close_offset_x;
    49  size_t row = start_y + close_offset_y;
    50  size_t mouse_x = event.x, mouse_y = event.y;
    51  if(mouse_y == row && mouse_x >= column && mouse_x < column + label_close.length())
    52  key = -1; // do return from view
    53  }
    54  break;
    55  }
    56  case KEY_ENT:
    57  key = -1; // do return from view
    58  break;
    59  default:
    60  break;
    61  }
    62  return calls[0];
    63 }
    diff --git a/docs/html/view__info_8h_source.html b/docs/html/view__info_8h_source.html index 992f9d7..4aaf34e 100644 --- a/docs/html/view__info_8h_source.html +++ b/docs/html/view__info_8h_source.html @@ -75,7 +75,7 @@ $(function() {
    Definition: view.h:19
    diff --git a/docs/html/view__monitor_8cpp_source.html b/docs/html/view__monitor_8cpp_source.html index 5c600ac..fa3a723 100644 --- a/docs/html/view__monitor_8cpp_source.html +++ b/docs/html/view__monitor_8cpp_source.html @@ -74,17 +74,17 @@ $(function() {
    uint8_t digitalRead0(void)
    Definition: b15f.cpp:173
    uint8_t readDipSwitch(void)
    Definition: b15f.cpp:191
    -
    void delay_ms(uint16_t ms)
    Definition: b15f.cpp:292
    -
    static B15F & getInstance(void)
    Definition: b15f.cpp:302
    +
    void delay_ms(uint16_t ms)
    Definition: b15f.cpp:333
    +
    static B15F & getInstance(void)
    Definition: b15f.cpp:343
    Definition: b15f.h:26
    -
    static void abort(std::string msg)
    Definition: b15f.cpp:327
    +
    static void abort(std::string msg)
    Definition: b15f.cpp:368
    uint16_t analogRead(uint8_t channel)
    Definition: b15f.cpp:220
    uint8_t digitalRead1(void)
    Definition: b15f.cpp:182
    void reconnect(void)
    Definition: b15f.cpp:57
    diff --git a/docs/html/view__monitor_8h_source.html b/docs/html/view__monitor_8h_source.html index 0c48289..c050394 100644 --- a/docs/html/view__monitor_8h_source.html +++ b/docs/html/view__monitor_8h_source.html @@ -75,7 +75,7 @@ $(function() { diff --git a/docs/html/view__promt_8cpp_source.html b/docs/html/view__promt_8cpp_source.html index cef242a..0e91d6e 100644 --- a/docs/html/view__promt_8cpp_source.html +++ b/docs/html/view__promt_8cpp_source.html @@ -73,7 +73,7 @@ $(function() {
    1 #include "view_promt.h"
    2 
    3 void ViewPromt::draw()
    4 {
    5  curs_set(1); // show cursor
    6 
    7  int li = text_offset_y;
    8  int ci = 0;
    9  for(std::string line : str_split(message + input, "\n"))
    10  {
    11  mvwprintw(win, ++li, text_offset_x, "%s", line.c_str());
    12  ci = line.length() + text_offset_x;
    13  }
    14 
    15  button_offset_x = (width - label_cancel.length() - sep.length() - label_confirm.length()) / 2;
    16  button_offset_y = height - text_offset_y;
    17 
    18  if(selection == 0)
    19  {
    20  wattron(win, A_REVERSE);
    21  mvwprintw(win, button_offset_y, button_offset_x, "%s", label_cancel.c_str());
    22  wattroff(win, A_REVERSE);
    23  mvwprintw(win, button_offset_y, button_offset_x + label_cancel.length(), "%s", sep.c_str());
    24  mvwprintw(win, button_offset_y, button_offset_x + label_cancel.length() + sep.length(), "%s", label_confirm.c_str());
    25  }
    26  else
    27  {
    28  mvwprintw(win, button_offset_y, button_offset_x, "%s", label_cancel.c_str());
    29  mvwprintw(win, button_offset_y, button_offset_x + label_cancel.length(), "%s", sep.c_str());
    30  wattron(win, A_REVERSE);
    31  mvwprintw(win, button_offset_y, button_offset_x + label_cancel.length() + sep.length(), "%s", label_confirm.c_str());
    32  wattroff(win, A_REVERSE);
    33  }
    34  wmove(win, li, ci);
    35 }
    36 
    37 void ViewPromt::setMessage(std::string message)
    38 {
    39  this->message = message;
    40 }
    41 
    42 void ViewPromt::setConfirm(std::string name, std::function<void(int)> call)
    43 {
    44  label_confirm = name;
    45  call_confirm = call;
    46 }
    47 
    48 void ViewPromt::setCancel(std::string name, bool cancelable)
    49 {
    50  label_cancel = name;
    51  this->cancelable = cancelable;
    52 }
    53 
    54 std::string ViewPromt::getInput()
    55 {
    56  return input;
    57 }
    58 
    59 std::function<void(int)> ViewPromt::keypress(int& key)
    60 {
    61  std::function<void(int)> ret = nullptr;
    62  switch(key)
    63  {
    64  case KEY_BACKSPACE:
    65  if(input.length())
    66  input.pop_back();
    67  break;
    68  case '\t':
    69  case KEY_LEFT:
    70  case KEY_RIGHT:
    71  selection = (selection + 1 ) % 2;
    72  break;
    73  case KEY_MOUSE:
    74  {
    75  // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
    76  MEVENT event;
    77  bool hit = false;
    78  if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
    79  {
    80  size_t column_start = start_x + button_offset_x;
    81  size_t row_start = start_y + button_offset_y;
    82  size_t mouse_x = event.x, mouse_y = event.y;
    83  if(mouse_y == row_start)
    84  {
    85  if(cancelable && mouse_x >= column_start && mouse_x < column_start + label_cancel.length())
    86  {
    87  if(selection == 0 || event.bstate & BUTTON1_DOUBLE_CLICKED)
    88  hit = true;
    89  selection = 0;
    90  }
    91  if(mouse_x >= column_start + label_cancel.length() + sep.length() && mouse_x < column_start + label_cancel.length() + sep.length() + label_confirm.length())
    92  {
    93  if(selection == 1 || event.bstate & BUTTON1_DOUBLE_CLICKED)
    94  hit = true;
    95  selection = 1;
    96  }
    97  }
    98  }
    99  if(!hit)
    100  break;
    101 
    102  // fall through to next case
    103  [[fallthrough]];
    104  }
    105  case KEY_ENT:
    106  if(selection == 0) // exit
    107  key = -1; // do return from view
    108  else
    109  ret = call_confirm;
    110  break;
    111  default:
    112  break;
    113  }
    114 
    115  if(key >= ' ' && key <= '~')
    116  input += (char) key;
    117 
    118  if(key != KEY_ENT)
    119  repaint();
    120  return ret;
    121 }
    diff --git a/docs/html/view__promt_8h_source.html b/docs/html/view__promt_8h_source.html index df2d726..6acbb7b 100644 --- a/docs/html/view__promt_8h_source.html +++ b/docs/html/view__promt_8h_source.html @@ -75,7 +75,7 @@ $(function() { diff --git a/docs/html/view__selection_8cpp_source.html b/docs/html/view__selection_8cpp_source.html index bd16c53..d78a6f9 100644 --- a/docs/html/view__selection_8cpp_source.html +++ b/docs/html/view__selection_8cpp_source.html @@ -73,7 +73,7 @@ $(function() {
    1 #include "view_selection.h"
    2 
    3 void ViewSelection::draw()
    4 {
    5  //curs_set(0); // hide cursor
    6  for(size_t i = 0; i < choices.size(); i++)
    7  {
    8  if(selection == i)
    9  wattron(win, A_REVERSE);
    10  mvwprintw(win, i + choice_offset_y, choice_offset_x, "%s", choices[i].c_str());
    11  if(selection == i)
    12  wattroff(win, A_REVERSE);
    13  }
    14 }
    15 
    16 void ViewSelection::addChoice(std::string name, call_t call)
    17 {
    18  choices.push_back(name);
    19  calls.push_back(call);
    20 }
    21 
    22 call_t ViewSelection::keypress(int& key)
    23 {
    24  call_t ret = nullptr;
    25  switch(key)
    26  {
    27  case KEY_UP:
    28  do
    29  selection = (selection - 1 + choices.size()) % choices.size();
    30  while(!choices[selection].length() && choices.size());
    31  break;
    32 
    33  case '\t':
    34  case KEY_DOWN:
    35  do
    36  selection = (selection + 1) % choices.size();
    37  while(!choices[selection].length() && choices.size());
    38  break;
    39 
    40  case KEY_MOUSE:
    41  {
    42  // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
    43  MEVENT event;
    44  bool hit = false;
    45  if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
    46  {
    47  size_t column_start = start_x + choice_offset_x;
    48  size_t row_start = start_y + choice_offset_y;
    49  size_t mouse_x = event.x, mouse_y = event.y;
    50  for(size_t i = 0; i < choices.size(); i++)
    51  if(choices[i].length() && mouse_y == row_start + i && mouse_x >= column_start && mouse_x < column_start + choices[i].length())
    52  {
    53  if(selection == i || event.bstate & BUTTON1_DOUBLE_CLICKED)
    54  hit = true;
    55  selection = i;
    56  }
    57  }
    58  if(!hit)
    59  break;
    60 
    61  // fall through to next case
    62  [[fallthrough]];
    63  }
    64 
    65  case KEY_ENT:
    66  if(selection == choices.size() - 1) // exit
    67  key = -1; // do return from view
    68  else
    69  ret = calls[selection];
    70  break;
    71  default:
    72  break;
    73  }
    74  repaint();
    75  return ret;
    76 }
    diff --git a/docs/html/view__selection_8h_source.html b/docs/html/view__selection_8h_source.html index ece3b9c..19bac8c 100644 --- a/docs/html/view__selection_8h_source.html +++ b/docs/html/view__selection_8h_source.html @@ -75,7 +75,7 @@ $(function() { diff --git a/firmware/global_vars.cpp b/firmware/global_vars.cpp index 1359c7e..7297949 100644 --- a/firmware/global_vars.cpp +++ b/firmware/global_vars.cpp @@ -8,4 +8,5 @@ volatile TLC5615 dac0(spi, SPIADR::AA0); volatile TLC5615 dac1(spi, SPIADR::AA1); volatile ADU adu; volatile USART usart; +volatile PWM pwm; volatile bool nextRequest = false; diff --git a/firmware/global_vars.h b/firmware/global_vars.h index 749948c..218f50e 100644 --- a/firmware/global_vars.h +++ b/firmware/global_vars.h @@ -5,6 +5,7 @@ #include "tlc5615.h" #include "adu.h" #include "usart.h" +#include "pwm.h" #define WDT_TIMEOUT WDTO_15MS @@ -17,6 +18,7 @@ extern volatile TLC5615 dac0; extern volatile TLC5615 dac1; extern volatile ADU adu; extern volatile USART usart; +extern volatile PWM pwm; extern volatile bool nextRequest; #endif // GLOBAL_VARS_H diff --git a/firmware/main.cpp b/firmware/main.cpp index 59cd64b..96f004f 100644 --- a/firmware/main.cpp +++ b/firmware/main.cpp @@ -28,6 +28,8 @@ int main() usart.init(); usart.initRX(); + + pwm.init(); // Hauptschleife, Verarbeitung der Requests while(1) diff --git a/firmware/pwm.cpp b/firmware/pwm.cpp index 0716236..f747725 100644 --- a/firmware/pwm.cpp +++ b/firmware/pwm.cpp @@ -1,18 +1,32 @@ #include "pwm.h" +const uint16_t PWM::PRESCALERS[] = {0, 1, 8, 64, 256, 1024}; +const uint8_t PWM::PRESCALER_COUNT = sizeof(PRESCALERS) / sizeof(uint16_t); -void pwmSetFrequency(uint32_t freq) +void PWM::init() const volatile { + // fast pwm mode, top = OCR0A, non inverting mode TCCR0A = _BV(COM0B1) | _BV(WGM00) | _BV(WGM01); - DDRB |= _BV(PB4); + TCCR0B = _BV(WGM02); - uint16_t p_ideal = ceil(float(F_CPU) / (freq * 0x100)); - for(int8_t i = PWM_PRESCALER_COUNT - 1; i >= 0 && PWM_PRESCALERS[i] >= p_ideal; i--) - TCCR0B = _BV(WGM02) | i; // set prescaler - if(p_ideal) - OCR0A = (uint8_t) (float(F_CPU) / (freq * PWM_PRESCALERS[TCCR0B & 0x07])); + // output signal on PB4 + DDRB |= _BV(PB4); } -void pwmSetValue(uint8_t value) +void PWM::setFrequency(uint32_t freq) const volatile +{ + uint16_t p_ideal = ceil(float(F_CPU) / (freq * 0x100)); + for(int8_t i = PRESCALER_COUNT - 1; i >= 0 && PRESCALERS[i] >= p_ideal; i--) + TCCR0B = (TCCR0B & 0xF8) | i; // set prescaler + if(p_ideal) + OCR0A = (uint8_t) (float(F_CPU) / (freq * PRESCALERS[TCCR0B & 0x07])); +} + +void PWM::setValue(uint8_t value) const volatile { OCR0B = value; } + +uint8_t PWM::getTop() const volatile +{ + return OCR0A; +} diff --git a/firmware/pwm.h b/firmware/pwm.h index a42e11b..dcf0d63 100644 --- a/firmware/pwm.h +++ b/firmware/pwm.h @@ -4,10 +4,17 @@ #include #include -const uint16_t PWM_PRESCALERS[] = {0, 1, 8, 64, 256, 1024}; -const uint8_t PWM_PRESCALER_COUNT = sizeof(PWM_PRESCALERS) / sizeof(uint16_t); - -void pwmSetFrequency(uint32_t); -void pwmSetValue(uint8_t); +class PWM +{ +public: + void init(void) const volatile; + void setFrequency(uint32_t) const volatile; + void setValue(uint8_t) const volatile; + uint8_t getTop(void) const volatile; + +private: + const static uint16_t PRESCALERS[]; + const static uint8_t PRESCALER_COUNT; +}; #endif // PWM_H diff --git a/firmware/requests.cpp b/firmware/requests.cpp index 0d11d3f..55f5cf6 100644 --- a/firmware/requests.cpp +++ b/firmware/requests.cpp @@ -62,6 +62,14 @@ void handleRequest() case RQ_ADC_DAC_STROKE: rqAdcDacStroke(); break; + + case RQ_PWM_SET_FREQ: + rqPwmSetFreq(); + break; + + case RQ_PWM_SET_VALUE: + rqPwmSetValue(); + break; default: break; @@ -217,11 +225,21 @@ void rqAdcDacStroke() usart.flush(); } -void rqSetPwm() +void rqPwmSetFreq() +{ + usart.initTX(); + uint32_t freq = usart.readU32(); + pwm.setFrequency(freq); + + usart.writeByte(pwm.getTop()); + usart.flush(); +} + +void rqPwmSetValue() { usart.initTX(); uint16_t value = usart.readByte(); - OCR0A = value; + pwm.setValue(value); usart.writeByte(USART::MSG_OK); usart.flush(); diff --git a/firmware/requests.h b/firmware/requests.h index 6fd3012..418b003 100644 --- a/firmware/requests.h +++ b/firmware/requests.h @@ -21,7 +21,8 @@ constexpr static uint8_t RQ_AA0 = 10; constexpr static uint8_t RQ_AA1 = 11; constexpr static uint8_t RQ_ADC = 12; constexpr static uint8_t RQ_ADC_DAC_STROKE = 13; -constexpr static uint8_t RQ_SET_PWM = 14; +constexpr static uint8_t RQ_PWM_SET_FREQ = 14; +constexpr static uint8_t RQ_PWM_SET_VALUE = 15; uint8_t const rq_len[] = { /* RQ_DISC */ 1, @@ -38,7 +39,8 @@ uint8_t const rq_len[] = { /* RQ_AA1 */ 3, /* RQ_ADC */ 2, /* RQ_ADC_DAC_STROKE */ 9, - /* RQ_SET_PWM */ 2 + /* RQ_PWM_SET_FREQ */ 5, + /* RQ_PWM_SET_VALUE */ 2 }; /** @@ -61,6 +63,7 @@ void rqAnalogWrite0(void); void rqAnalogWrite1(void); void rqAnalogRead(void); void rqAdcDacStroke(void); -void rqSetPwm(void); +void rqPwmSetFreq(void); +void rqPwmSetValue(void); #endif // REQUESTS_H diff --git a/firmware/usart.cpp b/firmware/usart.cpp index d039fcf..8be2392 100644 --- a/firmware/usart.cpp +++ b/firmware/usart.cpp @@ -130,3 +130,10 @@ uint16_t USART::readInt() volatile v |= readByte() << 8; return v; } + +uint32_t USART::readU32() volatile +{ + uint32_t v = readInt(); + v |= ((uint32_t) readInt()) << 16; + return v; +} diff --git a/firmware/usart.h b/firmware/usart.h index 5505a1b..a6cd610 100644 --- a/firmware/usart.h +++ b/firmware/usart.h @@ -102,6 +102,12 @@ public: * \return gelesenes Integer */ uint16_t readInt(void) volatile; + + /** + * Liest ein uint32_t aus dem Eingangspuffer + * \return gelesenes uint32_t + */ + uint32_t readU32(void) volatile; /**********************/