corz.org uses cookies to remember that you've seen this notice explaining that corz.org uses cookies, okay!
int h=10;
int m=8;
int s=37;
//
const char* ssid = "WiFi-SSID";
const char* password = "WiFi-password";
const char* ntpServer = "time1.google.com";
const char* time_zone = "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00";
int status = WL_IDLE_STATUS;
bool useRealTime = true;
bool doneUpdate = false;
int lastx,lasty;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
int Timeout = 15;
const long interval = 996;
void setup() {
Serial.begin(115200);
Serial.println("\n");
Serial.println("ESP32 Oscilloscope Clock v1.2");
dac_output_enable(DAC_CHANNEL_1);
dac_output_enable(DAC_CHANNEL_2);
Serial.print("\nConnecting to Wi-Fi..");
WiFi.begin (ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
Serial.print(".");
Timeout--;
if (Timeout==0){
useRealTime = false;
Serial.println("WiFi Timeout");
break;
}
}
Serial.println("");
if (useRealTime && Timeout!=0){
Serial.println("WiFi connected");
if (useRealTime) {
configTzTime(time_zone, ntpServer);
Serial.print("Time: ");
printLocalTime();
} else {
useRealTime = false;
Serial.println("Time functions disabled");
}
}
if (!useRealTime) {
Serial.println("Using Fixed Time");
}
Serial.println("Using Fixed Time");
h=(h*5)+m/12;
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
s++;
}
if (s==60) {
s=0;
m++;
if ((m==12)||(m==24)||(m==36)||(m==48)) {
h++;
}
}
if (m==60) {
m=0;
h++;
}
if (h==60) {
h=0;
}
if (m==0 && s<=1 && !doneUpdate) {
Serial.print("\nUpdated Time: ");
printLocalTime();
h=(h*5)+m/12;
doneUpdate = true;
}
if (m==1 && doneUpdate) {
doneUpdate = false;
}
DACPrepare(true);
PlotTable(DialDigits12,sizeof(DialDigits12),0x00,1,0);
PlotTable(DialData,sizeof(DialData),0x00,1,0);
PlotTable(HrPtrData, sizeof(HrPtrData), 0xFF,0,9*h);
PlotTable(MinPtrData,sizeof(MinPtrData),0xFF,0,9*m);
PlotTable(SecPtrData,sizeof(SecPtrData),0xFF,0,5*s);
DACUnprepare(true);
Serial.printf("\nHours:%d Minutes:%d Seconds:%d\n", h, m, s);
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
useRealTime = false;
} else {
Serial.println(&timeinfo, "%H:%M:%S, %A, %B %d %Y");
h=(int)timeinfo.tm_hour;
m=(int)timeinfo.tm_min;
s=(int)timeinfo.tm_sec;
if (h > 12) h=h-12;
String hpl = ""; String mpl = ""; String spl = "";
if (h != 1) hpl = "s"; if (m != 1) mpl = "s"; if (s != 1) spl = "s";
Serial.printf("Setting Analog Clock to %i Hour%s, %i Minute%s and %i Second%s.\n", h, hpl.c_str(), m, mpl.c_str(), s, spl.c_str());
}
}
void PlotTable(byte *SubTable, int SubTableSize, int skip, int opt, int offset) {
int i=offset;
while (i<SubTableSize){
if (SubTable[i+2]==skip){
i=i+3;
if (opt==1) if (SubTable[i]==skip) i++;
}
Line(SubTable[i],SubTable[i+1],SubTable[i+2],SubTable[i+3]);
if (opt==2){
Line(SubTable[i+2],SubTable[i+3],SubTable[i],SubTable[i+1]);
}
i=i+2;
if (SubTable[i+2]==0xFF) break;
}
}
void Line(byte x1, byte y1, byte x2, byte y2)
{
int acc;
if (y1 < y2) {
byte dy = y2 - y1;
if (x1 < x2) {
byte dx = x2 - x1;
if (dx > dy) {
acc = (dx >> 1);
for (; x1 <= x2; x1++) {
Dot(x1, y1);
acc -= dy;
if (acc < 0) {
y1++;
acc += dx;
}
}
}
else {
acc = dy >> 1;
for (; y1 <= y2; y1++) {
Dot(x1, y1);
acc -= dx;
if (acc < 0) {
x1++;
acc += dy;
}
}
}
}
else {
byte dx = x1 - x2;
if (dx > dy) {
acc = dx >> 1;
for (; x1 >= x2; x1--) {
Dot(x1, y1);
acc -= dy;
if (acc < 0) {
y1++;
acc += dx;
}
}
}
else {
acc = dy >> 1;
for (; y1 <= y2; y1++) {
Dot(x1, y1);
acc -= dx;
if (acc < 0) {
x1--;
acc += dy;
}
}
}
}
}
else {
byte dy = y1 - y2;
if (x1 < x2) {
byte dx = x2 - x1;
if (dx > dy) {
acc = dx >> 1;
for (; x1 <= x2; x1++) {
Dot(x1, y1);
acc -= dy;
if (acc < 0) {
y1--;
acc += dx;
}
}
}
else {
acc = dy >> 1;
for (; y1 >= y2; y1--) {
Dot(x1, y1);
acc -= dx;
if (acc < 0) {
x1++;
acc += dy;
}
}
}
}
else {
byte dx = x1 - x2;
if (dx > dy) {
acc = dx >> 1;
for (; x1 >= x2; x1--) {
Dot(x1, y1);
acc -= dy;
if (acc < 0) {
y1--;
acc += dx;
}
}
}
else {
acc = dy >> 1;
for (; y1 >= y2; y1--) {
Dot(x1, y1);
acc -= dx;
if (acc < 0) {
x1--;
acc += dy;
}
}
}
}
}
}
inline void Dot(int x, int y) {
if (lastx!=x){
lastx=x;
DAC1Write(x);
}
if (lasty!=y){
lasty=y;
DAC2Write(y);
}
}