嵌入式单片机学习C语言

嵌入式软件开发 - C语言总结

2016-10-19  本文已影响112人  dVito

平时主要还是C语言用的比较多,对C语言做一个总结吧,最基本的就不写了,把一下觉得重要的点总结一下吧。

单片机相关

常用数据结构

这里说说常用的数据结构,主要还是静态循环队列和链表两种。

具体的这里封装了两个头文件(es_fifo.h, es_list.h),可以看一下。

es_fifo.h

#ifndef __ES_FIFO_H
#define __ES_FIFO_H

#define ES_FIFO(name) (name)

#define _ES_FIFO_SIZE(fifo) (sizeof(ES_FIFO(fifo).items) / sizeof(ES_FIFO(fifo).items[0]))

#define es_fifo_def(type, fifo, size) \
struct fifo##_st\
{ \
unsigned short front, back, count; \
type items[size]; \
}ES_FIFO(fifo)

#define es_fifo_in(fifo, item) \
do{ \
if(es_fifo_has_space(fifo)) \
{ \
ES_FIFO(fifo).items[ES_FIFO(fifo).back] = item; \
ES_FIFO(fifo).back = ES_FIFO(fifo).back + 1; \
ES_FIFO(fifo).back = ES_FIFO(fifo).back == _ES_FIFO_SIZE(fifo) ? 0 : ES_FIFO(fifo).back; \
ES_FIFO(fifo).count++; \
} \
}while(0)

#define es_fifo_has_space(fifo) (ES_FIFO(fifo).count < _ES_FIFO_SIZE(fifo))

#define es_fifo_is_empty(fifo) (ES_FIFO(fifo).count == 0)

#define es_fifo_count(fifo) ES_FIFO(fifo).count

#define es_fifo_peek(fifo) ES_FIFO(fifo).items[ES_FIFO(fifo).front]

#define es_fifo_out(fifo) \
do{ \
if(!es_fifo_is_empty(fifo)) \
{ \
    ES_FIFO(fifo).front = (ES_FIFO(fifo).front + 1); \
    ES_FIFO(fifo).front = ES_FIFO(fifo).front == _ES_FIFO_SIZE(fifo) ? 0 : ES_FIFO(fifo).front; \
    ES_FIFO(fifo).count--; \
} \
}while(0)


#endif // __ES_FIFO_H

es_list.h

#ifndef __ES_LIST_H
#define __ES_LIST_H

#define ES_LIST_ENTRY(type) \
type *next;type *prev

#define es_list_first(list) ((list) ? (list) : NULL)

#define es_list_last(list) ((list) ? (list)->prev : NULL)

#define es_list_add(list, node) \
if(!list) { \
    list = node; \
    list->next = list; \
    list->prev = list; \
} \
else { \
    node->next = list; \
    list->prev->next = node; \
    node->prev = list->prev; \
    list->prev = node; \
}

#define es_list_del(list, node) \
{ \
    list = node == list ? (node->next == list ? NULL : node->next) : list; \
    (node)->prev->next = (node)->next; \
    (node)->next->prev = (node)->prev; \
} 

#define ___ESLISTV(node, line) node##line
#define __ESLISTV(node, line) ___ESLISTV(node, line)
#define _ESLISTV(node) __ESLISTV(node, __LINE__)

#define es_list_foreach(list, node) \
node = list; \
void *_ESLISTV(_next) = node ? node->next : NULL; \
void *_ESLISTV(_flag) = NULL; \
void *_ESLISTV(_list) = list; \
for(; list && (node != (list) \
    || _ESLISTV(_flag) == NULL); \
        node = _ESLISTV(_next), \
        _ESLISTV(_next) = node->next, \
        _ESLISTV(_flag) = _ESLISTV(_list) != list ? NULL : list, _ESLISTV(_list) = list)

#endif // __ES_LIST_H

上一篇下一篇

猜你喜欢

热点阅读