From 24cbad3257d3d26826766fd587f81fe9fa9d2453 Mon Sep 17 00:00:00 2001 From: Vic Date: Mon, 4 Jan 2021 14:57:32 +0100 Subject: [PATCH] more diagnostics --- tasmota/.xdrv_47_ftc532.ino.kate-swp | Bin 0 -> 710 bytes tasmota/xdrv_47_ftc532.ino | 99 ++++++++++++++++----------- 2 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 tasmota/.xdrv_47_ftc532.ino.kate-swp diff --git a/tasmota/.xdrv_47_ftc532.ino.kate-swp b/tasmota/.xdrv_47_ftc532.ino.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..46ab3cc1996b17b4471ee761f28e803930b1f42e GIT binary patch literal 710 zcmZ9}J4*vW5C`zx_zGx@im&)$F~)R~J2i#cNZ5$@Bzq`UN5B&f!eMbi?X2?|Y%Kj6 zehO1D(@2@8_M>XXok;mRH6g_OZ|~Kse7v9B^nQ}J zueE+Y3XyuY(lA?I(Kz6mUo}#>r*#$UZPs1s@2z%@qxUT@fyVw0kZ8Vgw)41~)R@|nE_(}rk1q(JjFnk{TI>^8&wW?!4ztiGbnVDb?q@X4~9 zk;vS0MGlVGBk%)z6wWRW6UX4}^5AjU>7RfV_vgulX3*H*^+OXJ2Rh`n1=Kd!i|`eD x3BF)2!`JK*e9m5hHG35f*lR=U>#%dy4RTI72{oL+%h<=j-@A_i_a9?V{{fF>QeprA literal 0 HcmV?d00001 diff --git a/tasmota/xdrv_47_ftc532.ino b/tasmota/xdrv_47_ftc532.ino index 5c076faa4..e88a80312 100644 --- a/tasmota/xdrv_47_ftc532.ino +++ b/tasmota/xdrv_47_ftc532.ino @@ -54,105 +54,122 @@ #define XDRV_47 47 #define FTC532_KEYS_MAX 8 +#define FTC532_RETRY 8 // number of reads before discarding #define FTC532_STATE_WAITING false #define FTC532_STATE_READING true // Rising edge timing in microseconds #define FTC532_BIT 377 +#define FTC532_NOISE (FTC532_BIT * 3 / 2) #define FTC532_SHORT (FTC532_BIT * 2) #define FTC532_LONG (FTC532_BIT * 4) #define FTC532_IDLE (FTC532_BIT * 10) #define FTC532_MAX (FTC532_BIT * 58) +#define DEBUG_FTC532 //@@@@@@@@@@@@@@@@ + struct FTC532 { volatile uint32_t rxtime; // ISR timer memory - volatile uint16_t sample = 0xF0F0; // buffer for bit-coded time samples + volatile uint16_t tsmp = 0xF0F0; // buffer for bit-coded time samples + volatile uint16_t sample = 0xF0F0; // complete samples volatile uint16_t rxbit; // ISR bit counter uint8_t keys = 0; // bitmap of active keys uint8_t old_keys = 0; // previously active keys volatile bool state; // ISR state - bool present = false; -#ifdef DEBUG_TASMOTA_DRIVER - volatile uint16_t errors; // error counter - volatile bool valid; // did we ever receive valid data? -#endif // DEBUG_TASMOTA_DRIVER + bool present = false; // driver active +#ifdef DEBUG_FTC532 + volatile uint16_t errors = 0; // inv. key error counter + volatile uint16_t frame = 0; // frame error counter + volatile uint16_t noise = 0; // noise detection counter + volatile bool valid = 0; // did we ever receive valid data? +#endif // DEBUG_FTC532 } Ftc532; const char ftc532_json[] PROGMEM = "\"FTC532\":{\"KEYS\":\""; -void ICACHE_RAM_ATTR ftc532_ISR(void) { // Hardware interrupt routine, triggers on rising edge +void ICACHE_RAM_ATTR ftc532_ISR(void) { // Hardware interrupt routine, triggers on rising edge uint32_t time = micros(); uint32_t time_diff = time - Ftc532.rxtime; Ftc532.rxtime = time; if (Ftc532.state == FTC532_STATE_WAITING) { - if (time_diff > FTC532_LONG + FTC532_SHORT) { // new frame + if (time_diff > FTC532_LONG + FTC532_SHORT) { // new frame Ftc532.rxbit = 0; Ftc532.state = FTC532_STATE_READING; } return; } // FTC532_STATE_READING starts here if (time_diff > FTC532_LONG + FTC532_BIT) { -#ifdef DEBUG_TASMOTA_DRIVER - ++Ftc532.errors; // frame error -#endif // DEBUG_TASMOTA_DRIVER +#ifdef DEBUG_FTC532 + ++Ftc532.frame; // frame error +#endif // DEBUG_FTC532 Ftc532.state = FTC532_STATE_WAITING; return; } if (time_diff > FTC532_SHORT + FTC532_BIT) { - Ftc532.sample |= (1 << Ftc532.rxbit); // LONG + Ftc532.tsmp |= (1 << Ftc532.rxbit); // LONG + } else if (time_diff < FTC532_NOISE) { // noise detector +#ifdef DEBUG_FTC532 + ++Ftc532.noise; +#endif // DEBUG_FTC532 + Ftc532.state = FTC532_STATE_WAITING; + return; } else { - Ftc532.sample &= ~(1 << Ftc532.rxbit); // SHORT + Ftc532.tsmp &= ~(1 << Ftc532.rxbit); // SHORT } ++Ftc532.rxbit; - if (Ftc532.rxbit == FTC532_KEYS_MAX * 2) { // frame complete + if (Ftc532.rxbit == FTC532_KEYS_MAX * 2) { // frame complete + Ftc532.sample = Ftc532.tsmp; // copy frame Ftc532.rxbit = 0; -#ifdef DEBUG_TASMOTA_DRIVER - Ftc532.valid = true; -#endif // DEBUG_TASMOTA_DRIVER Ftc532.state = FTC532_STATE_WAITING; +#ifdef DEBUG_FTC532 + Ftc532.valid = true; +#endif // DEBUG_FTC532 } } -void ftc532_init(void) { // Initialize +void ftc532_init(void) { // Initialize if (!PinUsed(GPIO_FTC532)) { return; } -#ifdef DEBUG_TASMOTA_DRIVER - Ftc532.errors = 0; - Ftc532.valid = false; -#endif // DEBUG_TASMOTA_DRIVER Ftc532.state = FTC532_STATE_WAITING; - Ftc532.rxtime = micros(); pinMode(Pin(GPIO_FTC532), INPUT_PULLUP); + Ftc532.rxtime = micros(); attachInterrupt(Pin(GPIO_FTC532), ftc532_ISR, RISING); Ftc532.present = true; } -void ftc532_update(void) { // Usually called every 50 ms -#ifdef DEBUG_TASMOTA_DRIVER -// WARNING: Reduce callback frequency if this code is enabled -// if ((Ftc532.sample & 0xF) != ((~Ftc532.sample >> 4) & 0xF) || ((Ftc532.sample >> 8) & 0xF) != ((~Ftc532.sample >> 12) & 0xF)) { -// AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: inverted sample does not match %x %x %x %x"), -// Ftc532.sample & 0xF, (~Ftc532.sample >> 4) & 0xF, (Ftc532.sample >> 8) & 0xF, (~Ftc532.sample >> 12) & 0xF); -// } -#endif // DEBUG_TASMOTA_DRIVER - Ftc532.keys = (Ftc532.sample & 0xF) | ((Ftc532.sample >> 4) & 0xF0); - if (Ftc532.keys != Ftc532.old_keys) { -#ifdef DEBUG_TASMOTA_DRIVER - AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: SAM=%04X KEY=%02X OLD=%02X ERR=%u OK=%u TIME=%lu Pin=%u"), - Ftc532.sample, Ftc532.keys, Ftc532.old_keys, Ftc532.errors, Ftc532.valid, Ftc532.rxtime, Pin(GPIO_FTC532)); -#endif // DEBUG_TASMOTA_DRIVER - ftc532_publish(); - Ftc532.old_keys = Ftc532.keys; +void ftc532_update(void) { // Usually called every 50 ms + uint16_t smp; + uint16_t i; + + while (i++ < FTC532_RETRY) { // fix 'ghost' keys from bad hardware + smp = Ftc532.sample; + if ((smp & 0xF0F0) != ((~smp & 0x0F0F) << 4)) { // inverted keys don't match + ++Ftc532.errors; +#ifdef DEBUG_FTC532 + AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: SAM=%04X"), smp); +#endif // DEBUG_FTC532 + } else { + Ftc532.keys = (smp & 0xF) | ((smp >> 4) & 0xF0); + if (Ftc532.keys != Ftc532.old_keys) { +#ifdef DEBUG_FTC532 + AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: SAM=%04X KEY=%02X OLD=%02X ERR=%u NOI=%u FRM=%u OK=%u TIME=%lu Pin=%u"), + Ftc532.sample, Ftc532.keys, Ftc532.old_keys, Ftc532.errors, Ftc532.noise, Ftc532.frame, Ftc532.valid, Ftc532.rxtime, Pin(GPIO_FTC532)); +#endif // DEBUG_FTC532 + ftc532_publish(); + Ftc532.old_keys = Ftc532.keys; + } + break; + } } } void ftc532_show() { - ResponseAppend_P(PSTR(",%s%02X\"}"), ftc532_json, Ftc532.keys); // Hex keys need JSON quotes + ResponseAppend_P(PSTR(",%s%02X\"}"), ftc532_json, Ftc532.keys); } void ftc532_publish(void) { - Response_P(PSTR("{%s%02X\"}}"), ftc532_json, Ftc532.keys); // Hex keys need JSON quotes + Response_P(PSTR("{%s%02X\"}}"), ftc532_json, Ftc532.keys); MqttPublishTeleSensor(); }