Skip to content

Commit 19d4e5f

Browse files
authored
Arduino ci (#2)
* add arduino-ci * add unit test
1 parent c6bfcca commit 19d4e5f

File tree

13 files changed

+181
-68
lines changed

13 files changed

+181
-68
lines changed

.arduino-ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
- leonardo
6+
- due
7+
- zero
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
arduino_ci:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: Arduino-CI/action@master
13+
# Arduino-CI/[email protected]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2011-2020 Rob Tillaart
3+
Copyright (c) 2011-2021 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

MultiMap.h

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
#pragma once
22
//
3-
// FILE: multimap.h
3+
// FILE: MultiMap.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.2
5+
// VERSION: 0.1.3
66
// DATE: 2011-01-26
77
// PURPOSE: Arduino library for fast non-linear mapping or interpolation of values
88
// URL: https://github.com/RobTillaart/MultiMap
99
// URL: http://playground.arduino.cc/Main/MultiMap
1010
//
11-
// HISTORY:
12-
// 0.0.1 2011-01-26 initial version (see forum)
13-
// .....
14-
// 0.1.0 2015-03-29
15-
// 0.1.1 2020-04-09
16-
// 0.1.2 2020-06-19 fix library.json
17-
//
11+
// HISTORY:
12+
// 0.0.1 2011-01-26 initial version (see forum)
13+
// .....
14+
// 0.1.0 2015-03-29
15+
// 0.1.1 2020-04-09
16+
// 0.1.2 2020-06-19 fix library.json
17+
// 0.1.3 2021-01-02 add arduino-CI
1818
//
1919

20-
#define MULTIMAP_LIB_VERSION "0.1.2"
20+
#define MULTIMAP_LIB_VERSION "0.1.3"
2121

22-
#include <Arduino.h>
22+
#include "Arduino.h"
2323

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

4545
/*
4646
* 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
47-
*
47+
*
4848
// note: the in array should have increasing values
49+
4950
template<typename T>
5051
T multiMap(T val, T* _in, T* _out, uint8_t size)
5152
{
52-
53-
static T lastvalue = -1;
54-
static T cache = -1;
55-
56-
if (val == lastvalue) return cache;
57-
lastvalue = val;
53+
static T lastvalue = -1;
54+
static T cache = -1;
5855
59-
// take care the value is within range
60-
// val = constrain(val, _in[0], _in[size-1]);
61-
if (val <= _in[0])
62-
{
63-
cache = _out[0];
64-
}
65-
else if (val >= _in[size-1])
66-
{
67-
cache = _out[size-1];
68-
}
56+
if (val == lastvalue)
57+
{
58+
return cache;
59+
}
60+
lastvalue = val;
61+
62+
// take care the value is within range
63+
// val = constrain(val, _in[0], _in[size-1]);
64+
if (val <= _in[0])
65+
{
66+
cache = _out[0];
67+
}
68+
else if (val >= _in[size-1])
69+
{
70+
cache = _out[size-1];
71+
}
72+
else
73+
{
74+
// search right interval; index 0 _in[0] already tested
75+
uint8_t pos = 1;
76+
while(val > _in[pos]) pos++;
77+
78+
// this will handle all exact "points" in the _in array
79+
if (val == _in[pos])
80+
{
81+
cache = _out[pos];
82+
}
6983
else
70-
{
71-
// search right interval; index 0 _in[0] already tested
72-
uint8_t pos = 1;
73-
while(val > _in[pos]) pos++;
74-
75-
// this will handle all exact "points" in the _in array
76-
if (val == _in[pos])
77-
{
78-
cache = _out[pos];
79-
}
80-
else
81-
{
82-
// interpolate in the right segment for the rest
83-
cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
84-
}
85-
}
86-
return cache;
84+
{
85+
// interpolate in the right segment for the rest
86+
cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
87+
}
88+
}
89+
return cache;
8790
}
8891
*/
8992

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
[![Arduino CI](https://github.com/RobTillaart/MultiMap/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MultiMap/blob/master/LICENSE)
4+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MultiMap.svg?maxAge=3600)](https://github.com/RobTillaart/MultiMap/releases)
5+
16
# MultiMap
27

38
Arduino library for fast non-linear mapping or interpolation of values
@@ -7,27 +12,28 @@ Arduino library for fast non-linear mapping or interpolation of values
712
In Arduino applications often the value of a sensor is mapped upon a more
813
usable value. E.g. the value of analogRead() is mapped onto 0 .. 5.0 Volt.
914
This is done by the map function which does a linear interpolation. This means
10-
```
15+
16+
```cpp
1117
output = C1 + input * C2
1218
```
19+
1320
As C1 and C2 are to be calculated Arduino has the **map()** that calculates the
1421
two variables runtime from two given mappings.
15-
```
22+
23+
```cpp
1624
output = map(input, I1, I2, O1, O2):
1725
```
1826

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

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

26-
The **input\[\]** array must have increasing values,
34+
- The **input\[\]** array must have increasing values,
2735
there is no such restriction for the **output\[\]** array.
28-
29-
**Multimap()** automatically constrains the output to the first and last
30-
value in the **output\[\]** array.
36+
- **Multimap()** automatically constrains the output to the first and last value in the **output\[\]** array.
3137

3238
## Operation
3339

examples/multimap_NTC/multimap_NTC.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// FILE: multimap_NTC.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// PURPOSE: demo
66
// DATE: 2020-04-09
77
// (c) : MIT
88
//
99

10-
#include "multimap.h"
10+
#include "MultiMap.h"
1111

1212
uint32_t start;
1313
uint32_t stop;

examples/multimap_NTC_int_FAIL/multimap_NTC_int_FAIL.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: multimap_NTC_int_FAIL.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// PURPOSE: demo of faulty optimizing
66
// DATE: 2020-04-09
77
// (c) : MIT
@@ -16,7 +16,7 @@
1616
//
1717

1818

19-
#include "multimap.h"
19+
#include "MultiMap.h"
2020

2121
uint32_t start;
2222
uint32_t stop;

examples/multimap_distance/multimap_distance.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
//
33
// FILE: multimap_distance.ino
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// PURPOSE: demo
77
// DATE: 2020-04-09
88
//
99

10-
#include "multimap.h"
10+
#include "MultiMap.h"
1111

1212
void setup()
1313
{

examples/multimap_functions/multimap_functions.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//
22
// FILE: multimap_functions.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// PURPOSE: demo (use serial plotter)...
66
// DATE: 2020-04-09
77
// (c) : MIT
88
//
99

1010

11-
#include "multimap.h"
11+
#include "MultiMap.h"
1212

1313
void setup()
1414
{

examples/multimap_timing/multimap_timing.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//
22
// FILE: multimap_timing.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// PURPOSE: demo
66
// DATE: 2020-04-09
77
// (c) : MIT
88
//
99

1010

11-
#include "multimap.h"
11+
#include "MultiMap.h"
1212

1313
int in[] = { 11, 22, 33};
1414
int out[] = {111, 222, 555};

0 commit comments

Comments
 (0)