forked from gicking/stm8gal
/
misc.c
410 lines (304 loc) · 11.4 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
\file misc.c
\author G. Icking-Konert
\date 2014-03-14
\version 0.1
\brief implementation of misc routines
implementation of routines not really fitting anywhere else
*/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "version.h"
#include "misc.h"
#include "globals.h"
// WIN32 specific
#if defined(WIN32)
#include "Windows.h"
// forground colours
#define FG_BLACK 0
#define FG_BLUE ( FOREGROUND_BLUE | FOREGROUND_INTENSITY )
#define FG_BROWN ( FOREGROUND_RED | FOREGROUND_GREEN )
#define FG_DARKBLUE ( FOREGROUND_BLUE )
#define FG_DARKGREY ( FOREGROUND_INTENSITY )
#define FG_GREEN ( FOREGROUND_GREEN )
#define FG_GREY ( FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE )
#define FG_LIGHTBLUE ( FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY )
#define FG_LIGHTGREEN ( FOREGROUND_GREEN | FOREGROUND_INTENSITY )
#define FG_PINK ( FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY )
#define FG_RED ( FOREGROUND_RED )
#define FG_LIGHTRED ( FOREGROUND_RED | FOREGROUND_INTENSITY )
#define FG_TURQUOISE ( FOREGROUND_BLUE | FOREGROUND_GREEN )
#define FG_VIOLET ( FOREGROUND_RED | FOREGROUND_BLUE )
#define FG_WHITE ( FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY )
#define FG_YELLOW ( FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY )
// background colours
#define BG_BLACK ( 0 )
#define BG_BLUE ( BACKGROUND_BLUE | BACKGROUND_INTENSITY )
#define BG_BROWN ( BACKGROUND_RED | BACKGROUND_GREEN )
#define BG_DARKBLUE ( BACKGROUND_BLUE )
#define BG_DARKGREY ( BACKGROUND_INTENSITY )
#define BG_GREEN ( BACKGROUND_GREEN )
#define BG_GREY ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE )
#define BG_LIGHTBLUE ( BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY )
#define BG_LIGHTGREEN ( BACKGROUND_GREEN | BACKGROUND_INTENSITY )
#define BG_PINK ( BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY )
#define BG_RED ( BACKGROUND_RED )
#define BG_LIGHTRED ( BACKGROUND_RED | BACKGROUND_INTENSITY )
#define BG_TURQUOISE ( BACKGROUND_BLUE | BACKGROUND_GREEN )
#define BG_VIOLET ( BACKGROUND_RED | BACKGROUND_BLUE )
#define BG_WHITE ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY )
#define BG_YELLOW ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY )
#endif // WIN32
void Error(const char *format, ...)
{
va_list vargs;
va_start(vargs, format);
setConsoleColor(PRM_COLOR_RED);
fprintf(stderr, "Error: ");
vfprintf(stderr, format, vargs);
va_end(vargs);
fprintf(stderr, "\n");
Exit(1, 1);
}
/**
\fn void Exit(uint8_t code, uint8_t pause)
\brief terminate program
\param[in] code return code of application to commandline
\param[in] pause wait for keyboard input before terminating
Terminate program. Replaces standard exit() routine which doesn't allow
for a \<return\> request prior to closing of the console window.
*/
void Exit(uint8_t code, uint8_t pause) {
// on error code !=0 ring bell
if (code) {
printf("\a");
fflush(stdout);
}
// reset text color to default
setConsoleColor(PRM_COLOR_DEFAULT);
// optionally prompt for <return>
if ((pause) && (!g_backgroungOperation)) {
printf("\npress <return> to exit");
fflush(stdout);
fflush(stdin);
getchar();
}
printf("\n");
// terminate application
exit(code);
} // Exit
/**
\fn void stripPath(char *in, char *out)
\brief strip path from application name
\param[in] in name of application incl. path
\param[in] out name of application excl. path
strip pathname from application name
*/
void stripPath(char *in, char *out) {
int32_t i;
char *tmp;
// find position of last path delimiter '/' (Posix) or '\' (Win)
tmp = in;
for (i=0; i<strlen(in); i++) {
if ((in[i] == '/') || (in[i] == '\\'))
tmp = in+i+1;
}
// copy name without path
sprintf(out, "%s", tmp);
} // stripPath
/**
\fn void get_version(uint16_t vers, uint8_t *major, uint8_t *minor, uint8_t *build, uint8_t *status)
\brief extract major / minor / build revision number from 16b identifier
\param[in] vers 16b revision number in format xx.xxxxxxxx.xxxxx.x
\param[out] major major revision number [15:14] -> 0..3
\param[out] minor minor revision number [13:6] -> 0..255
\param[out] build build number [5:1] -> 0..31
\param[out] status status [0] -> 0=beta; 1=released
extract major / minor / build revision number from 16b identifier
in format xx.xxxxxxxx.xxxxx.x
*/
void get_version(uint16_t vers, uint8_t *major, uint8_t *minor, uint8_t *build, uint8_t *status) {
// major version ([15:14] -> 0..7)
*major = (uint8_t) ((vers & 0xC000) >> 14);
// minor version ([13:6] -> 0..255)
*minor = (uint8_t) ((vers & 0x3FC0) >> 6);
// build number ([5:1] -> 0..31)
*build = (uint8_t) ((vers&0x003E) >> 1);
// release status ([0] -> 0=beta; 1=released)
*status = (uint8_t) (vers&0x0001);
} // get_version
/**
\fn void get_app_name(char *in, uint16_t vers, char *out)
\brief get application name and version
\param[in] in name of application incl. path
\param[in] vers 16b version identifier
\param[in] out name of application + version number
print application name and major / minor / build revision numbers. Remove path from app name
*/
void get_app_name(char *in, uint16_t vers, char *out) {
int32_t i;
char *tmp;
uint8_t major, minor, build, status;
// find position of last path delimiter '/' (Posix) or '\' (Win)
tmp = in;
for (i=0; i<strlen(in); i++) {
if ((in[i] == '/') || (in[i] == '\\'))
tmp = in+i+1;
}
// extract major / minor / build revision number
get_version(vers, &major, &minor, &build, &status);
// print app name & version
if (status==0)
sprintf(out, "%s (v%d.%d.%d beta)", tmp, major, minor, build);
else
sprintf(out, "%s (v%d.%d.%d)", tmp, major, minor, build);
} // get_app_name
/**
\fn void setConsoleTitle(const char *title)
\brief set title of console window
\param[in] title title for console window
set console title to application name + version number
Win32: uses Windows API functions
POSIX: use console escape sequence
*/
//
#if defined(WIN32) || defined(__APPLE__) || defined(__unix__)
void setConsoleTitle(const char *title) {
// for background operation skip to avoid strange control characters
if (g_backgroungOperation)
return;
#if defined(WIN32)
SetConsoleTitle(title);
#elif defined(__APPLE__) || defined(__unix__)
printf("%c]0;%s%c", '\033', title, '\007');
#else
#error unknown OS type
#endif
} // SetTitle
#endif // WIN32 || __APPLE__ || __unix__
/**
\fn void setConsoleColor(uint8_t color)
\brief set console text color
\param[in] color new text color
switch text color in console output to specified value
Win32: uses Windows API functions
POSIX: uses VT100 escape codes
uC: send command to PC
*/
void setConsoleColor(uint8_t color) {
// for background operation skip to avoid strange control characters
if (g_backgroungOperation)
return;
#if defined(WIN32)
static WORD oldColor, colorBck;
static char flag=0;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
// at first call get and store current text and backgound color
if (flag==0) {
flag = 1;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
oldColor = csbiInfo.wAttributes;
colorBck = (csbiInfo.wAttributes) & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
}
// set to text color
switch (color) {
// revert color to start value
case PRM_COLOR_DEFAULT:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), oldColor);
break;
// set color to black; retain background color
case PRM_COLOR_BLACK:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_BLACK | colorBck);
break;
// set color to blue; retain background color
case PRM_COLOR_BLUE:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_BLUE | colorBck);
break;
// set color to green; retain background color
case PRM_COLOR_GREEN:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_GREEN | colorBck);
break;
// set color to red; retain background color
case PRM_COLOR_RED:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_LIGHTRED | colorBck);
break;
// set color to pink; retain background color
case PRM_COLOR_PINK:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_PINK | colorBck);
break;
// set color to white; retain background color
case PRM_COLOR_WHITE:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_WHITE | colorBck);
break;
// set color to yellow; retain background color
case PRM_COLOR_YELLOW:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FG_YELLOW | colorBck);
break;
// else revert color to default
default:
//fprintf(stderr, "\n\ndefault\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), oldColor);
} // switch (color)
#elif defined(__APPLE__) || defined(__unix__)
// set to text color (see http://linuxgazette.net/issue65/padala.html)
switch (color) {
// revert color to start value
case PRM_COLOR_DEFAULT:
printf("\033[0m");
//printf("\n\nrevert\n\n");
fflush(stdout);
break;
// set color to black; retain background color
case PRM_COLOR_BLACK:
printf("\033[30m");
//printf("\n\nblack\n\n");
fflush(stdout);
break;
// set color to blue; retain background color
case PRM_COLOR_BLUE:
printf("\033[1;34m");
//printf("\n\nblue\n\n");
fflush(stdout);
break;
// set color to green; retain background color
case PRM_COLOR_GREEN:
printf("\033[1;32m");
//printf("\n\ngreen\n\n");
fflush(stdout);
break;
// set color to red; retain background color
case PRM_COLOR_RED:
printf("\033[1;31m");
//printf("\n\nred\n\n");
fflush(stdout);
break;
// set color to pink; retain background color
case PRM_COLOR_PINK:
printf("\033[1;35m");
//printf("\n\npink\n\n");
fflush(stdout);
break;
// set color to white; retain background color
case PRM_COLOR_WHITE:
printf("\033[37m");
//printf("\n\nwhite\n\n");
fflush(stdout);
break;
// set color to yellow; retain background color
case PRM_COLOR_YELLOW:
printf("\033[1;33m");
//printf("\n\nyellow\n\n");
fflush(stdout);
break;
// else revert color to default
default:
printf("\033[0m");
//printf("\n\ndefault\n\n");
fflush(stdout);
} // switch (color)
#else
#error unknown OS type
#endif
} // setConsoleColor
// end of file