Add display code to display.c file

This commit is contained in:
Martijn Scheepers
2019-01-31 10:14:33 +01:00
parent bbac6c3a08
commit d8b5a00376
7 changed files with 166 additions and 152 deletions

141
display.c Normal file
View File

@@ -0,0 +1,141 @@
#define _GNU_SOURCE
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "config.h"
#include "web.h"
#include "log.h"
#include "jsontypes.h"
#include "display.h"
#define CONTENT_LENGTH 21
#define MENU_LENGTH 8
#define MENU_SIZE 4
#define MENU_OFFSET 80
//write display line to h1502display char device
int write_device(char *ln){
int fd;
ssize_t ret;
fd = open("/dev/h1502display", O_WRONLY);
if (fd == -1) {
perror("open /dev/h1502display: ");
return -1;
}
ret = write(fd, ln, strlen(ln));
if(ret < (int)strlen(ln)){
perror("write /dev/h1502display: ");
close(fd);
return -1;
}
close(fd);
return 0;
}
//write display line to h1502display char device
/*
int fwrite_device(char *line){
FILE *fd;
fd = fopen("/dev/h1502display", "w");
if (fd == NULL) {
writesyslog(LOG_ERR, "write_device: opening /dev/h1502display failed");
return -1;
}
if(fwrite(line, sizeof(char), strlen(line), fd) != strlen(line)){
writesyslog(LOG_ERR, "write_device: writing /dev/h1502display failed");
fclose(fd);
return -1;
}
fclose(fd);
return 0;
}
*/
//write data to de h1502display device
int write_line(int y, int x, int invert, char *txt){
char *buf;
int ret;
if(strlen(txt) <= 0 ){
return -1;
}
buf = malloc((strlen(txt) + 8) * sizeof(char));
if(buf == NULL){
writesyslog(LOG_ERR, "write_line: out off memory");
return -1;
}
sprintf(buf, "%03d%03d%d%s", x, y, invert, txt);
ret = write_device(buf);
free(buf);
return ret;
}
void clear_display(void){
#ifndef TEST
write_line(ln[0], MENU_OFFSET, 0, " ");
write_line(ln[1], 0, 0, " ");
write_line(ln[2], 0, 0, " ");
write_line(ln[3], MENU_OFFSET, 0, " ");
#else
printf("-- Clear display timer --\n");
#endif
}
void write_display(int ln, int pos, int invert, char *txt){
#ifdef TEST
printf("Display write : %s\n", txt);
#else
write_line(ln, pos, invert, txt);
#endif
}
volatile int displaytimersemaphore = 0;
void display_timer_handler(union sigval arg){
displaytimersemaphore -= 1;
if(displaytimersemaphore == 0){
clear_display();
}
}
void display_timer(int time){
timer_t timer_id;
struct itimerspec ts;
struct sigevent se;
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = display_timer_handler;
se.sigev_notify_attributes = NULL;
ts.it_value.tv_sec = time;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
if (status == -1)
printf("error create display timer\n");
status = timer_settime(timer_id, 0, &ts, 0);
if (status == -1)
printf("error set display timer\n");
displaytimersemaphore += 1;
}

12
display.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef DISPLAY_FILE
#define DISPLAY_FILE
static int line[] = {
0, 8, 16, 24,
};
void clear_display(void);
void write_display(int line, int pos, int invert, char *txt);
void display_timer(int time);
#endif

View File

@@ -18,7 +18,7 @@
#include "web.h"
#include "relay.h"
#include "flags.h"
#include "display.h"
int input_fd_open(long int input){
char *fle, *flv;
@@ -99,10 +99,10 @@ void *readinputs(void *ptr){
}
clear_display();
write_display(ln[1], 0, 0, "Manual open");
write_display(line[1], 0, 0, "Manual open");
char *ln2;
asprintf(&ln2, "In: %ld - Relay: %ld", config->manualinput, config->relay);
write_display(ln[2], 0, 0, ln2);
write_display(line[2], 0, 0, ln2);
free(ln2);
display_timer(config->switchtime);

133
log.c
View File

@@ -1,21 +1,6 @@
#define _GNU_SOURCE
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "config.h"
#include "web.h"
#include "log.h"
#include "jsontypes.h"
#define CONTENT_LENGTH 21
#define MENU_LENGTH 8
#define MENU_SIZE 4
#define MENU_OFFSET 80
void writesyslog(int priority, const char *logtext){
#ifdef TEST
@@ -26,121 +11,3 @@ void writesyslog(int priority, const char *logtext){
syslog(priority, "%s", logtext);
closelog();
}
//write display line to h1502display char device
int write_device(char *line){
int fd;
ssize_t ret;
fd = open("/dev/h1502display", O_WRONLY);
if (fd == -1) {
perror("open /dev/h1502display: ");
return -1;
}
ret = write(fd, line, strlen(line));
if(ret < (int)strlen(line)){
perror("write /dev/h1502display: ");
close(fd);
return -1;
}
close(fd);
return 0;
}
//write display line to h1502display char device
int fwrite_device(char *line){
FILE *fd;
fd = fopen("/dev/h1502display", "w");
if (fd == NULL) {
writesyslog(LOG_ERR, "write_device: opening /dev/h1502display failed");
return -1;
}
if(fwrite(line, sizeof(char), strlen(line), fd) != strlen(line)){
writesyslog(LOG_ERR, "write_device: writing /dev/h1502display failed");
fclose(fd);
return -1;
}
fclose(fd);
return 0;
}
//write data to de h1502display device
int write_line(int y, int x, int invert, char *txt){
char *buf;
int ret;
if(strlen(txt) <= 0 ){
return -1;
}
buf = malloc((strlen(txt) + 8) * sizeof(char));
if(buf == NULL){
writesyslog(LOG_ERR, "write_line: out off memory");
return -1;
}
sprintf(buf, "%03d%03d%d%s", x, y, invert, txt);
ret = write_device(buf);
free(buf);
return ret;
}
void clear_display(void){
#ifndef TEST
write_line(ln[0], MENU_OFFSET, 0, " ");
write_line(ln[1], 0, 0, " ");
write_line(ln[2], 0, 0, " ");
write_line(ln[3], MENU_OFFSET, 0, " ");
#endif
}
void write_display(int line, int pos, int invert, char *txt){
#ifdef TEST
printf("Display write : %s\n", txt);
#else
write_line(line, pos, invert, txt);
#endif
}
volatile int displaytimersemaphore = 0;
void display_timer_handler(union sigval arg){
displaytimersemaphore -= 1;
if(displaytimersemaphore == 0){
clear_display();
printf("------------------------------ Clear display timer --------------------------------\n");
}
}
void display_timer(int time){
timer_t timer_id;
struct itimerspec ts;
struct sigevent se;
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = display_timer_handler;
se.sigev_notify_attributes = NULL;
ts.it_value.tv_sec = time;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
if (status == -1)
printf("error create display timer\n");
status = timer_settime(timer_id, 0, &ts, 0);
if (status == -1)
printf("error set display timer\n");
displaytimersemaphore += 1;
}

8
log.h
View File

@@ -3,12 +3,4 @@
void writesyslog(int priority, const char *logtext);
static int ln[] = {
0, 8, 16, 24,
};
void clear_display(void);
void write_display(int line, int pos, int invert, char *txt);
void display_timer(int time);
#endif

5
main.c
View File

@@ -18,6 +18,7 @@
#include "inputs.h"
#include "reader.h"
#include "flags.h"
#include "display.h"
#define STARTMSG "Starting acsreader version " VERSION
@@ -27,8 +28,8 @@
int main(int argc, char **argv){
writesyslog(LOG_INFO, "Starting ACSreader\n");
clear_display();
write_display(ln[1], 0, 0, "Starting");
write_display(ln[2], 0, 0, "ACSreader");
write_display(line[1], 0, 0, "Starting");
write_display(line[2], 0, 0, "ACSreader");
display_timer(5);
createpidfile("/var/run/acsreader.pid");

View File

@@ -19,6 +19,7 @@
#include "inputs.h"
#include "reader.h"
#include "relay.h"
#include "display.h"
void readerrequest(struct reader_t *reader, struct config_t *config){
server_credential_request(reader, config);
@@ -41,8 +42,8 @@ void readerrequest(struct reader_t *reader, struct config_t *config){
char *log;
if(asprintf(&log, "CARD ID %d", reader->cardnumber) > 0){
clear_display();
write_display(ln[1], 0, 0, log);
write_display(ln[2], 0, 0, "access granted");
write_display(line[1], 0, 0, log);
write_display(line[2], 0, 0, "access granted");
display_timer(reader->opentime);
}
free(log);
@@ -59,8 +60,8 @@ void readerrequest(struct reader_t *reader, struct config_t *config){
char *log;
if(asprintf(&log, "CARD ID %d", reader->cardnumber) > 0){
clear_display();
write_display(ln[1], 0, 0, log);
write_display(ln[2], 0, 0, "access denied");
write_display(line[1], 0, 0, log);
write_display(line[2], 0, 0, "access denied");
display_timer(3);
}
free(log);
@@ -113,8 +114,8 @@ void readerrequest(struct reader_t *reader, struct config_t *config){
}
if(len > 0){
clear_display();
write_display(ln[1], 0, 0, ln1);
write_display(ln[2], 0, 0, ln2);
write_display(line[1], 0, 0, ln1);
write_display(line[2], 0, 0, ln2);
writesyslog(LOG_ERR, log);
display_timer(5);
}