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: implement std::error::Error for Error
  • use_defmt: All types derive implementations of defmt::Format to allow them to be formatted by defmt when 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 for the DFR0299 response messages. After initialising, calls to Parser::process_byte will advance the internal state machine and return any complete messages.

Enums

Available commands supported by the DFR0299

Disk types that the device might report the status of. Note that the definitions here are slightly different to those of Control::PlaybackSource.

EQ presets supported by the device

Error states for dfr0299. Includes errors for both serialisation parsing

Possible error states reported by the device

After processing a byte the parser will return either Incomplete to indicate that it requires more data or Complete to indicate that a full message has been successfully processed

Repeat modes supported by the device

Input data sources supported by the device. I don’t know what Sleep means here.

Whether to request an ACK from the device

Possible messages we may receive from the DFR0299.

Constants

Packet start byte

Packet end byte

Packet version field. This just seems to be hardcoded to 0xff and not actually used for anything

Type Definitions

Newtype wrapping this crate’s Error