Skip to content

Commit

Permalink
Arduino ci (#2)
Browse files Browse the repository at this point in the history
* add arduino-ci
* add unit test
  • Loading branch information
RobTillaart authored Jan 2, 2021
1 parent c6bfcca commit 19d4e5f
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 68 deletions.
7 changes: 7 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
13 changes: 13 additions & 0 deletions .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: Arduino CI

on: [push, pull_request]

jobs:
arduino_ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/[email protected]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2011-2020 Rob Tillaart
Copyright (c) 2011-2021 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
Expand Down
93 changes: 48 additions & 45 deletions MultiMap.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#pragma once
//
// FILE: multimap.h
// FILE: MultiMap.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// DATE: 2011-01-26
// PURPOSE: Arduino library for fast non-linear mapping or interpolation of values
// URL: https://github.com/RobTillaart/MultiMap
// URL: http://playground.arduino.cc/Main/MultiMap
//
// HISTORY:
// 0.0.1 2011-01-26 initial version (see forum)
// .....
// 0.1.0 2015-03-29
// 0.1.1 2020-04-09
// 0.1.2 2020-06-19 fix library.json
//
// HISTORY:
// 0.0.1 2011-01-26 initial version (see forum)
// .....
// 0.1.0 2015-03-29
// 0.1.1 2020-04-09
// 0.1.2 2020-06-19 fix library.json
// 0.1.3 2021-01-02 add arduino-CI
//

#define MULTIMAP_LIB_VERSION "0.1.2"
#define MULTIMAP_LIB_VERSION "0.1.3"

#include <Arduino.h>
#include "Arduino.h"

// note: the in array should have increasing values
template<typename T>
Expand All @@ -44,46 +44,49 @@ T multiMap(T val, T* _in, T* _out, uint8_t size)

/*
* speed optimized version if inputs do not change often e.g. 2 2 2 2 2 3 3 3 3 5 5 5 5 5 5 8 8 8 8 5 5 5 5 5
*
*
// note: the in array should have increasing values
template<typename T>
T multiMap(T val, T* _in, T* _out, uint8_t size)
{
static T lastvalue = -1;
static T cache = -1;
if (val == lastvalue) return cache;
lastvalue = val;
static T lastvalue = -1;
static T cache = -1;
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
if (val <= _in[0])
{
cache = _out[0];
}
else if (val >= _in[size-1])
{
cache = _out[size-1];
}
if (val == lastvalue)
{
return cache;
}
lastvalue = val;
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
if (val <= _in[0])
{
cache = _out[0];
}
else if (val >= _in[size-1])
{
cache = _out[size-1];
}
else
{
// search right interval; index 0 _in[0] already tested
uint8_t pos = 1;
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos])
{
cache = _out[pos];
}
else
{
// search right interval; index 0 _in[0] already tested
uint8_t pos = 1;
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos])
{
cache = _out[pos];
}
else
{
// interpolate in the right segment for the rest
cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}
}
return cache;
{
// interpolate in the right segment for the rest
cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}
}
return cache;
}
*/

Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

[![Arduino CI](https://github.com/RobTillaart/MultiMap/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MultiMap/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MultiMap.svg?maxAge=3600)](https://github.com/RobTillaart/MultiMap/releases)

# MultiMap

Arduino library for fast non-linear mapping or interpolation of values
Expand All @@ -7,27 +12,28 @@ Arduino library for fast non-linear mapping or interpolation of values
In Arduino applications often the value of a sensor is mapped upon a more
usable value. E.g. the value of analogRead() is mapped onto 0 .. 5.0 Volt.
This is done by the map function which does a linear interpolation. This means
```

```cpp
output = C1 + input * C2
```

As C1 and C2 are to be calculated Arduino has the **map()** that calculates the
two variables runtime from two given mappings.
```

```cpp
output = map(input, I1, I2, O1, O2):
```

In many cases when there is no linear mapping possible, as the 'points' are not on a single line,
one needs non-linear math to calculate the output. **Multimap()** just does that.
In many cases when there is no linear mapping possible, as the 'points' are not on a single line.
One needs non-linear math to calculate the output, **Multimap()** just does that.

**Multimap()** needs two equal sized arrays of 'points', **input\[\]** and **output\[\]**, it looks up the
**out = Multimap(value, input, output, size)** needs two equal sized arrays of reference 'points', **input\[\]** and **output\[\]**, it looks up the
input value in the input\[\] array and if needed it linear interpolates between two
points of the output\[\] array.

The **input\[\]** array must have increasing values,
- The **input\[\]** array must have increasing values,
there is no such restriction for the **output\[\]** array.

**Multimap()** automatically constrains the output to the first and last
value in the **output\[\]** array.
- **Multimap()** automatically constrains the output to the first and last value in the **output\[\]** array.

## Operation

Expand Down
4 changes: 2 additions & 2 deletions examples/multimap_NTC/multimap_NTC.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// FILE: multimap_NTC.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo
// DATE: 2020-04-09
// (c) : MIT
//

#include "multimap.h"
#include "MultiMap.h"

uint32_t start;
uint32_t stop;
Expand Down
4 changes: 2 additions & 2 deletions examples/multimap_NTC_int_FAIL/multimap_NTC_int_FAIL.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: multimap_NTC_int_FAIL.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo of faulty optimizing
// DATE: 2020-04-09
// (c) : MIT
Expand All @@ -16,7 +16,7 @@
//


#include "multimap.h"
#include "MultiMap.h"

uint32_t start;
uint32_t stop;
Expand Down
4 changes: 2 additions & 2 deletions examples/multimap_distance/multimap_distance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
//
// FILE: multimap_distance.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo
// DATE: 2020-04-09
//

#include "multimap.h"
#include "MultiMap.h"

void setup()
{
Expand Down
4 changes: 2 additions & 2 deletions examples/multimap_functions/multimap_functions.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// FILE: multimap_functions.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo (use serial plotter)...
// DATE: 2020-04-09
// (c) : MIT
//


#include "multimap.h"
#include "MultiMap.h"

void setup()
{
Expand Down
4 changes: 2 additions & 2 deletions examples/multimap_timing/multimap_timing.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// FILE: multimap_timing.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo
// DATE: 2020-04-09
// (c) : MIT
//


#include "multimap.h"
#include "MultiMap.h"

int in[] = { 11, 22, 33};
int out[] = {111, 222, 555};
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MultiMap.git"
},
"version":"0.1.2",
"version":"0.1.3",
"frameworks": "arduino",
"platforms": "*"
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name=MultiMap
version=0.1.2
version=0.1.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Library for fast non-linear interpolation by means of two arrays.
paragraph=
category=Data Processing
url=https://github.com/RobTillaart/Arduino/MultiMap
architectures=*
includes=multimap.h
includes=MultiMap.h
depends=
84 changes: 84 additions & 0 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2021-01-02
// PURPOSE: unit tests for the multiMap
// https://github.com/RobTillaart/MultiMap
// 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>

#define assertEqualFloat(arg1, arg2, arg3) assertOp("assertEqualFloat", "expected", fabs(arg1 - arg2), compareLessOrEqual, "<=", "actual", arg3)
// #define assertEqualINF(arg) assertOp("assertEqualINF", "expected", INFINITY, compareEqual, "==", "actual", arg)
// #define assertEqualNAN(arg) assertOp("assertEqualNAN", "expected", true, compareEqual, "==", "actual", isnan(arg))


#include "MultiMap.h"


unittest_setup()
{
}

unittest_teardown()
{
}

/*
unittest(test_new_operator)
{
assertEqualINF(exp(800));
assertEqualINF(0.0/0.0);
assertEqualINF(42);
assertEqualNAN(INFINITY - INFINITY);
assertEqualNAN(0.0/0.0);
assertEqualNAN(42);
}
*/

unittest(test_all)
{
fprintf(stderr, "VERSION: %s\n", MULTIMAP_LIB_VERSION);

// based on the distance example
// out[] holds the distances in cm
float out[] = {150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20};
// in[] holds the measured analogRead() values for that distance
float in[] = { 90, 97, 105, 113, 124, 134, 147, 164, 185, 218, 255, 317, 408, 506};

assertEqualFloat(150.000, multiMap<float>(80, in, out, 14), 0.001);
assertEqualFloat(136.250, multiMap<float>(100, in, out, 14), 0.001);
assertEqualFloat(65.4545, multiMap<float>(200, in, out, 14), 0.001);
assertEqualFloat(42.7419, multiMap<float>(300, in, out, 14), 0.001);
assertEqualFloat(30.8791, multiMap<float>(400, in, out, 14), 0.001);
assertEqualFloat(20.6122, multiMap<float>(500, in, out, 14), 0.001);
assertEqualFloat(20.0000, multiMap<float>(600, in, out, 14), 0.001);
}

unittest_main()

// --------

0 comments on commit 19d4e5f

Please sign in to comment.