/*
* simple_print.c
*
* Created on: 19 Aug 2013
* Author: jammic
*
* This implements a small set of useful text output
* routines for use in small beacon code.
* redirects output to the serial port ...
*
*/
#include "config.h"
#include "simple_print.h"
#include "serial.h"
#include <string.h>
#if 0
/***********/
/* checksum handling used in final selftest frame of 'XML' printed by
the beacon */
static uint16_t sum = 0; /** the checksum value */
static char use_sum =0; /** if non-zero then checksum the values */
static uint8_t * use_ptTab = 0; /** pointer to a string buffer if used */
/**
* Initialise the checksum , and enable the checksumming of printed data
*
* @param None
* @return None
*
*/
void print_init_sum(void)
{
sum = 0;
use_sum = 1;
}
/**
* Stop checksumming the characters printed
*
* @param None
* @return None
*
*/
void print_stop_sum(void)
{
use_sum=0;
}
/**
* Print the checksum value as an unsigned decimal number
*
* @param None
* @return None
*
*/
void print_sum(void)
{
print_dec(sum);
}
/**
* Function to print a string to the serial port
*
* this computes the checksum of the data as it goes.
*
* @param[in] s A pointer to a null terminated array of characters
*
* @return None
*/
void print_str(char * s)
{
int i=0;
while (s&& *s)
{
if(use_sum)
{
checksumByte((uint8_t) *s, &sum);
}
if (use_ptTab==0)
{
PutCharTim(*s++);
}
else
{
use_ptTab[i++]=(*s++);
use_ptTab[i]='\0';
}
}
}
/**
* Function to print carriage return
*
* The side effect is that the values of the characters printed are included in the
* checksum if required
* @param None
* @return None
*/
void print_crlf(void)
{
print_str("\r\n");
}
/**
* Function to print comma
*
* The side effect is that the values of the characters printed are included in the
* checksum if required
*/
void print_comma(void)
{
print_str(",");
}
/**
* Function to print unsigned number
*
* Printed as a variable number of digits
* sufficient to display the value of the number
* @param[in] n Unsigned long number to print
* @return None
*/
void print_dec(unsigned long n)
{
char buff[11]; // 10 chars
buff[10]=0; // terminate
char *p=buff+9;
do // at least one loop to ensure a 0 is printed.
{
char d;
d=n%10;
n/=10;
*p-- = d+'0' ;
} while(n);
p++;
print_str(p);
}
/**
* Function to print unsigned number
*
* Number is printed as a fixed number of digits with
* leading zeros. Will only print width least significant digits eg.
* print_dec_width(123456,4) will print 3456.
* print_dec_width(23, 4) will print 0023
*
* @param[in] n The number to print
* @param[in] width The field width to use
* @return None
*/
void print_dec_width(unsigned long n, unsigned char width)
{
char buff[11]; // 10 chars
buff[10]=0; // terminate
char *p=buff+9;
do // at least one loop to ensure a 0 is printed.
{
char d;
d=n%10;
n/=10;
*p-- = d+'0' ;
} while(--width > 0);
p++;
print_str(p);
}
/**
* Function to print a signed number
*
* If the number is negative, print with a leading '-' sign
* as a variable number of digits
* @param[in] n long signed number
* @return None
*/
void print_signed(long n)
{
if(n<0)
{
print_str("-");
n=0-n;
}
print_dec(n);
}
/**
* Print hex to the buffer pointed at
*
* This is legacy code - may not be needed in current beacons
*
* @param[in] tabOut A pointer to a buffer to fill
* @param[in] n The value to print
* @param[in] fmt A format specifier
*
* @return None
**/
void format_hex(uint8_t * tabOut, unsigned long n, eHexFmt fmt)
{
use_ptTab=tabOut;
print_hex( n, fmt);
use_ptTab=0;
}
/**
* Function to print unsigned number
*
* This prints as a fixed number of hexadecimal digits with
* leading zeros.
* print_hex(1000,h4) will print 3FA.
* print_dec_width(23, h04) will print 0017
*
*
* @param[in] n The number to print
* @param[in] fmt A member of the eHexFmt enumeration
* @return None
*
*/
void print_hex(unsigned long n, eHexFmt fmt)
{
char buff[9]; // 8 spaces with a terminating \0
int i;
char c;
char *p=buff+7; // point at least significant char
if(fmt & 0x10 ) // space prefixed hex
{
c=' ';
fmt &=0x0F; // remove flag leaving number of chars
}
else
{
c='0';
}
for(i=0; i<8; i++)
{
buff[i]=c;
}
buff[8]=0;
// lay out the hex digits right justified in buffer, stop when done.
// the remainder of the buffer to the left is either spaces or zeros.
do // at least one loop to ensure a '0' is produced.
{
char d;
d=n & 0x0f;
n>>=4;
*p-- = d>9 ? d-10+'A' : d+'0' ;
} while(n);
p=buff + 8 - fmt; // index in the right number of chars from the start to output
print_str(p);
}
// end of SIMPLE_PRINT block
#endif