7 changed files with 160 additions and 4 deletions
-
6components/key_monitor/zkey_service.cpp
-
1components/key_monitor/zkey_service.hpp
-
5os/zos.hpp
-
42os/zos_schduler.cpp
-
67os/zos_schduler.hpp
-
23os/zos_thread.cpp
-
20os/zos_thread.hpp
@ -0,0 +1,42 @@ |
|||
#include "zos_schduler.hpp"
|
|||
|
|||
#include "ticket.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
void ZOSSchduler::regPeriodJob(function<void(Context& context)> 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); |
|||
} |
|||
}); |
|||
} |
@ -0,0 +1,67 @@ |
|||
#pragma once
|
|||
#include <stdio.h>
|
|||
|
|||
#include <functional>
|
|||
#include <list>
|
|||
|
|||
// #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<void(Context& context)> job, uint32_t period_ms) { |
|||
this->job = job; |
|||
this->period_ms = period_ms; |
|||
} |
|||
function<void(Context& context)> job; |
|||
uint32_t period_ms = 0; |
|||
uint32_t lastcall = 0; |
|||
uint32_t schedule_times = 0; |
|||
}; |
|||
|
|||
/*******************************************************************************
|
|||
* PARA * |
|||
*******************************************************************************/ |
|||
list<PeriodJob*> m_periodJobs; |
|||
ZOSThread m_thread; |
|||
|
|||
public: |
|||
ZOSSchduler() {} |
|||
|
|||
void initialize(){}; |
|||
|
|||
void regPeriodJob(function<void(Context& context)> 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
|
@ -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<void()> 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(); } |
@ -0,0 +1,20 @@ |
|||
#pragma once
|
|||
#include "osbasic_h.hpp"
|
|||
|
|||
namespace iflytop { |
|||
class ZOSThread { |
|||
osThreadId _defaultTaskHandle; |
|||
int _stack_size; |
|||
osPriority _priority; |
|||
function<void()> _func; |
|||
const char* _threadname; |
|||
|
|||
public: |
|||
void init(const char* threadname, int stack_size = 512, osPriority priority = osPriorityNormal); |
|||
void run(function<void()> func); |
|||
|
|||
public: |
|||
void __callfunc(); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue