Expand description
Serial protocol for the DFR0299 MP3 player module
This crate provides zero-allocation, no_std-compatible
serialisation and deserialisation for the commands supported by the
DFR02999 MP3 player module.
Communication with the module is via UART at 9600-8-N-1.
§Features
std: implementstd::error::ErrorforErroruse_defmt: All types derive implementations ofdefmt::Formatto allow them to be formatted bydefmtwhen used on embedded devices
§Usage - serialisation
This example just demonstrates serialising commands into a buffer.
For implementations using mio_serial on Linux and a hardware UART
on an RP2040 microcontroller see
the github repo
use dfr0299::{Command};
let mut buf = [0u8; 10];
Command::Reset.serialise(&mut buf)?;
// do something with the buffer, e.g. write to a uart peripheral
Command::Track(1).serialise(&mut buf)?;
// send the buffer, e.g.
// uart.write_full_blocking(&buf)?;§Usage - parsing
For a concrete application, see the mio_serial example
on github
use dfr0299::{Parser, ParseResult};
fn process<R: std::io::Read>(mut uart: R) -> Result<(), Box<dyn std::error::Error>> {
let mut parser = Parser::new();
let mut buf = [0u8; 1];
loop {
uart.read_exact(&mut buf)?;
match parser.process_byte(buf[0]) {
Ok(ParseResult::Incomplete) => {}
Ok(ParseResult::Complete(msg)) => {
println!("Message received: {msg:?}");
}
Err(e) => {
println!("Parse error: {e}");
}
}
}
}§Packet structure
- START = 0x7e
- VERSION = 0xff
- LEN = number of bytes to follow, inc. LEN & VER, not inc. checksum
- CMD = the command
- FEEDBACK = whether to request ack/feedback
- PARAM_H = parameter high byte
- PARAM_L = parameter low byte
- CHECKSUM_H = checksum high byte
- CHECKSUM_L = checksum low byte
- STOP = 0xef
The checksum is the twos complement of the sum over the packet bytes (excluding START). Note that the example packets in the datasheet have incorrect checksums.
Structs§
- Parser
- Parser for the DFR0299 response messages. After initialising, calls
to
Parser::process_bytewill advance the internal state machine and return any complete messages.
Enums§
- Command
- Available commands supported by the DFR0299
- Disk
- Disk types that the device might report the status of. Note that
the definitions here are slightly different to those of
Control::PlaybackSource. - EqMode
- EQ presets supported by the device
- Error
- Error states for dfr0299. Includes errors for both serialisation parsing
- Module
Error Type - Possible error states reported by the device
- Parse
Result - After processing a byte the parser will return either
Incompleteto indicate that it requires more data orCompleteto indicate that a full message has been successfully processed - Playback
Mode - Repeat modes supported by the device
- Playback
Source - Input data sources supported by the device. I don’t know what
Sleepmeans here. - Request
Ack - Whether to request an ACK from the device
- Response
- Possible messages we may receive from the DFR0299.
Constants§
- START
- Packet start byte
- STOP
- Packet end byte
- VERSION
- Packet version field. This just seems to be hardcoded to
0xffand not actually used for anything
Type Aliases§
- Result
- Newtype wrapping this crate’s Error