-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialDebug.c
126 lines (118 loc) · 3.56 KB
/
SerialDebug.c
1
2
3
4
5
6
7
8
9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
******************************************************************************
* @file SerialChart.c
* @author Hongxi Wong
* @version V1.0.1
* @date 2020/2/17
* @brief UWP Serial debug assistant lib based on HAL
******************************************************************************
* @attention
*
******************************************************************************
*/
#include "SerialDebug.h"
uint8_t DebugBuffer[100] = {0};
uint8_t position = 0;
uint8_t Debug_Count = 0;
uint8_t Debug_Period = 10;
void Serial_Debug(UART_HandleTypeDef *huart, uint16_t debug_period, float a, float b, float c, float d, float e, float f)
{
if (debug_period == 0)
return;
if (Debug_Count >= debug_period)
{
Debug_Count = 0;
Debug_Buf_Generate(huart, a, b, c, d, e, f, (uint8_t *)DebugBuffer);
}
Debug_Count++;
}
void Debug_Buf_Send(UART_HandleTypeDef *huart, uint8_t *data, uint8_t length)
{
HAL_UART_Transmit_DMA(huart, data, length);
}
static int _float_rounding(float raw)
{
static int integer;
static float decimal;
integer = (int)raw;
decimal = raw - integer;
if (decimal >= 0.5f)
integer++;
return integer;
}
void Debug_Buf_Generate(UART_HandleTypeDef *huart, float a, float b, float c, float d, float e, float f, uint8_t *data)
{
uint8_t position = 0;
float buffer[6] = {a, b, c, d, e, f};
for (uint8_t cnt = 0; cnt < 6; cnt++)
{
if (buffer[cnt] > -0.01f && buffer[cnt] < 0.01f)
{
data[position++] = cnt + 'a';
data[position++] = '=';
data[position++] = '0';
data[position++] = ',';
continue;
}
if (buffer[cnt] > 0.0f && buffer[cnt] < 0.1f)
{
data[position++] = cnt + 'a';
data[position++] = '=';
data[position++] = '0';
data[position++] = '.';
data[position++] = '0';
data[position++] = _float_rounding(buffer[cnt] * 100) % 10 + '0';
data[position++] = ',';
continue;
}
if (buffer[cnt] > -0.1f && buffer[cnt] < 0.0f)
{
data[position++] = cnt + 'a';
data[position++] = '=';
data[position++] = '-';
data[position++] = '0';
data[position++] = '.';
data[position++] = '0';
data[position++] = _float_rounding(-buffer[cnt] * 100) % 10 + '0';
data[position++] = ',';
continue;
}
float2char(buffer[cnt], data, cnt, &position);
}
data[position - 1] = '\n';
Debug_Buf_Send(huart, (uint8_t *)DebugBuffer, position);
}
void float2char(float floatdata, uint8_t *buffer, uint8_t n, uint8_t *position) //浮点型数,存储的字符数组,字符数组的长度
{
int32_t slope;
int32_t temp;
int8_t i, j;
slope = _float_rounding(floatdata * 100);
buffer[*position] = n + 'a';
*position += 1;
buffer[*position] = '=';
*position += 1;
if (slope < 0) //判断是否大于0
{
buffer[*position] = '-';
slope = -slope;
*position += 1;
}
temp = slope; //取整数部分
for (i = 0; temp != 0; i++) //计算整数部分的位数
temp /= 10;
temp = slope;
for (j = i; j >= 0; j--) //将整数部分转换成字符串型
{
buffer[*position + j] = temp % 10 + '0'; //从后往前
temp /= 10;
if ((i - j) == 1)
{
buffer[*position + j - 1] = '.'; //添加小数点
j -= 1;
}
}
*position += i + 1;
buffer[*position] = ',';
*position += 1;
}