Skip to content

Commit

Permalink
fix(DataTome): 🐛 fixed the library compiling issue on avr boards
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreHiroyuki committed Aug 5, 2024
1 parent 52486d9 commit a285fbf
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 39 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ build/
.idea/
CMakeSettings.json

.pio/
# Local Build Test
main.cpp

.pio/
52 changes: 50 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
{
"files.associations": {
"*.postcss": "tailwindcss",
"*.svelte": "svelte",
"*.html": "html",
"array": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"utility": "cpp"
"utility": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
},
"conventionalCommits.scopes": [
"DataTomeAnalysis",
"DataTomeMvAvg",
"test",
"readme",
"library_metadata"
"library_metadata",
"DataTome"
]
}
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Open an issue on the repository and provide the following information:
2. What board and/or framework are you using?
3. How can the issue be reproduced? Provide a minimal code snippet that reproduces the issue if possible.

## Code Convention

- Don't include C++ standard library functions, because it does not compile for AVR boards (C std libs still works).

## Testing

The library comes with a set of automated tests. Contributions should include tests to cover the new functionality and ensure that no regressions are introduced.
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "DataTome",
"version": "1.6.3",
"version": "1.7.0",
"description": "Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome. Focus on the developer's experience and performance.",
"keywords": "sensors, input, data, processing, analysis, arduino, library, filter, moving average, smooth, standard, deviation, mean, partial, tome, datatome",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DataTome
version=1.6.3
version=1.7.0
author=Alexandre Hiroyuki Yamauchi <alex.hiroyuki@outlook.com>
maintainer=Alexandre Hiroyuki Yamauchi <alex.hiroyuki@outlook.com>
sentence=Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome.
Expand Down
9 changes: 7 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ platform = espressif32
board = esp32dev
framework = arduino

[env:arduino_avr]
framework = arduino
platform = atmelavr
board = uno

[env:desktop]
platform = native
build_flags =
-D unitTesting
build_flags = -D unitTesting
build_type = test
68 changes: 42 additions & 26 deletions src/DataTomeAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,30 @@
#define DATA_TOME_ANALYSIS_H

#include <DataTomeMvAvg.h>

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <math.h>
#include <stdlib.h>

template <typename TypeOfArray, typename TypeOfSum = TypeOfArray>
class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
public:
DataTomeAnalysis(size_t size) : DataTomeMvAvg<TypeOfArray, TypeOfSum>(size) {}

TypeOfArray min() {
TypeOfArray min = (*this)[0];
TypeOfArray minimum() {
TypeOfArray result = (*this)[0];
for (size_t i = 1; i < this->point_count(); i++) {
if ((*this)[i] < min) min = (*this)[i];
if ((*this)[i] < result) result = (*this)[i];
}

return min;
return result;
}

TypeOfArray max() {
TypeOfArray max = (*this)[0];
TypeOfArray maximum() {
TypeOfArray result = (*this)[0];
for (size_t i = 1; i < this->point_count(); i++) {
if ((*this)[i] > max) max = (*this)[i];
if ((*this)[i] > result) result = (*this)[i];
}

return max;
return result;
}

TypeOfArray median() {
Expand All @@ -45,7 +43,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
Expand All @@ -66,7 +67,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -101,7 +105,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -144,22 +151,22 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

/** Partial Methods **/

TypeOfArray partial_min(size_t partial_id) {
TypeOfArray min = (*this)[0];
TypeOfArray partial_minimum(size_t partial_id) {
TypeOfArray result = (*this)[0];
for (size_t i = 1; i < this->partial_point_count(partial_id); i++) {
if ((*this)[i] < min) min = (*this)[i];
if ((*this)[i] < result) result = (*this)[i];
}

return min;
return result;
}

TypeOfArray partial_max(size_t partial_id) {
TypeOfArray max = (*this)[0];
TypeOfArray partial_maximum(size_t partial_id) {
TypeOfArray result = (*this)[0];
for (size_t i = 1; i < this->partial_point_count(partial_id); i++) {
if ((*this)[i] > max) max = (*this)[i];
if ((*this)[i] > result) result = (*this)[i];
}

return max;
return result;
}

TypeOfArray partial_median(size_t partial_id) {
Expand All @@ -173,7 +180,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
Expand All @@ -196,7 +206,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -233,7 +246,10 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

std::sort(temp, temp + current_size);
qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down
6 changes: 4 additions & 2 deletions src/DataTomeMvAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#ifndef DATA_TOME_MV_AVG_H
#define DATA_TOME_MV_AVG_H

#include <cstdlib>
#include <cstring> // memset
#include <stdlib.h>
#include <string.h> // memset

template <typename TypeOfArray, typename TypeOfSum = TypeOfArray>
class DataTomeMvAvg {
Expand Down Expand Up @@ -159,6 +159,8 @@ class DataTomeMvAvg {
DataTomeMvAvg<TypeOfArray, TypeOfSum> &clear() {
memset(_array, 0, sizeof(TypeOfArray) * _array_size);

memset(_partial_sums, 0, sizeof(TypeOfArray) * _partial_sums_counter);

_current_index = 0;

_average_sum = 0;
Expand Down
8 changes: 4 additions & 4 deletions test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void test_minAndMax(void) {
testAnalysis.push(data[i]);
}

TEST_ASSERT_EQUAL(data[0], testAnalysis.min());
TEST_ASSERT_EQUAL(data[9], testAnalysis.max());
TEST_ASSERT_EQUAL(data[0], testAnalysis.minimum());
TEST_ASSERT_EQUAL(data[9], testAnalysis.maximum());
}

void test_median(void) {
Expand Down Expand Up @@ -87,8 +87,8 @@ void test_partialMinAndMax(void) {
testAnalysis.push(data[i]);
}

TEST_ASSERT_EQUAL(data[2], testAnalysis.partial_min(partial_id));
TEST_ASSERT_EQUAL(data[4], testAnalysis.partial_max(partial_id));
TEST_ASSERT_EQUAL(data[2], testAnalysis.partial_minimum(partial_id));
TEST_ASSERT_EQUAL(data[4], testAnalysis.partial_maximum(partial_id));
}

void test_partialMedian(void) {
Expand Down

0 comments on commit a285fbf

Please sign in to comment.