-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a3040 eq info update response
- Loading branch information
Showing
7 changed files
with
100 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
pub use bass_up_update::*; | ||
pub use eq_info_update::*; | ||
pub use eq_update_command::*; | ||
Check warning on line 3 in soundcore-lib/src/devices/a3040.rs GitHub Actions / Run cargo check
|
||
pub use features::*; | ||
pub use sound_mode_update_command::*; | ||
|
||
mod bass_up_update; | ||
mod eq_info_update; | ||
mod eq_update_command; | ||
mod features; | ||
mod sound_mode_update_command; | ||
|
||
pub use bass_up_update::*; | ||
pub use eq_update_command::*; | ||
pub use features::*; | ||
pub use sound_mode_update_command::*; | ||
pub use eq_info_update::*; | ||
Check warning on line 13 in soundcore-lib/src/devices/a3040.rs GitHub Actions / Run cargo check
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use log::warn; | ||
use nom::combinator::map; | ||
use nom::error::context; | ||
use nom::number::complete::le_u8; | ||
use nom::sequence::tuple; | ||
use strum::EnumCount; | ||
use crate::models::EQProfile; | ||
use crate::parsers::{ParseError, ParseResult}; | ||
|
||
pub fn parse_a3040_eq_info_update<'a, E: ParseError<'a>>(bytes: &'a [u8]) -> ParseResult<EQProfile, E> { | ||
context( | ||
"parse_a3040_eq_info_update", | ||
map(tuple((le_u8, le_u8)), |(b1, b2)| { | ||
let eq_idx = ((b1 & 0xFF) as u16) | ||
| (((b2 & 0xFF) as u16) << 8).clamp(0, EQProfile::COUNT as u16); | ||
match EQProfile::from_id_le(eq_idx) { | ||
Some(eq) => eq, | ||
None => { | ||
warn!("Unknown EQ profile index from eq info update: {}", eq_idx); | ||
EQProfile::SoundcoreSignature | ||
}, | ||
} | ||
}), | ||
)(bytes) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
pub fn should_parse_eq_info_update() { | ||
let bytes = &[0x01, 0x00]; | ||
let result = parse_a3040_eq_info_update::<nom::error::VerboseError<&[u8]>>(bytes); | ||
assert_eq!(result, Ok((&[][..], EQProfile::Acoustic))); | ||
} | ||
|
||
#[test] | ||
pub fn should_default_if_index_is_out_of_bounds() { | ||
let bytes = &[0xFF, 0x00]; | ||
let result = parse_a3040_eq_info_update::<nom::error::VerboseError<&[u8]>>(bytes); | ||
assert_eq!(result, Ok((&[][..], EQProfile::SoundcoreSignature))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use nom::combinator::map; | ||
use nom::error::context; | ||
|
||
use crate::api::SoundcoreDeviceState; | ||
use crate::devices::parse_a3040_eq_info_update; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::models::EQProfile; | ||
use crate::packets::StateTransformationPacket; | ||
use crate::parsers::{ParseError, ParseResult}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)] | ||
pub struct EqInfoUpdate(pub EQProfile); | ||
|
||
pub fn parse_eq_info_update<'a, E: ParseError<'a>>( | ||
bytes: &'a [u8], | ||
) -> ParseResult<EqInfoUpdate, E> { | ||
context( | ||
"parse_eq_info_update", | ||
map(parse_a3040_eq_info_update, EqInfoUpdate), | ||
)(bytes) | ||
} | ||
|
||
impl StateTransformationPacket for EqInfoUpdate { | ||
fn transform_state(self, state: &SoundcoreDeviceState) -> SoundcoreDeviceState { | ||
let mut state = state.clone(); | ||
state.eq_configuration.set_profile(self.0); | ||
state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters