0.1.0 TOPMAX

This commit is contained in:
Rob Tillaart 2023-05-18 21:21:08 +02:00
parent 2ee0388db8
commit 5ab82c5833
15 changed files with 546 additions and 0 deletions

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
- esp8266
# - mega2560
- rpipico

4
libraries/TOPMAX/.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,17 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,11 @@
# Change Log TOPMAX
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.0] - 2023-05-18
- initial version

21
libraries/TOPMAX/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023-2023 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,68 @@
[![Arduino CI](https://github.com/RobTillaart/TOPMAX/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/TOPMAX/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/TOPMAX/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/TOPMAX/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/TOPMAX/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/TOPMAX/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/TOPMAX.svg?maxAge=3600)](https://github.com/RobTillaart/TOPMAX/releases)
# TOPMAX
Arduino library to track the top N maxima.
## Description
This experimental library tracks the top N maxima of a series of values.
It can be used e.g. to track the top 5 peak temperatures over a given
period of time.
#### Links
- https://github.com/RobTillaart/TOPMAX
- https://github.com/RobTillaart/TOPMIN
- https://github.com/RobTillaart/runningAverage
- https://github.com/RobTillaart/MINMAX
## Interface
```cpp
#include "TOPMAX.h"
```
- **TOPMAX(uint8_t size = 5)** Constructor, defines the number of elements it can hold.
Default number of elements is 5. If size < 3 it becomes 3.
- **uint8_t count()** returns the number of elements in the internal array.
- **uint8_t size()** returns the maximum number of elements in the internal array.
- **void reset()** reset the internal counter to 0, clearing the system.
- **bool add(float value)** add a value to the TOPMAX object to check it needs to be in the top N of maxima.
- **float get(uint8_t index)** get an element of the internal array.
index == count().
- **bool fill(float value)** convenience function to fill the internal array
with a single value e.g. 0.
# Future
#### Must
- keep functional in sync with TOPMIN
- improve documentation.
#### Should
- add unit tests.
- add more examples.
#### Could
- create template class
- create derived class that holds an index or timestamp
- uint32_t extra per element.
#### Wont

103
libraries/TOPMAX/TOPMAX.cpp Normal file
View File

@ -0,0 +1,103 @@
//
// FILE: TOPMAX.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2023-05-18
// PURPOSE: Arduino library to track top n maxima.
// URL: https://github.com/RobTillaart/TOPMAX
#include "TOPMAX.h"
TOPMAX::TOPMAX(uint8_t size)
{
_size = size;
if (_size < 3) _size = 3;
_count = 0;
_arr = (float *) malloc(_size * sizeof(float));
}
TOPMAX::~TOPMAX()
{
if (_arr) free(_arr);
}
uint8_t TOPMAX::count()
{
return _count;
}
uint8_t TOPMAX::size()
{
return _size;
}
void TOPMAX::reset()
{
_count = 0;
}
bool TOPMAX::add(float value)
{
// initial
if (_count == 0)
{
_arr[_count] = value;
_count++;
return true;
}
// not all elements filled.
if (_count < _size)
{
int i = _count - 1;
while ((i >= 0) && (value < _arr[i]))
{
_arr[i + 1] = _arr[i];
i--;
}
_arr[i + 1] = value;
_count++;
return true;
}
// too small (or equal)
if (value <= _arr[0]) return false;
// insert
int i = 1;
while ((i < _size) && (value > _arr[i]))
{
_arr[i - 1] = _arr[i];
i++;
}
_arr[i-1] = value;
return true;
}
float TOPMAX::get(uint8_t index)
{
if (index > _count) return NAN;
return _arr[index];
}
void TOPMAX::fill(float value)
{
for (int i = 0; i < _size; i++)
{
_arr[i] = value;
}
_count = _size;
}
// -- END OF FILE --

39
libraries/TOPMAX/TOPMAX.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
//
// FILE: TOPMAX.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2023-05-18
// PURPOSE: Arduino library to track top n maxima.
// URL: https://github.com/RobTillaart/TOPMAX
#define TOPMAX_LIB_VERSION (F("0.1.0"))
#include "Arduino.h"
class TOPMAX
{
public:
TOPMAX(uint8_t size = 5);
~TOPMAX();
uint8_t count();
uint8_t size();
void reset();
bool add(float value);
float get(uint8_t index);
void fill(float value);
private:
uint8_t _size;
uint8_t _count;
float * _arr;
};
// -- END OF FILE --

View File

@ -0,0 +1,50 @@
//
// FILE: TOPMAX_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: TOPMAX demo
// URL: https://github.com/RobTillaart/TOPMAX
#include "TOPMAX.h"
TOPMAX tm(10);
uint32_t cnt = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TOPMAX_LIB_VERSION: ");
Serial.println(TOPMAX_LIB_VERSION);
Serial.println();
tm.fill(0);
tm.reset();
}
void loop()
{
int x = random(10000);
Serial.print(cnt++);
Serial.print("\t");
Serial.print(x);
Serial.print("\t");
for (int i = 0; i < tm.count(); i++)
{
Serial.print(tm.get(i));
Serial.print("\t");
}
Serial.println();
tm.add(x);
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,20 @@
# Syntax Colouring Map For TOPMAX
# Data types (KEYWORD1)
TOPMAX KEYWORD1
# Methods and Functions (KEYWORD2)
count KEYWORD2
size KEYWORD2
reset KEYWORD2
add KEYWORD2
get KEYWORD2
fill KEYWORD2
# Constants (LITERAL1)
TOPMAX_LIB_VERSION LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "TOPMAX",
"keywords": "maxima",
"description": "Arduino library to track top n maxima.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/TOPMAX"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": "TOPMAX.h"
}

View File

@ -0,0 +1,11 @@
name=TOPMAX
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library to track top n maxima.
paragraph=
category=Data Processing
url=https://github.com/RobTillaart/TOPMAX
architectures=*
includes=TOPMAX.h
depends=

View File

@ -0,0 +1,121 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2023-05-18
// PURPOSE: unit tests for the TOPMAX library
// https://github.com/RobTillaart/TOPMAX
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// ----------------------------
// assertEqual(expected, actual); // a == b
// assertNotEqual(unwanted, actual); // a != b
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
// assertLess(upperBound, actual); // a < b
// assertMore(lowerBound, actual); // a > b
// assertLessOrEqual(upperBound, actual); // a <= b
// assertMoreOrEqual(lowerBound, actual); // a >= b
// assertTrue(actual);
// assertFalse(actual);
// assertNull(actual);
// // special cases for floats
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
// assertInfinity(actual); // isinf(a)
// assertNotInfinity(actual); // !isinf(a)
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "TOPMAX.h"
unittest_setup()
{
fprintf(stderr, "TOPMAX_LIB_VERSION: %s\n", (char *) TOPMAX_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constructor)
{
TOPMAX tm(5);
assertEqual(5, tm.size());
assertEqual(0, tm.count());
}
unittest(test_add)
{
TOPMAX tm(5);
assertEqual(5, tm.size());
for (int i = 0; i < 5; i++)
{
assertEqual(i, tm.count());
tm.add(i);
}
for (int i = 0; i < 5; i++)
{
assertEqual(5, tm.count());
tm.add(i);
}
}
unittest(test_get)
{
TOPMAX tm(5);
assertEqual(5, tm.size());
for (int i = 0; i < 10; i++)
{
tm.add(i);
int idx = tm.count();
assertEqualFloat(i, tm.get(idx - 1), 0.001);
}
for (int i = 0; i < tm.count(); i++)
{
fprintf(stderr, "%f\t", tm.get(i));
}
fprintf(stderr, "\n");
}
unittest(test_reset)
{
TOPMAX tm(5);
assertEqual(5, tm.size());
assertEqual(0, tm.count());
for (int i = 0; i < 5; i++)
{
tm.add(i);
}
assertEqual(5, tm.count());
tm.reset();
assertEqual(0, tm.count());
}
unittest_main()
// -- END OF FILE --