Browse Source

Load settings from SD card

master
n0m1s 6 years ago
parent
commit
864de0f9b8
6 changed files with 228 additions and 0 deletions
  1. +9
    -0
      clock_settings.h
  2. +18
    -0
      lamp.ino
  3. +147
    -0
      settings.cpp
  4. +22
    -0
      settings.h
  5. +22
    -0
      settings.ini
  6. +10
    -0
      wifi_settings.h

+ 9
- 0
clock_settings.h View File

@ -0,0 +1,9 @@
#ifndef CLOCK_SETTINGS_H
#define CLOCK_SETTINGS_H
class ClockSettings
{
//TODO
};
#endif //CLOCK_SETTINGS_H

+ 18
- 0
lamp.ino View File

@ -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() {
}

+ 147
- 0
settings.cpp View File

@ -0,0 +1,147 @@
#include "settings.h"
#include <SD.h>
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;
}

+ 22
- 0
settings.h View File

@ -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

+ 22
- 0
settings.ini View File

@ -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

+ 10
- 0
wifi_settings.h View File

@ -0,0 +1,10 @@
#ifndef WIFI_SETTINGS_H
#define WIFI_SETTINGS_H
class WifiSettings
{
public:
//TODO
};
#endif //WIFI_SETTINGS_H

Loading…
Cancel
Save