Created
September 4, 2017 15:23
-
-
Save gregorthebigmac/8e8948967d3aeec1a1a85818ec112afa to your computer and use it in GitHub Desktop.
raspberry_pi_batt_test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
write_to_end_log() takes an int argument because this **WILL** run until the battery | |
expires, and the machine shuts down. Because of this, we want to make sure we don't | |
lose data because it was trying to write to a file during power failure. This is for | |
testing the RPi's battery consumption to get real-world data. It's MEANT to go until | |
battery failure, and this is only for testing purposes. Therefore, we will write to | |
two files, and alternate between writing to them, just in case the power should fail | |
DURING the writing process. We only need to know the startup time and the time of the | |
last file write, so we keep one startup-log file, and two end-log files, just in case. | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <ctime> | |
#include <unistd.h> | |
using std::endl; | |
using std::ifstream; | |
using std::ofstream; | |
using std::string; | |
int write_to_start_log(int error_counter, string path); | |
int write_to_end_log(int file_number, int error_counter, string path); | |
int main(int argc, char *argv[]) { | |
string path = "/home/pi/cpp/batt_logs/"; | |
int log_file_switcher = 1; // this number should only ever be 1 or 2. | |
int start_log_error_counter = write_to_start_log(0, path); | |
int end_log_error_counter = write_to_end_log(log_file_switcher, 0, path); | |
while(true) { | |
// todo: if either error_counter goes above 3, then either abort entirely, or | |
// do some kind of error handling at the OS Level for failing to open a file. | |
if (log_file_switcher == 1) { | |
log_file_switcher++; | |
} | |
else if (log_file_switcher == 2) { | |
log_file_switcher--; | |
} | |
else { | |
log_file_switcher = 1; | |
} | |
end_log_error_counter = write_to_end_log(log_file_switcher, end_log_error_counter, path); | |
usleep(60000000); // wait one minute between file writes (1,000,000 usec = 1 sec) | |
} | |
return 0; | |
} | |
int write_to_start_log(int error_counter, string path) { | |
ofstream fout; | |
string filename = path + "start_log.txt"; | |
fout.open(filename.c_str()); | |
if (fout.is_open()) { | |
time_t t0 = time(0); | |
struct tm * now = localtime ( & t0 ); | |
int year = now->tm_year + 1900; | |
int month = now->tm_mon + 1; | |
int day = now->tm_mday; | |
int hour = now->tm_hour; | |
int min = now->tm_min; | |
int sec = now->tm_sec; | |
fout << "Start Date/Time: " << year << "-"; | |
if (month < 10) { | |
fout << "0"; | |
} | |
fout << month << "-"; | |
if (day < 10) { | |
fout << "0"; | |
} | |
fout << day << " "; | |
if (hour < 10) { | |
fout << "0"; | |
} | |
fout << hour << ":"; | |
if (min < 10) { | |
fout << "0"; | |
} | |
fout << min << ":"; | |
if (sec < 10) { | |
fout << "0"; | |
} | |
fout << sec << endl; | |
fout.close(); | |
} | |
else { | |
return ++error_counter; | |
} | |
return error_counter; | |
} | |
int write_to_end_log(int file_number, int error_counter, string path) { | |
ofstream fout; | |
string filename = path + "end_log"; | |
if (file_number == 1) { | |
filename.append("1.txt"); | |
} | |
else if (file_number == 2) { | |
filename.append("2.txt"); | |
} | |
else { | |
// todo: error handling for invalid switcher. This SHOULD never happen. | |
} | |
fout.open(filename.c_str()); | |
if (fout.is_open()) { | |
time_t t0 = time(0); | |
struct tm * now = localtime ( & t0 ); | |
int year = now->tm_year + 1900; | |
int month = now->tm_mon + 1; | |
int day = now->tm_mday; | |
int hour = now->tm_hour; | |
int min = now->tm_min; | |
int sec = now->tm_sec; | |
fout << "End Date/Time: " << year << "-"; | |
if (month < 10) { | |
fout << "0"; | |
} | |
fout << month << "-"; | |
if (day < 10) { | |
fout << "0"; | |
} | |
fout << day << " "; | |
if (hour < 10) { | |
fout << "0"; | |
} | |
fout << hour << ":"; | |
if (min < 10) { | |
fout << "0"; | |
} | |
fout << min << ":"; | |
if (sec < 10) { | |
fout << "0"; | |
} | |
fout << sec << endl; | |
} | |
else { | |
return ++error_counter; | |
} | |
return error_counter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment