Krolyxon
Commit 3120783

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8U8g2srcclibu8x8_u8toa.c

109 lines · 2.6 KB · C

1/*
2
3 u8x8_u8toa.c
4
5 Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
6
7 Copyright (c) 2016, olikraus@gmail.com
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice, this list
14 of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright notice, this
17 list of conditions and the following disclaimer in the documentation and/or other
18 materials provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#include "u8x8.h"
38
39static const unsigned char u8x8_u8toa_tab[3] = { 100, 10, 1 } ;
40const char *u8x8_u8toap(char * dest, uint8_t v)
41{
42 uint8_t pos;
43 uint8_t d;
44 uint8_t c;
45 for( pos = 0; pos < 3; pos++ )
46 {
47 d = '0';
48 c = *(u8x8_u8toa_tab+pos);
49 while( v >= c )
50 {
51 v -= c;
52 d++;
53 }
54 dest[pos] = d;
55 }
56 dest[3] = '\0';
57 return dest;
58}
59
60/* v = value, d = number of digits */
61const char *u8x8_u8toa(uint8_t v, uint8_t d)
62{
63 static char buf[4];
64 d = 3-d;
65 return u8x8_u8toap(buf, v) + d;
66}
67
68const char *u8x8_s8toa(int8_t v, uint8_t d)
69{
70 static char buf[5];
71 uint16_t u = v;
72 buf[0] = '+';
73 if ( v < 0 )
74 {
75 buf[0] = '-';
76 u = -v;
77 }
78 u8x8_u8toap(buf+1, (uint8_t)u);
79 if ( d == 1 )
80 {
81 buf[1] = buf[3];
82 buf[2] = '\0';
83 }
84 else if ( d == 2 )
85 {
86 buf[1] = buf[2];
87 buf[2] = buf[3];
88 buf[3] = '\0';
89 }
90 return buf;
91}
92
93const char *u8x8_u8tox(uint8_t v, uint8_t d)
94{
95 const char hexdigit[]="0123456789ABCDEF";
96 static char buf[3];
97 if ( d == 1 )
98 {
99 buf[0] = hexdigit[v & 0xF];
100 buf[1] = '\0';
101 }
102 else if ( d == 2 )
103 {
104 buf[0] = hexdigit[v >> 4 & 0xF];
105 buf[1] = hexdigit[v & 0xF];
106 buf[2] = '\0';
107 }
108 return buf;
109}