Skip to content

Commit 78297e5

Browse files
committed
prepared better trigger handling
1 parent 7eaeb3d commit 78297e5

File tree

5 files changed

+63
-63
lines changed

5 files changed

+63
-63
lines changed

include/DMDUtil/DMD.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ class DMDUTILAPI DMD
152152
void convertToHostByteOrder() {}
153153
void convertToNetworkByteOrder() {}
154154
};
155-
156-
struct PUPTrigger
157-
{
158-
char source = 0;
159-
uint16_t id = 0;
160-
uint8_t value = 1;
161-
};
162155
#pragma pack(pop) // Reset to default packing
163156

164157
void FindDisplays();
@@ -242,7 +235,7 @@ class DMDUTILAPI DMD
242235
std::atomic<bool> m_dmdFrameReady;
243236
std::atomic<bool> m_stopFlag;
244237
std::atomic<uint8_t> m_updateBufferQueuePosition;
245-
std::atomic<PUPTrigger> m_pupTrigger;
238+
std::atomic<uint16_t> m_pupSceneId;
246239

247240
bool m_hasUpdateBuffered = false;
248241
static bool m_finding;

include/DMDUtil/SceneGenerator.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ namespace DMDUtil
1717

1818
struct SceneData
1919
{
20-
char source;
2120
int sceneId;
22-
int value;
2321
int frameCount;
2422
int durationPerFrame;
2523
bool interruptable = false;
@@ -41,9 +39,9 @@ class DMDUTILAPI SceneGenerator
4139

4240
bool parseCSV(const std::string& csv_filename);
4341
bool generateDump(const std::string& dump_filename, int id = -1);
44-
bool getSceneExists(char source, int sceneId, int value) const;
45-
bool getSceneInfo(char source, int sceneId, int value, int& frameCount, int& durationPerFrame, bool& interruptable,
46-
bool& startImmediately, int& repeat, int& endFrame) const;
42+
uint16_t getSceneId(char source, int event, int value) const;
43+
bool getSceneInfo(int sceneId, int& frameCount, int& durationPerFrame, bool& interruptable, bool& startImmediately,
44+
int& repeat, int& endFrame) const;
4745
bool generateFrame(int sceneId, int frameIndex, uint8_t* buffer, int group = -1);
4846
void setDepth(int depth);
4947
void Reset()

scene_test.csv

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
# Format of a PUP scene line:
22
#
3-
# 1: PUP trigger source (D - DMD, W - Switch, S - Solenoid, L - Light),
4-
# 2: PUP trigger ID,
5-
# 3: PUP trigger value,
6-
# 4: number of frames,
7-
# 5: duration of each frame,
8-
# 6: 0 - not interruptable, 1 - interruptable by frame match or PUP trigger
9-
# 7: 0 - start immediately, replacing triggering frame, 1 - start after frame duration (see 5)
10-
# 8: 0 - play once, 1 - loop, >= 2 - repeat x times
11-
# 9: 0 - no frame groups, >= 2 - create x frame groups (you get x times the number of frames entered in 2 to play changing scenes)
12-
# 10: 0 - play frame group in order, 1 - play random frame group
13-
# 11: 0 - no autostart, >= 1 - start this scene after x seconds of inactivity (no new frames), only use once, could be combined with frame groups
14-
# 12: 0 - when scene is finished, show last frame of the scene until a new frame is matched, 1 - black screen, 2 - show last frame before scene started
3+
# 1: PUP scene ID
4+
# 2: number of frames
5+
# 3: duration of each frame
6+
# 4: 0 - not interruptable, 1 - interruptable by frame match or PUP event
7+
# 5: 0 - start immediately, replacing triggering frame, 1 - start after frame duration (see 3)
8+
# 6: 0 - play once, 1 - loop, >= 2 - repeat x times
9+
# 7: 0 - no frame groups, >= 2 - create x frame groups (you get x times the number of frames entered in 2 to play changing scenes)
10+
# 8: 0 - play frame group in order, 1 - play random frame group
11+
# 9: 0 - no autostart, >= 1 - start this scene after x seconds of inactivity (no new frames), only use once, could be combined with frame groups
12+
# 10: 0 - when scene is finished, show last frame of the scene until a new frame is matched, 1 - black screen, 2 - show last frame before scene started
1513
#
1614
# Postions 4 - 10 are optional. If not provided, the default is 0.
1715

16+
# Format of a PUP event line:
17+
#
18+
# 1: PUP event source (D - DMD, W - Switch, S - Solenoid, L - Light, E - custom Event)
19+
# 2: PUP event ID
20+
# 3: PUP event value
21+
# 4: PUP scene ID to trigger
22+
1823
# Scene 60001
19-
D,60001,1,38,16
24+
60001,38,16
2025

2126
# Scene 61479, interruptable
22-
D,61479,1,52,20,1
27+
61479,52,20,1
2328

2429
# Scene 63500, interruptable, starting delayed by frame duration, loop
25-
D,63500,1,40,20,1,1,1
30+
63500,40,20,1,1,1
2631

2732
# Scene 63511, interruptable, starting delayed by frame duration, play once, 3 frame groups
2833
# This will generate 3 groups of 75 frames, so 225 frames in total.
2934
# Every time the PUP ID 63511 get triggered, only one of the three groups gets played (75 frames).
3035
# First occurrence of the PUP ID will play group 1, the second group 2. After the third group, it will start again with group 1.
31-
D,63511,1,75,20,1,1,0,3
36+
63511,75,20,1,1,0,3
3237

3338
# This will generate 4 groups of 200 frames, so 800 frames in total.
3439
# If no frame is matched for 5 seconds, randomly one of the 4 groups gets played once.
@@ -39,4 +44,4 @@ D,63511,1,75,20,1,1,0,3
3944
# Normally, the last scores are shown until someone starts a new game.
4045
# Now the scores are displayed for 5 seconds, than a random short video scene out of four gets played for 4 seconds.
4146
# Then the scores are shown again and so on until someone starts a game.
42-
D,63600,1,200,20,1,1,0,4,1,5,2
47+
63600,200,20,1,1,0,4,1,5,2

src/DMD.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -896,23 +896,22 @@ void DMD::SerumThread()
896896
}
897897
else
898898
{
899-
PUPTrigger pupTrigger = m_pupTrigger.load(std::memory_order_relaxed);
900-
if (pupTrigger.source != 0)
899+
uint16_t sceneId = m_pupSceneId.load(std::memory_order_relaxed);
900+
if (sceneId > 0)
901901
{
902-
if (m_pGenerator->getSceneInfo(pupTrigger.source, pupTrigger.id, pupTrigger.value, sceneFrameCount,
903-
sceneDurationPerFrame, sceneInterruptable, sceneStartImmediately,
904-
sceneRepeatCount, sceneEndFrame))
902+
if (m_pGenerator->getSceneInfo(sceneId, sceneFrameCount, sceneDurationPerFrame, sceneInterruptable,
903+
sceneStartImmediately, sceneRepeatCount, sceneEndFrame))
905904
{
906-
Log(DMDUtil_LogLevel_DEBUG, "Serum: trigger ID %lu found in scenes, frame count=%d, duration=%dms",
907-
pupTrigger.id, sceneFrameCount, sceneDurationPerFrame);
905+
Log(DMDUtil_LogLevel_DEBUG, "Serum: PUP Scene ID %lu found in scenes, frame count=%d, duration=%dms",
906+
sceneId, sceneFrameCount, sceneDurationPerFrame);
908907
sceneCurrentFrame = 0;
909908
nextSceneFrame = std::chrono::duration_cast<std::chrono::milliseconds>(
910909
std::chrono::system_clock::now().time_since_epoch())
911910
.count() +
912911
(sceneStartImmediately ? 0 : sceneDurationPerFrame);
913912
}
914-
pupTrigger.source = 0; // Reset the trigger after processing
915-
m_pupTrigger.store(pupTrigger, std::memory_order_release);
913+
// Reset the trigger after processing
914+
m_pupSceneId.store(0, std::memory_order_release);
916915
}
917916
else
918917
{
@@ -1085,7 +1084,7 @@ void DMD::SerumThread()
10851084

10861085
if (m_pSerum->triggerID < 0xffffffff)
10871086
{
1088-
if (m_pGenerator->getSceneInfo('D', m_pSerum->triggerID, 1, sceneFrameCount, sceneDurationPerFrame,
1087+
if (m_pGenerator->getSceneInfo(m_pSerum->triggerID, sceneFrameCount, sceneDurationPerFrame,
10891088
sceneInterruptable, sceneStartImmediately, sceneRepeatCount,
10901089
sceneEndFrame))
10911090
{
@@ -2085,15 +2084,19 @@ void DMD::HandleTrigger(uint16_t id)
20852084
}
20862085
}
20872086

2088-
void DMD::SetPUPTrigger(const char source, const uint16_t id, const uint8_t value)
2087+
void DMD::SetPUPTrigger(const char source, const uint16_t event, const uint8_t value)
20892088
{
2090-
if (m_pSerum && m_pGenerator->getSceneExists(source, id, value))
2089+
if (m_pSerum)
20912090
{
2092-
while (m_pupTrigger.load(std::memory_order_acquire).source != 0)
2091+
uint16_t id = m_pGenerator->getSceneId(source, event, value);
2092+
if (id > 0)
20932093
{
2094-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
2094+
while (m_pupSceneId.load(std::memory_order_acquire) != 0)
2095+
{
2096+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
2097+
}
2098+
m_pupSceneId.store(id, std::memory_order_release);
20952099
}
2096-
m_pupTrigger.store({source, id, value}, std::memory_order_release);
20972100
}
20982101
}
20992102

src/SceneGenerator.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,16 @@ bool SceneGenerator::parseCSV(const std::string& csv_filename)
9494
try
9595
{
9696
SceneData data;
97-
data.source = row[0][0]; // First character of the first column
98-
data.sceneId = std::stoi(row[1]);
99-
data.value = std::stoi(row[2]);
100-
data.frameCount = std::stoi(row[3]);
101-
data.durationPerFrame = std::stoi(row[4]);
102-
if (row.size() >= 6) data.interruptable = (std::stoi(row[5]) == 1);
103-
if (row.size() >= 7) data.immediateStart = (std::stoi(row[6]) == 1);
104-
if (row.size() >= 8) data.repeat = std::stoi(row[7]);
105-
if (row.size() >= 9) data.frameGroups = std::stoi(row[8]) == 0 ? 1 : std::stoi(row[8]);
106-
if (row.size() >= 10) data.random = (std::stoi(row[9]) == 1);
107-
if (row.size() >= 11) data.autoStart = std::stoi(row[10]);
108-
if (row.size() >= 12) data.endFrame = std::stoi(row[11]);
97+
data.sceneId = std::stoi(row[0]);
98+
data.frameCount = std::stoi(row[1]);
99+
data.durationPerFrame = std::stoi(row[2]);
100+
if (row.size() >= 4) data.interruptable = (std::stoi(row[3]) == 1);
101+
if (row.size() >= 5) data.immediateStart = (std::stoi(row[4]) == 1);
102+
if (row.size() >= 6) data.repeat = std::stoi(row[5]);
103+
if (row.size() >= 7) data.frameGroups = std::stoi(row[6]) == 0 ? 1 : std::stoi(row[6]);
104+
if (row.size() >= 8) data.random = (std::stoi(row[7]) == 1);
105+
if (row.size() >= 9) data.autoStart = std::stoi(row[8]);
106+
if (row.size() >= 10) data.endFrame = std::stoi(row[9]);
109107

110108
m_sceneData.push_back(data);
111109
}
@@ -173,9 +171,11 @@ bool SceneGenerator::generateDump(const std::string& dump_filename, int id)
173171
return true;
174172
}
175173

176-
bool SceneGenerator::getSceneExists(char source, int sceneId, int value) const
174+
uint16_t SceneGenerator::getSceneId(char source, int event, int value) const
177175
{
178-
auto it = std::find_if(m_sceneData.begin(), m_sceneData.end(), [source, sceneId, value](const SceneData& data)
176+
return 0;
177+
/*
178+
auto it = std::find_if(m_sceneData.begin(), m_sceneData.end(), [source, event, value](const SceneData& data)
179179
{ return data.source == source && data.sceneId == sceneId && data.value == value; });
180180
181181
if (it == m_sceneData.end())
@@ -184,13 +184,14 @@ bool SceneGenerator::getSceneExists(char source, int sceneId, int value) const
184184
}
185185
186186
return true;
187+
*/
187188
}
188189

189-
bool SceneGenerator::getSceneInfo(char source, int sceneId, int value, int& frameCount, int& durationPerFrame,
190-
bool& interruptable, bool& startImmediately, int& repeat, int& endFrame) const
190+
bool SceneGenerator::getSceneInfo(int sceneId, int& frameCount, int& durationPerFrame, bool& interruptable,
191+
bool& startImmediately, int& repeat, int& endFrame) const
191192
{
192-
auto it = std::find_if(m_sceneData.begin(), m_sceneData.end(), [source, sceneId, value](const SceneData& data)
193-
{ return data.source == source && data.sceneId == sceneId && data.value == value; });
193+
auto it = std::find_if(m_sceneData.begin(), m_sceneData.end(),
194+
[sceneId](const SceneData& data) { return data.sceneId == sceneId; });
194195

195196
if (it == m_sceneData.end())
196197
{

0 commit comments

Comments
 (0)