From 9109cc8077ede6e016a4613eeb75953f514fbdd8 Mon Sep 17 00:00:00 2001 From: Derisis13 Date: Mon, 30 Aug 2021 21:52:18 +0200 Subject: [PATCH] Version 1.4.4 Changed function check_timing: now it takes a struct tm* instead of generating one - now the main function gets the current time after initialising the shutter array. --- main.c | 21 ++++++++++++--------- timing.c | 20 +++++++++----------- timing.h | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 29807e8..3409ecc 100755 --- a/main.c +++ b/main.c @@ -1,10 +1,10 @@ #include "timing.h" -#include "moving_debug.h" +#include "moving.h" #include #define FILEPATH "menetrend.txt" /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * SHUTTER AUTOMATA - * --------------- + * ----------------- * C program for automating remote controlled shutters via an openwrt router and a paired remote. * This program needs to be called by Cron in few minute intervals. * Raise and lowering times as well as the desired lowering percentage is read from a local file (menetrend.txt), and is @@ -16,27 +16,30 @@ int main () { shutter ercsi13[] = { { - 1, 0, 0, 0, 0, 27, 0 //fiúszoba + 1, 0, 0, 0, 0, 23, 0 //fiúszoba }, { - 2, 0, 0, 0, 0, 27, 0 //lányszoba + 2, 0, 0, 0, 0, 23, 0 //lányszoba }, { - 3, 0, 0, 0, 0, 27, 0 //nappali bal + 3, 0, 0, 0, 0, 23, 0 //nappali bal }, { - 4, 0, 0, 0, 0, 27, 0 //nappali jobb + 4, 0, 0, 0, 0, 23, 0 //nappali jobb }, { - 5, 0, 0, 0, 0, 27, 0 //szülői szoba + 5, 0, 0, 0, 0, 16, 0 //konyha }, { - 6, 0, 0, 0, 0, 18, 0 //konyha + 6, 0, 0, 0, 0, 23, 0 //szülői szoba }, { 7, 0, 0, 0, 0, 33, 0 //előtető (lehúzás = kieresztés, felhúzás = behúzás) } }; + //get the time of launch + time_t t = time (NULL); + struct tm *now = localtime (&t); //scan the schedule for (int i = 0; i < 7; ++i) @@ -54,7 +57,7 @@ int main () for (int i = 0; i < 7; ++i) { set_ch (&ercsi13[i], &ch); - switch (check_timing (&ercsi13[i])) + switch (check_timing (&ercsi13[i], now)) { case up:press_button (up); break; diff --git a/timing.c b/timing.c index 06e2925..f20f458 100644 --- a/timing.c +++ b/timing.c @@ -73,36 +73,34 @@ void get_timing (shutter *r, FILE *schedule) while (fscanf (schedule, "%d:%d", &tmp_hour, &tmp_min) != 2) fscanf (schedule, "%*c"); - r->up.tm_hour = (char )tmp_hour; - r->up.tm_min = (char )tmp_min; + r->up.tm_hour = (char) tmp_hour; + r->up.tm_min = (char) tmp_min; while (fscanf (schedule, "%d:%d", &tmp_hour, &tmp_min) != 2) fscanf (schedule, "%*c"); - r->down.tm_hour = (char )tmp_hour; - r->down.tm_min = (char )tmp_min; + r->down.tm_hour = (char) tmp_hour; + r->down.tm_min = (char) tmp_min; while (fscanf (schedule, "%d", &tmp_percentage) != 1) fscanf (schedule, "%*c"); - r->percentage = (char )tmp_percentage; + r->percentage = (char) tmp_percentage; fscanf (schedule, "%*[^\n]s\n"); } -buttons check_timing (shutter *r) +buttons check_timing (shutter *r, struct tm *now) { if (r->percentage == 0) return stop; - time_t t = time (NULL); - struct tm *now = localtime (&t); - if ((r->down.tm_hour == now->tm_hour) && (r->down.tm_min == now->tm_min)) + + if ((r->down.tm_hour == now->tm_hour) && (r->down.tm_min == (now->tm_min))) { return down; } - if ((r->up.tm_hour == now->tm_hour) && (r->up.tm_min == now->tm_min)) + if ((r->up.tm_hour == now->tm_hour) && (r->up.tm_min == (now->tm_min))) { return up; } - return stop; } diff --git a/timing.h b/timing.h index 2082adc..1bdf620 100644 --- a/timing.h +++ b/timing.h @@ -12,7 +12,7 @@ void get_timing (shutter *r, FILE *schedule); //checks whether it's time to raise/lower and acts accordingly -buttons check_timing (shutter *r); +buttons check_timing (shutter *r, struct tm* now); //checks the schedule file and sets the readhead to today's label int find_today (FILE *schedule);