| @@ -3,7 +3,10 @@ |
| @@ -3,7 +3,10 @@ |
3
| #include <unistd.h> |
3
| #include <unistd.h> |
4
| #include <string.h> |
4
| #include <string.h> |
5
| #include <time.h> |
5
| #include <time.h> |
| |
6
| +#include <stdbool.h> |
| |
7
| +#include <termios.h> |
6
| #include "ui.h" |
8
| #include "ui.h" |
| |
9
| +#include "bus.h" |
7
| |
10
| |
8
| #define TIME_LENGTH 24 |
11
| #define TIME_LENGTH 24 |
9
| #define HEADER_TEXT_LENGTH 24 |
12
| #define HEADER_TEXT_LENGTH 24 |
| @@ -17,11 +20,23 @@ char uilog[LOG_LINES][LOG_LENGTH]; |
| @@ -17,11 +20,23 @@ char uilog[LOG_LINES][LOG_LENGTH]; |
17
| char started[TIME_LENGTH]; |
20
| char started[TIME_LENGTH]; |
18
| char uid[HEADER_TEXT_LENGTH]; |
21
| char uid[HEADER_TEXT_LENGTH]; |
19
| char uidDate[HEADER_TEXT_LENGTH]; |
22
| char uidDate[HEADER_TEXT_LENGTH]; |
| |
23
| +char cmd[LOG_LENGTH]; |
20
| pid_t mainPid; |
24
| pid_t mainPid; |
| |
25
| +bool heat = false; |
| |
26
| +bool pump = false; |
| |
27
| +unsigned int temp = 1; |
| |
28
| +unsigned int debit = 1; |
| |
29
| +struct termios old={0}; |
| |
30
| +bool uiStop = false; |
| |
31
| + |
| |
32
| +void stopUI() |
| |
33
| +{ |
| |
34
| + uiStop = true; |
| |
35
| +} |
21
| |
36
| |
22
| void* drawUI(void* we) |
37
| void* drawUI(void* we) |
23
| { |
38
| { |
24
| - while(1) |
39
| + while(!uiStop) |
25
| { |
40
| { |
26
| printf("\x1b[2J\x1b[1;1H"); |
41
| printf("\x1b[2J\x1b[1;1H"); |
27
| displayUI(); |
42
| displayUI(); |
| @@ -31,6 +46,17 @@ void* drawUI(void* we) |
| @@ -31,6 +46,17 @@ void* drawUI(void* we) |
31
| return NULL; |
46
| return NULL; |
32
| } |
47
| } |
33
| |
48
| |
| |
49
| +void setDFG(void) |
| |
50
| +{ |
| |
51
| + if(tcgetattr(0, &old)<0) |
| |
52
| + perror("tcsetattr()"); |
| |
53
| + old.c_lflag&=~ICANON; |
| |
54
| + old.c_cc[VMIN]=1; |
| |
55
| + old.c_cc[VTIME]=0; |
| |
56
| + if(tcsetattr(0, TCSANOW, &old)<0) |
| |
57
| + perror("tcsetattr ICANON"); |
| |
58
| +} |
| |
59
| + |
34
| void setTagName(char* tag) |
60
| void setTagName(char* tag) |
35
| { |
61
| { |
36
| time_t now = time(NULL); |
62
| time_t now = time(NULL); |
| @@ -40,6 +66,8 @@ void setTagName(char* tag) |
| @@ -40,6 +66,8 @@ void setTagName(char* tag) |
40
| |
66
| |
41
| void initUILog() |
67
| void initUILog() |
42
| { |
68
| { |
| |
69
| + setDFG(); |
| |
70
| + cmd[0]='\0'; |
43
| started[0]='\0'; |
71
| started[0]='\0'; |
44
| mainPid = getpid(); |
72
| mainPid = getpid(); |
45
| uid[0]='\0'; |
73
| uid[0]='\0'; |
| @@ -80,15 +108,36 @@ char* fillLogWithSpaces(char* buf, char* text) |
| @@ -80,15 +108,36 @@ char* fillLogWithSpaces(char* buf, char* text) |
80
| return buf; |
108
| return buf; |
81
| } |
109
| } |
82
| |
110
| |
| |
111
| +char* fillStatusWithSpaces(char* buf, char* text) |
| |
112
| +{ |
| |
113
| + strncpy(buf, text, LOG_LENGTH); |
| |
114
| + strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-7); |
| |
115
| + |
| |
116
| + return buf; |
| |
117
| +} |
83
| |
118
| |
84
| void setStartTime(char* sT) |
119
| void setStartTime(char* sT) |
85
| { |
120
| { |
86
| strcpy(started, sT); |
121
| strcpy(started, sT); |
87
| } |
122
| } |
88
| |
123
| |
| |
124
| +char getch() |
| |
125
| +{ |
| |
126
| + char buf=0; |
| |
127
| + if(read(0,&buf,1)<0) |
| |
128
| + perror("read()"); |
| |
129
| + return buf; |
| |
130
| +} |
| |
131
| + |
89
| void displayUI() |
132
| void displayUI() |
90
| { |
133
| { |
91
| char buffer[LOG_LENGTH]; |
134
| char buffer[LOG_LENGTH]; |
| |
135
| + struct timeval tv = {0, 5000}; |
| |
136
| + char bufferO[4]; |
| |
137
| + fd_set rdfs; |
| |
138
| + FD_ZERO(&rdfs); |
| |
139
| + FD_SET(STDIN_FILENO, &rdfs); |
| |
140
| + select(1, &rdfs, NULL, NULL, &tv); |
92
| //header |
141
| //header |
93
| 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"); |
142
| 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"); |
94
| printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since")); |
143
| printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since")); |
| @@ -100,7 +149,10 @@ void displayUI() |
| @@ -100,7 +149,10 @@ void displayUI() |
100
| printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer)); |
149
| printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer)); |
101
| sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); |
150
| sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); |
102
| printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); |
151
| printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); |
103
| - 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"); |
152
| + 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); |
| |
154
| + printf("%s \u2503\n", fillStatusWithSpaces(buffer, buffer)); |
| |
155
| + 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"); |
104
| //body |
156
| //body |
105
| int i=0; |
157
| int i=0; |
106
| while(i<LOG_LINES) |
158
| while(i<LOG_LINES) |
| @@ -108,5 +160,45 @@ void displayUI() |
| @@ -108,5 +160,45 @@ void displayUI() |
108
| printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1])); |
160
| printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1])); |
109
| i++; |
161
| i++; |
110
| } |
162
| } |
| |
163
| + 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"); |
| |
164
| + //cmd |
| |
165
| + if(FD_ISSET(STDIN_FILENO, &rdfs)) |
| |
166
| + { |
| |
167
| + bufferO[0] = getch(); |
| |
168
| + if(bufferO[0] >= 65 && bufferO[0] <= 90 && bufferO[1] == '\0') |
| |
169
| + { |
| |
170
| + sprintf(buffer, "%c", bufferO[0] + 32); |
| |
171
| + strcat(cmd, buffer); |
| |
172
| + } |
| |
173
| + else if(bufferO[0] >= 97 && bufferO[0] <= 122 && bufferO[1] == '\0') |
| |
174
| + { |
| |
175
| + sprintf(buffer, "%c", bufferO[0]); |
| |
176
| + strcat(cmd, buffer); |
| |
177
| + } |
| |
178
| + else if(bufferO[0] == 32) |
| |
179
| + { |
| |
180
| + sprintf(buffer, "%c", bufferO[0]); |
| |
181
| + strcat(cmd, buffer); |
| |
182
| + } |
| |
183
| + else if(bufferO[0] == 10) |
| |
184
| + { |
| |
185
| + sendToBus(cmd); |
| |
186
| + cmd[0]='\0'; |
| |
187
| + } |
| |
188
| + else if(bufferO[0] == 127) |
| |
189
| + { |
| |
190
| + if(strlen(cmd)>0) |
| |
191
| + cmd[strlen(cmd)-1]='\0'; |
| |
192
| + } |
| |
193
| + else |
| |
194
| + { |
| |
195
| + //sprintf(buffer, "Unrecognized %02x", bufferO[0]); |
| |
196
| + //strcat(cmd, buffer); |
| |
197
| + } |
| |
198
| + } |
| |
199
| + sprintf(buffer, "\u2503> %s", cmd); |
| |
200
| + printf("%s\u2503\n", fillStatusWithSpaces(buffer, buffer)); |
111
| 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"); |
201
| 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"); |
| |
202
| + printf("\x1b[2A\x1b[%dC", strlen(cmd) + 3); |
| |
203
| + fflush(stdout); |
112
| } |
204
| } |