Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions examples/MessageQ/NewThread/arduino_main.cpp
Original file line number Diff line number Diff line change
@@ -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 <Adafruit_AHTX0.h>
#include <rtthread.h>
#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);
}
39 changes: 39 additions & 0 deletions examples/MessageQ/NewThread/common.h
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions examples/MessageQ/NewThread/threadEntry.cpp
Original file line number Diff line number Diff line change
@@ -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 <rtthread.h>
#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);
}
}
}