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.
This commit is contained in:
Gregg 2021-03-04 07:18:13 -06:00
parent 5c5361be7b
commit 26a38b68cc
3 changed files with 46 additions and 78 deletions

View File

@ -16,8 +16,7 @@ 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<float>()+0.5; // "simulate" a half-degree temperature change (it's okay that we set step size in SpanRange to 1)...
float temperature=temp->getVal<float>()+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;

View File

@ -1520,60 +1520,21 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){
nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"type\":\"%s\"",type);
if(perms&PR){
if(perms&NV && !(flags&GET_NV)){
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);
else
nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%s",uvPrint(value).c_str());
}
break;
case STRING:
nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":\"%s\"",value.STRING);
break;
} // switch
} // print Characteristic value
} // permissions=PR
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<float>(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;
}
///////////////////////////////

View File

@ -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,11 +339,29 @@ struct SpanCharacteristic{
} // get()
template <typename A, typename B, typename S=int> SpanCharacteristic *setRange(A min, B max, S step=0){
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 <typename T, typename A=boolean, typename B=boolean> void init(T val, A min=0, B max=1){
uvSet(value,val);
@ -351,7 +369,8 @@ struct SpanCharacteristic{
uvSet(minValue,min);
uvSet(maxValue,max);
uvSet(stepValue,0);
}
} // init()
template <class T=int> T getVal(){
return(uvGet<T>(value));
@ -364,7 +383,7 @@ struct SpanCharacteristic{
template <typename T> 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);
};