<commit_before>#include "usbutil.h"

// TODO move this up to cantranslator.pde
USB_HANDLE USB_INPUT_HANDLE = 0;


void sendMessage(CanUsbDevice* usbDevice, uint8_t* message, int messageSize) {
#ifdef DEBUG
    Serial.print("sending message: ");
    Serial.println((char*)message);
#endif

    // Make sure the USB write is 100% complete before messing with this buffer
    // after we copy the message into it - the Microchip library doesn't copy
    // the data to its own internal buffer. See #171 for background on this
    // issue.
    int i = 0;
    while(usbDevice->device.HandleBusy(USB_INPUT_HANDLE)) {
        i++;
        if(i > 1) {
            // bail, USB probably isn't connected
            return;
        }
    }

    int nextByteIndex = 0;
    while(nextByteIndex < messageSize) {
        while(usbDevice->device.HandleBusy(USB_INPUT_HANDLE));
        int bytesToTransfer = min(USB_PACKET_SIZE,
                messageSize - nextByteIndex);
        uint8_t* currentByte = (uint8_t*)(usbDevice->sendBuffer
                + nextByteIndex);
        USB_INPUT_HANDLE = usbDevice->device.GenWrite(usbDevice->endpoint,
                currentByte, bytesToTransfer);
        // TODO we could import a newer version of SoftwareSerial and use its
        // write() function instead.
        usbDevice->serial.print(*currentByte);
        nextByteIndex += bytesToTransfer;
    }
}

void initializeUsb(CanUsbDevice* usbDevice) {
    Serial.print("Initializing USB...  ");
    usbDevice->device.InitializeSystem(true);
    while(usbDevice->device.GetDeviceState() < CONFIGURED_STATE);
    Serial.println("Done.");
}

USB_HANDLE armForRead(CanUsbDevice* usbDevice, char* buffer) {
    buffer[0] = 0;<commit_msg>Send over serial first, in case no USB is connected and we bail.<commit_after>#include "usbutil.h"

// TODO move this up to cantranslator.pde
USB_HANDLE USB_INPUT_HANDLE = 0;


void sendMessage(CanUsbDevice* usbDevice, uint8_t* message, int messageSize) {
#ifdef DEBUG
    Serial.print("sending message: ");
    Serial.println((char*)message);
#endif

    strncpy(usbDevice->sendBuffer, (char*)message, messageSize);
    usbDevice->sendBuffer[messageSize] = '\n';
    messageSize += 1;
    // TODO we could import a newer version of SoftwareSerial and use its
    // write() function instead.
    Serial.write((const uint8_t*)usbDevice->sendBuffer, messageSize);

    // Make sure the USB write is 100% complete before messing with this buffer
    // after we copy the message into it - the Microchip library doesn't copy
    // the data to its own internal buffer. See #171 for background on this
    // issue.
    int i = 0;
    while(usbDevice->device.HandleBusy(USB_INPUT_HANDLE)) {
        i++;
        if(i > 1) {
            // bail, USB probably isn't connected
            return;
        }
    }

    int nextByteIndex = 0;
    while(nextByteIndex < messageSize) {
        while(usbDevice->device.HandleBusy(USB_INPUT_HANDLE));
        int bytesToTransfer = min(USB_PACKET_SIZE,
                messageSize - nextByteIndex);
        uint8_t* currentByte = (uint8_t*)(usbDevice->sendBuffer
                + nextByteIndex);
        USB_INPUT_HANDLE = usbDevice->device.GenWrite(usbDevice->endpoint,
                currentByte, bytesToTransfer);
        nextByteIndex += bytesToTransfer;
    }
}

void initializeUsb(CanUsbDevice* usbDevice) {
    Serial.print("Initializing USB...  ");
    usbDevice->device.InitializeSystem(true);
    while(usbDevice->device.GetDeviceState() < CONFIGURED_STATE);
    Serial.println("Done.");
}

USB_HANDLE armForRead(CanUsbDevice* usbDevice, char* buffer) {
    buffer[0] = 0;
{'fraction_non_alphanumeric': 0.10634789777411377, 'fraction_numerical': 0.008793624622148942, 'mean_word_length': 2.969465648854962, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '5077044', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1051, 'n_words': 278}