Commit 9e9f365e7a1c5a0f43e4fb01ebfad249ef6ebb09

Authored by henyxia
1 parent d4d5bb6d

Added HVC interface

Serial is now an external call
Makefile
... ... @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
3 3 CC_LIBS=-pthread
4 4 INCLUDES=
5 5  
6   -SOURCES=main.c printx.c ui.c nfc.c bus.c
  6 +SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c
7 7 OBJECTS=$(SOURCES:.c=.o)
8 8 OUTPUT=tweekd
9 9  
... ...
hvc.c 0 → 100644
... ... @@ -0,0 +1,70 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <termios.h>
  4 +#include <unistd.h>
  5 +#include <errno.h>
  6 +#include <sys/types.h>
  7 +#include <sys/stat.h>
  8 +#include <fcntl.h>
  9 +#include <strings.h>
  10 +#include <inttypes.h>
  11 +#include "ui.h"
  12 +#include "hvc.h"
  13 +#include "printx.h"
  14 +#include "serial.h"
  15 +
  16 +#define DEVICE1 "/dev/ttyACM1"
  17 +#define SPEED B19200
  18 +#define HVC_POLLING_TIME 500000
  19 +#define WAIT_BEFORE_RETRY 5000
  20 +#define GET_DEVICE_MODEL 'A'
  21 +#define DEVICE_MODEL_HVC 'C'
  22 +#define GET_TEMP 'H'
  23 +
  24 +// Globals
  25 +int hvc_fd = -1;
  26 +bool hvcStop = false;
  27 +bool sPump = false;
  28 +bool sHeat = false;
  29 +struct termios hvcSaveterm;
  30 +
  31 +bool initHVC()
  32 +{
  33 + unsigned char data;
  34 + printx(DEBUG, HVC, "Connecting to interface 1\n");
  35 + if(init_serial(DEVICE1, SPEED, &hvc_fd, &hvcSaveterm))
  36 + {
  37 + printx(DEBUG, HVC, "Connected to an interface\n");
  38 + sendData(&hvc_fd, GET_DEVICE_MODEL);
  39 + printx(DEBUG, HVC, "Sended identification request %02x\n", GET_DEVICE_MODEL);
  40 + data = getData(&hvc_fd);
  41 + if(data == DEVICE_MODEL_HVC)
  42 + printx(INFO, HVC, "HVC Arduino connected\n");
  43 + else
  44 + printx(DEBUG, HVC, "HVC Arduino not found, response was %02x\n", data);
  45 + }
  46 + else
  47 + {
  48 + printx(WARNING, HVC, "Unable to connect to such interface\n");
  49 + return false;
  50 + }
  51 +
  52 + return true;
  53 +}
  54 +
  55 +void* processHVC(void* we)
  56 +{
  57 + uint8_t data;
  58 +
  59 + while(!hvcStop)
  60 + {
  61 + printx(DEBUG, HVC, "Querying data\n");
  62 + sendData(&hvc_fd, GET_TEMP);
  63 + data = getData(&hvc_fd);
  64 + setTemp(data);
  65 +
  66 + usleep(HVC_POLLING_TIME);
  67 + }
  68 +
  69 + return NULL;
  70 +}
... ...
hvc.h 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +#ifndef __HVC_H__
  2 +#define __HVC_H__
  3 +
  4 +#include <stdbool.h>
  5 +
  6 +bool initHVC();
  7 +void* processHVC();
  8 +void stopHVC();
  9 +
  10 +#endif
... ...
... ... @@ -4,13 +4,14 @@
4 4 #include "ui.h"
5 5 #include "nfc.h"
6 6 #include "bus.h"
  7 +#include "hvc.h"
7 8  
8 9 #include <unistd.h>
9 10  
10 11 int main(void)
11 12 {
12 13 int ret;
13   - pthread_t tUI, tNFC, tBUS;//tHVC
  14 + pthread_t tUI, tNFC, tBUS, tHVC;
14 15 initUILog();
15 16 if(!initLog())
16 17 {
... ... @@ -35,6 +36,12 @@ int main(void)
35 36 return 3;
36 37 }
37 38 pthread_create(&tBUS, NULL, processBus, NULL);
  39 + if(!initHVC())
  40 + {
  41 + printx(ERROR, MAIN, "Unable to start the HVC interface\n");
  42 + return 4;
  43 + }
  44 + pthread_create(&tHVC, NULL, processHVC, NULL);
38 45 pthread_join(tUI, NULL);
39 46 closeLog();
40 47 return 0;
... ...
... ... @@ -11,11 +11,11 @@
11 11 #include "ui.h"
12 12 #include "nfc.h"
13 13 #include "printx.h"
  14 +#include "serial.h"
14 15  
15 16 #define DEVICE1 "/dev/ttyACM0"
16 17 #define SPEED B19200
17 18 #define NFC_POLLING_TIME 500000
18   -#define WAIT_BEFORE_RETRY 50000
19 19 #define NFC_TAG_LENGTH 9
20 20 #define GET_DEVICE_MODEL 'A'
21 21 #define DEVICE_MODEL_NFC 'B'
... ... @@ -26,71 +26,20 @@
26 26 #define GET_TAG_DETAILS 'H'
27 27  
28 28 // Globals
29   -int serial_fd = -1;
  29 +int nfc_fd = -1;
30 30 bool nfcStop = false;
31 31 struct termios saveterm;
32 32  
33   -bool init_serial(char* device, int speed)
34   -{
35   - struct termios new;
36   -
37   - int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK);
38   -
39   - if(fd<0)
40   - return false;
41   -
42   - tcgetattr(fd,&saveterm); // save current port settings
43   - bzero(&new,sizeof(new));
44   - new.c_cflag=CLOCAL|CREAD|speed|CS8;
45   - new.c_iflag=0;
46   - new.c_oflag=0;
47   - new.c_lflag=0; // set input mode (non-canonical, no echo,...)
48   - new.c_cc[VTIME]=0; // inter-character timer unused
49   - new.c_cc[VMIN]=1; // blocking read until 1 char received
50   - tcflush(fd, TCIFLUSH);
51   - tcsetattr(fd,TCSANOW,&new);
52   -
53   - serial_fd = fd;
54   - sleep(2);
55   - return true;
56   -}
57   -
58   -void close_serial()
59   -{
60   - tcsetattr(serial_fd,TCSANOW,&saveterm);
61   - close(serial_fd);
62   -}
63   -
64   -void stopNFC()
65   -{
66   - close_serial();
67   - nfcStop = true;
68   -}
69   -
70   -unsigned char getData()
71   -{
72   - unsigned char buf;
73   - while(read(serial_fd, &buf, 1) != 1)
74   - usleep(WAIT_BEFORE_RETRY);
75   - return buf;
76   -}
77   -
78   -void sendData(unsigned char data)
79   -{
80   - while(write(serial_fd, &data, 1) != 1)
81   - usleep(WAIT_BEFORE_RETRY);
82   -}
83   -
84 33 bool initNFC()
85 34 {
86 35 unsigned char data;
87 36 printx(DEBUG, NFC, "Connecting to interface 1\n");
88   - if(init_serial(DEVICE1, SPEED))
  37 + if(init_serial(DEVICE1, SPEED, &nfc_fd, &saveterm))
89 38 {
90 39 printx(DEBUG, NFC, "Connected to an interface\n");
91   - sendData(GET_DEVICE_MODEL);
  40 + sendData(&nfc_fd, GET_DEVICE_MODEL);
92 41 printx(DEBUG, NFC, "Sended identification request %02x\n", GET_DEVICE_MODEL);
93   - data = getData();
  42 + data = getData(&nfc_fd);
94 43 if(data == DEVICE_MODEL_NFC)
95 44 printx(INFO, NFC, "NFC Arduino connected\n");
96 45 else
... ... @@ -105,36 +54,42 @@ bool initNFC()
105 54 return true;
106 55 }
107 56  
  57 +void stopNFC()
  58 +{
  59 + close_serial(&nfc_fd, &saveterm);
  60 + nfcStop = true;
  61 +}
  62 +
108 63 bool isTagPresent(char* tag)
109 64 {
110 65 unsigned char data;
111   - sendData(GET_NFC_STATUS);
112   - data = getData();
  66 + sendData(&nfc_fd, GET_NFC_STATUS);
  67 + data = getData(&nfc_fd);
113 68 if(data == PROFESSOR_TAG)
114 69 {
115   - sendData(GET_TAG_DETAILS);
  70 + sendData(&nfc_fd, GET_TAG_DETAILS);
116 71 printx(INFO, NFC, "Professor Tag Detected %02x\n", data);
117   - return (read(serial_fd, tag, 6) == 6);
  72 + //return (read(serial_fd, tag, 6) == 6);
118 73 }
119 74 else if(data == STUDENT_TAG)
120 75 {
121   - sendData(GET_TAG_DETAILS);
  76 + sendData(&nfc_fd, GET_TAG_DETAILS);
122 77 printx(INFO, NFC, "Student Tag Detected\n");
123   - tag[0] = getData();
  78 + tag[0] = getData(&nfc_fd);
124 79 printx(DEBUG, NFC, "Last read %02x\n", tag[0]);
125   - tag[1] = getData();
  80 + tag[1] = getData(&nfc_fd);
126 81 printx(DEBUG, NFC, "Last read %02x\n", tag[1]);
127   - tag[2] = getData();
  82 + tag[2] = getData(&nfc_fd);
128 83 printx(DEBUG, NFC, "Last read %02x\n", tag[2]);
129   - tag[3] = getData();
  84 + tag[3] = getData(&nfc_fd);
130 85 printx(DEBUG, NFC, "Last read %02x\n", tag[3]);
131   - tag[4] = getData();
  86 + tag[4] = getData(&nfc_fd);
132 87 printx(DEBUG, NFC, "Last read %02x\n", tag[4]);
133   - tag[5] = getData();
  88 + tag[5] = getData(&nfc_fd);
134 89 printx(DEBUG, NFC, "Last read %02x\n", tag[5]);
135   - tag[6] = getData();
  90 + tag[6] = getData(&nfc_fd);
136 91 printx(DEBUG, NFC, "Last read %02x\n", tag[6]);
137   - tag[7] = getData();
  92 + tag[7] = getData(&nfc_fd);
138 93 printx(DEBUG, NFC, "Last read %02x\n", tag[7]);
139 94 return true;
140 95 }
... ... @@ -148,7 +103,7 @@ bool isTagPresent(char* tag)
148 103 printx(DEBUG, NFC, "Read data did not matched with expected data %02x\n", data);
149 104 return false;
150 105 }
151   - return true;
  106 + return false;
152 107 }
153 108  
154 109 void* processNFC(void* we)
... ...
serial.c 0 → 100644
... ... @@ -0,0 +1,55 @@
  1 +#include <stdbool.h>
  2 +#include <termios.h>
  3 +#include <fcntl.h>
  4 +#include <unistd.h>
  5 +#include <sys/types.h>
  6 +#include <strings.h>
  7 +#include "serial.h"
  8 +
  9 +#define WAIT_BEFORE_RETRY 50000
  10 +
  11 +bool init_serial(char* device, int speed, int *serial_fd, struct termios *saveterm)
  12 +{
  13 + struct termios new;
  14 +
  15 + int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK);
  16 +
  17 + if(fd<0)
  18 + return false;
  19 +
  20 + tcgetattr(fd,saveterm); // save current port settings
  21 + bzero(&new,sizeof(new));
  22 + new.c_cflag=CLOCAL|CREAD|speed|CS8;
  23 + new.c_iflag=0;
  24 + new.c_oflag=0;
  25 + new.c_lflag=0; // set input mode (non-canonical, no echo,...)
  26 + new.c_cc[VTIME]=0; // inter-character timer unused
  27 + new.c_cc[VMIN]=1; // blocking read until 1 char received
  28 + tcflush(fd, TCIFLUSH);
  29 + tcsetattr(fd,TCSANOW,&new);
  30 +
  31 + *serial_fd = fd;
  32 + sleep(2);
  33 + return true;
  34 +}
  35 +
  36 +void close_serial(int *serial_fd, struct termios *saveterm)
  37 +{
  38 + tcsetattr(*serial_fd,TCSANOW,saveterm);
  39 + close(*serial_fd);
  40 +}
  41 +
  42 +unsigned char getData(int *serial_fd)
  43 +{
  44 + unsigned char buf;
  45 + while(read(*serial_fd, &buf, 1) != 1)
  46 + usleep(WAIT_BEFORE_RETRY);
  47 + return buf;
  48 +}
  49 +
  50 +void sendData(int *serial_fd, unsigned char data)
  51 +{
  52 + while(write(*serial_fd, &data, 1) != 1)
  53 + usleep(WAIT_BEFORE_RETRY);
  54 +}
  55 +
... ...
serial.h 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +#ifndef __SERIAL_H__
  2 +#define __SERIAL_H__
  3 +
  4 +bool init_serial(char*, int, int*, struct termios*);
  5 +void close_serial(int*, struct termios*);
  6 +unsigned char getData(int*);
  7 +void sendData(int*, unsigned char);
  8 +
  9 +#endif
... ...
... ... @@ -34,6 +34,26 @@ void stopUI()
34 34 uiStop = true;
35 35 }
36 36  
  37 +void setHeat(bool s)
  38 +{
  39 + heat = s;
  40 +}
  41 +
  42 +void setPump(bool s)
  43 +{
  44 + pump = s;
  45 +}
  46 +
  47 +void setTemp(unsigned int t)
  48 +{
  49 + temp = t;
  50 +}
  51 +
  52 +void setDebit(unsigned int d)
  53 +{
  54 + debit = d;
  55 +}
  56 +
37 57 void* drawUI(void* we)
38 58 {
39 59 while(!uiStop)
... ... @@ -150,7 +170,7 @@ void displayUI()
150 170 sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
151 171 printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
152 172 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");
153   - sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103 PUMP[%s] %04d", heat ? "ON " : "OFF", temp, pump ? "ON " : "OFF", debit);
  173 + sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103 PUMP[%s] %04d", heat ? "ON " : "OFF", temp+25, pump ? "ON " : "OFF", debit);
154 174 printf("%s \u2503\n", fillStatusWithSpaces(buffer, buffer));
155 175 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");
156 176 //body
... ...
1 1 #ifndef __UI_H__
2 2 #define __UI_H__
3 3  
  4 +#include <stdbool.h>
  5 +
4 6 void* drawUI(void*);
5 7 void initUILog();
6 8 void addToLog(char*);
7 9 void displayUI();
  10 +void setDebit(unsigned int);
  11 +void setHeat(bool);
  12 +void setPump(bool);
8 13 void setStartTime(char*);
9 14 void setTagName(char*);
  15 +void setTemp(unsigned int);
10 16 void stopUI();
11 17  
12 18 #endif
... ...