diff --git a/Makefile b/Makefile index 6847773..b2c7344 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g CC_LIBS=-pthread INCLUDES= -SOURCES=main.c printx.c ui.c nfc.c +SOURCES=main.c printx.c ui.c nfc.c bus.c OBJECTS=$(SOURCES:.c=.o) OUTPUT=tweekd diff --git a/bus.c b/bus.c new file mode 100644 index 0000000..083dd7e --- /dev/null +++ b/bus.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "printx.h" +#include "ui.h" +#include "nfc.h" + +#define CMD_MAX 70 + +int bus; +bool busFree = true; +bool busStop = false; + +void stopBus() +{ + busStop = true; +} + +void processCmd(char* buffer) +{ + buffer[strlen(buffer)-1]='\0'; + if(strcmp(buffer, "quit") == 0 || strcmp(buffer, "exit") == 0) + { + printx(INFO, BUS, "Exit request receved, processing ...\n"); + stopNFC(); + //stopHVC(); + stopBus(); + stopUI(); + } + //else + //printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit")); +} + +void* processBus(void* we) +{ + bus = open("bus.log", O_RDWR); + char buffer[CMD_MAX]; + printx(DEBUG, BUS, "Waiting for events\n"); + while(!busStop) + { + while(busFree); + while(!busFree); + printx(DEBUG, BUS, "Event receved !\n"); + busFree = false; + lseek(bus, 0, SEEK_SET); + printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX)); + ftruncate(bus, 0); + sync(); + processCmd(buffer); + busFree = true; + printx(DEBUG, BUS, buffer); + } + return NULL; +} + +bool initBus() +{ + bus = creat("bus.log", 0666); + if(bus == -1) + { + printf("Unable to open the bus file\n"); + return false; + } + + return true; +} + +void sendToBus(char* cmd) +{ + while(!busFree); + busFree = false; + ftruncate(bus, 0); + ftruncate(bus, CMD_MAX); + lseek(bus, 0, SEEK_SET); + dprintf(bus, "%s\n", cmd); + busFree = true; +} diff --git a/bus.h b/bus.h new file mode 100644 index 0000000..44d9b4f --- /dev/null +++ b/bus.h @@ -0,0 +1,8 @@ +#ifndef __BUS_H__ +#define __BUS_H__ + +bool initBus(); +void* processBus(void*); +void sendToBus(char*); + +#endif diff --git a/main.c b/main.c index 665c8ba..26e7829 100644 --- a/main.c +++ b/main.c @@ -3,13 +3,14 @@ #include "printx.h" #include "ui.h" #include "nfc.h" +#include "bus.h" #include int main(void) { int ret; - pthread_t tUI, tNFC;//tHVC + pthread_t tUI, tNFC, tBUS;//tHVC initUILog(); if(!initLog()) { @@ -28,6 +29,12 @@ int main(void) return 2; } pthread_create(&tNFC, NULL, processNFC, NULL); + if(!initBus()) + { + printx(ERROR, MAIN, "Unable to start the BUS interface\n"); + return 3; + } + pthread_create(&tBUS, NULL, processBus, NULL); pthread_join(tUI, NULL); closeLog(); return 0; diff --git a/nfc.c b/nfc.c index a543fee..13a3747 100644 --- a/nfc.c +++ b/nfc.c @@ -27,7 +27,7 @@ // Globals int serial_fd = -1; -bool stop = false; +bool nfcStop = false; struct termios saveterm; bool init_serial(char* device, int speed) @@ -61,6 +61,12 @@ void close_serial() close(serial_fd); } +void stopNFC() +{ + close_serial(); + nfcStop = true; +} + unsigned char getData() { unsigned char buf; @@ -150,7 +156,7 @@ void* processNFC(void* we) char myTag[NFC_TAG_LENGTH]; char buffer[NFC_TAG_LENGTH]; - while(!stop) + while(!nfcStop) { printx(DEBUG, NFC, "Scanning for tags\n"); if(isTagPresent(buffer)) diff --git a/nfc.h b/nfc.h index a598d63..cba7ca4 100644 --- a/nfc.h +++ b/nfc.h @@ -5,5 +5,6 @@ bool initNFC(); void* processNFC(); +void stopNFC(); #endif diff --git a/ui.c b/ui.c index 260956f..90eff35 100644 --- a/ui.c +++ b/ui.c @@ -3,7 +3,10 @@ #include #include #include +#include +#include #include "ui.h" +#include "bus.h" #define TIME_LENGTH 24 #define HEADER_TEXT_LENGTH 24 @@ -17,11 +20,23 @@ char uilog[LOG_LINES][LOG_LENGTH]; char started[TIME_LENGTH]; char uid[HEADER_TEXT_LENGTH]; char uidDate[HEADER_TEXT_LENGTH]; +char cmd[LOG_LENGTH]; pid_t mainPid; +bool heat = false; +bool pump = false; +unsigned int temp = 1; +unsigned int debit = 1; +struct termios old={0}; +bool uiStop = false; + +void stopUI() +{ + uiStop = true; +} void* drawUI(void* we) { - while(1) + while(!uiStop) { printf("\x1b[2J\x1b[1;1H"); displayUI(); @@ -31,6 +46,17 @@ void* drawUI(void* we) return NULL; } +void setDFG(void) +{ + if(tcgetattr(0, &old)<0) + perror("tcsetattr()"); + old.c_lflag&=~ICANON; + old.c_cc[VMIN]=1; + old.c_cc[VTIME]=0; + if(tcsetattr(0, TCSANOW, &old)<0) + perror("tcsetattr ICANON"); +} + void setTagName(char* tag) { time_t now = time(NULL); @@ -40,6 +66,8 @@ void setTagName(char* tag) void initUILog() { + setDFG(); + cmd[0]='\0'; started[0]='\0'; mainPid = getpid(); uid[0]='\0'; @@ -80,15 +108,36 @@ char* fillLogWithSpaces(char* buf, char* text) return buf; } +char* fillStatusWithSpaces(char* buf, char* text) +{ + strncpy(buf, text, LOG_LENGTH); + strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-7); + + return buf; +} void setStartTime(char* sT) { strcpy(started, sT); } +char getch() +{ + char buf=0; + if(read(0,&buf,1)<0) + perror("read()"); + return buf; +} + void displayUI() { char buffer[LOG_LENGTH]; + struct timeval tv = {0, 5000}; + char bufferO[4]; + fd_set rdfs; + FD_ZERO(&rdfs); + FD_SET(STDIN_FILENO, &rdfs); + select(1, &rdfs, NULL, NULL, &tv); //header printf("\u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n"); printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since")); @@ -100,7 +149,10 @@ void displayUI() printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer)); sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); - printf("\u2522\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n"); + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n"); + sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103 PUMP[%s] %04d", heat ? "ON " : "OFF", temp, pump ? "ON " : "OFF", debit); + printf("%s \u2503\n", fillStatusWithSpaces(buffer, buffer)); + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n"); //body int i=0; while(i= 65 && bufferO[0] <= 90 && bufferO[1] == '\0') + { + sprintf(buffer, "%c", bufferO[0] + 32); + strcat(cmd, buffer); + } + else if(bufferO[0] >= 97 && bufferO[0] <= 122 && bufferO[1] == '\0') + { + sprintf(buffer, "%c", bufferO[0]); + strcat(cmd, buffer); + } + else if(bufferO[0] == 32) + { + sprintf(buffer, "%c", bufferO[0]); + strcat(cmd, buffer); + } + else if(bufferO[0] == 10) + { + sendToBus(cmd); + cmd[0]='\0'; + } + else if(bufferO[0] == 127) + { + if(strlen(cmd)>0) + cmd[strlen(cmd)-1]='\0'; + } + else + { + //sprintf(buffer, "Unrecognized %02x", bufferO[0]); + //strcat(cmd, buffer); + } + } + sprintf(buffer, "\u2503> %s", cmd); + printf("%s\u2503\n", fillStatusWithSpaces(buffer, buffer)); printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n"); + printf("\x1b[2A\x1b[%dC", strlen(cmd) + 3); + fflush(stdout); } diff --git a/ui.h b/ui.h index 87c9194..8055603 100644 --- a/ui.h +++ b/ui.h @@ -7,5 +7,6 @@ void addToLog(char*); void displayUI(); void setStartTime(char*); void setTagName(char*); +void stopUI(); #endif -- libgit2 0.21.2