diff --git a/examples/MessageQ/NewThread/arduino_main.cpp b/examples/MessageQ/NewThread/arduino_main.cpp new file mode 100644 index 0000000..86728c4 --- /dev/null +++ b/examples/MessageQ/NewThread/arduino_main.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021-2022, RTduino Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-14 Stanley Lwin first version + */ + +#include +#include +#include "common.h" + +rt_thread_t tid = RT_NULL; +Adafruit_AHTX0 aht; + +struct rt_messagequeue mq; +rt_uint8_t msg_pool[2048]; + +void setup() +{ + Serial.begin(); + Serial.println("Adafruit AHT10/AHT20 demo!"); + + if (! aht.begin()) + { + Serial.println("Could not find AHT? Check wiring"); + while (1) delay(10); + } + + Serial.println("AHT10 or AHT20 found"); + + rt_mq_init(&mq,"msgQ",&msg_pool[0],sizeof(struct data),sizeof(msg_pool),RT_IPC_FLAG_FIFO); + + tid = rt_thread_create("thread", thread_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + + if(tid != RT_NULL) + { + rt_thread_startup(tid); + } + else + { + rt_thread_delete(tid); + } +} + +void loop() +{ + sensors_event_t humidity, temp; + struct data Data; + + aht.getEvent(&humidity, &temp); + + Data.temp= temp.temperature; + Data.humidity = humidity.relative_humidity; + + rt_mq_send(&mq,&Data, sizeof(struct data)); + + delay(500); +} diff --git a/examples/MessageQ/NewThread/common.h b/examples/MessageQ/NewThread/common.h new file mode 100644 index 0000000..219c6e1 --- /dev/null +++ b/examples/MessageQ/NewThread/common.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021-2022, RTduino Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-14 Stanley Lwin first version + */ + +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/*New thread configuration*/ +#define THREAD_PRIORITY 21 +#define THREAD_STACK_SIZE 1024 +#define THREAD_TIMESLICE 5 + +void thread_entry(void *parameter); + +/*msg queue control block*/ +extern struct rt_messagequeue mq; +extern rt_uint8_t msg_pool[2048]; + +/*data*/ +struct data{ + float temp; + float humidity; +}; +typedef struct data Data; + +#ifdef __cplusplus +} +#endif +#endif diff --git a/examples/MessageQ/NewThread/threadEntry.cpp b/examples/MessageQ/NewThread/threadEntry.cpp new file mode 100644 index 0000000..f0340db --- /dev/null +++ b/examples/MessageQ/NewThread/threadEntry.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021-2022, RTduino Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-14 Stanley Lwin first version + */ + +#include +#include "common.h" + +/*Entry function for tid*/ +void thread_entry(void *parameter) +{ + struct data mainData; + while(1) + { + /* Receive messages from the message queue */ + if (rt_mq_recv(&mq, &mainData, sizeof(struct data), RT_WAITING_FOREVER) == RT_EOK) + { + rt_kprintf("Temperature: %f\n",mainData.temp); + rt_kprintf("Humidity: %f\n",mainData.humidity); + } + } +}