Skip to content

Commit dbf3b14

Browse files
Merge pull request #11 from talbotmcinnis/InvertableDimmer
Invertable dimmer
2 parents ea04b03 + 3e8f649 commit dbf3b14

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

examples/OneOfEverything/OneOfEverything.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ DcsBios::Switch3Pos switch3PosExample("MSG_0", 1, 2);
1818
const byte multiPosPins[4] = {1,2,3,4};
1919
DcsBios::SwitchMultiPos switchMulitPosExample("MSG_0", multiPosPins, 4);
2020

21+
DcsBios::Dimmer defaultDimmerExample(0x1012, 5);
22+
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);
28+
2129
DcsBios::LED masterCaution(0x1012, 0x0800, 13);
2230

2331
void setup() {

src/internal/Dimmer.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,33 @@ namespace DcsBios {
1111
void onDcsBiosFrameSync();
1212
char pin_;
1313
const char* msg_;
14+
int minOutput_;
15+
int maxOutput_;
16+
unsigned int (*map_function_)(unsigned int newValue);
17+
1418
public:
15-
Dimmer(unsigned int address, char pin) : Int16Buffer(address){
19+
Dimmer(unsigned int address, char pin, int minOutput=0, int maxOutput=255) : Int16Buffer(address){
20+
pin_ = pin;
21+
minOutput_ = minOutput;
22+
maxOutput_ = maxOutput;
23+
map_function_ = NULL;
24+
}
25+
Dimmer(unsigned int address, char pin, unsigned int (*map_function)(unsigned int newValue)) : Int16Buffer(address){
1626
pin_ = pin;
27+
map_function_ = map_function;
1728
}
1829
virtual void loop() {
1930
if (hasUpdatedData()) {
20-
analogWrite(pin_, getData()/255);
21-
}
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+
}
2240
}
23-
2441
void SetControl( const char* msg )
2542
{
2643
msg_ = msg;

0 commit comments

Comments
 (0)