commit 877614b7a3490381a865d0a9c6d26d63d145c41e Author: Derisis13 Date: Mon Aug 30 17:21:29 2021 +0200 Version 1.1: Second working version. First commit. Added features: disabling channels for the day by having the same raise and lower timings. diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..55f4815 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.19) +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 + ) \ No newline at end of file diff --git a/idozites.c b/idozites.c new file mode 100644 index 0000000..7981971 --- /dev/null +++ b/idozites.c @@ -0,0 +1,101 @@ +// +// Created by lacko on 08/08/2021. +// + +#include "idozites.h" +#include + +//function, which navigates the file, and stops at the label of today. +int find_today (FILE *schedule) +{ + time_t t = time (NULL); + struct tm *now = localtime (&t); + char buffer[4] = {0}; + while (find_next_day (schedule) == 1) + { + fgets (buffer, 4, schedule); + switch (now->tm_wday) + { + case 0: + if (strcasecmp (buffer, "VAS") == 0) + return 1; + break; + case 1: + if (strcasecmp (buffer, "HET") == 0) + return 1; + break; + case 2: + if (strcasecmp (buffer, "KED") == 0) + return 1; + break; + case 3: + if (strcasecmp (buffer, "SZE") == 0) + return 1; + break; + case 4: + if (strcasecmp (buffer, "CSU") == 0) + return 1; + break; + case 5: + if (strcasecmp (buffer, "PEN") == 0) + return 1; + break; + case 6: + if (strcasecmp (buffer, "SZO") == 0) + return 1; + break; + } + } + return 0; +} + +int find_next_day (FILE *f) +{ + char c; + while (fscanf (f, "%c", &c) == 1) + { + if (c == '\n' && fgetc (f) == '\n') + return 1; + } + return 0; +} + +void get_timing (redony *r, FILE *schedule) +{ + int channel = 0; + while (channel != r->ch) + { + fscanf (schedule, "%*[^\n]s\n"); + while (fscanf (schedule, "%d", &channel) != 1) + { + fscanf (schedule, "%*c"); + } + } + + while (fscanf (schedule, "%d:%d", &r->up.tm_hour, &r->up.tm_min) != 2) + fscanf (schedule, "%*c"); + + while (fscanf (schedule, "%d:%d", &r->down.tm_hour, &r->down.tm_min) != 2) + fscanf (schedule, "%*c"); + fscanf (schedule, "%*[^\n]s\n"); + +} + +buttons check_timing (redony *r) +{ + if ((r->up.tm_hour == r->down.tm_hour) && (r->up.tm_min == r->down.tm_min)) + 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)) + { + return down; + } + + if ((r->up.tm_hour == now->tm_hour) && (r->up.tm_min == now->tm_min)) + { + return up; + } + + return stop; +} diff --git a/idozites.h b/idozites.h new file mode 100644 index 0000000..222ded5 --- /dev/null +++ b/idozites.h @@ -0,0 +1,19 @@ +// +// Created by lacko on 08/08/2021. +// +#ifndef _IDOZITES_H_ +#define _IDOZITES_H_ + +#include "redony.h" +#include +#include + +void get_timing (redony *r, FILE *schedule); + +buttons check_timing (redony *r); + +int find_today (FILE *schedule); + +int find_next_day (FILE *f); + +#endif //_IDOZITES_H_ \ No newline at end of file diff --git a/main.c b/main.c new file mode 100755 index 0000000..4a88243 --- /dev/null +++ b/main.c @@ -0,0 +1,62 @@ +#include "idozites.h" +#include "mozgatas_demo.h" +#include +#define FILEPATH "menetrend.txt" +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * REDŐNY 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) + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +int main () +{ + redony ercsi13[] = { + { + 1, 0, 0 //fiúszoba + }, + { + 2, 0, 0 //lányszoba + }, + { + 3, 0, 0 //nappali bal + }, + { + 4, 0, 0 //nappali jobb + }, + { + 5, 0, 0 //szülői szoba + }, + { + 6, 0, 0 //konyha + }, + { + 7, 0, 0 //előtető (lehúzás = kieresztés, felhúzás = behúzás) + } + }; + + //beolvassuk minden redőny időpontját + for (int i = 0; i < 7; ++i) + { + FILE *schedule = fopen (FILEPATH, "r"); + find_today (schedule); + get_timing (&ercsi13[i], schedule); + fclose (schedule); + } + + /* + //exportáljuk a gpio-kat, hogy tudjuk használni őket + export (); + */ + + //leellenőrizzük az időpontokat, minden redőny úgy mozog, ahhogy azt megmondtuk neki. + for (int i = 0; i < 7; ++i) + { + set_ch (&ercsi13[i]); + press_button (check_timing (&ercsi13[i])); + } + return 0; +} \ No newline at end of file diff --git a/mozgatas.c b/mozgatas.c new file mode 100644 index 0000000..e54c993 --- /dev/null +++ b/mozgatas.c @@ -0,0 +1,70 @@ +// +// 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 new file mode 100644 index 0000000..4a55569 --- /dev/null +++ b/mozgatas.h @@ -0,0 +1,23 @@ +// +// 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 new file mode 100644 index 0000000..820b2bc --- /dev/null +++ b/mozgatas_demo.c @@ -0,0 +1,43 @@ +// +// Created by lacko on 19/08/2021. +// + +#include "mozgatas_demo.h" +#include + +void set_ch (redony *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 () +{ + printf ("remote reset\n"); +} + +void press_button (buttons b) +{ + switch (b) + { + case up: printf ("up\n"); + break; + case down: printf ("down\n"); + break; + case stop: printf ("stop\n"); + break; + case prev: printf ("previous\n"); + break; + case next: printf ("next ch\n"); + break; + default: printf ("hiba\n"); + } +} \ No newline at end of file diff --git a/mozgatas_demo.h b/mozgatas_demo.h new file mode 100644 index 0000000..abaaa16 --- /dev/null +++ b/mozgatas_demo.h @@ -0,0 +1,21 @@ +// +// Created by lacko on 19/08/2021. +// + +#ifndef _MOZGATAS_DEMO_H_ +#define _MOZGATAS_DEMO_H_ +#include "redony.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_DEMO_H_ diff --git a/redony.h b/redony.h new file mode 100644 index 0000000..0427cef --- /dev/null +++ b/redony.h @@ -0,0 +1,29 @@ +// +// Created by lacko on 08/08/2021. +// + +#ifndef _REDONY_H_ +#define _REDONY_H_ + +#include + + +typedef struct ido { + int tm_hour; + int tm_min; +} ido; + +typedef struct redony { + int ch; + ido up; + ido down; +} redony; + +typedef enum buttons { + up = 17, + stop, + down, + prev, + next +} buttons; +#endif //_REDONY_H_