Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Dec 12, 2022
1 parent e64cbe0 commit 7fd2483
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
54 changes: 37 additions & 17 deletions src/clock_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ char ssid[] = "router"; // network SSID (name)
char pass[] = "password"; // for networks that require a password
int status = WL_IDLE_STATUS; // the WiFi radio's status

/**
* @brief attempts to connect to the worldtimeapi.org website
* @retval true if connection successful, false if unsuccessful
*/
bool connect_to_webpage()
{
Serial.println("Attempting to connect to webpage");
Expand All @@ -36,6 +40,9 @@ bool connect_to_webpage()
}
}

/**
* @brief connects to a wifi network, call on startup
*/
void setup_wifi()
{
// attempt to connect to WiFi network:
Expand All @@ -58,18 +65,24 @@ void setup_wifi()
}
}

/**
* @brief
* @retval true if string containing time has been stored
*/
bool read_webpage()
{
int len = client.available();
if (len == 0) {
return false;
}
memset(buffer, 0x0, sizeof(buffer));
int index = 0;
unsigned int index = 0;
while (client.available()) {
char c = client.read();
char c = client.read(); // read data from webpage
buffer[index] = c;
index++;
if (index < sizeof(buffer) - 1) { // prevent buffer overflow
index++;
}
}
timeSinceStart = millis();
Serial.println("Content received");
Expand All @@ -81,18 +94,27 @@ bool read_webpage()
memset(startTimeBuf, 0x0, sizeof(startTimeBuf));
int i = 22;
int k = 0;
while (i <= 29) {
while (i <= 29) { // read the part of the response containing the time into a smaller buffer
startTimeBuf[k] = response[i];
i++;
k++;
}
} else {
return false; // "datetime" not found in response
}
interrupts();
delay(500);
delay(500); // don't spam the API too fast if webpage_read takes more than one attempt
} else {
return false; // no data read; index==0
}
return true;
}

/**
* @brief converts a string representation of time to an integer value of seconds
* @param t: char* string representing time, must be 8 characters, ex: 16:31:02
* @retval integer value of seconds since midnight.
*/
int stringToTimeInt(char* t)
{
char hours[2];
Expand All @@ -110,14 +132,22 @@ int stringToTimeInt(char* t)
return (hoursInt * 60 * 60) + (minsInt * 60) + secsInt;
}

char* getStartTime()
/**
* @brief connects to wifi and saves a string representing the time.
* @note blocks until wifi connects and webpage returns a good response
* @retval pointer to global variable that contains the time represented as a string
*/
void getStartTime()
{
setup_wifi();
while (!read_webpage())
;
return startTimeBuf;
}

/**
* @brief This function uses millis() to calculate the current time without any more connections to a time API.
* @retval pointer to global variable string containing the current time in 24 hour time
*/
char* getCurrentTime()
{
int curMillis = millis();
Expand All @@ -132,14 +162,4 @@ char* getCurrentTime()
sprintf(timeBuf, "%02d:%02d:%02d", hours % 24, mins, secs);
return timeBuf;
}

// void setup() {
// Serial.begin(9600);
// while(!Serial);
// Serial.println(getStartTime());
// }
// void loop() {
// Serial.println(getCurrentTime());
// delay(1000);
// }
#endif
3 changes: 1 addition & 2 deletions src/unit_tests.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef UNIT_TESTS_H
#define UNIT_TESTS_H
#include "Printable.h"
#include "fsm_types.h"
#include <Arduino.h>

Expand Down Expand Up @@ -266,7 +265,7 @@ void runAllTests()
if (passed) {
Serial.println("All tests passed!");
} else {
Serial.println("All tests completed, some failed :(");
Serial.println("All tests run, some failed :(");
}
while (true)
;
Expand Down

0 comments on commit 7fd2483

Please sign in to comment.