diff --git a/clock_settings.h b/clock_settings.h new file mode 100644 index 0000000..9080a32 --- /dev/null +++ b/clock_settings.h @@ -0,0 +1,9 @@ +#ifndef CLOCK_SETTINGS_H +#define CLOCK_SETTINGS_H + +class ClockSettings +{ + //TODO +}; + +#endif //CLOCK_SETTINGS_H diff --git a/lamp.ino b/lamp.ino new file mode 100644 index 0000000..e83c9cc --- /dev/null +++ b/lamp.ino @@ -0,0 +1,18 @@ +#include "settings.h" + +Settings const* settings = NULL; + +void setup() { + Serial.begin(9600); + + Serial.print("\nLoading settings..."); + + settings = Settings::load("settings.ini"); + if(settings == NULL) + Serial.println(" NOT OK"); + else + Serial.println(" OK"); +} + +void loop() { +} diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..20b4c20 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,147 @@ +#include "settings.h" + +#include + +bool is_whitespace(char const c) +{ + return c == ' ' || c == '\t'; +} + +bool is_printable(char const c) +{ + //cf. ASCII table + return c >= 0x20 && c <= 0x7E; +} + +Settings::Settings() +{ +} + +Settings::~Settings() +{ +} + +void Settings::add_line(String const& section, String const& key, String const& val) +{ + Serial.print("Section: \""); + Serial.print(section); + Serial.print("\", Key: \""); + Serial.print(key); + Serial.print("\", Val: \""); + Serial.print(val); + Serial.println("\""); +} + +Settings const* Settings::load(char const* settings_filename) +{ + Serial.println(""); //TODO: remove debug message + if(!SD.begin(D8)) + { + Serial.println("cannot open SD card"); + return NULL; + } + + File file = SD.open("settings.ini", FILE_READ); + if(!file) + { + Serial.println("cannot open settings file"); + return NULL; + } + + Settings* settings = new Settings(); + + Serial.println("----Settings----"); //TODO: remove debug message + + bool first_char_of_line = true; + bool is_comment_line = false; + bool is_section_line = false; + unsigned int nb_whitespace = 0; //used for trimming the end of lines + + String current_section = ""; + String current_key = ""; + String current_val = ""; + + bool write_to_key = true; + + while(file.available()) + { + char const c = file.read(); + if(!is_printable(c)) continue; + + if(first_char_of_line) + { + //trim whitespace at beginning of line + if(is_whitespace(c)) continue; + + first_char_of_line = false; + + //check if comment, section or normal line + if(c == ';') + { + is_comment_line = true; + continue; + } + else if (c == '[') + { + is_section_line = true; + current_section = ""; + continue; + } + } + + if(c == '\n') + { + first_char_of_line = true; + is_comment_line = false; + is_section_line = false; + + settings->add_line(current_section, current_key, current_val); + + current_key = ""; + current_val = ""; + write_to_key = true; + } + else if(is_comment_line) + { + //discarding comment lines + continue; + } + else if(is_section_line) + { + if(c == ']') + { + //discard end of line + is_section_line = false; + is_comment_line = true; + } + else + { + current_section += c; + } + } + else + { + if(is_whitespace(c)) + ++nb_whitespace; + else if(write_to_key && c == '=') + { + write_to_key = false; + } + else + { + if(write_to_key) + current_key += c; + else + current_val += c; + } + } + } + if(!first_char_of_line && !is_comment_line && !is_section_line) + settings->add_line(current_section, current_key, current_val); + + file.close(); + + Serial.println("----End settings----"); //TODO: remove debug log + + return settings; +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..fc54e26 --- /dev/null +++ b/settings.h @@ -0,0 +1,22 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include "Arduino.h" +#include "wifi_settings.h" +#include "clock_settings.h" + +class Settings +{ + private: + Settings(); + ~Settings(); + + void add_line(String const& section, String const& key, String const& val); + + public: + static Settings const* load(char const* settings_filename); + + protected: +}; + +#endif //SETTINGS_H diff --git a/settings.ini b/settings.ini new file mode 100644 index 0000000..1759cc1 --- /dev/null +++ b/settings.ini @@ -0,0 +1,22 @@ +; Configuration of the WiFi connection. +; If multiple connections are needed, multiple sections can be added as follows : +; +; [wifi:1] +; ssid=SSID_1 +; password=PASS_1 +; [wifi:2] +; ssid=SSID_2 +; password=PASS_2 +; +; Note: The [wifi] section without number is synonymous to [wifi:0] +[wifi] +ssid=WIFI_SSID +password=WIFI_PASSWORD + +; Configuration of the NTP connection. +; Since the RTC module is here to do most of the time keeping, +; the NTP requests can be quite far in between +[clock] +server=pool.ntp.org +; time between NTP requests, in minutes +cooldown=15 diff --git a/wifi_settings.h b/wifi_settings.h new file mode 100644 index 0000000..b568ee4 --- /dev/null +++ b/wifi_settings.h @@ -0,0 +1,10 @@ +#ifndef WIFI_SETTINGS_H +#define WIFI_SETTINGS_H + +class WifiSettings +{ + public: + //TODO +}; + +#endif //WIFI_SETTINGS_H