From 9e9f365e7a1c5a0f43e4fb01ebfad249ef6ebb09 Mon Sep 17 00:00:00 2001 From: henyxia Date: Thu, 5 Mar 2015 14:49:07 +0100 Subject: [PATCH] Added HVC interface Serial is now an external call --- Makefile | 2 +- hvc.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hvc.h | 10 ++++++++++ main.c | 9 ++++++++- nfc.c | 95 +++++++++++++++++++++++++---------------------------------------------------------------------- serial.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ serial.h | 9 +++++++++ ui.c | 22 +++++++++++++++++++++- ui.h | 6 ++++++ 9 files changed, 205 insertions(+), 73 deletions(-) create mode 100644 hvc.c create mode 100644 hvc.h create mode 100644 serial.c create mode 100644 serial.h diff --git a/Makefile b/Makefile index b2c7344..1fac1ca 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 bus.c +SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c OBJECTS=$(SOURCES:.c=.o) OUTPUT=tweekd diff --git a/hvc.c b/hvc.c new file mode 100644 index 0000000..99cbe60 --- /dev/null +++ b/hvc.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ui.h" +#include "hvc.h" +#include "printx.h" +#include "serial.h" + +#define DEVICE1 "/dev/ttyACM1" +#define SPEED B19200 +#define HVC_POLLING_TIME 500000 +#define WAIT_BEFORE_RETRY 5000 +#define GET_DEVICE_MODEL 'A' +#define DEVICE_MODEL_HVC 'C' +#define GET_TEMP 'H' + +// Globals +int hvc_fd = -1; +bool hvcStop = false; +bool sPump = false; +bool sHeat = false; +struct termios hvcSaveterm; + +bool initHVC() +{ + unsigned char data; + printx(DEBUG, HVC, "Connecting to interface 1\n"); + if(init_serial(DEVICE1, SPEED, &hvc_fd, &hvcSaveterm)) + { + printx(DEBUG, HVC, "Connected to an interface\n"); + sendData(&hvc_fd, GET_DEVICE_MODEL); + printx(DEBUG, HVC, "Sended identification request %02x\n", GET_DEVICE_MODEL); + data = getData(&hvc_fd); + if(data == DEVICE_MODEL_HVC) + printx(INFO, HVC, "HVC Arduino connected\n"); + else + printx(DEBUG, HVC, "HVC Arduino not found, response was %02x\n", data); + } + else + { + printx(WARNING, HVC, "Unable to connect to such interface\n"); + return false; + } + + return true; +} + +void* processHVC(void* we) +{ + uint8_t data; + + while(!hvcStop) + { + printx(DEBUG, HVC, "Querying data\n"); + sendData(&hvc_fd, GET_TEMP); + data = getData(&hvc_fd); + setTemp(data); + + usleep(HVC_POLLING_TIME); + } + + return NULL; +} diff --git a/hvc.h b/hvc.h new file mode 100644 index 0000000..9d61688 --- /dev/null +++ b/hvc.h @@ -0,0 +1,10 @@ +#ifndef __HVC_H__ +#define __HVC_H__ + +#include + +bool initHVC(); +void* processHVC(); +void stopHVC(); + +#endif diff --git a/main.c b/main.c index 26e7829..f4bdd0e 100644 --- a/main.c +++ b/main.c @@ -4,13 +4,14 @@ #include "ui.h" #include "nfc.h" #include "bus.h" +#include "hvc.h" #include int main(void) { int ret; - pthread_t tUI, tNFC, tBUS;//tHVC + pthread_t tUI, tNFC, tBUS, tHVC; initUILog(); if(!initLog()) { @@ -35,6 +36,12 @@ int main(void) return 3; } pthread_create(&tBUS, NULL, processBus, NULL); + if(!initHVC()) + { + printx(ERROR, MAIN, "Unable to start the HVC interface\n"); + return 4; + } + pthread_create(&tHVC, NULL, processHVC, NULL); pthread_join(tUI, NULL); closeLog(); return 0; diff --git a/nfc.c b/nfc.c index 13a3747..dd7ed9b 100644 --- a/nfc.c +++ b/nfc.c @@ -11,11 +11,11 @@ #include "ui.h" #include "nfc.h" #include "printx.h" +#include "serial.h" #define DEVICE1 "/dev/ttyACM0" #define SPEED B19200 #define NFC_POLLING_TIME 500000 -#define WAIT_BEFORE_RETRY 50000 #define NFC_TAG_LENGTH 9 #define GET_DEVICE_MODEL 'A' #define DEVICE_MODEL_NFC 'B' @@ -26,71 +26,20 @@ #define GET_TAG_DETAILS 'H' // Globals -int serial_fd = -1; +int nfc_fd = -1; bool nfcStop = false; struct termios saveterm; -bool init_serial(char* device, int speed) -{ - struct termios new; - - int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK); - - if(fd<0) - return false; - - tcgetattr(fd,&saveterm); // save current port settings - bzero(&new,sizeof(new)); - new.c_cflag=CLOCAL|CREAD|speed|CS8; - new.c_iflag=0; - new.c_oflag=0; - new.c_lflag=0; // set input mode (non-canonical, no echo,...) - new.c_cc[VTIME]=0; // inter-character timer unused - new.c_cc[VMIN]=1; // blocking read until 1 char received - tcflush(fd, TCIFLUSH); - tcsetattr(fd,TCSANOW,&new); - - serial_fd = fd; - sleep(2); - return true; -} - -void close_serial() -{ - tcsetattr(serial_fd,TCSANOW,&saveterm); - close(serial_fd); -} - -void stopNFC() -{ - close_serial(); - nfcStop = true; -} - -unsigned char getData() -{ - unsigned char buf; - while(read(serial_fd, &buf, 1) != 1) - usleep(WAIT_BEFORE_RETRY); - return buf; -} - -void sendData(unsigned char data) -{ - while(write(serial_fd, &data, 1) != 1) - usleep(WAIT_BEFORE_RETRY); -} - bool initNFC() { unsigned char data; printx(DEBUG, NFC, "Connecting to interface 1\n"); - if(init_serial(DEVICE1, SPEED)) + if(init_serial(DEVICE1, SPEED, &nfc_fd, &saveterm)) { printx(DEBUG, NFC, "Connected to an interface\n"); - sendData(GET_DEVICE_MODEL); + sendData(&nfc_fd, GET_DEVICE_MODEL); printx(DEBUG, NFC, "Sended identification request %02x\n", GET_DEVICE_MODEL); - data = getData(); + data = getData(&nfc_fd); if(data == DEVICE_MODEL_NFC) printx(INFO, NFC, "NFC Arduino connected\n"); else @@ -105,36 +54,42 @@ bool initNFC() return true; } +void stopNFC() +{ + close_serial(&nfc_fd, &saveterm); + nfcStop = true; +} + bool isTagPresent(char* tag) { unsigned char data; - sendData(GET_NFC_STATUS); - data = getData(); + sendData(&nfc_fd, GET_NFC_STATUS); + data = getData(&nfc_fd); if(data == PROFESSOR_TAG) { - sendData(GET_TAG_DETAILS); + sendData(&nfc_fd, GET_TAG_DETAILS); printx(INFO, NFC, "Professor Tag Detected %02x\n", data); - return (read(serial_fd, tag, 6) == 6); + //return (read(serial_fd, tag, 6) == 6); } else if(data == STUDENT_TAG) { - sendData(GET_TAG_DETAILS); + sendData(&nfc_fd, GET_TAG_DETAILS); printx(INFO, NFC, "Student Tag Detected\n"); - tag[0] = getData(); + tag[0] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[0]); - tag[1] = getData(); + tag[1] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[1]); - tag[2] = getData(); + tag[2] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[2]); - tag[3] = getData(); + tag[3] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[3]); - tag[4] = getData(); + tag[4] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[4]); - tag[5] = getData(); + tag[5] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[5]); - tag[6] = getData(); + tag[6] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[6]); - tag[7] = getData(); + tag[7] = getData(&nfc_fd); printx(DEBUG, NFC, "Last read %02x\n", tag[7]); return true; } @@ -148,7 +103,7 @@ bool isTagPresent(char* tag) printx(DEBUG, NFC, "Read data did not matched with expected data %02x\n", data); return false; } - return true; + return false; } void* processNFC(void* we) diff --git a/serial.c b/serial.c new file mode 100644 index 0000000..8fb404d --- /dev/null +++ b/serial.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include "serial.h" + +#define WAIT_BEFORE_RETRY 50000 + +bool init_serial(char* device, int speed, int *serial_fd, struct termios *saveterm) +{ + struct termios new; + + int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK); + + if(fd<0) + return false; + + tcgetattr(fd,saveterm); // save current port settings + bzero(&new,sizeof(new)); + new.c_cflag=CLOCAL|CREAD|speed|CS8; + new.c_iflag=0; + new.c_oflag=0; + new.c_lflag=0; // set input mode (non-canonical, no echo,...) + new.c_cc[VTIME]=0; // inter-character timer unused + new.c_cc[VMIN]=1; // blocking read until 1 char received + tcflush(fd, TCIFLUSH); + tcsetattr(fd,TCSANOW,&new); + + *serial_fd = fd; + sleep(2); + return true; +} + +void close_serial(int *serial_fd, struct termios *saveterm) +{ + tcsetattr(*serial_fd,TCSANOW,saveterm); + close(*serial_fd); +} + +unsigned char getData(int *serial_fd) +{ + unsigned char buf; + while(read(*serial_fd, &buf, 1) != 1) + usleep(WAIT_BEFORE_RETRY); + return buf; +} + +void sendData(int *serial_fd, unsigned char data) +{ + while(write(*serial_fd, &data, 1) != 1) + usleep(WAIT_BEFORE_RETRY); +} + diff --git a/serial.h b/serial.h new file mode 100644 index 0000000..ab4f3b5 --- /dev/null +++ b/serial.h @@ -0,0 +1,9 @@ +#ifndef __SERIAL_H__ +#define __SERIAL_H__ + +bool init_serial(char*, int, int*, struct termios*); +void close_serial(int*, struct termios*); +unsigned char getData(int*); +void sendData(int*, unsigned char); + +#endif diff --git a/ui.c b/ui.c index 90eff35..e269c47 100644 --- a/ui.c +++ b/ui.c @@ -34,6 +34,26 @@ void stopUI() uiStop = true; } +void setHeat(bool s) +{ + heat = s; +} + +void setPump(bool s) +{ + pump = s; +} + +void setTemp(unsigned int t) +{ + temp = t; +} + +void setDebit(unsigned int d) +{ + debit = d; +} + void* drawUI(void* we) { while(!uiStop) @@ -150,7 +170,7 @@ void displayUI() sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); printf("%s \u2503\n", fillHeaderWithSpaces(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\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); + sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103 PUMP[%s] %04d", heat ? "ON " : "OFF", temp+25, 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 diff --git a/ui.h b/ui.h index 8055603..773cd48 100644 --- a/ui.h +++ b/ui.h @@ -1,12 +1,18 @@ #ifndef __UI_H__ #define __UI_H__ +#include + void* drawUI(void*); void initUILog(); void addToLog(char*); void displayUI(); +void setDebit(unsigned int); +void setHeat(bool); +void setPump(bool); void setStartTime(char*); void setTagName(char*); +void setTemp(unsigned int); void stopUI(); #endif -- libgit2 0.21.2