Skip to content

Commit 4cc3dd2

Browse files
authored
Merge pull request #22 from rust-embedded-community/next
Version 0.5.0
2 parents 69e1e1e + f731037 commit 4cc3dd2

File tree

11 files changed

+320
-33
lines changed

11 files changed

+320
-33
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [0.5.0] - 2025-01-17
9+
10+
This release focuses on improving the internal message data types and their usage.
11+
12+
### Added
13+
14+
- `Message` enum variants for *System Common* and *System Realtime* messages.
15+
- `U14` primitive value type used by *Pitch Wheel* and *Song Position Pointer* messages.
16+
- Derive `Debug`, `Clone`, `Eq`, and `PartialEq` for `U4`.
17+
- Derive `Debug`, `Clone`, `Eq`, and `PartialEq` for `InvalidU4`.
18+
- Derive `Debug`, `Clone`, `Eq`, and `PartialEq` for `InvalidU7`.
19+
- Derive `Debug`, `Clone`, `Eq`, and `PartialEq` for `Payload`.
20+
- Derive `Debug`, `Clone`, `Eq`, and `PartialEq` for `Raw`.
21+
- Re-exports of common data types in the `message` module.
22+
23+
### Changed
24+
25+
- Changed pitch wheel `Message` variant from `PitchWheelChange(Channel, U7, U7)` to `PitchWheelChange(Channel, U14)`. Use `U14::from_split_u7` and `U14::split_u7` functions for conversions.
26+
827
## [0.4.0] - 2025-01-03
928

1029
This release focuses on:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[package]
22
name = "usbd-midi"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
authors = [
55
"Beau Trepp <[email protected]>",
66
"Florian Jung <[email protected]>",
77
"Oliver Rockstedt <[email protected]>",
88
]
99
edition = "2021"
10-
description = "A USB MIDI implementation for usb-device."
10+
rust-version = "1.78"
11+
description = "USB MIDI device class implementation for use with usb-device."
1112
homepage = "https://github.com/rust-embedded-community/usbd-midi"
1213
repository = "https://github.com/rust-embedded-community/usbd-midi"
1314
license = "MIT"

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
# usbd-midi
22

3-
A simple USB MIDI device class for [usb-device](https://crates.io/crates/usb-device).
3+
A USB MIDI device class implementation for [usb-device](https://crates.io/crates/usb-device) based on the [USB Device Class Definition for MIDI Devices](https://www.usb.org/sites/default/files/midi10.pdf) specification.
44

5-
Currently this aims to be a very simple implementation, that allows the microcontroller to send or receive MIDI information to/from a host like a desktop computer.
5+
This class allows the device to exchange MIDI messages with a host like a desktop computer. It requires the use of a driver (e.g. a HAL) that implements the `usb-device` traits.
66

7-
This crate requires the use of a HAL that implements the `usb-device` traits.
7+
**NOTE:** only MIDI 1.0 protocol is currently supported.
88

9-
## Example
9+
## Message Types
10+
11+
While the crate focuses on transfer functionality, it provides some basic message types with conversions for convenience. These types are gated behind a `message-types` feature, which is enabled by default.
12+
13+
For more complex use cases, it is recommended to use a specialized crate like [midi-types](https://crates.io/crates/midi-types) or [wmidi](https://crates.io/crates/wmidi) and interface with it by using the raw event packet bytes. The [ESP32-S3 example](examples/example-esp32s3/) shows how to do this in detail.
14+
15+
## Examples
16+
17+
The example below shows some basic usage without any platform-dependent parts. Please refer to the [examples](examples/) directory for code that can be run on real hardware.
1018

1119
### Receive MIDI
1220

13-
Turn on an LED as long as note C2 is pressed. The example only shows the hardware-independent parts.
21+
Turn on an LED as long as note C2 is pressed.
1422

1523
```rust ignore
1624
use usb_device::prelude::*;
1725
use usbd_midi::{
18-
message::{channel::Channel, notes::Note},
19-
Message,
26+
message::{Message, Channel, Note},
2027
UsbMidiClass,
2128
UsbMidiPacketReader,
2229
};

examples/example-esp32s3/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
esp-hal = { version = "0.22", features = ["esp32s3"] }
8-
esp-backtrace = { version = "0.14.2", features = [
7+
esp-hal = { version = "0.23", features = ["esp32s3"] }
8+
esp-backtrace = { version = "0.15.0", features = [
99
"esp32s3",
1010
"panic-handler",
1111
"println",
1212
] }
13-
esp-println = { version = "0.12.0", features = ["esp32s3", "log"] }
13+
esp-println = { version = "0.13.0", features = ["esp32s3", "log"] }
1414
usb-device = { version = "0.3.2", features = ["control-buffer-256"] }
1515
usbd-midi = { path = "../../" }
1616
midi-convert = "0.2.0"

examples/example-esp32s3/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const SYSEX_BUFFER_SIZE: usize = 64;
2323
fn main() -> ! {
2424
// Some basic setup to run the MCU at maximum clock speed.
2525
let mut config = Config::default();
26-
config.cpu_clock = clock::CpuClock::Clock240MHz;
26+
config.cpu_clock = clock::CpuClock::_240MHz;
2727
let peripherals = esp_hal::init(config);
2828

2929
let usb_bus_allocator = otg_fs::UsbBus::new(

src/message/data/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Primitives and structures used in USB MIDI data.
22
3+
pub mod u14;
34
pub mod u4;
45
pub mod u7;
56

src/message/data/u14.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//! A primitive value with 14-bit length.
2+
3+
use crate::message::data::{u7::U7, FromClamped, FromOverFlow};
4+
5+
/// A primitive value that can be from 0-0x4000
6+
#[derive(Debug, Clone, Eq, PartialEq)]
7+
pub struct U14(pub(crate) u16);
8+
9+
/// Error representing that this value is not a valid u14
10+
#[derive(Debug, Clone, Eq, PartialEq)]
11+
pub struct InvalidU14(pub u16);
12+
13+
impl TryFrom<u16> for U14 {
14+
type Error = InvalidU14;
15+
16+
fn try_from(value: u16) -> Result<Self, Self::Error> {
17+
if value > 0x3FFF {
18+
Err(InvalidU14(value))
19+
} else {
20+
Ok(U14(value))
21+
}
22+
}
23+
}
24+
25+
impl From<U14> for u16 {
26+
fn from(value: U14) -> u16 {
27+
value.0
28+
}
29+
}
30+
31+
impl FromOverFlow<u16> for U14 {
32+
fn from_overflow(value: u16) -> U14 {
33+
const MASK: u16 = 0b0011_1111_1111_1111;
34+
let value = MASK & value;
35+
U14(value)
36+
}
37+
}
38+
39+
impl FromClamped<u16> for U14 {
40+
fn from_clamped(value: u16) -> U14 {
41+
match U14::try_from(value) {
42+
Ok(x) => x,
43+
_ => U14::MAX,
44+
}
45+
}
46+
}
47+
48+
impl U14 {
49+
/// Maximum value for the type.
50+
pub const MAX: U14 = U14(0x3FFF);
51+
/// Minimum value for the type.
52+
pub const MIN: U14 = U14(0);
53+
54+
/// Creates a new U14 value from an (U7, U7) tuple containing the LSB and MSB.
55+
pub fn from_split_u7(value: (U7, U7)) -> Self {
56+
Self((value.0 .0 as u16) | ((value.1 .0 as u16) << 7))
57+
}
58+
59+
/// Returns the LSB and MSB of the value as (U7, U7) tuple.
60+
pub fn split_u7(&self) -> (U7, U7) {
61+
(U7((self.0 & 0x7F) as u8), U7((self.0 >> 7) as u8))
62+
}
63+
}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn try_from_valid() {
71+
assert_eq!(U14::try_from(0x1234), Ok(U14(0x1234)));
72+
}
73+
74+
#[test]
75+
fn try_from_invalid() {
76+
assert_eq!(U14::try_from(0x4000), Err(InvalidU14(0x4000)));
77+
}
78+
79+
#[test]
80+
fn from_overflow() {
81+
assert_eq!(U14::from_overflow(0x400F), U14(0x0F));
82+
}
83+
84+
#[test]
85+
fn from_clamped() {
86+
assert_eq!(U14::from_clamped(0x400F), U14(0x3FFF));
87+
}
88+
89+
#[test]
90+
fn from_split_u7() {
91+
assert_eq!(U14::from_split_u7((U7(0x7F), U7(0x6F))), U14(0x37FF));
92+
}
93+
94+
#[test]
95+
fn split_u7() {
96+
assert_eq!(U14(0x37FF).split_u7(), (U7(0x7F), U7(0x6F)));
97+
}
98+
}

src/message/data/u4.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
use crate::message::data::{FromClamped, FromOverFlow};
44

55
/// A primitive value that can be from 0-0x0F
6+
#[derive(Debug, Clone, Eq, PartialEq)]
67
pub struct U4(u8);
78

89
/// Error representing that this value is not a valid u4
10+
#[derive(Debug, Clone, Eq, PartialEq)]
911
pub struct InvalidU4(pub u8);
1012

1113
impl TryFrom<u8> for U4 {

src/message/data/u7.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::message::data::{FromClamped, FromOverFlow};
77
pub struct U7(pub(crate) u8);
88

99
/// Error representing that this value is not a valid u7
10+
#[derive(Debug, Clone, Eq, PartialEq)]
1011
pub struct InvalidU7(pub u8);
1112

1213
impl TryFrom<u8> for U7 {

0 commit comments

Comments
 (0)