{"id":9448,"date":"2022-02-08T16:57:06","date_gmt":"2022-02-08T08:57:06","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=9448"},"modified":"2022-02-08T16:57:06","modified_gmt":"2022-02-08T08:57:06","slug":"arduino-queue","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=9448","title":{"rendered":"Arduino - Queue"},"content":{"rendered":"<p>--<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/SMFSW\/cQueue\" target=\"_blank\" rel=\"noopener\">GitHub - SMFSW\/cQueue: Queue handling library (written in plain c)<\/a><\/li>\n<li><a href=\"https:\/\/groangao.pixnet.net\/blog\/post\/24474489\" target=\"_blank\" rel=\"noopener\">[C,C++] typedef struct \u7528\u6cd5\u8aaa\u660e<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u5b89\u88dd<\/h2>\n<p>\u7ba1\u7406\u7a0b\u5f0f\u5eab \u2192 \u641c\u5c0b\u00a0cqueue \u2192 \u5b89\u88dd<\/p>\n<p>--<\/p>\n<h2>SimpleQueue.ino<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/SMFSW\/cQueue\/blob\/master\/examples\/SimpleQueue\/SimpleQueue.ino\" target=\"_blank\" rel=\"noopener\">cQueue\/SimpleQueue.ino at master \u00b7 SMFSW\/cQueue \u00b7 GitHub<\/a><\/li>\n<\/ul>\n<pre class=\"lang:c decode:true\">#include &lt;cQueue.h&gt;\r\n\r\n#define\tIMPLEMENTATION\tLIFO\r\n\r\ntypedef struct strRec {\r\n\tuint16_t\tentry1;\r\n\tuint16_t\tentry2;\r\n} Rec;\r\n\r\nRec tab[6] = {\r\n\t{ 0x1234, 0x3456 },\r\n\t{ 0x5678, 0x7890 },\r\n\t{ 0x90AB, 0xABCD },\r\n\t{ 0xCDEF, 0xEFDC },\r\n\t{ 0xDCBA, 0xBA09 },\r\n\t{ 0x0987, 0x8765 }\r\n};\r\n\r\nQueue_t\t\tq;\t\/\/ Queue declaration\r\n\r\n\/\/ the setup function runs once when you press reset or power the board\r\nvoid setup() {\r\n\tSerial.begin(115200);\r\n\t\r\n\tq_init(&amp;q, sizeof(Rec), 10, IMPLEMENTATION, false);\r\n}\r\n\r\n\/\/ the loop function runs over and over again forever\r\nvoid loop() {\r\n\tunsigned int i;\r\n\t\r\n\tfor (i = 0 ; i &lt; sizeof(tab)\/sizeof(Rec) ; i++)\r\n\t{\r\n\t\tRec rec = tab[i];\r\n\t\tq_push(&amp;q, &amp;rec);\r\n\t}\r\n\t\r\n\tfor (i = 0 ; i &lt; sizeof(tab)\/sizeof(Rec) ; i++)\r\n\t{\r\n\t\tRec rec;\r\n\t\tq_pop(&amp;q, &amp;rec);\r\n\t\tSerial.print(rec.entry1, HEX);\r\n\t\tSerial.print(\" \");\r\n\t\tSerial.println(rec.entry2, HEX);\r\n\t}\r\n\t\r\n\twhile(1);\r\n}\r\n<\/pre>\n<p>--<\/p>\n<h2>\u5c0f\u6539\u61c9\u7528<\/h2>\n<ul>\n<li>typedef struct \u5ba3\u544a\u4f47\u5217\u7d50\u69cb<\/li>\n<li>\u4f7f\u7528\u524d\u8a18\u5f97 Rec rec \u5ba3\u544a<\/li>\n<li>q_pop \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a<\/li>\n<li>\u7d50\u69cb\u5143\u7d20\u53ef\u4ee5\u5c11\u7d66\u4e0d\u80fd\u591a\u7d66<\/li>\n<\/ul>\n<pre class=\"lang:c decode:true\">#include &lt;Arduino.h&gt;\r\n#include &lt;cQueue.h&gt;\r\n\r\ntypedef struct strRec {\r\n    uint8_t entry1;\r\n    uint8_t entry2;\r\n    uint8_t entry3;\r\n    uint8_t entry4;\r\n    uint8_t entry5;\r\n    uint8_t entry6;\r\n} Rec;\r\n\r\nQueue_t q;\r\n\r\nvoid setup() {\r\n    Serial1.begin(115200);\r\n    Serial1.setDebugOutput(true);\r\n    Serial1.println();\r\n\r\n    \/\/ LIFO \/ FIFO\r\n    q_init(&amp;q, sizeof(Rec), 5, FIFO, false);\r\n\r\n    Rec rec;\r\n    rec = {255,30,0,0};\r\n    q_push(&amp;q, &amp;rec);\r\n\r\n    rec = {0,5,0,2};\r\n    q_push(&amp;q, &amp;rec);\r\n}\r\n\r\nvoid loop() {\r\n    Rec rec;\r\n\r\n    if (q_pop(&amp;q, &amp;rec))\r\n    {\r\n        Serial1.print(rec.entry1, HEX);\r\n        Serial1.println();\r\n    }\r\n\r\n    if (q_pop(&amp;q, &amp;rec))\r\n    {\r\n        Serial1.print(rec.entry1, HEX);\r\n        Serial1.println();\r\n    }\r\n}<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"9448\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,083&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- GitHub - SMF...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"9448\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,083&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9448"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9448"}],"version-history":[{"count":5,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9448\/revisions"}],"predecessor-version":[{"id":9453,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9448\/revisions\/9453"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}