From 81fb16ba41e89b596b191d096b61088484b41f92 Mon Sep 17 00:00:00 2001 From: Mats van Reenen Date: Fri, 25 Dec 2020 00:45:03 +0100 Subject: [PATCH] update --- Makefile | 3 +- src/main.c | 117 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 99 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 5ad8da5..6a81505 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,13 @@ CC := gcc CURDIR := $(shell pwd) DEBUGDIR := $(CURDIR)/debug -SRCDIR := $(CURDIR)/src +SRCDIR := $(CURDIR)/src main.o: src/main.c $(CC) -c $(SRCDIR)/main.c -o $(DEBUGDIR)/main.o domeTimer: main.o + mkdir -p $(DEBUGDIR) $(CC) $(SRCDIR)/main.c -o $(CURDIR)/domeTimer all: domeTimer diff --git a/src/main.c b/src/main.c index 21a3df2..d4e46a9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,89 @@ #include #include #include +#include #include #include +#define DEBUG + typedef struct { uint32_t scheduler[5]; uint32_t next; - char extention[4]; + char filename[128]; } Script_t; Script_t *Scripts[128]; +#ifdef DEBUG +char timestr[19]; +char *timeToStr(time_t t){ + struct tm T = *localtime(&t); + sprintf(×tr[0], "%04d-%02d-%02d %d:%02d:%02d", T.tm_year+1900, T.tm_mon+1, T.tm_mday, T.tm_hour, T.tm_min, T.tm_sec); + return ×tr[0]; +} +char *timeToStr2(struct tm T){ + sprintf(×tr[0], "%04d-%02d-%02d %d:%02d:%02d", T.tm_year+1900, T.tm_mon+1, T.tm_mday, T.tm_hour, T.tm_min, T.tm_sec); + return ×tr[0]; +} +#endif + +uint32_t getNextTrigger(Script_t *script){ + uint8_t daysInMouth[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + time_t ti = time(NULL); + struct tm t = *localtime(&ti); + while(1){ + printf("mounth: %d (%s), mask: 0x%x\n", t.tm_mon, timeToStr2(t), script->scheduler[3]); + if(1 << (t.tm_mon) & script->scheduler[3]){ + while(1){ + printf("day: %d (%s), mask: 0x%x\n", t.tm_mday, timeToStr2(t), script->scheduler[2]); + if(1 << (t.tm_mday) & script->scheduler[2]){ + while(1){ + printf("hour: %d (%s), mask: 0x%x\n", t.tm_hour, timeToStr2(t), script->scheduler[1]); + if(1 << (t.tm_hour) & script->scheduler[1]){ + while(1){ + printf("minute: %d (%s), mask: 0x%x\n", t.tm_min, timeToStr2(t), script->scheduler[1]); + if(1 << (t.tm_hour) & script->scheduler[1]){ + return mktime(&t); + } + t.tm_min++; + if(t.tm_min > 60){ + t.tm_min = 0; + t.tm_sec = 0; + break; + } + } + } + t.tm_hour++; + if(t.tm_hour > 23){ + t.tm_hour = 0; + t.tm_min = 0; + t.tm_sec = 0; + break; + } + } + } + t.tm_mday++; + if(t.tm_mday > daysInMouth[t.tm_mon]){ + t.tm_mday = 1; + t.tm_hour = 0; + t.tm_min = 0; + t.tm_sec = 0; + break; + } + } + } + t.tm_mon++; + if(t.tm_mon >= 12){ + t.tm_year++; + t.tm_mon = 0; + t.tm_mday = 1; + t.tm_hour = 0; + t.tm_min = 0; + t.tm_sec = 0; + } + } +} + //TODO: add devide option //TOOD: add full file name instad of only extention Script_t *getSchedule(char *str){ @@ -21,23 +94,25 @@ Script_t *getSchedule(char *str){ scit.scheduler[2] = scit.scheduler[3] = scit.scheduler[4] = 0; - scit.extention[3] = 0; int8_t cmd[3]; cmd[0] = cmd[1] = cmd[2] = 0; - printf("filename: %s\n", str); // read the filename - for(uint8_t ic=0; iccmd[1]; i--){ scit.scheduler[is] |= 1<= strlen(str)){ - memcpy(&scit.extention, str+ic+1, strlen(str) - ic); - scit.extention[strlen(str) - ic] = 0; - }else - memcpy(&scit.extention, str + strlen(str) - 3, 3); break; } if(is == 5) break; } - printf("scheduler: [\n 0x%x\n 0x%x\n 0x%x\n 0x%x\n 0x%x\n]\nextention: %s\n", + + // copy filename + memcpy(&scit.filename, str, strlen(str)+1); + + // get next time to trigger + scit.next = getNextTrigger(&scit); + +#ifdef DEBUG + printf("scheduler: [\n 0x%08x\n 0x%08x\n 0x%08x\n 0x%08x\n 0x%08x\n]\nnext trigger: %s\nfilename: %s\n", scit.scheduler[0], scit.scheduler[1], scit.scheduler[2], scit.scheduler[3], scit.scheduler[4], - scit.extention + timeToStr(scit.next), + scit.filename ); +#endif } void getScripts(){