Skip to content

Commit

Permalink
Implement now missing itoa (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbek committed Jan 6, 2023
1 parent 7d592af commit e7d19b5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tools.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Tools for USB HID Autofire
//

void strrev(char* arr, int start, int end) {
char temp;

if (start >= end)
return;

temp = *(arr + start);
*(arr + start) = *(arr + end);
*(arr + end) = temp;

start++;
end--;
strrev(arr, start, end);
}

char *itoa(int number, char *arr, int base)
{
int i = 0, r, negative = 0;

if (number == 0)
{
arr[i] = '0';
arr[i + 1] = '\0';
return arr;
}

if (number < 0 && base == 10)
{
number *= -1;
negative = 1;
}

while (number != 0)
{
r = number % base;
arr[i] = (r > 9) ? (r - 10) + 'a' : r + '0';
i++;
number /= base;
}

if (negative)
{
arr[i] = '-';
i++;
}

strrev(arr, 0, i - 1);

arr[i] = '\0';

return arr;
}
7 changes: 7 additions & 0 deletions tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef FLIPPERZERO_FIRMWARE_TOOLS_H
#define FLIPPERZERO_FIRMWARE_TOOLS_H

void strrev(char *arr, int start, int end);
char *itoa(int number, char *arr, int base);

#endif //FLIPPERZERO_FIRMWARE_TOOLS_H
3 changes: 3 additions & 0 deletions usb_hid_autofire.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <gui/gui.h>
#include <input/input.h>
#include "version.h"
#include "tools.h"

// Uncomment to be able to make a screenshot
//#define USB_HID_AUTOFIRE_SCREENSHOT
Expand All @@ -25,7 +26,9 @@ uint32_t autofire_delay = 10;
static void usb_hid_autofire_render_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx);
char autofire_delay_str[12];
//std::string pi = "pi is " + std::to_string(3.1415926);
itoa(autofire_delay, autofire_delay_str, 10);
//sprintf(autofire_delay_str, "%lu", autofire_delay);

canvas_clear(canvas);

Expand Down

0 comments on commit e7d19b5

Please sign in to comment.