diff --git a/source/shoh/.cproject b/source/shoh/.cproject
index ff20bcd..b0b54ab 100644
--- a/source/shoh/.cproject
+++ b/source/shoh/.cproject
@@ -53,6 +53,7 @@
+
@@ -86,6 +87,7 @@
+
diff --git a/source/shoh/src/main.cpp b/source/shoh/src/main.cpp
index d1dc3d6..30bee65 100644
--- a/source/shoh/src/main.cpp
+++ b/source/shoh/src/main.cpp
@@ -7,50 +7,21 @@
#include "ThreadCommon.h"
#include "Master.h"
-#include "Rotary.h"
-#include "Manager.h"
-#include "UserInterface.h"
-#include "Log.h"
+
int main(void)
{
- LOG_INFO("Starting system init");
SystemCoreClockUpdate();
Board_Init();
retarget_init();
- LOG_INFO("System init done");
-
- ThreadCommon::ThreadManager* manager = new ThreadCommon::ThreadManager;
- ThreadCommon::QueueManager* qmanager = new ThreadCommon::QueueManager;
- LOG_INFO("Creating queues");
- qmanager->createQueue(100,
- sizeof(Event),
- ThreadCommon::QueueManager::master_event_all);
- qmanager->createQueue(20,
- sizeof(Event),
- ThreadCommon::QueueManager::manager_event_master);
- qmanager->createQueue(20,
- sizeof(UserInterface::InterfaceWithData),
- ThreadCommon::QueueManager::ui_event_manager);
-
- LOG_INFO("Creating tasks");
- manager->createTask(thread_master, "master",
- configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
- static_cast(qmanager));
- manager->createTask(thread_manager, "manager",
- configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
- static_cast(qmanager));
- manager->createTask(thread_rotary, "rotary",
- configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
- static_cast(qmanager));
- manager->createTask(thread_user_interface, "user_interface",
- configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
- static_cast(qmanager));
-
- LOG_INFO("Start the real time kernel with preemption");
+ ThreadCommon::CommonManagers manager = {new ThreadCommon::ThreadManager,
+ new ThreadCommon::QueueManager};
+ manager.tm->createTask(thread_master, "master",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(&manager));
vTaskStartScheduler ();
return 1;
diff --git a/source/shoh/src/threads/common/QueueManager.cpp b/source/shoh/src/threads/common/QueueManager.cpp
index 61a750a..9ca2293 100644
--- a/source/shoh/src/threads/common/QueueManager.cpp
+++ b/source/shoh/src/threads/common/QueueManager.cpp
@@ -13,14 +13,11 @@ ThreadCommon::QueueManager::QueueManager() {}
bool ThreadCommon::QueueManager::createQueue(size_t queue_length, size_t item_size, Queue_id qid)
{
QueueHandle_t queue_to_create;
- LOG_DEBUG("Creating queue with id %d", qid);
if ((queue_to_create = xQueueCreate(queue_length, item_size)))
{
- LOG_DEBUG("Queue with id %d has been created", qid);
queues.insert({qid, queue_to_create});
return true;
}
- LOG_ERROR("Failed to cerate queue with id %d", qid);
return false;
}
diff --git a/source/shoh/src/threads/common/ThreadCommon.h b/source/shoh/src/threads/common/ThreadCommon.h
index 2e6040b..7d6b85c 100644
--- a/source/shoh/src/threads/common/ThreadCommon.h
+++ b/source/shoh/src/threads/common/ThreadCommon.h
@@ -18,6 +18,7 @@
namespace ThreadCommon
{
+
enum RotaryAction
{
Right,
@@ -45,7 +46,8 @@ namespace ThreadCommon
master_event_all,
relay_event_master,
manager_event_master,
- ui_event_manager
+ ui_event_manager,
+ logging_message_all
};
QueueManager();
~QueueManager() = default;
@@ -70,6 +72,12 @@ namespace ThreadCommon
std::map queues;
};
+ typedef struct _CommonManagers
+ {
+ ThreadManager * tm;
+ QueueManager * qm;
+ } CommonManagers;
+
/* global variables */
/* 'receiver'_'what'_'sender'_q */
/*
diff --git a/source/shoh/src/threads/common/ThreadManager.cpp b/source/shoh/src/threads/common/ThreadManager.cpp
index 877c5eb..2ac2803 100644
--- a/source/shoh/src/threads/common/ThreadManager.cpp
+++ b/source/shoh/src/threads/common/ThreadManager.cpp
@@ -1,5 +1,4 @@
#include "ThreadCommon.h"
-#include "Log.h"
ThreadCommon::ThreadManager::ThreadManager(){}
@@ -11,18 +10,12 @@ bool ThreadCommon::ThreadManager::createTask(void (*task_func)(void*),
void* parameters)
{
const char * t_name = name.c_str();
- LOG_DEBUG("Creating task [name: %s, priority: %ld, stack: %ld]",
- t_name, priority, stack_size);
BaseType_t taskCreated = xTaskCreate(task_func,
t_name,
stack_size,
parameters,
priority,
NULL);
- if (!(taskCreated == pdPASS))
- {
- LOG_ERROR("Failed to create a task [name: %s, priority: %ld, stack: %ld]",
- t_name, priority, stack_size)
- }
+ assert(taskCreated == pdPASS);
return (taskCreated == pdPASS);
}
diff --git a/source/shoh/src/threads/common/Log.h b/source/shoh/src/threads/logging/Log.h
similarity index 88%
rename from source/shoh/src/threads/common/Log.h
rename to source/shoh/src/threads/logging/Log.h
index fc4d1ab..65536b6 100644
--- a/source/shoh/src/threads/common/Log.h
+++ b/source/shoh/src/threads/logging/Log.h
@@ -5,13 +5,11 @@
#include "board.h"
#include
#include
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
-/*
- This simlpe logging framework is dependant
- on std lib's multithread support, thus
- if needed on different platforms, please
- wrap it with mutexes.
-*/
+extern QueueHandle_t logging_queue;
/* ================= Settings ================== */
#define LOG_COLORED_OUTPUT
@@ -44,7 +42,9 @@
#define _LOG_STREAMOUT(message, message_length) \
INT_ASSERT(message_length > 0); \
- printf("%.*s\n", message_length, message); \
+ if (logging_queue) { \
+ xQueueSend(logging_queue, (void*)message, portMAX_DELAY); \
+ }
static void create_log_line(const char * _status,
const char * _location,
@@ -69,20 +69,20 @@ static void create_log_line(const char * _status,
}
#define LOG_INFO(fmt, ...) \
- create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
+ create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#define LOG_WARNING(fmt, ...) \
- create_log_line(C_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
+ create_log_line(C_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#define LOG_ERROR(fmt, ...) \
- create_log_line(C_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
+ create_log_line(C_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#if LOG_DEBUG_MESSAGES
#define LOG_DEBUG(fmt, ...) \
- create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
+ create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#else
#define LOG_DEBUG(fmt, ...)
#endif
-#endif /* __THREAD_COMMON_LOG_H_ */
\ No newline at end of file
+#endif /* __THREAD_COMMON_LOG_H_ */
diff --git a/source/shoh/src/threads/logging/Logging.cpp b/source/shoh/src/threads/logging/Logging.cpp
new file mode 100644
index 0000000..ab1176c
--- /dev/null
+++ b/source/shoh/src/threads/logging/Logging.cpp
@@ -0,0 +1,35 @@
+/*
+ * Logging.cpp
+ *
+ * Created on: 12 May 2023
+ * Author: tylen
+ */
+
+#include "Logging.h"
+#include "ThreadCommon.h"
+#include "Log.h"
+#include "queue.h"
+
+Logging::Logging(ThreadCommon::QueueManager * qm) : _qm(qm) {
+ q = _qm->getQueue(ThreadCommon::QueueManager::logging_message_all);
+}
+
+Logging::~Logging() {
+ // TODO Auto-generated destructor stub
+}
+
+void Logging::taskFunction()
+{
+ char data[LOG_BUFFER_MAX_CAP] = {0};
+ xQueueReceive(q, static_cast(data), portMAX_DELAY);
+ mutex.lock();
+ printf("%s\n", data);
+ mutex.unlock();
+}
+
+void thread_logging(void* pvParams)
+{
+ ThreadCommon::CommonManagers * manager = static_cast(pvParams);
+ Logging log(manager->qm);
+ log.taskFunction();
+}
diff --git a/source/shoh/src/threads/logging/Logging.h b/source/shoh/src/threads/logging/Logging.h
new file mode 100644
index 0000000..4dd608c
--- /dev/null
+++ b/source/shoh/src/threads/logging/Logging.h
@@ -0,0 +1,27 @@
+/*
+ * Logging.h
+ *
+ * Created on: 12 May 2023
+ * Author: tylen
+ */
+
+#ifndef THREADS_LOGGING_LOGGING_H_
+#define THREADS_LOGGING_LOGGING_H_
+
+#include "Fmutex.h"
+#include "ThreadCommon.h"
+
+class Logging {
+public:
+ Logging(ThreadCommon::QueueManager * qm);
+ virtual ~Logging();
+ void taskFunction();
+private:
+ ThreadCommon::QueueManager * _qm;
+ QueueHandle_t q;
+ Fmutex mutex;
+};
+
+void thread_logging(void* pvParams);
+
+#endif /* THREADS_LOGGING_LOGGING_H_ */
diff --git a/source/shoh/src/threads/manager/Manager.cpp b/source/shoh/src/threads/manager/Manager.cpp
index 668c551..fafac81 100644
--- a/source/shoh/src/threads/manager/Manager.cpp
+++ b/source/shoh/src/threads/manager/Manager.cpp
@@ -52,6 +52,7 @@ void Manager::taskFunction()
void thread_manager(void* pvParams)
{
- Manager m(static_cast(pvParams));
+ ThreadCommon::CommonManagers * manager = static_cast(pvParams);
+ Manager m(manager->qm);
m.taskFunction();
}
diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp
index 18072e9..ba16029 100644
--- a/source/shoh/src/threads/master/Master.cpp
+++ b/source/shoh/src/threads/master/Master.cpp
@@ -7,6 +7,13 @@
#include "Master.h"
#include "Log.h"
+#include "ThreadCommon.h"
+#include "Rotary.h"
+#include "Manager.h"
+#include "Logging.h"
+#include "UserInterface.h"
+#include "queue.h"
+#include "Logging.h"
static const char* rotary_direction[] =
{
@@ -16,6 +23,8 @@ static const char* rotary_direction[] =
"Idle"
};
+QueueHandle_t logging_queue;
+
Master::Master(ThreadCommon::QueueManager* qm) : _qm(qm)
{
LOG_DEBUG("Creating Master");
@@ -75,6 +84,44 @@ void Master::taskFunction() {
void thread_master(void* pvParams) {
- Master m(static_cast(pvParams));
+ ThreadCommon::CommonManagers * manager = static_cast(pvParams);
+
+ manager->qm->createQueue(50,
+ LOG_BUFFER_MAX_CAP,
+ ThreadCommon::QueueManager::logging_message_all);
+ logging_queue = manager->qm->getQueue(ThreadCommon::QueueManager::logging_message_all);
+ manager->tm->createTask(thread_logging, "logging",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(manager));
+
+ LOG_INFO("Logging Active");
+ LOG_INFO("Started the real time kernel with preemption");
+ LOG_INFO("Master Started");
+ Master m(manager->qm);
+ LOG_INFO("Master is creating queues");
+ manager->qm->createQueue(100,
+ sizeof(Event),
+ ThreadCommon::QueueManager::master_event_all);
+ manager->qm->createQueue(20,
+ sizeof(Event),
+ ThreadCommon::QueueManager::manager_event_master);
+ manager->qm->createQueue(20,
+ sizeof(UserInterface::InterfaceWithData),
+ ThreadCommon::QueueManager::ui_event_manager);
+
+
+ LOG_INFO("Master is creating tasks");
+ manager->tm->createTask(thread_master, "master",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(manager));
+ manager->tm->createTask(thread_manager, "manager",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(manager));
+ manager->tm->createTask(thread_rotary, "rotary",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(manager));
+ manager->tm->createTask(thread_user_interface, "user_interface",
+ configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
+ static_cast(manager));
m.taskFunction();
}
diff --git a/source/shoh/src/threads/master/Master.h b/source/shoh/src/threads/master/Master.h
index b0ae831..14c44a0 100644
--- a/source/shoh/src/threads/master/Master.h
+++ b/source/shoh/src/threads/master/Master.h
@@ -10,6 +10,8 @@
#include "chip.h"
#include "board.h"
+#include "FreeRTOS.h"
+#include "task.h"
#include "ThreadCommon.h"
#include "Event.h"
#include "task.h"
diff --git a/source/shoh/src/threads/rotary/Rotary.cpp b/source/shoh/src/threads/rotary/Rotary.cpp
index 71c36ce..a161c5b 100644
--- a/source/shoh/src/threads/rotary/Rotary.cpp
+++ b/source/shoh/src/threads/rotary/Rotary.cpp
@@ -86,6 +86,7 @@ void thread_rotary(void* pvParams)
QueueHandle_t rotary_isr_q = xQueueCreate(15, sizeof(char));
p_rotary_isr_q = &rotary_isr_q;
- Rotary r(static_cast(pvParams));
+ ThreadCommon::CommonManagers * manager = static_cast(pvParams);
+ Rotary r(manager->qm);
r.taskFunction();
}
diff --git a/source/shoh/src/threads/user_interface/UserInterface.cpp b/source/shoh/src/threads/user_interface/UserInterface.cpp
index a720cc0..2668308 100644
--- a/source/shoh/src/threads/user_interface/UserInterface.cpp
+++ b/source/shoh/src/threads/user_interface/UserInterface.cpp
@@ -60,11 +60,11 @@ void UserInterface::handleEvent(InterfaceWithData* ui_data)
void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str)
{
//Interpret empty string as clear.
- if(!strlen(str))
+ if(!strlen(str)){
lcd->clear();
LOG_INFO("Clear up LCD");
//Print the text otherwise.
- else
+ }else
{
lcd->setCursor(0, 0);
lcd->print(str);
@@ -97,6 +97,7 @@ void UserInterface::initLCD1()
void thread_user_interface(void* pvParams)
{
- UserInterface ui(static_cast(pvParams));
+ ThreadCommon::CommonManagers * manager = static_cast(pvParams);
+ UserInterface ui(manager->qm);
ui.taskFunction();
}