Blame view

ui.c 3.95 KB
ea1218f1   henyxia   UI finished
1
2
3
4
5
6
7
8
9
  #include <stdio.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <string.h>
  #include <time.h>
  #include "ui.h"
  
  #define	TIME_LENGTH			24
  #define	HEADER_TEXT_LENGTH	24
3bdc9daf   henyxia   NFC Added
10
  #define	LOG_LINES			35
297a72df   henyxia   Added sender
11
  #define	LOG_LENGTH			82
cdaa873f   henyxia   Log fixed
12
13
  #define	IPS					20
  #define	SCREEN_TIME			1000000/IPS
19082eeb   henyxia   UI design not wor...
14
  #define	SPACES				"                                                                                                                                                                                                                                                                  "
ea1218f1   henyxia   UI finished
15
  
ea1218f1   henyxia   UI finished
16
17
18
19
  char	uilog[LOG_LINES][LOG_LENGTH];
  char	started[TIME_LENGTH];
  char	uid[HEADER_TEXT_LENGTH];
  char	uidDate[HEADER_TEXT_LENGTH];
cdaa873f   henyxia   Log fixed
20
21
22
23
24
25
26
27
28
29
30
31
32
  pid_t	mainPid;
  
  void* drawUI(void* we)
  {
  	while(1)
  	{
  		printf("\x1b[2J\x1b[1;1H");
  		displayUI();
  		usleep(SCREEN_TIME);
  	}
  
  	return NULL;
  }
ea1218f1   henyxia   UI finished
33
  
3bdc9daf   henyxia   NFC Added
34
35
36
37
38
39
40
  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
41
42
  void initUILog()
  {
cdaa873f   henyxia   Log fixed
43
44
  	started[0]='\0';
  	mainPid = getpid();
ea1218f1   henyxia   UI finished
45
46
47
48
49
50
  	uid[0]='\0';
  	uidDate[0]='\0';
  	for(int i=0; i<LOG_LINES; i++)
  		uilog[i][0] = '\0';
  }
  
19082eeb   henyxia   UI design not wor...
51
  char* fillHeaderWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
52
  {
19082eeb   henyxia   UI design not wor...
53
  	char tmpretheader[HEADER_TEXT_LENGTH+1];
ea1218f1   henyxia   UI finished
54
55
56
57
  	strcpy(tmpretheader, text);
  	while(strlen(tmpretheader) < HEADER_TEXT_LENGTH)
  		strcat(tmpretheader, " ");
  
19082eeb   henyxia   UI design not wor...
58
59
60
61
62
  	for(int i=0; i<strlen(tmpretheader); i++)
  		buf[i] = tmpretheader[i];
  	buf[strlen(tmpretheader)] = '\0';
  
  	return buf;
ea1218f1   henyxia   UI finished
63
64
65
66
  }
  
  void addToLog(char* newStr)
  {
cdaa873f   henyxia   Log fixed
67
  	for(int i=LOG_LINES-2; i>=0; i--)
ea1218f1   henyxia   UI finished
68
69
70
71
  		strcpy(uilog[i+1], uilog[i]);
  	strcpy(uilog[0], newStr);
  }
  
19082eeb   henyxia   UI design not wor...
72
  char* fillLogWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
73
  {
19082eeb   henyxia   UI design not wor...
74
  	strncpy(buf, text, LOG_LENGTH);
19082eeb   henyxia   UI design not wor...
75
  	if(strlen(buf) > 0)
297a72df   henyxia   Added sender
76
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1);
19082eeb   henyxia   UI design not wor...
77
  	else
297a72df   henyxia   Added sender
78
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-12);
19082eeb   henyxia   UI design not wor...
79
80
  
  	return buf;
ea1218f1   henyxia   UI finished
81
82
83
84
85
86
87
88
89
90
  }
  
  
  void setStartTime(char* sT)
  {
  	strcpy(started, sT);
  }
  
  void displayUI()
  {
19082eeb   henyxia   UI design not wor...
91
  	char	buffer[LOG_LENGTH];
ea1218f1   henyxia   UI finished
92
93
  	//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...
94
  	printf("\u2503 %s \u2503      T H E      \u2503", fillHeaderWithSpaces(buffer, "Started since"));
ea1218f1   henyxia   UI finished
95
  	sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag");
3bdc9daf   henyxia   NFC Added
96
97
98
     	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
99
  	sprintf(buffer, "PID %d", mainPid);
19082eeb   henyxia   UI design not wor...
100
  	printf("\u2503 %s \u2503  P R O J E C T  \u2503 ", fillHeaderWithSpaces(buffer, buffer));
ea1218f1   henyxia   UI finished
101
  	sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
19082eeb   henyxia   UI design not wor...
102
103
  	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");
ea1218f1   henyxia   UI finished
104
  	//body
19082eeb   henyxia   UI design not wor...
105
106
107
108
109
110
  	int i=0;
  	while(i<LOG_LINES)
  	{
  		printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1]));
  		i++;
  	}
ea1218f1   henyxia   UI finished
111
112
  	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");
  }