ESP + Reorganizacja
This commit is contained in:
85
ESP32/ESP32-HomeSpan/06-DimmableLED.ino
Normal file
85
ESP32/ESP32-HomeSpan/06-DimmableLED.ino
Normal file
@@ -0,0 +1,85 @@
|
||||
/*********************************************************************************
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-2024 Gregg E. Berman
|
||||
*
|
||||
* https://github.com/HomeSpan/HomeSpan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
********************************************************************************/
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// HomeSpan: A HomeKit implementation for the ESP32 //
|
||||
// ------------------------------------------------ //
|
||||
// //
|
||||
// Example 6: One working on/off LED and one working //
|
||||
// dimmable LED, both based on the LightBulb //
|
||||
// Service //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include "DEV_LED.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
// Example 6 changes Example 5 so that LED #2 is now dimmable, instead of just on/off. This requires us to create a new
|
||||
// derived Service we will name "DEV_DimmableLED" Instead of creating a new file to store this definition, we will simply
|
||||
// tack it on to the end of the DEV_LED.h file that includes the code we created in Example 5 to control an on/off LED.
|
||||
// Grouping similar-style Services in one ".h" file makes it easier to organize your custom devices.
|
||||
|
||||
// As usual, all previous comments have been deleted and only new changes from the previous example are shown.
|
||||
|
||||
// NOTE: The Arduino/ESP32 code base does not include the function analogWrite() which is typically used to create a PWM
|
||||
// output to drive the brightness of an LED. Instead, the ESP32 code base itself includes a set of functions to create PWM output
|
||||
// and the ESP32 chip has built-in PWM functionality specifically for this purpose.
|
||||
|
||||
// HomeSpan wraps all of this PWM functionality into a single integrated class called LedPin, making it very easy to define
|
||||
// dimmable LED pins and set the PWM level (i.e. duty cycle) from 0-100%. Use of this LedPin class is shown in DEV_DimmableLED.
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
homeSpan.begin(Category::Lighting,"HomeSpan LED");
|
||||
|
||||
new SpanAccessory();
|
||||
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
|
||||
new DEV_LED(16); // create an on/off LED attached to pin 16 (same as in Example 5)
|
||||
|
||||
new SpanAccessory();
|
||||
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
|
||||
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()
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
void loop(){
|
||||
|
||||
homeSpan.poll();
|
||||
|
||||
} // end of loop()
|
||||
19
ESP32/ESP32-HomeSpan/DEV_RELAY.h
Normal file
19
ESP32/ESP32-HomeSpan/DEV_RELAY.h
Normal file
@@ -0,0 +1,19 @@
|
||||
struct DEV_RELAY : Service::Switch { // deriving from the HomeSpan Switch service
|
||||
|
||||
int relayPin;
|
||||
SpanCharacteristic *power;
|
||||
|
||||
DEV_RELAY(int relayPin) : Service::Switch() {
|
||||
|
||||
power = new Characteristic::On();
|
||||
this->relayPin = relayPin;
|
||||
pinMode(relayPin, OUTPUT);
|
||||
}
|
||||
|
||||
boolean update() {
|
||||
|
||||
digitalWrite(relayPin, power->getNewVal());
|
||||
|
||||
return(true);
|
||||
}
|
||||
};
|
||||
33
ESP32/ESP32-HomeSpan/ESP32-HomeSpan.ino
Normal file
33
ESP32/ESP32-HomeSpan/ESP32-HomeSpan.ino
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include "LED.h"
|
||||
#include "DEV_RELAY.h"
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
|
||||
homeSpan.setPairingCode("11122333");
|
||||
homeSpan.setQRID("111-22-333");
|
||||
|
||||
homeSpan.begin(Category::Bridges, "HomeSpan Bridge");
|
||||
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new LED(LED_BUILTIN);
|
||||
|
||||
|
||||
// Accessory 2: Relay Switch
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new DEV_RELAY(4); // instantiates a new relay
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
homeSpan.poll();
|
||||
}
|
||||
28
ESP32/ESP32-HomeSpan/LED.h
Normal file
28
ESP32/ESP32-HomeSpan/LED.h
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
struct LED : Service::LightBulb { // First we create a derived class from the HomeSpan LightBulb Service
|
||||
|
||||
int ledPin; // this variable stores the pin number defined for this LED
|
||||
SpanCharacteristic *power; // here we create a generic pointer to a SpanCharacteristic named "power" that we will use below
|
||||
|
||||
// Next we define the constructor for DEV_LED. Note that it takes one argument, ledPin,
|
||||
// which specifies the pin to which the LED is attached.
|
||||
|
||||
LED(int ledPin) : Service::LightBulb(){
|
||||
|
||||
power=new Characteristic::On(); // this is where we create the On Characterstic we had previously defined in setup(). Save this in the pointer created above, for use below
|
||||
this->ledPin=ledPin; // don't forget to store ledPin...
|
||||
pinMode(ledPin,OUTPUT); // ...and set the mode for ledPin to be an OUTPUT (standard Arduino function)
|
||||
|
||||
} // end constructor
|
||||
|
||||
// Finally, we over-ride the default update() method with instructions that actually turn on/off the LED. Note update() returns type boolean
|
||||
|
||||
boolean update(){
|
||||
|
||||
digitalWrite(ledPin,power->getNewVal()); // use a standard Arduino function to turn on/off ledPin based on the return of a call to power->getNewVal() (see below for more info)
|
||||
|
||||
return(true); // return true to indicate the update was successful (otherwise create code to return false if some reason you could not turn on the LED)
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user