Completed SpanButton()
And finished code for Example 15 to verify SpanButton() works as expected. To Do: add comments and notes to Example 15.
This commit is contained in:
parent
c997ca3462
commit
f056b6ae82
|
|
@ -26,9 +26,8 @@ void setup() {
|
|||
new Characteristic::Version("1.1.0");
|
||||
|
||||
new SpanAccessory();
|
||||
|
||||
new DEV_Identify("Switched LED","HomeSpan","123-ABC","20mA LED","0.9",0);
|
||||
new DEV_DimmableLED(0,17);
|
||||
new DEV_DimmableLED(0,17,19,5,18);
|
||||
|
||||
} // end of setup()
|
||||
|
||||
|
|
|
|||
|
|
@ -11,21 +11,29 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
|
|||
|
||||
PwmPin *pwmPin; // reference to PWM Pin
|
||||
int ledPin; // pin number defined for this LED
|
||||
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
|
||||
int channel; // PWM channel used for this LED (should be unique for each LED)
|
||||
SpanCharacteristic *power; // reference to the On Characteristic
|
||||
SpanCharacteristic *level; // reference to the Brightness Characteristic
|
||||
|
||||
DEV_DimmableLED(int channel, int ledPin, ServiceType sType=ServiceType::Regular) : Service::LightBulb(sType){ // // NEW! modified constructor() method
|
||||
DEV_DimmableLED(int channel, int ledPin, int powerPin, int raisePin, int lowerPin, ServiceType sType=ServiceType::Regular) : Service::LightBulb(sType){
|
||||
|
||||
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%
|
||||
|
||||
new SpanButton(19);
|
||||
new SpanButton(powerPin); // NEW! create new SpanButton to control power on pin number "powerPin"
|
||||
new SpanButton(raisePin,1000); // NEW! create new SpanButton to increase brightness on pin number "raisePin"
|
||||
new SpanButton(lowerPin,3000,500); // NEW! create new SpanButton to decrease brightness 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
|
||||
|
||||
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
|
||||
|
|
@ -65,13 +73,41 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
|
|||
|
||||
void button(int pin, boolean isLong){
|
||||
|
||||
Serial.print("Found button press on pin: ");
|
||||
Serial.print(pin);
|
||||
Serial.print(" type: ");
|
||||
Serial.print(isLong?"LONG":"SHORT");
|
||||
Serial.print("\n");
|
||||
LOG1("Found button press on pin: ");
|
||||
LOG1(pin);
|
||||
LOG1(" type: ");
|
||||
LOG1(isLong?"LONG":"SHORT");
|
||||
LOG1("\n");
|
||||
|
||||
if(pin==powerPin && !isLong){
|
||||
power->setVal(1-power->getVal());
|
||||
} else
|
||||
|
||||
if(pin==powerPin && isLong){
|
||||
if(power->getVal())
|
||||
level->setVal(100);
|
||||
else
|
||||
level->setVal(5);
|
||||
} else
|
||||
|
||||
if(pin==raisePin){
|
||||
int newLevel=level->getVal()+(isLong?10:1);
|
||||
if(newLevel>100)
|
||||
newLevel=100;
|
||||
level->setVal(newLevel);
|
||||
} else
|
||||
|
||||
if(pin==lowerPin){
|
||||
int newLevel=level->getVal()-(isLong?10:1);
|
||||
if(newLevel<5)
|
||||
newLevel=5;
|
||||
level->setVal(newLevel);
|
||||
}
|
||||
|
||||
pwmPin->set(channel,power->getVal()*level->getVal());
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
|
|
|
|||
12
src/HAP.cpp
12
src/HAP.cpp
|
|
@ -97,21 +97,11 @@ void HAPClient::init(){
|
|||
for(int i=0;i<homeSpan.Accessories.size();i++){ // identify all services with over-ridden loop() methods
|
||||
for(int j=0;j<homeSpan.Accessories[i]->Services.size();j++){
|
||||
SpanService *s=homeSpan.Accessories[i]->Services[j];
|
||||
|
||||
if((void*)(s->*(&SpanService::loop)) != (void*)(&SpanService::loop)) // save pointers to services in Loops vector
|
||||
if((void(*)())(s->*(&SpanService::loop)) != (void(*)())(&SpanService::loop)) // save pointers to services in Loops vector
|
||||
homeSpan.Loops.push_back(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(int i=0;i<homeSpan.PushButtons.size();i++){
|
||||
Serial.print("PushButton Found on pin: ");
|
||||
Serial.print(homeSpan.PushButtons[i]->pin);
|
||||
Serial.print(" iid: ");
|
||||
Serial.print(homeSpan.PushButtons[i]->service->iid);
|
||||
Serial.print("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
|
|
|||
|
|
@ -386,10 +386,29 @@ void Span::processSerialCommand(char *c){
|
|||
}
|
||||
break;
|
||||
|
||||
case 'i':{
|
||||
Serial.print("\n*** HomeSpan Info ***\n\n");
|
||||
char cBuf[128];
|
||||
for(int i=0;i<Accessories.size();i++){ // identify all services with over-ridden loop() methods
|
||||
for(int j=0;j<Accessories[i]->Services.size();j++){
|
||||
SpanService *s=Accessories[i]->Services[j];
|
||||
sprintf(cBuf,"Service aid=%2d iid=%2d Update: %3s Loop: %3s Button: %3s\n",Accessories[i]->aid,s->iid,
|
||||
(void(*)())(s->*(&SpanService::update))!=(void(*)())(&SpanService::update)?"YES":"NO",
|
||||
(void(*)())(s->*(&SpanService::loop))!=(void(*)())(&SpanService::loop)?"YES":"NO",
|
||||
(void(*)(int,boolean))(s->*(&SpanService::button))!=(void(*)(int,boolean))(&SpanService::button)?"YES":"NO"
|
||||
);
|
||||
Serial.print(cBuf);
|
||||
}
|
||||
}
|
||||
Serial.print("\n*** End Status ***\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case '?': {
|
||||
Serial.print("\n*** HomeSpan Commands ***\n\n");
|
||||
Serial.print(" s - print connection status\n");
|
||||
Serial.print(" d - print attributes database\n");
|
||||
Serial.print(" i - print detailed info about configuration\n");
|
||||
Serial.print(" W - delete stored WiFi data and restart\n");
|
||||
Serial.print(" H - delete stored HomeKit Pairing data and restart\n");
|
||||
Serial.print(" F - delete all stored WiFi Network and HomeKit Pairing data and restart\n");
|
||||
|
|
@ -1173,6 +1192,9 @@ SpanButton::SpanButton(int pin, unsigned long longTime, unsigned long shortTime)
|
|||
this->longTime=longTime;
|
||||
service=homeSpan.Accessories.back()->Services.back();
|
||||
|
||||
if((void(*)(int,boolean))(service->*(&SpanService::button))==(void(*)(int,boolean))(&SpanService::button))
|
||||
Serial.print("*** WARNING: No button() method defined for this PushButton!\n\n");
|
||||
|
||||
homeSpan.PushButtons.push_back(this);
|
||||
|
||||
pinMode(pin,INPUT_PULLUP);
|
||||
|
|
@ -1182,20 +1204,20 @@ SpanButton::SpanButton(int pin, unsigned long longTime, unsigned long shortTime)
|
|||
|
||||
void SpanButton::check(){
|
||||
|
||||
if(state==UNTRIGGERED && !digitalRead(pin)){
|
||||
state=TRIGGERED;
|
||||
if(!isTriggered && !digitalRead(pin)){
|
||||
isTriggered=true;
|
||||
unsigned long cTime=millis();
|
||||
shortAlarm=cTime+shortTime;
|
||||
longAlarm=cTime+longTime;
|
||||
} else
|
||||
|
||||
if(state==TRIGGERED && digitalRead(pin)){
|
||||
if(isTriggered && digitalRead(pin)){
|
||||
unsigned long cTime=millis();
|
||||
if(cTime>longAlarm)
|
||||
service->button(pin, true);
|
||||
else if(cTime>shortAlarm)
|
||||
service->button(pin, false);
|
||||
state=UNTRIGGERED;
|
||||
isTriggered=false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,19 +239,13 @@ struct SpanBuf{ // temporary storage buffer for us
|
|||
struct SpanButton{
|
||||
|
||||
int pin; // pin number
|
||||
boolean isTriggered=false; // flag triggered when button is pressed
|
||||
unsigned long shortTime; // time (in millis) required to register a short press
|
||||
unsigned long longTime; // time (in millis) required to register a long press
|
||||
unsigned long shortAlarm; // alarm time to trigger a short press
|
||||
unsigned long longAlarm; // alarm time to triger a long press
|
||||
SpanService *service; // Service to which this PushButton is attached
|
||||
|
||||
enum {
|
||||
UNTRIGGERED,
|
||||
TRIGGERED,
|
||||
SHORT,
|
||||
LONG
|
||||
} state=UNTRIGGERED;
|
||||
|
||||
SpanButton(int pin, unsigned long longTime=2000, unsigned long shortTime=5);
|
||||
void check();
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue