#include <GyverTimers.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
float voltage = 0.0;
int settime = 0;
int next = -1;
int tempnext = 0;
int selectbutton;
int timersecond = 0;
int confirmbutton;
int m;
int s;
String name = "BLAZE - ZERO";
String version = "v1.0";
String ending = "- Press Reset -";
String displaytime;
int progressbar = 1;
int progressflag = 0;
const unsigned char logo [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x7f, 0xf0, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x7f, 0xfc, 0x1c, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x3f, 0xfe, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x3f, 0xff, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1f, 0xff, 0x8f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1f, 0xff, 0x9f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x3f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x87, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0xff, 0xff, 0xff, 0xff, 0x9f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xef, 0xf9, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x0f, 0xf8, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x0f, 0xf8, 0x1f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x0f, 0xf8, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x0f, 0xf8, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x0f, 0xf8, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x03, 0xf0, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x00, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0x80, 0x00, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xc0, 0x00, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xe0, 0x01, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf0, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup()
{
Serial.begin(9600);
Timer1.setFrequency(10);
Timer1.enableISR();
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
oled.clearDisplay();
startMillis = millis();
pinMode(8, OUTPUT);digitalWrite(8, LOW);//Charger IC Enable
pinMode(7, OUTPUT);digitalWrite(7, HIGH);//Tine Set 2^1 bit / USB Hub Reset indicator
//pinMode(6, OUTPUT);digitalWrite(6, LOW);//Time Set 2^0 bit
//pinMode(5, OUTPUT);digitalWrite(5, HIGH);//Reset Indicator
pinMode(4, OUTPUT);digitalWrite(4, HIGH);//USB Hub Reset
//pinMode(3, OUTPUT);digitalWrite(3, HIGH);//OLED Turn ON
pinMode(10, INPUT_PULLUP);//Time Set Button
pinMode(9, INPUT_PULLUP);//Enable Timer Button
//oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); //or 0x3C
delay(500); // wait for initializing
digitalWrite(4, LOW);
mainpage();
}
void loop()
{
selectbutton = digitalRead(10);
confirmbutton = digitalRead(9);
optionselect();
if(next>=0) printtable();
execution();
}
void mainpage(){
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE, BLACK); // text color
oled.drawBitmap(0, 0, logo, 128, 64, WHITE);
oled.setCursor(64 - name.length()*3, 0); //centre text
oled.println(name);
oled.setCursor(100, 50);
oled.println(version); // text to display
oled.display(); // show on OLED
}
void optionselect(){
if(selectbutton == LOW){
delay(200);
next++;
if(next < 4){
settime = 100*next;// 60 = 1min
//printtable();
}
//else if(next == 5) {
//displayvoltage();
//}
else if (next == 5){
next = 0;
settime = 0;
//printtable();
}
timersecond = settime;
tempnext = next;
//Serial.println(timersecond);
}
}
void execution(){
if(confirmbutton == LOW){
progressbar = 1;
delay(200);
if(next == 4){
digitalWrite(7, LOW);
digitalWrite(4, HIGH);
printtable();
delay(500);
digitalWrite(7, HIGH);
digitalWrite(4, LOW);
printtable();
}
else{
while(settime != 0){
currentMillis = millis();
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
settime--;
timedisplay();
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
next = 0;
//digitalWrite(3, LOW);//OLED Turn ON
digitalWrite(8, !digitalRead(8));
printtable();
}
}
}
void printtable(){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE, BLACK);
oled.setCursor(0, 0);
//digitalWrite(8, HIGH);
oled.println("Menu");
//oled.setCursor(90, 0);
//oled.print(voltage);oled.println("V");
nextline(2);
printline();
nextline(4);
if(next == 0) oled.setTextColor(BLACK, WHITE);
oled.print("Charger");
//if(next==0) arrow();
oled.setTextColor(WHITE, BLACK);
oled.setCursor(70, oled.getCursorY());
if(digitalRead(8) == HIGH) oled.print("ON:" );
else oled.print("OFF:" );
oled.print(voltage,3);
nextline(10);
if(next == 1) oled.setTextColor(BLACK, WHITE);
oled.print("30mins");
oled.setTextColor(WHITE, BLACK);
//if(next==1) arrow();
nextline(10);
if(next == 2) oled.setTextColor(BLACK, WHITE);
oled.print("60mins");
oled.setTextColor(WHITE, BLACK);
//if(next==2) arrow();
nextline(10);
if(next == 3) oled.setTextColor(BLACK, WHITE);
oled.print("90mins");
oled.setTextColor(WHITE, BLACK);
//if(next==3) arrow();
nextline(10);
if(next == 4) oled.setTextColor(BLACK, WHITE);
oled.print("USB Hub");
oled.setTextColor(WHITE, BLACK);
oled.setCursor(70, oled.getCursorY());
if(digitalRead(4) == LOW) oled.print("ON");
else oled.print("Reset" );;
oled.display();
}
void timedisplay(){
int minutes = settime/60;
int seconds = settime%60;
displaytime = "";
if(minutes < 10) displaytime.concat("0");
displaytime.concat(minutes);
displaytime.concat(":");
if(seconds < 10) displaytime.concat("0");
displaytime.concat(seconds);
oled.clearDisplay();
//countdowndisplay();
drawPercentbar(20, 48, 70, 10,progressbar);
oledDisplayCenter(displaytime);
oled.display();
//Serial.println(progressflag);
progressflag++;
if(progressflag == timersecond/100){
progressbar++;
progressflag = 0;
}
}
void printend(){
oled.clearDisplay();
oled.setTextSize(1);
oledDisplayCenter(ending);
oled.display();
}
// void countdowndisplay(){
// oled.setTextSize(1); // text size
// oled.setCursor(0,0);
// oled.print("Countdown...");
// }
/*void clearOLEDLine(int row){
for (int y=row; y<=row+6; y++){
for (int x=0; x<127; x++){
oled.drawPixel(x, y, BLACK);
}
}
//lcd.setCursor(0,line);
}*/
void printline(){
for (int y = oled.getCursorY(); y<= oled.getCursorY(); y++){
for (int x=0; x<127; x++){
oled.drawPixel(x, y, WHITE);
}
}
}
void nextline(int number){
oled.setCursor(0, oled.getCursorY()+number);
}
void arrow(){
oled.setCursor(100, oled.getCursorY());
oled.print("<<-");
//oled.invertDisplay(true);
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.setTextColor(WHITE, BLACK);
oled.setTextSize(3);
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.print(text); // text to display
//oled.display();
}
void drawPercentbar(int x,int y, int width,int height, int progress)
{
progress = progress > timersecond ? timersecond : progress;
progress = progress < 0 ? 0 :progress;
float bar = ((float)(width-4) / timersecond) * progress;
oled.drawRect(x, y, width, height, WHITE);
oled.fillRect(x+2, y+2, bar , height-4, WHITE);
// Display progress text
oled.setCursor(width + 22, y+1 );
oled.setTextSize(1);
oled.setTextColor(WHITE, BLACK);
oled.print(progress);
oled.print("%");
// if( progress >=50)
// oled.setTextColor(BLACK, WHITE); // 'inverted' text
}
ISR(TIMER1_A) {
float analogvalue = analogRead(A0);
voltage = (analogRead(A0) * 5.000) / 1024;
//Serial.println(voltage);
}
// void displayvoltage(){
// while(next == 5){
// oled.clearDisplay();
// oled.setTextSize(1);
// String voltmeter = String(voltage, 2);
// //oled.setCursor(0, 0);
// oledDisplayCenter(voltmeter); // text to display
// //oled.println(voltage);
// oled.display();
// }
// }