avr_utils.h

 avr_utils.h

Go to the documentation of this file.
1 
33 #ifndef AVR_UTILS_H_
34 #define AVR_UTILS_H_
35 
36 #include <stddef.h>
37 
38 #pragma message ( "avr_utils.h included" )
39 
47 void UTILS_ReverseBuffer(uint8_t *Buffer, size_t Size)
48 {
49  uint8_t ch;
50  uint8_t *begin_ptr = Buffer;
51  uint8_t *end_ptr = Buffer + Size - 1;
52  for (size_t i = 0; i < Size / 2; i++)
53  {
54  ch = *end_ptr;
55  *end_ptr = *begin_ptr;
56  *begin_ptr = ch;
57  begin_ptr++;
58  end_ptr--;
59  }
60 }
61 
68 void UTILS_ReverseString(char *Buffer)
69 {
70  size_t size;
71  char ch;
72  char *begin_ptr = Buffer;
73  char *end_ptr = Buffer;
74  while (*++end_ptr);
75  size = end_ptr - begin_ptr;
76  for (size_t i = 0; i < size / 2; i++)
77  {
78  ch = *end_ptr;
79  *end_ptr = *begin_ptr;
80  *begin_ptr = ch;
81  begin_ptr++;
82  end_ptr--;
83  }
84 }
85 
93 uint8_t UTILS_IntToString(int Number, char *Buffer)
94 {
95  uint8_t i = 0;
96  uint8_t sign = 0;
97  if (Number < 0)
98  {
99  Number *= -1;
100  sign = 1;
101  }
102  do
103  {
104  Buffer[i++] = Number % 10 + '0';
105  }
106  while ((Number /= 10) > 0);
107  if (sign == 1)
108  {
109  Buffer[i++] = '-';
110  }
111  UTILS_ReverseBuffer((uint8_t*)Buffer,i);
112  Buffer[i] = '\0';
113  return i;
114 }
115 
123 uint8_t UTILS_LongToString(long Number, char *Buffer)
124 {
125  uint8_t i = 0;
126  uint8_t sign = 0;
127  if (Number < 0)
128  {
129  Number *= -1;
130  sign = 1;
131  }
132  do
133  {
134  Buffer[i++] = Number % 10 + '0';
135  }
136  while ((Number /= 10) > 0);
137  if (sign == 1)
138  {
139  Buffer[i++] = '-';
140  }
141  UTILS_ReverseBuffer((uint8_t*)Buffer,i);
142  Buffer[i] = '\0';
143  return i;
144 }
145 
154 uint8_t UTILS_FloatToString(float Number, char *Buffer, uint8_t Precision)
155 {
156  int ipart = (int)Number;
157  float fpart = Number - (float)ipart;
158  size_t length1 = UTILS_IntToString(ipart,Buffer);
159  if (Precision != 0)
160  {
161  Buffer[length1] = '.';
162  }
163  if ((fpart < 0.1 && fpart > 0.0) || (fpart < -0.0 && fpart > -1.0))
164  {
165  length1++;
166  Buffer[length1] = '0';
167  }
168  fpart = fpart * pow(10, Precision);
169  fpart = abs(fpart);
170  size_t length2 = UTILS_IntToString((int)fpart,Buffer+length1+1);
171  Buffer[length1 + 1 + length2] = '\0';
172  return length1 + 1 + length2;
173 }
174 
181 int UTILS_StringToInt(char* Buffer)
182 {
183  uint8_t sign = 0;
184  int number = 0;
185  if (*Buffer == '-')
186  {
187  sign = 1;
188  Buffer++;
189  }
190  while (*Buffer)
191  {
192  number = number*10 + (*Buffer -'0');
193  Buffer++;
194  }
195  if (sign)
196  {
197  number = (-1) * number;
198  }
199  return number;
200 }
201 
208 int UTILS_StringToLong(char* Buffer)
209 {
210  uint8_t sign = 0;
211  long number = 0;
212  if (*Buffer == '-')
213  {
214  sign = 1;
215  Buffer++;
216  }
217  while (*Buffer)
218  {
219  number = number*10 + (*Buffer -'0');
220  Buffer++;
221  }
222  if (sign)
223  {
224  number = (-1) * number;
225  }
226  return number;
227 }
228 
229 #endif /* AVR_UTILS_H_ */

Post a Comment

Post a Comment (0)

Previous Post Next Post