#include #include #include #include "mtask.h" #include "itu.h" #include "debug.h" typedef void(*OUTDATA)( char a ) ; typedef void(*OUTSTR)( char* a ) ; static char* OutData( const OUTDATA OutTarget, const OUTSTR OutStrTarget, char* Format, char* St ) ; static int OutPrintf( const OUTDATA OutTarget, const OUTSTR OutStrTarget, char* Format, char* St ) ; static void BufferOut( char a ) ; static void BufferStrOut( char* a ) ; static void WaitSemaphoreForPrintf( void ) ; static void ClearSemaphoreForPrintf( void ) ; static void WaitSemaphoreForLcdPrintf( void ) ; static void ClearSemaphoreForLcdPrintf( void ) ; static char* BufferPtr ; char SemaphoreForPrintf ; char SemaphoreForLcdPrintf ; #define Keta 2 void tsprintf( char* Buffer, char* Format, ... ) { va_list ap ; char* St ; va_start(ap, Format ) ; St = ( char* )ap ; BufferPtr = Buffer ; OutPrintf( BufferOut, BufferStrOut, Format, St ) ; *BufferPtr = 0 ; } void tprintf( char* Format, ... ) { va_list ap ; char* St ; va_start(ap, Format ) ; St = ( char* )ap ; WaitSemaphoreForPrintf() ; OutPrintf( DbgOut, dbgprint, Format, St ) ; ClearSemaphoreForPrintf() ; } void lcdprintf( char* Format, ... ) { extern void LcdPutCh( char Charactor ) ; extern void printlcd( char *String ) ; va_list ap ; char* St ; va_start(ap, Format ) ; St = ( char* )ap ; WaitSemaphoreForLcdPrintf() ; OutPrintf( LcdPutCh, printlcd, Format, St ) ; ClearSemaphoreForLcdPrintf() ; } /****************************************************************************** 機能要約 printf の代わり 機能 引数 戻り値 注意 ******************************************************************************/ static int OutPrintf( const OUTDATA OutTarget, const OUTSTR OutStrTarget, char* Format, char* St ) { while( *Format != 0 ) { switch( *Format ) { case '%' : if( Format[ 1 ] == '%' ) { OutTarget( *Format ) ; Format += 2 ; } else { Format = OutData( OutTarget, OutStrTarget, Format, St ) ; St += sizeof( long ) ; } break ; case 0 : return( 0 ) ; case 10 : OutTarget( 13 ) ; OutTarget( 10 ) ; Format ++ ; break ; default : OutTarget( *Format ) ; Format ++ ; break ; } } return( 0 ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ static char* OutData( const OUTDATA OutTarget, const OUTSTR OutStrTarget, char* Format, char* St ) { int LongFlag ; long Param ; LongFlag = 0 ; while( 1 ) { ( Format ) ++ ; if( *Format == 0 ) { return( Format ) ; } switch( tolower( *Format ) ) { case 'd' : Param = *( ( long* )St ); if( LongFlag == 0 ) { Param &= 0xffff ; if( Param & 0x8000 ) { Param |= 0xffff0000 ; } } if( Param < 0 ) { OutTarget( '-' ) ; Param = ( - Param ) ; } OutStrTarget( ltoa( Param ) ) ; ( Format ) ++ ; return( Format ) ; case 'x' : Param = *( ( long* )St ); if( LongFlag == 0 ) { OutStrTarget( MakeShortHexStr( Param ) ) ; } else { OutStrTarget( MakeLongHexStr( Param ) ) ; } ( Format ) ++ ; return( Format ) ; case 's' : ( Format ) ++ ; if( *( ( char** )St ) == NULL ) { OutStrTarget( "(null)" ) ; return( Format ) ; } OutStrTarget( *( ( char** )St ) ) ; return( Format ) ; case 'c' : Param = *( ( long* )St ); OutTarget( Param ) ; ( Format ) ++ ; return( Format ) ; case 'l' : LongFlag = 1 ; break ; } } } /****************************************************************************** 機能要約 long を 10進文字列にする 機能 引数 a 文字列にしたいデータ 戻り値 文字列へのポインタ 注意 ******************************************************************************/ char* ltoa( unsigned long a ) { static char* OrgBuffer ; if( a == 0 ) { return( "0" ) ; } OrgBuffer = ItoA10( a ) ; for(;*OrgBuffer != 0 ; OrgBuffer ++ ) { if( *OrgBuffer >= '1' ) { return( OrgBuffer ) ; } } return( "" ) ; } /****************************************************************************** 機能要約 int を8桁の10進文字列にする 機能 引数 a 文字列にしたいデータ 戻り値 文字列へのポインタ 注意 ******************************************************************************/ char* ItoA10( unsigned long a ) { //65536*65536=4294967296 static const unsigned long pow[] = { 100000000u, 1000000u, 10000u, 100u, 1u, 0 } ; static char Str[ 12 ] ; char* Buffer ; unsigned long* Pow ; if( a < 100 ) { // 100 以下の場合には、処理が速くなるようにする memset( Str, '0', 8 ) ; memcpy( &Str[ 8 ], to10ind( a ), Keta + 1 ) ; return( Str ) ; } if( a < 10000 ) { // 10000 以下の場合には、処理が速くなるようにする memset( Str, '0', 6 ) ; memcpy( &Str[ 6 ], to10ind( a / 100 ), Keta ) ; memcpy( &Str[ 8 ], to10ind( a % 100 ), Keta + 1 ) ; return( Str ) ; } Buffer = Str ; Pow = ( unsigned long* )pow ; while( *Pow != 0 ) { memcpy( Buffer, to10ind( a / *Pow ) , Keta ) ; a %= ( *Pow ) ; Buffer += Keta ; Pow ++ ; } *Buffer = 0 ; return( Str ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ char* MakeByteHexStr( unsigned char hex ) { static char Buf[ 3 ] ; memcpy( Buf, toHexind( hex ), Keta + 1 ) ; return( Buf ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ char* MakeShortHexStr( unsigned short hex ) { static char Buf[ 5 ] ; memcpy( Buf, toHexind( hex >> 8 ), Keta ) ; memcpy( &Buf[ 2 ], toHexind( hex ), Keta + 1 ) ; return( Buf ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ char* MakeLongHexStr( unsigned long hex ) { static char Buf[ 9 ] ; memcpy( Buf , toHexind( hex >> 24 ), Keta ) ; memcpy( &Buf[ 2 ] , toHexind( hex >> 16 ), Keta ) ; memcpy( &Buf[ 4 ] , toHexind( hex >> 8 ), Keta ) ; memcpy( &Buf[ 6 ] , toHexind( hex ), Keta + 1 ) ; return( Buf ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ char* to10ind( unsigned int data ) { //処理を早くするためにテーブルにしている static const char* const str10[] = { "00","01","02","03","04","05","06","07","08","09" , "10","11","12","13","14","15","16","17","18","19" , "20","21","22","23","24","25","26","27","28","29" , "30","31","32","33","34","35","36","37","38","39" , "40","41","42","43","44","45","46","47","48","49" , "50","51","52","53","54","55","56","57","58","59" , "60","61","62","63","64","65","66","67","68","69" , "70","71","72","73","74","75","76","77","78","79" , "80","81","82","83","84","85","86","87","88","89" , "90","91","92","93","94","95","96","97","98","99" , NULL } ; if( data >= 100 ) { return( "??" ) ; } return( ( char* )( str10[ data ] ) ) ; } /****************************************************************************** 機能要約 機能 引数 戻り値 注意 ******************************************************************************/ char* toHexind( unsigned int data ) { //処理を早くするためにテーブルにしている static const char* const strHex[] = { "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F" , "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" , "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F" , "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F" , "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F" , "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F" , "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F" , "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F" , "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F" , "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F" , "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF" , "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF" , "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF" , "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF" , "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF" , "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF" , NULL } ; data &= 0xff ; return( ( char* )( strHex[ data ] ) ) ; } static void BufferOut( char a ) { *BufferPtr = a ; BufferPtr ++ ; } static void BufferStrOut( char* a ) { for( ; *a != 0 ; a ++ ) { BufferOut( *a ) ; } } static void WaitSemaphoreForPrintf( void ) { while( SemaphoreForPrintf ) { Sleep( 0 ) ; } SemaphoreForPrintf = 1 ; } static void ClearSemaphoreForPrintf( void ) { SemaphoreForPrintf = 0 ; } static void WaitSemaphoreForLcdPrintf( void ) { while( SemaphoreForLcdPrintf ) { Sleep( 0 ) ; } SemaphoreForLcdPrintf = 1 ; } static void ClearSemaphoreForLcdPrintf( void ) { SemaphoreForLcdPrintf = 0 ; } void InitPrintf( void ) { SemaphoreForPrintf = 0 ; SemaphoreForLcdPrintf = 0 ; }