Version 1.1: Second working version. First commit. Added features: disabling channels for the day by having the same raise and lower timings.

This commit is contained in:
2021-08-30 17:21:29 +02:00
commit 877614b7a3
9 changed files with 378 additions and 0 deletions

10
CMakeLists.txt Executable file
View File

@@ -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
)

101
idozites.c Normal file
View File

@@ -0,0 +1,101 @@
//
// Created by lacko on 08/08/2021.
//
#include "idozites.h"
#include <string.h>
//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;
}

19
idozites.h Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by lacko on 08/08/2021.
//
#ifndef _IDOZITES_H_
#define _IDOZITES_H_
#include "redony.h"
#include <time.h>
#include <stdio.h>
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_

62
main.c Executable file
View File

@@ -0,0 +1,62 @@
#include "idozites.h"
#include "mozgatas_demo.h"
#include <stdio.h>
#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;
}

70
mozgatas.c Normal file
View File

@@ -0,0 +1,70 @@
//
// Created by lacko on 08/08/2021.
//
#include "mozgatas.h"
#include <stdio.h>
#include <unistd.h> //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");
}

23
mozgatas.h Normal file
View File

@@ -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_

43
mozgatas_demo.c Normal file
View File

@@ -0,0 +1,43 @@
//
// Created by lacko on 19/08/2021.
//
#include "mozgatas_demo.h"
#include <stdio.h>
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");
}
}

21
mozgatas_demo.h Normal file
View File

@@ -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_

29
redony.h Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by lacko on 08/08/2021.
//
#ifndef _REDONY_H_
#define _REDONY_H_
#include <time.h>
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_