/***************************************************************************

  BionxControl
  © 2025 -2026 christoph holzheuer
  christoph.holzheuer@gmail.com

  Using:

  mhs_can_drv.c
  © 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
  Klaus Demlehner, klaus@mhs-elektronik.de
  @see www.mhs-elektronik.de

  Based on Bionx data type descriptions from:

  BigXionFlasher USB V 0.2.4 rev. 97
  © 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
  @see www.bigxionflasher.org

  Bionx Bike Info
  © 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
  @see www.ts-soft.de

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.
  @see https://github.com/bikemike/bionx-bikeinfo

 ***************************************************************************/


#include <QElapsedTimer>
#include <QApplication>
#include <QChar>

#include <bc.h>
#include <chrono>
#include <thread>


//! erzeugt einen std::runtime_error mit text und optionalem parameter

BCException::BCException(const QString& what, const QString& param )
  : std::runtime_error( param.isEmpty() ? what.toStdString() : QString( "%1: %2" ).arg(what,param).toStdString( ) )
{

}

BCException::BCException( const QString& what, int errCode )
  : std::runtime_error(  QString( "%1: %2" ).arg(what,errCode).toStdString( ) )
{

}



namespace bc
{

  void delay_seconds( uint32_t tval )
  {
    std::this_thread::sleep_for(std::chrono::seconds( tval ) );
  }


  void delay_millis( uint32_t tval )
  {
    std::this_thread::sleep_for(std::chrono::milliseconds( tval ) );
  }


  void delay_micros( uint32_t tval )
  {
    std::this_thread::sleep_for(std::chrono::microseconds( tval ) );
  }

  /**
       * @brief Macht aus int x den String 000x zum schönaussehen.
       * @param count der Wert
       * @param len die voranzustellenden Nullen
       *
       * Macht aus int x den String 000x zum schönaussehen.
       *
       */

  QString formatInt( int count, int len )
  {
    QString result( "%0" );
    result = result.arg( count, len, 10, QChar( '0' ) );
    return result;
  }


  void processEventsFor(int milliseconds)
  {
    QElapsedTimer timer;
    timer.start();

    while (timer.elapsed() < milliseconds) {
      QApplication::processEvents(QEventLoop::AllEvents, 50);
    }
  }
} // namespace cbc
