source: firmware/MSR/src/num_conversion.c@ 10907

Last change on this file since 10907 was 10588, checked in by neise, 14 years ago
added MSR Firmware MSR is the little brother of FSC
File size: 8.6 KB
Line 
1//-----------------------------------------------------------------------------
2
3#include "num_conversion.h"
4//-----------------------------------------------------------------------------
5
6//__flash U08 NC_HEX_ARRAY[] = "0123456789ABCDEF";
7U08 NC_HEX_ARRAY[] = "0123456789ABCDEF";
8//-----------------------------------------------------------------------------
9
10U08 nc_buffer[NC_BUFFER_LENGTH]; // Conversion buffer
11U08 nc_format_buffer[NC_BUFFER_LENGTH]; // Format buffer
12
13static U08 nc_int_digits[8];
14static U08 nc_dec_digits[6];
15//-----------------------------------------------------------------------------
16
17pU08 nc_format(pU08 source_ptr,U08 digits)
18{
19 U08 len = strlen((const char *)source_ptr);
20 pU08 dest_ptr = (pU08)&nc_format_buffer;
21
22 // Fillup loop
23 while (digits-- > len)
24 {
25 *dest_ptr++ = NC_FILL_CHAR;
26 }
27
28 // Copy loop
29 while (len--)
30 {
31 *dest_ptr++ = *source_ptr++;
32 }
33
34 *dest_ptr = 0; // Terminate format string
35
36 return (pU08)&nc_format_buffer;
37}
38//-----------------------------------------------------------------------------
39
40pU08 nc_U08_to_str(U08 value,U08 digits)
41{
42 pU08 pstr = nc_buffer;
43
44 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
45 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
46 *pstr++ = ('0' + (value % 10));
47 *pstr = 0;
48
49 if (!digits) // If digits = 0, then return buffer start
50 {
51 return nc_buffer;
52 }
53 else // Do formatted output
54 {
55 return nc_format(nc_buffer,digits);
56 }
57}
58//-----------------------------------------------------------------------------
59
60pU08 nc_S08_to_str(S08 signed_value,U08 digits)
61{
62 pU08 pstr = nc_buffer;
63 U08 value;
64
65 if (signed_value < 0)
66 {
67 *pstr++ = '-';
68 value = -signed_value;
69 }
70 else
71 {
72 value = signed_value;
73 }
74
75 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
76 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
77 *pstr++ = ('0' + (value % 10));
78 *pstr = 0;
79
80 if (!digits) // If digits = 0, then return buffer start
81 {
82 return nc_buffer;
83 }
84 else // Do formatted output
85 {
86 return nc_format(nc_buffer,digits);
87 }
88}
89//-----------------------------------------------------------------------------
90
91pU08 nc_U08_to_hex(U08 value)
92{
93 pU08 pstr = nc_buffer;
94
95 *pstr++ = NC_HEX_ARRAY[value >> 4];
96 *pstr++ = NC_HEX_ARRAY[value & 0x0F];
97 *pstr = 0;
98
99 return (nc_buffer);
100}
101//-----------------------------------------------------------------------------
102
103pU08 nc_U08_to_bin(U08 value)
104{
105 pU08 pstr = nc_buffer;
106 U08 n;
107
108 for (n = 7; n < 8; n--)
109 {
110 if (value & (1 << n))
111 {
112 *pstr++ = '1';
113 }
114 else
115 {
116 *pstr++ = '0';
117 }
118 }
119
120 *pstr = 0;
121
122 return (nc_buffer);
123}
124//-----------------------------------------------------------------------------
125
126pU08 nc_U16_to_str(U16 value,U08 digits)
127{
128 pU08 pstr = nc_buffer;
129
130 if (value >= 10000) *pstr++ = ('0' + (value / 10000));
131 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
132 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
133 if (value >= 10)*pstr++ = ('0' + (value % 100 / 10));
134 *pstr++ = ('0' + (value % 10));
135 *pstr = 0;
136
137 if (!digits) // If digits = 0, then return buffer start
138 {
139 return nc_buffer;
140 }
141 else // Do formatted output
142 {
143 return nc_format(nc_buffer,digits);
144 }
145}
146//-----------------------------------------------------------------------------
147
148pU08 nc_S16_to_str(S16 signed_value,U08 digits)
149{
150 pU08 pstr = nc_buffer;
151 U16 value;
152
153 if (signed_value < 0)
154 {
155 *pstr++ = '-';
156 value = -signed_value;
157 }
158 else
159 {
160 value = signed_value;
161 }
162
163 if (value >= 10000) *pstr++ = ('0' + (value / 10000));
164 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
165 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
166 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
167 *pstr++ = ('0' + (value % 10));
168 *pstr = 0;
169
170 if (!digits) // If digits = 0, then return buffer start
171 {
172 return nc_buffer;
173 }
174 else // Do formatted output
175 {
176 return nc_format(nc_buffer,digits);
177 }
178}
179//-----------------------------------------------------------------------------
180
181pU08 nc_U16_to_hex(U16 value)
182{
183 pU08 pstr = nc_buffer;
184 tMEM16 data;
185
186 data.word = value;
187
188 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) >> 4];
189 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) & 0x0F];
190
191 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) >> 4];
192 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) & 0x0F];
193
194 *pstr = 0;
195
196 return (nc_buffer);
197}
198//-----------------------------------------------------------------------------
199
200pU08 nc_U32_to_str(U32 value,U08 digits)
201{
202 pU08 pstr = nc_buffer;
203
204 if (value >= 1000000000) *pstr++ = ('0' + (value / 1000000000));
205 if (value >= 100000000) *pstr++ = ('0' + (value % 1000000000 / 100000000));
206 if (value >= 10000000) *pstr++ = ('0' + (value % 100000000 / 10000000));
207 if (value >= 1000000) *pstr++ = ('0' + (value % 10000000 / 1000000));
208 if (value >= 100000) *pstr++ = ('0' + (value % 1000000 / 100000));
209 if (value >= 10000) *pstr++ = ('0' + (value % 100000 / 10000));
210 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
211 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
212 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
213
214 *pstr++ = ('0' + (value % 10));
215 *pstr = 0;
216
217 if (!digits) // If digits = 0, then return buffer start
218 {
219 return nc_buffer;
220 }
221 else // Do formatted output
222 {
223 return nc_format(nc_buffer,digits);
224 }
225}
226//-----------------------------------------------------------------------------
227
228pU08 nc_S32_to_str(S32 signed_value,U08 digits)
229{
230 pU08 pstr = nc_buffer;
231 U32 value;
232
233 if (signed_value < 0)
234 {
235 *pstr++ = '-';
236 value = -signed_value;
237 }
238 else
239 {
240 value = signed_value;
241 }
242
243 if (value >= 1000000000) *pstr++ = ('0' + (value / 1000000000));
244 if (value >= 100000000) *pstr++ = ('0' + (value % 1000000000 / 100000000));
245 if (value >= 10000000) *pstr++ = ('0' + (value % 100000000 / 10000000));
246 if (value >= 1000000) *pstr++ = ('0' + (value % 10000000 / 1000000));
247 if (value >= 100000) *pstr++ = ('0' + (value % 1000000 / 100000));
248 if (value >= 10000) *pstr++ = ('0' + (value % 100000 / 10000));
249 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
250 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
251 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
252
253 *pstr++ = ('0' + (value % 10));
254 *pstr = 0;
255
256 if (!digits) // If digits = 0, then return buffer start
257 {
258 return nc_buffer;
259 }
260 else // Do formatted output
261 {
262 return nc_format(nc_buffer,digits);
263 }
264}
265//-----------------------------------------------------------------------------
266
267pU08 nc_U32_to_hex(U32 value)
268{
269 pU08 pstr = nc_buffer;
270 tMEM32 data;
271
272 data.dword = value;
273
274 *pstr++ = NC_HEX_ARRAY[(data.byte[3]) >> 4];
275 *pstr++ = NC_HEX_ARRAY[(data.byte[3]) & 0x0F];
276
277 *pstr++ = NC_HEX_ARRAY[(data.byte[2]) >> 4];
278 *pstr++ = NC_HEX_ARRAY[(data.byte[2]) & 0x0F];
279
280 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) >> 4];
281 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) & 0x0F];
282
283 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) >> 4];
284 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) & 0x0F];
285
286 *pstr = 0;
287
288 return (nc_buffer);
289}
290//-----------------------------------------------------------------------------
291
292pU08 nc_float_to_str(float value,U08 decimals,U08 digits)
293{
294 S08 n;
295 U08 int_count;
296 U08 dec_count;
297 float dec_part;
298 U32 int_part;
299 pU08 pstr = nc_buffer;
300
301 if (decimals > 5)
302 {
303 decimals = 5;
304 }
305
306 if (value < 0.0)
307 {
308 *pstr++ = '-';
309 value = -value;
310 }
311
312 int_part = value;
313 dec_part = value - int_part;
314 int_count = 0;
315 dec_count = 0;
316
317 if (int_part == 0)
318 {
319 *pstr++ = '0';
320 }
321
322 while (int_part > 0)
323 {
324 nc_int_digits[int_count] = int_part % 10;
325 int_part = int_part - nc_int_digits[int_count];
326
327 if (int_part > 0)
328 {
329 int_part = int_part / 10;
330 }
331
332 int_count++; // go to the next digit
333 }
334
335 while (dec_count < decimals)
336 {
337 dec_part = dec_part * 10.0;
338 nc_dec_digits[dec_count] = dec_part;
339
340 if (nc_dec_digits[dec_count] > 0)
341 {
342 dec_part = dec_part - nc_dec_digits[dec_count];
343 }
344
345 dec_count++;
346 }
347
348 for (n = int_count - 1; n > -1; n--)
349 {
350 *pstr++ = 48 + nc_int_digits[n];
351 }
352
353 *pstr++ = '.';
354
355 for (n = 0; n < dec_count; n++)
356 {
357 *pstr++ = 48 + nc_dec_digits[n];
358 }
359
360 // Terminate string
361 *pstr = 0;
362
363 if (!digits) // If digits = 0, then return buffer start
364 {
365 return nc_buffer;
366 }
367 else // Do formatted output
368 {
369 return nc_format(nc_buffer,digits);
370 }
371}
Note: See TracBrowser for help on using the repository browser.