Skip to content

Commit

Permalink
#133 Separated utills definition from declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
forntoh committed Oct 9, 2023
1 parent 65d2074 commit 5d4f73c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 47 deletions.
50 changes: 50 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "utils.h"

#include "Constants.h"

void substring(char* str, uint8_t start, uint8_t size, char* substr) {
strncpy(substr, str + start, size);
substr[size] = '\0';
}

void concat(const char* first, char second, const char* third, char* result) {
size_t len1 = strlen(first);
size_t len3 = strlen(third);

memcpy(result, first, len1);
result[len1] = second;
memcpy(result + len1 + 1, third, len3);
result[len1 + 1 + len3] = '\0';
}

void concat(const char* first, char second, char* result) {
size_t len1 = strlen(first);
memcpy(result, first, len1);
result[len1] = second;
result[len1 + 1] = '\0';
}

void concat(const char* first, const char* second, char* result) {
strcpy(result, first);
strcat(result, second);
}

void remove(char* str, uint8_t index, uint8_t count) {
uint8_t len = strlen(str);
if (index + count > len) {
count = len - index;
}
memmove(str + index, str + index + count, len - count - index + 1);
}

long mapProgress(uint16_t progress, long minValue, long maxValue) {
// Map the progress value to a new range (minValue to maxValue)
return map(progress, MIN_PROGRESS, MAX_PROGRESS, minValue, maxValue);
}

float mapProgress(uint16_t progress, float minValue, float maxValue) {
// Normalize the progress value and map it to the specified floating-point
// range
return static_cast<float>(progress) / MAX_PROGRESS * (maxValue - minValue) +
minValue;
}
65 changes: 18 additions & 47 deletions src/utils.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,20 @@
#pragma once
#ifndef MenuUtils_H
#define MenuUtils_H

#include <Arduino.h>

void substring(char* str, uint8_t start, uint8_t size, char* substr) {
strncpy(substr, str + start, size);
substr[size] = '\0';
}

void concat(const char* first, char second, const char* third, char* result) {
size_t len1 = strlen(first);
size_t len3 = strlen(third);

memcpy(result, first, len1);
result[len1] = second;
memcpy(result + len1 + 1, third, len3);
result[len1 + 1 + len3] = '\0';
}

void concat(const char* first, char second, char* result) {
size_t len1 = strlen(first);
memcpy(result, first, len1);
result[len1] = second;
result[len1 + 1] = '\0';
}

void concat(const char* first, const char* second, char* result) {
strcpy(result, first);
strcat(result, second);
}

void remove(char* str, uint8_t index, uint8_t count) {
uint8_t len = strlen(str);
if (index + count > len) {
count = len - index;
}
memmove(str + index, str + index + count, len - count - index + 1);
}

long mapProgress(uint16_t progress, long minValue, long maxValue) {
// Map the progress value to a new range (minValue to maxValue)
return map(progress, MIN_PROGRESS, MAX_PROGRESS, minValue, maxValue);
}

float mapProgress(uint16_t progress, float minValue, float maxValue) {
// Normalize the progress value and map it to the specified floating-point
// range
return static_cast<float>(progress) / MAX_PROGRESS * (maxValue - minValue) +
minValue;
}
void substring(char* str, uint8_t start, uint8_t size, char* substr);

void concat(const char* first, char second, const char* third, char* result);

void concat(const char* first, char second, char* result);

void concat(const char* first, const char* second, char* result);

void remove(char* str, uint8_t index, uint8_t count);

long mapProgress(uint16_t progress, long minValue, long maxValue);

float mapProgress(uint16_t progress, float minValue, float maxValue);

#endif

0 comments on commit 5d4f73c

Please sign in to comment.