-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcustom_hal.ino
51 lines (41 loc) · 1.25 KB
/
custom_hal.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// JLed custom HAL example.
// Copyright 2019 by Jan Delgado. All rights reserved.
// https://github.com/jandelgado/jled
// we include jled_base.h instead of "jled.h" since we define our own JLed
// class using our custom HAL.
#include <jled_base.h>
// a custom HAL for the Arduino, inverting output and ticking with half
// the speed. In general, a JLed HAL class must satisfy the following
// interface:
//
// class JledHal {
// public:
// JledHal(PinType pin);
// void analogWrite(uint8_t val) const;
// uint32_t millis() const;
// }
//
class CustomHal {
public:
using PinType = uint8_t;
explicit CustomHal(PinType pin) noexcept : pin_(pin) {}
void analogWrite(uint8_t val) const {
// some platforms, e.g. STM need lazy initialization
if (!setup_) {
::pinMode(pin_, OUTPUT);
setup_ = true;
}
::analogWrite(pin_, 255 - val);
}
uint32_t millis() const { return ::millis() >> 1; }
private:
mutable bool setup_ = false;
PinType pin_;
};
class JLed : public jled::TJLed<CustomHal, JLed> {
using jled::TJLed<CustomHal, JLed>::TJLed;
};
// uses above defined CustomHal
auto led = JLed(LED_BUILTIN).Blink(1000, 1000).Repeat(5);
void setup() {}
void loop() { led.Update(); }