Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
conversions.cpp
Go to the documentation of this file.
1 /*!
2 
3 Copyright 2018(c) Analog Devices, Inc.
4 
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  - Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  - Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15  - Neither the name of Analog Devices, Inc. nor the names of its
16  contributors may be used to endorse or promote products derived
17  from this software without specific prior written permission.
18  - The use of this software may or may not infringe the patent rights
19  of one or more patent holders. This license does not release you
20  from the requirement that you obtain separate licenses from these
21  patent holders to use this software.
22  - Use of the software either in source or binary form, must be run
23  on or directly connected to an Analog Devices Inc. component.
24 
25 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
27 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
31 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 /*! @file
38  @ingroup LTPSM_InFlightUpdate
39  Library File
40 */
41 
42 #include "conversions.h"
43 
44 // Routines from the internet that are mixed in.
45 // Author Mike Jones
46 
47 
48 uint16_t httoi(char *value)
49 {
50  struct CHexMap
51  {
52  char chr;
53  int value;
54  };
55 
56  struct CHexMap HexMap[22] =
57  {
58  {'0', 0}, {'1', 1},
59  {'2', 2}, {'3', 3},
60  {'4', 4}, {'5', 5},
61  {'6', 6}, {'7', 7},
62  {'8', 8}, {'9', 9},
63  {'A', 10}, {'B', 11},
64  {'C', 12}, {'D', 13},
65  {'E', 14}, {'F', 15},
66  {'a', 10}, {'b', 11},
67  {'c', 12}, {'d', 13},
68  {'e', 14}, {'f', 15}
69  };
70  char *s = value;
71  uint16_t result = 0;
72  boolean firsttime = true;
73  boolean found;
74  int i;
75  if (*s == '0' && *(s + 1) == 'x') s += 2;
76  while (*s != '\0')
77  {
78  found = false;
79  for (i = 0; i < 22; i++)
80  {
81  if (*s == HexMap[i].chr)
82  {
83  if (!firsttime) result <<= 4;
84  //printf("add: %d\n", HexMap[i].value);
85  result |= HexMap[i].value;
86  found = true;
87  break;
88  }
89  }
90  if (!found) break;
91  s++;
92  firsttime = false;
93  }
94  //printf("finish: 0x%02x\n", result);
95  return result;
96 }
97 
98 // http://forum.arduino.cc/index.php?topic=44262.0
99 char *ftoa(char *a, double f, int precision)
100 {
101  long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
102 
103  char *ret = a;
104  long heiltal = (long)f;
105  itoa(heiltal, a, 10);
106  while (*a != '\0') a++;
107  *a++ = '.';
108  long desimal = abs((long)((f - heiltal) * p[precision]));
109  itoa(desimal, a, 10);
110  return ret;
111 }
long ret
char * ftoa(char *a, double f, int precision)
Definition: conversions.cpp:99
Copyright 2018(c) Analog Devices, Inc.
uint16_t httoi(char *value)
Definition: conversions.cpp:48
static int i
Definition: DC2430A.ino:184