// Inculde Library
# include <Wire.h>
# include <Adafruit_GFX.h>
# include <Adafruit_SSD1306.h>
// Define the Screen Measurment AND Dislay
# define SCREEN_WIDTH 128 // OLED display width, in pixels
# define SCREEN_HEIGHT 64 // OLED display height, in pixels
# define OLED_RESET -1 // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define variables for counting time
unsigned long start_time;
unsigned long elapsed_time = 0;
// Functions declared
void vInitOLED();
void vDisplayStatus(int xDeviceStatus);
void vDisplayTime();
void vDisplayChargeIcon();
void vReadVoltageValue();
void vDisplayVoltageValue(float voltage);
// 'Battery Symbol', 17x9px
const unsigned char epd_bitmap_Battery_Symbol [] PROGMEM = {
0x7f, 0xfe, 0x00, 0x80, 0x02, 0x00, 0x80, 0x03, 0x00, 0x83, 0x13, 0x80, 0x87, 0xe3, 0x80, 0x89,
0x83, 0x80, 0x80, 0x03, 0x00, 0x80, 0x02, 0x00, 0x7f, 0xfe, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 48)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
epd_bitmap_Battery_Symbol
};
float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void vInitOLED() {
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
while (true);
}
// Clear the display
display.clearDisplay();
display.display();
// Set the start time
start_time = millis();
}
// ===========================================================================================
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
Wire.begin();
vInitOLED();
}
// ===========================================================================================
void loop() {
// Calculate the elapsed time
elapsed_time = (millis() - start_time) / 1000;
// Clear the display
display.clearDisplay();
vDisplayStatus(1);
vReadVoltageValue();
vDisplayChargeIcon();
vDisplayTime();
display.display();
}
// ===========================================================================================
// Display the charging status
void vDisplayStatus(int xDeviceStatus) {
display.setTextSize(1.95);
display.setCursor(0, 20);
display.println("STATUS");
// Display the status text based on the input parameter
switch (xDeviceStatus) {
case 0:
display.setCursor(40, 20);
display.println(": Charging");
break;
case 1:
display.setCursor(40, 20);
display.println(": Discharging");
break;
default:
display.setCursor(40, 20);
display.println(": OFF");
break;
}
}
// Display the elapsed time on the display
void vDisplayTime() {
display.setTextSize(1.95);
display.setTextColor(WHITE);
display.setCursor(0, 40);
display.printf("Time - %02d:%02d:%02d", elapsed_time / 3600, (elapsed_time / 60) % 60, elapsed_time % 60);
}
// Display the image of battery
void vDisplayChargeIcon() {
display.drawBitmap(110, 0, epd_bitmap_Battery_Symbol, 17, 9, WHITE);
}
// Read the value
void vReadVoltageValue() {
// read the input on analog pin GIOP36:
int analogValue = analogRead(A0);
// Rescale to potentiometer's voltage (from 0V to 3.3V):
float voltage = floatMap(analogValue, 0, 4095, 0, 3.3);
vDisplayVoltageValue(voltage);
}
// Display the volts after reading
void vDisplayVoltageValue(float voltage) {
// Display the volts
display.setTextSize(1.95);
display.setCursor(0,0);
display.println("Volt - ");
display.setCursor(40,0);
display.print(voltage);
}