<commit_before>/*

Copyright 2015-2021 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include "board/Board.h"
#include "board/common/USBMIDIOverSerial/USBMIDIOverSerial.h"
#include "Commands.h"
#include "core/src/general/Timing.h"

/// Time in milliseconds after which USB connection state should be checked
#define USB_CONN_CHECK_TIME 2000

namespace
{
    MIDI::USBMIDIpacket_t                  USBMIDIpacket;
    Board::USBMIDIOverSerial::packetType_t packetType;

    void checkUSBconnection()
    {
        static uint32_t lastCheckTime       = 0;
        static bool     lastConnectionState = false;

        if (core::timing::currentRunTimeMs() - lastCheckTime > USB_CONN_CHECK_TIME)
        {
            bool newState = Board::USB::isUSBconnected();

            if (lastConnectionState != newState)
            {
                USBMIDIpacket.Event = static_cast<uint8_t>(USBLink::internalCMD_t::usbState);
                USBMIDIpacket.Data1 = newState;
                Board::USBMIDIOverSerial::write(UART_CHANNEL_USB_LINK, USBMIDIpacket, Board::USBMIDIOverSerial::packetType_t::internal);

                lastConnectionState = newState;
            }
        }
    }
}    // namespace

int main(void)
{
    Board::init();

    while (1)
    {
        //USB -> UART
        if (Board::USB::readMIDI(USBMIDIpacket))
            Board::USBMIDIOverSerial::write(UART_CHANNEL_USB_LINK, USBMIDIpacket, Board::USBMIDIOverSerial::packetType_t::midi);

        //UART -> USB
        if (Board::USBMIDIOverSerial::read(UART_CHANNEL_USB_LINK, USBMIDIpacket, packetType))
        {
            if (packetType == Board::USBMIDIOverSerial::packetType_t::midi)
            {
                Board::USB::writeMIDI(USBMIDIpacket);
            }
            else
            {
                //internal command
                if (USBMIDIpacket.Event == static_cast<uint8_t>(USBLink::internalCMD_t::rebootBTLDR))
                {<commit_msg>usb link: fix usb connection being checked too often<commit_after>/*

Copyright 2015-2021 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include "board/Board.h"
#include "board/common/USBMIDIOverSerial/USBMIDIOverSerial.h"
#include "Commands.h"
#include "core/src/general/Timing.h"

/// Time in milliseconds after which USB connection state should be checked
#define USB_CONN_CHECK_TIME 2000

namespace
{
    MIDI::USBMIDIpacket_t                  USBMIDIpacket;
    Board::USBMIDIOverSerial::packetType_t packetType;

    void checkUSBconnection()
    {
        static uint32_t lastCheckTime       = 0;
        static bool     lastConnectionState = false;

        if (core::timing::currentRunTimeMs() - lastCheckTime > USB_CONN_CHECK_TIME)
        {
            bool newState = Board::USB::isUSBconnected();

            if (lastConnectionState != newState)
            {
                USBMIDIpacket.Event = static_cast<uint8_t>(USBLink::internalCMD_t::usbState);
                USBMIDIpacket.Data1 = newState;
                Board::USBMIDIOverSerial::write(UART_CHANNEL_USB_LINK, USBMIDIpacket, Board::USBMIDIOverSerial::packetType_t::internal);

                lastConnectionState = newState;
            }

            lastCheckTime = core::timing::currentRunTimeMs();
        }
    }
}    // namespace

int main(void)
{
    Board::init();

    while (1)
    {
        //USB -> UART
        if (Board::USB::readMIDI(USBMIDIpacket))
            Board::USBMIDIOverSerial::write(UART_CHANNEL_USB_LINK, USBMIDIpacket, Board::USBMIDIOverSerial::packetType_t::midi);

        //UART -> USB
        if (Board::USBMIDIOverSerial::read(UART_CHANNEL_USB_LINK, USBMIDIpacket, packetType))
        {
            if (packetType == Board::USBMIDIOverSerial::packetType_t::midi)
            {
                Board::USB::writeMIDI(USBMIDIpacket);
            }
            else
            {
                //internal command
                if (USBMIDIpacket.Event == static_cast<uint8_t>(USBLink::internalCMD_t::rebootBTLDR))
                {
{'fraction_non_alphanumeric': 0.09330914953647723, 'fraction_numerical': 0.009270455461507456, 'mean_word_length': 2.751322751322751, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1740306', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1441, 'n_words': 344}