PageRenderTime 56ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/uDW/ufuncs.c

https://gitlab.com/tormod/udw-firmware
C | 104 lines | 71 code | 13 blank | 20 comment | 17 complexity | 2bdbbafe4443f0162ef9b12ad2215031 MD5 | raw file
  1. /*
  2. DriveWire server implementation for uDW
  3. - general purpose functions, stdlib replacements etc
  4. Copyright 2014 Tormod Volden
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stm32f10x_usart.h>
  17. #include <uart.h>
  18. #include "udw.h"
  19. #include "ufuncs.h"
  20. int ustrncmp(const char *s1, const char *s2, int n)
  21. {
  22. char p,q;
  23. while ((p = *s1++) == (q = *s2++) && p && --n);
  24. return p-q;
  25. }
  26. int ustrcmp(const char *s1, char *s2)
  27. {
  28. char p,q;
  29. while ((p = *s1++) == (q = *s2++) && p);
  30. return p-q;
  31. }
  32. int ustrcpy(char *d, const char *s)
  33. {
  34. char *o = d;
  35. while ((*d++ = *s++));
  36. return d-o-1;
  37. }
  38. unsigned int ustrlen(char *s)
  39. {
  40. char *o = s;
  41. while (*s++);
  42. return s-o;
  43. }
  44. /* TODO: Use the built-in isdigit() from ctype.h instead? */
  45. int uisdigit(const char c)
  46. {
  47. return c >= '0' && c <= '9';
  48. }
  49. int uscanf2d(const char *start, int *value)
  50. {
  51. const char *s = start;
  52. while (*s && !uisdigit(*s))
  53. s++;
  54. if (uisdigit(*s))
  55. *value = *s++ - '0';
  56. else
  57. return 0;
  58. if (uisdigit(*s))
  59. *value = *value * 10 + *s++ - '0';
  60. return (s - start);
  61. }
  62. #ifdef UDWDEBUG
  63. void debug_print_2x(int num)
  64. {
  65. debug_print_char((num>>4)+((num>>4)>9?'A'-10:'0'));
  66. debug_print_char((num&0xF)+((num&0xF)>9?'A'-10:'0'));
  67. }
  68. void debug_print_4x(int num)
  69. {
  70. debug_print_2x(num >> 8);
  71. debug_print_2x(num & 0xFF);
  72. }
  73. void debug_print_value(int num)
  74. {
  75. debug_print_char('$');
  76. if (num < 0) {
  77. debug_print("NEG? ");
  78. return;
  79. } else if (num > 255) {
  80. debug_print("OVF? ");
  81. return;
  82. }
  83. debug_print_2x(num);
  84. debug_print_char(' ');
  85. return;
  86. }
  87. #endif /* UDWDEBUG */