analog mapping return float value now

noize parameter working against input of mapping translation
This commit is contained in:
2019-06-26 23:35:55 +03:00
parent d7e4181377
commit 3542ba1a6a
3 changed files with 28 additions and 23 deletions

Binary file not shown.

View File

@@ -449,39 +449,40 @@ void Input::contactPoll() {
void Input::analogPoll() { void Input::analogPoll() {
int16_t mappedInputVal; int16_t inputVal;
int32_t mappedInputVal; // 100x inputVal
aJsonObject *inputMap = aJson.getObjectItem(inputObj, "map"); aJsonObject *inputMap = aJson.getObjectItem(inputObj, "map");
short Noize = ANALOG_NOIZE; int16_t Noize = ANALOG_NOIZE;
short simple = 0; short simple = 0;
/*
#if defined(ARDUINO_ARCH_STM32)
WiringPinMode inputPinMode;
#endif
#if defined(__SAM3X8E__)||defined(ARDUINO_ARCH_AVR)||defined(ARDUINO_ARCH_ESP8266)||defined(ARDUINO_ARCH_ESP32)
#endif */
uint32_t inputPinMode; uint32_t inputPinMode;
int max=1024*100;
int min=0;
if (inType & IN_ACTIVE_HIGH) { if (inType & IN_ACTIVE_HIGH) {
inputPinMode = INPUT; inputPinMode = INPUT;
} else { } else {
inputPinMode = INPUT_PULLUP; inputPinMode = INPUT_PULLUP;
} }
pinMode(pin, inputPinMode); pinMode(pin, inputPinMode);
mappedInputVal = analogRead(pin); inputVal = analogRead(pin);
// Mapping // Mapping
if (inputMap && inputMap->type == aJson_Array) if (inputMap && inputMap->type == aJson_Array)
{ {
int max=1024;
if (aJson.getArraySize(inputMap)>=4) if (aJson.getArraySize(inputMap)>=4)
mappedInputVal = map (mappedInputVal, mappedInputVal = map (inputVal,
aJson.getArrayItem(inputMap, 0)->valueint, aJson.getArrayItem(inputMap, 0)->valueint,
aJson.getArrayItem(inputMap, 1)->valueint, aJson.getArrayItem(inputMap, 1)->valueint,
aJson.getArrayItem(inputMap, 2)->valueint, min=aJson.getArrayItem(inputMap, 2)->valueint*100,
max=aJson.getArrayItem(inputMap, 3)->valueint); max=aJson.getArrayItem(inputMap, 3)->valueint*100);
else mappedInputVal = inputVal*100;
if (aJson.getArraySize(inputMap)==5) Noize = aJson.getArrayItem(inputMap, 4)->valueint; if (aJson.getArraySize(inputMap)==5) Noize = aJson.getArrayItem(inputMap, 4)->valueint;
if (mappedInputVal>max) mappedInputVal=max;
if (mappedInputVal>max) mappedInputVal = max;
if (mappedInputVal<min) mappedInputVal = min;
if (aJson.getArraySize(inputMap)==2) if (aJson.getArraySize(inputMap)==2)
{ {
simple = 1; simple = 1;
@@ -497,14 +498,16 @@ void Input::analogPoll() {
store->currentValue = mappedInputVal; store->currentValue = mappedInputVal;
}} }}
else else
if (abs(mappedInputVal - store->currentValue)>Noize) // value changed >ANALOG_NOIZE //if (abs(mappedInputVal - store->currentValue)>Noize || mappedInputVal == min || mappedInputVal ==max) // value changed >ANALOG_NOIZE
if (abs(inputVal - store->currentValue)>Noize || mappedInputVal == min || mappedInputVal ==max) // value changed >ANALOG_NOIZE
store->bounce = 0; store->bounce = 0;
else // no change else // no change
if (store->bounce<ANALOG_STATE_ATTEMPTS) store->bounce ++; if (store->bounce<ANALOG_STATE_ATTEMPTS) store->bounce ++;
if (store->bounce<ANALOG_STATE_ATTEMPTS-1 && (mappedInputVal != store->currentValue)) //confirmed change if (store->bounce<ANALOG_STATE_ATTEMPTS-1 && (mappedInputVal != store->currentValue)) //confirmed change
{ {
onAnalogChanged(mappedInputVal); onAnalogChanged(mappedInputVal/100.);
// store->currentValue = mappedInputVal;
store->currentValue = mappedInputVal; store->currentValue = mappedInputVal;
} }
@@ -563,7 +566,7 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri
} }
} }
void Input::onAnalogChanged(int newValue) { void Input::onAnalogChanged(float newValue) {
debugSerial << F("IN:") << (pin) << F("=") << newValue << endl; debugSerial << F("IN:") << (pin) << F("=") << newValue << endl;
aJsonObject *item = aJson.getObjectItem(inputObj, "item"); aJsonObject *item = aJson.getObjectItem(inputObj, "item");
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit"); aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
@@ -581,15 +584,17 @@ void Input::onAnalogChanged(int newValue) {
strncpy(addrstr,emit->valuestring,sizeof(addrstr)); strncpy(addrstr,emit->valuestring,sizeof(addrstr));
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
char strVal[16]; char strVal[16];
itoa(newValue,strVal,10); //itoa(newValue,strVal,10);
printFloatValueToStr(newValue, strVal);
if (mqttClient.connected() && !ethernetIdleCount) if (mqttClient.connected() && !ethernetIdleCount)
mqttClient.publish(addrstr, strVal, true); mqttClient.publish(addrstr, strVal, true);
} }
if (item) { if (item) {
int intNewValue = round(newValue);
Item it(item->valuestring); Item it(item->valuestring);
if (it.isValid()) { if (it.isValid()) {
it.Ctrl(0, 1, &newValue, true); it.Ctrl(0, 1, &intNewValue, true);
} }
} }
} }

View File

@@ -103,7 +103,7 @@ public:
boolean isValid(); boolean isValid();
void onContactChanged(int newValue); void onContactChanged(int newValue);
void onAnalogChanged(int newValue); void onAnalogChanged(float newValue);
int poll(short cause); int poll(short cause);
void setup(); void setup();