59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <thread>
|
|
#include <csignal>
|
|
|
|
#include "joystick-reader.h"
|
|
#include "udp-driver.h"
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
// Парсим аргументы командной строки для частоты
|
|
int frequency = 5;
|
|
const char* sendAddress;
|
|
if (argc == 1) {
|
|
sendAddress = "127.0.0.1";
|
|
} else if (argc == 2) {
|
|
sendAddress = argv[1];
|
|
} else if (argc == 3) {
|
|
sendAddress = argv[1];
|
|
frequency = std::atoi(argv[2]);
|
|
} else {
|
|
std::cerr << "Usage: " << argv[0] << " send.ip.addr.v4 [Frequency]" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::cout << "Starting joystick reader with frequency: " << frequency << " Hz" << std::endl;
|
|
|
|
JoystickReader reader;
|
|
const int64_t timeInterval = 1000 / frequency;
|
|
while (!reader.initialize()) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
}
|
|
|
|
UDPSocket udp(sendAddress, 1066);
|
|
std::vector<uint16_t> data;
|
|
|
|
std::cout << "Sending data to " << sendAddress << ":1066" << std::endl;
|
|
|
|
int counter = 0;
|
|
while (reader.readData(data)) {
|
|
if (!udp.sendFrame(data)) {
|
|
std::cerr << "Failed to send UDP packet" << std::endl;
|
|
}
|
|
|
|
// Выводим прогресс каждые 100 пакетов
|
|
if (++counter % 100 == 0) {
|
|
std::cout << "Sent " << counter << " packets" << std::endl;
|
|
}
|
|
|
|
// вычислим время для сна
|
|
// должно быть время now+timeInterval
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(timeInterval));
|
|
}
|
|
|
|
std::cout << "Exiting..." << std::endl;
|
|
return 0;
|
|
}
|