-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add light output, reconfigure for new breakout board
- Loading branch information
Showing
4 changed files
with
168 additions
and
19 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,46 @@ | ||
#include "light.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "Arduino.h" | ||
|
||
Light::Light(int pin) | ||
: mPin(pin) | ||
{} | ||
|
||
void Light::tick(float deltaSeconds) { | ||
uint32_t deltaMS = static_cast<uint32_t>(deltaSeconds * 1000.0f); | ||
if( mVal < mTargetVal ) { | ||
mVal += mFadeInDelta * deltaMS; | ||
} else if( mVal > mTargetVal ) { | ||
mVal -= mFadeOutDelta * deltaMS; | ||
} | ||
|
||
mVal = std::max( mVal, static_cast<int32_t>(MinVal) ); | ||
mVal = std::min( mVal, static_cast<int32_t>(MaxVal) ); | ||
set( mVal ); | ||
} | ||
|
||
void Light::target( uint8_t v ) { | ||
mTargetVal = v; | ||
if( mTargetVal > mVal ) { | ||
set( mTargetVal ); | ||
} | ||
} | ||
|
||
void Light::set( uint8_t v ) { | ||
mVal = std::max( v, MinVal ); | ||
mVal = std::min( v, MaxVal ); | ||
analogWrite( mPin, v ); | ||
mVal = v; | ||
} | ||
|
||
void Light::add( uint8_t v ) { | ||
if( MaxVal - mVal < v ) set(MaxVal); | ||
else set( static_cast<uint8_t>(mVal + v) ); | ||
} | ||
|
||
void Light::sub( uint8_t v ) { | ||
if( v > mVal ) set( MinVal ); | ||
else set( static_cast<uint8_t>(mVal - v) ); | ||
} |
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,32 @@ | ||
#ifndef LIGHT_H | ||
#define LIGHT_H | ||
|
||
#include <cstdint> | ||
|
||
class Light { | ||
private: | ||
static const uint8_t MinVal = 0x00; | ||
static const uint8_t MaxVal = 0xff; | ||
// In val per millisecond i.e 1 == fade over 255 milliseconds | ||
static const uint8_t mFadeInDelta = 4; | ||
static const uint8_t mFadeOutDelta = 4; | ||
|
||
public: | ||
Light(int pin); | ||
Light() = default; | ||
Light( const Light& ) = default; | ||
Light& operator=( const Light& ) = default; | ||
|
||
void tick(float deltaSeconds); | ||
void target( uint8_t v ); | ||
void set( uint8_t v ); | ||
void add( uint8_t v ); | ||
void sub( uint8_t v ); | ||
|
||
private: | ||
int mPin; | ||
int32_t mVal; | ||
uint8_t mTargetVal; | ||
}; | ||
|
||
#endif |