-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSpiMaster.h
More file actions
70 lines (59 loc) · 1.95 KB
/
SpiMaster.h
File metadata and controls
70 lines (59 loc) · 1.95 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include <cstddef>
#include <cstdint>
#include <algorithm>
namespace Pinetime {
namespace Drivers {
template <typename T>
concept IsSpiMaster =
requires(T spi, uint8_t pin, const uint8_t* constData, uint8_t* data, const uint8_t* constCommand, uint8_t* command, size_t size) {
{ spi.Init() } -> std::same_as<bool>;
{ spi.Write(pin, constData, size) } -> std::same_as<bool>;
{ spi.Read(pin, command, size, data, size) } -> std::same_as<bool>;
{ spi.WriteCmdAndBuffer(pin, constCommand, size, constData, size) } -> std::same_as<bool>;
{ spi.OnStartedEvent() };
{ spi.OnEndEvent() };
{ spi.Sleep() };
{ spi.Wakeup() };
};
namespace Interface {
template <class T>
requires IsSpiMaster<T>
class SpiMaster {
public:
SpiMaster(T& spiMaster) : impl {spiMaster} {
}
SpiMaster(const SpiMaster&) = delete;
SpiMaster& operator=(const SpiMaster&) = delete;
SpiMaster(SpiMaster&&) = delete;
SpiMaster& operator=(SpiMaster&&) = delete;
bool Init() {
return impl.Init();
}
bool Write(uint8_t pinCsn, const uint8_t* data, size_t size) {
return impl.Write(pinCsn, data, size);
}
bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) {
return impl.Read(pinCsn, cmd, cmdSize, data, dataSize);
}
bool WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize) {
return impl.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize);
}
void OnStartedEvent() {
impl.OnStartedEvent();
}
void OnEndEvent() {
impl.OnEndEvent();
}
void Sleep() {
impl.Sleep();
}
void Wakeup() {
impl.Wakeup();
}
private:
T& impl;
};
}
}
}