PCA9685 - Add telemetry output

This commit is contained in:
andrethomas 2018-10-01 00:18:37 +02:00
parent 068211cae9
commit da0b283fe7
1 changed files with 19 additions and 2 deletions

View File

@ -32,6 +32,7 @@
uint8_t pca9685_detected = 0;
uint16_t pca9685_freq = USE_PCA9685_FREQ;
uint16_t pca9685_pin_pwm_value[16];
void PCA9685_Detect(void)
{
@ -58,6 +59,7 @@ void PCA9685_Reset(void)
PCA9685_SetPWMfreq(USE_PCA9685_FREQ);
for (uint8_t pin=0;pin<16;pin++) {
PCA9685_SetPWM(pin,0,false);
pca9685_pin_pwm_value[pin] = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"PCA9685\":{\"RESET\":\"OK\"}}"));
}
@ -115,6 +117,7 @@ bool PCA9685_Command(void)
if (',' == XdrvMailbox.data[ca]) { paramcount++; }
}
UpperCase(XdrvMailbox.data,XdrvMailbox.data);
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET")) { PCA9685_Reset(); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"PWMF")) {
@ -122,7 +125,7 @@ bool PCA9685_Command(void)
uint16_t new_freq = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
if ((new_freq >= 24) && (new_freq <= 1526)) {
PCA9685_SetPWMfreq(new_freq);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"PCA9685\":{\"PWMF\":%i, \"Result\":\"OK\"}}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"PCA9685\":{\"PWMF\":%i, \"Result\":\"OK\"}}"),new_freq);
return serviced;
}
} else { // No parameter was given for setfreq, so we return current setting
@ -159,6 +162,17 @@ bool PCA9685_Command(void)
return serviced;
}
void PCA9685_OutputTelemetry(void) {
if (0 == pca9685_detected) { return; } // We do not do this if the PCA9685 has not been detected
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_TIME "\":\"%s\",\"PCA9685\": {"), GetDateAndTime(DT_LOCAL).c_str());
snprintf_P(mqtt_data,sizeof(mqtt_data), PSTR("%s\"PWM_FREQ\":%i,"),mqtt_data,pca9685_freq);
for (uint8_t pin=0;pin<16;pin++) {
snprintf_P(mqtt_data,sizeof(mqtt_data), PSTR("%s\"PWM%i\":%i,"),mqtt_data,pin,pca9685_pin_pwm_value[pin]);
}
snprintf_P(mqtt_data,sizeof(mqtt_data),PSTR("%s\"END\":1}}"),mqtt_data);
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
}
boolean Xdrv15(byte function)
{
boolean result = false;
@ -169,6 +183,9 @@ boolean Xdrv15(byte function)
break;
case FUNC_EVERY_SECOND:
PCA9685_Detect();
if (tele_period == 0) {
PCA9685_OutputTelemetry();
}
break;
case FUNC_EVERY_50_MSECOND:
break;