Files
Flipdot-display/lib/BROSE9323/buffer_crc.h
T

25 lines
958 B
C
Raw Normal View History

#pragma once
#include <stdint.h>
// CRC-16/CCITT-FALSE of a display buffer as BROSE9323 keeps it: rows of (width + 7) / 8
// bytes, bit (x & 7) is dot x. The unused bits at the end of each row are left out, so
// only the dots count (fillScreen sets them, drawPixel never does). The Nano answers the
// C command with it; the ESP32 compares it with its own copy (src/esp32/nano_link).
inline uint16_t bufferCrc(const uint8_t *buffer, uint8_t width, uint8_t height)
{
const uint8_t row_bytes = (width + 7) / 8;
const uint8_t last_mask = (width & 7) ? (uint8_t)((1 << (width & 7)) - 1) : 0xFF;
uint16_t crc = 0xFFFF;
for (uint8_t y = 0; y < height; y++)
for (uint8_t i = 0; i < row_bytes; i++)
{
uint8_t b = buffer[y * row_bytes + i];
if (i == row_bytes - 1)
b &= last_mask;
crc ^= (uint16_t)b << 8;
for (uint8_t k = 0; k < 8; k++)
crc = (crc & 0x8000) ? (uint16_t)((crc << 1) ^ 0x1021) : (uint16_t)(crc << 1);
}
return crc;
}