Skip to content

Commit a518bd9

Browse files
committed
0.1.1 map2bits
1 parent 9dab440 commit a518bd9

File tree

18 files changed

+372
-65
lines changed

18 files changed

+372
-65
lines changed

libraries/map2bits/.arduino-ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ compile:
2525
# - esp8266
2626
# - mega2560
2727
- rpipico
28+
libraries:
29+
- "printHelpers"

libraries/map2bits/.github/workflows/arduino-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ jobs:
66
runs-on: ubuntu-latest
77
timeout-minutes: 5
88
steps:
9-
- uses: actions/checkout@v4
10-
- uses: arduino/arduino-lint-action@v1
9+
- uses: actions/checkout@v5
10+
- uses: arduino/arduino-lint-action@v2
1111
with:
1212
library-manager: update
1313
compliance: strict

libraries/map2bits/.github/workflows/arduino_test_runner.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
timeout-minutes: 20
99

1010
steps:
11-
- uses: actions/checkout@v4
11+
- uses: actions/checkout@v5
1212
- uses: ruby/setup-ruby@v1
1313
with:
1414
ruby-version: 2.6

libraries/map2bits/.github/workflows/jsoncheck.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ on:
55
paths:
66
- '**.json'
77
pull_request:
8+
paths:
9+
- '**.json'
810

911
jobs:
1012
test:
1113
runs-on: ubuntu-latest
1214
timeout-minutes: 5
1315
steps:
14-
- uses: actions/checkout@v4
16+
- uses: actions/checkout@v5
1517
- name: json-syntax-check
1618
uses: limitusus/json-syntax-check@v2
1719
with:

libraries/map2bits/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.1.1] - 2025-11-26
10+
- add map64(float) and map32() for "clean API"
11+
- add map16() for investigation, (~equal in performance).
12+
- increased MAXSIZE to 64
13+
- add example map2bits_analogRead.ino
14+
- update readme.md - test ESP32 performance
15+
- update GitHub actions
16+
- minor edits
17+
918
## [0.1.0] - 2024-01-02
1019
- initial version.

libraries/map2bits/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) 2024-2024 Rob Tillaart
3+
Copyright (c) 2024-2025 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

libraries/map2bits/README.md

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ Arduino library for mapping a float to a number of bits.
1616

1717
## Description
1818

19-
Map2bits is an **experimental** library which can be used to map a float
20-
value to a number of HIGH bits in an 32 bits integer..
19+
**Experimental**
20+
21+
Map2bits is an experimental library which can be used to map a float
22+
value to a number of HIGH bits in an 32 bits integer.
2123
The float value can be the result of a calculation or measurement from a
2224
sensor, e.g. temperature, humidity, light, distance, direction or pressure.
2325

@@ -36,7 +38,7 @@ In short many devices to drive or applications to think of.
3638
As always, feedback is welcome.
3739

3840

39-
#### Related
41+
### Related
4042

4143
Other mapping libraries
4244

@@ -54,15 +56,25 @@ Other mapping libraries
5456
#include "map2bits.h"
5557
```
5658

57-
#### Functions
59+
### Constructor
5860

5961
- **map2bits()** Constructor, defines a default mapping of (0..100 => 10)
6062
- **uint8_t init(float in_min, float in_max, uint32_t bits)** defines the mapping
6163
input range and how many bits output should be generated.
64+
65+
### Mapping
66+
67+
The output of the **map()** functions is a bit mask and can be used to drive e.g. a led bar etc.
68+
6269
- **uint32_t map(float value)** maps value to an uint32_t with the appropriate
6370
number of bits set. These are constrained by the number of bits set in **init()**.
64-
65-
The output of **map()** is a bit mask and can be used to drive e.g. a led bar etc.
71+
- **uint16_t map16(float value)** maps value to an uint16_t with the appropriate
72+
number of bits set. These are constrained by the number of bits set in **init()**.
73+
Slightly faster (~2.5%).
74+
- **uint32_t map32(float value)** equals map().
75+
- **uint64_t map64(float value)** maps value to an uint64_t with the appropriate
76+
number of bits set. These are constrained by the number of bits set in **init()**.
77+
Typical only used if one wants more than 32 bits.
6678

6779

6880
## Operation
@@ -85,15 +97,33 @@ See examples.
8597

8698
Indicative performance measured with the **map2bits_performance.ino** example.
8799
Performance depends on input chosen, platform support of float, and if
88-
values are constrained (out range) or interpolated (in range)
100+
values are constrained (out range) or interpolated (in range).
101+
Run tests on your own board to get the numbers.
102+
103+
Note: times in microseconds per call
104+
Note: UNO at 16 MHz, ESP32 at 240 MHz
105+
Note: out of range == 33% in range, 66% out range
106+
Note: map() == map32()
107+
108+
| board | function | in range | out range | notes |
109+
|:--------:|:----------:|:----------:|:-----------:|:--------|
110+
| UNO R3 | map16 | 45.22 | 22.08 |
111+
| UNO R3 | map32 | 46.28 | 22.49 |
112+
| UNO R3 | map64 | 49.00 | 23.60 | for > 32 bits
113+
| | | | |
114+
| ESP32 | map16 | 0.42 | 0.24 |
115+
| ESP32 | map32 | 0.41 | 0.23 |
116+
| ESP32 | map64 | 0.52 | 0.33 | for > 32 bits
117+
118+
119+
## Polarity
89120

90-
Note: time in microseconds per call
91-
Note: UNO at 16 MHz, ESP32 at 240 MHz
121+
The user can swap polarity by inverting with ~
92122

93-
| board | map - in range | map - out range | notes |
94-
|:-------:|:---------------:|:-----------------:|:--------|
95-
| UNO | 45.35 us | 22.20 us | 33% in range, 66% out range
96-
| ESP32 | | | to do
123+
```cpp
124+
x = ~mb.map(42); // x = 0xFFFFFFF0 =>
125+
x = ~mb.map(42) & 0xFFFF // x = 0x0000FFF0 optional mask.
126+
```
97127

98128

99129
## Future
@@ -105,29 +135,25 @@ Note: UNO at 16 MHz, ESP32 at 240 MHz
105135

106136
#### Should
107137

108-
- extend the idea to applications
109-
- add examples
110-
- optimize
138+
- add examples / applications
139+
- investigate optimize
140+
- do we need map16() => optimizer does clean up.
111141

112142
#### Could
113143

114-
- test other boards
115-
- **map2bits64(value)** uint64_t for 33 - 64 bits output
116-
- might need printHelpers
117-
- **map2bitsDouble(value)** Double version (input range + precision)
118-
- **map2bitsDouble64(value)** Double version (33 - 64 bits output)
119-
- uint32_t input version
144+
- map logarithmic (map exponent)
145+
- MSB/LSB 00000011 <> 0000110000 ? (SIZE = 6)
146+
- reverse from CRC library
147+
148+
#### Wont
149+
120150
- map2DAC?
121151
- maps a range to analogWrite()..
122152
- separate class.
123153
- polarity 0001 <> 1110 ?
124154
- ~operator
125-
- MSB/LSB 0001 <> 1000 ?
126-
- reverse from CRC library
127-
128-
129-
#### Wont
130-
155+
- map16(uint32_t) map32(uint32_t) map64(uint32_t) => accuracy float is sufficient
156+
- map2bitsDouble(value), map2bitsDouble64(value) Double version (input range + precision)
131157

132158
## Support
133159

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// FILE: map2bits_analogRead.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: map2bits demo
5+
// URL: https://github.com/RobTillaart/map2bits
6+
7+
8+
#include "map2bits.h"
9+
10+
map2bits mb;
11+
12+
13+
void setup()
14+
{
15+
Serial.begin(115200);
16+
Serial.println();
17+
Serial.println(__FILE__);
18+
Serial.print("MAP2BITS_LIB_VERSION: ");
19+
Serial.println(MAP2BITS_LIB_VERSION);
20+
Serial.println();
21+
22+
mb.init(0, 1023, 16);
23+
}
24+
25+
26+
void loop()
27+
{
28+
Serial.println(mb.map(analogRead(A0)), BIN);
29+
delay(50);
30+
}
31+
32+
33+
// -- END OF FILE --

libraries/map2bits/examples/map2bits_demo/map2bits_demo.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ map2bits mb;
1313
void setup()
1414
{
1515
Serial.begin(115200);
16+
Serial.println();
1617
Serial.println(__FILE__);
1718
Serial.print("MAP2BITS_LIB_VERSION: ");
1819
Serial.println(MAP2BITS_LIB_VERSION);

0 commit comments

Comments
 (0)