Skip to content

Instantly share code, notes, and snippets.

@kinoshita-lab
Created April 9, 2023 02:41
Show Gist options
  • Save kinoshita-lab/7f2bf1f3bdda092adf7eb520e402e499 to your computer and use it in GitHub Desktop.
Save kinoshita-lab/7f2bf1f3bdda092adf7eb520e402e499 to your computer and use it in GitHub Desktop.
FakeMsTimer
#include <Arduino.h>
#include "FakeMsTimer.h"
namespace FakeMsTimer
{
uint32_t _milliseconds = 0;
uint32_t _counter = 0;
bool _started = false;
void (*func)() = nullptr;
uint32_t _lastMillis;
void set(uint32_t milliseconds, void (*f)())
{
_milliseconds = milliseconds;
func = f;
}
void start()
{
_counter = 0;
_started = true;
}
void stop()
{
_counter = 0;
_started = false;
}
void fakeTick()
{
if (!_started) {
return;
}
const uint32_t now = millis();
if (_lastMillis == 0) {
_lastMillis = now;
}
const uint32_t delta = millis() - _lastMillis;
_lastMillis = now;
_counter += delta;
if (_counter >= _milliseconds) {
if (func) {
func();
}
_counter = 0;
}
}
}
#pragma once
#include <stdint.h>
// ニセMSTimer2
namespace FakeMsTimer
{
extern void set(uint32_t milliseconds, void (*f)());
extern void start();
extern void stop();
extern void fakeTick();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment