From 08337620067aa88d2ae1895e67d359be0123c9fc Mon Sep 17 00:00:00 2001 From: Derisis13 Date: Mon, 30 Aug 2021 19:04:07 +0200 Subject: [PATCH] Changed comments, variable names and filenames to be in English --- CMakeLists.txt | 8 +++--- main.c | 58 ++++++++++++++++++++++------------------ moving.c | 60 ++++++++++++++++++++++++++++++++++++++++++ moving.h | 20 ++++++++++++++ mozgatas.c | 70 ------------------------------------------------- mozgatas.h | 23 ---------------- mozgatas_demo.c | 8 +----- mozgatas_demo.h | 7 ++--- shutter.h | 27 +++++++++---------- timing.c | 24 +++++++++-------- timing.h | 10 ++++--- 11 files changed, 153 insertions(+), 162 deletions(-) create mode 100644 moving.c create mode 100644 moving.h delete mode 100644 mozgatas.c delete mode 100644 mozgatas.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 55f4815..b5668b6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ project(redony_automata C) set(CMAKE_C_STANDARD 99) -add_executable(redony_automata main.c idozites.c idozites.h - #mozgatas.c mozgatas.h - redony.h - mozgatas_demo.c mozgatas_demo.h +add_executable(redony_automata main.c timing.c timing.h + moving.c moving.h + shutter.h + #mozgatas_demo.c mozgatas_demo.h ) \ No newline at end of file diff --git a/main.c b/main.c index 4a88243..df61485 100755 --- a/main.c +++ b/main.c @@ -1,44 +1,44 @@ -#include "idozites.h" -#include "mozgatas_demo.h" +#include "timing.h" +#include "moving.h" #include #define FILEPATH "menetrend.txt" /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * REDŐNY AUTOMATA + * SHUTTER AUTOMATA * --------------- - * C program az itthoni redőnyök automatizálására egy kiszuperált router és egy kompatibilis távirányító segítségével. - * A programot egy időzítő (Cron) hívja meg fix időközönként. - * Egy helyi fájlból, a menetrend.txt-ből olvassa ki a felhúzások és lehúzások idejét, ha annyi az idő, fel/lehúzza, - * ez egy heti menetrendet képes tárolni és végrehajtani. - * GPIO-kkal vezérli a távirányító gombmátrixát. - * A programot OpenWrt 19.07-re fordítom. HW: D-Link DIR-600. (fordítási környezethez:$ source openwrt.config) + * 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 + * executed; the schedule contains one full week. + * The program uses GPIO2 for resetting the remote and GPIO17-21 for controlling the remote. + * The program is compiled to openwrt 19.07.8; HW: D-Link DIR-600 rev.B6E * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ int main () { - redony ercsi13[] = { + shutter ercsi13[] = { { - 1, 0, 0 //fiúszoba + 1, 0, 0, 0, 0, 27, 0 //fiúszoba }, { - 2, 0, 0 //lányszoba + 2, 0, 0, 0, 0, 27, 0 //lányszoba }, { - 3, 0, 0 //nappali bal + 3, 0, 0, 0, 0, 27, 0 //nappali bal }, { - 4, 0, 0 //nappali jobb + 4, 0, 0, 0, 0, 27, 0 //nappali jobb }, { - 5, 0, 0 //szülői szoba + 5, 0, 0, 0, 0, 27, 0 //szülői szoba }, { - 6, 0, 0 //konyha + 6, 0, 0, 0, 0, 18, 0 //konyha }, { - 7, 0, 0 //előtető (lehúzás = kieresztés, felhúzás = behúzás) + 7, 0, 0, 0, 0, 33, 0 //előtető (lehúzás = kieresztés, felhúzás = behúzás) } }; - //beolvassuk minden redőny időpontját + //scan the schedule for (int i = 0; i < 7; ++i) { FILE *schedule = fopen (FILEPATH, "r"); @@ -47,16 +47,24 @@ int main () fclose (schedule); } - /* - //exportáljuk a gpio-kat, hogy tudjuk használni őket - export (); - */ + reset (); + byte ch = 1; //remote defaults to ch 1 - //leellenőrizzük az időpontokat, minden redőny úgy mozog, ahhogy azt megmondtuk neki. + //check timings and act accordingly for (int i = 0; i < 7; ++i) { - set_ch (&ercsi13[i]); - press_button (check_timing (&ercsi13[i])); + set_ch (&ercsi13[i], &ch); + switch (check_timing (&ercsi13[i])) + { + + case up:press_button (up); + break; + case stop:break; + case down:lower (&ercsi13[i]); + break; + case prev: + case next:break; + }; } return 0; } \ No newline at end of file diff --git a/moving.c b/moving.c new file mode 100644 index 0000000..913dc5e --- /dev/null +++ b/moving.c @@ -0,0 +1,60 @@ +// +// Created by lacko on 08/08/2021. +// + +#include "moving.h" +#include +#include +#include //project is to be compiled to openwrt + +void set_ch (shutter *r, byte *ch) +{ + while (*ch != r->ch) + { + if (*ch > r->ch) + { + press_button (prev); + *ch--; + } + else if (*ch < r->ch) + { + press_button (next); + *ch++; + } + } +} + +//doing negative logic as reset is active low and GPIO2 is 1 on startup +void reset () +{ + FILE *gpio2 = fopen ("/sys/class/gpio/gpio2/value", "w"); + fprintf (gpio2, "0"); + fclose (gpio2); + sleep (1); + gpio2 = fopen ("/sys/class/gpio/gpio2/value", "w"); + fprintf (gpio2, "1"); + fclose (gpio2); + sleep (3); +} + +//doing positive logic as button GPIOs are 0 on startup +void press_button (buttons b) +{ + char *fname = calloc (29, 1); + sprintf (fname, "/sys/class/gpio/gpio%d/value", b); + FILE *gpio = fopen (fname, "w"); + usleep (200000); + fprintf (gpio, "0"); + fclose (gpio); + gpio = fopen (fname, "w"); + usleep (200000); + fprintf (gpio, "1"); + fclose (gpio); +} + +void lower (shutter *r) +{ + press_button (down); + usleep (r->percentage * r->rolltime_down * 10000); + press_button (stop); +} \ No newline at end of file diff --git a/moving.h b/moving.h new file mode 100644 index 0000000..fdfa317 --- /dev/null +++ b/moving.h @@ -0,0 +1,20 @@ +// +// Created by lacko on 08/08/2021. +// Redőnyök mozgatásáért felelős függvényeket tartalmaz +// +#include "shutter.h" +#ifndef _MOZGATAS_H_ +#define _MOZGATAS_H_ + +//set to the desired ch on the remote +void set_ch (shutter *r, byte *ch); + +//activates the gpio given to it and thus "presses" the button +void press_button (buttons b); + +//resets the remote so we start from ch 1 +void reset (); + +//lowers the shutter to the percentage level described in the shutter structure +void lower (shutter *r); +#endif //_MOZGATAS_H_ diff --git a/mozgatas.c b/mozgatas.c deleted file mode 100644 index e54c993..0000000 --- a/mozgatas.c +++ /dev/null @@ -1,70 +0,0 @@ -// -// Created by lacko on 08/08/2021. -// - -#include "mozgatas.h" -#include -#include //ugye openwrt-re fordul - -void set_ch (redony *r) -{ - reset (); - for (int i = 1; i < r->ch; ++i) - { - press_button (next); - } -} - -/* -void export () -{ - printf ("exporting...\n"); - FILE *export = fopen ("/sys/class/gpio/export", "w"); - int gpios[] = {2, 17, 18, 19, 20, 21}; - for (int i = 0; i < 6; ++i) - { - fprintf (export, "%d", gpios[i]); - char fname[33] = {'\0'}; - sprintf (fname, "/sys/class/gpio/gpio%d/direction", gpios[i]); - FILE *gpio = fopen (fname, "w"); - fprintf (gpio, "out"); - } - printf ("exporting done.\n"); - -}*/ - -//doing negative logic as reset is active low and GPIOs are 1 on startup -void reset () -{ - //printf ("resetting...\n"); - - FILE *gpio2 = fopen ("/sys/class/gpio/gpio2/value", "w"); - fprintf (gpio2, "0"); - fclose (gpio2); - sleep (1); - gpio2 = fopen ("/sys/class/gpio/gpio2/value", "w"); - fprintf (gpio2, "1"); - fclose (gpio2); - sleep (3); - - //printf ("reset done\n"); -} - -//doing positive logic as GPIOs are 0 on startup -void press_button (buttons b) -{ - //printf ("activating gpio %d...\n", b); - - char fname[29] = {'\0'}; - sprintf (fname, "/sys/class/gpio/gpio%d/value", b); - FILE *gpio = fopen (fname, "w"); - fprintf (gpio, "0"); - fclose (gpio); - gpio = fopen (fname, "w"); - usleep (200000); - fprintf (gpio, "1"); - fclose (gpio); - sleep (1); - - //printf ("done\n"); -} \ No newline at end of file diff --git a/mozgatas.h b/mozgatas.h deleted file mode 100644 index 4a55569..0000000 --- a/mozgatas.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by lacko on 08/08/2021. -// Redőnyök mozgatásáért felelős függvényeket tartalmaz -// -#include "redony.h" -#ifndef _MOZGATAS_H_ -#define _MOZGATAS_H_ - - -//beállítja a távirányítón a kívánt redőny csatornáját -void set_ch (redony *r); - -//megnyomja a kívánt gombot -void press_button (buttons b); - -//exportálja a használt GPIO-kat -void export (); - -//reseteli a távirányítót, hogy a 0-ás csatornáról kezdjünk -void reset (); - - -#endif //_MOZGATAS_H_ diff --git a/mozgatas_demo.c b/mozgatas_demo.c index 820b2bc..0e4d27b 100644 --- a/mozgatas_demo.c +++ b/mozgatas_demo.c @@ -5,19 +5,13 @@ #include "mozgatas_demo.h" #include -void set_ch (redony *r) +void set_ch (shutter *r) { reset (); for (int i = 1; i <= r->ch; ++i) printf ("ch. %d\n", i); } - -void export () -{ - printf ("gpios exported\n"); -} - //reset láb active low, de negatív logikával van bekötve void reset () { diff --git a/mozgatas_demo.h b/mozgatas_demo.h index abaaa16..ed34ee1 100644 --- a/mozgatas_demo.h +++ b/mozgatas_demo.h @@ -4,17 +4,14 @@ #ifndef _MOZGATAS_DEMO_H_ #define _MOZGATAS_DEMO_H_ -#include "redony.h" +#include "shutter.h" //beállítja a távirányítón a kívánt redőny csatornáját -void set_ch (redony *r); +void set_ch (shutter *r); //megnyomja a kívánt gombot void press_button (buttons b); -//exportálja a használt GPIO-kat -void export (); - //reseteli a távirányítót, hogy a 0-ás csatornáról kezdjünk void reset (); diff --git a/shutter.h b/shutter.h index 3517608..0219d3e 100644 --- a/shutter.h +++ b/shutter.h @@ -2,32 +2,31 @@ // Created by lacko on 08/08/2021. // -#ifndef _REDONY_H_ -#define _REDONY_H_ +#ifndef _SHUTTER_H_ +#define _SHUTTER_H_ #include typedef char byte; -typedef struct ido { +typedef struct minitime { byte tm_hour; byte tm_min; -} ido; +} minitime; -typedef struct redony { - byte ch; - ido up; - ido down; - byte rolltime_up; //measured in seconds - byte rolltime_down; //measured in seconds - byte percentage; -} redony; +typedef struct shutter { + byte ch; //assigned channel on the connected remote + minitime up; //time of raise + minitime down; //time of lowering + byte rolltime_down; //measured in seconds, measured when the shutter is rolling down (usually faster than rolling up) + byte percentage; //the percentage to which the shutter is to be lowered +} shutter; typedef enum buttons { - up = 17, + up = 17, //I'm lucky, I found 5 consecutively numbered gpios, starting with gpio 17 stop, down, prev, next } buttons; -#endif //_REDONY_H_ +#endif //_SHUTTER_H_ diff --git a/timing.c b/timing.c index 12b83e3..c1c347a 100644 --- a/timing.c +++ b/timing.c @@ -2,10 +2,9 @@ // Created by lacko on 08/08/2021. // -#include "idozites.h" +#include "timing.h" #include -//function, which navigates the file, and stops at the label of today. int find_today (FILE *schedule) { time_t t = time (NULL); @@ -17,31 +16,31 @@ int find_today (FILE *schedule) switch (now->tm_wday) { case 0: - if (strcasecmp (buffer, "VAS") == 0) + if (strcmp (buffer, "VAS") == 0) return 1; break; case 1: - if (strcasecmp (buffer, "HET") == 0) + if (strcmp (buffer, "HET") == 0) return 1; break; case 2: - if (strcasecmp (buffer, "KED") == 0) + if (strcmp (buffer, "KED") == 0) return 1; break; case 3: - if (strcasecmp (buffer, "SZE") == 0) + if (strcmp (buffer, "SZE") == 0) return 1; break; case 4: - if (strcasecmp (buffer, "CSU") == 0) + if (strcmp (buffer, "CSU") == 0) return 1; break; case 5: - if (strcasecmp (buffer, "PEN") == 0) + if (strcmp (buffer, "PEN") == 0) return 1; break; case 6: - if (strcasecmp (buffer, "SZO") == 0) + if (strcmp (buffer, "SZO") == 0) return 1; break; } @@ -77,13 +76,16 @@ void get_timing (shutter *r, FILE *schedule) while (fscanf (schedule, "%d:%d", &r->down.tm_hour, &r->down.tm_min) != 2) fscanf (schedule, "%*c"); - fscanf (schedule, "%*[^\n]s\n"); + while (fscanf (schedule, "%d", &r->percentage) != 1) + fscanf (schedule, "%*c"); + + fscanf (schedule, "%*[^\n]s\n"); } buttons check_timing (shutter *r) { - if ((r->up.tm_hour == r->down.tm_hour) && (r->up.tm_min == r->down.tm_min)) + if (r->percentage == 0) return stop; time_t t = time (NULL); struct tm *now = localtime (&t); diff --git a/timing.h b/timing.h index af5de06..2082adc 100644 --- a/timing.h +++ b/timing.h @@ -1,19 +1,23 @@ // // Created by lacko on 08/08/2021. // -#ifndef _IDOZITES_H_ -#define _IDOZITES_H_ +#ifndef _TIMING_H_ +#define _TIMING_H_ #include "shutter.h" #include #include +//reads the timing and percentage form the schedule file void get_timing (shutter *r, FILE *schedule); +//checks whether it's time to raise/lower and acts accordingly buttons check_timing (shutter *r); +//checks the schedule file and sets the readhead to today's label int find_today (FILE *schedule); +//sets the readhead to the next day-label int find_next_day (FILE *f); -#endif //_IDOZITES_H_ \ No newline at end of file +#endif //_TIMING_H_ \ No newline at end of file