more diagnostics

This commit is contained in:
Vic 2021-01-04 14:57:32 +01:00
parent cd77945e28
commit 24cbad3257
2 changed files with 58 additions and 41 deletions

Binary file not shown.

View File

@ -54,105 +54,122 @@
#define XDRV_47 47 #define XDRV_47 47
#define FTC532_KEYS_MAX 8 #define FTC532_KEYS_MAX 8
#define FTC532_RETRY 8 // number of reads before discarding
#define FTC532_STATE_WAITING false #define FTC532_STATE_WAITING false
#define FTC532_STATE_READING true #define FTC532_STATE_READING true
// Rising edge timing in microseconds // Rising edge timing in microseconds
#define FTC532_BIT 377 #define FTC532_BIT 377
#define FTC532_NOISE (FTC532_BIT * 3 / 2)
#define FTC532_SHORT (FTC532_BIT * 2) #define FTC532_SHORT (FTC532_BIT * 2)
#define FTC532_LONG (FTC532_BIT * 4) #define FTC532_LONG (FTC532_BIT * 4)
#define FTC532_IDLE (FTC532_BIT * 10) #define FTC532_IDLE (FTC532_BIT * 10)
#define FTC532_MAX (FTC532_BIT * 58) #define FTC532_MAX (FTC532_BIT * 58)
#define DEBUG_FTC532 //@@@@@@@@@@@@@@@@
struct FTC532 { struct FTC532 {
volatile uint32_t rxtime; // ISR timer memory 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 volatile uint16_t rxbit; // ISR bit counter
uint8_t keys = 0; // bitmap of active keys uint8_t keys = 0; // bitmap of active keys
uint8_t old_keys = 0; // previously active keys uint8_t old_keys = 0; // previously active keys
volatile bool state; // ISR state volatile bool state; // ISR state
bool present = false; bool present = false; // driver active
#ifdef DEBUG_TASMOTA_DRIVER #ifdef DEBUG_FTC532
volatile uint16_t errors; // error counter volatile uint16_t errors = 0; // inv. key error counter
volatile bool valid; // did we ever receive valid data? volatile uint16_t frame = 0; // frame error counter
#endif // DEBUG_TASMOTA_DRIVER volatile uint16_t noise = 0; // noise detection counter
volatile bool valid = 0; // did we ever receive valid data?
#endif // DEBUG_FTC532
} Ftc532; } Ftc532;
const char ftc532_json[] PROGMEM = "\"FTC532\":{\"KEYS\":\""; 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 = micros();
uint32_t time_diff = time - Ftc532.rxtime; uint32_t time_diff = time - Ftc532.rxtime;
Ftc532.rxtime = time; Ftc532.rxtime = time;
if (Ftc532.state == FTC532_STATE_WAITING) { 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.rxbit = 0;
Ftc532.state = FTC532_STATE_READING; Ftc532.state = FTC532_STATE_READING;
} }
return; return;
} // FTC532_STATE_READING starts here } // FTC532_STATE_READING starts here
if (time_diff > FTC532_LONG + FTC532_BIT) { if (time_diff > FTC532_LONG + FTC532_BIT) {
#ifdef DEBUG_TASMOTA_DRIVER #ifdef DEBUG_FTC532
++Ftc532.errors; // frame error ++Ftc532.frame; // frame error
#endif // DEBUG_TASMOTA_DRIVER #endif // DEBUG_FTC532
Ftc532.state = FTC532_STATE_WAITING; Ftc532.state = FTC532_STATE_WAITING;
return; return;
} }
if (time_diff > FTC532_SHORT + FTC532_BIT) { 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 { } else {
Ftc532.sample &= ~(1 << Ftc532.rxbit); // SHORT Ftc532.tsmp &= ~(1 << Ftc532.rxbit); // SHORT
} }
++Ftc532.rxbit; ++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; Ftc532.rxbit = 0;
#ifdef DEBUG_TASMOTA_DRIVER
Ftc532.valid = true;
#endif // DEBUG_TASMOTA_DRIVER
Ftc532.state = FTC532_STATE_WAITING; 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; } 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.state = FTC532_STATE_WAITING;
Ftc532.rxtime = micros();
pinMode(Pin(GPIO_FTC532), INPUT_PULLUP); pinMode(Pin(GPIO_FTC532), INPUT_PULLUP);
Ftc532.rxtime = micros();
attachInterrupt(Pin(GPIO_FTC532), ftc532_ISR, RISING); attachInterrupt(Pin(GPIO_FTC532), ftc532_ISR, RISING);
Ftc532.present = true; Ftc532.present = true;
} }
void ftc532_update(void) { // Usually called every 50 ms void ftc532_update(void) { // Usually called every 50 ms
#ifdef DEBUG_TASMOTA_DRIVER uint16_t smp;
// WARNING: Reduce callback frequency if this code is enabled uint16_t i;
// 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"), while (i++ < FTC532_RETRY) { // fix 'ghost' keys from bad hardware
// Ftc532.sample & 0xF, (~Ftc532.sample >> 4) & 0xF, (Ftc532.sample >> 8) & 0xF, (~Ftc532.sample >> 12) & 0xF); smp = Ftc532.sample;
// } if ((smp & 0xF0F0) != ((~smp & 0x0F0F) << 4)) { // inverted keys don't match
#endif // DEBUG_TASMOTA_DRIVER ++Ftc532.errors;
Ftc532.keys = (Ftc532.sample & 0xF) | ((Ftc532.sample >> 4) & 0xF0); #ifdef DEBUG_FTC532
if (Ftc532.keys != Ftc532.old_keys) { AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: SAM=%04X"), smp);
#ifdef DEBUG_TASMOTA_DRIVER #endif // DEBUG_FTC532
AddLog_P(LOG_LEVEL_DEBUG, PSTR("FTC: SAM=%04X KEY=%02X OLD=%02X ERR=%u OK=%u TIME=%lu Pin=%u"), } else {
Ftc532.sample, Ftc532.keys, Ftc532.old_keys, Ftc532.errors, Ftc532.valid, Ftc532.rxtime, Pin(GPIO_FTC532)); Ftc532.keys = (smp & 0xF) | ((smp >> 4) & 0xF0);
#endif // DEBUG_TASMOTA_DRIVER if (Ftc532.keys != Ftc532.old_keys) {
ftc532_publish(); #ifdef DEBUG_FTC532
Ftc532.old_keys = Ftc532.keys; 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() { 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) { 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(); MqttPublishTeleSensor();
} }