diff --git a/src/matrix.c b/src/matrix.c index 468b465..e8e7ebd 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -6,53 +6,54 @@ #include -#define MATRIX_LETTER_TRAIL 4 - typedef struct MatrixLetter { - Lfsr *lfsr; int8_t row; + int8_t trail_row; uint8_t column; } MatrixLetter; typedef struct MatrixContext { Lfsr lfsr; + uint8_t trail; MatrixLetter letters[80]; } MatrixContext; -static MatrixContext ctx; +static MatrixContext mctx; -static void matrixLetter_init(MatrixLetter *ctx, Lfsr *lfsr) { - ctx->lfsr = lfsr; - ctx->column = lfsr_next(ctx->lfsr) % 80; - ctx->row = lfsr_next(ctx->lfsr) % 50 - 50; +static void matrixLetter_init(MatrixLetter *ctx) { + ctx->column = lfsr_next(&mctx.lfsr) % 80; + ctx->row = lfsr_next(&mctx.lfsr) % 50 - 50; + ctx->trail_row = ctx->row - (lfsr_next(&mctx.lfsr) % mctx.trail + 3); } static void matrixLetter_poll(MatrixLetter *ctx) { ctx->row++; + ctx->trail_row++; if (ctx->row < 0) return; - const uint8_t c = lfsr_next(ctx->lfsr) % 256; + const uint8_t c = lfsr_next(&mctx.lfsr) % 256; video_put(ctx->column, ctx->row, c); - if (ctx->row < MATRIX_LETTER_TRAIL) + if (ctx->trail_row < 0) return; - video_put(ctx->column, ctx->row - MATRIX_LETTER_TRAIL, ' '); + video_put(ctx->column, ctx->trail_row, ' '); } -void matrix_init(void) { - lfsr_init(&ctx.lfsr, 1); +void matrix_init(uint8_t trail) { + lfsr_init(&mctx.lfsr, 1); + mctx.trail = trail; // using the same LFSR for different letters makes the randomness // not reproducible in case we decide to refactor the number of letters, // but that's not an issue we care about at the moment - for (size_t i = 0; i < countof(ctx.letters); ++i) { - matrixLetter_init(&ctx.letters[i], &ctx.lfsr); + for (size_t i = 0; i < countof(mctx.letters); ++i) { + matrixLetter_init(&mctx.letters[i]); } } void matrix_poll(void) { - for (size_t i = 0; i < countof(ctx.letters); ++i) { - matrixLetter_poll(&ctx.letters[i]); + for (size_t i = 0; i < countof(mctx.letters); ++i) { + matrixLetter_poll(&mctx.letters[i]); } -} \ No newline at end of file +} diff --git a/src/matrix.h b/src/matrix.h index 26088e6..32e343e 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -1,7 +1,9 @@ #ifndef CEDA_MATRIX_H #define CEDA_MATRIX_H -void matrix_init(void); +#include + +void matrix_init(uint8_t trail); void matrix_poll(void); #endif // CEDA_MATRIX_H