code refactor

This commit is contained in:
2023-09-17 09:27:37 +02:00
parent 33cbb41fe2
commit 17e974f0a1
4 changed files with 122 additions and 189 deletions

View File

@@ -16,3 +16,4 @@ lib_deps =
fbiego/ESP32Time@^2.0.4
neironx/RTCLib@^1.6.0
arduino-libraries/NTPClient@^3.2.1
paulstoffregen/Time@^1.6.1

View File

@@ -0,0 +1,80 @@
#include "clock.h"
int RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year, RTC_day_of_week;
bool RTC_present;
const char *ssid = "iot";
const char *password = "Rijnstraat214";
void getRTCTime()
{
// RTC_seconds = timeClient.getSeconds();
// RTC_minutes = timeClient.getMinutes();
// RTC_hours = timeClient.getHours();
// RTC_day_of_week = 1
// RTC_day = timeClient.getDay();
// RTC_month = timeClient.
// RTC_year =
}
{
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
//setup NTP
timeClient.begin();
getRTCTime();
byte prevSeconds=RTC_seconds;
unsigned long RTC_ReadingStartTime=millis();
RTC_present=true;
while(prevSeconds==RTC_seconds)
{
getRTCTime();
//Serial.println(RTC_seconds);
if ((millis()-RTC_ReadingStartTime)>3000)
{
Serial.println(F("Warning! RTC DON'T RESPOND!"));
RTC_present=false;
break;
}
}
setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
}
void loopClock()
{
timeClient.update();
// if (((millis()%60000)==0)&&(RTC_present)) //synchronize with RTC every 10 seconds
// {
// getRTCTime();
// setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
// Serial.println("sync:");
// Serial.println(RTC_seconds);
// }
}
String getTimeNow()
{
if (value[hModeValueIndex]==24) return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
else return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
}
String updateDateString()
{
static unsigned long lastTimeDateUpdate=millis();
static String DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
if ((millis()-lastTimeDateUpdate)>1000)
{
lastTimeDateUpdate=millis();
DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
}
return DateString;
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <Arduino.h>
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <ESP32Time.h>
void initClock();
void loopClock();
String getTimeNow();
String updateDateString();

View File

@@ -1,10 +1,5 @@
#include <Arduino.h>
#include <NTPClient.h>
// change next line to use with another board/shield
#include <WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
#include "clock.h"
const String FirmwareVersion="010310";
//Format _X.XX__
//NIXIE CLOCK MCU107 by GRA & AFCH (fominalec@gmail.com)
@@ -28,8 +23,9 @@
//#include <TimeLib.h>
//#include <Tone.h>
// #include <EEPROM.h>
#include <ESP32Time.h>
#include <RTClib.h>
#include "JC_Button.h"
#define Anode0Pin 10
#define Anode1Pin 3
@@ -44,6 +40,10 @@
#define Anode2ON digitalWrite(Anode2Pin,HIGH)
#define Anode2OFF digitalWrite(Anode2Pin,LOW)
#define mosiPin 5
#define misoPin 9
#define sckPin 6
String updateDateString();
String updateDisplayString();
void doDotBlink();
@@ -54,8 +54,11 @@ byte decToBcd(byte val);
String antiPoisoning2(String fromStr, String toStr);
String getTimeNow();
String antiPoisoning2(String fromStr, String toStr);
void doTest();
// void getRTCTime();
void modesChanger();
void doIndication();
//char* parseSong(char *p);
const byte LEpin= 7; //pin Latch Enabled data accepted while HI level
//const byte DHVpin=5; // off/on MAX1771 Driver Hight Voltage(DHV) 110-220V
@@ -69,7 +72,6 @@ const byte pinDown=2;
//const byte pinUpperDots=12; //HIGH value light a dots
//const byte pinLowerDots=4; //HIGH value light a dots
const word fpsLimit=16666; // 1/60*1.000.000 //limit maximum refresh rate on 60 fps
bool RTC_present;
String stringToDisplay="000000";// Conten of this string will be displayed on tubes (must be 6 chars length)
@@ -88,9 +90,7 @@ byte dotPattern=B00000000; //bit mask for separeting dots
//B00000000 - turn off up and down dots
//B1100000 - turn off all dots
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
int RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year, RTC_day_of_week;
//-- ------------0--------1--------2-------3--------4--------5--------6--------7--------8--------9--------10-------11-------12-------13-------14
// names: Time, Date, Alarm, 12/24 hours, mintues, seconds, day, month, year, hour, minute, second alarm01 hour_format
@@ -152,170 +152,53 @@ byte LEDsGreenValueEEPROMAddress=9;
byte LEDsBlueValueEEPROMAddress=10;
//buttons pins declarations
//ClickButton setButton(pinSet, LOW, CLICKBTN_PULLUP);
//ClickButton upButton(pinUp, LOW, CLICKBTN_PULLUP);
//ClickButton downButton(pinDown, LOW, CLICKBTN_PULLUP);
///////////////////
Button setButton(pinSet, 20,true, true);
Button upButton(pinUp, 20,true, true);
Button downButton(pinDown, 20,true, true);
const unsigned long longClickTime(2000);
//Tone tone1;
#define isdigit(n) (n >= '0' && n <= '9')
//char *song = "MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d";
//char *song = "PinkPanther:d=4,o=5,b=160:8d#,8e,2p,8f#,8g,2p,8d#,8e,16p,8f#,8g,16p,8c6,8b,16p,8d#,8e,16p,8b,2a#,2p,16a,16g,16e,16d,2e";
//char *song="VanessaMae:d=4,o=6,b=70:32c7,32b,16c7,32g,32p,32g,32p,32d#,32p,32d#,32p,32c,32p,32c,32p,32c7,32b,16c7,32g#,32p,32g#,32p,32f,32p,16f,32c,32p,32c,32p,32c7,32b,16c7,32g,32p,32g,32p,32d#,32p,32d#,32p,32c,32p,32c,32p,32g,32f,32d#,32d,32c,32d,32d#,32c,32d#,32f,16g,8p,16d7,32c7,32d7,32a#,32d7,32a,32d7,32g,32d7,32d7,32p,32d7,32p,32d7,32p,16d7,32c7,32d7,32a#,32d7,32a,32d7,32g,32d7,32d7,32p,32d7,32p,32d7,32p,32g,32f,32d#,32d,32c,32d,32d#,32c,32d#,32f,16c";
//char *song="DasBoot:d=4,o=5,b=100:d#.4,8d4,8c4,8d4,8d#4,8g4,a#.4,8a4,8g4,8a4,8a#4,8d,2f.,p,f.4,8e4,8d4,8e4,8f4,8a4,c.,8b4,8a4,8b4,8c,8e,2g.,2p";
//char *song="Scatman:d=4,o=5,b=200:8b,16b,32p,8b,16b,32p,8b,2d6,16p,16c#.6,16p.,8d6,16p,16c#6,8b,16p,8f#,2p.,16c#6,8p,16d.6,16p.,16c#6,16b,8p,8f#,2p,32p,2d6,16p,16c#6,8p,16d.6,16p.,16c#6,16a.,16p.,8e,2p.,16c#6,8p,16d.6,16p.,16c#6,16b,8p,8b,16b,32p,8b,16b,32p,8b,2d6,16p,16c#.6,16p.,8d6,16p,16c#6,8b,16p,8f#,2p.,16c#6,8p,16d.6,16p.,16c#6,16b,8p,8f#,2p,32p,2d6,16p,16c#6,8p,16d.6,16p.,16c#6,16a.,16p.,8e,2p.,16c#6,8p,16d.6,16p.,16c#6,16a,8p,8e,2p,32p,16f#.6,16p.,16b.,16p.";
//char *song="Popcorn:d=4,o=5,b=160:8c6,8a#,8c6,8g,8d#,8g,c,8c6,8a#,8c6,8g,8d#,8g,c,8c6,8d6,8d#6,16c6,8d#6,16c6,8d#6,8d6,16a#,8d6,16a#,8d6,8c6,8a#,8g,8a#,c6";
//char *song="WeWishYou:d=4,o=5,b=200:d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,2g,d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,1g,d,g,g,g,2f#,f#,g,f#,e,2d,a,b,8a,8a,8g,8g,d6,d,d,e,a,f#,2g";
//#define OCTAVE_OFFSET 0
char *p;
// int notes[] = { 0,
// NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
// NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
// NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
// NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
// };
// int fireforks[]={0,0,1,//1
// -1,0,0,//2
// 0,1,0,//3
// 0,0,-1,//4
// 1,0,0,//5
// 0,-1,0}; //array with RGB rules (0 - do nothing, -1 - decrese, +1 - increse
void setRTCDateTime(byte h, byte m, byte s, byte d, byte mon, byte y, byte w=1);
int functionDownButton=0;
int functionUpButton=0;
bool LEDsLock=false;
//ESP32Time tijd(3600);
DS1307 rtc;
const char *ssid = "iot";
const char *password = "Rijnstraat214";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void doTest();
void getRTCTime();
void modesChanger();
void doIndication();
void setTime(int RTC_hours, int RTC_minutes, int RTC_seconds, int RTC_day, int RTC_month, int RTC_year)
{
tijd.setTime(RTC_seconds,RTC_minutes,RTC_hours,RTC_day,RTC_month,RTC_year);
}
/*******************************************************************************************************
Init Programm
*******************************************************************************************************/
void setup()
{
// digitalWrite(DHVpin, LOW); // off MAX1771 Driver Hight Voltage(DHV) 110-220V
Wire.begin();
Wire.setPins(0,1);
rtc.begin();
//setRTCDateTime(23,40,00,25,7,15,1);
// Wire.begin();
// Wire.setPins(0,1);
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
// if (!rtc.isrunning()) {
// Serial.println("RTC is NOT running!");
// // following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(__DATE__, __TIME__));
// }
// if (EEPROM.read(HourFormatEEPROMAddress)!=12) value[hModeValueIndex]=24; else value[hModeValueIndex]=12;
// if (EEPROM.read(RGBLEDsEEPROMAddress)!=0) RGBLedsOn=true; else RGBLedsOn=false;
// if (EEPROM.read(AlarmTimeEEPROMAddress)==255) value[AlarmHourIndex]=0; else value[AlarmHourIndex]=EEPROM.read(AlarmTimeEEPROMAddress);
// if (EEPROM.read(AlarmTimeEEPROMAddress+1)==255) value[AlarmMinuteIndex]=0; else value[AlarmMinuteIndex]=EEPROM.read(AlarmTimeEEPROMAddress+1);
// if (EEPROM.read(AlarmTimeEEPROMAddress+2)==255) value[AlarmSecondIndex]=0; else value[AlarmSecondIndex]=EEPROM.read(AlarmTimeEEPROMAddress+2);
// if (EEPROM.read(AlarmArmedEEPROMAddress)==255) value[Alarm01]=0; else value[Alarm01]=EEPROM.read(AlarmArmedEEPROMAddress);
// if (EEPROM.read(LEDsLockEEPROMAddress)==255) LEDsLock=false; else LEDsLock=EEPROM.read(LEDsLockEEPROMAddress);
// Serial.print("led lock=");
// Serial.println(LEDsLock);
// pinMode(RedLedPin, OUTPUT);
// pinMode(GreenLedPin, OUTPUT);
// pinMode(BlueLedPin, OUTPUT);
// pinMode(pinUpperDots, OUTPUT);
// pinMode(pinLowerDots, OUTPUT);
//digitalWrite(pinUpperDots, HIGH);
//while(1);
// tone1.begin(pinBuzzer);
// song=parseSong(song);
pinMode(LEpin, OUTPUT);
//pinMode(DHVpin, OUTPUT);
// SPI setup
SPI.begin(5,9,6); //
// SPI setup
SPI.begin(mosiPin,misoPin,sckPin); //
SPI.setDataMode (SPI_MODE3); // Mode 3 SPI
SPI.setClockDivider(SPI_CLOCK_DIV128); // SCK = 16MHz/128= 125kHz
pinMode(Anode0Pin, OUTPUT);
pinMode(Anode1Pin, OUTPUT);
pinMode(Anode2Pin, OUTPUT);
//buttons pins inits
// pinMode(pinSet, INPUT_PULLUP);
// pinMode(pinUp, INPUT_PULLUP);
// pinMode(pinDown, INPUT_PULLUP);
////////////////////////////
//pinMode(pinBuzzer, OUTPUT);
//buttons objects inits
// setButton.debounceTime = 20; // Debounce timer in ms
// setButton.multiclickTime = 30; // Time limit for multi clicks
// setButton.longClickTime = 2000; // time until "held-down clicks" register
// upButton.debounceTime = 20; // Debounce timer in ms
// upButton.multiclickTime = 30; // Time limit for multi clicks
// upButton.longClickTime = 2000; // time until "held-down clicks" register
// downButton.debounceTime = 20; // Debounce timer in ms
// downButton.multiclickTime = 30; // Time limit for multi clicks
// downButton.longClickTime = 2000; // time until "held-down clicks" register
setButton.begin();
upButton.begin();
downButton.begin();
//
//digitalWrite(DHVpin, HIGH); // on MAX1771 Driver Hight Voltage(DHV) 110-220V
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
doTest();
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// if (LEDsLock==1)
// {
// setLEDsFromEEPROM();
// }
getRTCTime();
byte prevSeconds=RTC_seconds;
unsigned long RTC_ReadingStartTime=millis();
RTC_present=true;
while(prevSeconds==RTC_seconds)
{
getRTCTime();
//Serial.println(RTC_seconds);
if ((millis()-RTC_ReadingStartTime)>3000)
{
Serial.println(F("Warning! RTC DON'T RESPOND!"));
RTC_present=false;
break;
}
}
setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
// digitalWrite(DHVpin, LOW); // off MAX1771 Driver Hight Voltage(DHV) 110-220V
//setRTCDateTime(RTC_hours,RTC_minutes,RTC_seconds,RTC_day,RTC_month,RTC_year,1); //записываем только что считанное время в RTC чтобы запустить новую микросхему
// digitalWrite(DHVpin, HIGH); // on MAX1771 Driver Hight Voltage(DHV) 110-220V
//p=song;
initClock();
}
int rotator=0; //index in array with RGB "rules" (increse by one on each 255 cycles)
@@ -339,14 +222,9 @@ long modesChangePeriod=timeModePeriod;
MAIN Programm
***************************************************************************************************************/
void loop() {
timeClient.update();
// if (((millis()%60000)==0)&&(RTC_present)) //synchronize with RTC every 10 seconds
// {
// getRTCTime();
// setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
// Serial.println("sync:");
// Serial.println(RTC_seconds);
// }
loopClock();
//p=playmusic(p);
@@ -762,25 +640,7 @@ byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void getRTCTime()
{
DateTime now = timeClient.;
// char buf[20];
// Serial.println(tijd.tostr(buf));
// Wire.beginTransmission(DS1307_ADDRESS);
// Wire.write(zero);
// Wire.endTransmission();
// Wire.requestFrom(DS1307_ADDRESS, 7);
RTC_seconds = now.second();
RTC_minutes = now.minute();
RTC_hours = now.hour();
RTC_day_of_week = now.dayOfWeek();
RTC_day = now.day();
RTC_month = now.month();
RTC_year = now.year();
}
word doEditBlink(int pos)
{
@@ -1148,23 +1008,3 @@ String updateDisplayString()
}
return stringToDisplay;
}
String getTimeNow()
{
// DateTime time = rtc.now();
if (value[hModeValueIndex]==24) return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
else return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
}
String updateDateString()
{
// DateTime time = tijd.now();
static unsigned long lastTimeDateUpdate=millis();
static String DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
if ((millis()-lastTimeDateUpdate)>1000)
{
lastTimeDateUpdate=millis();
DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
}
return DateString;
}