Blame view

ui.c 7.02 KB
ea1218f1   henyxia   UI finished
1
2
3
4
5
  #include <stdio.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <string.h>
  #include <time.h>
d4d5bb6d   henyxia   Bus working
6
7
  #include <stdbool.h>
  #include <termios.h>
ea1218f1   henyxia   UI finished
8
  #include "ui.h"
d4d5bb6d   henyxia   Bus working
9
  #include "bus.h"
ea1218f1   henyxia   UI finished
10
11
12
  
  #define	TIME_LENGTH			24
  #define	HEADER_TEXT_LENGTH	24
3bdc9daf   henyxia   NFC Added
13
  #define	LOG_LINES			35
297a72df   henyxia   Added sender
14
  #define	LOG_LENGTH			82
cdaa873f   henyxia   Log fixed
15
16
  #define	IPS					20
  #define	SCREEN_TIME			1000000/IPS
19082eeb   henyxia   UI design not wor...
17
  #define	SPACES				"                                                                                                                                                                                                                                                                  "
ea1218f1   henyxia   UI finished
18
  
ea1218f1   henyxia   UI finished
19
20
21
22
  char	uilog[LOG_LINES][LOG_LENGTH];
  char	started[TIME_LENGTH];
  char	uid[HEADER_TEXT_LENGTH];
  char	uidDate[HEADER_TEXT_LENGTH];
d4d5bb6d   henyxia   Bus working
23
  char	cmd[LOG_LENGTH];
cdaa873f   henyxia   Log fixed
24
  pid_t	mainPid;
d4d5bb6d   henyxia   Bus working
25
26
27
28
29
30
31
32
33
34
35
  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;
  }
cdaa873f   henyxia   Log fixed
36
  
9e9f365e   henyxia   Added HVC interface
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  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;
  }
  
cdaa873f   henyxia   Log fixed
57
58
  void* drawUI(void* we)
  {
d4d5bb6d   henyxia   Bus working
59
  	while(!uiStop)
cdaa873f   henyxia   Log fixed
60
61
62
63
64
65
66
67
  	{
  		printf("\x1b[2J\x1b[1;1H");
  		displayUI();
  		usleep(SCREEN_TIME);
  	}
  
  	return NULL;
  }
ea1218f1   henyxia   UI finished
68
  
d4d5bb6d   henyxia   Bus working
69
70
71
72
73
74
75
76
77
78
79
  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");
  }
  
3bdc9daf   henyxia   NFC Added
80
81
82
83
84
85
86
  void setTagName(char* tag)
  {
  	time_t now = time(NULL);
  	strcpy(uid, tag);
  	strftime(uidDate, HEADER_TEXT_LENGTH, "%F-%T:%d", localtime(&now));
  }
  
ea1218f1   henyxia   UI finished
87
88
  void initUILog()
  {
d4d5bb6d   henyxia   Bus working
89
90
  	setDFG();
  	cmd[0]='\0';
cdaa873f   henyxia   Log fixed
91
92
  	started[0]='\0';
  	mainPid = getpid();
ea1218f1   henyxia   UI finished
93
94
95
96
97
98
  	uid[0]='\0';
  	uidDate[0]='\0';
  	for(int i=0; i<LOG_LINES; i++)
  		uilog[i][0] = '\0';
  }
  
19082eeb   henyxia   UI design not wor...
99
  char* fillHeaderWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
100
  {
19082eeb   henyxia   UI design not wor...
101
  	char tmpretheader[HEADER_TEXT_LENGTH+1];
ea1218f1   henyxia   UI finished
102
103
104
105
  	strcpy(tmpretheader, text);
  	while(strlen(tmpretheader) < HEADER_TEXT_LENGTH)
  		strcat(tmpretheader, " ");
  
19082eeb   henyxia   UI design not wor...
106
107
108
109
110
  	for(int i=0; i<strlen(tmpretheader); i++)
  		buf[i] = tmpretheader[i];
  	buf[strlen(tmpretheader)] = '\0';
  
  	return buf;
ea1218f1   henyxia   UI finished
111
112
113
114
  }
  
  void addToLog(char* newStr)
  {
cdaa873f   henyxia   Log fixed
115
  	for(int i=LOG_LINES-2; i>=0; i--)
ea1218f1   henyxia   UI finished
116
117
118
119
  		strcpy(uilog[i+1], uilog[i]);
  	strcpy(uilog[0], newStr);
  }
  
19082eeb   henyxia   UI design not wor...
120
  char* fillLogWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
121
  {
19082eeb   henyxia   UI design not wor...
122
  	strncpy(buf, text, LOG_LENGTH);
19082eeb   henyxia   UI design not wor...
123
  	if(strlen(buf) > 0)
297a72df   henyxia   Added sender
124
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1);
19082eeb   henyxia   UI design not wor...
125
  	else
297a72df   henyxia   Added sender
126
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-12);
19082eeb   henyxia   UI design not wor...
127
128
  
  	return buf;
ea1218f1   henyxia   UI finished
129
130
  }
  
d4d5bb6d   henyxia   Bus working
131
132
133
134
135
136
137
  char* fillStatusWithSpaces(char* buf, char* text)
  {
  	strncpy(buf, text, LOG_LENGTH);
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-7);
  
  	return buf;
  }
ea1218f1   henyxia   UI finished
138
139
140
141
142
143
  
  void setStartTime(char* sT)
  {
  	strcpy(started, sT);
  }
  
d4d5bb6d   henyxia   Bus working
144
145
146
147
148
149
150
151
  char getch()
  {
      char buf=0;
      if(read(0,&buf,1)<0)
          perror("read()");
      return buf;
  }
  
ea1218f1   henyxia   UI finished
152
153
  void displayUI()
  {
19082eeb   henyxia   UI design not wor...
154
  	char	buffer[LOG_LENGTH];
d4d5bb6d   henyxia   Bus working
155
156
157
158
159
160
  	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);
ea1218f1   henyxia   UI finished
161
162
  	//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");
19082eeb   henyxia   UI design not wor...
163
  	printf("\u2503 %s \u2503      T H E      \u2503", fillHeaderWithSpaces(buffer, "Started since"));
ea1218f1   henyxia   UI finished
164
  	sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag");
3bdc9daf   henyxia   NFC Added
165
166
167
     	printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
  	printf("\u2503 %s \u2503    T W E E K    \u2503 ", fillHeaderWithSpaces(buffer, started));
  	printf("%s \u2503\n", fillHeaderWithSpaces(buffer, " "));
cdaa873f   henyxia   Log fixed
168
  	sprintf(buffer, "PID %d", mainPid);
19082eeb   henyxia   UI design not wor...
169
  	printf("\u2503 %s \u2503  P R O J E C T  \u2503 ", fillHeaderWithSpaces(buffer, buffer));
ea1218f1   henyxia   UI finished
170
  	sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
19082eeb   henyxia   UI design not wor...
171
  	printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
d4d5bb6d   henyxia   Bus working
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");
9e9f365e   henyxia   Added HVC interface
173
  	sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103  PUMP[%s] %04d", heat ? "ON " : "OFF", temp+25, pump ? "ON " : "OFF", debit);
d4d5bb6d   henyxia   Bus working
174
175
  	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");
ea1218f1   henyxia   UI finished
176
  	//body
19082eeb   henyxia   UI design not wor...
177
178
179
180
181
182
  	int i=0;
  	while(i<LOG_LINES)
  	{
  		printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1]));
  		i++;
  	}
d4d5bb6d   henyxia   Bus working
183
184
185
186
187
188
189
190
191
192
193
194
195
  	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");
  	//cmd
  	if(FD_ISSET(STDIN_FILENO, &rdfs))
  	{
  		bufferO[0] = getch();
  		if(bufferO[0] >= 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]);
5dd66ab0   henyxia   Added timer optio...
196
197
198
199
200
  			strcat(cmd, buffer);
  		}
  		else if(bufferO[0] >= 48 && bufferO[0] <= 57)
  		{
  			sprintf(buffer, "%c", bufferO[0]);
d4d5bb6d   henyxia   Bus working
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  			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));
ea1218f1   henyxia   UI finished
226
  	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");
d4d5bb6d   henyxia   Bus working
227
228
  	printf("\x1b[2A\x1b[%dC", strlen(cmd) + 3);
  	fflush(stdout);
ea1218f1   henyxia   UI finished
229
  }