$ show kitschpatrol/brain
This is an Arduino library for parsing serial data packets from NeuroSky-based EEG headsets (Star Wars Force Trainer, Mattel MindFlex). The Brain class implements a complete packet protocol parser with sync byte detection (0xAA, 0xAA), length validation, checksum verification, and data extraction. It exposes signal quality, attention, meditation values, and 8 EEG power bands through getter methods and CSV output. The library is designed for embedded systems with limited memory, using fixed-size buffers and Stream-based interfaces compatible with both HardwareSerial and SoftwareSerial. The architecture is a single monolithic class with no external dependencies beyond the Arduino framework. Data flows from serial input through packet parsing to either CSV string output or individual getter methods. The code demonstrates solid embedded engineering practices for its domain, including proper state management, error handling, and protocol compliance. However, it uses dated C++ patterns (char* instead of std::string, boolean instead of bool) and fixed buffers that could overflow with malformed input. This library is well-suited for hobbyist brain-computer interface projects using legacy NeuroSky hardware. It's not suitable for modern Arduino versions without modification, and the hardware it targets is aging. The code is clean and reusable but lacks tests and modern safety features.
The LLM identified 3 files worth highlighting for reuse:
The Brain class implements a robust serial packet parser for the NeuroSky protocol. Key reusable patterns include: sync byte detection (0xAA, 0xAA) for packet boundaries, length-based packet validation with MAX_PACKET_LENGTH protection, checksum verification (255 - accumulator), and state machine for packet assembly. The parsePacket() method cleanly extracts structured data from raw bytes using a switch statement on packet type codes. This could be extracted and adapted for other serial protocol implementations with minimal modification.
The header defines a clean Stream-based interface that decouples the parser from specific serial implementations. The class can work with HardwareSerial, SoftwareSerial, or any Stream subclass. Getter methods follow a consistent naming pattern (readSignalQuality, readAttention, etc.) making the API predictable and discoverable. The private state is well-encapsulated with clear separation between parsing state and exposed data.
A well-structured example demonstrating the sensor feedback loop pattern: read sensor data, transform it (attention value to LED interval), and act on it. Uses non-blocking timing (millis-based) instead of delay(), which is critical for responsive embedded systems. The example shows proper error checking (signal quality validation) before acting on sensor data.