Updated Examples to use `setRange()` instead of `new SpanRange()`

setRange() is preferred method.  SpanRange() is legacy only
This commit is contained in:
Gregg 2021-03-07 17:39:42 -06:00
parent ff0dfefc47
commit 53268127be
10 changed files with 36 additions and 27 deletions

View File

@ -62,30 +62,39 @@ void setup() {
new Service::LightBulb();
new Characteristic::On(true); // NEW: Providing an argument sets its initial value. In this case it means the LightBulb will be turned on at start-up
new Characteristic::Brightness(50); // NEW: This allows control of the Brightness of the LightBulb, with an initial value of 50% upon start-up (Note 1)
new SpanRange(20,100,5); // NEW: This sets the range of the Brightness to be from a min of 20%, to a max of 100%, in steps of 5% (Note 2)
// In addition to setting the initial value of a Characteristic, it is also possible to override the default min/max/step range specified by HAP.
// We do this with the setRange() method:
// setRange(min, max, step), where
//
// min = minimum allowed value
// max = maximum allowed value
// step = step size (can be left blank, in which case the HAP default is retained)
// The setRange() method can be called on any numerical-based Characteristic that supports range overrides. The easiest way to apply to method is to call it right
// after instantiating a new Characteristic. Don't forget to surround the "new" command in parentheses when chaining a method in this fashion.
// Here we create a Brightness Characteristic to set the brightness of the LightBulb with an initial value of 50% and an allowable range
// from 20-100% in steps of 5%. See Notes 1 and 2 below for more details:
(new Characteristic::Brightness(50))->setRange(20,100,5);
new Service::Fan();
new Characteristic::Active();
new Characteristic::RotationDirection(); // NEW: This allows control of the Rotation Direction of the Fan
new Characteristic::RotationSpeed(25); // NEW: This allows control of the Rotation Speed of the Fan, with an initial value of 25% upon start-up (Note 1)
new SpanRange(0,100,25); // NEW: This sets the range of the Rotation Speed to be from a min of 0%, to a max of 100%, in steps of 25%
(new Characteristic::RotationSpeed(50))->setRange(0,100,25); // NEW: This allows control of the Rotation Speed of the Fan, with an initial value of 50% and a range from 0-100 in steps of 25%
// NOTE 1: Setting the initial value of the Brightness Characteristic to 50% does not by itself cause HomeKit to turn the light on to 50% upon start-up.
// Rather, this is governed by the initial value of the On Characteristic, which in this case happens to be set to true. If it were set to false,
// or left unspecified (default is false) then the LightBulb will be off at start-up. However, it will jump to 50% brightness as soon as turned on
// for the first time. This same logic applies to the Active and RotationSpeed Characteristics for a Fan.
// NOTE 2: The default range for Characteristics that support a range of values is specified in the HAP Section 9. For Brightness, the range defaults
// to min=0%, max=100%, step=1%. SpanRange(min,max,step) can be used to over-ride this default. SpanRange is generic and can be used wih other Characteristics
// that support a range of values, such as RotationSpeed. Whenever a new SpanRange is defined it is applied to the most recently defined Characteristic.
// It only has an effect if that Characteristic utilizes ranges.
// RECOMMENDATION: Using SpanRange to change the minimum Brightness from 0% (the default) to 20% (or any non-zero value) provides for a better
// NOTE 2: The default range for Characteristics that support a range of values is specified in HAP Section 9. For Brightness, the range defaults
// to min=0%, max=100%, step=1%. Using setRange() to change the minimum Brightness from 0% to 20% (or any non-zero value) provides for a better
// HomeKit experience. This is because the LightBulb power is controlled by the On Characteristic, and allowing Brightness to be as low as 0%
// sometimes results in HomeKit turning on the LightBulb but with Brightness=0%, which is not very intuitive. This can occur when asking Siri
// to lower the Brightness all the way, and turnign on the LightBulb. By setting a minumum value of 20%, HomeKit always ensures that there is
// to lower the Brightness all the way, and then turning on the LightBulb. By setting a minumum value of 20%, HomeKit always ensures that there is
// some Brightness value whenever the LightBulb is turned on.
} // end of setup()

View File

@ -43,7 +43,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
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)
new SpanRange(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)
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.

View File

@ -41,7 +41,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
new SpanRange(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%
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.

View File

@ -41,7 +41,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
new SpanRange(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%
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.

View File

@ -64,7 +64,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
new SpanRange(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%
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!!

View File

@ -56,7 +56,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
new SpanRange(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%
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
@ -116,7 +116,7 @@ struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode)
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%
new SpanRange(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%
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;

View File

@ -56,7 +56,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
new SpanRange(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%
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

View File

@ -71,11 +71,11 @@ struct DEV_WindowShade : Service::WindowCovering { // A motorized Window Sha
DEV_WindowShade() : Service::WindowCovering(){ // constructor() method
current=new Characteristic::CurrentPosition(0); // Windows Shades have positions that range from 0 (fully lowered) to 100 (fully raised)
new SpanRange(0,100,10); // set the allowable current-position range to 0-100 IN STEPS of 10
current=new Characteristic::CurrentPosition(0); // Window Shades have positions that range from 0 (fully lowered) to 100 (fully raised)
current->setRange(0,100,10); // set the allowable current-position range to 0-100 IN STEPS of 10
target=new Characteristic::TargetPosition(0); // Windows Shades have positions that range from 0 (fully lowered) to 100 (fully raised)
new SpanRange(0,100,10); // set the allowable target-position range to 0-100 IN STEPS of 10
target=new Characteristic::TargetPosition(0); // Window Shades have positions that range from 0 (fully lowered) to 100 (fully raised)
target->setRange(0,100,10); // set the allowable target-position range to 0-100 IN STEPS of 10
Serial.print("Configuring Motorized Window Shade"); // initialization message
Serial.print("\n");

View File

@ -33,7 +33,7 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
power=new Characteristic::On();
level=new Characteristic::Brightness(favoriteLevel); // Brightness Characteristic with an initial value equal to the favorite level
new SpanRange(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%
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%
// NEW! Below we create three SpanButton() objects. In the first we specify the pin number, as required, but allow SpanButton() to use
// its default values for a LONG press (2000 ms), a SINGLE press (5 ms), and a DOUBLE press (200 ms). In the second and third we change the

View File

@ -359,7 +359,7 @@ struct SpanCharacteristic{
uvSet(stepValue,step);
customRange=true;
if(step>0)
if(uvGet<double>(stepValue)>0)
sprintf(c,": %s/%s/%s\n",uvPrint(minValue),uvPrint(maxValue),uvPrint(stepValue));
else
sprintf(c,": %s/%s\n",uvPrint(minValue),uvPrint(maxValue));