Magic Poi Lite 0.1
IOT LED Poi
Loading...
Searching...
No Matches
Macros | Functions | Variables
Main.cpp File Reference

Main program file. More...

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <EEPROM.h>
#include "secrets.h"
#include "Authentication.h"
#include "Loading.h"
#include "Playing.h"
Include dependency graph for Main.cpp:

Macros

#define led   D4
 Pin number for the built-in LED.
 

Functions

void ICACHE_RAM_ATTR handleUpdateInterrupt ()
 Interrupt service routine for handling update button press.
 
void handleAuthenticationAndLoading ()
 Handles authentication and loading of timeline data.
 
void setup ()
 Sets up the system including WiFi connection, authentication, and loading and playing timeline data.
 
void loop ()
 Main loop of the program.
 

Variables

Authentication authentication
 Instance of the Authentication class.
 
Loading loading
 Instance of the Loading class.
 
Playing playing
 Instance of the Playing class.
 
const int blueLEDPin = D5
 Pin number for the blue LED.
 
const int greenLEDPin = D6
 Pin number for the green LED.
 
const int redLEDPin = D7
 Pin number for the red LED.
 
const int btnUpdatePin = D1
 Pin number for the update Button.
 
const int btnStartPin = D2
 Pin number for the start Button.
 
ESP8266WiFiMulti WiFiMulti
 WiFi multi-client manager for ESP8266.
 
volatile bool updateRequested = false
 Flag indicating whether an update is requested.
 

Detailed Description

Main program file.

The main program - includes setup() and loop(). Currently set to connect to Magic Poi Lite API on startup and fetch the current timeline. Then display the timeline on RGB LEDs according to timings.

Macro Definition Documentation

◆ led

#define led   D4

Pin number for the built-in LED.

This constant represents the pin number for the built-in LED on the D1 Mini.

Referenced by Playing::processTimelineData(), setup(), and Playing::setup().

Function Documentation

◆ handleAuthenticationAndLoading()

void handleAuthenticationAndLoading ( )

Handles authentication and loading of timeline data.

This function performs the process of checking for saved authentication tokens, handling authentication, and loading timeline data. It prints messages to indicate the status of these operations.

114 {
115 Serial.println("Setup: Connection & Authentication");
116
117 bool isAuthenticated = authentication.checkSavedToken();
118 isAuthenticated = false; // test re-auth - todo: tie this to a button press - todo: not working without this?
119
120 if (isAuthenticated) {
121 Serial.println("Authentication found. Using saved.");
122 if (loading.load()) {
123 Serial.println("LOADED AND SAVED TIMELINE SUCCESSFULLY");
124 if (playing.setup()) {
125 Serial.println("SETUP COMPLETE");
126 }
127 } else {
128 Serial.println("LOADING AND SAVING TIMELINE UNSUCCESSFUL");
129 }
130 } else {
131 bool isAuthenticating = authentication.authenticate();
132 if (isAuthenticating) {
133 isAuthenticated = authentication.checkSavedToken();
134 if (isAuthenticated) {
135 Serial.println("Authenticated using login success. Should be saved now, using..");
136 if (loading.load()) {
137 Serial.println("LOADED AND SAVED TIMELINE SUCCESSFULLY");
138 if (playing.setup()) {
139 Serial.println("SETUP COMPLETE");
140 }
141 } else {
142 Serial.println("LOADING AND SAVING TIMELINE UNSUCCESSFUL");
143 }
144 } else {
145 Serial.println("Auth check saved JWT after password login failed");
146 }
147 } else {
148 Serial.println("Failed to authenticate with password");
149 }
150 }
151}
Playing playing
Instance of the Playing class.
Definition Main.cpp:39
Loading loading
Instance of the Loading class.
Definition Main.cpp:32
Authentication authentication
Instance of the Authentication class.
Definition Main.cpp:25
bool checkSavedToken()
Checks for a saved JWT token and loads it if available.
Definition Authentication.cpp:177
bool authenticate()
Authenticates the client with the server.
Definition Authentication.cpp:117
bool load()
Loads data required for the application.
Definition Loading.cpp:181
bool setup()
Sets up the playing process.
Definition Playing.cpp:204

References Authentication::authenticate(), authentication, Authentication::checkSavedToken(), Loading::load(), loading, playing, and Playing::setup().

Referenced by loop(), and setup().

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

◆ handleUpdateInterrupt()

void ICACHE_RAM_ATTR handleUpdateInterrupt ( )

Interrupt service routine for handling update button press.

This function sets the updateRequested flag to true when the update button is pressed.

103 {
104 updateRequested = true;
105}
volatile bool updateRequested
Flag indicating whether an update is requested.
Definition Main.cpp:96

References updateRequested.

Referenced by setup().

Here is the caller graph for this function:

◆ loop()

void loop ( )

Main loop of the program.

This function is the main loop of the program. It checks for update requests and calls handleAuthenticationAndLoading() if an update is requested. It also calls the play function to execute the playing process, which involves changing LED colors over time according to the timeline data. Additionally, it adds a delay of 5 seconds between each play iteration to control the timing of the LED color changes.

200 {
201 if (updateRequested) {
202 updateRequested = false;
204 }
205
206 playing.play();
207 delay(5000); // Example: Delay for 5 seconds between each play
208}
void handleAuthenticationAndLoading()
Handles authentication and loading of timeline data.
Definition Main.cpp:114
void play()
Plays the timeline data.
Definition Playing.cpp:402

References handleAuthenticationAndLoading(), Playing::play(), playing, and updateRequested.

Here is the call graph for this function:

◆ setup()

void setup ( )

Sets up the system including WiFi connection, authentication, and loading and playing timeline data.

This function initializes the system by establishing a WiFi connection, setting up interrupt for update button, and calling handleAuthenticationAndLoading() to handle authentication and loading processes.

159 {
160 Serial.begin(115200);
161 pinMode(led, OUTPUT);
162 digitalWrite(led, HIGH); // HIGH is off for D1 mini
163
164 // Set the digital pins as outputs
165 pinMode(greenLEDPin, OUTPUT);
166 pinMode(redLEDPin, OUTPUT);
167 pinMode(blueLEDPin, OUTPUT);
168 pinMode(btnUpdatePin, INPUT_PULLUP);
169
170 // WiFi connection
171 WiFi.mode(WIFI_STA);
172 WiFiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
173 Serial.println("Connecting to WiFi");
174 while (WiFiMulti.run() != WL_CONNECTED) {
175 Serial.print(".");
176 delay(500);
177 }
178
179 Serial.println("");
180 Serial.println("WiFi connected");
181 Serial.println("IP address: ");
182 Serial.println(WiFi.localIP());
183
184 // Set up interrupt for update button
185 attachInterrupt(digitalPinToInterrupt(btnUpdatePin), handleUpdateInterrupt, FALLING);
186
187 // Handle authentication and loading
189}
const int greenLEDPin
Pin number for the green LED.
Definition Main.cpp:60
ESP8266WiFiMulti WiFiMulti
WiFi multi-client manager for ESP8266.
Definition Main.cpp:89
void ICACHE_RAM_ATTR handleUpdateInterrupt()
Interrupt service routine for handling update button press.
Definition Main.cpp:103
#define led
Pin number for the built-in LED.
Definition Main.cpp:46
const int redLEDPin
Pin number for the red LED.
Definition Main.cpp:67
const int btnUpdatePin
Pin number for the update Button.
Definition Main.cpp:75
const int blueLEDPin
Pin number for the blue LED.
Definition Main.cpp:53

References blueLEDPin, btnUpdatePin, greenLEDPin, handleAuthenticationAndLoading(), handleUpdateInterrupt(), led, redLEDPin, and WiFiMulti.

Here is the call graph for this function:

Variable Documentation

◆ authentication

Authentication authentication

Instance of the Authentication class.

This object is used for handling authentication operations.

Referenced by handleAuthenticationAndLoading().

◆ blueLEDPin

const int blueLEDPin = D5

Pin number for the blue LED.

This constant represents the pin number for the blue LED.

Referenced by Playing::changeColours(), and setup().

◆ btnStartPin

const int btnStartPin = D2

Pin number for the start Button.

This constant represents the pin number for the start Button.

◆ btnUpdatePin

const int btnUpdatePin = D1

Pin number for the update Button.

This constant represents the pin number for the update Button. Used for fetching content from the web.

Referenced by setup().

◆ greenLEDPin

const int greenLEDPin = D6

Pin number for the green LED.

This constant represents the pin number for the green LED.

Referenced by Playing::changeColours(), and setup().

◆ loading

Loading loading

Instance of the Loading class.

This object is used for loading timeline data.

Referenced by handleAuthenticationAndLoading().

◆ playing

Playing playing

Instance of the Playing class.

This object is used for playing timeline data.

Referenced by handleAuthenticationAndLoading(), and loop().

◆ redLEDPin

const int redLEDPin = D7

Pin number for the red LED.

This constant represents the pin number for the red LED.

Referenced by Playing::changeColours(), and setup().

◆ updateRequested

volatile bool updateRequested = false

Flag indicating whether an update is requested.

This flag is set to true when an update is requested via the update button interrupt.

Referenced by handleUpdateInterrupt(), and loop().

◆ WiFiMulti

ESP8266WiFiMulti WiFiMulti

WiFi multi-client manager for ESP8266.

This object is used for managing multiple WiFi connections on ESP8266.

Referenced by setup().