mirror of
https://github.com/Derisis13/Shutter.git
synced 2025-12-06 19:32:48 +01:00
Changed comments, variable names and filenames to be in English
This commit is contained in:
@@ -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
|
||||
)
|
||||
58
main.c
58
main.c
@@ -1,44 +1,44 @@
|
||||
#include "idozites.h"
|
||||
#include "mozgatas_demo.h"
|
||||
#include "timing.h"
|
||||
#include "moving.h"
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
60
moving.c
Normal file
60
moving.c
Normal file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by lacko on 08/08/2021.
|
||||
//
|
||||
|
||||
#include "moving.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h> //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);
|
||||
}
|
||||
20
moving.h
Normal file
20
moving.h
Normal file
@@ -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_
|
||||
70
mozgatas.c
70
mozgatas.c
@@ -1,70 +0,0 @@
|
||||
//
|
||||
// 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
23
mozgatas.h
@@ -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_
|
||||
@@ -5,19 +5,13 @@
|
||||
#include "mozgatas_demo.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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 ()
|
||||
{
|
||||
|
||||
@@ -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 ();
|
||||
|
||||
|
||||
27
shutter.h
27
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 <time.h>
|
||||
|
||||
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_
|
||||
|
||||
24
timing.c
24
timing.c
@@ -2,10 +2,9 @@
|
||||
// Created by lacko on 08/08/2021.
|
||||
//
|
||||
|
||||
#include "idozites.h"
|
||||
#include "timing.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);
|
||||
@@ -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);
|
||||
|
||||
10
timing.h
10
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 <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
//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_
|
||||
#endif //_TIMING_H_
|
||||
Reference in New Issue
Block a user