Magic Poi Lite 0.1
IOT LED Poi
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
Playing Class Reference
Collaboration diagram for Playing:
Collaboration graph

Public Member Functions

void processTimelineData (const String &timelineData)
 Processes the timeline data received from the server.
 
int * getColours ()
 Returns the array of LED colors.
 
long * getTimings ()
 Returns the array of timings.
 
int getMaxTimingsNum ()
 Returns the maximum number of timings in the timeline.
 
String loadTimeline ()
 Loads the timeline data from the disk.
 
bool setup ()
 Sets up the playing process.
 
void play ()
 Plays the timeline data.
 
void changeColours (int choice)
 Changes the colors of the RGB LED based on the provided choice.
 
void useTimelineData ()
 Uses the timeline data to change LED colors over time.
 

Private Attributes

int maxTimingsNum = 50
 Number of timings in the timeline.
 
String timelineNumber = "0"
 Variable to use int tinelineFilePath.
 
String timelineFilePath = "/timeline" + timelineNumber + ".txt"
 File path of the timeline file.
 
const char * timeline_keys [50]
 Array of keys in the timeline JSON.
 
JsonVariant redVal
 Red color value extracted from JSON.
 
JsonVariant greenVal
 Green color value extracted from JSON.
 
JsonVariant blueVal
 Blue color value extracted from JSON.
 
int redInt = 1
 Red color value as an integer.
 
int greenInt = 1
 Green color value as an integer.
 
int blueInt = 1
 Blue color value as an integer.
 
long timings [50]
 Array storing timings extracted from JSON.
 
int colours [50]
 Array storing colors extracted from JSON.
 
bool already_got_data = false
 Flag indicating whether timeline data has been loaded.
 
long playStartTime = 0
 Timestamp when timeline playing started.
 
JsonObject root
 JSON object root.
 
int iter = 0
 Iterator for timeline data.
 
uint8_t signal
 Signal indicator.
 
long previousMillis = 0
 Timestamp of the last LED update.
 
long currentMillis2 = 0
 Current timestamp.
 
bool playing = true
 Flag indicating whether playing is active.
 
int runNum = 0
 Run number.
 
int currentIndex = 0
 Current index in the timeline data arrays.
 

Member Function Documentation

◆ changeColours()

void Playing::changeColours ( int choice)

Changes the colors of the RGB LED based on the provided choice.

This method changes the colors of the RGB LED based on the provided choice.

Parameters
choiceAn integer representing the color choice:
  • 0: Red
  • 1: Green
  • 2: Blue
  • 3: Cyan
  • 4: Magenta
  • 5: Yellow
  • 6: White
  • Any other value: Default (turn off the LED)
234{
235 switch (choice)
236 {
237 case 0: // red
238 analogWrite(redLEDPin, 255);
239 analogWrite(greenLEDPin, 0);
240 analogWrite(blueLEDPin, 0);
241 Serial.println("RED");
242 break;
243 case 1: // green
244 analogWrite(redLEDPin, 0);
245 analogWrite(greenLEDPin, 255);
246 analogWrite(blueLEDPin, 0);
247 Serial.println("GREEN");
248 break;
249 case 2: // blue
250 analogWrite(redLEDPin, 0);
251 analogWrite(greenLEDPin, 0);
252 analogWrite(blueLEDPin, 255);
253 Serial.println("BLUE");
254 break;
255 case 3: // cyan
256 analogWrite(redLEDPin, 0);
257 analogWrite(greenLEDPin, 255);
258 analogWrite(blueLEDPin, 255);
259 Serial.println("CYAN");
260 break;
261 case 4: // magenta
262 analogWrite(redLEDPin, 255);
263 analogWrite(greenLEDPin, 0);
264 analogWrite(blueLEDPin, 255);
265 Serial.println("MAGENTA");
266 break;
267 case 5: // yellow
268 analogWrite(redLEDPin, 255);
269 analogWrite(greenLEDPin, 255);
270 analogWrite(blueLEDPin, 0);
271 Serial.println("YELLOW");
272 break;
273 case 6: // white
274 analogWrite(redLEDPin, 255);
275 analogWrite(greenLEDPin, 255);
276 analogWrite(blueLEDPin, 255);
277 Serial.println("WHITE");
278 break;
279 default:
280 //todo: add more colours - default is shown for all strobe effects currently
281 analogWrite(redLEDPin, 0);
282 analogWrite(greenLEDPin, 0);
283 analogWrite(blueLEDPin, 0);
284 Serial.println("DEFAULT");
285 break;
286 }
287}
const int greenLEDPin
Pin number for the green LED.
Definition Main.cpp:60
const int redLEDPin
Pin number for the red LED.
Definition Main.cpp:67
const int blueLEDPin
Pin number for the blue LED.
Definition Main.cpp:53

References blueLEDPin, greenLEDPin, and redLEDPin.

Referenced by useTimelineData().

Here is the caller graph for this function:

◆ getColours()

int * Playing::getColours ( )

Returns the array of LED colors.

This method returns a pointer to the array containing LED colors extracted from the timeline data.

Returns
Pointer to the array of LED colors.
132{
133 return colours;
134}
int colours[50]
Array storing colors extracted from JSON.
Definition Playing.h:95

References colours.

◆ getMaxTimingsNum()

int Playing::getMaxTimingsNum ( )

Returns the maximum number of timings in the timeline.

This method returns the maximum number of timings available in the timeline data.

Returns
Maximum number of timings in the timeline.
156{
157 return maxTimingsNum;
158}
int maxTimingsNum
Number of timings in the timeline.
Definition Playing.h:33

References maxTimingsNum.

◆ getTimings()

long * Playing::getTimings ( )

Returns the array of timings.

This method returns a pointer to the array containing timings extracted from the timeline data.

Returns
Pointer to the array of timings.
144{
145 return timings;
146}
long timings[50]
Array storing timings extracted from JSON.
Definition Playing.h:90

References timings.

◆ loadTimeline()

String Playing::loadTimeline ( )

Loads the timeline data from the disk.

This method loads the timeline data from the disk using the LittleFS (Little File System). If LittleFS initialization succeeds and the file exists, the method reads the data from the file, processes it using the processTimelineData() method, and returns the timeline data as a string. If the file doesn't exist or if there's any failure during the file system operation, an empty string is returned.

Returns
A string containing the loaded timeline data, or an empty string if the data couldn't be loaded.
172{
173 Serial.print("Loading Timeline from LittleFS ");
174 Serial.println(timelineFilePath);
175 String timelineData = "";
176 if (LittleFS.begin())
177 {
178 if (LittleFS.exists(timelineFilePath))
179 {
180 File file = LittleFS.open(timelineFilePath, "r");
181 if (file)
182 {
183 timelineData = file.readString();
184 file.close();
185 Serial.println("Timeline data:");
186 Serial.println(timelineData);
187 processTimelineData(timelineData); // Process the timeline data. Todo: error here?
188 }
189 }
190 LittleFS.end();
191 }
192 return timelineData;
193}
void processTimelineData(const String &timelineData)
Processes the timeline data received from the server.
Definition Playing.cpp:41
String timelineFilePath
File path of the timeline file.
Definition Playing.h:48

References processTimelineData(), and timelineFilePath.

Referenced by setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ play()

void Playing::play ( )

Plays the timeline data.

This method plays the timeline data by repeatedly calling the useTimelineData() method, which changes LED colors over time according to the timeline data.

403{
404 // int num_timings = getMaxTimingsNum();
405 // // Print the colours array
406 // Serial.print("Colours in memory: ");
407 // for (int i = 0; i < num_timings; i++)
408 // {
409 // Serial.print(colours[i]);
410 // Serial.print(" ");
411 // }
412 // Serial.println(); // Print a new line after the array
413 // // Print the timeline array
414 // Serial.print("Timings in memory: ");
415 // for (int i = 0; i < num_timings; i++)
416 // {
417 // Serial.print(timings[i]);
418 // Serial.print(" ");
419 // }
420 // Serial.println(); // Print a new line after the array
422}
void useTimelineData()
Uses the timeline data to change LED colors over time.
Definition Playing.cpp:300

References useTimelineData().

Referenced by loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processTimelineData()

void Playing::processTimelineData ( const String & timelineData)

Processes the timeline data received from the server.

This method processes the timeline data received from the server. It deserializes the JSON data, extracts key-value pairs, and stores the LED colors and timings for each event in the timeline.

Parameters
timelineDataThe timeline data received from the server.
42{
43 // Serial.println("processTimelineData");
44 // DynamicJsonDocument led_doc(1500);
45 deserializeJson(led_doc, timelineData);
46 root = led_doc.as<JsonObject>();
47
48 iter = 0;
49 for (JsonPair kv : root)
50 {
51 // String key = kv.key().c_str();
52 // JsonObject subDict = kv.value().as<JsonObject>();
53 // iter = 0;
54 // // Process the sub-dictionary
55 // for (JsonPair subKv : subDict) {
56 Serial.print("got ");
57 Serial.println(kv.key().c_str());
58 timeline_keys[iter] = kv.key().c_str(); // should be the last one here..
59 iter++;
60 // }
61 }
62 Serial.print("iter: ");
63 Serial.print(iter);
64
66
67 for (int i = 0; i < iter; i++)
68 {
69 if (strcmp(timeline_keys[i], "") == 0)
70 {
71 Serial.println("end");
72 }
73 else
74 {
75 redVal = led_doc[timeline_keys[i]][0];
76 greenVal = led_doc[timeline_keys[i]][1];
77 blueVal = led_doc[timeline_keys[i]][2];
78
79 redInt = redVal.as<int>();
80 // initialise arrays for timings:
81 colours[i] = redInt;
82 timings[i] = atol(timeline_keys[i]);
83
84 // if (redInt == 13) {
85 // Serial.println("LUCKY 13!!!");
86 // }
87 greenInt = greenVal.as<int>();
88 blueInt = blueVal.as<int>();
89 Serial.print("got colours for ");
90 Serial.print(timeline_keys[i]);
91 Serial.print(": ");
92 Serial.print(redInt);
93 Serial.print(", ");
94 Serial.print(greenInt);
95 Serial.print(", ");
96 Serial.println(blueInt);
97 }
98 }
99 // todo: errors from here?
100
101 // // Print the colours array
102 // Serial.print("Colours from disk: ");
103 // for (int i = 0; i < iter; i++)
104 // {
105 // Serial.print(colours[i]);
106 // Serial.print(" ");
107 // }
108 // Serial.println(); // Print a new line after the array
109 // // Print the timeline array
110 // Serial.print("Timings from disk: ");
111 // for (int i = 0; i < iter; i++)
112 // {
113 // Serial.print(timings[i]);
114 // Serial.print(" ");
115 // }
116 // Serial.println(); // Print a new line after the array
117 already_got_data = true;
118 digitalWrite(led, LOW);
119
120 // todo: test:
121 playStartTime = millis();
122}
#define led
Pin number for the built-in LED.
Definition Main.cpp:46
JsonVariant redVal
Red color value extracted from JSON.
Definition Playing.h:60
const char * timeline_keys[50]
Array of keys in the timeline JSON.
Definition Playing.h:55
int greenInt
Green color value as an integer.
Definition Playing.h:80
JsonObject root
JSON object root.
Definition Playing.h:110
JsonVariant blueVal
Blue color value extracted from JSON.
Definition Playing.h:70
JsonVariant greenVal
Green color value extracted from JSON.
Definition Playing.h:65
bool already_got_data
Flag indicating whether timeline data has been loaded.
Definition Playing.h:100
long playStartTime
Timestamp when timeline playing started.
Definition Playing.h:105
int blueInt
Blue color value as an integer.
Definition Playing.h:85
int iter
Iterator for timeline data.
Definition Playing.h:115
int redInt
Red color value as an integer.
Definition Playing.h:75

References already_got_data, blueInt, blueVal, colours, greenInt, greenVal, iter, led, maxTimingsNum, playStartTime, redInt, redVal, root, timeline_keys, and timings.

Referenced by loadTimeline(), and setup().

Here is the caller graph for this function:

◆ setup()

bool Playing::setup ( )

Sets up the playing process.

This method sets up the playing process by initializing necessary components, loading timeline data from the disk, and processing it.

Returns
true if setup is successful, false otherwise.
205{
206 digitalWrite(led, HIGH);
207 // Simulate playing process
208 Serial.println("Playing...");
209 // Main function of the app here
210 String currentTimelineData = loadTimeline();
211 Serial.print("Got timeline Data from disk: ");
212 Serial.println(currentTimelineData);
213 processTimelineData(currentTimelineData);
214 return true;
215}
String loadTimeline()
Loads the timeline data from the disk.
Definition Playing.cpp:171

References led, loadTimeline(), and processTimelineData().

Referenced by handleAuthenticationAndLoading().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ useTimelineData()

void Playing::useTimelineData ( )

Uses the timeline data to change LED colors over time.

This method uses the timeline data to change LED colors over time. It checks if it's time to change colors based on the timings in the timeline data. If it's time, it changes the colors according to the current index in the timeline data and moves to the next index. If the end of the timeline data is reached, it loops back to the beginning.

301{
302 // Get the current time
303 currentMillis2 = millis();
304
305 // Check if it's time to change colors
307 // Update the previousMillis for the next iteration
309 Serial.print(timings[currentIndex]);
310 Serial.print(": ");
311 // Change the colors based on the current index
313
314 // Move to the next index
315 currentIndex++;
316
317 // If we reach the end of the array, loop back to the beginning
319 currentIndex = 0;
320 }
321 }
322// Serial.println("play");
323// // test:
324// if (playing)
325// {
326// Serial.println("checking Timeline here");
327// if (runNum > maxTimingsNum - 2) // todo: ???
328// {
329// // Serial.println("initialising runNum and playStartTime");s
330// runNum = 0;
331// playStartTime = millis();
332// }
333// // another sanity check:
334// if (timings[runNum] > 0)
335// {
336// Serial.print("Checking timing for ");
337// Serial.print(timings[runNum]);
338// currentMillis2 = millis() - playStartTime; // reset the clock! At the beginning of play this should be 0!
339
340// if (currentMillis2 < timings[0])
341// {
342// Serial.print("< ");
343// Serial.println(runNum);
344// runNum++;
345// }
346// // I think currently timings[] array must be in time order to work. If the last time is smaller than the previous nothing happens? todo: sort?
347// else if (currentMillis2 >= timings[runNum] && currentMillis2 <= timings[runNum + 1]) // todo: this omits the last signal..?
348// { // saved signal
349// // Serial.print("== ");
350// Serial.print(runNum);
351// Serial.print(" of: ");
352// Serial.println(maxTimingsNum - 1);
353// // sanity check:
354// // if (colours[runNum] < 13)
355// // { // max is 22 otherwise it's a bogus signal
356// signal = colours[runNum];
357// // long time = timings[runNum];
358// // flashy = flashes[runNum];
359// Serial.print("colour is set to: ");
360// Serial.println(colours[runNum]);
361// Serial.print("timings is set to: ");
362// Serial.println(timings[runNum]);
363// changeColours(colours[runNum]);
364// runNum++;
365// // todo: deal with colours here - separate funciton
366// // }
367// }
368// else if (currentMillis2 >= timings[runNum + 1])
369// {
370// runNum = maxTimingsNum; // went over - trigger reset
371// }
372// else
373// {
374// // test code:
375// // Serial.println("what else? ");
376// // Serial.println("runNum: ");
377// // Serial.println(runNum);
378// // Serial.print("millis: ");
379// // Serial.println(millis());
380// // Serial.print("currentMillis2: ");
381// // Serial.println(currentMillis2);
382// // Serial.print("timings[runNum]: ");
383// // Serial.println(timings[runNum]);
384// // Serial.print("timings[runNum + 1]: ");
385// // Serial.println(timings[runNum + 1]);
386
387// playStartTime = millis();
388// Serial.println("resetting playStartTime");
389// runNum++;
390// }
391// }
392// }
393}
long previousMillis
Timestamp of the last LED update.
Definition Playing.h:125
long currentMillis2
Current timestamp.
Definition Playing.h:130
int currentIndex
Current index in the timeline data arrays.
Definition Playing.h:145
void changeColours(int choice)
Changes the colors of the RGB LED based on the provided choice.
Definition Playing.cpp:233

References changeColours(), colours, currentIndex, currentMillis2, maxTimingsNum, previousMillis, and timings.

Referenced by play().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ maxTimingsNum

int Playing::maxTimingsNum = 50
private

Number of timings in the timeline.

This variable stores the maximum number of timings in the timeline. It is used to limit the size of the timings array.

Referenced by getMaxTimingsNum(), processTimelineData(), and useTimelineData().

◆ timeline_keys

const char* Playing::timeline_keys[50]
private

Array of keys in the timeline JSON.

This array stores the keys extracted from the timeline JSON data.

Referenced by processTimelineData().

◆ timelineFilePath

String Playing::timelineFilePath = "/timeline" + timelineNumber + ".txt"
private

File path of the timeline file.

This variable stores the file path of the timeline data file.

Referenced by loadTimeline().

◆ timelineNumber

String Playing::timelineNumber = "0"
private

Variable to use int tinelineFilePath.

This variable is a number which allows mulitple timelines to be stored in sequence.


The documentation for this class was generated from the following files: