//======================================================
// LED
//======================================================
// File Name : message.c
// Function  : Message Utility
//------------------------------------------------------
// Rev.01 2008.03.24 Takanaga Yamazaki
//------------------------------------------------------
// (C) Copyright 2008 Takanaga Yamazaki
//======================================================
// ---- License Information ----------------------------
// Anyone can FREELY use this code fully or partially
// under conditions shown below.
// 1. You should use this code under the GNU GPL.
// 2. You should remain this header text in your codes
//   including Copyright credit and License Information.
// 3. Your codes should inherit this license information.
//======================================================
// ---- Patent Notice ----------------------------------
// I have not cared whether this system (hw + sw) causes 
// infringement on the patent, copyright, trademark,
// or trade secret rights of others. You have all 
// responsibilities for determining if your designs
// and products infringe on the intellectual property
// rights of others, when you use technical information
// included in this system for your business.
//======================================================
// ---- Disclaimers ------------------------------------
// The function and reliability of this system are not 
// guaranteed. They may cause any damages to loss of
// properties, data, money, profits, life, or business.
// By adopting this sytem even partially, you assume
// all responsibility for its use.
//======================================================

#include "common.h"
#include <string.h>
#include <math.h>

#define CNT_DELAY  100000

//-----------------------------------
// Globals to Communicate USB Routine
//-----------------------------------
extern UI32 count_in;
extern UI32 count_out;
extern UI08 buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];

//-----------------
// Delay for USB
//-----------------
void delay(volatile UI32 i)
{
  while(i) i--; 
}

//--------------------------
// USB Put String
//--------------------------
void USB_Put_Str(UI08* str)
{
  #ifdef __DEBUG__
  
    delay(CNT_DELAY);
    count_in = strlen((char*) str);
    UserToPMABufferCopy((UI08 *)str, ENDP1_TXADDR, count_in);
    SetEPTxCount(ENDP1, count_in);
    SetEPTxValid(ENDP1);

  #endif
}

//--------------------------
// USB Put CRLF
//--------------------------
void USB_Put_CRLF(void)
{
  #ifdef __DEBUG__

    USB_Put_Str((UI08*) "\n\r");
    
  #endif    
}

//--------------------------
// USB Put Integer UI32
//--------------------------
void USB_Put_Int_UI32(UI32 num)
{
  #ifdef __DEBUG__
  
    UI08 buf[16];

    UI32toStr(num, buf, 10);
    USB_Put_Str(buf);
    
  #endif    
}

//--------------------------
// USB Put Integer SI32
//--------------------------
void USB_Put_Int_SI32(SI32 num)
{
  #ifdef __DEBUG__
  
    UI08 buf[16];

    SI32toStr(num, buf, 10);
    USB_Put_Str(buf);
    
  #endif    
}

//--------------------------
// USB Put FLOT
//--------------------------
void USB_Put_FLOT(FLOT fnum, UI32 digit_dec)
{
  #ifdef __DEBUG__
  
    UI08 buf[32];

    FLOTtoStr(fnum, buf, digit_dec);
    USB_Put_Str(buf);
    
  #endif    
}

//------------------------
// Convert UI32 to String 
//------------------------
UI08* UI32toStr(UI32 value, UI08 *string, UI32 radix)
{
    UI08 *dst;
    UI08 digits[32];
    UI32 i, n;

    dst = string;
    if ((radix < 2) || (radix > 36))
    {
        *dst = 0;
        return string;
    }
    i = 0;
    do
    {
        n = value % radix;
        digits[i++] = (n < 10 ? (char)n+'0' : (char)n-10+'a');
        value = value / radix;
    } while (value != 0);
    while (i > 0) *dst++ = digits[--i];
    *dst = 0;
    return dst;
}

//------------------------
// Convert SI32 to String 
//------------------------
UI08* SI32toStr(SI32 value, UI08 *string, UI32 radix)
{
    UI08 *dst;
    
    dst = string;

    if (value >= 0)
    {
        dst = UI32toStr((UI32) value, dst, radix);
    }
    else
    {
        *dst++ = '-';
        value = 0 - value;
        dst = UI32toStr((UI32) value, dst, radix);
    }
    return dst;
}

//------------------------
// Convert FLOT to String
//------------------------
UI08* FLOTtoStr(FLOT fvalue, UI08 *string, UI32 digit_dec)
{
    UI08 *dst;
    FLOT fvalue_int;
    FLOT fvalue_dec;
    SI32 ivalue_int;
    SI32 ivalue_dec;
    
    dst = string;
    
    fvalue_dec = _MODF_(fvalue, &fvalue_int);
    fvalue_dec = _FABS_(fvalue_dec) * _POW_(10.0, (FLOT) digit_dec);
    ivalue_int = (SI32) fvalue_int;
    ivalue_dec = (SI32) fvalue_dec;
    
    dst = SI32toStr(ivalue_int, dst, 10);
    *dst++ = '.';
    dst = SI32toStr(ivalue_dec, dst, 10);
    return dst;
}

//======================================================
// End of Program
//======================================================
