//-----------------------------------------------------------------------------
//
// \brief   Some tool functions
//
//-----------------------------------------------------------------------------


#include <lepto/tools.h>      // myself
#include <string.h>           // strlen

/*
void delay( volatile uint32_t nCount)
{
  while(nCount--)
  {
  }
}
*/

const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


char *rotateString(char *buf)
{
   int end=strlen(buf)-1;
   int start=0;
   char temp;

   buf[end+1]=0;
   while(start<end)
   {
      temp=buf[end];
      buf[end]=buf[start];
      buf[start]=temp;
      end--;
      start++;
   }
   
   return(buf);
}


char *itoa_l(int value,  char *buf, int maxBuf, int base, int digitsCount /*=0*/, bool checkNegative /*=true*/ )
{
   int pos=0;
   bool negativ=false;
   int digitPos=0;
   unsigned int usValue=value;

   if( checkNegative && (base==10) )
      if(value<0)
      {
         negativ=true;
         usValue=value*-1;
      }

   do{
      buf[pos++]=digits[usValue%base];
      usValue/=base;
      digitPos++;
   }
   while(usValue);

   while(digitPos<digitsCount)
   {
      buf[pos++]=digits[0];
      digitPos++;
   }

   if(negativ)
      buf[pos++]='-';

   if(!pos)
      buf[pos++]=digits[0];

   buf[pos++]=0;

   rotateString(buf);

   return(buf);
};


char *litoa(long int value,  char *buf, int maxBuf, int base, int digitsCount /*=0*/, bool checkNegative /*=true*/ )
{
   int pos=0;
   bool negativ=false;
   unsigned long int usValue=value;

   if( checkNegative && (base==10) )
      if(value<0)
      {
         negativ=true;
         usValue=value*-1;
      }

   do{
      buf[pos++]=digits[usValue%base];
      usValue/=base;
   }
   while(usValue);

   if(negativ)
      buf[pos++]='-';

   if(!pos)
      buf[pos++]=digits[0];

   buf[pos++]=0;

   rotateString(buf);

   return(buf);
};


//---fin-----------------------------------------------------------------------
