3 B15F *B15F::instance =
nullptr;
4 errorhandler_t B15F::errorhandler =
nullptr;
14 instance =
new B15F();
38 abort(
"Verbindung kann nicht repariert werden");
51 for (uint8_t i = 0; i < 16; i++)
53 usart.
transmit(&rq[0], 0,
sizeof(rq));
58 catch (std::exception &ex)
68 uint8_t dummy = rand() % 256;
76 assertRequestLength(rq, RQ_TEST);
77 usart.
transmit(&rq[0], 0,
sizeof(rq));
80 usart.
receive(&aw[0], 0,
sizeof(aw));
83 assertCode(aw[1], dummy);
89 uint16_t dummy = rand() % (0xFFFF / 3);
94 static_cast<uint8_t >(dummy & 0xFF),
95 static_cast<uint8_t >(dummy >> 8)
98 assertRequestLength(rq, RQ_INT_TEST);
99 usart.
transmit(&rq[0], 0,
sizeof(rq));
102 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
104 assertCode(aw, dummy * 3);
110 std::vector<std::string> info;
117 assertRequestLength(rq, RQ_INFO);
118 usart.
transmit(&rq[0], 0,
sizeof(rq));
121 usart.
receive(&n, 0,
sizeof(n));
125 usart.
receive(&len, 0,
sizeof(len));
128 usart.
receive(reinterpret_cast<uint8_t *>(&str[0]), 0, len);
131 info.push_back(std::string(str));
135 usart.
receive(&aw, 0,
sizeof(aw));
137 abort(
"Board Info fehlerhalft: code " + std::to_string((
int) aw));
144 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
149 std::this_thread::sleep_for(std::chrono::microseconds(us));
154 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
155 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
156 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
162 std::array<char, 128> buffer;
164 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(),
"r"), pclose);
167 throw std::runtime_error(
"popen() failed!");
169 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr)
171 result += buffer.data();
188 std::cout << ex.what() << std::endl;
213 assertRequestLength(rq, RQ_SELF_TEST);
214 usart.
transmit(&rq[0], 0,
sizeof(rq));
217 usart.
receive(&aw, 0,
sizeof(aw));
229 assertRequestLength(rq, RQ_DIGITAL_WRITE_0);
230 usart.
transmit(&rq[0], 0,
sizeof(rq));
233 usart.
receive(&aw, 0,
sizeof(aw));
245 assertRequestLength(rq, RQ_DIGITAL_WRITE_1);
246 usart.
transmit(&rq[0], 0,
sizeof(rq));
249 usart.
receive(&aw, 0,
sizeof(aw));
261 assertRequestLength(rq, RQ_DIGITAL_READ_0);
262 usart.
transmit(&rq[0], 0,
sizeof(rq));
265 usart.
receive(&aw, 0,
sizeof(aw));
277 assertRequestLength(rq, RQ_DIGITAL_READ_1);
278 usart.
transmit(&rq[0], 0,
sizeof(rq));
281 usart.
receive(&aw, 0,
sizeof(aw));
293 assertRequestLength(rq, RQ_READ_DIP_SWITCH);
294 usart.
transmit(&rq[0], 0,
sizeof(rq));
297 usart.
receive(&aw, 0,
sizeof(aw));
309 static_cast<uint8_t >(value & 0xFF),
310 static_cast<uint8_t >(value >> 8)
313 assertRequestLength(rq, RQ_ANALOG_WRITE_0);
314 usart.
transmit(&rq[0], 0,
sizeof(rq));
317 usart.
receive(&aw, 0,
sizeof(aw));
326 static_cast<uint8_t >(value & 0xFF),
327 static_cast<uint8_t >(value >> 8)
330 assertRequestLength(rq, RQ_ANALOG_WRITE_1);
331 usart.
transmit(&rq[0], 0,
sizeof(rq));
334 usart.
receive(&aw, 0,
sizeof(aw));
342 abort(
"Bad ADC channel: " + std::to_string(channel));
350 assertRequestLength(rq, RQ_ANALOG_READ);
351 usart.
transmit(&rq[0], 0,
sizeof(rq));
354 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
357 abort(
"Bad ADC data detected (1)");
361 void B15F::analogSequence(uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b,
362 uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
365 buffer_a += offset_a;
366 buffer_b += offset_b;
375 static_cast<uint8_t >(start & 0xFF),
376 static_cast<uint8_t >(start >> 8),
377 static_cast<uint8_t >(delta & 0xFF),
378 static_cast<uint8_t >(delta >> 8),
379 static_cast<uint8_t >(count & 0xFF),
380 static_cast<uint8_t >(count >> 8)
383 assertRequestLength(rq, RQ_ADC_DAC_STROKE);
384 usart.
transmit(&rq[0], 0,
sizeof(rq));
386 for (uint16_t i = 0; i < count; i++)
390 usart.
receive(reinterpret_cast<uint8_t *>(&buffer_a[i]), 0, 2);
392 if (buffer_a[i] > 1023)
393 abort(
"Bad ADC data detected (2)");
402 usart.
receive(reinterpret_cast<uint8_t *>(&buffer_b[i]), 0, 2);
404 if (buffer_b[i] > 1023)
405 abort(
"Bad ADC data detected (3)");
414 usart.
receive(&aw, 0,
sizeof(aw));
425 static_cast<uint8_t>((freq >> 0) & 0xFF),
426 static_cast<uint8_t>((freq >> 8) & 0xFF),
427 static_cast<uint8_t>((freq >> 16) & 0xFF),
428 static_cast<uint8_t>((freq >> 24) & 0xFF)
431 assertRequestLength(rq, RQ_PWM_SET_FREQ);
432 usart.
transmit(&rq[0], 0,
sizeof(rq));
435 usart.
receive(&aw, 0,
sizeof(aw));
449 assertRequestLength(rq, RQ_PWM_SET_VALUE);
450 usart.
transmit(&rq[0], 0,
sizeof(rq));
453 usart.
receive(&aw, 0,
sizeof(aw));
464 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
465 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8),
469 assertRequestLength(rq, RQ_SET_MEM_8);
470 usart.
transmit(&rq[0], 0,
sizeof(rq));
473 usart.
receive(&aw, 0,
sizeof(aw));
484 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
485 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8)
488 assertRequestLength(rq, RQ_GET_MEM_8);
489 usart.
transmit(&rq[0], 0,
sizeof(rq));
492 usart.
receive(&aw, 0,
sizeof(aw));
503 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
504 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8),
505 static_cast<uint8_t >(val & 0xFF),
506 static_cast<uint8_t >(val >> 8)
509 assertRequestLength(rq, RQ_SET_MEM_16);
510 usart.
transmit(&rq[0], 0,
sizeof(rq));
513 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
524 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
525 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8)
528 assertRequestLength(rq, RQ_GET_MEM_16);
529 usart.
transmit(&rq[0], 0,
sizeof(rq));
532 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
555 assertRequestLength(rq, RQ_COUNTER_OFFSET);
556 usart.
transmit(&rq[0], 0,
sizeof(rq));
559 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
560 return reinterpret_cast<uint16_t*>(aw);
572 assertRequestLength(rq, RQ_SERVO_ENABLE);
573 usart.
transmit(&rq[0], 0,
sizeof(rq));
576 usart.
receive(&aw, 0,
sizeof(aw));
589 assertRequestLength(rq, RQ_SERVO_DISABLE);
590 usart.
transmit(&rq[0], 0,
sizeof(rq));
593 usart.
receive(&aw, 0,
sizeof(aw));
600 throw DriverException(
"Impulslänge ist zu lang: " + std::to_string(pos));
607 static_cast<uint8_t >(pos & 0xFF),
608 static_cast<uint8_t >(pos >> 8)
611 assertRequestLength(rq, RQ_SERVO_SET_POS);
612 usart.
transmit(&rq[0], 0,
sizeof(rq));
615 usart.
receive(&aw, 0,
sizeof(aw));
637 std::string device =
exec(
"bash -c 'ls /dev/ttyAMA* 2> /dev/null'");
640 std::string device =
exec(
"bash -c 'ls /dev/ttyUSB* 2> /dev/null'");
643 while (device.find(
' ') != std::string::npos || device.find(
'\n') != std::string::npos ||
644 device.find(
'\t') != std::string::npos)
647 if (device.length() == 0)
648 abort(
"Adapter nicht gefunden");
650 std::cout <<
PRE <<
"Verwende Adapter: " << device << std::endl;
653 std::cout <<
PRE <<
"Stelle Verbindung mit Adapter her... " << std::flush;
656 std::cout <<
"OK" << std::endl;
659 std::cout <<
PRE <<
"Teste Verbindung... " << std::flush;
686 abort(
"Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
688 std::cout <<
"OK" << std::endl;
693 std::cout <<
PRE <<
"AVR Firmware Version: " << info[0] <<
" um " << info[1] <<
" Uhr (" << info[2] <<
")" 697 std::string& avr_commit_hash = info[3];
698 if(avr_commit_hash.compare(COMMIT_HASH))
700 std::cout <<
PRE <<
"Unterschiedliche commit hashes: " << std::endl;
701 std::cout << std::string(
PRE.length(),
' ') <<
"AVR: " << avr_commit_hash << std::endl;
702 std::cout << std::string(
PRE.length(),
' ') <<
"Control: " << COMMIT_HASH << std::endl << std::endl;
703 abort(
"Versionen inkompatibel. Bitte Software aktualisieren!");