ESP + Reorganizacja
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
/*********************************************************************************
|
||||
* 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 9: Logging messages to the Serial Monitor //
|
||||
// //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include "DEV_LED.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
// HomeSpan sends a variety of messages to the Serial Monitor of the Arduino IDE whenever the device is connected
|
||||
// to a computer. Message output can be performed either by the usual Serial.print() or Serial.printf() functions,
|
||||
// or by one of three user macros: LOG0(), LOG1() and LOG2(). These three macros output messages to the Serial Monitor
|
||||
// depending on HomeSpan's Log Level setting:
|
||||
|
||||
// at a setting of 0, only LOG0() message are output; LOG1() and LOG2() messages are ignored
|
||||
// at a setting of 1, both LOG0() and LOG1() messages are output; LOG2() messages are ignored
|
||||
// at a setting of 2, all LOG0(), LOG1(), and LOG2() messages are output
|
||||
|
||||
// Example 9 illustrates how to add such log messages. The code is identical to Example 8 (without comments), except
|
||||
// that LOG0() and LOG1() messages have been added to DEV_LED.h. The LOG0() messages will always be
|
||||
// output to the Arduino Serial Monitor. The LOG1() messages will only be output if the Log Level is set to 1 or 2.
|
||||
|
||||
// The setLogLevel() method of homeSpan can used to change the log level as follows:
|
||||
|
||||
// homeSpan.setLogLevel(0) - sets Log Level to 0
|
||||
// homeSpan.setLogLevel(1) - sets Log Level to 1
|
||||
// homeSpan.setLogLevel(2) - sets Log Level to 2
|
||||
|
||||
// The method should be called BEFORE homeSpan.begin() - see below for proper use. Note that the Log Level
|
||||
// can also be changed dynamically during runtime via the HomeSpan CLI by typing 'L0', 'L1', or 'L2' into the Serial Monitor
|
||||
|
||||
// There are two forms of the LOG0(), LOG1(), and LOG2() macros. The first form takes only a single argument and outputs
|
||||
// messsges using the Serial.print(var) function. This allows you to output any single variable or text message, but does not allow you
|
||||
// to control the format, or to output more than one variable at a time. The second form take multiple arguments, where the first
|
||||
// is a standard C++ formatting string, and any remaining arguments are consumed according to the format string. This form
|
||||
// utilizes the variadic Serial.printf(char *fmt [,var1, var2...]) function.
|
||||
|
||||
// RECOMMENDATION: Since a HomeSpan ESP32 is meant to be physically connected to real-world devices, you may find
|
||||
// yourself with numerous ESP32s each configured with a different set of Accessories. To aid in identification
|
||||
// you may want to add LOG0() statements containing some sort of initialization message to the constructors for
|
||||
// each derived Service, such as DEV_LED. Doing so allows HomeSpan to "report" on its configuration upon start-up. See
|
||||
// DEV_LED for examples.
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
homeSpan.setLogLevel(1); // NEW - Sets Log Level to 1, which causes LOG1() messages to be output
|
||||
|
||||
homeSpan.begin(Category::Bridges,"HomeSpan Bridge");
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Simple LED");
|
||||
new DEV_LED(16);
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Dimmable LED");
|
||||
new DEV_DimmableLED(17);
|
||||
|
||||
} // end of setup()
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
void loop(){
|
||||
|
||||
homeSpan.poll();
|
||||
|
||||
} // end of loop()
|
||||
107
ESP32/HomeSpan-master/examples/09-MessageLogging/DEV_LED.h
Normal file
107
ESP32/HomeSpan-master/examples/09-MessageLogging/DEV_LED.h
Normal file
@@ -0,0 +1,107 @@
|
||||
|
||||
////////////////////////////////////
|
||||
// DEVICE-SPECIFIC LED SERVICES //
|
||||
////////////////////////////////////
|
||||
|
||||
struct DEV_LED : Service::LightBulb { // ON/OFF LED
|
||||
|
||||
int ledPin; // pin number defined for this LED
|
||||
SpanCharacteristic *power; // reference to the On Characteristic
|
||||
|
||||
DEV_LED(int ledPin) : Service::LightBulb(){ // constructor() method
|
||||
|
||||
power=new Characteristic::On();
|
||||
this->ledPin=ledPin;
|
||||
pinMode(ledPin,OUTPUT);
|
||||
|
||||
// Here we output log messages when the constructor is initially called.
|
||||
// We use LOG0() to ensure the message is always output regardless of the
|
||||
// LOG Level setting. Note this uses the single-argument form of LOG(), so
|
||||
// multiple calls are needed to create a complete message
|
||||
|
||||
LOG0("Configuring On/Off LED: Pin="); // initialization message
|
||||
LOG0(ledPin);
|
||||
LOG0("\n");
|
||||
|
||||
} // end constructor
|
||||
|
||||
boolean update(){ // update() method
|
||||
|
||||
// Here we output log messages whenever update() is called, which is helpful
|
||||
// for debugging purposes if your physical device is not functioning as expected.
|
||||
// Since it's just for debugging, we use LOG1() instead of LOG0(). Note we can
|
||||
// output both the current as well as the new power settings. We've again
|
||||
// used the single-argument form of LOG() to create this message
|
||||
|
||||
LOG1("Updating On/Off LED on pin=");
|
||||
LOG1(ledPin);
|
||||
LOG1(": Current Power=");
|
||||
LOG1(power->getVal()?"true":"false");
|
||||
LOG1(" New Power=");
|
||||
LOG1(power->getNewVal()?"true":"false");
|
||||
LOG1("\n");
|
||||
|
||||
digitalWrite(ledPin,power->getNewVal());
|
||||
|
||||
return(true); // return true
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
|
||||
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
|
||||
|
||||
LedPin *ledPin; // reference to Led Pin
|
||||
SpanCharacteristic *power; // reference to the On Characteristic
|
||||
SpanCharacteristic *level; // reference to the Brightness Characteristic
|
||||
|
||||
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%
|
||||
|
||||
// Here we once again output log messages when the constructor is initially called.
|
||||
// However, this time we use the multi-argument form of LOG() that resembles a
|
||||
// standard printf() function, which makes for more compact code.
|
||||
|
||||
LOG0("Configuring Dimmable LED: Pin=%d\n",pin); // initialization message
|
||||
|
||||
this->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
|
||||
|
||||
} // end constructor
|
||||
|
||||
boolean update(){ // update() method
|
||||
|
||||
// Note that since Dimmable_LED has two updateable Characteristics,
|
||||
// HomeKit may be requesting either or both to be updated. We can
|
||||
// use the "isUpdated" flag of each Characteristic to output a message
|
||||
// only if HomeKit actually requested an update for that Characteristic.
|
||||
// Since update() is called whenever there is an update to at least
|
||||
// one of the Characteristics in a Service, either power, level, or both
|
||||
// will have its "isUpdated" flag set.
|
||||
|
||||
// As above, we use the multi-argument form of LOG() to create the messages
|
||||
// Note that for DimmableLED, ledPin has a method getPin() that retrieves the
|
||||
// pin number so you don't need to store it separately.
|
||||
|
||||
LOG1("Updating Dimmable LED on pin=%dL Current Power=%s Current Brightness=%d",ledPin->getPin(),power->getVal()?"true":"false",level->getVal());
|
||||
|
||||
if(power->updated())
|
||||
LOG1(" New Power=%s",power->getNewVal()?"true":"false");
|
||||
|
||||
if(level->updated())
|
||||
LOG1(" New Brightness=%d",level->getNewVal());
|
||||
|
||||
LOG1("\n");
|
||||
|
||||
ledPin->set(power->getNewVal()*level->getNewVal());
|
||||
|
||||
return(true); // return true
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
Reference in New Issue
Block a user