-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrp2040_ws2812.h
107 lines (88 loc) · 2.55 KB
/
rp2040_ws2812.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//---------------------------------------------------------------------------
#pragma once
#include "javelin/split/split.h"
#include JAVELIN_BOARD_CONFIG
//---------------------------------------------------------------------------
#if JAVELIN_RGB
class Ws2812 {
public:
static void Initialize();
static void Update() { instance.Update(); }
static bool IsAvailable() { return true; }
static size_t GetCount() { return JAVELIN_RGB_COUNT; }
static void SetRgb(size_t pixelId, int r, int g, int b) {
instance.SetRgb(pixelId, (r << 16) | (g << 24) | (b << 8));
}
static void SetRgb(size_t pixelId, uint32_t ws2812Color) {
instance.SetRgb(pixelId, ws2812Color);
}
static void RegisterTxHandler() { Split::RegisterTxHandler(&instance); }
static void RegisterRxHandler() {
Split::RegisterRxHandler(SplitHandlerId::RGB, &instance);
}
private:
#if JAVELIN_SPLIT
struct Ws2812Data final : public SplitTxHandler, SplitRxHandler {
#else
struct Ws2812Data {
#endif
bool dirty;
#if JAVELIN_SPLIT
bool slaveDirty;
#endif
uint32_t lastUpdate;
uint32_t pixelValues[JAVELIN_RGB_COUNT];
void Update();
void SetRgb(size_t pixelId, uint32_t ws2812Color) {
if (pixelId >= JAVELIN_RGB_COUNT) {
return;
}
#if JAVELIN_USE_RGB_MAP
pixelId = RGB_MAP[pixelId];
#endif
if (pixelValues[pixelId] == ws2812Color) {
return;
}
#if JAVELIN_SPLIT
if (Split::IsLeft()) {
if (pixelId < JAVELIN_RGB_LEFT_COUNT) {
dirty = true;
} else {
slaveDirty = true;
}
} else {
if (pixelId < JAVELIN_RGB_LEFT_COUNT) {
slaveDirty = true;
} else {
dirty = true;
}
}
#else
dirty = true;
#endif
pixelValues[pixelId] = ws2812Color;
}
#if JAVELIN_SPLIT
void UpdateBuffer(TxBuffer &buffer) final;
void OnTransmitConnected() final { slaveDirty = true; }
void OnTransmitConnectionReset() final { slaveDirty = true; }
void OnDataReceived(const void *data, size_t length) final;
#endif
};
static Ws2812Data instance;
};
#else
// Do nothing instance.
class Ws2812 {
public:
static void Initialize() {}
static void Update() {}
static bool IsAvailable() { return false; }
static size_t GetCount() { return 0; }
static void SetRgb(size_t pixelId, int r, int g, int b) {}
static void SetRgb(size_t pixelId, uint32_t ws2812Color) {}
static void RegisterTxHandler() {}
static void RegisterRxHandler() {}
};
#endif
//---------------------------------------------------------------------------