<commit_before>#include <libusb.h>
#include <memory>

#include "usb_worker.h"

constexpr unsigned int timeout = 200;

namespace roboime
{
    usb_worker::usb_worker(const config& conf) :
        conf(conf)
    {
        libusb_init(&context);
        // 5824/1500
        handle = libusb_open_device_with_vid_pid(context, 0x16c0, 0x05dc);
        std::cout << "Acquired transmitter USB handle." << std::endl;
    }

    usb_worker::~usb_worker()
    {
        libusb_close(handle);
        libusb_exit(context);
    }

    void
    usb_worker::async_process_action(const std::vector<std::shared_ptr<action>>& command)
    {
        unsigned char full_packet[46];
        memset(full_packet, 255, 47);
        full_packet[0] = 254;
        full_packet[1] = 0;
        full_packet[2] = 44;

        for (int i = 0, end_i = command.size(); i < end_i && i < 6; ++i)
        {
            auto raw_command = command[i].get();
            std::vector<char> buffer = raw_command->as_buffer();
            memcpy(&full_packet[0] + 3 + 7 * i, buffer.data(), 7);
        }
        full_packet[45] = 55;

        for (int i = 0; i < sizeof(full_packet) / sizeof(full_packet[0]); ++i)
            std::cout << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) full_packet[i] << " ";
        std::cout << std::endl;
        // 5696
        libusb_control_transfer(handle, 0x40, 3, 0, 0, full_packet, 46, timeout);
    }

    void
    usb_worker::send_raw_data(unsigned char* command, size_t size)
    {
        libusb_control_transfer(handle, 0x40, 3, 0, 0, command, size, timeout);
    }
}<commit_msg>Show when transmitter is not found.<commit_after>#include <libusb.h>
#include <memory>

#include "usb_worker.h"

constexpr unsigned int timeout = 200;

namespace roboime
{
    usb_worker::usb_worker(const config& conf) :
        conf(conf)
    {
        libusb_init(&context);
        // 5824/1500
        handle = libusb_open_device_with_vid_pid(context, 0x16c0, 0x05dc);
        if (handle) {
            std::cout << "Acquired transmitter USB handle." << std::endl;
        } else {
            std::cerr << "Could not find USB transmitter." << std::endl;
            throw std::runtime_error("Could not find USB transmitter.");
        }
    }

    usb_worker::~usb_worker()
    {
        libusb_close(handle);
        libusb_exit(context);
    }

    void
    usb_worker::async_process_action(const std::vector<std::shared_ptr<action>>& command)
    {
        unsigned char full_packet[46];
        memset(full_packet, 255, 47);
        full_packet[0] = 254;
        full_packet[1] = 0;
        full_packet[2] = 44;

        for (int i = 0, end_i = command.size(); i < end_i && i < 6; ++i)
        {
            auto raw_command = command[i].get();
            std::vector<char> buffer = raw_command->as_buffer();
            memcpy(&full_packet[0] + 3 + 7 * i, buffer.data(), 7);
        }
        full_packet[45] = 55;

        for (int i = 0; i < sizeof(full_packet) / sizeof(full_packet[0]); ++i)
            std::cout << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) full_packet[i] << " ";
        std::cout << std::endl;
        // 5696
        libusb_control_transfer(handle, 0x40, 3, 0, 0, full_packet, 46, timeout);
    }

    void
    usb_worker::send_raw_data(unsigned char* command, size_t size)
    {
        libusb_control_transfer(handle, 0x40, 3, 0, 0, command, size, timeout);
    }
}

// vim: et sw=4 ts=4 sts=4
{'fraction_non_alphanumeric': 0.1467438494934877, 'fraction_numerical': 0.03907380607814761, 'mean_word_length': 2.4909090909090907, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3844699', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1158, 'n_words': 252}