#include <MsTimer2.h>
#include "myNumbers.h"
#include <Wire.h> //I2C Arduino Library
#include <Adafruit_GFX.h>
#include "Adafruit_SSD1306.h"
// HARDWARE I2C: A4 -> SDA, A5 -> SCL
// --------- HARDWARE SPI OLED: 11 -> MOSI/DIN, 13 -> SCK
#define PIN_RESET 4 // dummy
/*
* unused
*
#define BUTTON1 A3
#define BUTTON2 A2
#define LED_BLUE 11
#define LED_GREEN 10
#define LED_RED 9
#define LED_OFF LOW
#define LED_ON HIGH
#define SERIAL_SPEED 9600
*/
byte hours = 18;
byte minutes = 59;
byte seconds = 50;
byte tick = 0;
int vcc;
Adafruit_SSD1306 * oled = new Adafruit_SSD1306(PIN_RESET);
/*
* unused
*
void readVcc() {
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(10); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
vcc = ADCL;
vcc |= ADCH<<8;
vcc = 1126400L / vcc;
}
*/
void hideA(byte x, short y, int h) {
oled->fillRect(x, y+h, 16, 32-h, BLACK);
}
void hideB(byte x, short y, int h) {
oled->fillRect(x, y, 16, 16+h, BLACK);
}
void myFont(byte x, short y, byte b, bool change) {
int i = 0;
int j = 18;
int h = 0;
if (change == true && tick > 19) {
if(tick < 33) {
h = tick - 20;
if (b == 0) {
oled->drawBitmap(x, y, zz1, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz0, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz0b, 16, 16, WHITE);
} else if (b == 1) {
oled->drawBitmap(x, y, zz2, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz1, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz1b, 16, 16, WHITE);
} else if (b == 2) {
oled->drawBitmap(x, y, zz3, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz2, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz2b, 16, 16, WHITE);
} else if (b == 3) {
oled->drawBitmap(x, y, zz4, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz3, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz3b, 16, 16, WHITE);
} else if (b == 4) {
oled->drawBitmap(x, y, zz5, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz4, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz4b, 16, 16, WHITE);
} else if (b == 5) {
oled->drawBitmap(x, y, zz6, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz5, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz5b, 16, 16, WHITE);
} else if (b == 6) {
oled->drawBitmap(x, y, zz7, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz6, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz6b, 16, 16, WHITE);
} else if (b == 7) {
oled->drawBitmap(x, y, zz8, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz7, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz7b, 16, 16, WHITE);
} else if (b == 8) {
oled->drawBitmap(x, y, zz9, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz8, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz8b, 16, 16, WHITE);
} else if (b == 9) {
oled->drawBitmap(x, y, zz0, 16, 16, WHITE);
hideA(x,y,h);
// oled->drawBitmap(x, y+h, zz9, 16, 16, WHITE);
// hideA(x,y,16);
oled->drawBitmap(x, y+j, zz9b, 16, 16, WHITE);
}
oled->drawLine(x-1, y+h-1, x+17, y+h-1, WHITE);
oled->drawLine(x-1, y+h-1, x+2, y+16, WHITE);
oled->drawLine(x+17, y+h-1, x+14, y+16, WHITE);
} else {
h = tick - 33;
if (b == 0) {
oled->drawBitmap(x, y+j, zz0b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz1b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz1, 16, 16, WHITE);
} else if (b == 1) {
oled->drawBitmap(x, y+j, zz1b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz2b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz2, 16, 16, WHITE);
} else if (b == 2) {
oled->drawBitmap(x, y+j, zz2b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz3b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz3, 16, 16, WHITE);
} else if (b == 3) {
oled->drawBitmap(x, y+j, zz3b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz4b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz4, 16, 16, WHITE);
} else if (b == 4) {
oled->drawBitmap(x, y+j, zz4b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz5b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz5, 16, 16, WHITE);
} else if (b == 5) {
oled->drawBitmap(x, y+j, zz5b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz6b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz6, 16, 16, WHITE);
} else if (b == 6) {
oled->drawBitmap(x, y+j, zz6b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz7b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz7, 16, 16, WHITE);
} else if (b == 7) {
oled->drawBitmap(x, y+j, zz7b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz8b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz8, 16, 16, WHITE);
} else if (b == 8) {
oled->drawBitmap(x, y+j, zz8b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz9b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz9, 16, 16, WHITE);
} else if (b == 9) {
oled->drawBitmap(x, y+j, zz9b, 16, 16, WHITE);
hideB(x,y,h);
oled->drawBitmap(x, y+h, zz0b, 16, 16, WHITE);
oled->fillRect(x, y, 16, 16, BLACK);
oled->drawBitmap(x, y, zz0, 16, 16, WHITE);
}
oled->drawLine(x-1, y+17+h, x+17, y+17+h, WHITE);
oled->drawLine(x+2, y+17, x-1, y+17+h, WHITE);
oled->drawLine(x+14, y+17, x+16, y+17+h, WHITE);
}
} else {
if (b == 0) {
oled->drawBitmap(x, y+i, zz0, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz0b, 16, 16, WHITE);
} else if (b == 1) {
oled->drawBitmap(x, y+i, zz1, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz1b, 16, 16, WHITE);
} else if (b == 2) {
oled->drawBitmap(x, y+i, zz2, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz2b, 16, 16, WHITE);
} else if (b == 3) {
oled->drawBitmap(x, y+i, zz3, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz3b, 16, 16, WHITE);
} else if (b == 4) {
oled->drawBitmap(x, y+i, zz4, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz4b, 16, 16, WHITE);
} else if (b == 5) {
oled->drawBitmap(x, y+i, zz5, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz5b, 16, 16, WHITE);
} else if (b == 6) {
oled->drawBitmap(x, y+i, zz6, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz6b, 16, 16, WHITE);
} else if (b == 7) {
oled->drawBitmap(x, y+i, zz7, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz7b, 16, 16, WHITE);
} else if (b == 8) {
oled->drawBitmap(x, y+i, zz8, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz8b, 16, 16, WHITE);
} else if (b == 9) {
oled->drawBitmap(x, y+i, zz9, 16, 16, WHITE);
oled->drawBitmap(x, y+j, zz9b, 16, 16, WHITE);
}
}
}
inline void flipClock() {
bool flap = false;
int t = hours/10;
if ((hours+1)%10 == 0 && (minutes+1)%60 == 0 && (seconds+1)%60 == 0) flap=true;
myFont(5, 10, t, flap);
flap = false;
t = hours - t*10;
if ((minutes+1)%60 == 0 && (seconds+1)%60 == 0) flap=true;
myFont(23, 10, t, flap);
flap = false;
t = minutes/10;
if ((minutes+1)%10 == 0 && (seconds+1)%60 == 0) flap=true;
myFont(46, 10, t, flap);
flap = false;
t = minutes - t*10;
if ((seconds+1)%60 == 0) flap=true;
myFont(61, 10, t, flap);
flap = false;
t = seconds/10;
if ((seconds+1)%10 == 0) flap=true;
myFont(87, 10, t, flap);
t = seconds - t*10;
myFont(105, 10, t, true);
}
inline void ticking() {
tick = (tick+1) % 40;
if (tick == 0) {
seconds++;
}
if (seconds > 59) {
minutes += seconds / 60;
seconds = seconds % 60;
}
if (minutes > 59) {
hours += minutes / 60;
minutes = minutes % 60;
}
if (hours > 23) {
hours = hours % 24;
}
}
void setup() {
//pinMode(BUTTON1, INPUT_PULLUP);
//pinMode(BUTTON2, INPUT_PULLUP);
//pinMode(LED_RED, OUTPUT);
//pinMode(LED_GREEN, OUTPUT);
//pinMode(LED_BLUE, OUTPUT);
Serial.begin(9600);
oled->begin();
oled->clearDisplay();
oled->display();
//oled->invertDisplay(true);
oled->fillRect(0, 0, 128, 64, WHITE);
MsTimer2::set(25, ticking); // 50ms period -> 1sec = 40 parts/ticks
MsTimer2::start();
}
void loop() {
delay(7); // "refresh rate"
oled->clearDisplay();
flipClock();
oled->display();
}