Browse Source

update

master
zhaohe 2 years ago
parent
commit
7100ff3f22
  1. 7
      chip/delay.c
  2. 2
      chip/delay.h
  3. 4
      hal/zhal.hpp
  4. 33
      hal/zhal_core.cpp
  5. 52
      hal/zhal_core.hpp

7
chip/delay.c

@ -36,3 +36,10 @@ uint32_t haspassedms(uint32_t ticket) {
}
return UINT32_MAX - ticket + nowticket;
}
uint32_t haspassedms2(uint32_t lastticket, uint32_t nowticket) {
if (nowticket >= lastticket) {
return nowticket - lastticket;
}
return UINT32_MAX - lastticket + nowticket;
}

2
chip/delay.h

@ -12,6 +12,8 @@ void chip_delay_ms(uint32_t n);
void ifly_delay_ms(uint32_t n);
uint32_t chip_get_ticket();
uint32_t haspassedms(uint32_t ticket);
uint32_t haspassedms2(uint32_t lastticket, uint32_t nowticket);
#ifdef __cplusplus
}

4
hal/zhal.hpp

@ -0,0 +1,4 @@
#pragma once
#include "../chip/iflytop_no_os.h"
#include "gpio.hpp"
#include "zhal_core.hpp"

33
hal/zhal_core.cpp

@ -0,0 +1,33 @@
#include "zhal_core.hpp"
using namespace std;
using namespace iflytop;
namespace iflytop {
ZHALCORE ZHALCORE_instance;
}
void ZHALCORE::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 ZHALCORE::loop() {
static uint32_t ticket = 0;
Context context;
if (haspassedms(ticket) < 1) {
return;
}
ticket = chip_get_ticket();
for (auto iter = m_periodJobs.begin(); iter != m_periodJobs.end(); iter++) {
auto periodJob = *iter;
if (haspassedms2(periodJob->lastcall, ticket) < periodJob->period_ms) {
continue;
}
context.periodJob = periodJob;
periodJob->lastcall = ticket;
periodJob->job(context);
periodJob->schedule_times++;
}
}

52
hal/zhal_core.hpp

@ -0,0 +1,52 @@
#pragma once
#include <stdio.h>
#include <functional>
#include <list>
#include "../chip/iflytop_no_os.h"
//
namespace iflytop {
using namespace std;
class ZHALCORE;
extern ZHALCORE ZHALCORE_instance;
class ZHALCORE {
public:
class PeriodJob;
class Context {
friend class ZHALCORE;
protected:
PeriodJob* periodJob;
public:
};
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;
};
list<PeriodJob*> m_periodJobs;
public:
ZHALCORE() {}
static ZHALCORE* getInstance() { return &ZHALCORE_instance; }
void initialize(){};
void regPeriodJob(function<void(Context& context)> job, uint32_t period_ms);
void loop();
};
#define ZHAL_CORE_REG(period_ms, job) ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context& context) { job }, period_ms);
} // namespace iflytop
Loading…
Cancel
Save