-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hexbright.h: split out set_light_level, allow for #define selection o…
…f alternatives
- Loading branch information
Showing
5 changed files
with
121 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "hexbright.h" | ||
|
||
void set_light_level_linear(unsigned long level) { | ||
// sets actual light level, altering value to be perceptually linear, based on steven's area brightness (cube root) | ||
|
||
// LOW 255 approximately equals HIGH 48/49. There is a color change. | ||
// Values < 4 do not provide any light. | ||
// I don't know about relative power draw. | ||
|
||
// look at linearity_test.ino for more detail on these algorithms. | ||
|
||
digitalWriteFast(DPIN_PWR, HIGH); | ||
if(level == 0) { | ||
// lowest possible power, but cpu still running (DPIN_PWR still high) | ||
digitalWriteFast(DPIN_DRV_MODE, LOW); | ||
analogWrite(DPIN_DRV_EN, 0); | ||
} else if(level == OFF_LEVEL) { | ||
// power off (DPIN_PWR LOW) | ||
digitalWriteFast(DPIN_PWR, LOW); | ||
digitalWriteFast(DPIN_DRV_MODE, LOW); | ||
analogWrite(DPIN_DRV_EN, 0); | ||
} else { | ||
byte value; | ||
if(level<=500) { | ||
digitalWriteFast(DPIN_DRV_MODE, LOW); | ||
value = (byte)(.000000633*(level*level*level)+.000632*(level*level)+.0285*level+3.98); | ||
} else { | ||
level -= 500; | ||
digitalWriteFast(DPIN_DRV_MODE, HIGH); | ||
value = (byte)(.00000052*(level*level*level)+.000365*(level*level)+.108*level+44.8); | ||
} | ||
analogWrite(DPIN_DRV_EN, value); | ||
} | ||
} | ||
|
||
|
||
void set_light_level_simple(unsigned long level) { | ||
// Values < 4 do not provide any light. | ||
digitalWriteFast(DPIN_PWR, HIGH); | ||
if(level == OFF_LEVEL) { | ||
// power off (DPIN_PWR LOW) | ||
digitalWriteFast(DPIN_PWR, LOW); | ||
digitalWriteFast(DPIN_DRV_MODE, LOW); | ||
analogWrite(DPIN_DRV_EN, 0); | ||
} else { | ||
byte value; | ||
if(level<=500) { | ||
digitalWriteFast(DPIN_DRV_MODE, LOW); | ||
value = level/1.96078431373; // costs about 40 bytes more than a bit-shift | ||
} else { | ||
level -= 500; | ||
value = level/1.96078431373; | ||
digitalWriteFast(DPIN_DRV_MODE, HIGH); | ||
} | ||
analogWrite(DPIN_DRV_EN, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef SET_LIGHT_LEVEL_H | ||
#define SET_LIGHT_LEVEL_H | ||
|
||
// All set_light_level versions accept values from -1 to 1000 (-1==OFF_LEVEL); | ||
// the specific version chosen is called internal to the hexbright library. | ||
// Calling directly should work, but you may have some weird bugs. | ||
|
||
// A perceptually linear light output function. | ||
// Sets actual light level, altering value to | ||
// be perceptually linear, based on steven's | ||
// area brightness (cube root) | ||
extern void set_light_level_linear(unsigned long level); | ||
|
||
// WIP | ||
// A perceptually linear light output function. | ||
// Sets actual light level, altering value to | ||
// be perceptually linear, based on steven's | ||
// area brightness (cube root) | ||
// Also tries to avoid glitches as we transition | ||
// between low/high output by carefully timing | ||
// pwm transitions | ||
extern void set_light_level_smooth(unsigned long level); | ||
|
||
// A simple light output function | ||
// 0-500: 0-256 low | ||
// 501-1000: 0-256 high | ||
extern void set_light_level_simple(unsigned long level); | ||
|
||
|
||
|
||
////// actually do selection... | ||
// set_light_level_simple requested? | ||
#ifdef SET_LIGHT_LEVEL_SIMPLE | ||
#define set_light_level set_light_level_simple | ||
#endif | ||
|
||
// default to set_light_level_linear | ||
#ifndef set_light_level | ||
#define set_light_level set_light_level_linear | ||
#endif | ||
|
||
|
||
#endif // SET_LIGHT_LINEAR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters