Created
April 9, 2023 02:41
-
-
Save kinoshita-lab/7f2bf1f3bdda092adf7eb520e402e499 to your computer and use it in GitHub Desktop.
FakeMsTimer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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