/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Term.C
**     Project   : DC18
**     Processor : MC56F8006_32_LQFP
**     Component : Term
**     Version   : Component 02.029, Driver 03.18, CPU db: 3.00.188
**     Compiler  : Metrowerks DSP C Compiler
**     Date/Time : 5/31/2010, 10:09 AM
**     Abstract  :
**
**     Settings  :
**
**     Contents  :
**         SendStr    - void Term_SendStr(uint8_t *str);
**         SendNum    - void Term_SendNum(int32_t number);
**         SendChar   - void Term_SendChar(uint8_t Val);
**         MoveTo     - void Term_MoveTo(uint8_t x, uint8_t y);
**         ReadChar   - void Term_ReadChar(uint8_t *c);
**         KeyPressed - bool Term_KeyPressed(void);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/


/* MODULE Term. */
#include "Term.h"
#include "Inhr6.h"


#define COLUMNS  80                    /* Number of columns */
#define ROWS     24                    /* Number of rows */


/* Internal method prototypes */
static void SendESCPrefix(void);
static void LongToStr(void *s, int32_t n);

/*
** ===================================================================
**     Method      :  Term_SendChar (component Term)
**
**     Description :
**         Send char to terminal.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Val             - Char to send
**     Returns     : Nothing
** ===================================================================
*/
void Term_SendChar(uint8_t Val)
{
  while (Inhr6_SendChar(Val) == ERR_TXFULL){} /* Send char */
}

/*
** ===================================================================
**     Method      :  Term_SendESCPrefix (component Term)
**
**     Description :
**         The method sends first (common) part of an excape sequence
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void SendESCPrefix(void)
{
  while (Inhr6_SendChar((uint8_t) 0x1B) == ERR_TXFULL){} /* Send the part of the escape sequence */
  while (Inhr6_SendChar((uint8_t) 0x5B) == ERR_TXFULL){} /* Send the part of the escape sequence */
}

/*
** ===================================================================
**     Method      :  Term_ReadChar (component Term)
**
**     Description :
**         Read char from terminal.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * c               - Pointer to a char variable
**     Returns     : Nothing
** ===================================================================
*/
void Term_ReadChar(uint8_t *c)
{
  while(Inhr6_RecvChar(c) != ERR_OK){} /* Receive char */
}

/*
** ===================================================================
**     Method      :  Term_KeyPressed (component Term)
**
**     Description :
**         Test if char was received from the terminal.
**     Parameters  : None
**     Returns     :
**         ---             - Return TRUE if user pressed some key
** ===================================================================
*/
bool Term_KeyPressed(void)
{
  return (bool)((Inhr6_GetCharsInRxBuf() == 0) ? FALSE : TRUE); /* Result number of chars in receive buffer */
}

/*
** ===================================================================
**     Method      :  Term_SendStr (component Term)
**
**     Description :
**         Send string to terminal.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * str             - Pointer to a string variable
**     Returns     : Nothing
** ===================================================================
*/
void Term_SendStr(void *str)
{
  uint8_t *ptr =  ((uint8_t *) str);
  while (*ptr != '\0') {
    while (Inhr6_SendChar(*ptr) == ERR_TXFULL){} /* Send char */
    ptr++;                             /* Increment the pointer */
  }
}

/*
** ===================================================================
**     Method      :  Term_LongToStr (component Term)
**
**     Description :
**         The method converts long int number to string.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void LongToStr(void *s, int32_t n)
{
  uint8_t *ptr =  ((uint8_t *) s);
  uint8_t i=0, j;
  uint8_t tmp;
  bool sign = (bool)(n < 0);

  if (sign){
    n *= -1;
  }
  if (n == 0){
    ptr[i++] = '0';
  }
  while (n > 0) {
    ptr[i++] = (uint8_t)((n % 10) + '0');
    n /= 10;
  }
  if (sign){
    ptr[i++] = '-';
  }
  for(j=0; j<(i/2); j++) {
    tmp = ptr[j];
    ptr[j] = ptr[(i-j)-1];
    ptr[(i-j)-1] = tmp;
  }
  ptr[i] = '\0';
}

/*
** ===================================================================
**     Method      :  Term_SendNum (component Term)
**
**     Description :
**         Send number to terminal.
**     Parameters  :
**         NAME            - DESCRIPTION
**         number          - Long number
**     Returns     : Nothing
** ===================================================================
*/
void Term_SendNum(int32_t number)
{
  uint8_t str[15];                     /* Temporary variable */
  uint8_t i=0;                         /* Temporary variable */

  LongToStr(str, number);              /* Conversion number to the string */
  while (str[i] != '\0') {
    while (Inhr6_SendChar((uint8_t)str[i]) == ERR_TXFULL){} /* Send char */
    i++;                               /* Increase the variable */
  }
}

/*
** ===================================================================
**     Method      :  Term_MoveTo (component Term)
**
**     Description :
**         Move cursor to position x, y. Range of coordinates can be
**         from 1 to the size, which depends on the used terminal.
**         Position [1,1] represents the upper left corner.
**     Parameters  :
**         NAME            - DESCRIPTION
**         x               - Column number
**         y               - Row number
**     Returns     : Nothing
** ===================================================================
*/
void Term_MoveTo(uint8_t x,uint8_t y)
{
  SendESCPrefix();                     /* Send the escape prefix */
  Term_SendNum((int32_t) y);           /* Send number of line */
  while (Inhr6_SendChar((uint8_t) ';') == ERR_TXFULL){} /* Send the part of the escape sequence */
  Term_SendNum((int32_t) x);           /* Send number of column */
  while (Inhr6_SendChar((uint8_t) 'H') == ERR_TXFULL){} /* Send the part of the escape sequence */
}


/* END Term. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.00 [04.35]
**     for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/

