add: gpio drivers
This commit is contained in:
		
							parent
							
								
									ba70677393
								
							
						
					
					
						commit
						3cd6668f9c
					
				
					 8 changed files with 131 additions and 76 deletions
				
			
		| 
						 | 
					@ -7,5 +7,8 @@
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
driver_gpio_set(relay_t *relay, int value)
 | 
					driver_gpio_set(relay_t *relay, int value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    // disable "unused parameter" warning (happens when using wiring_debug)
 | 
				
			||||||
 | 
					    (void)relay;
 | 
				
			||||||
 | 
					    (void)value;
 | 
				
			||||||
    digitalWrite(relay->number, value);
 | 
					    digitalWrite(relay->number, value);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,5 +7,8 @@
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
driver_piface_set(relay_t *relay, int value)
 | 
					driver_piface_set(relay_t *relay, int value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    // disable "unused parameter" warning (happens when using wiring_debug)
 | 
				
			||||||
 | 
					    (void)relay;
 | 
				
			||||||
 | 
					    (void)value;
 | 
				
			||||||
    digitalWrite(DRIVER_PIFACE_GPIO_BASE + relay->number, value);
 | 
					    digitalWrite(DRIVER_PIFACE_GPIO_BASE + relay->number, value);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										46
									
								
								handlers/loop.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								handlers/loop.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,46 @@
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <logger.h>
 | 
				
			||||||
 | 
					#include <models/controller.h>
 | 
				
			||||||
 | 
					#include <handlers.h>
 | 
				
			||||||
 | 
					#include <drivers.h>
 | 
				
			||||||
 | 
					#include <enums.h>
 | 
				
			||||||
 | 
					#include <helpers.h>
 | 
				
			||||||
 | 
					#include <wiringPi.h>
 | 
				
			||||||
 | 
					#include <wiring_debug.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					handler_loop(controller_t *controller)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    LOG_DEBUG("===== IDLE LOOP START =====");
 | 
				
			||||||
 | 
					    for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        relay_t *relay = controller->relays[i];
 | 
				
			||||||
 | 
					        if(relay_is_active(relay, time(NULL)))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            LOG_DEBUG("relay %d is active", i);
 | 
				
			||||||
 | 
					            if(relay->number >= 2)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                driver_gpio_set(relay, HIGH);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                driver_piface_set(relay, HIGH);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if(relay->number >= 2)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                driver_gpio_set(relay, LOW);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                driver_piface_set(relay, LOW);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										50
									
								
								handlers/poll.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								handlers/poll.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,50 @@
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					#include <lmdb.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					#include <sys/socket.h>
 | 
				
			||||||
 | 
					#include <poll.h>
 | 
				
			||||||
 | 
					#include <signal.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <logger.h>
 | 
				
			||||||
 | 
					#include <models/controller.h>
 | 
				
			||||||
 | 
					#include <database.h>
 | 
				
			||||||
 | 
					#include <config.h>
 | 
				
			||||||
 | 
					#include <constants.h>
 | 
				
			||||||
 | 
					#include <handlers.h>
 | 
				
			||||||
 | 
					#include <drivers.h>
 | 
				
			||||||
 | 
					#include <enums.h>
 | 
				
			||||||
 | 
					#include <helpers.h>
 | 
				
			||||||
 | 
					#include <wiringPi.h>
 | 
				
			||||||
 | 
					#include <piFace.h>
 | 
				
			||||||
 | 
					#include <wiring_debug.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					handler_poll(struct pollfd *fds, controller_t *controller, MDB_env *mdb_env)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /* An event on one of the fds has occurred. */
 | 
				
			||||||
 | 
					    for(int i = 0; i < POLL_FDS_COUNT; i++) {
 | 
				
			||||||
 | 
					        if(fds[i].revents & POLLIN)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* data may be read on device number i. */
 | 
				
			||||||
 | 
					            LOG_DEBUG("fd %i may read data", fds[i].fd);
 | 
				
			||||||
 | 
					            switch(i)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                case POLL_FDS_DISCOVERY:
 | 
				
			||||||
 | 
					                    handler_discovery(fds[i].fd, controller);
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                case POLL_FDS_COMMAND:
 | 
				
			||||||
 | 
					                    handler_command(fds[i].fd, controller);
 | 
				
			||||||
 | 
					                    controller_save(controller, mdb_env);
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if(fds[i].revents & POLLHUP)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* A hangup has occurred on device number i. */
 | 
				
			||||||
 | 
					            LOG_DEBUG("fd %i got closed", fds[i].fd);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
#define SECONDS_PER_DAY 86400 // 60 * 60 * 24
 | 
					#define SECONDS_PER_DAY 86400 // 60 * 60 * 24
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define SECONDS_PER_MINUTE 60
 | 
					#define SECONDS_PER_MINUTE 60
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define POLL_FDS_COUNT 2
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,8 +3,8 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum poll_fgs
 | 
					enum poll_fgs
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    POLL_FGS_DISCOVERY,
 | 
					    POLL_FDS_DISCOVERY,
 | 
				
			||||||
    POLL_FGS_COMMAND
 | 
					    POLL_FDS_COMMAND
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum discovery_mapping
 | 
					enum discovery_mapping
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
#ifndef CONTROLLER_HANDLERS_H
 | 
					#ifndef CONTROLLER_HANDLERS_H
 | 
				
			||||||
#define CONTROLLER_HANDLERS_H
 | 
					#define CONTROLLER_HANDLERS_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <poll.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <models/controller.h>
 | 
					#include <models/controller.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -21,4 +23,10 @@ handler_command(int fd, controller_t *controller);
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
handler_discovery(int fd, controller_t *controller);
 | 
					handler_discovery(int fd, controller_t *controller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					handler_loop(controller_t *this_controller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					handler_poll(struct pollfd *fds, controller_t *controller, MDB_env *mdb_env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CONTROLLER_HANDLERS_H */
 | 
					#endif /* CONTROLLER_HANDLERS_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										91
									
								
								main.c
									
										
									
									
									
								
							
							
						
						
									
										91
									
								
								main.c
									
										
									
									
									
								
							| 
						 | 
					@ -12,6 +12,7 @@
 | 
				
			||||||
#include <models/controller.h>
 | 
					#include <models/controller.h>
 | 
				
			||||||
#include <database.h>
 | 
					#include <database.h>
 | 
				
			||||||
#include <config.h>
 | 
					#include <config.h>
 | 
				
			||||||
 | 
					#include <constants.h>
 | 
				
			||||||
#include <handlers.h>
 | 
					#include <handlers.h>
 | 
				
			||||||
#include <drivers.h>
 | 
					#include <drivers.h>
 | 
				
			||||||
#include <enums.h>
 | 
					#include <enums.h>
 | 
				
			||||||
| 
						 | 
					@ -21,17 +22,18 @@
 | 
				
			||||||
#include <wiring_debug.h>
 | 
					#include <wiring_debug.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static MDB_env *mdb_env;
 | 
					static MDB_env *mdb_env;
 | 
				
			||||||
static int fd_discovery;
 | 
					 | 
				
			||||||
static int fd_command;
 | 
					 | 
				
			||||||
static controller_t *this_controller;
 | 
					static controller_t *this_controller;
 | 
				
			||||||
 | 
					static struct pollfd poll_fds[POLL_FDS_COUNT];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
terminate(int signum)
 | 
					terminate(int signum)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    LOG_INFO("terminating controller (%d)", signum);
 | 
					    LOG_INFO("terminating controller (%d)", signum);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    close(fd_discovery);
 | 
					    for(int i = 0; i < POLL_FDS_COUNT; ++i)
 | 
				
			||||||
    close(fd_command);
 | 
					    {
 | 
				
			||||||
 | 
					        close(poll_fds[i].fd);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    mdb_env_close(mdb_env);
 | 
					    mdb_env_close(mdb_env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -40,34 +42,6 @@ terminate(int signum)
 | 
				
			||||||
    exit(signum);
 | 
					    exit(signum);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					 | 
				
			||||||
handle_poll(struct pollfd *fds, int fd_count)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    /* An event on one of the fds has occurred. */
 | 
					 | 
				
			||||||
    for(int i = 0; i < fd_count; i++) {
 | 
					 | 
				
			||||||
        if(fds[i].revents & POLLIN)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            /* data may be read on device number i. */
 | 
					 | 
				
			||||||
            LOG_DEBUG("fd %i may read data", fds[i].fd);
 | 
					 | 
				
			||||||
            switch(i)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                case POLL_FGS_DISCOVERY:
 | 
					 | 
				
			||||||
                    handler_discovery(fd_discovery, this_controller);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case POLL_FGS_COMMAND:
 | 
					 | 
				
			||||||
                    handler_command(fd_command, this_controller);
 | 
					 | 
				
			||||||
                    controller_save(this_controller, mdb_env);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if(fds[i].revents & POLLHUP)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            /* A hangup has occurred on device number i. */
 | 
					 | 
				
			||||||
            LOG_DEBUG("fd %i got closed", fds[i].fd);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief The main function
 | 
					 * @brief The main function
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
| 
						 | 
					@ -97,8 +71,8 @@ main(int argc, char** argv)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this_controller = controller_load(mdb_env);
 | 
					    this_controller = controller_load(mdb_env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fd_discovery = helper_open_discovery_socket(this_controller->discovery_port);
 | 
					    int fd_discovery = helper_open_discovery_socket(this_controller->discovery_port);
 | 
				
			||||||
    fd_command = helper_bind_tcp_server("0.0.0.0", this_controller->command_port, 128);
 | 
					    int fd_command = helper_bind_tcp_server("0.0.0.0", this_controller->command_port, 128);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this_controller->command_port = helper_get_port(fd_command);
 | 
					    this_controller->command_port = helper_get_port(fd_command);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -113,61 +87,30 @@ main(int argc, char** argv)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /******************** SETUP SOCKETS ********************/
 | 
					    /******************** SETUP SOCKETS ********************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    struct pollfd fds[2];
 | 
					 | 
				
			||||||
    int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS;
 | 
					    int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS;
 | 
				
			||||||
    int ret;
 | 
					    int ret;
 | 
				
			||||||
    int fd_count = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Open STREAMS device. */
 | 
					    /* Open STREAMS device. */
 | 
				
			||||||
    fds[POLL_FGS_DISCOVERY].fd = fd_discovery;
 | 
					    poll_fds[POLL_FDS_DISCOVERY].fd = fd_discovery;
 | 
				
			||||||
    fds[POLL_FGS_DISCOVERY].events = POLLIN;
 | 
					    poll_fds[POLL_FDS_DISCOVERY].events = POLLIN;
 | 
				
			||||||
    LOG_DEBUG("setup fd_discovery as %i on index %i", fd_discovery, fd_count);
 | 
					    LOG_DEBUG("setup fd_discovery as %i on index %i", fd_discovery, POLL_FDS_DISCOVERY);
 | 
				
			||||||
    fd_count++;
 | 
					    poll_fds[POLL_FDS_COMMAND].fd = fd_command;
 | 
				
			||||||
    fds[POLL_FGS_COMMAND].fd = fd_command;
 | 
					    poll_fds[POLL_FDS_COMMAND].events = POLLIN;
 | 
				
			||||||
    fds[POLL_FGS_COMMAND].events = POLLIN;
 | 
					    LOG_DEBUG("setup fd_command as %i on index %i", fd_command, POLL_FDS_COMMAND);
 | 
				
			||||||
    LOG_DEBUG("setup fd_command as %i on index %i", fd_command, fd_count);
 | 
					 | 
				
			||||||
    fd_count++;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /******************** START MAIN LOOP ********************/
 | 
					    /******************** START MAIN LOOP ********************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for(;;)
 | 
					    for(;;)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        ret = poll(fds, fd_count, timeout_msecs);
 | 
					        ret = poll(poll_fds, POLL_FDS_COUNT, timeout_msecs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if(ret == 0)
 | 
					        if(ret == 0)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            LOG_DEBUG("===== IDLE LOOP START =====");
 | 
					            handler_loop(this_controller);
 | 
				
			||||||
            for(uint_fast8_t i = 0; i < this_controller->relay_count; ++i)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                relay_t *relay = this_controller->relays[i];
 | 
					 | 
				
			||||||
                if(relay_is_active(relay, time(NULL)))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    LOG_DEBUG("relay %d is active", i);
 | 
					 | 
				
			||||||
                    if(relay->number >= 2)
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        driver_gpio_set(relay, HIGH);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    else
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        driver_piface_set(relay, HIGH);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                else
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    if(relay->number >= 2)
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        driver_gpio_set(relay, LOW);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    else
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        driver_piface_set(relay, LOW);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if(ret > 0)
 | 
					        if(ret > 0)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            handle_poll(fds, fd_count);
 | 
					            handler_poll(poll_fds, this_controller, mdb_env);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue