This commit is contained in:
Mats van Reenen 2020-12-25 00:45:03 +01:00
parent acaf867434
commit 81fb16ba41
2 changed files with 99 additions and 21 deletions

View File

@ -2,12 +2,13 @@ CC := gcc
CURDIR := $(shell pwd) CURDIR := $(shell pwd)
DEBUGDIR := $(CURDIR)/debug DEBUGDIR := $(CURDIR)/debug
SRCDIR := $(CURDIR)/src SRCDIR := $(CURDIR)/src
main.o: src/main.c main.o: src/main.c
$(CC) -c $(SRCDIR)/main.c -o $(DEBUGDIR)/main.o $(CC) -c $(SRCDIR)/main.c -o $(DEBUGDIR)/main.o
domeTimer: main.o domeTimer: main.o
mkdir -p $(DEBUGDIR)
$(CC) $(SRCDIR)/main.c -o $(CURDIR)/domeTimer $(CC) $(SRCDIR)/main.c -o $(CURDIR)/domeTimer
all: domeTimer all: domeTimer

View File

@ -1,16 +1,89 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <dirent.h> #include <dirent.h>
#include <pthread.h> #include <pthread.h>
#define DEBUG
typedef struct { typedef struct {
uint32_t scheduler[5]; uint32_t scheduler[5];
uint32_t next; uint32_t next;
char extention[4]; char filename[128];
} Script_t; } Script_t;
Script_t *Scripts[128]; Script_t *Scripts[128];
#ifdef DEBUG
char timestr[19];
char *timeToStr(time_t t){
struct tm T = *localtime(&t);
sprintf(&timestr[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 &timestr[0];
}
char *timeToStr2(struct tm T){
sprintf(&timestr[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 &timestr[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 //TODO: add devide option
//TOOD: add full file name instad of only extention //TOOD: add full file name instad of only extention
Script_t *getSchedule(char *str){ Script_t *getSchedule(char *str){
@ -21,23 +94,25 @@ Script_t *getSchedule(char *str){
scit.scheduler[2] = scit.scheduler[2] =
scit.scheduler[3] = scit.scheduler[3] =
scit.scheduler[4] = 0; scit.scheduler[4] = 0;
scit.extention[3] = 0;
int8_t cmd[3]; int8_t cmd[3];
cmd[0] = cmd[1] = cmd[2] = 0; cmd[0] = cmd[1] = cmd[2] = 0;
printf("filename: %s\n", str);
// read the filename // read the filename
for(uint8_t ic=0; ic<strlen(str); ic++){ uint8_t ic;
for(ic=0; ic<strlen(str); ic++){
switch ((char)*(str+ic)){ switch ((char)*(str+ic)){
case ' ': case ' ':
switch (cmd[0]) switch (cmd[0])
{ {
case '-': case '-':
// set all bits in the range
for(uint8_t i=cmd[2]; i>cmd[1]; i--){ for(uint8_t i=cmd[2]; i>cmd[1]; i--){
scit.scheduler[is] |= 1<<i; scit.scheduler[is] |= 1<<i;
} }
break; break;
default: default:
// set the bit from last number from the list
scit.scheduler[is] |= (is == 0) ? 1<<(cmd[1]/2) : 1<<cmd[1]; scit.scheduler[is] |= (is == 0) ? 1<<(cmd[1]/2) : 1<<cmd[1];
break; break;
} }
@ -45,51 +120,53 @@ Script_t *getSchedule(char *str){
cmd[0] = 0; cmd[0] = 0;
cmd[1] = 0; cmd[1] = 0;
break; break;
case '*': case '*': // set all bits
scit.scheduler[is] = ~0; scit.scheduler[is] = ~0;
break; break;
case '0' ... '9': case '0' ... '9': // read a number
printf("number detected. char: %c, cmd: [%c, %d, %d], ", (char)*(str+ic), cmd[0], cmd[1], cmd[2]); if(cmd[0] == '-'){ // the second numer of a range should not be overritten
if(cmd[0] == '-'){
cmd[2] *= 10; cmd[2] *= 10;
cmd[2] += (char)*(str+ic) - '0'; cmd[2] += (char)*(str+ic) - '0';
}else{ }else{
cmd[1] *= 10; cmd[1] *= 10;
cmd[1] += (char)*(str+ic) - '0'; cmd[1] += (char)*(str+ic) - '0';
} }
printf("cmd: [%c, %d, %d]\n", cmd[0], cmd[1], cmd[2]);
break; break;
case ',': case ',': // list of times
cmd[0] = ','; cmd[0] = ',';
scit.scheduler[is] |= (is == 0) ? 1<<(cmd[1]/2) : 1<<cmd[1]; scit.scheduler[is] |= (is == 0) ? 1<<(cmd[1]/2) : 1<<cmd[1];
cmd[1] = 0; cmd[1] = 0;
break; break;
case '-': case '-': // range
if(is == 0) if(is == 0) // nimutes are devided by 2
cmd[1] /= 2; cmd[1] /= 2;
cmd[0] = '-'; cmd[0] = '-';
break; break;
} }
if((char)*(str+ic) == '.'){ if((char)*(str+ic) == '.'){
uint8_t i = 0;
if(ic+3 >= 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; break;
} }
if(is == 5) if(is == 5)
break; 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[0],
scit.scheduler[1], scit.scheduler[1],
scit.scheduler[2], scit.scheduler[2],
scit.scheduler[3], scit.scheduler[3],
scit.scheduler[4], scit.scheduler[4],
scit.extention timeToStr(scit.next),
scit.filename
); );
#endif
} }
void getScripts(){ void getScripts(){