Changed PwmPin to LedPin in all Tutorial Examples

Greatly simplifies use interface.  No need to specify or save channels.  And no need to even save pin number since that can be found using LedPin->getPin() method whenever needed.
This commit is contained in:
Gregg 2021-03-21 09:54:57 -05:00
parent d49bca9bbe
commit 451a2885b1
16 changed files with 97 additions and 133 deletions

View File

@ -54,7 +54,7 @@ void setup() {
// and the ESP32 chip has built-in PWM functionality specifically for this purpose. There are numerous libraries
// you can download that mimics or reproduces analogWrite() in some form or another. HomeSpan conveniently comes with
// it own version of a wrapper around the ESP32 PWM classes that make it very easy to define PWM "channel," attach a pin,
// and set the PWM level (or duty cycle) from 0-100%. These functions are encapsualted in the PwmPin class, as defined in
// and set the PWM level (or duty cycle) from 0-100%. These functions are encapsualted in the LedPin class, as defined in
// extras/PwmPin.h. We will include this file in our updated DEV_LED.h for use with DEV_DimmableLED.
Serial.begin(115200);
@ -89,7 +89,7 @@ void setup() {
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
new DEV_DimmableLED(0,17); // NEW! create a dimmable LED attached to pin 17 using PWM channel 0. See new code at end of DEV_LED.h
new DEV_DimmableLED(17); // NEW! create a dimmable (PWM-driven) LED attached to pin 17. See new code at end of DEV_LED.h
} // end of setup()

View File

@ -33,34 +33,33 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // NEW! Create reference to PWM Pin instantiated below
int channel; // NEW! Store the PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // NEW! Create reference to LED Pin instantiated below
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // NEW! Create a reference to the Brightness Characteristic instantiated below
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){ // constructor() method
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // NEW! Instantiate the Brightness Characteristic with an initial value of 50% (same as we did in Example 4)
level->setRange(5,100,1); // NEW! This sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1% (different from Example 4 values)
this->channel=channel; // NEW! Save the channel number (from 0-15)
this->pwmPin=new PwmPin(channel, ledPin); // NEW! Configures the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called.
this->ledPin=new LedPin(pin); // NEW! Configures a PWM LED for output to the specified pin. Note pinMode() does NOT need to be called in advance
} // end constructor
boolean update(){ // update() method
// Here we set the duty cycle (brightness) of the LED by callng pwmPin with the appropriate channel.
// The second argument should be a number from 0-100 (representing %brightness). HomeKit sets the on/off
// status of the LED ("power") separately from the brightness of the LED ("level"). This means HomeKit can
// request the LED be turned off, but retain the brightness level so that it does not need to be resent if
// the LED is turned back on. Multiplying the newValue of the power Characteristic (as a boolean) with the
// newValue of the Brightness Characteristic (as an int) is a short-hand way of creating the logic to
// set the PWM level when the LED is off (always zero) or on (whatever the brightness level is).
// Here we set the brightness of the LED by calling ledPin->set(brightness), where brightness=0-100.
// Note HomeKit sets the on/off status of a LightBulb separately from its brightness, which means HomeKit
// can request a LightBulb be turned off, but still retains the brightness level so that it does not need
// to be resent once the LightBulb is turned back on.
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
// Multiplying the newValue of the On Characteristic ("power", which is a boolean) with the newValue of the
// Brightness Characteristic ("level", which is an integer) is a short-hand way of creating the logic to
// set the LED level to zero when the LightBulb is off, or to the current brightness level when it is on.
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true

View File

@ -102,7 +102,7 @@ void setup() {
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
new DEV_DimmableLED(0,17); // NEW! create a dimmable LED attached to pin 17 using PWM channel 0. See new code at end of DEV_LED.h
new DEV_DimmableLED(17); // create a dimmable (PWM-driven) LED attached to pin 17
} // end of setup()

View File

@ -31,26 +31,24 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // reference to PWM Pin
int channel; // PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){ // constructor() method
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
level->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->channel=channel; // save the channel number (from 0-15)
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called.
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
} // end constructor
boolean update(){ // update() method
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true

View File

@ -98,7 +98,7 @@ void setup() {
// new Service::HAPProtocolInformation(); - DELETED - NO LONGER NEEDED
// new Characteristic::Version("1.1.0"); - DELETED - NO LONGER NEEDED
new DEV_DimmableLED(0,17); // create a dimmable LED attached to pin 17 using PWM channel 0
new DEV_DimmableLED(17); // create a dimmable (PWM-driven) LED attached to pin 17
} // end of setup()

View File

@ -31,26 +31,24 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // reference to PWM Pin
int channel; // PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){ // constructor() method
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
level->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->channel=channel; // save the channel number (from 0-15)
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called.
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
} // end constructor
boolean update(){ // update() method
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true

View File

@ -85,11 +85,11 @@ void setup() {
new DEV_Identify("On/Off LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_LED(16);
// Defines a Dimmable LED Accessory attached to pin 17 using PWM channel 0
// Defines a Dimmable (PWM-driven) LED Accessory attached to pin 17
new SpanAccessory();
new DEV_Identify("Dimmable LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_DimmableLED(0,17);
new DEV_DimmableLED(17);
} // end of setup()

View File

@ -53,31 +53,26 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // reference to PWM Pin
int ledPin; // pin number defined for this LED <- NEW!!
int channel; // PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){ // constructor() method
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
level->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->channel=channel; // save the channel number (from 0-15)
this->ledPin=ledPin; // LED pin number <- NEW!!
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called.
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
// Here we output log messages when the constructor is initially called.
// We use Serial.print() since to ensure the message is always output
// regardless of the VERBOSITY setting.
// regardless of the VERBOSITY setting. Note that ledPin has a method getPin()
// that retrieves the pin number so you don't need to store it separately.
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
Serial.print(ledPin);
Serial.print(" Channel=");
Serial.print(channel);
Serial.print(ledPin->getPin());
Serial.print("\n");
} // end constructor
@ -89,13 +84,8 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
// is not functioning as expected. Since it's just for debugging,
// we use LOG1() instead of Serial.print().
// Note that in the prior example we did not save the ledPin number for
// DimmableLED since it was only needed by the constructor for initializing
// PwmPin(). For this example we add ledPin as a saved variable (see the two
// lines marketed NEW!! above) for the sole purpose of this log message.
LOG1("Updating Dimmable LED on pin=");
LOG1(ledPin);
LOG1(ledPin->getPin());
LOG1(": Current Power=");
LOG1(power->getVal()?"true":"false");
LOG1(" Current Brightness=");
@ -121,7 +111,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
LOG1("\n");
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true

View File

@ -59,15 +59,15 @@ void setup() {
new SpanAccessory();
new DEV_Identify("On/Off LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_LED(16); // Create an On/Off LED attached to pin 16
new DEV_LED(16); // Create an On/Off LED attached to pin 16
new SpanAccessory();
new DEV_Identify("Dimmable LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_DimmableLED(0,17); // Create a Dimmable LED using PWM channel 0, attached to pin 17
new DEV_DimmableLED(17); // Create a Dimmable (PWM-driven) LED using attached to pin 17
new SpanAccessory();
new DEV_Identify("RGB LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_RgbLED(1,2,3,32,22,23); // Create an RGB LED using PWM channels 1,2,3, attached to pins 32,22,23 (for R, G, and B LED anodes)
new DEV_RgbLED(32,22,23); // Create an RGB LED attached to pins 32,22,23 (for R, G, and B LED anodes)
} // end of setup()

View File

@ -45,27 +45,21 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // reference to PWM Pin
int ledPin; // pin number defined for this LED
int channel; // PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){ // constructor() method
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
level->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->channel=channel; // save the channel number (from 0-15)
this->ledPin=ledPin; // save LED pin number
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
Serial.print(ledPin);
Serial.print(" Channel=");
Serial.print(channel);
Serial.print(ledPin->getPin());
Serial.print("\n");
} // end constructor
@ -73,12 +67,12 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
boolean update(){ // update() method
LOG1("Updating Dimmable LED on pin=");
LOG1(ledPin);
LOG1(ledPin->getPin());
LOG1(": Current Power=");
LOG1(power->getVal()?"true":"false");
LOG1(" Current Brightness=");
LOG1(level->getVal());
if(power->updated()){
LOG1(" New Power=");
LOG1(power->getNewVal()?"true":"false");
@ -91,7 +85,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
LOG1("\n");
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true
@ -102,32 +96,27 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode)
PwmPin *redPin, *greenPin, *bluePin;
int redChannel, greenChannel, blueChannel;
LedPin *redPin, *greenPin, *bluePin;
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *H; // reference to the Hue Characteristic
SpanCharacteristic *S; // reference to the Saturation Characteristic
SpanCharacteristic *V; // reference to the Brightness Characteristic
DEV_RgbLED(int redChannel, int greenChannel, int blueChannel, int redPin, int greenPin, int bluePin) : Service::LightBulb(){ // constructor() method
DEV_RgbLED(int red_pin, int green_pin, int blue_pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
H=new Characteristic::Hue(0); // instantiate the Hue Characteristic with an initial value of 0 out of 360
S=new Characteristic::Saturation(0); // instantiate the Saturation Characteristic with an initial value of 0%
V=new Characteristic::Brightness(100); // instantiate the Brightness Characteristic with an initial value of 100%
V->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->redChannel=redChannel; // save the channel number (from 0-15)
this->greenChannel=greenChannel;
this->blueChannel=blueChannel;
this->redPin=new PwmPin(redChannel, redPin); // instantiate the PWM channel and attach the specified pin
this->greenPin=new PwmPin(greenChannel, greenPin);
this->bluePin=new PwmPin(blueChannel, bluePin);
this->redPin=new LedPin(red_pin); // configures a PWM LED for output to the RED pin
this->greenPin=new LedPin(green_pin); // configures a PWM LED for output to the GREEN pin
this->bluePin=new LedPin(blue_pin); // configures a PWM LED for output to the BLUE pin
char cBuf[128];
sprintf(cBuf,"Configuring RGB LED: Pins=(%d,%d,%d) Channels=(%d,%d,%d)\n",redPin,greenPin,bluePin,redChannel,greenChannel,blueChannel);
sprintf(cBuf,"Configuring RGB LED: Pins=(%d,%d,%d)\n",redPin->getPin(),greenPin->getPin(),bluePin->getPin());
Serial.print(cBuf);
} // end constructor
@ -143,7 +132,7 @@ struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode)
p=power->getVal();
char cBuf[128];
sprintf(cBuf,"Updating RGB LED on pins=(%d,%d,%d): ",redPin->getPin(),greenPin->getPin(),bluePin->getPin());
sprintf(cBuf,"Updating RGB LED: Pins=(%d,%d,%d): ",redPin->getPin(),greenPin->getPin(),bluePin->getPin());
LOG1(cBuf);
if(power->updated()){
@ -178,24 +167,24 @@ struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode)
}
LOG1(cBuf);
// Here we call a static function of PwmPin that converts HSV to RGB.
// Here we call a static function of LedPin that converts HSV to RGB.
// Parameters must all be floats in range of H[0,360], S[0,1], and V[0,1]
// R, G, B, returned [0,1] range as well
PwmPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); // since HomeKit provides S and V in percent, scale down by 100
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); // since HomeKit provides S and V in percent, scale down by 100
int R, G, B;
R=p*r*100; // since PwmPin uses percent, scale back up by 100, and multiple by status fo power (either 0 or 1)
R=p*r*100; // since LedPin uses percent, scale back up by 100, and multiple by status fo power (either 0 or 1)
G=p*g*100;
B=p*b*100;
sprintf(cBuf,"RGB=(%d,%d,%d)\n",R,G,B);
LOG1(cBuf);
redPin->set(redChannel,R); // update the PWM channels with new values
greenPin->set(greenChannel,G);
bluePin->set(blueChannel,B);
redPin->set(R); // update the ledPin channels with new values
greenPin->set(G);
bluePin->set(B);
return(true); // return true

View File

@ -83,7 +83,7 @@ void setup() {
new SpanAccessory();
new DEV_Identify("Ceiling Fan #1","HomeSpan","123-ABC","20mA LED","0.9",0);
(new DEV_DimmableLED(0,17))->setPrimary(); // Here we specify DEV_DimmableLED as the Primary Service by "chaining" setPrimary() to the pointer return by new. Note parentheses!
(new DEV_DimmableLED(17))->setPrimary(); // Here we specify DEV_DimmableLED as the Primary Service by "chaining" setPrimary() to the pointer return by new. Note parentheses!
new Service::Fan();
new Characteristic::Active();
new Characteristic::RotationDirection();
@ -91,8 +91,8 @@ void setup() {
new SpanAccessory();
new DEV_Identify("Ceiling Fan #2","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_DimmableLED(0,17);
(new Service::Fan())->setPrimary(); // Here we specify the Fan as the Primary Service. Again, note how we encapsulated the "new" command in parentheses, then chained setPrimary()
new DEV_DimmableLED(17);
(new Service::Fan())->setPrimary(); // Here we specify the Fan as the Primary Service. Again, note how we encapsulated the "new" command in parentheses, then chained setPrimary()
new Characteristic::Active();
new Characteristic::RotationDirection();
new Characteristic::RotationSpeed(0);
@ -121,7 +121,7 @@ void setup() {
new SpanAccessory();
new DEV_Identify("Ceiling Fan #3","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_DimmableLED(0,17);
new DEV_DimmableLED(17);
new Characteristic::Name("Main Light"); // Here we create a name for the Dimmable LED
new DEV_LED(16);
new Characteristic::Name("Night Light"); // Here we create a name for the On/Off LED
@ -149,7 +149,7 @@ void setup() {
new SpanAccessory();
(new DEV_Identify("Ceiling Fan #4","HomeSpan","123-ABC","20mA LED","0.9",0))->setPrimary(); // specify DEV_Identify as the Primary Service
new DEV_DimmableLED(0,17);
new DEV_DimmableLED(17);
new Characteristic::Name("Main Light");
new DEV_LED(16);
new Characteristic::Name("Night Light");
@ -172,7 +172,7 @@ void setup() {
new Characteristic::RotationDirection();
new Characteristic::RotationSpeed(0);
new Characteristic::Name("Fan");
new DEV_DimmableLED(0,17);
new DEV_DimmableLED(17);
new Characteristic::Name("Main Light");
new DEV_LED(16);
new Characteristic::Name("Night Light");

View File

@ -45,27 +45,21 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
PwmPin *pwmPin; // reference to PWM Pin
int ledPin; // pin number defined for this LED
int channel; // PWM channel used for this LED (should be unique for each LED)
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
DEV_DimmableLED(int channel, int ledPin) : Service::LightBulb(){
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
level->setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
this->channel=channel; // save the channel number (from 0-15)
this->ledPin=ledPin; // save LED pin number
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
Serial.print(ledPin);
Serial.print(" Channel=");
Serial.print(channel);
Serial.print(ledPin->getPin());
Serial.print("\n");
} // end constructor
@ -73,12 +67,12 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
boolean update(){ // update() method
LOG1("Updating Dimmable LED on pin=");
LOG1(ledPin);
LOG1(ledPin->getPin());
LOG1(": Current Power=");
LOG1(power->getVal()?"true":"false");
LOG1(" Current Brightness=");
LOG1(level->getVal());
if(power->updated()){
LOG1(" New Power=");
LOG1(power->getNewVal()?"true":"false");
@ -91,7 +85,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
LOG1("\n");
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true

View File

@ -139,7 +139,7 @@ void setup() {
new SpanAccessory();
new DEV_Identify("PushButton LED","HomeSpan","123-ABC","20mA LED","0.9",0);
new DEV_DimmableLED(0,17,23,5,18); // NEW! added three extra arguments to specify the pin numbers for three SpanButtons() - see DEV_LED.h
new DEV_DimmableLED(17,23,5,18); // NEW! added three extra arguments to specify the pin numbers for three SpanButtons() - see DEV_LED.h
} // end of setup()

View File

@ -16,8 +16,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
// raise button: SHORT press increases brightness by 1%; LONG press increases brightness by 10%; DOUBLE press increases brightness to maximum
// lower button: SHORT press decreases brightness by 1%; LONG press decreases brightness by 10%; DOUBLE press decreases brightness to minimum
PwmPin *pwmPin; // reference to PWM Pin
int ledPin; // pin number defined for this LED
LedPin *ledPin; // reference to Led Pin
int powerPin; // NEW! pin with pushbutton to turn on/off LED
int raisePin; // NEW! pin with pushbutton to increase brightness
int lowerPin; // NEW! pin with pushButton to decrease brightness
@ -26,9 +25,9 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
SpanCharacteristic *level; // reference to the Brightness Characteristic
int favoriteLevel=50; // NEW! keep track of a 'favorite' level
// NEW! Consructor includes 3 additionl arguments to specify pin numbers for power, raise, and lower buttons
// NEW! Consructor includes 3 additional arguments to specify pin numbers for power, raise, and lower buttons
DEV_DimmableLED(int channel, int ledPin, int powerPin, int raisePin, int lowerPin) : Service::LightBulb(){
DEV_DimmableLED(int pin, int powerPin, int raisePin, int lowerPin) : Service::LightBulb(){
power=new Characteristic::On();
@ -46,15 +45,13 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
new SpanButton(raisePin,500); // NEW! create new SpanButton to increase brightness using pushbutton on pin number "raisePin"
new SpanButton(lowerPin,500); // NEW! create new SpanButton to decrease brightness using pushbutton on pin number "lowerPin"
this->channel=channel; // save the channel number (from 0-15)
this->ledPin=ledPin; // save LED pin number
this->powerPin=powerPin; // NEW! save power pushbutton pin number
this->raisePin=raisePin; // NEW! save increase brightness pushbutton pin number
this->lowerPin=lowerPin; // NEW! save decrease brightness pushbutton pin number
this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
Serial.print(ledPin);
Serial.print(ledPin->getPin());
Serial.print(" Channel=");
Serial.print(channel);
Serial.print("\n");
@ -64,7 +61,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
boolean update(){ // update() method
LOG1("Updating Dimmable LED on pin=");
LOG1(ledPin);
LOG1(ledPin->getPin());
LOG1(": Current Power=");
LOG1(power->getVal()?"true":"false");
LOG1(" Current Brightness=");
@ -82,7 +79,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
LOG1("\n");
pwmPin->set(channel,power->getNewVal()*level->getNewVal());
ledPin->set(power->getNewVal()*level->getNewVal());
return(true); // return true
@ -115,9 +112,9 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
LOG1("Saved new brightness level="); // ...and output log message
LOG1(favoriteLevel);
LOG1("\n");
pwmPin->set(channel,(1-power->getVal())*level->getVal()); // blink the LED to indicate new level has been saved
ledPin->set((1-power->getVal())*level->getVal()); // blink the LED to indicate new level has been saved
delay(100);
pwmPin->set(channel,(1-power->getVal())*level->getVal());
ledPin->set((1-power->getVal())*level->getVal());
}
} else
@ -153,7 +150,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
// Don't forget to set the new power and level for the actual LED - the above code by itself only changes the values of the Characteristics
// within HomeKit! We still need to take an action on the actual LED itself.
// Note the line below is similar to, but not the same as, the pwmPin->set function used in the update() method above. Within the
// Note the line below is similar to, but not the same as, the ledPin->set function used in the update() method above. Within the
// update() method we used getNewVal() because we wanted to change the LED to match the NEW VALUES requested by the user via the
// HomeKit Controller. We did not need to (and must not) use setVal() to modify these values in the update() method since HomeSpan
// automatically does this for us, provided we return StatusCode::OK at the end of the update() method.
@ -164,7 +161,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
// as shown below. As usual, HomeSpan will send Event Notifications to all registered HomeKit Controllers letting them know about any changes
// we made using setVal().
pwmPin->set(channel,power->getVal()*level->getVal()); // update the physical LED to reflect the new values
ledPin->set(power->getVal()*level->getVal()); // update the physical LED to reflect the new values
}

View File

@ -6,12 +6,13 @@
LedPin::LedPin(uint8_t pin, uint8_t level){
if(numChannels+ServoPin::numChannels>15){
Serial.printf("\n*** ERROR: Can't create LedPin(%d) - no open PWM channels ***\n\n",pin);
ledChannel.gpio_num=0;
return;
}
enabled=true;
if(numChannels==0){ // first instantiation of an LedPin
if(numChannels==0){ // first instantiation of an LedPin
ledc_timer_config_t ledTimer;
ledTimer.timer_num=LEDC_TIMER_0;
ledTimer.duty_resolution=LEDC_TIMER_10_BIT;
@ -40,7 +41,6 @@ LedPin::LedPin(uint8_t pin, uint8_t level){
ledChannel.hpoint=0;
ledc_channel_config(&ledChannel);
set(level);
//Serial.printf("Configured LED on Pin %d using Channel %d in Speed Mode %d\n",ledChannel.gpio_num,ledChannel.channel,ledChannel.speed_mode);
}
///////////////////
@ -118,6 +118,7 @@ void LedPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){
ServoPin::ServoPin(uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){
if(numChannels>7 || numChannels>(15-LedPin::numChannels)){
Serial.printf("\n*** ERROR: Can't create ServoPin(%d) - no open PWM channels ***\n\n",pin);
servoChannel.gpio_num=0;
return;
}
@ -128,12 +129,14 @@ ServoPin::ServoPin(uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t
this->minDegrees=minDegrees;
microsPerDegree=(double)(maxMicros-minMicros)/(maxDegrees-minDegrees);
ledc_timer_config_t ledTimer;
ledTimer.timer_num=LEDC_TIMER_1;
ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE;
ledTimer.duty_resolution=LEDC_TIMER_16_BIT;
ledTimer.freq_hz=50;
ledc_timer_config(&ledTimer);
if(numChannels==0){ // first instantiation of a ServoPin
ledc_timer_config_t ledTimer;
ledTimer.timer_num=LEDC_TIMER_1;
ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE;
ledTimer.duty_resolution=LEDC_TIMER_16_BIT;
ledTimer.freq_hz=50;
ledc_timer_config(&ledTimer);
}
servoChannel.gpio_num=pin;
servoChannel.speed_mode=LEDC_HIGH_SPEED_MODE;
@ -143,7 +146,6 @@ ServoPin::ServoPin(uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t
servoChannel.hpoint=0;
servoChannel.duty*=micros2duty;
set(initDegrees);
//Serial.printf("Configured Servo on Pin %d using Channel %d in Speed Mode %d\n",servoChannel.gpio_num,servoChannel.channel,servoChannel.speed_mode);
}
///////////////////

View File

@ -3,10 +3,7 @@
// as well as compile and test from this point. This file is ignored when the library is included in other sketches.
#include "PwmPin.h"
//ServoPin servo(3,18,-90);
//ServoPin servo(18,0,500,2200,-90,90);
void setup(){
Serial.begin(115200);
@ -30,14 +27,14 @@ void setup(){
LedPin d10(19);
LedPin d11(19);
LedPin d12(19);
LedPin red(17,100);
LedPin red(17);
// ServoPin servo(18,0,500,2200,-90,90);
ServoPin s0(19);
ServoPin servo(18,45);
ServoPin s1(19);
while(0){
while(1){
for(int i=0;i<100;i++){
yellow.set(i);
delay(10);