GlobLib
HAL and API libraries for MCUs and hardware.
string11.c
Go to the documentation of this file.
1 
17 #include "string11.h"
18 
19 static void (*out)(uint8_t);
20 
21 int32_t str2int(char *buffer){
22 
23  uint8_t isMinus = 0;
24  uint8_t i = 0;
25  int32_t number = 0;
26 
27  if(buffer[0] == '-'){
28 
29  isMinus = 1;
30  i = 1;
31  }
32 
33 
34  while((buffer[i] != '\0') && i < 125){
35 
36  number *= 10;
37  number += buffer[i]-48;
38  i++;
39 
40  if(i > 120){
41 
42  number = 0;
43  break;
44  }
45  }
46 
47  if(isMinus){
48 
49  number *= -1;
50  }
51 
52  return number;
53 
54 }
55 
56 uint32_t str2uint(char *buffer){
57 
58  uint8_t i = 0;
59  uint32_t number = 0;
60 
61  while((buffer[i] != '\0') && i < 125){
62 
63  number *= 10;
64  number += buffer[i]-48;
65  i++;
66 
67  if(i > 120){
68 
69  number = 0;
70  break;
71  }
72  }
73  return number;
74 }
75 
76 float str2float(char *buffer){
77 
78  uint8_t isMinus = 0;
79  uint8_t i = 0;
80  float number = 0;
81  uint8_t pointLocation = 127;
82 
83  if(buffer[0] == '-'){
84 
85  isMinus = 1;
86  i = 1;
87  }
88 
89  while((buffer[i] != '\0') && i < 125){
90 
91  if(buffer[i] == 46){
92 
93  pointLocation = i;
94  i++;
95  continue;
96  }
97 
98  number *= 10;
99  number += buffer[i]-48;
100  i++;
101 
102  if(i > 120){
103 
104  number = 0;
105  break;
106  }
107  }
108 
109  if(isMinus){
110 
111  number *= -1;
112  }
113 
114  for(int8_t j = 0; j < (i - pointLocation - 1); j++){
115 
116  number /= 10;
117  }
118 
119  return number;
120 }
121 
122 void STRING11_setOutput(void (*out_fun)(uint8_t)){
123 
124  out = out_fun;
125  return;
126 }
127 
129 
130  if(out == NULL){
131 
132  return NULL;
133  }
134  return out;
135 }
136 
137 void print_c(char x){
138 
139  out((char)(x));
140 }
141 
142 void print_u8(uint8_t x){
143 
144  if(x < 10){
145 
146  out(x+48);
147  return;
148  }
149  if(x < 100){
150 
151  out((uint8_t)(x/10) + 48);
152  out(x%10 + 48);
153  return;
154  }
155  else{
156 
157  out((uint8_t)(x/100) + 48);
158  x = x%100;
159  out((uint8_t)(x/10) + 48);
160  out(x%10 + 48);
161  return;
162  }
163 }
164 
165 void print_8(int8_t x){
166 
167  if(x < 0){
168 
169  out('-');
170  print_u8(x*-1);
171  return;
172  }
173 
174  print_u8(x);
175 
176 }
177 
178 void print_u16(uint16_t x){
179 
180  uint16_t top = 10000;
181  uint16_t mod;
182  uint8_t printed = 0;
183 
184  if(x == 0){
185 
186  out((char)'0');
187  return;
188  }
189 
190  while(top){
191 
192  mod = x/top;
193  if(mod || printed){
194  out((uint8_t)(mod+48));
195  printed = 1;
196  }
197  x = x % top;
198  top /= 10;
199  }
200  return;
201 }
202 
203 void print_16(int16_t x){
204 
205  if(x < 0){
206 
207  out('-');
208  print_u16(x*-1);
209  }
210  else{
211 
212  print_u16(x);
213  }
214  return;
215 }
216 
217 void print_u32(uint32_t x){
218 
219  uint32_t top = 1000000000;
220  uint32_t mod;
221  uint8_t printed = 0;
222 
223  if(x == 0){
224 
225  out((char)'0');
226  printed = 1;
227  return;
228  }
229 
230  while(top){
231 
232  mod = x/top;
233  if(mod || printed){
234  out((uint8_t)(mod+48));
235  printed = 1;
236  }
237  x = x % top;
238  top /= 10;
239  }
240  return;
241 }
242 
243 void print_32(int32_t x){
244 
245  if(x < 0){
246 
247  out('-');
248  print_u32(x*-1);
249  }
250  else{
251 
252  print_u32(x);
253  }
254  return;
255 
256 }
257 
258 void print_f(float x){
259 
260  if(x < 0){
261 
262  out('-');
263  x *= -1;
264  }
265 
266  int32_t intpart = (int32_t)x;
267  float decpart = x - intpart;
268  uint8_t count = 0;
269 
270  print_u32(intpart);
271 
272  out((char)'.');
273 
274  while(count < MAX_DEC){
275 
276  decpart *= 10;
277  out((uint8_t)decpart + 48);
278  decpart -= (uint8_t)decpart;
279  count++;
280  }
281 
282 }
283 
284 void print_s(char *x){
285 
286  while(*x != 0){
287 
288  out((char)*x);
289  x++;
290  }
291 
292 }
293 
294 void prints_c(char x){
295 
296  print_c(x);
297  print_c((char)' ');
298 }
299 
300 void prints_u8(uint8_t x){
301 
302  print_u8(x);
303  print_c((char)' ');
304 
305 }
306 
307 void prints_8(int8_t x){
308 
309  print_8(x);
310  print_c((char)' ');
311 }
312 
313 void prints_u16(uint16_t x){
314 
315  print_u16(x);
316  print_c((char)' ');
317 }
318 
319 void prints_16(int16_t x){
320 
321  print_16(x);
322  print_c((char)' ');
323 }
324 
325 void prints_u32(uint32_t x){
326 
327  print_u32(x);
328  print_c((char)' ');
329 }
330 
331 void prints_32(int32_t x){
332 
333  print_32(x);
334  print_c((char)' ');
335 }
336 
337 void prints_f(float x){
338 
339  print_f(x);
340  print_c((char)' ');
341 }
342 
343 void prints_s(char *x){
344 
345  print_s(x);
346  print_c((char)' ');
347 }
348 
349 void printc_c(char x){
350 
351  print_c(x);
352  print_c((char)',');
353 }
354 
355 void printc_u8(uint8_t x){
356 
357  print_u8(x);
358  print_c((char)',');
359 
360 }
361 
362 void printc_8(int8_t x){
363 
364  print_8(x);
365  print_c((char)',');
366 }
367 
368 void printc_u16(uint16_t x){
369 
370  print_u16(x);
371  print_c((char)',');
372 }
373 
374 void printc_16(int16_t x){
375 
376  print_16(x);
377  print_c((char)',');
378 }
379 
380 void printc_u32(uint32_t x){
381 
382  print_u32(x);
383  print_c((char)',');
384 }
385 
386 void printc_32(int32_t x){
387 
388  print_32(x);
389  print_c((char)',');
390 }
391 
392 void printc_f(float x){
393 
394  print_f(x);
395  print_c((char)',');
396 }
397 
398 void printc_s(char *x){
399 
400  print_s(x);
401  print_c((char)',');
402 }
403 
404 void printl_c(char x){
405 
406  print_c(x);
407  print_c((char)'\n');
408  print_c((char)'\r');
409 }
410 
411 void printl_u8(uint8_t x){
412 
413  print_u8(x);
414  print_c((char)'\n');
415  print_c((char)'\r');
416 
417 }
418 
419 void printl_8(int8_t x){
420 
421  print_8(x);
422  print_c((char)'\n');
423  print_c((char)'\r');
424 }
425 
426 void printl_u16(uint16_t x){
427 
428  print_u16(x);
429  print_c((char)'\n');
430  print_c((char)'\r');
431 }
432 
433 void printl_16(int16_t x){
434 
435  print_16(x);
436  print_c((char)'\n');
437  print_c((char)'\r');
438 }
439 
440 void printl_u32(uint32_t x){
441 
442  print_u32(x);
443  print_c((char)'\n');
444  print_c((char)'\r');
445 }
446 
447 void printl_32(int32_t x){
448 
449  print_32(x);
450  print_c((char)'\n');
451  print_c((char)'\r');
452 }
453 
454 void printl_f(float x){
455 
456  print_f(x);
457  print_c((char)'\n');
458  print_c((char)'\r');
459 }
460 
461 void printl_s(char *x){
462 
463  print_s(x);
464  print_c((char)'\n');
465  print_c((char)'\r');
466 }
467 
468 void printt_c(char x){
469 
470  print_c(x);
471  print_c((char)'\t');
472 }
473 
474 void printt_u8(uint8_t x){
475 
476  print_u8(x);
477  print_c((char)'\t');
478 
479 }
480 
481 void printt_8(int8_t x){
482 
483  print_8(x);
484  print_c((char)'\t');
485 }
486 
487 void printt_u16(uint16_t x){
488 
489  print_u16(x);
490  print_c((char)'\t');
491 }
492 
493 void printt_16(int16_t x){
494 
495  print_16(x);
496  print_c((char)'\t');
497 }
498 
499 void printt_u32(uint32_t x){
500 
501  print_u32(x);
502  print_c((char)'\t');
503 }
504 
505 void printt_32(int32_t x){
506 
507  print_32(x);
508  print_c((char)'\t');
509 }
510 
511 void printt_f(float x){
512 
513  print_f(x);
514  print_c((char)'\t');
515 }
516 
517 void printt_s(char *x){
518 
519  print_s(x);
520  print_c((char)'\t');
521 }
522 
void printl_u8(uint8_t x)
Send a unsigned byte plus a new line to the output stream. Implemented internally.
Definition: string11.c:411
void printl_c(char x)
Send a character plus a new line to the output stream. Implemented internally.
Definition: string11.c:404
float str2float(char *buffer)
Convert a null terminated string to a float.
Definition: string11.c:76
void printc_32(int32_t x)
Send a signed integer plus a comma to the output stream. Implemented internally.
Definition: string11.c:386
void printl_16(int16_t x)
Send a signed short plus a new line to the output stream. Implemented internally. ...
Definition: string11.c:433
void printt_8(int8_t x)
Send a signed byte plus a tab to the output stream. Implemented internally.
Definition: string11.c:481
void printt_u8(uint8_t x)
Send a unsigned byte plus a tab to the output stream. Implemented internally.
Definition: string11.c:474
void STRING11_setOutput(void(*out_fun)(uint8_t))
Set the target output stream for print functions.
Definition: string11.c:122
void print_u16(uint16_t x)
Send a unsigned short to the output stream. Implemented internally.
Definition: string11.c:178
void(* v_fp_u8)(uint8_t)
Function pointer typedef for void function with uint8_t parameter.
Definition: string11.h:55
v_fp_u8 STRING11_getOutput(void)
Get the current target output stream.
Definition: string11.c:128
void printt_u16(uint16_t x)
Send a unsigned short plus a tab to the output stream. Implemented internally.
Definition: string11.c:487
void print_16(int16_t x)
Send a signed short to the output stream. Implemented internally.
Definition: string11.c:203
void printc_u16(uint16_t x)
Send a unsigned short plus a comma to the output stream. Implemented internally.
Definition: string11.c:368
void print_c(char x)
Send a character to the output stream. Implemented internally.
Definition: string11.c:137
void printt_u32(uint32_t x)
Send a unsigned integer plus a tab to the output stream. Implemented internally.
Definition: string11.c:499
Header file for C11 string library.
void printl_f(float x)
Send a float plus a new line to the output stream. Implemented internally.
Definition: string11.c:454
void printt_f(float x)
Send a float plus a tab to the output stream. Implemented internally.
Definition: string11.c:511
void prints_8(int8_t x)
Send a signed byte plus a space to the output stream. Implemented internally.
Definition: string11.c:307
void printl_u32(uint32_t x)
Send a unsigned integer plus a new line to the output stream. Implemented internally.
Definition: string11.c:440
int32_t str2int(char *buffer)
Convert a null terminated string to an integer.
Definition: string11.c:21
void printt_c(char x)
Send a character plus a tab to the output stream. Implemented internally.
Definition: string11.c:468
void printt_32(int32_t x)
Send a signed integer plus a tab to the output stream. Implemented internally.
Definition: string11.c:505
void print_32(int32_t x)
Send a signed integer to the output stream. Implemented internally.
Definition: string11.c:243
void printt_s(char *x)
Send a string literal plus a tab to the output stream. Implemented internally.
Definition: string11.c:517
void prints_16(int16_t x)
Send a signed short plus a space to the output stream. Implemented internally.
Definition: string11.c:319
void prints_u8(uint8_t x)
Send a unsigned byte plus a space to the output stream. Implemented internally.
Definition: string11.c:300
void printc_u32(uint32_t x)
Send a unsigned integer plus a comma to the output stream. Implemented internally.
Definition: string11.c:380
void print_u8(uint8_t x)
Send a unsigned byte to the output stream. Implemented internally.
Definition: string11.c:142
void prints_u32(uint32_t x)
Send a unsigned integer plus a space to the output stream. Implemented internally.
Definition: string11.c:325
void printl_s(char *x)
Send a string literal plus a new line to the output stream. Implemented internally.
Definition: string11.c:461
void print_f(float x)
Send a float to the output stream. Implemented internally.
Definition: string11.c:258
void prints_u16(uint16_t x)
Send a unsigned short plus a space to the output stream. Implemented internally.
Definition: string11.c:313
#define MAX_DEC
Maximum number of decimal places a float will contain.
Definition: flashWrite.h:85
void printc_f(float x)
Send a float plus a comma to the output stream. Implemented internally.
Definition: string11.c:392
void prints_f(float x)
Send a float plus a space to the output stream. Implemented internally.
Definition: string11.c:337
void prints_s(char *x)
Send a string literal plus a space to the output stream. Implemented internally.
Definition: string11.c:343
void printl_u16(uint16_t x)
Send a unsigned short plus a new line to the output stream. Implemented internally.
Definition: string11.c:426
void printl_8(int8_t x)
Send a signed byte plus a new line to the output stream. Implemented internally.
Definition: string11.c:419
void print_s(char *x)
Send a string literal to the output stream. Implemented internally.
Definition: string11.c:284
void print_8(int8_t x)
Send a signed byte to the output stream. Implemented internally.
Definition: string11.c:165
void printc_8(int8_t x)
Send a signed byte plus a comma to the output stream. Implemented internally.
Definition: string11.c:362
uint32_t str2uint(char *buffer)
Convert a null terminated string to an unsigned integer.
Definition: string11.c:56
void prints_32(int32_t x)
Send a signed integer plus a space to the output stream. Implemented internally.
Definition: string11.c:331
void printc_16(int16_t x)
Send a signed short plus a comma to the output stream. Implemented internally.
Definition: string11.c:374
void prints_c(char x)
Send a character plus a space to the output stream. Implemented internally.
Definition: string11.c:294
void print_u32(uint32_t x)
Send a unsigned integer to the output stream. Implemented internally.
Definition: string11.c:217
void printl_32(int32_t x)
Send a signed integer plus a new line to the output stream. Implemented internally.
Definition: string11.c:447
void printc_c(char x)
Send a character plus a comma to the output stream. Implemented internally.
Definition: string11.c:349
void printc_s(char *x)
Send a string literal plus a comma to the output stream. Implemented internally.
Definition: string11.c:398
void printt_16(int16_t x)
Send a signed short plus a tab to the output stream. Implemented internally.
Definition: string11.c:493
void printc_u8(uint8_t x)
Send a unsigned byte plus a comma to the output stream. Implemented internally.
Definition: string11.c:355