From 6a74ce92838be3491c77d4ded6386f6b5c130d08 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 5 Sep 2021 08:54:34 -0500 Subject: [PATCH] Add setValidValues(int n, ...) method to Characteristic Allows user to explicitly set the valid values for a Characteristic of type UINT8. Throws an error if used with any other Characteristic type. --- src/HomeSpan.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/HomeSpan.h | 4 +++- src/Span.h | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2ce3689..47f3567 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1600,6 +1600,10 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ if(uvGet(stepValue)>0) nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minStep\":%s",uvPrint(stepValue).c_str()); } + + if(validValues){ + nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"valid-values\":%s",validValues); + } } if(desc && (flags&GET_DESC)){ @@ -1720,6 +1724,40 @@ unsigned long SpanCharacteristic::timeVal(){ return(homeSpan.snapTime-updateTime); } +/////////////////////////////// + +void SpanCharacteristic::setValidValues(int n, ...){ + char c[256]; + String *s = new String("["); + va_list vl; + va_start(vl,n); + for(int i=0;ic_str(); + sprintf(c,": ValidValues=%s\n",validValues); + } + + homeSpan.configLog+=c; +} + /////////////////////////////// // SpanRange // /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index d1e5b14..f61d911 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -256,8 +256,9 @@ struct SpanCharacteristic{ UVal minValue; // Characteristic minimum (not applicable for STRING) UVal maxValue; // Characteristic maximum (not applicable for STRING) UVal stepValue; // Characteristic step size (not applicable for STRING) - boolean staticRange; // Flag that indiates whether Range is static and cannot be changed with setRange() + boolean staticRange; // Flag that indicates whether Range is static and cannot be changed with setRange() boolean customRange=false; // Flag for custom ranges + const char *validValues=NULL; // Optional JSON array of valid values. Applicable only to uint8 Characteristics boolean *ev; // Characteristic Event Notify Enable (per-connection) char *nvsKey=NULL; // key for NVS storage of Characteristic value @@ -274,6 +275,7 @@ struct SpanCharacteristic{ boolean updated(){return(isUpdated);} // returns isUpdated unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated + void setValidValues(int n, ...); // sets a list of 'n' valid values allowed for a Characteristic. Only applicable if format=uint8 String uvPrint(UVal &u){ char c[64]; diff --git a/src/Span.h b/src/Span.h index 64695af..be8b4aa 100644 --- a/src/Span.h +++ b/src/Span.h @@ -381,7 +381,7 @@ namespace Service { // SPAN CHARACTERISTICS (HAP Chapter 9) // ////////////////////////////////////////// -// Macro to define Span Characteristic structures based on name of HAP Characteristic, default value, and mix/max value (not applicable for STRING or BOOL which default to min=0, max=1) +// Macro to define Span Characteristic structures based on name of HAP Characteristic, default value, and min/max value (not applicable for STRING or BOOL which default to min=0, max=1) #define CREATE_CHAR(TYPE,HAPCHAR,DEFVAL,MINVAL,MAXVAL) \ struct HAPCHAR : SpanCharacteristic { HAPCHAR(TYPE val=DEFVAL, boolean nvsStore=false) : SpanCharacteristic {&hapChars.HAPCHAR} { init(val,nvsStore,(TYPE)MINVAL,(TYPE)MAXVAL); } };