Skip to content

Commit 3e8f649

Browse files
committed
Version of dimmer that can take a custom value mapper
1 parent e6ffba5 commit 3e8f649

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

examples/OneOfEverything/OneOfEverything.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ DcsBios::SwitchMultiPos switchMulitPosExample("MSG_0", multiPosPins, 4);
2020

2121
DcsBios::Dimmer defaultDimmerExample(0x1012, 5);
2222
DcsBios::Dimmer invertedDimmerExample(0x1012, 5, 200,0);
23+
unsigned int myValueMapper(unsigned int dcsValue)
24+
{
25+
return dcsValue % 10;
26+
}
27+
DcsBios::Dimmer mappedDimmerExample(0x1012, 5, myValueMapper);
2328

2429
DcsBios::LED masterCaution(0x1012, 0x0800, 13);
2530

src/internal/Dimmer.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ namespace DcsBios {
1313
const char* msg_;
1414
int minOutput_;
1515
int maxOutput_;
16+
unsigned int (*map_function_)(unsigned int newValue);
17+
1618
public:
1719
Dimmer(unsigned int address, char pin, int minOutput=0, int maxOutput=255) : Int16Buffer(address){
1820
pin_ = pin;
1921
minOutput_ = minOutput;
2022
maxOutput_ = maxOutput;
23+
map_function_ = NULL;
24+
}
25+
Dimmer(unsigned int address, char pin, unsigned int (*map_function)(unsigned int newValue)) : Int16Buffer(address){
26+
pin_ = pin;
27+
map_function_ = map_function;
2128
}
2229
virtual void loop() {
2330
if (hasUpdatedData()) {
24-
analogWrite(pin_,
25-
map(getData(), 0, 65535, minOutput_, maxOutput_));
26-
}
31+
analogWrite(pin_, mapValue(getData()));
32+
}
33+
}
34+
unsigned int mapValue(unsigned int value) {
35+
if (map_function_) {
36+
return map_function_(value);
37+
} else {
38+
return map(value, 0, 65535, minOutput_, maxOutput_);
39+
}
2740
}
28-
2941
void SetControl( const char* msg )
3042
{
3143
msg_ = msg;

0 commit comments

Comments
 (0)