循環(huán)
緩沖區(qū)是嵌入式軟件工程師在日常開發(fā)過程中的關(guān)鍵組件。多年來,互聯(lián)網(wǎng)上出現(xiàn)了許多不同的循環(huán)緩沖區(qū)實(shí)現(xiàn)和示例。我非常喜歡這個模塊,可以GitHub上找到這個開源的 CBUF.h 模塊。地址:
https://github.com/barraq/BRBrain/blob/master/firmware/CBUF.h
CBUF.h 模塊使用宏實(shí)現(xiàn)循環(huán)
緩沖區(qū),具體源碼如下所示;
#if?!defined(?CBUF_H?)
#define?CBUF_H???????/**
/*?----?Include?Files?----------------------------------------------------?*/
/*?----?Constants?and?Types?----------------------------------------------?*/
/**
*???Initializes?the?circular?buffer?for?use.
*/?
#define?CBUF_Init(?cbuf?)???????cbuf.m_getIdx?=?cbuf.m_putIdx?=?0
/**
*???Returns?the?number?of?elements?which?are?currently?contained?in?the?
?*??circular?buffer.
*/
#define?CBUF_Len(?cbuf?)????????((typeof(?cbuf.m_putIdx?))((?cbuf.m_putIdx?)?-?(?cbuf.m_getIdx?)))
/**
*???Appends?an?element?to?the?end?of?the?circular?buffer
*/
#define?CBUF_Push(?cbuf,?elem?)?(cbuf.m_entry)[?cbuf.m_putIdx ?