]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/usb_util.c
Massdrop keyboards console device support for hid_listen
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / usb / usb_util.c
1 #include "samd51j18a.h"
2 #include "string.h"
3 #include "usb_util.h"
4
5 char digit(int d, int radix)
6 {
7     if (d < 10)
8     {
9         return d + '0';
10     }
11     else
12     {
13         return d - 10 + 'A';
14     }
15 }
16
17 int UTIL_ltoa_radix(int64_t value, char *dest, int radix)
18 {
19     int64_t original = value;       //save original value
20     char buf[25] = "";
21     int c = sizeof(buf)-1;
22     int last = c;
23     int d;
24     int size;
25
26     if (value < 0)                  //if it's negative, take the absolute value
27         value = -value;
28
29     do                              //write least significant digit of value that's left
30     {
31         d = (value % radix);
32         buf[--c] = digit(d, radix);
33         value /= radix;
34     } while (value);
35
36     if (original < 0)
37         buf[--c] = '-';
38
39     size = last - c + 1;            //includes null at end
40     memcpy(dest, &buf[c], last - c + 1);
41
42     return (size - 1);              //without null termination
43 }
44
45 int UTIL_ltoa(int64_t value, char *dest)
46 {
47     return UTIL_ltoa_radix(value, dest, 10);
48 }
49
50 int UTIL_itoa(int value, char *dest)
51 {
52     return UTIL_ltoa_radix((int64_t)value, dest, 10);
53 }
54
55 int UTIL_utoa(uint32_t value, char *dest)
56 {
57     return UTIL_ltoa_radix((int64_t)value, dest, 10);
58 }
59