From 26a38b68ccaeb98577007c537e89409af9dba104 Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 4 Mar 2021 07:18:13 -0600 Subject: [PATCH] Completed full implementation of setRange() and deprecation of SpanRange() Next TO DO: Complete entry of default min/max into all Characteristics (except BOOL and STRING) defined Span.h so that setRange() error checking works for all Characteristics. --- examples/12-ServiceLoops/DEV_Sensors.h | 7 ++- src/HomeSpan.cpp | 74 +++++--------------------- src/HomeSpan.h | 43 ++++++++++----- 3 files changed, 46 insertions(+), 78 deletions(-) diff --git a/examples/12-ServiceLoops/DEV_Sensors.h b/examples/12-ServiceLoops/DEV_Sensors.h index b172eb6..86f0cff 100644 --- a/examples/12-ServiceLoops/DEV_Sensors.h +++ b/examples/12-ServiceLoops/DEV_Sensors.h @@ -16,9 +16,8 @@ struct DEV_TempSensor : Service::TemperatureSensor { // A standalone Tempera // of Celsius or Fahrenheit for each Service, it does not appear to work as advertised. temp=new Characteristic::CurrentTemperature(30.0); // instantiate the Current Temperature Characteristic - //new SpanRange(-50,100,1); // expand the range from the HAP default of 0-100 to -50 to 100 to allow for negative temperatures - temp->setRange(-50,100,5.5); - + temp->setRange(-50,100); // expand the range from the HAP default of 0-100 to -50 to 100 to allow for negative temperatures + Serial.print("Configuring Temperature Sensor"); // initialization message Serial.print("\n"); @@ -40,7 +39,7 @@ struct DEV_TempSensor : Service::TemperatureSensor { // A standalone Tempera void loop(){ if(temp->timeVal()>5000){ // check time elapsed since last update and proceed only if greater than 5 seconds - float temperature=temp->getVal()+0.5; // "simulate" a half-degree temperature change (it's okay that we set step size in SpanRange to 1)... + float temperature=temp->getVal()+0.5; // "simulate" a half-degree temperature change... if(temperature>35.0) // ...but cap the maximum at 35C before starting over at -30C temperature=-30.0; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 6ce917b..da10762 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1519,61 +1519,22 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ if(flags&GET_TYPE) nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"type\":\"%s\"",type); - if(perms&PR){ - - if(perms&NV && !(flags&GET_NV)){ + if(perms&PR){ + if(perms&NV && !(flags&GET_NV)) nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } else { - - switch(format){ - case BOOL: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%s",value.BOOL?"true":"false"); - break; - - case INT: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%d",value.INT); - break; - - case UINT8: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%u",value.UINT8); - break; - - case UINT16: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%u",value.UINT16); - break; - - case UINT32: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%u",value.UINT32); - break; - - case UINT64: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%llu",value.UINT64); - break; - - case FLOAT: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%lg",value.FLOAT); - if(customRange && (flags&GET_META)){ - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minValue\":%lg,\"maxValue\":%lg",minValue.FLOAT,maxValue.FLOAT); - if(stepValue.FLOAT>0) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minStep\":%lg",stepValue.FLOAT); - } - break; - - case STRING: - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":\"%s\"",value.STRING); - break; - - } // switch - } // print Characteristic value - } // permissions=PR - - + else + nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%s",uvPrint(value).c_str()); + } if(flags&GET_META){ nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"%s\"",formatCodes[format]); - if(range && (flags&GET_META)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minValue\":%d,\"maxValue\":%d,\"minStep\":%d",range->min,range->max,range->step); + if(customRange && (flags&GET_META)){ + nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minValue\":%s,\"maxValue\":%s",uvPrint(minValue).c_str(),uvPrint(maxValue).c_str()); + + if(uvGet(stepValue)>0) + nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minStep\":%s",uvPrint(stepValue).c_str()); + } } if(desc && (flags&GET_DESC)){ @@ -1699,20 +1660,13 @@ unsigned long SpanCharacteristic::timeVal(){ /////////////////////////////// SpanRange::SpanRange(int min, int max, int step){ - this->min=min; - this->max=max; - this->step=step; - - homeSpan.configLog+="------>SpanRange: " + String(min) + "/" + String(max) + "/" + String(step); if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty() || homeSpan.Accessories.back()->Services.back()->Characteristics.empty() ){ - homeSpan.configLog+=" *** ERROR! Can't create new Range without a defined Characteristic! ***\n"; + homeSpan.configLog+="------>SpanRange: *** ERROR! Can't create new Range without a defined Characteristic! ***\n"; homeSpan.nFatalErrors++; - return; + } else { + homeSpan.Accessories.back()->Services.back()->Characteristics.back()->setRange(min,max,step); } - - homeSpan.configLog+="\n"; - homeSpan.Accessories.back()->Services.back()->Characteristics.back()->range=this; } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index a050b18..f1951c8 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -244,7 +244,6 @@ struct SpanCharacteristic{ UVal maxValue; // Characteristic maximum (not applicable for STRING) UVal stepValue; // Characteristic step size (not applicable for STRING) boolean customRange=false; // Flag for custom ranges - SpanRange *range=NULL; // Characteristic min/max/step; NULL = default values (optional) boolean *ev; // Characteristic Event Notify Enable (per-connection) uint32_t aid=0; // Accessory ID - passed through from Service containing this Characteristic @@ -281,7 +280,8 @@ struct SpanCharacteristic{ sprintf(c,"%llg",u.FLOAT); return(String(c)); case FORMAT::STRING: - return(String(u.STRING)); + sprintf(c,"\"%s\"",u.STRING); + return(String(c)); } // switch } // str() @@ -339,19 +339,38 @@ struct SpanCharacteristic{ } // get() template SpanCharacteristic *setRange(A min, B max, S step=0){ - uvSet(minValue,min); - uvSet(maxValue,max); - uvSet(stepValue,step); - customRange=true; - } + char c[256]; + homeSpan.configLog+="------>Range: "; + + if(format==BOOL || format==STRING){ + sprintf(c,"*** ERROR! Can't change range for STRING or BOOL Characteristics! ***\n",hapName); + homeSpan.nFatalErrors++; + } else { + + uvSet(minValue,min); + uvSet(maxValue,max); + uvSet(stepValue,step); + customRange=true; + + if(step>0) + sprintf(c,"%s/%s/%s\n",uvPrint(minValue),uvPrint(maxValue),uvPrint(stepValue)); + else + sprintf(c,"%s/%s\n",uvPrint(minValue),uvPrint(maxValue)); + } + homeSpan.configLog+=c; + return(this); + + } // setRange() + template void init(T val, A min=0, B max=1){ uvSet(value,val); uvSet(newValue,val); uvSet(minValue,min); uvSet(maxValue,max); uvSet(stepValue,0); - } + + } // init() template T getVal(){ return(uvGet(value)); @@ -364,7 +383,7 @@ struct SpanCharacteristic{ template void setVal(T val){ if(format==STRING){ - Serial.printf("\n*** WARNING: Attempt to update Characteristic::%s(\"%s\") with setVal() ignored. Can't update string Characteristics once they are initialized!\n\n",hapName,value.STRING); + Serial.printf("\n*** WARNING: Attempt to update Characteristic::%s(\"%s\") with setVal() ignored. Can't update STRING Characteristics once they are initialized!\n\n",hapName,value.STRING); return; } @@ -385,17 +404,13 @@ struct SpanCharacteristic{ sb.val=dummy; // set dummy "val" so that sprintfNotify knows to consider this "update" homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector - } // setValue + } // setVal() }; /////////////////////////////// struct SpanRange{ - int min; - int max; - int step; - SpanRange(int min, int max, int step); };