Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
ascii_support.cpp
Go to the documentation of this file.
1/***********************************************************************/
25#include "embedded_memory.h"
26#include "embedded_math.h"
27#include "ascii_support.h"
28#include <stdlib.h>
29
30static ROM char ASCIItable[]="zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz";
31
32char* itoa( int value, char* result, int base)
33{
34 // check that the base if valid
35 if (base < 2 || base > 36) { *result = '\0'; return result; }
36
37 char* ptr = result, *ptr1 = result, tmp_char;
38 int tmp_value;
39
40 do {
41 tmp_value = value;
42 value /= base;
43 *ptr++ = ASCIItable[35 + (tmp_value - value * base)];
44 } while ( value );
45
46 // Apply negative sign
47 if (tmp_value < 0) *ptr++ = '-';
48 *ptr-- = '\0';
49 while(ptr1 < ptr) {
50 tmp_char = *ptr;
51 *ptr--= *ptr1;
52 *ptr1++ = tmp_char;
53 }
54 return result;
55}
56
57static ROM char hexdigits[] = "0123456789abcdef";
58
59char * utox( char* result, uint32_t value, uint8_t nibbles)
60{
61 for( int i=0; i < nibbles; ++i)
62 {
63 *result = hexdigits[(value >> 4*(nibbles-1)) & 0x0f];
64 value <<= 4;
65 ++result;
66 }
67 *result=0;
68 return result;
69}
70
71char * lutox( char* result, uint64_t value)
72{
73 utox( result, (uint32_t)(value >> 32), 8);
74 utox( result+8, value & 0xffffffff, 8);
75 result[16]=0;
76 return result+16;
77}
78
79char * my_itoa( char * target, int value)
80 {
81 if( value < 0)
82 {
83 *target++ = '-';
84 return my_itoa( target, -value);
85 }
86 if( value < 10)
87 {
88 *target++ = (char)(value + '0');
89 *target=0;
90 return target;
91 }
92 else
93 {
94 target = my_itoa( target, value / 10);
95 return my_itoa( target, value % 10);
96 }
97 }
98
99char * my_ftoa( char * target, float value)
100 {
101 if( value < 0.0f)
102 {
103 *target++='-';
104 value = -value;
105 }
106 else if( value == 0.0f)
107 {
108 *target++='0';
109 *target++='.';
110 *target++='0';
111 *target=0;
112 return target;
113 }
114
115 int exponent=0;
116
117 while( value >= 10.0f)
118 {
119 value /= 10.0f;
120 ++exponent;
121 }
122
123 while( value < 1.0f)
124 {
125 value *= 10.0f;
126 --exponent;
127 }
128
129 uint8_t digit = (uint8_t)value;
130 value -= (float)digit;
131 *target++ = (char)(digit + '0');
132 *target++ = '.';
133
134 value *= 1000000.0f;
135 unsigned fractional_number = round( value);
136 for( unsigned digit=0; digit<6; ++digit)
137 {
138 target[5-digit] = (char)(fractional_number % 10 + '0');
139 fractional_number /= 10;
140 }
141 target+=6;
142 *target++='e';
143 return( my_itoa( target, (int)exponent));
144 }
145
146void portable_ftoa ( char* res, float value, unsigned no_of_decimals, unsigned res_len )
147{
148// ASSERT( no_of_decimals <= res_len-2);
149
150 unsigned i=no_of_decimals;
151 while( i-- > 0)
152 value *=10.0f;
153
154 int number;
155 char sign;
156
157 if( value < 0.0f)
158 {
159 sign = '-';
160 number = (int)( -value + 0.5f);
161 }
162 else
163 {
164 sign = ' ';
165 number = (int)( value + 0.5f);
166 }
167
168 char * target = res + res_len;
169 *target-- = 0;
170
171 for( i=no_of_decimals; i; --i)
172 {
173 *target-- = (char)(number % 10 + '0');
174 number /= 10;
175 }
176
177 *target-- = '.';
178 if( number == 0)
179 {
180 *target -- = '0';
181 }
182 else while(( number > 0) && ( target > res+1))
183 {
184 *target-- = (char)(number % 10 + '0');
185 number /= 10;
186 }
187
188 *target-- = sign;
189
190 while( target >= res)
191 *target-- = ' ';
192
193}
194
196char * format_integer( char *s, int32_t value)
197{
198 if( value < 0)
199 {
200 *s++='-';
201 return format_integer( s, -value);
202 }
203 if( value < 10)
204 *s++ = value + '0';
205 else
206 {
207 s = format_integer( s, value / 10);
208 *s++ = value % 10 + '0';
209 }
210 *s=0;
211 return s;
212}
213
214#define isdigit(c) (c >= '0' && c <= '9')
215float my_atof(const char *s)
216{
217 /*
218 * Copyright (C) 2014, Galois, Inc.
219 * This sotware is distributed under a standard, three-clause BSD license.
220 * Please see the file LICENSE, distributed with this software, for specific
221 * terms and conditions.
222 * Taken from here: https://github.com/GaloisInc/minlibc/blob/master/atof.c
223 */
224
225 // This function stolen from either Rolf Neugebauer or Andrew Tolmach.
226 // Probably Rolf.
227 float a = 0.0f;
228 int e = 0;
229 int c;
230 while ((c = *s++) != '\0' && isdigit(c)) {
231 a = a*10.0f + (float)(c - '0');
232 }
233 if (c == '.') {
234 while ((c = *s++) != '\0' && isdigit(c)) {
235 a = a*10.0f + (float)(c - '0');
236 e = e-1;
237 }
238 }
239 if (c == 'e' || c == 'E') {
240 int sign = 1;
241 int i = 0;
242 c = *s++;
243 if (c == '+')
244 c = *s++;
245 else if (c == '-') {
246 c = *s++;
247 sign = -1;
248 }
249 while (isdigit(c)) {
250 i = i*10 + (c - '0');
251 c = *s++;
252 }
253 e += i*sign;
254 }
255 while (e > 0) {
256 a *= 10.0f;
257 e--;
258 }
259 while (e < 0) {
260 a *= 0.1f;
261 e++;
262 }
263 return a;
264}
265
float my_atof(const char *s)
char * utox(char *result, uint32_t value, uint8_t nibbles)
char * format_integer(char *s, int32_t value)
signed integer to ASCII returning the string end
char * lutox(char *result, uint64_t value)
#define isdigit(c)
void portable_ftoa(char *res, float value, unsigned no_of_decimals, unsigned res_len)
char * my_ftoa(char *target, float value)
char * itoa(int value, char *result, int base)
char * my_itoa(char *target, int value)
Simple and fast ASCII converters.
float a[2]
float s[2][6]
mathematical vector of arbitrary type and size
Definition vector.h:40
defines platform-dependent algorithms and constants
settings to allow compiling embedded software on a PC target
#define ROM