Update Overview.md

This commit is contained in:
HomeSpan 2020-11-21 15:08:23 -06:00 committed by GitHub
parent cd135d6958
commit 01db723d48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 77 additions and 2 deletions

View File

@ -12,7 +12,7 @@ There are three primary components of HomeSpan:
1. **An End-User Environment** - allows an end-user to configure a standalone HomeSpan device using only a simple Control Button and the device's Status LED 1. **An End-User Environment** - allows an end-user to configure a standalone HomeSpan device using only a simple Control Button and the device's Status LED
### Overview of the HomeSpan API # Overview of the HomeSpan API
The basic structure of a HomeSpan sketch is as follows: The basic structure of a HomeSpan sketch is as follows:
@ -45,7 +45,7 @@ void setup() {
homeSpan.begin(); // initialize HomeSpan homeSpan.begin(); // initialize HomeSpan
/// CONFIGURATION OF ACCESSORY ATTRIBUTE DATABASE GOES HERE /// /// DEFINITION OF HAP ACCESSORY ATTRIBUTE DATABASE GOES HERE ///
} // end of setup() } // end of setup()
@ -56,6 +56,81 @@ void loop(){
} // end of loop() } // end of loop()
``` ```
## Creating the HAP Accessory Attribute Database
The next step is to implement the code that defines the HAP Accessory Attribute Database, which is not really a database but simply a list of all HAP accessory objects, Service objects, and Characteristic objects implemented by this HomeSpan device.
You create a new HAP Accessory by instantiating a new `SpanAccessory` object as follows:
```C++
new SpanAccessory();
```
SpanAccessory takes no paramaters, and you do not need to save the object in a variable since HomeSpan automatically registers the Accessory inside the homespan object.
Once a HAP Accessory is created, you can then start adding HAP Services and HAP Characteristics by instantiating HomeSpan Service and Characteristic objects. Every HAP Service supported by HomeSpan is defined in the `Service` namespace. Every HAP Characteristic supported by HomeSpan is defined in the `Characteristic` namespace. See [HomeSpan Services and Characteristics](ServiceList.md) for a complete list.
For example, to add a HAP Carbon Dioxide Sensor Service to an Accessory, simply instantiate the corresponding HomeSpan Service object as follows:
```C++
new Service::CarbonDioxideSensor();
```
HomeSpan Service objects take no parameters, and you do not need to save the object in a variable. HomeSpan will automatically register this Service and attach it to the last Accessory you defined.
HAP Characteristics are added to a Service in a similar fashion. For example, to add a HAP Carbon Dioxide Detected Characteristic, simply instantiate the corresponding HomeSpan Chararacteristic object as follows:
```C++
new Characteristic::CarbonDioxideDetected();
```
HomeSpan will automatically register this Characteristic and attach it to the last Service you defined. However, unlike Accessory and Service Objects, Characteristics do take a single optional argument (not shown above) that initializes the value of the Characteristic at start-up. If you don't specify an argument, HomeSpan will use a sensible default.
A complete HAP Accessory Attribute Database is thus defined by simply instantiating one or more SpanAccessory objects, each with one or more Service objects, and that in turn each contain one of more Characteristic objects. For example, a HomeSpan device supporting a variable-speed, reversible ceiling fan with a non-dimmable ceiling light, and a table lamp with a dimmable lightbulb, could be implemented as follows:
```C++
#include "HomeSpan.h" // include the HomeSpan library
void setup() {
Serial.begin(115200); // start the Serial interface
homeSpan.begin(); // initialize HomeSpan
new SpanAccessory(); // Reversible, Variable-Speed Ceiling Fan with Non-Dimmable Ceiling Light
new Service::Fan();
new Characteristic::Active();
new Characteristic::RotationSpeed(100); // initialize default speed to be 100%
new Characteristic::RotationDirection();
new Service::LightBulb();
new Characteristics::On();
new SpanAccessory() // Table Lamp with Dimmable Light
new Service::LightBulb();
new Characteristic::On();
new Characteristics::Brightness(50); // initializes default brightness to be 50%
} // end of setup()
void loop(){
homeSpan.poll();
} // end of loop()
```
As you can see, you do not need to name any objects, or specify any HAP parameters, such a format types, UUID codes, etc. However, the ORDER in which you instantiate objects is critical. Characteristics are automatically associated with the last Service instantiated, and Services are automatically associated with the last Accessory instantiated.
> HomeSpan has extensive error checking. At start-up, HomeSpan will validate the configuration of the HAP Accessory Attribute Database to ensure that every Accessory has all the required Services, and each Service has all the required Characteristics. If HomeSpan finds an Accessory is missing a required Service, a Service is missing a required Characteristic, or a Characteristic that is neither required nor optional has been added to a Service that does not support that Characteristic, HomeSpan will report these errors and halt the program.
In fact, if you tried to run the above sketch you would find it failed to validate. That's because each Accessory is missing two required Services - the HAP Accessory Information Service, and the HAP Protcol Information Service. See the [Tutorials](Tutorials.md) for full completed and valid configurations that include these required HAP Services.
Need to discuss micro-controller usage to connect to real-world devices (not to connect to other web servers) Need to discuss micro-controller usage to connect to real-world devices (not to connect to other web servers)