-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathqueue.c
More file actions
199 lines (180 loc) · 6.51 KB
/
queue.c
File metadata and controls
199 lines (180 loc) · 6.51 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "queue.h"
//==============================================================================
// 函数声明
//==============================================================================
void Queue_Init(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * buffer, unsigned int len);
void Queue_Clear(QUEUE_HandleTypeDef * hqueue);
unsigned int Queue_Count(QUEUE_HandleTypeDef * hqueue);
QUEUE_StatusTypeDef Queue_Push(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T data);
unsigned int Queue_Push_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len);
QUEUE_StatusTypeDef Queue_Pop(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata);
unsigned int Queue_Pop_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len);
QUEUE_StatusTypeDef Queue_Peek(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata);
unsigned int Queue_Peek_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len);
//==============================================================================
// 函数名称:Queue_Init
// 函数功能:初始化(创建)队列,每个队列必须先执行该函数才能使用。
// 函数参数:hqueue 队列变量指针
// 函数参数:buffer 队列缓存区地址
// 函数参数:len 队列缓存区长度
// 函数返回:void
//==============================================================================
void Queue_Init(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * buffer, unsigned int len)
{
hqueue->buffer = buffer;
hqueue->buffer_length = len;
Queue_Clear(hqueue);
}
//==============================================================================
// 函数名称:Queue_Clear
// 函数功能:清空队列
// 函数参数:hqueue 队列变量指针
// 函数返回:void
//==============================================================================
void Queue_Clear(QUEUE_HandleTypeDef * hqueue)
{
hqueue->head = 0;
hqueue->tail = 0;
}
//==============================================================================
// 函数名称:Queue_Count
// 函数功能:获取队列内数据的个数
// 函数参数:hqueue 队列变量指针
// 函数返回:队列内数据的个数
//==============================================================================
unsigned int Queue_Count(QUEUE_HandleTypeDef * hqueue)
{
if(hqueue->head <= hqueue->tail)
{
return (unsigned int)(hqueue->tail - hqueue->head);
}
else
{
return (unsigned int)(hqueue->buffer_length + hqueue->tail - hqueue->head);
}
}
//==============================================================================
// 函数名称:Queue_Push
// 函数功能:压入数据到队列中
// 函数参数:hqueue 队列变量指针
// 函数参数:data 待压入队列的数据
// 函数返回:队列状态
//==============================================================================
QUEUE_StatusTypeDef Queue_Push(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T data)
{
unsigned int tmp = (hqueue->tail + 1) % hqueue->buffer_length;
if(tmp == hqueue->head)
{
return QUEUE_OVERLOAD;
}
else
{
hqueue->buffer[hqueue->tail] = data;
hqueue->tail = tmp;
return QUEUE_OK;
}
}
//==============================================================================
// 函数名称:Queue_Push_Array
// 函数功能:压入一组数据到队列中
// 函数参数:hqueue 队列变量指针
// 函数参数:pdatas 待压入队列的数组地址
// 函数参数:len 待压入队列的数组长度
// 函数返回:成功压入队列数据的数量
//==============================================================================
unsigned int Queue_Push_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)
{
unsigned int i;
for(i=0; i<len; i++)
{
if(Queue_Push(hqueue,pdatas[i]) == QUEUE_OVERLOAD)
{
break;
}
}
return i;
}
//==============================================================================
// 函数名称:Queue_Pop
// 函数功能:从队列中弹出数据
// 函数参数:hqueue 队列变量指针
// 函数参数:pdata 待弹出队列的数据缓存地址
// 函数返回:队列状态
//==============================================================================
QUEUE_StatusTypeDef Queue_Pop(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata)
{
if(hqueue->head == hqueue->tail)
{
return QUEUE_VOID;
}
else
{
*pdata = hqueue->buffer[hqueue->head];
hqueue->head = (hqueue->head + 1) % hqueue->buffer_length;
return QUEUE_OK;
}
}
//==============================================================================
// 函数名称:Queue_Pop_Array
// 函数功能:从队列中弹出一组数据
// 函数参数:hqueue 队列变量指针
// 函数参数:pdatas 待弹出队列的数据缓存地址
// 函数参数:len 待弹出队列的数据的最大长度
// 函数返回:实际弹出数据的数量
//==============================================================================
unsigned int Queue_Pop_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)
{
unsigned int i;
for(i=0; i<len; i++)
{
if(Queue_Pop(hqueue, &pdatas[i]) == QUEUE_VOID)
{
break;
}
}
return i;
}
//==============================================================================
// 函数名称:Queue_Peek
// 函数功能:从队列头部返回数据(不删除队列中的数据)
// 函数参数:hqueue 队列变量指针
// 函数参数:pdata 待返回队列的数据缓存地址
// 函数返回:队列状态
//==============================================================================
QUEUE_StatusTypeDef Queue_Peek(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata)
{
if(hqueue->head == hqueue->tail)
{
return QUEUE_VOID;
}
else
{
*pdata = hqueue->buffer[hqueue->head];
return QUEUE_OK;
}
}
//==============================================================================
// 函数名称:Queue_Peek_Array
// 函数功能:从队列中返回一组数据(不删除队列中的数据)
// 函数参数:hqueue 队列变量指针
// 函数参数:pdatas 待返回队列的数据缓存地址
// 函数参数:len 待返回队列的数据的最大长度
// 函数返回:实际返回数据的数量
//==============================================================================
unsigned int Queue_Peek_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)
{
unsigned int i;
if(hqueue->head == hqueue->tail)
{
return 0;
}
if(Queue_Count(hqueue) < len)
{
len = Queue_Count(hqueue);
}
for(i=0; i<len; i++)
{
pdatas[i] = hqueue->buffer[(hqueue->head + i) % hqueue->buffer_length];
}
return len;
}