프로그래밍 초보 탈출

Libraries/C언어 Library

[String] String Library - ToStr

째즈토끼 2022. 6. 21. 17:17

Number Type 데이터를 간단히 문자열로 변환.
LCD 표시, LOG 기록, Debug Text, 시리얼 텍스트 전송 등에 사용

사용 방법은 ...

uart_sendtext("ADC=");
uart_sendtext(tostr_int16(g_adc_value));
uart_send(_CR);

fnd_display(tostr_int16F("%04u", count));

log_write("ADC = ", tostr_int16(g_adc_value), "\r\n");
/*
 * lib_tostr.c
 *
 *  Created on: 2020. 7. 30.
 *      Author: jazz1
 */

//=================================================================================================
//
//=================================================================================================

#include "lib_common.h"

#include <stdio.h>
#include <stdlib.h>

//=================================================================================================
//
//=================================================================================================

#define	BUFFER_SIZE		32
#define	BUFFER_COUNT		4	// 동시 사용 개수

static char g_tostr_buffer[BUFFER_COUNT][BUFFER_SIZE];
static uint8_t g_tostr_buffer_index = 0;

static char *tostr_malloc(void)
{
	// free는 필요 없음. 버퍼를 순차적으로 할당.
	if (g_tostr_buffer_index == BUFFER_COUNT) g_tostr_buffer_index = 0;
	return g_tostr_buffer[g_tostr_buffer_index++];
}
//=================================================================================================
//
//=================================================================================================
char *tostr_int16F(char *format, int16_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_int32F(char *format, int32_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_int64F(char *format, int64_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_uint16F(char *format, uint16_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_uint32F(char *format, uint32_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_uint64F(char *format, uint64_t value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}

char *tostr_doubleF(char *format, double value)
{
	char *buffer = tostr_malloc();
	sprintf(buffer, format, value);
	return buffer;
}
/*
 * lib_tostr.h
 *
 *  Created on: 2020. 7. 30.
 *      Author: jazz1
 */

#ifndef __LIB_TOSTR_H__
#define __LIB_TOSTR_H__
//=================================================================================================
//
//=================================================================================================

#include "lib_types.h"

//=================================================================================================
//
//=================================================================================================

// format은 간단하게. 내부 버퍼 크기가 작다.
char *tostr_int16F(char *format, int16_t value);
char *tostr_int32F(char *format, int32_t value);
char *tostr_int64F(char *format, int64_t value);
char *tostr_uint16F(char *format, uint16_t value);
char *tostr_uint32F(char *format, uint32_t value);
char *tostr_uint64F(char *format, uint64_t value);
char *tostr_doubleF(char *format, double value);

#define	tostr_int16(value)		tostr_int16F("%d", value)
#define	tostr_int32(value)		tostr_int32F("%ld", value)
#define	tostr_int64(value)		tostr_int64F("%lld", value)
#define	tostr_uint16(value)		tostr_uint16F("%u", value)
#define	tostr_uint32(value)		tostr_uint32F("%lu", value)
#define	tostr_uint64(value)		tostr_uint64F("%llu", value)
#define	tostr_bool_10(value)		((value) ? "1" : "0")
#define	tostr_bool_yn(value)		((value) ? "yes" : "no")
#define	tostr_bool_tf(value)		((value) ? "true" : "false")
#define	tostr_bool_on(value)		((value) ? "on" : "off")
#define	tostr_bool(value)		tostr_bool_10(value)
#define	tostr_double(value)		tostr_doubleF("%f", value)
#define	tostr_float(value)		tostr_double(value)

#define	tostr_hex8(value)		tostr_uint16F("%02X", value)
#define	tostr_hex16(value)		tostr_uint16F("%04X", value)
#define	tostr_hex32(value)		tostr_uint32F("%08lX", value)
#define	tostr_hex64(value)		tostr_uint64F("%08llX", value)

//=================================================================================================
//
//=================================================================================================
#endif /* __LIB_TOSTR_H__ */