请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:CC1312R 嗨、大家好:
我正在使用 CC1312 15.4中的最新 SDK
我使用队列。 h 并使用其例程代码、如图所示:
这个示例代码不是问题、但是我的要求是在启动期间将一批数据存储在这个队列中。 因此、我已经对代码进行了以下修改:
但此时、代码将继续循环、而输出值将相同。 应如何解决此问题?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
嗨、大家好:
我正在使用 CC1312 15.4中的最新 SDK
我使用队列。 h 并使用其例程代码、如图所示:
这个示例代码不是问题、但是我的要求是在启动期间将一批数据存储在这个队列中。 因此、我已经对代码进行了以下修改:
但此时、代码将继续循环、而输出值将相同。 应如何解决此问题?
尊敬的 Faker:
如果你查看 Queue.h、就会发现 Queue_enqueue 中没有发生存储器分配。 这意味着您必须自行分配和取消分配每个元素。
在您发布的代码中、每个元素都是根据编译器的工作方式引用其自身的。
以下是工作代码:
/* * Copyright (c) 2015-2019, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * *Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * *Neither the name of Texas Instruments Incorporated nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* ======== empty.c ======== */ /* For usleep() */ #include <unistd.h> #include <stdint.h> #include <stdlib.h> #include <stddef.h> /* Driver Header files */ #include <ti/drivers/GPIO.h> // #include <ti/drivers/I2C.h> // #include <ti/drivers/SPI.h> // #include <ti/drivers/Watchdog.h> // Import Display Driver definitions #include <ti/display/Display.h> /* Driver configuration */ #include "ti_drivers_config.h" #include <ti/sysbios/knl/Queue.h> typedef struct Rec { Queue_Elem _elem; int data; } Rec; int mainThread(void) { Queue_Handle q; Rec r1, r2; Rec* rp; // Initialize optional Display parameters Display_Handle handle; Display_Params params; Display_Params_init(¶ms); // Open Display implementation handle = Display_open(Display_Type_UART, ¶ms); if (handle == NULL) { // Display_open() failed while(1); } // create a Queue instance 'q' q = Queue_create(NULL, NULL); for (int i = 0; i < 3; i++) { Rec *r; r = malloc(sizeof(Rec)); r->data = 300 * i; Queue_enqueue(q, &r->_elem); } // deQ the records and print their data values until Q is empty while ((Queue_Handle)(rp = Queue_dequeue(q)) != q) { Display_printf(handle, 0, 0, "%d\n", rp->data); free(rp); } return (0); }
此致、
亚瑟