#include <Arduino.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <SD.h>
#include <SPI.h>
// Define for OLED and RTC Modules
#define I2C_SDA 21 //OLED and DS3231: SDA --> GPIO21
#define I2C_SCL 22 //OLED and DS3231: SCL --> GPIO22
#define OLED_I2C_ADDRESS 0x3c //OLED I2C Address
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G OLEDdisplay = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int CS = 5;
// Config file content
myFile config;
String iniContent = "";
// Variable to keep track of med info
String pills[3];
String when[3];
String notif[3][4];
void setup() {
Serial.begin(115200);
// SD card initialize
if (!SD.begin(CS))
{
Serial.println(("SD card Initialization falied"));
return;
}
// Read config file
myFile = SD.open("/config.ini", FILE_READ);
if (myFile)
{
Serial.println("config.ini is opened");
// Read all the content in the file and save it to a string
while (myFile.available()) {
char c = myFile.read();
iniContent += c; // Append each character to the iniContent variable
}
// Close the file
myFile.close();
Serial.println("File closed.");
// Output the content stored in the iniContent variable
Serial.println("File contents:");
Serial.println(iniContent);
}
else
{
Serial.println("Error opening config.in");
}
Serial.println("Initialization done.");
Serial.println("*********************************************");
}
// Save medicines information in arrays
parseIni(iniContent);
printMedications(); // Print the parsed medications
// Initialize OLED display
Serial.println("Start OLED Display...");
OLEDdisplay.begin(OLED_I2C_ADDRESS, true);
OLEDdisplay.setContrast(0); // dim display
OLEDdisplay.display();
Wire.begin(I2C_SDA, I2C_SCL);
OLEDdisplay.clearDisplay();
OLEDdisplay.setTextSize(1);
OLEDdisplay.setTextColor(SH110X_WHITE);
OLEDdisplay.setCursor(0, 0);
}
void loop() {
// Display medicines info looping in order.
static int currentMed = 0;
OLEDdisplay.clearDisplay();
OLEDdisplay.setCursor(0, 0);
OLEDdisplay.setTextSize(1);
OLEDdisplay.setTextColor(SH110X_WHITE);
OLEDdisplay.print("Pill: ");
OLEDdisplay.println(pills[currentMed]);
OLEDdisplay.print("Time: ");
displayTimes(notif[currentMed]);
OLEDdisplay.print("When: ");
OLEDdisplay.println(when[currentMed]);
OLEDdisplay.display();
delay(5000);
currentMed = (currentMed + 1) % 3;
}
void displayTimes(String notif[]) {
int count = 0;
for (int i = 0; i < 4; i++) {
if (notif[i].length() > 0) {
if (count == 2) {
OLEDdisplay.println();
OLEDdisplay.print(" ");
}
OLEDdisplay.print(notif[i]);
if (i < 3 && notif[i + 1].length() > 0) {
OLEDdisplay.print(", ");
}
count++;
}
}
OLEDdisplay.println();
}
void parseIni(String ini) {
String lines[12];
int lineCount = 0;
int startIndex = 0;
int endIndex = ini.indexOf('\n');
while (endIndex != -1) {
lines[lineCount++] = ini.substring(startIndex, endIndex);
startIndex = endIndex + 1;
endIndex = ini.indexOf('\n', startIndex);
}
lines[lineCount++] = ini.substring(startIndex);
int medIndex = -1;
for (int i = 0; i < lineCount; i++) {
if (lines[i].startsWith("[")) {
medIndex++;
} else if (lines[i].startsWith("Pill")) {
pills[medIndex] = getValue(lines[i]);
} else if (lines[i].startsWith("Notif_time")) {
fillNotifArray(notif[medIndex], lines[i]);
} else if (lines[i].startsWith("When")) {
when[medIndex] = getValue(lines[i]);
}
}
}
String getValue(String line) {
return line.substring(line.indexOf('=') + 2);
}
void fillNotifArray(String notif[], String line) {
for (int i = 0; i < 4; i++) {
notif[i] = "";
}
int startIndex = line.indexOf('=') + 2;
int endIndex = line.indexOf(',', startIndex);
int index = 0;
while (endIndex != -1 && index < 4) {
notif[index++] = line.substring(startIndex, endIndex);
startIndex = endIndex + 2;
endIndex = line.indexOf(',', startIndex);
}
if (startIndex < line.length() && index < 4) {
notif[index] = line.substring(startIndex);
}
}
void printMedications() {
for (int i = 0; i < 3; i++) {
Serial.print("Medication ");
Serial.println(i + 1);
Serial.print("Pill: ");
Serial.println(pills[i]);
Serial.print("When: ");
Serial.println(when[i]);
Serial.print("Notification Times: ");
for (int j = 0; j < 4; j++) {
Serial.print(notif[i][j]);
if (j < 3) Serial.print(", ");
}
Serial.println("\n");
}
}