2026-10-13 00:34:11 +00:00

23 lines
669 B
Dart

import 'dart:typed_data';
/// Calculates the NEG8 checksum for a buffer.
/// NEG8: start at 0, subtract each byte, keep lower 8 bits.
/// The result should match the last byte of a valid frame.
int calculateNeg8(Uint8List buf) {
int neg8 = 0;
for (int i = 0; i < buf.length; i++) {
neg8 = (neg8 - buf[i]) & 0xFF;
}
return neg8;
}
/// Returns true if the frame's last byte equals the NEG8 of all preceding bytes.
bool validateFrame(Uint8List frame) {
if (frame.isEmpty) return false;
int neg8 = 0;
for (int i = 0; i < frame.length - 1; i++) {
neg8 = (neg8 - frame[i]) & 0xFF;
}
return neg8 == frame[frame.length - 1];
}