diff --git a/firmware/libs/ir/recv/src/ir_recv.c b/firmware/libs/ir/recv/src/ir_recv.c index eed8762..c7ce882 100644 --- a/firmware/libs/ir/recv/src/ir_recv.c +++ b/firmware/libs/ir/recv/src/ir_recv.c @@ -136,44 +136,56 @@ static void process_ir_sample(ir_ctx_t *ctx, int16_t raw) ctx->state = IR_STATE_WAIT_HEADER_GAP; break; - case IR_STATE_WAIT_HEADER_GAP: +case IR_STATE_WAIT_HEADER_GAP: if (!active) { - // Header-Puls ist vorbei, wir sind im Gap + // Wir sind jetzt im Gap nach dem Header. + // Wir wechseln in einen Modus, der auf den Start des ersten Bits (Mark) wartet. ctx->state = IR_STATE_WAIT_MARK; ctx->bit_count = 0; ctx->bit_acc = 0; - ctx->timer = 0; - // LOG_DBG removed to save timing + ctx->timer = 0; } break; case IR_STATE_WAIT_MARK: if (active) { - // Rising edge: Space is over. Measure it! - // threshold between 8 and 16 is ~12 - bool bit = (ctx->timer >= 12); - - /* Right-shift: The FIRST bit received becomes Bit 0 of the result - * after 24 shifts. */ - ctx->bit_acc = (ctx->bit_acc >> 1) | (bit ? (1 << 23) : 0); - - LOG_DBG("Bit %u: %u (T:%u)", ctx->bit_count, bit, ctx->timer); - - if (++ctx->bit_count >= 24) + // Steigende Flanke: Das Space (die Pause) ist vorbei. + // Jetzt bewerten wir die Länge der Pause (Timer). + if (ctx->timer >= 4) // Mindestlänge für Rauschunterdrückung { - ctx->state = IR_STATE_VALIDATE; + bool bit = (ctx->timer >= 12); // Schwelle zwischen 8 und 16 Samples + + /* Rechtsschub: Das erste empfangene Bit landet nach 24 Schüben an Bit 0 */ + ctx->bit_acc = (ctx->bit_acc >> 1) | (bit ? (1 << 23) : 0); + + LOG_DBG("Bit %u: %d (T:%u)", ctx->bit_count, bit, ctx->timer); + + if (++ctx->bit_count >= 24) + { + ctx->state = IR_STATE_VALIDATE; + } + else + { + ctx->state = IR_STATE_WAIT_SPACE; + } + } + else if (ctx->bit_count == 0 && ctx->timer == 0) + { + // Sonderfall: Erster Puls nach dem Header-Gap. s + // Wir fangen gerade erst an zu zählen. Nichts tun. } else { - ctx->state = IR_STATE_WAIT_SPACE; + // Zu kurz -> Rauschen + ctx->state = IR_STATE_IDLE; } ctx->timer = 0; } else { - ctx->timer++; // Counting space length + ctx->timer++; // Wir zählen die Samples der Pause } break;