diff --git a/components/key_monitor/zkey_service.cpp b/components/key_monitor/zkey_service.cpp index 5bcd173..4aa5eff 100644 --- a/components/key_monitor/zkey_service.cpp +++ b/components/key_monitor/zkey_service.cpp @@ -39,15 +39,15 @@ void ZKeyService::processEachAfterFilter(ZKey* each, bool now_io_state) { if (now_io_state) { each->keep_state_count = 0; each->hasProcessed = false; - m_listener(each, zke_rising_edge); + if (m_listener) m_listener(each, zke_rising_edge); } else { - m_listener(each, zke_falling_edge); + if (m_listener) m_listener(each, zke_falling_edge); each->keep_state_count = 0; } each->last_after_filter_io_state = now_io_state; } else { if (now_io_state) { - m_listener(each, zke_keep); + if (m_listener) m_listener(each, zke_keep); } } } diff --git a/components/key_monitor/zkey_service.hpp b/components/key_monitor/zkey_service.hpp index 76e1d39..fc9788f 100644 --- a/components/key_monitor/zkey_service.hpp +++ b/components/key_monitor/zkey_service.hpp @@ -55,6 +55,7 @@ class ZKeyService { public: void initialize(ZKey* keys, int numKey, KeyListener_t listener); + void setListener(KeyListener_t listener) { m_listener = listener; } void periodicJob(); private: diff --git a/os/zos.hpp b/os/zos.hpp index 373251e..6f624cc 100644 --- a/os/zos.hpp +++ b/os/zos.hpp @@ -11,7 +11,10 @@ #include "ticket.hpp" // #include "os_default_schduler.hpp" - +// +#include "zos_thread.hpp" +// +#include "zos_schduler.hpp" extern "C" { typedef struct { uint32_t __reserved0; diff --git a/os/zos_schduler.cpp b/os/zos_schduler.cpp new file mode 100644 index 0000000..1ab5eaa --- /dev/null +++ b/os/zos_schduler.cpp @@ -0,0 +1,42 @@ +#include "zos_schduler.hpp" + +#include "ticket.hpp" +using namespace std; +using namespace iflytop; + +void ZOSSchduler::regPeriodJob(function job, uint32_t period_ms) { + PeriodJob* periodJob = new PeriodJob(job, period_ms); + ZASSERT(periodJob != NULL); + m_periodJobs.push_back(periodJob); +} + +void ZOSSchduler::loop() { + static uint32_t ticket = 0; + Context context; + if (zos_haspassedms(ticket) < 1) { + return; + } + ticket = zos_get_tick(); + for (auto iter = m_periodJobs.begin(); iter != m_periodJobs.end(); iter++) { + auto periodJob = *iter; + + if ((uint32_t)zos_haspassedms2(periodJob->lastcall, ticket) < periodJob->period_ms) { + continue; + } + + context.periodJob = periodJob; + periodJob->lastcall = ticket; + periodJob->job(context); + periodJob->schedule_times++; + } +} + +void ZOSSchduler::startSchedule(const char* threadname, int stack_size, osPriority priority) { + m_thread.init(threadname, stack_size, priority); + m_thread.run([this]() { + while (true) { + loop(); + osDelay(1); + } + }); +} diff --git a/os/zos_schduler.hpp b/os/zos_schduler.hpp new file mode 100644 index 0000000..a66a457 --- /dev/null +++ b/os/zos_schduler.hpp @@ -0,0 +1,67 @@ +#pragma once +#include + +#include +#include + +// #include "sdk/os/zos.hpp" +#include "osbasic_h.hpp" +#include "zos_thread.hpp" + +// +namespace iflytop { +using namespace std; + +class ZOSSchduler { + public: + class PeriodJob; + /******************************************************************************* + * Context * + *******************************************************************************/ + class Context { + friend class ZOSSchduler; + + protected: + PeriodJob* periodJob; + + public: + uint32_t getScheduleTimes() { return periodJob->schedule_times; } + }; + + /******************************************************************************* + * PeriodJob * + *******************************************************************************/ + class PeriodJob { + public: + PeriodJob(function job, uint32_t period_ms) { + this->job = job; + this->period_ms = period_ms; + } + function job; + uint32_t period_ms = 0; + uint32_t lastcall = 0; + uint32_t schedule_times = 0; + }; + + /******************************************************************************* + * PARA * + *******************************************************************************/ + list m_periodJobs; + ZOSThread m_thread; + + public: + ZOSSchduler() {} + + void initialize(){}; + + void regPeriodJob(function job, uint32_t period_ms); + void startSchedule(const char* threadname, int stack_size, osPriority priority); + + private: + void loop(); + +}; + +// #define ZHAL_CORE_REG(period_ms, job) ZOSSchduler::getInstance()->regPeriodJob([this](ZOSSchduler::Context& context) { job }, period_ms); + +} // namespace iflytop \ No newline at end of file diff --git a/os/zos_thread.cpp b/os/zos_thread.cpp new file mode 100644 index 0000000..3440aae --- /dev/null +++ b/os/zos_thread.cpp @@ -0,0 +1,23 @@ +#include "zos_thread.hpp" +using namespace iflytop; +using namespace std; + +static void zosthread_default_task(void const *argument) { + ZOSThread *thread = (ZOSThread *)argument; + thread->__callfunc(); +} + +void ZOSThread::init(const char *threadname, int stack_size, osPriority priority) { + _stack_size = stack_size; + _priority = priority; + _threadname = threadname; +} + +void ZOSThread::run(function func) { + _func = func; + osThreadDef(zosthread_default_task, zosthread_default_task, _priority, 0, _stack_size); + _defaultTaskHandle = osThreadCreate(osThread(zosthread_default_task), this); + ZASSERT(_defaultTaskHandle != NULL); +} + +void ZOSThread::__callfunc() { _func(); } diff --git a/os/zos_thread.hpp b/os/zos_thread.hpp new file mode 100644 index 0000000..63ccbcb --- /dev/null +++ b/os/zos_thread.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "osbasic_h.hpp" + +namespace iflytop { +class ZOSThread { + osThreadId _defaultTaskHandle; + int _stack_size; + osPriority _priority; + function _func; + const char* _threadname; + + public: + void init(const char* threadname, int stack_size = 512, osPriority priority = osPriorityNormal); + void run(function func); + + public: + void __callfunc(); +}; + +} // namespace iflytop \ No newline at end of file