From 064d881e9cf8782ddc233ddcceb6e16a3b32afdc Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 27 Feb 2021 12:54:32 -0600 Subject: [PATCH] Refactored getVal() and getNewVal() Functionality is identical, but template has been moved into UVal structure along with setVal, which simplifies code and allows for other uses if needed. --- src/HomeSpan.h | 66 ++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 1d52edc..7e23842 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -107,7 +107,40 @@ union UVal { FLOAT=(double)val; break; } - } + } // set() + + template T get(FORMAT fmt){ + + switch(fmt){ + + case FORMAT::BOOL: + return((T) BOOL); + + case FORMAT::INT: + return((T) INT); + + case FORMAT::UINT8: + return((T) UINT8); + + case FORMAT::UINT16: + return((T) UINT16); + + case FORMAT::UINT32: + return((T) UINT32); + + case FORMAT::UINT64: + return((T) UINT64); + + case FORMAT::FLOAT: + return((T) FLOAT); + + case FORMAT::STRING: + Serial.print("*** ERROR: Can't use getVal() or getNewVal() for string Characteristics.\n\n"); + return(0); + } + + } // get() + }; /////////////////////////////// @@ -321,34 +354,9 @@ struct SpanCharacteristic{ boolean updated(){return(isUpdated);} // returns isUpdated unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated - template T getVal(){return(getValue(value));} // returns UVal value - template T getNewVal(){return(getValue(newValue));} // returns UVal newValue - - template T getValue(UVal v){ - - switch(format){ - case BOOL: - return((T) v.BOOL); - case INT: - return((T) v.INT); - case UINT8: - return((T) v.UINT8); - case UINT16: - return((T) v.UINT16); - case UINT32: - return((T) v.UINT32); - case UINT64: - return((T) v.UINT64); - case FLOAT: - return((T) v.FLOAT); - case STRING: - Serial.print("*** ERROR: Can't use getVal() or getNewVal() for string Characteristics.\n\n"); - return(0); - - } // switch - - } // getValue - + template T getVal(){return(value.get(format));} // returns UVal value + template T getNewVal(){return(newValue.get(format));} // returns UVal newValue + template void setVal(T val){ value.set(format, val);