--
--
安裝
管理程式庫 → 搜尋 cqueue → 安裝
--
SimpleQueue.ino
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 |
#include <cQueue.h> #define IMPLEMENTATION LIFO typedef struct strRec { uint16_t entry1; uint16_t entry2; } Rec; Rec tab[6] = { { 0x1234, 0x3456 }, { 0x5678, 0x7890 }, { 0x90AB, 0xABCD }, { 0xCDEF, 0xEFDC }, { 0xDCBA, 0xBA09 }, { 0x0987, 0x8765 } }; Queue_t q; // Queue declaration // the setup function runs once when you press reset or power the board void setup() { Serial.begin(115200); q_init(&q, sizeof(Rec), 10, IMPLEMENTATION, false); } // the loop function runs over and over again forever void loop() { unsigned int i; for (i = 0 ; i < sizeof(tab)/sizeof(Rec) ; i++) { Rec rec = tab[i]; q_push(&q, &rec); } for (i = 0 ; i < sizeof(tab)/sizeof(Rec) ; i++) { Rec rec; q_pop(&q, &rec); Serial.print(rec.entry1, HEX); Serial.print(" "); Serial.println(rec.entry2, HEX); } while(1); } |
--
小改應用
- typedef struct 宣告佇列結構
- 使用前記得 Rec rec 宣告
- q_pop 判斷佇列是否為空
- 結構元素可以少給不能多給
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 |
#include <Arduino.h> #include <cQueue.h> typedef struct strRec { uint8_t entry1; uint8_t entry2; uint8_t entry3; uint8_t entry4; uint8_t entry5; uint8_t entry6; } Rec; Queue_t q; void setup() { Serial1.begin(115200); Serial1.setDebugOutput(true); Serial1.println(); // LIFO / FIFO q_init(&q, sizeof(Rec), 5, FIFO, false); Rec rec; rec = {255,30,0,0}; q_push(&q, &rec); rec = {0,5,0,2}; q_push(&q, &rec); } void loop() { Rec rec; if (q_pop(&q, &rec)) { Serial1.print(rec.entry1, HEX); Serial1.println(); } if (q_pop(&q, &rec)) { Serial1.print(rec.entry1, HEX); Serial1.println(); } } |
--
686 total views, 1 views today