-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm-utils.c
321 lines (255 loc) · 12.1 KB
/
gm-utils.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
/*
File: gm-utils.c
Project: m-gen
Version: 1.1
Copyright (C) 2019 leopardus
This file is part of m-gen
https://github.com/Leopardus4/m-gen
m-gen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3,
as published by the Free Software Foundation.
m-gen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
with m-gen. If not, see
http://www.gnu.org/licenses/
*/
#include <stdio.h>
#include <string.h> //strcmp(), strrchr()
#include <ctype.h> //toupper()
#include "gm-utils.h"
#include "m-gen.h"
/*---------------------------------------------------*/
/*
Function copies strFrom to strTo and adds or changes extension
to newExt (in format ".new" - with or without dot).
size - size of strTo buffer.
If strTo is too short, name from strFrom will be truncated to fit newExt.
*/
void changeExtension(char* strTo, const char* strFrom, size_t size, const char* newExt)
{
char* extension;
int position;
const int newExtLenght = strlen(newExt) + 1;
extension = strrchr(strFrom, '.');
if(extension == NULL)
{
position = strlen(strFrom);
if(position <= (size - newExtLenght) )
snprintf(strTo, size, "%s%s", strFrom, newExt);
else
{
snprintf(strTo, size, "%s", strFrom);
snprintf( &(strTo[size-newExtLenght]), newExtLenght, "%s", newExt);
}
}
else
{
snprintf(strTo, size, "%s", strFrom);
position = (int) (extension - strFrom);
if(position <= (size - newExtLenght) )
snprintf( &(strTo[position]), size - position, "%s", newExt);
else
snprintf( &(strTo[size-newExtLenght]), newExtLenght, "%s", newExt);
}
}
/*---------------------------------------------------*/
/*
This function tests if given file exist in working directory
Returns 1 if yes
or 0 if no.
*/
int fileExist(const char* filename)
{
FILE* fp = fopen(filename, "r");
if(fp != NULL)
{
fclose(fp);
return 1;
}
else
return 0;
}
/*---------------------------------------------------*/
/*
Function creates 'Header guard' from given filename.
All lower case will be replaced by upper case,
digits and upper case will be copied,
other characters will be replaced by underscore character.
Function will end:
if strFrom[i] == '\0' ---'zero' will be copied
or if strFrom[size-1] != 0 ---strFrom[size-1] will be ignored
and strTo[size-1] will get '\0' value.
*/
void createHeaderGuard(char* strTo, const char* strFrom, size_t size)
{
for(int i=0; i<(size-1); ++i)
{
if(isalnum(strFrom[i]))
{
if(islower(strFrom[i]))
strTo[i] = toupper( (unsigned)strFrom[i] );
else
strTo[i] = strFrom[i];
}
else if(strFrom[i] == '\0')
{
strTo[i] = '\0';
return;
}
else
strTo[i] = '_';
}
strTo[size-1] = '\0';
}
/*---------------------------------------------------*/
/*
Funcion finds given section (sectionLetter) (in format $x)
in given file (read permission required),
sets position in file to start of this sections (after '$' and sectionLetter)
and additionally returns this position
or -1 in case of error.
*/
long findSection(FILE* fp, char sectionLetter)
{
char c;
if(sectionLetter == '$')
return -1;
// goto start of file
if( fseek(fp, 0, SEEK_SET) != 0)
return -1;
while( (c=getc(fp)) != EOF )
{
if(c == '$') //'dollar' - new section sign
{
c = getc(fp); //next character
if(c == sectionLetter)
return ftell(fp);
}
}
message(ERR, "Cannot find section '$%c' in input file. \n", sectionLetter);
return -1;
}
/*---------------------------------------------------*/
void printPinModes(FILE* output)
{
fprintf(output,
"Avaiable modes of GPIO: \n"
" digital gpio - i. e. communication with other digital chips \n"
" i digital Input \n"
" o digital Output \n"
" d Double (in+out) \n"
" \n"
" other modes: \n"
" b Button - for input buttons / sensors \n"
" (it uses internal pull-up resistor) \n"
" l active Low output - / for transistors / leds etc. \n"
" h active High output - / \n"
" \n"
);
}
/*---------------------------------------------------*/
void printPinMacros(FILE* output)
{
fprintf(output,
"m-gen creates set of macros for each gpio. \n"
"It depends on gpio mode from .gm file. \n"
" \n"
"All macros which should only set something in registers \n"
"are equivalents of void xxxx(void) functions, \n"
"or in 'inline' mode, are declared as such. \n"
" \n"
"Macros created to test something (abc_isActive(), ...) \n"
"returns zero (false) or non-zero value (true), \n"
"so may be used in conditionals (if-else, \"?:\", ...) \n"
"If 'inline' mode is used, they are declared as \n"
"uintX_t xxxx(void), where \"X\" is the fastest length for \n"
"given CPU (i. e. 32 for Cortex M). \n"
" \n"
" \n"
" \n"
" [ #define gpio_enableAccess() ] \n"
" This macro is present only on some platforms. \n"
" If present, it's used to i. e. turn on gpio clock \n"
" (it's common for all gpio, \n"
" and MUST be used before any other macros) \n"
" \n"
" \n"
" \n"
"Macros for 1 pin: (abc - symbolic pin name from .gm file) \n"
" \n"
"digital Input (i): \n"
" \n"
" #define abc_dirIn() - initializing gpio as input \n"
" \n"
" #define abc_isHigh() + testing state of gpio \n"
" | i. e.: \n"
" #define abc_isLow() + if( abc_isHigh() ) {code} \n"
" \n"
" \n"
" \n"
"digital Output (o): \n"
" \n"
" #define abc_dirOut() - initializing gpio as output \n"
" \n"
" #define abc_setHigh() + changing state of gpio \n"
" | (high/low) \n"
" #define abc_setLow() + \n"
" \n"
" It's required to use setHigh() or setLow() macro \n"
" after dirOut(), because state of gpio is unknown. \n"
" \n"
" \n"
" \n"
"digital input and output (d): \n"
" \n"
" [ #define abc_init() ] - present only on some platforms\n"
" It initializes pin as gpio \n"
" (and i. e. disables pull-up) \n"
" If present, should be used before any operations \n"
" on this gpio. \n"
" It's required to use dirIn() or dirOut() \n"
" after init(), because direction is unknown. \n"
" \n"
" #define abc_dirIn() + \n"
" #define abc_dirOut() | As described above \n"
" | \n"
" #define abc_isHigh() | \n"
" #define abc_isLow() | \n"
" | \n"
" #define abc_setHigh() | \n"
" #define abc_setLow() + \n"
" \n"
" \n"
" \n"
"active low / active high output (l/h): \n"
" (macros for both are identicall, \n"
" so it's possible to exchange them) \n"
" \n"
" #define abc_asOutput() - initializing gpio as output \n"
" and changing state to 'Off' \n"
" \n"
" #define abc_On() - changing state of gpio \n"
" to turn 'On' actuator \n"
" \n"
" #define abc_Off() - changing state of gpio \n"
" to turn 'Off' actuator \n"
" \n"
" \n"
" \n"
"active low input with internal pull-up resistor (b): \n"
" \n"
" #define abc_asInput() - initializing gpio as input \n"
" and turning on pull-up \n"
" \n"
" #define abc_isActive() - checking if sensor is active \n"
" i. e.: \n"
" if( abc_isActive() ) {code} \n"
" \n"
" #define abc_isInactive()- checking if sensor is inactive\n"
" \n"
" \n"
);
}