Skip to content

Commit 622c4e6

Browse files
committed
provide fast loop option
1 parent 8f8404a commit 622c4e6

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/SnapClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ class SnapClient {
145145
p_snapprocessor = &processor;
146146
}
147147

148+
/// Provides the actual processor
149+
SnapProcessor& snapProcessor(){
150+
return *p_snapprocessor;
151+
}
152+
148153
/// Defines the Snap output implementation to be used
149154
void setSnapOutput(SnapOutput &out){
150155
p_snapprocessor->setSnapOutput(out);

src/api/SnapProcessor.h

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SnapProcessor {
4444
resizeData();
4545
}
4646
header_received = false;
47+
loop_status = LoopStart;
4748

4849
return result;
4950
}
@@ -70,7 +71,12 @@ class SnapProcessor {
7071
void setStartTask(bool flag) { http_task_start = flag; }
7172

7273
/// Call via SnapClient in Arduino Loop!
73-
virtual void doLoop() { processLoopStep(); }
74+
virtual void doLoop() {
75+
if (is_fast_loop)
76+
processLoopStepFast();
77+
else
78+
processLoopStep();
79+
}
7480

7581
/// Defines the output class
7682
void setOutput(AudioOutput &output) { p_snap_output->setOutput(output); }
@@ -96,6 +102,11 @@ class SnapProcessor {
96102
p_snap_output->setAudioInfo(info);
97103
}
98104

105+
// Select loop processing with minimum delays
106+
void setFastLoop(bool flag){
107+
is_fast_loop = flag;
108+
}
109+
99110
protected:
100111
const char *TAG = "SnapProcessor";
101112
// WiFiClient default_client;
@@ -124,6 +135,53 @@ class SnapProcessor {
124135
bool header_received = false;
125136
bool is_time_set = false;
126137
SnapTime &snap_time = SnapTime::instance();
138+
bool is_fast_loop = false;
139+
enum loop_status_enum { LoopStart, LoopStep, LoopEnd };
140+
loop_status_enum loop_status = LoopStart;
141+
142+
bool processLoopStepFast() {
143+
switch (loop_status) {
144+
case LoopStart: {
145+
if (connectClient()) {
146+
ESP_LOGI(TAG, "... connected");
147+
} else {
148+
delay(10);
149+
return true;
150+
}
151+
152+
now = snap_time.time();
153+
if (!writeHallo()) {
154+
ESP_LOGI(TAG, "writeHallo");
155+
return false;
156+
}
157+
loop_status = LoopStep;
158+
return true;
159+
}
160+
161+
case LoopStep: {
162+
if (!processMessageLoop()) {
163+
loop_status = LoopEnd;
164+
}
165+
// some additional processing while we wait for data
166+
processExt();
167+
// logHeap();
168+
checkHeap();
169+
return true;
170+
}
171+
172+
case LoopEnd: {
173+
loop_status = LoopStart;
174+
if (id_counter % 100 == 0) {
175+
logHeap();
176+
}
177+
// For rtos, give audio output some space
178+
delay(1);
179+
return true;
180+
}
181+
}
182+
// we should not get here
183+
return false;
184+
}
127185

128186
bool processLoopStep() {
129187
if (connectClient()) {

0 commit comments

Comments
 (0)