@@ -0,0 +1,47 @@
|
|||||||
|
name: CI
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Rust CI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Rust
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
profile: minimal
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
components: rustfmt, clippy
|
||||||
|
|
||||||
|
- name: Cache Cargo registry
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
|
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-cargo-
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
env:
|
||||||
|
SQLX_OFFLINE: true
|
||||||
|
run: cargo test --workspace --verbose --all
|
||||||
|
|
||||||
|
- name: Run Clippy (linter)
|
||||||
|
run: cargo clippy --all-targets --all-features -- -D warnings
|
||||||
|
|
||||||
|
- name: Run fmt check
|
||||||
|
run: cargo fmt --all -- --check
|
||||||
|
|
||||||
|
- name: Build project
|
||||||
|
run: cargo build --workspace --verbose
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
repos:
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
# ─────────────── PRE-COMMIT ───────────────
|
||||||
|
|
||||||
|
- id: fmt
|
||||||
|
name: rustfmt
|
||||||
|
entry: cargo fmt -- --check
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-commit]
|
||||||
|
|
||||||
|
- id: clippy
|
||||||
|
name: clippy
|
||||||
|
entry: cargo clippy -- -D warnings
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-commit]
|
||||||
|
|
||||||
|
# ─────────────── PRE-PUSH ───────────────
|
||||||
|
|
||||||
|
- id: test
|
||||||
|
name: cargo test
|
||||||
|
entry: cargo test --all
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-push]
|
||||||
|
|
||||||
|
- id: build
|
||||||
|
name: cargo build release
|
||||||
|
entry: cargo build --release
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-push]
|
||||||
|
|
||||||
|
- id: audit
|
||||||
|
name: cargo audit
|
||||||
|
entry: cargo audit
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-push]
|
||||||
@@ -1 +0,0 @@
|
|||||||
# verify import order
|
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright 2026 Iceberg
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "stable"
|
||||||
|
components = ["rustfmt", "clippy"]
|
||||||
+8
-48
@@ -66,78 +66,38 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_range_rejects_out_of_range() {
|
fn validate_range_rejects_out_of_range() {
|
||||||
let err = validate_range(11u8, 0..=10, || "bad value".into())
|
let err = validate_range(11u8, 0..=10, || "bad value".into()).unwrap_err();
|
||||||
.unwrap_err();
|
|
||||||
|
|
||||||
assert!(err.to_string().contains("bad value"));
|
assert!(err.to_string().contains("bad value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_ascii_padded_accepts_ascii_and_padding() {
|
fn validate_ascii_padded_accepts_ascii_and_padding() {
|
||||||
assert!(validate_ascii_padded(
|
assert!(validate_ascii_padded(b"ABC ", 4, b' ', "name",).is_ok());
|
||||||
b"ABC ",
|
|
||||||
4,
|
|
||||||
b' ',
|
|
||||||
"name",
|
|
||||||
)
|
|
||||||
.is_ok());
|
|
||||||
|
|
||||||
assert!(validate_ascii_padded(
|
assert!(validate_ascii_padded(&[b'A', 0, 0, 0], 4, 0, "password",).is_ok());
|
||||||
&[b'A', 0, 0, 0],
|
|
||||||
4,
|
|
||||||
0,
|
|
||||||
"password",
|
|
||||||
)
|
|
||||||
.is_ok());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_ascii_padded_rejects_wrong_length() {
|
fn validate_ascii_padded_rejects_wrong_length() {
|
||||||
assert!(validate_ascii_padded(
|
assert!(validate_ascii_padded(b"ABC", 4, b' ', "name",).is_err());
|
||||||
b"ABC",
|
|
||||||
4,
|
|
||||||
b' ',
|
|
||||||
"name",
|
|
||||||
)
|
|
||||||
.is_err());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_ascii_padded_rejects_non_ascii() {
|
fn validate_ascii_padded_rejects_non_ascii() {
|
||||||
assert!(validate_ascii_padded(
|
assert!(validate_ascii_padded(&[0xFF, b'A', b'B', b'C'], 4, b' ', "name",).is_err());
|
||||||
&[0xFF, b'A', b'B', b'C'],
|
|
||||||
4,
|
|
||||||
b' ',
|
|
||||||
"name",
|
|
||||||
)
|
|
||||||
.is_err());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_bitmask_accepts_valid_bits() {
|
fn validate_bitmask_accepts_valid_bits() {
|
||||||
assert!(validate_bitmask(
|
assert!(validate_bitmask(0b0101, 0b1111, || "".into(),).is_ok());
|
||||||
0b0101,
|
|
||||||
0b1111,
|
|
||||||
|| "".into(),
|
|
||||||
)
|
|
||||||
.is_ok());
|
|
||||||
|
|
||||||
assert!(validate_bitmask(
|
assert!(validate_bitmask(0, 0b1111, || "".into(),).is_ok());
|
||||||
0,
|
|
||||||
0b1111,
|
|
||||||
|| "".into(),
|
|
||||||
)
|
|
||||||
.is_ok());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_bitmask_rejects_invalid_bits() {
|
fn validate_bitmask_rejects_invalid_bits() {
|
||||||
let err = validate_bitmask(
|
let err = validate_bitmask(0b10000, 0b01111, || "invalid mask".into()).unwrap_err();
|
||||||
0b10000,
|
|
||||||
0b01111,
|
|
||||||
|| "invalid mask".into(),
|
|
||||||
)
|
|
||||||
.unwrap_err();
|
|
||||||
|
|
||||||
assert!(err.to_string().contains("invalid mask"));
|
assert!(err.to_string().contains("invalid mask"));
|
||||||
}
|
}
|
||||||
|
|||||||
+89
-318
@@ -14,14 +14,10 @@ use crate::{
|
|||||||
dsp408::{
|
dsp408::{
|
||||||
mapper::{
|
mapper::{
|
||||||
self,
|
self,
|
||||||
constants::{PRESET_INDEX_MAX, CONFIG_CHUNK_INDEX_MAX},
|
constants::{CONFIG_CHUNK_INDEX_MAX, PRESET_INDEX_MAX},
|
||||||
response::map_response,
|
response::map_response,
|
||||||
},
|
},
|
||||||
protocol::{
|
protocol::{commands, config::decode_config, frame::extract_frame},
|
||||||
commands,
|
|
||||||
config::decode_config,
|
|
||||||
frame::extract_frame,
|
|
||||||
},
|
|
||||||
types,
|
types,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -44,33 +40,21 @@ pub struct DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DSP408 {
|
impl DSP408 {
|
||||||
|
pub fn new(host: IpAddr, port: u16, device_id: u8) -> Result<Self, DSPError> {
|
||||||
pub fn new(
|
|
||||||
host: IpAddr,
|
|
||||||
port: u16,
|
|
||||||
device_id: u8,
|
|
||||||
) -> Result<Self, DSPError> {
|
|
||||||
|
|
||||||
if matches!(host, IpAddr::V6(_)) {
|
if matches!(host, IpAddr::V6(_)) {
|
||||||
return Err(DSPError::InvalidHost(
|
return Err(DSPError::InvalidHost("DSP doesn't support IPv6"));
|
||||||
"DSP doesn't support IPv6"
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
return Err(DSPError::InvalidPort(
|
return Err(DSPError::InvalidPort("Port must be between 1 and 65535"));
|
||||||
"Port must be between 1 and 65535"
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if device_id == 0 || device_id > 254 {
|
if device_id == 0 || device_id > 254 {
|
||||||
return Err(DSPError::InvalidDeviceId(
|
return Err(DSPError::InvalidDeviceId(
|
||||||
"Device ID must be between 1 and 254"
|
"Device ID must be between 1 and 254",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
connection: types::DeviceConnection {
|
connection: types::DeviceConnection {
|
||||||
host,
|
host,
|
||||||
@@ -88,83 +72,43 @@ impl DSP408 {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_raw(
|
fn send_raw(&mut self, data: &[u8]) -> Result<(), DSPError> {
|
||||||
&mut self,
|
let _guard = self.lock.lock().unwrap();
|
||||||
data: &[u8],
|
|
||||||
) -> Result<(), DSPError> {
|
|
||||||
|
|
||||||
|
|
||||||
let _guard =
|
|
||||||
self.lock.lock().unwrap();
|
|
||||||
|
|
||||||
|
|
||||||
let socket =
|
|
||||||
self.socket
|
|
||||||
.as_mut()
|
|
||||||
.ok_or(
|
|
||||||
DSPError::NotConnected
|
|
||||||
)?;
|
|
||||||
|
|
||||||
|
let socket = self.socket.as_mut().ok_or(DSPError::NotConnected)?;
|
||||||
|
|
||||||
socket.write_all(data)?;
|
socket.write_all(data)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recv_raw(
|
fn recv_raw(&mut self) -> Result<Vec<u8>, DSPError> {
|
||||||
&mut self
|
let socket = self.socket.as_mut().ok_or(DSPError::NotConnected)?;
|
||||||
) -> Result<Vec<u8>, DSPError> {
|
|
||||||
|
|
||||||
let socket =
|
|
||||||
self.socket
|
|
||||||
.as_mut()
|
|
||||||
.ok_or(
|
|
||||||
DSPError::NotConnected
|
|
||||||
)?;
|
|
||||||
|
|
||||||
|
|
||||||
let mut buffer =
|
|
||||||
vec![0u8; RECV_BUFSIZE];
|
|
||||||
|
|
||||||
|
let mut buffer = vec![0u8; RECV_BUFSIZE];
|
||||||
|
|
||||||
match socket.read(&mut buffer) {
|
match socket.read(&mut buffer) {
|
||||||
|
Ok(0) => Err(DSPError::ConnectionClosed),
|
||||||
Ok(0) =>
|
|
||||||
Err(DSPError::ConnectionClosed),
|
|
||||||
|
|
||||||
Ok(size) => {
|
Ok(size) => {
|
||||||
buffer.truncate(size);
|
buffer.truncate(size);
|
||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
},
|
}
|
||||||
|
|
||||||
Err(e)
|
Err(e) if e.kind() == std::io::ErrorKind::TimedOut => Err(DSPError::Timeout),
|
||||||
if e.kind()
|
|
||||||
==
|
|
||||||
std::io::ErrorKind::TimedOut =>
|
|
||||||
{
|
|
||||||
Err(DSPError::Timeout)
|
|
||||||
},
|
|
||||||
|
|
||||||
Err(e) =>
|
Err(e) => Err(DSPError::Io(e)),
|
||||||
Err(DSPError::Io(e)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_recv(
|
pub fn send_recv(&mut self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
|
||||||
&mut self,
|
|
||||||
command: &[u8],
|
|
||||||
) -> Result<types::DeviceResponseType, DSPError> {
|
|
||||||
|
|
||||||
self.send_raw(command)?;
|
self.send_raw(command)?;
|
||||||
|
|
||||||
let raw =
|
let raw = self.recv_raw()?;
|
||||||
self.recv_raw()?;
|
|
||||||
|
|
||||||
let frame =
|
let frame = extract_frame(&raw)?;
|
||||||
extract_frame(&raw)?;
|
|
||||||
|
|
||||||
let response =
|
let response = map_response(&frame)?;
|
||||||
map_response(&frame)?;
|
|
||||||
|
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
@@ -174,46 +118,29 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn state(&self) -> Result<&types::DSPState, DSPError> {
|
pub fn state(&self) -> Result<&types::DSPState, DSPError> {
|
||||||
self.state
|
self.state.as_ref().ok_or(DSPError::NotConnected)
|
||||||
.as_ref()
|
|
||||||
.ok_or(DSPError::NotConnected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state_mut(&mut self) -> Result<&mut types::DSPState, DSPError> {
|
fn state_mut(&mut self) -> Result<&mut types::DSPState, DSPError> {
|
||||||
self.state
|
self.state.as_mut().ok_or(DSPError::NotConnected)
|
||||||
.as_mut()
|
|
||||||
.ok_or(DSPError::NotConnected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- Commands ----------
|
// ---------- Commands ----------
|
||||||
// Command `0x10`
|
// Command `0x10`
|
||||||
pub fn connect(&mut self) -> Result<(), DSPError> {
|
pub fn connect(&mut self) -> Result<(), DSPError> {
|
||||||
|
let key = (self.connection.host, self.connection.device_id);
|
||||||
let key = (
|
|
||||||
self.connection.host,
|
|
||||||
self.connection.device_id
|
|
||||||
);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let map = instances()
|
let map = instances().lock().unwrap();
|
||||||
.lock()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if map.contains_key(&key) {
|
if map.contains_key(&key) {
|
||||||
return Err(DSPError::AlreadyConnected);
|
return Err(DSPError::AlreadyConnected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr = SocketAddr::new(
|
let addr = SocketAddr::new(self.connection.host, self.connection.port);
|
||||||
self.connection.host,
|
|
||||||
self.connection.port
|
|
||||||
);
|
|
||||||
|
|
||||||
let stream =
|
let stream = TcpStream::connect_timeout(&addr, SOCKET_TIMEOUT)?;
|
||||||
TcpStream::connect_timeout(
|
|
||||||
&addr,
|
|
||||||
SOCKET_TIMEOUT
|
|
||||||
)?;
|
|
||||||
|
|
||||||
stream.set_read_timeout(Some(SOCKET_TIMEOUT))?;
|
stream.set_read_timeout(Some(SOCKET_TIMEOUT))?;
|
||||||
|
|
||||||
@@ -226,18 +153,12 @@ impl DSP408 {
|
|||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
while start.elapsed() < SOCKET_TIMEOUT {
|
while start.elapsed() < SOCKET_TIMEOUT {
|
||||||
|
|
||||||
let raw = self.recv_raw()?;
|
let raw = self.recv_raw()?;
|
||||||
let frame = extract_frame(&raw)?;
|
let frame = extract_frame(&raw)?;
|
||||||
let response = map_response(&frame)?;
|
let response = map_response(&frame)?;
|
||||||
|
|
||||||
if matches!(response, types::DeviceResponseType::HandshakeAck(_)) {
|
if matches!(response, types::DeviceResponseType::HandshakeAck(_)) {
|
||||||
|
instances().lock().unwrap().insert(key, ());
|
||||||
instances()
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert(key, ());
|
|
||||||
|
|
||||||
|
|
||||||
self.connected = true;
|
self.connected = true;
|
||||||
|
|
||||||
@@ -248,9 +169,7 @@ impl DSP408 {
|
|||||||
let mut preset_names = Vec::with_capacity(PRESET_INDEX_MAX);
|
let mut preset_names = Vec::with_capacity(PRESET_INDEX_MAX);
|
||||||
|
|
||||||
for i in 0..PRESET_INDEX_MAX {
|
for i in 0..PRESET_INDEX_MAX {
|
||||||
preset_names.push(
|
preset_names.push(self.get_preset_name(i + 1)?);
|
||||||
self.get_preset_name(i + 1)?
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let state = self.read_config_state()?;
|
let state = self.read_config_state()?;
|
||||||
@@ -277,31 +196,15 @@ impl DSP408 {
|
|||||||
|
|
||||||
// Command `0x11`
|
// Command `0x11`
|
||||||
pub fn disconnect(&mut self) {
|
pub fn disconnect(&mut self) {
|
||||||
|
let key = (self.connection.host, self.connection.device_id);
|
||||||
let key = (
|
|
||||||
self.connection.host,
|
|
||||||
self.connection.device_id
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(mut socket) = self.socket.take() {
|
if let Some(mut socket) = self.socket.take() {
|
||||||
|
let _ = socket.write_all(&commands::build_disconnect());
|
||||||
|
|
||||||
let _ =
|
let _ = socket.shutdown(std::net::Shutdown::Both);
|
||||||
socket.write_all(
|
|
||||||
&commands::build_disconnect()
|
|
||||||
);
|
|
||||||
|
|
||||||
let _ =
|
|
||||||
socket.shutdown(
|
|
||||||
std::net::Shutdown::Both
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instances().lock().unwrap().remove(&key);
|
||||||
instances()
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.remove(&key);
|
|
||||||
|
|
||||||
|
|
||||||
self.connected = false;
|
self.connected = false;
|
||||||
}
|
}
|
||||||
@@ -428,7 +331,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
if !self.request_acknowledgement()? {
|
if !self.request_acknowledgement()? {
|
||||||
return Err(DSPError::OperationFailed(
|
return Err(DSPError::OperationFailed(
|
||||||
"Store Configuration fails".into()
|
"Store Configuration fails".into(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,9 +373,7 @@ impl DSP408 {
|
|||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
types::DeviceResponseType::Acknowledgement(ack) => {
|
types::DeviceResponseType::Acknowledgement(ack) => Ok(ack.success),
|
||||||
Ok(ack.success)
|
|
||||||
}
|
|
||||||
other => Err(DSPError::UnexpectedResponse(format!(
|
other => Err(DSPError::UnexpectedResponse(format!(
|
||||||
"Expected Acknowledgement, got {:?}",
|
"Expected Acknowledgement, got {:?}",
|
||||||
other
|
other
|
||||||
@@ -516,9 +417,7 @@ impl DSP408 {
|
|||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
types::DeviceResponseType::ConfigChunk(config) => {
|
types::DeviceResponseType::ConfigChunk(config) => Ok(config),
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
other => Err(DSPError::UnexpectedResponse(format!(
|
other => Err(DSPError::UnexpectedResponse(format!(
|
||||||
"Expected ConfigChunk, got {:?}",
|
"Expected ConfigChunk, got {:?}",
|
||||||
other
|
other
|
||||||
@@ -549,20 +448,14 @@ impl DSP408 {
|
|||||||
channel_1: types::Channel,
|
channel_1: types::Channel,
|
||||||
channel_2: types::Channel,
|
channel_2: types::Channel,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let (raw_channel_1, raw_channel_2) = mapper::copy_channel_to_raw(channel_1, channel_2)?;
|
let (raw_channel_1, raw_channel_2) = mapper::copy_channel_to_raw(channel_1, channel_2)?;
|
||||||
|
|
||||||
let request = commands::build_copy_channel(
|
let request = commands::build_copy_channel(raw_channel_1, raw_channel_2)?;
|
||||||
raw_channel_1,
|
|
||||||
raw_channel_2,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
types::DeviceResponseType::Acknowledgement(ack) => {
|
types::DeviceResponseType::Acknowledgement(ack) => Ok(ack.success),
|
||||||
Ok(ack.success)
|
|
||||||
}
|
|
||||||
other => Err(DSPError::UnexpectedResponse(format!(
|
other => Err(DSPError::UnexpectedResponse(format!(
|
||||||
"Expected Acknowledgement, got {:?}",
|
"Expected Acknowledgement, got {:?}",
|
||||||
other
|
other
|
||||||
@@ -647,7 +540,6 @@ impl DSP408 {
|
|||||||
knee: u16,
|
knee: u16,
|
||||||
threshold: f32,
|
threshold: f32,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::output_channel_to_raw(channel)?;
|
let raw_channel = mapper::output_channel_to_raw(channel)?;
|
||||||
let raw_ratio = mapper::ratio_to_raw(ratio)?;
|
let raw_ratio = mapper::ratio_to_raw(ratio)?;
|
||||||
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
||||||
@@ -674,10 +566,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
let state = self.state_mut()?;
|
let state = self.state_mut()?;
|
||||||
|
|
||||||
state
|
state.current_config.output_state_mut(channel).compressor = types::Compressor {
|
||||||
.current_config
|
|
||||||
.output_state_mut(channel)
|
|
||||||
.compressor = types::Compressor {
|
|
||||||
threshold,
|
threshold,
|
||||||
ratio,
|
ratio,
|
||||||
attack,
|
attack,
|
||||||
@@ -701,16 +590,11 @@ impl DSP408 {
|
|||||||
freq: f32,
|
freq: f32,
|
||||||
filter_type: types::CrossoverFilter,
|
filter_type: types::CrossoverFilter,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_freq = mapper::frequency_hz_to_raw(freq)?;
|
let raw_freq = mapper::frequency_hz_to_raw(freq)?;
|
||||||
let raw_filter = mapper::crossover_filter_to_raw(filter_type)?;
|
let raw_filter = mapper::crossover_filter_to_raw(filter_type)?;
|
||||||
|
|
||||||
let request = commands::build_low_pass(
|
let request = commands::build_low_pass(raw_channel, raw_freq, raw_filter)?;
|
||||||
raw_channel,
|
|
||||||
raw_freq,
|
|
||||||
raw_filter,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -763,16 +647,11 @@ impl DSP408 {
|
|||||||
freq: f32,
|
freq: f32,
|
||||||
filter_type: types::CrossoverFilter,
|
filter_type: types::CrossoverFilter,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_freq = mapper::frequency_hz_to_raw(freq)?;
|
let raw_freq = mapper::frequency_hz_to_raw(freq)?;
|
||||||
let raw_filter = mapper::crossover_filter_to_raw(filter_type)?;
|
let raw_filter = mapper::crossover_filter_to_raw(filter_type)?;
|
||||||
|
|
||||||
let request = commands::build_high_pass(
|
let request = commands::build_high_pass(raw_channel, raw_freq, raw_filter)?;
|
||||||
raw_channel,
|
|
||||||
raw_freq,
|
|
||||||
raw_filter,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -830,7 +709,6 @@ impl DSP408 {
|
|||||||
filter_type: types::PEQFilter,
|
filter_type: types::PEQFilter,
|
||||||
bypass: bool,
|
bypass: bool,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_band = mapper::peq_band_to_raw(band, channel)?;
|
let raw_band = mapper::peq_band_to_raw(band, channel)?;
|
||||||
let raw_gain = mapper::eq_gain_db_to_raw(gain_db)?;
|
let raw_gain = mapper::eq_gain_db_to_raw(gain_db)?;
|
||||||
@@ -868,11 +746,8 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state
|
state.current_config.input_state_mut(input).peq_chain.bands
|
||||||
.current_config
|
[band as usize] = peq;
|
||||||
.input_state_mut(input)
|
|
||||||
.peq_chain
|
|
||||||
.bands[band as usize] = peq;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
@@ -895,19 +770,11 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command `0x34` (Set Gain)
|
// Command `0x34` (Set Gain)
|
||||||
pub fn set_channel_gain(
|
pub fn set_channel_gain(&mut self, channel: types::Channel, db: f32) -> Result<bool, DSPError> {
|
||||||
&mut self,
|
|
||||||
channel: types::Channel,
|
|
||||||
db: f32,
|
|
||||||
) -> Result<bool, DSPError> {
|
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_db = mapper::gain_db_to_raw(db)?;
|
let raw_db = mapper::gain_db_to_raw(db)?;
|
||||||
|
|
||||||
let request = commands::build_gain(
|
let request = commands::build_gain(raw_channel, raw_db)?;
|
||||||
raw_channel,
|
|
||||||
raw_db,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -921,17 +788,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state
|
state.current_config.input_state_mut(input).gain = db;
|
||||||
.current_config
|
|
||||||
.input_state_mut(input)
|
|
||||||
.gain = db;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state
|
state.current_config.output_state_mut(output).gain = db;
|
||||||
.current_config
|
|
||||||
.output_state_mut(output)
|
|
||||||
.gain = db;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -946,18 +807,10 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command `0x35` (Set Mute)
|
// Command `0x35` (Set Mute)
|
||||||
pub fn set_mute(
|
pub fn set_mute(&mut self, channel: types::Channel, muted: bool) -> Result<bool, DSPError> {
|
||||||
&mut self,
|
|
||||||
channel: types::Channel,
|
|
||||||
muted: bool,
|
|
||||||
) -> Result<bool, DSPError> {
|
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
|
|
||||||
let request = commands::build_mute(
|
let request = commands::build_mute(raw_channel, muted as u8)?;
|
||||||
raw_channel,
|
|
||||||
muted as u8,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -971,17 +824,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state
|
state.current_config.input_state_mut(input).mute = muted;
|
||||||
.current_config
|
|
||||||
.input_state_mut(input)
|
|
||||||
.mute = muted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state
|
state.current_config.output_state_mut(output).mute = muted;
|
||||||
.current_config
|
|
||||||
.output_state_mut(output)
|
|
||||||
.mute = muted;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1001,13 +848,9 @@ impl DSP408 {
|
|||||||
channel: types::Channel,
|
channel: types::Channel,
|
||||||
inverted: bool,
|
inverted: bool,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
|
|
||||||
let request = commands::build_inverted_gain(
|
let request = commands::build_inverted_gain(raw_channel, inverted as u8)?;
|
||||||
raw_channel,
|
|
||||||
inverted as u8,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1021,17 +864,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state
|
state.current_config.input_state_mut(input).phase_inverted = inverted;
|
||||||
.current_config
|
|
||||||
.input_state_mut(input)
|
|
||||||
.phase_inverted = inverted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state
|
state.current_config.output_state_mut(output).phase_inverted = inverted;
|
||||||
.current_config
|
|
||||||
.output_state_mut(output)
|
|
||||||
.phase_inverted = inverted;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1052,14 +889,10 @@ impl DSP408 {
|
|||||||
delay: f32,
|
delay: f32,
|
||||||
unit: types::DelayUnit,
|
unit: types::DelayUnit,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_delay = mapper::delay_to_raw(delay, unit)?;
|
let raw_delay = mapper::delay_to_raw(delay, unit)?;
|
||||||
|
|
||||||
let request = commands::build_set_delay(
|
let request = commands::build_set_delay(raw_channel, raw_delay)?;
|
||||||
raw_channel,
|
|
||||||
raw_delay,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1075,17 +908,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state
|
state.current_config.input_state_mut(input).delay = delay;
|
||||||
.current_config
|
|
||||||
.input_state_mut(input)
|
|
||||||
.delay = delay;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state
|
state.current_config.output_state_mut(output).delay = delay;
|
||||||
.current_config
|
|
||||||
.output_state_mut(output)
|
|
||||||
.delay = delay;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1100,18 +927,11 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command `0x39` (Set Input Source)
|
// Command `0x39` (Set Input Source)
|
||||||
pub fn set_input_source(
|
pub fn set_input_source(&mut self, input_source: types::InputSource) -> Result<bool, DSPError> {
|
||||||
&mut self,
|
|
||||||
input_source: types::InputSource,
|
|
||||||
) -> Result<bool, DSPError> {
|
|
||||||
|
|
||||||
let raw_source = mapper::input_source_to_raw(input_source.r#type)?;
|
let raw_source = mapper::input_source_to_raw(input_source.r#type)?;
|
||||||
let raw_frequency = mapper::frequency_value_to_raw(input_source.frequency)?;
|
let raw_frequency = mapper::frequency_value_to_raw(input_source.frequency)?;
|
||||||
|
|
||||||
let request = commands::build_set_input_source(
|
let request = commands::build_set_input_source(raw_source, raw_frequency)?;
|
||||||
raw_source,
|
|
||||||
raw_frequency,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1140,14 +960,10 @@ impl DSP408 {
|
|||||||
output: types::OutputChannel,
|
output: types::OutputChannel,
|
||||||
inputs: Vec<types::InputChannel>,
|
inputs: Vec<types::InputChannel>,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_output = mapper::output_channel_to_raw(output)?;
|
let raw_output = mapper::output_channel_to_raw(output)?;
|
||||||
let raw_inputs = mapper::matrix_to_raw(&inputs)?;
|
let raw_inputs = mapper::matrix_to_raw(&inputs)?;
|
||||||
|
|
||||||
let request = commands::build_matrix_routing(
|
let request = commands::build_matrix_routing(raw_output, raw_inputs)?;
|
||||||
raw_output,
|
|
||||||
raw_inputs,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1181,7 +997,6 @@ impl DSP408 {
|
|||||||
source: types::Channel,
|
source: types::Channel,
|
||||||
destinations: Vec<types::Channel>,
|
destinations: Vec<types::Channel>,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_source = mapper::channel_to_raw(source)?;
|
let raw_source = mapper::channel_to_raw(source)?;
|
||||||
let bitmask = mapper::link_channels_to_raw(source, &destinations)?;
|
let bitmask = mapper::link_channels_to_raw(source, &destinations)?;
|
||||||
|
|
||||||
@@ -1197,10 +1012,7 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Link source
|
// Link source
|
||||||
let request = commands::build_link_channel(
|
let request = commands::build_link_channel(raw_source, bitmask)?;
|
||||||
raw_source,
|
|
||||||
bitmask,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1219,7 +1031,6 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Clear destination links
|
// Clear destination links
|
||||||
for destination in &destinations {
|
for destination in &destinations {
|
||||||
if *destination == source {
|
if *destination == source {
|
||||||
@@ -1228,10 +1039,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
let raw_destination = mapper::channel_to_raw(*destination)?;
|
let raw_destination = mapper::channel_to_raw(*destination)?;
|
||||||
|
|
||||||
let request = commands::build_link_channel(
|
let request = commands::build_link_channel(raw_destination, 0x00)?;
|
||||||
raw_destination,
|
|
||||||
0x00,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1268,13 +1076,9 @@ impl DSP408 {
|
|||||||
channel: types::Channel,
|
channel: types::Channel,
|
||||||
bypass: bool,
|
bypass: bool,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
|
|
||||||
let request = commands::build_bypass_peq(
|
let request = commands::build_bypass_peq(raw_channel, bypass as u8)?;
|
||||||
raw_channel,
|
|
||||||
bypass as u8,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1288,13 +1092,13 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state.current_config.input_state_mut(input)
|
state.current_config.input_state_mut(input).peq_chain.bypass = bypass;
|
||||||
.peq_chain
|
|
||||||
.bypass = bypass;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state.current_config.output_state_mut(output)
|
state
|
||||||
|
.current_config
|
||||||
|
.output_state_mut(output)
|
||||||
.peq_chain
|
.peq_chain
|
||||||
.bypass = bypass;
|
.bypass = bypass;
|
||||||
}
|
}
|
||||||
@@ -1316,14 +1120,10 @@ impl DSP408 {
|
|||||||
channel: types::Channel,
|
channel: types::Channel,
|
||||||
name: &str,
|
name: &str,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::channel_to_raw(channel)?;
|
let raw_channel = mapper::channel_to_raw(channel)?;
|
||||||
let raw_name = mapper::channel_name_to_raw(name)?;
|
let raw_name = mapper::channel_name_to_raw(name)?;
|
||||||
|
|
||||||
let request = commands::build_store_channel_name(
|
let request = commands::build_store_channel_name(raw_channel, &raw_name)?;
|
||||||
raw_channel,
|
|
||||||
&raw_name,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1337,13 +1137,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
match channel {
|
match channel {
|
||||||
types::Channel::Input(input) => {
|
types::Channel::Input(input) => {
|
||||||
state.current_config.input_state_mut(input)
|
state.current_config.input_state_mut(input).name = name.to_string();
|
||||||
.name = name.to_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::Channel::Output(output) => {
|
types::Channel::Output(output) => {
|
||||||
state.current_config.output_state_mut(output)
|
state.current_config.output_state_mut(output).name = name.to_string();
|
||||||
.name = name.to_string();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1366,7 +1164,6 @@ impl DSP408 {
|
|||||||
hold: u16,
|
hold: u16,
|
||||||
release: u16,
|
release: u16,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::input_channel_to_raw(channel)?;
|
let raw_channel = mapper::input_channel_to_raw(channel)?;
|
||||||
let raw_threshold = mapper::gate_threshold_db_to_raw(threshold)?;
|
let raw_threshold = mapper::gate_threshold_db_to_raw(threshold)?;
|
||||||
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
||||||
@@ -1391,10 +1188,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
let state = self.state_mut()?;
|
let state = self.state_mut()?;
|
||||||
|
|
||||||
state
|
state.current_config.input_state_mut(channel).gate = types::Gate {
|
||||||
.current_config
|
|
||||||
.input_state_mut(channel)
|
|
||||||
.gate = types::Gate {
|
|
||||||
attack,
|
attack,
|
||||||
release,
|
release,
|
||||||
hold,
|
hold,
|
||||||
@@ -1419,18 +1213,13 @@ impl DSP408 {
|
|||||||
attack: u16,
|
attack: u16,
|
||||||
release: u16,
|
release: u16,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_channel = mapper::output_channel_to_raw(channel)?;
|
let raw_channel = mapper::output_channel_to_raw(channel)?;
|
||||||
let raw_threshold = mapper::threshold_db_to_raw(threshold)?;
|
let raw_threshold = mapper::threshold_db_to_raw(threshold)?;
|
||||||
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
let raw_attack = mapper::attack_ms_to_raw(attack)?;
|
||||||
let raw_release = mapper::release_ms_to_raw(release)?;
|
let raw_release = mapper::release_ms_to_raw(release)?;
|
||||||
|
|
||||||
let request = commands::build_set_limiter(
|
let request =
|
||||||
raw_channel,
|
commands::build_set_limiter(raw_channel, raw_threshold, raw_attack, raw_release)?;
|
||||||
raw_threshold,
|
|
||||||
raw_attack,
|
|
||||||
raw_release,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1442,10 +1231,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
let state = self.state_mut()?;
|
let state = self.state_mut()?;
|
||||||
|
|
||||||
state
|
state.current_config.output_state_mut(channel).limiter = types::Limiter {
|
||||||
.current_config
|
|
||||||
.output_state_mut(channel)
|
|
||||||
.limiter = types::Limiter {
|
|
||||||
threshold,
|
threshold,
|
||||||
attack,
|
attack,
|
||||||
release,
|
release,
|
||||||
@@ -1462,10 +1248,7 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command `0x40` (Get Level Meters)
|
// Command `0x40` (Get Level Meters)
|
||||||
pub fn get_meter_levels(
|
pub fn get_meter_levels(&mut self) -> Result<types::Meters, DSPError> {
|
||||||
&mut self,
|
|
||||||
) -> Result<types::Meters, DSPError> {
|
|
||||||
|
|
||||||
let request = commands::build_get_meter_levels();
|
let request = commands::build_get_meter_levels();
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
@@ -1487,16 +1270,11 @@ impl DSP408 {
|
|||||||
output_channel: types::OutputChannel,
|
output_channel: types::OutputChannel,
|
||||||
gain: f32,
|
gain: f32,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
||||||
let raw_output_channel = mapper::output_channel_to_raw(output_channel)?;
|
let raw_output_channel = mapper::output_channel_to_raw(output_channel)?;
|
||||||
let raw_gain = mapper::matrix_gain_db_to_raw(gain)?;
|
let raw_gain = mapper::matrix_gain_db_to_raw(gain)?;
|
||||||
|
|
||||||
let request = commands::build_matrix_gain(
|
let request = commands::build_matrix_gain(raw_input_channel, raw_output_channel, raw_gain)?;
|
||||||
raw_input_channel,
|
|
||||||
raw_output_channel,
|
|
||||||
raw_gain,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1510,9 +1288,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
let index = types::InputChannel::iter()
|
let index = types::InputChannel::iter()
|
||||||
.position(|x| x == input_channel)
|
.position(|x| x == input_channel)
|
||||||
.ok_or_else(|| DSPError::Validation(ValidationError::Value(
|
.ok_or_else(|| {
|
||||||
"Invalid input channel".to_string()
|
DSPError::Validation(ValidationError::Value(
|
||||||
)))?;
|
"Invalid input channel".to_string(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
state
|
state
|
||||||
.current_config
|
.current_config
|
||||||
@@ -1537,16 +1317,12 @@ impl DSP408 {
|
|||||||
frequency_band: types::DiscreteFrequency,
|
frequency_band: types::DiscreteFrequency,
|
||||||
gain: f32,
|
gain: f32,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
||||||
let raw_frequency_band = mapper::frequency_value_to_raw(frequency_band)?;
|
let raw_frequency_band = mapper::frequency_value_to_raw(frequency_band)?;
|
||||||
let raw_gain = mapper::eq_gain_db_to_raw(gain)?;
|
let raw_gain = mapper::eq_gain_db_to_raw(gain)?;
|
||||||
|
|
||||||
let request = commands::build_set_geq_band(
|
let request =
|
||||||
raw_input_channel,
|
commands::build_set_geq_band(raw_input_channel, raw_frequency_band, raw_gain)?;
|
||||||
raw_frequency_band,
|
|
||||||
raw_gain,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1560,9 +1336,11 @@ impl DSP408 {
|
|||||||
|
|
||||||
let index = types::DiscreteFrequency::iter()
|
let index = types::DiscreteFrequency::iter()
|
||||||
.position(|x| x == frequency_band)
|
.position(|x| x == frequency_band)
|
||||||
.ok_or_else(|| DSPError::Validation(ValidationError::Value(
|
.ok_or_else(|| {
|
||||||
"Invalid GEQ frequency band".to_string()
|
DSPError::Validation(ValidationError::Value(
|
||||||
)))?;
|
"Invalid GEQ frequency band".to_string(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
state
|
state
|
||||||
.current_config
|
.current_config
|
||||||
@@ -1585,13 +1363,9 @@ impl DSP408 {
|
|||||||
input_channel: types::InputChannel,
|
input_channel: types::InputChannel,
|
||||||
bypass: bool,
|
bypass: bool,
|
||||||
) -> Result<bool, DSPError> {
|
) -> Result<bool, DSPError> {
|
||||||
|
|
||||||
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
let raw_input_channel = mapper::input_channel_to_raw(input_channel)?;
|
||||||
|
|
||||||
let request = commands::build_set_geq_bypass(
|
let request = commands::build_set_geq_bypass(raw_input_channel, bypass as u8)?;
|
||||||
raw_input_channel,
|
|
||||||
bypass as u8,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resp = self.send_recv(&request)?;
|
let resp = self.send_recv(&request)?;
|
||||||
|
|
||||||
@@ -1630,14 +1404,11 @@ impl DSP408 {
|
|||||||
let config = decode_config(&raw)?;
|
let config = decode_config(&raw)?;
|
||||||
|
|
||||||
if !self.request_acknowledgement()? {
|
if !self.request_acknowledgement()? {
|
||||||
return Err(DSPError::OperationFailed(
|
return Err(DSPError::OperationFailed("DSP initialization".into()));
|
||||||
"DSP initialization".into()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for DSP408 {
|
impl Drop for DSP408 {
|
||||||
|
|||||||
+275
-226
@@ -2,30 +2,16 @@ use strum::IntoEnumIterator;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{
|
||||||
errors::{
|
errors::{DecodingError, MapperError, ValidationError},
|
||||||
DecodingError,
|
validator::{validate_ascii_padded, validate_range},
|
||||||
MapperError,
|
|
||||||
ValidationError,
|
|
||||||
},
|
|
||||||
validator::{
|
|
||||||
validate_ascii_padded,
|
|
||||||
validate_range,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
dsp408::{
|
dsp408::{
|
||||||
protocol::{
|
protocol::{constants as protocol_constants, validators},
|
||||||
constants as protocol_constants,
|
|
||||||
validators,
|
|
||||||
},
|
|
||||||
types,
|
types,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{constants as mapper_constants, decode_ascii, encode_ascii};
|
||||||
constants as mapper_constants,
|
|
||||||
decode_ascii,
|
|
||||||
encode_ascii,
|
|
||||||
};
|
|
||||||
|
|
||||||
// ------ CHANNEL ------
|
// ------ CHANNEL ------
|
||||||
|
|
||||||
@@ -71,13 +57,11 @@ pub fn channel_to_raw(channel: types::Channel) -> Result<u8, MapperError> {
|
|||||||
pub fn raw_to_channel(value: u8) -> Result<types::Channel, MapperError> {
|
pub fn raw_to_channel(value: u8) -> Result<types::Channel, MapperError> {
|
||||||
validators::validate_raw_channel(value)?;
|
validators::validate_raw_channel(value)?;
|
||||||
|
|
||||||
raw_to_channel_value(value)
|
raw_to_channel_value(value).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown channel value: {:#04x}",
|
||||||
DecodingError::new(
|
value
|
||||||
format!("Unknown channel value: {:#04x}", value)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +83,11 @@ pub fn channel_name_to_raw(name: &str) -> Result<Vec<u8>, MapperError> {
|
|||||||
"Channel name",
|
"Channel name",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(encode_ascii(name, protocol_constants::CHANNEL_NAME_LENGTH, protocol_constants::CHANNEL_NAME_PADDING)?)
|
Ok(encode_ascii(
|
||||||
|
name,
|
||||||
|
protocol_constants::CHANNEL_NAME_LENGTH,
|
||||||
|
protocol_constants::CHANNEL_NAME_PADDING,
|
||||||
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_channel_name(raw_name: &[u8]) -> Result<String, MapperError> {
|
pub fn raw_to_channel_name(raw_name: &[u8]) -> Result<String, MapperError> {
|
||||||
@@ -111,11 +99,9 @@ pub fn raw_to_channel_name(raw_name: &[u8]) -> Result<String, MapperError> {
|
|||||||
// ------ ATTACK ------
|
// ------ ATTACK ------
|
||||||
|
|
||||||
pub fn attack_ms_to_raw(value: u16) -> Result<u16, MapperError> {
|
pub fn attack_ms_to_raw(value: u16) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(value, mapper_constants::ATTACK_MS_RANGE, || {
|
||||||
value,
|
format!("Invalid attack: {:#06x}", value)
|
||||||
mapper_constants::ATTACK_MS_RANGE,
|
})?;
|
||||||
|| format!("Invalid attack: {:#06x}", value),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(value - 1)
|
Ok(value - 1)
|
||||||
}
|
}
|
||||||
@@ -129,11 +115,9 @@ pub fn raw_to_attack_ms(value: u16) -> Result<u16, MapperError> {
|
|||||||
// ------ RELEASE ------
|
// ------ RELEASE ------
|
||||||
|
|
||||||
pub fn release_ms_to_raw(value: u16) -> Result<u16, MapperError> {
|
pub fn release_ms_to_raw(value: u16) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(value, mapper_constants::RELEASE_MS_RANGE, || {
|
||||||
value,
|
format!("Invalid release: {:#06x}", value)
|
||||||
mapper_constants::RELEASE_MS_RANGE,
|
})?;
|
||||||
|| format!("Invalid release: {:#06x}", value),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(value - 1)
|
Ok(value - 1)
|
||||||
}
|
}
|
||||||
@@ -147,11 +131,9 @@ pub fn raw_to_release_ms(value: u16) -> Result<u16, MapperError> {
|
|||||||
// ------ HOLD ------
|
// ------ HOLD ------
|
||||||
|
|
||||||
pub fn hold_ms_to_raw(hold: u16) -> Result<u16, MapperError> {
|
pub fn hold_ms_to_raw(hold: u16) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(hold, mapper_constants::HOLD_MS_RANGE, || {
|
||||||
hold,
|
format!("Invalid hold: {:#06x}", hold)
|
||||||
mapper_constants::HOLD_MS_RANGE,
|
})?;
|
||||||
|| format!("Invalid hold: {:#06x}", hold),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(hold - 1)
|
Ok(hold - 1)
|
||||||
}
|
}
|
||||||
@@ -165,11 +147,9 @@ pub fn raw_to_hold_ms(hold: u16) -> Result<u16, MapperError> {
|
|||||||
// ------ GATE THRESHOLD ------
|
// ------ GATE THRESHOLD ------
|
||||||
|
|
||||||
pub fn gate_threshold_db_to_raw(db: f32) -> Result<u16, MapperError> {
|
pub fn gate_threshold_db_to_raw(db: f32) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(db, mapper_constants::GATE_THRESHOLD_DB_RANGE, || {
|
||||||
db,
|
format!("Invalid gate threshold: {db}")
|
||||||
mapper_constants::GATE_THRESHOLD_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid gate threshold: {db}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let db = (db * 2.0).round() / 2.0;
|
let db = (db * 2.0).round() / 2.0;
|
||||||
|
|
||||||
@@ -185,13 +165,12 @@ pub fn raw_to_gate_threshold_db(value: u16) -> Result<f32, MapperError> {
|
|||||||
// ------ EQ GAIN ------
|
// ------ EQ GAIN ------
|
||||||
|
|
||||||
fn eq_gain_raw_steps() -> f32 {
|
fn eq_gain_raw_steps() -> f32 {
|
||||||
(*protocol_constants::EQ_GAIN_RAW_RANGE.end()
|
(*protocol_constants::EQ_GAIN_RAW_RANGE.end() - *protocol_constants::EQ_GAIN_RAW_RANGE.start())
|
||||||
- *protocol_constants::EQ_GAIN_RAW_RANGE.start()) as f32
|
as f32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq_gain_step_db() -> f32 {
|
fn eq_gain_step_db() -> f32 {
|
||||||
(*mapper_constants::EQ_GAIN_DB_RANGE.end()
|
(*mapper_constants::EQ_GAIN_DB_RANGE.end() - *mapper_constants::EQ_GAIN_DB_RANGE.start())
|
||||||
- *mapper_constants::EQ_GAIN_DB_RANGE.start())
|
|
||||||
/ eq_gain_raw_steps()
|
/ eq_gain_raw_steps()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,29 +179,21 @@ fn eq_quantize(db: f32) -> f32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn eq_gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
pub fn eq_gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(gain, mapper_constants::EQ_GAIN_DB_RANGE, || {
|
||||||
gain,
|
format!("Invalid EQ gain: {gain}")
|
||||||
mapper_constants::EQ_GAIN_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid EQ gain: {gain}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let gain = eq_quantize(gain);
|
let gain = eq_quantize(gain);
|
||||||
|
|
||||||
Ok(
|
Ok(*protocol_constants::EQ_GAIN_RAW_RANGE.start()
|
||||||
*protocol_constants::EQ_GAIN_RAW_RANGE.start()
|
+ ((gain - *mapper_constants::EQ_GAIN_DB_RANGE.start()) / eq_gain_step_db()).round() as u16)
|
||||||
+ ((gain - *mapper_constants::EQ_GAIN_DB_RANGE.start())
|
|
||||||
/ eq_gain_step_db())
|
|
||||||
.round() as u16
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_eq_gain(value: u16) -> Result<f32, MapperError> {
|
pub fn raw_to_eq_gain(value: u16) -> Result<f32, MapperError> {
|
||||||
validators::validate_raw_eq_gain(value)?;
|
validators::validate_raw_eq_gain(value)?;
|
||||||
|
|
||||||
let gain =
|
let gain = *mapper_constants::EQ_GAIN_DB_RANGE.start()
|
||||||
*mapper_constants::EQ_GAIN_DB_RANGE.start()
|
+ (value - *protocol_constants::EQ_GAIN_RAW_RANGE.start()) as f32 * eq_gain_step_db();
|
||||||
+ (value - *protocol_constants::EQ_GAIN_RAW_RANGE.start()) as f32
|
|
||||||
* eq_gain_step_db();
|
|
||||||
|
|
||||||
Ok(eq_quantize(gain))
|
Ok(eq_quantize(gain))
|
||||||
}
|
}
|
||||||
@@ -248,7 +219,7 @@ pub fn peq_band_to_raw(band: types::PEQBand, channel: types::Channel) -> Result<
|
|||||||
types::Channel::Input(_) => {
|
types::Channel::Input(_) => {
|
||||||
if matches!(band, types::PEQBand::B9) {
|
if matches!(band, types::PEQBand::B9) {
|
||||||
return Err(MapperError::Validation(ValidationError::Value(
|
return Err(MapperError::Validation(ValidationError::Value(
|
||||||
"PEQ Band B9 is not available on input channels".to_string()
|
"PEQ Band B9 is not available on input channels".to_string(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,7 +235,9 @@ pub fn peq_band_to_raw(band: types::PEQBand, channel: types::Channel) -> Result<
|
|||||||
// ------ GAIN ------
|
// ------ GAIN ------
|
||||||
|
|
||||||
fn gain_split_raw() -> u16 {
|
fn gain_split_raw() -> u16 {
|
||||||
((mapper_constants::GAIN_SPLIT_DB - mapper_constants::GAIN_DB_RANGE.start()) / mapper_constants::GAIN_LOW_STEP_DB).round() as u16
|
((mapper_constants::GAIN_SPLIT_DB - mapper_constants::GAIN_DB_RANGE.start())
|
||||||
|
/ mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
|
.round() as u16
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quantize_gain(db: f32) -> f32 {
|
fn quantize_gain(db: f32) -> f32 {
|
||||||
@@ -276,18 +249,21 @@ fn quantize_gain(db: f32) -> f32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
pub fn gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(gain, mapper_constants::GAIN_DB_RANGE, || {
|
||||||
gain,
|
format!("Invalid channel gain: {gain}")
|
||||||
mapper_constants::GAIN_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid channel gain: {gain}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let gain = quantize_gain(gain);
|
let gain = quantize_gain(gain);
|
||||||
|
|
||||||
if gain < mapper_constants::GAIN_SPLIT_DB {
|
if gain < mapper_constants::GAIN_SPLIT_DB {
|
||||||
Ok(((gain - mapper_constants::GAIN_DB_RANGE.start()) / mapper_constants::GAIN_LOW_STEP_DB).round() as u16)
|
Ok(
|
||||||
|
((gain - mapper_constants::GAIN_DB_RANGE.start()) / mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
|
.round() as u16,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Ok(gain_split_raw() + ((gain - mapper_constants::GAIN_SPLIT_DB) / mapper_constants::GAIN_HIGH_STEP_DB).round() as u16)
|
Ok(gain_split_raw()
|
||||||
|
+ ((gain - mapper_constants::GAIN_SPLIT_DB) / mapper_constants::GAIN_HIGH_STEP_DB)
|
||||||
|
.round() as u16)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,25 +271,28 @@ pub fn raw_to_gain(raw_gain: u16) -> Result<f32, MapperError> {
|
|||||||
validators::validate_raw_gain(raw_gain)?;
|
validators::validate_raw_gain(raw_gain)?;
|
||||||
|
|
||||||
if raw_gain < gain_split_raw() {
|
if raw_gain < gain_split_raw() {
|
||||||
Ok(mapper_constants::GAIN_DB_RANGE.start() + raw_gain as f32 * mapper_constants::GAIN_LOW_STEP_DB)
|
Ok(mapper_constants::GAIN_DB_RANGE.start()
|
||||||
|
+ raw_gain as f32 * mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
} else {
|
} else {
|
||||||
Ok(mapper_constants::GAIN_SPLIT_DB + (raw_gain - gain_split_raw()) as f32 * mapper_constants::GAIN_HIGH_STEP_DB)
|
Ok(mapper_constants::GAIN_SPLIT_DB
|
||||||
|
+ (raw_gain - gain_split_raw()) as f32 * mapper_constants::GAIN_HIGH_STEP_DB)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ CONTIGUOUS FREQUENCY ------
|
// ------ CONTIGUOUS FREQUENCY ------
|
||||||
|
|
||||||
pub fn frequency_hz_to_raw(hz: f32) -> Result<u16, MapperError> {
|
pub fn frequency_hz_to_raw(hz: f32) -> Result<u16, MapperError> {
|
||||||
|
validate_range(hz, mapper_constants::FREQUENCY_HZ_RANGE, || {
|
||||||
|
format!("Invalid frequency: {hz}")
|
||||||
|
})?;
|
||||||
|
|
||||||
validate_range(
|
let device_steps = (protocol_constants::FREQUENCY_RAW_RANGE.end()
|
||||||
hz,
|
- protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32;
|
||||||
mapper_constants::FREQUENCY_HZ_RANGE,
|
|
||||||
|| format!("Invalid frequency: {hz}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let device_steps = (protocol_constants::FREQUENCY_RAW_RANGE.end() - protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32;
|
let raw = ((hz / mapper_constants::FREQUENCY_HZ_RANGE.start()).ln()
|
||||||
|
/ (mapper_constants::FREQUENCY_HZ_RANGE.end()
|
||||||
let raw = ((hz / mapper_constants::FREQUENCY_HZ_RANGE.start()).ln() / (mapper_constants::FREQUENCY_HZ_RANGE.end() / mapper_constants::FREQUENCY_HZ_RANGE.start()).ln()
|
/ mapper_constants::FREQUENCY_HZ_RANGE.start())
|
||||||
|
.ln()
|
||||||
* device_steps)
|
* device_steps)
|
||||||
.round();
|
.round();
|
||||||
|
|
||||||
@@ -323,10 +302,15 @@ pub fn frequency_hz_to_raw(hz: f32) -> Result<u16, MapperError> {
|
|||||||
pub fn raw_to_frequency_hz(value: u16) -> Result<f32, MapperError> {
|
pub fn raw_to_frequency_hz(value: u16) -> Result<f32, MapperError> {
|
||||||
validators::validate_raw_frequency(value)?;
|
validators::validate_raw_frequency(value)?;
|
||||||
|
|
||||||
let device_steps = (protocol_constants::FREQUENCY_RAW_RANGE.end() - protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32;
|
let device_steps = (protocol_constants::FREQUENCY_RAW_RANGE.end()
|
||||||
let normalized = (value - protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32 / device_steps;
|
- protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32;
|
||||||
|
let normalized =
|
||||||
|
(value - protocol_constants::FREQUENCY_RAW_RANGE.start()) as f32 / device_steps;
|
||||||
|
|
||||||
Ok(mapper_constants::FREQUENCY_HZ_RANGE.start() * (mapper_constants::FREQUENCY_HZ_RANGE.end() / mapper_constants::FREQUENCY_HZ_RANGE.start()).powf(normalized))
|
Ok(mapper_constants::FREQUENCY_HZ_RANGE.start()
|
||||||
|
* (mapper_constants::FREQUENCY_HZ_RANGE.end()
|
||||||
|
/ mapper_constants::FREQUENCY_HZ_RANGE.start())
|
||||||
|
.powf(normalized))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ DISCRETE FREQUENCY ------
|
// ------ DISCRETE FREQUENCY ------
|
||||||
@@ -405,20 +389,17 @@ fn raw_to_discrete_frequency_value(raw: u8) -> Option<types::DiscreteFrequency>
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn frequency_value_to_raw(frequency: types::DiscreteFrequency) -> Result<u8, MapperError> {
|
pub fn frequency_value_to_raw(frequency: types::DiscreteFrequency) -> Result<u8, MapperError> {
|
||||||
|
|
||||||
Ok(discrete_frequency_to_raw_value(frequency))
|
Ok(discrete_frequency_to_raw_value(frequency))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_frequency_value(frequency: u8) -> Result<types::DiscreteFrequency, MapperError> {
|
pub fn raw_to_frequency_value(frequency: u8) -> Result<types::DiscreteFrequency, MapperError> {
|
||||||
validators::validate_raw_discrete_frequency(frequency)?;
|
validators::validate_raw_discrete_frequency(frequency)?;
|
||||||
|
|
||||||
raw_to_discrete_frequency_value(frequency)
|
raw_to_discrete_frequency_value(frequency).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown frequency value: {:#04x}",
|
||||||
DecodingError::new(
|
frequency
|
||||||
format!("Unknown frequency value: {:#04x}", frequency)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,38 +465,41 @@ pub fn crossover_filter_to_raw(filter: types::CrossoverFilter) -> Result<u8, Map
|
|||||||
pub fn raw_to_crossover_filter(raw: u8) -> Result<types::CrossoverFilter, MapperError> {
|
pub fn raw_to_crossover_filter(raw: u8) -> Result<types::CrossoverFilter, MapperError> {
|
||||||
validators::validate_raw_crossover_filter(raw)?;
|
validators::validate_raw_crossover_filter(raw)?;
|
||||||
|
|
||||||
raw_to_crossover_filter_value(raw)
|
raw_to_crossover_filter_value(raw).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown crossover filter value: {:#04x}",
|
||||||
DecodingError::new(
|
raw
|
||||||
format!("Unknown crossover filter value: {:#04x}", raw)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ PEQ Q ------
|
// ------ PEQ Q ------
|
||||||
|
|
||||||
pub fn peq_q_to_raw(q: f32, filter: types::PEQFilter) -> Result<u8, MapperError> {
|
pub fn peq_q_to_raw(q: f32, filter: types::PEQFilter) -> Result<u8, MapperError> {
|
||||||
validate_range(
|
validate_range(q, mapper_constants::peq_q_range(filter), || {
|
||||||
q,
|
format!("Invalid PEQ Q: {q}")
|
||||||
mapper_constants::peq_q_range(filter),
|
})?;
|
||||||
|| format!("Invalid PEQ Q: {q}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let raw_range = (protocol_constants::PEQ_Q_RAW_RANGE.end() - protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32;
|
let raw_range = (protocol_constants::PEQ_Q_RAW_RANGE.end()
|
||||||
|
- protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32;
|
||||||
|
|
||||||
Ok(((q / mapper_constants::PEQ_Q_RANGE.start()).ln() / (mapper_constants::PEQ_Q_RANGE.end() / mapper_constants::PEQ_Q_RANGE.start()).ln() * raw_range).round() as u8
|
Ok(((q / mapper_constants::PEQ_Q_RANGE.start()).ln()
|
||||||
|
/ (mapper_constants::PEQ_Q_RANGE.end() / mapper_constants::PEQ_Q_RANGE.start()).ln()
|
||||||
|
* raw_range)
|
||||||
|
.round() as u8
|
||||||
+ protocol_constants::PEQ_Q_RAW_RANGE.start())
|
+ protocol_constants::PEQ_Q_RAW_RANGE.start())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_peq_q(value: u8) -> Result<f32, MapperError> {
|
pub fn raw_to_peq_q(value: u8) -> Result<f32, MapperError> {
|
||||||
validators::validate_raw_peq_q(value)?;
|
validators::validate_raw_peq_q(value)?;
|
||||||
|
|
||||||
let raw_range = (protocol_constants::PEQ_Q_RAW_RANGE.end() - protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32;
|
let raw_range = (protocol_constants::PEQ_Q_RAW_RANGE.end()
|
||||||
|
- protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32;
|
||||||
let normalized = (value - protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32 / raw_range;
|
let normalized = (value - protocol_constants::PEQ_Q_RAW_RANGE.start()) as f32 / raw_range;
|
||||||
|
|
||||||
Ok(mapper_constants::PEQ_Q_RANGE.start() * (mapper_constants::PEQ_Q_RANGE.end() / mapper_constants::PEQ_Q_RANGE.start()).powf(normalized))
|
Ok(mapper_constants::PEQ_Q_RANGE.start()
|
||||||
|
* (mapper_constants::PEQ_Q_RANGE.end() / mapper_constants::PEQ_Q_RANGE.start())
|
||||||
|
.powf(normalized))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ FILTER ------
|
// ------ FILTER ------
|
||||||
@@ -556,13 +540,11 @@ pub fn peq_filter_to_raw(filter: types::PEQFilter) -> Result<u8, MapperError> {
|
|||||||
pub fn raw_to_peq_filter(value: u8) -> Result<types::PEQFilter, MapperError> {
|
pub fn raw_to_peq_filter(value: u8) -> Result<types::PEQFilter, MapperError> {
|
||||||
validators::validate_raw_peq_filter(value)?;
|
validators::validate_raw_peq_filter(value)?;
|
||||||
|
|
||||||
raw_to_peq_filter_value(value)
|
raw_to_peq_filter_value(value).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown PEQ filter value: {:#04x}",
|
||||||
DecodingError::new(
|
value
|
||||||
format!("Unknown PEQ filter value: {:#04x}", value)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -618,24 +600,20 @@ pub fn ratio_to_raw(ratio: types::Ratio) -> Result<u16, MapperError> {
|
|||||||
pub fn raw_to_ratio(value: u16) -> Result<types::Ratio, MapperError> {
|
pub fn raw_to_ratio(value: u16) -> Result<types::Ratio, MapperError> {
|
||||||
validators::validate_raw_compressor_ratio(value)?;
|
validators::validate_raw_compressor_ratio(value)?;
|
||||||
|
|
||||||
raw_to_ratio_value(value)
|
raw_to_ratio_value(value).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown ratio value: {:#04x}",
|
||||||
DecodingError::new(
|
value
|
||||||
format!("Unknown ratio value: {:#04x}", value)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ KNEE ------
|
// ------ KNEE ------
|
||||||
|
|
||||||
pub fn knee_db_to_raw(db: u16) -> Result<u16, MapperError> {
|
pub fn knee_db_to_raw(db: u16) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(db, mapper_constants::KNEE_DB_RANGE, || {
|
||||||
db,
|
format!("Invalid compressor knee: {db}")
|
||||||
mapper_constants::KNEE_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid compressor knee: {db}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(db)
|
Ok(db)
|
||||||
}
|
}
|
||||||
@@ -648,11 +626,9 @@ pub fn raw_to_knee_db(value: u16) -> Result<u16, MapperError> {
|
|||||||
// ------ THRESHOLD ------
|
// ------ THRESHOLD ------
|
||||||
|
|
||||||
pub fn threshold_db_to_raw(db: f32) -> Result<u16, MapperError> {
|
pub fn threshold_db_to_raw(db: f32) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(db, mapper_constants::THRESHOLD_DB_RANGE, || {
|
||||||
db,
|
format!("Invalid compressor threshold: {db}")
|
||||||
mapper_constants::THRESHOLD_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid compressor threshold: {db}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let db = (db * 2.0).round() / 2.0;
|
let db = (db * 2.0).round() / 2.0;
|
||||||
|
|
||||||
@@ -670,28 +646,22 @@ pub fn link_channels_to_raw(
|
|||||||
source: types::Channel,
|
source: types::Channel,
|
||||||
destinations: &[types::Channel],
|
destinations: &[types::Channel],
|
||||||
) -> Result<u8, MapperError> {
|
) -> Result<u8, MapperError> {
|
||||||
|
|
||||||
let mut mask = 0u8;
|
let mut mask = 0u8;
|
||||||
|
|
||||||
match source {
|
match source {
|
||||||
types::Channel::Input(source_channel) => {
|
types::Channel::Input(source_channel) => {
|
||||||
for destination in destinations {
|
for destination in destinations {
|
||||||
let types::Channel::Input(destination_channel) = destination else {
|
let types::Channel::Input(destination_channel) = destination else {
|
||||||
return Err(MapperError::Validation(
|
return Err(MapperError::Validation(ValidationError::value(
|
||||||
ValidationError::value(
|
"Input channel cannot link to output channel",
|
||||||
"Input channel cannot link to output channel"
|
)));
|
||||||
),
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if *destination_channel < source_channel {
|
if *destination_channel < source_channel {
|
||||||
return Err(MapperError::Validation(
|
return Err(MapperError::Validation(ValidationError::value(format!(
|
||||||
ValidationError::value(format!(
|
|
||||||
"{:?} cannot link to previous channel {:?}",
|
"{:?} cannot link to previous channel {:?}",
|
||||||
source_channel,
|
source_channel, destination_channel
|
||||||
destination_channel
|
))));
|
||||||
)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mask |= 1 << (*destination_channel as u8);
|
mask |= 1 << (*destination_channel as u8);
|
||||||
@@ -701,21 +671,16 @@ pub fn link_channels_to_raw(
|
|||||||
types::Channel::Output(source_channel) => {
|
types::Channel::Output(source_channel) => {
|
||||||
for destination in destinations {
|
for destination in destinations {
|
||||||
let types::Channel::Output(destination_channel) = destination else {
|
let types::Channel::Output(destination_channel) = destination else {
|
||||||
return Err(MapperError::Validation(
|
return Err(MapperError::Validation(ValidationError::value(
|
||||||
ValidationError::value(
|
"Output channel cannot link to input channel",
|
||||||
"Output channel cannot link to input channel"
|
)));
|
||||||
),
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if *destination_channel < source_channel {
|
if *destination_channel < source_channel {
|
||||||
return Err(MapperError::Validation(
|
return Err(MapperError::Validation(ValidationError::value(format!(
|
||||||
ValidationError::value(format!(
|
|
||||||
"{:?} cannot link to previous channel {:?}",
|
"{:?} cannot link to previous channel {:?}",
|
||||||
source_channel,
|
source_channel, destination_channel
|
||||||
destination_channel
|
))));
|
||||||
)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mask |= 1 << (*destination_channel as u8);
|
mask |= 1 << (*destination_channel as u8);
|
||||||
@@ -726,7 +691,10 @@ pub fn link_channels_to_raw(
|
|||||||
Ok(mask)
|
Ok(mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_link_channels(source_raw: u8, destination_mask: u8) -> Result<Vec<types::Channel>, MapperError> {
|
pub fn raw_to_link_channels(
|
||||||
|
source_raw: u8,
|
||||||
|
destination_mask: u8,
|
||||||
|
) -> Result<Vec<types::Channel>, MapperError> {
|
||||||
validators::validate_raw_link_channel(source_raw, destination_mask)?;
|
validators::validate_raw_link_channel(source_raw, destination_mask)?;
|
||||||
|
|
||||||
let source = raw_to_channel(source_raw)?;
|
let source = raw_to_channel(source_raw)?;
|
||||||
@@ -757,7 +725,9 @@ pub fn raw_to_link_channels(source_raw: u8, destination_mask: u8) -> Result<Vec<
|
|||||||
// ------ MATRIX GAIN ------
|
// ------ MATRIX GAIN ------
|
||||||
|
|
||||||
fn matrix_gain_split_raw() -> u16 {
|
fn matrix_gain_split_raw() -> u16 {
|
||||||
((mapper_constants::GAIN_SPLIT_DB - mapper_constants::MATRIX_GAIN_DB_RANGE.start()) / mapper_constants::GAIN_LOW_STEP_DB).round() as u16
|
((mapper_constants::GAIN_SPLIT_DB - mapper_constants::MATRIX_GAIN_DB_RANGE.start())
|
||||||
|
/ mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
|
.round() as u16
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quantize_matrix_gain(db: f32) -> f32 {
|
fn quantize_matrix_gain(db: f32) -> f32 {
|
||||||
@@ -769,19 +739,20 @@ fn quantize_matrix_gain(db: f32) -> f32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn matrix_gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
pub fn matrix_gain_db_to_raw(gain: f32) -> Result<u16, MapperError> {
|
||||||
validate_range(
|
validate_range(gain, mapper_constants::MATRIX_GAIN_DB_RANGE, || {
|
||||||
gain,
|
format!("Invalid matrix gain: {gain}")
|
||||||
mapper_constants::MATRIX_GAIN_DB_RANGE,
|
})?;
|
||||||
|| format!("Invalid matrix gain: {gain}"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let gain = quantize_matrix_gain(gain);
|
let gain = quantize_matrix_gain(gain);
|
||||||
|
|
||||||
if gain < mapper_constants::GAIN_SPLIT_DB {
|
if gain < mapper_constants::GAIN_SPLIT_DB {
|
||||||
Ok(((gain - mapper_constants::MATRIX_GAIN_DB_RANGE.start()) / mapper_constants::GAIN_LOW_STEP_DB).round() as u16)
|
Ok(((gain - mapper_constants::MATRIX_GAIN_DB_RANGE.start())
|
||||||
|
/ mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
|
.round() as u16)
|
||||||
} else {
|
} else {
|
||||||
Ok(matrix_gain_split_raw()
|
Ok(matrix_gain_split_raw()
|
||||||
+ ((gain - mapper_constants::GAIN_SPLIT_DB) / mapper_constants::GAIN_HIGH_STEP_DB).round() as u16)
|
+ ((gain - mapper_constants::GAIN_SPLIT_DB) / mapper_constants::GAIN_HIGH_STEP_DB)
|
||||||
|
.round() as u16)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -789,9 +760,11 @@ pub fn raw_to_matrix_gain(raw_gain: u16) -> Result<f32, MapperError> {
|
|||||||
validators::validate_raw_matrix_gain(raw_gain)?;
|
validators::validate_raw_matrix_gain(raw_gain)?;
|
||||||
|
|
||||||
if raw_gain < matrix_gain_split_raw() {
|
if raw_gain < matrix_gain_split_raw() {
|
||||||
Ok(mapper_constants::MATRIX_GAIN_DB_RANGE.start() + raw_gain as f32 * mapper_constants::GAIN_LOW_STEP_DB)
|
Ok(mapper_constants::MATRIX_GAIN_DB_RANGE.start()
|
||||||
|
+ raw_gain as f32 * mapper_constants::GAIN_LOW_STEP_DB)
|
||||||
} else {
|
} else {
|
||||||
Ok(mapper_constants::GAIN_SPLIT_DB + (raw_gain - matrix_gain_split_raw()) as f32 * mapper_constants::GAIN_HIGH_STEP_DB)
|
Ok(mapper_constants::GAIN_SPLIT_DB
|
||||||
|
+ (raw_gain - matrix_gain_split_raw()) as f32 * mapper_constants::GAIN_HIGH_STEP_DB)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -819,7 +792,12 @@ pub fn matrix_to_raw(input_channels: &[types::InputChannel]) -> Result<u8, Mappe
|
|||||||
pub fn raw_to_matrix(raw: u8) -> Result<Vec<types::InputChannel>, MapperError> {
|
pub fn raw_to_matrix(raw: u8) -> Result<Vec<types::InputChannel>, MapperError> {
|
||||||
validators::validate_raw_matrix_bitmask(raw)?;
|
validators::validate_raw_matrix_bitmask(raw)?;
|
||||||
|
|
||||||
Ok([types::InputChannel::InA, types::InputChannel::InB, types::InputChannel::InC, types::InputChannel::InD]
|
Ok([
|
||||||
|
types::InputChannel::InA,
|
||||||
|
types::InputChannel::InB,
|
||||||
|
types::InputChannel::InC,
|
||||||
|
types::InputChannel::InD,
|
||||||
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|&c| raw & matrix_input_mask(c) != 0)
|
.filter(|&c| raw & matrix_input_mask(c) != 0)
|
||||||
.collect())
|
.collect())
|
||||||
@@ -832,14 +810,11 @@ fn delay_raw_steps() -> f32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn delay_to_raw(delay: f32, unit: types::DelayUnit) -> Result<u8, MapperError> {
|
pub fn delay_to_raw(delay: f32, unit: types::DelayUnit) -> Result<u8, MapperError> {
|
||||||
|
|
||||||
let (delay_min, delay_max) = mapper_constants::delay_range(unit);
|
let (delay_min, delay_max) = mapper_constants::delay_range(unit);
|
||||||
|
|
||||||
validate_range(
|
validate_range(delay, delay_min..=delay_max, || {
|
||||||
delay,
|
format!("Invalid channel delay: {delay}")
|
||||||
delay_min..=delay_max,
|
})?;
|
||||||
|| format!("Invalid channel delay: {delay}")
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let raw = *protocol_constants::DELAY_RAW_RANGE.start() as f32
|
let raw = *protocol_constants::DELAY_RAW_RANGE.start() as f32
|
||||||
+ ((delay - delay_min) / (delay_max - delay_min) * delay_raw_steps()).round();
|
+ ((delay - delay_min) / (delay_max - delay_min) * delay_raw_steps()).round();
|
||||||
@@ -852,27 +827,39 @@ pub fn raw_to_delay(value: u8, unit: types::DelayUnit) -> Result<f32, MapperErro
|
|||||||
|
|
||||||
let (delay_min, delay_max) = mapper_constants::delay_range(unit);
|
let (delay_min, delay_max) = mapper_constants::delay_range(unit);
|
||||||
|
|
||||||
Ok(delay_min + ((value - protocol_constants::DELAY_RAW_RANGE.start()) as f32 / delay_raw_steps()) * (delay_max - delay_min))
|
Ok(delay_min
|
||||||
|
+ ((value - protocol_constants::DELAY_RAW_RANGE.start()) as f32 / delay_raw_steps())
|
||||||
|
* (delay_max - delay_min))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ COPY CHANNEL ------
|
// ------ COPY CHANNEL ------
|
||||||
|
|
||||||
pub fn copy_channel_to_raw(source: types::Channel, destination: types::Channel) -> Result<(u8, u8), MapperError> {
|
pub fn copy_channel_to_raw(
|
||||||
|
source: types::Channel,
|
||||||
|
destination: types::Channel,
|
||||||
|
) -> Result<(u8, u8), MapperError> {
|
||||||
if source == destination {
|
if source == destination {
|
||||||
return Err(ValidationError::Value("Source channel and destination channel can't be the same".to_string()).into())
|
return Err(ValidationError::Value(
|
||||||
|
"Source channel and destination channel can't be the same".to_string(),
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
match (&source, &destination) {
|
match (&source, &destination) {
|
||||||
(types::Channel::Input(_), types::Channel::Output(_))
|
(types::Channel::Input(_), types::Channel::Output(_))
|
||||||
| (types::Channel::Output(_), types::Channel::Input(_)) => {
|
| (types::Channel::Output(_), types::Channel::Input(_)) => {
|
||||||
return Err(ValidationError::Value(
|
return Err(ValidationError::Value(
|
||||||
"Channels need to be both inputs or outputs".to_string()).into(),
|
"Channels need to be both inputs or outputs".to_string(),
|
||||||
);
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((channel_to_raw_value(source), channel_to_raw_value(destination)))
|
Ok((
|
||||||
|
channel_to_raw_value(source),
|
||||||
|
channel_to_raw_value(destination),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -919,10 +906,22 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn channel_specific_raw_values() {
|
fn channel_specific_raw_values() {
|
||||||
assert_eq!(channel_to_raw(types::Channel::Input(types::InputChannel::InA)).unwrap(), 0x00);
|
assert_eq!(
|
||||||
assert_eq!(channel_to_raw(types::Channel::Input(types::InputChannel::InD)).unwrap(), 0x03);
|
channel_to_raw(types::Channel::Input(types::InputChannel::InA)).unwrap(),
|
||||||
assert_eq!(channel_to_raw(types::Channel::Output(types::OutputChannel::Out1)).unwrap(), 0x04);
|
0x00
|
||||||
assert_eq!(channel_to_raw(types::Channel::Output(types::OutputChannel::Out8)).unwrap(), 0x0B);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
channel_to_raw(types::Channel::Input(types::InputChannel::InD)).unwrap(),
|
||||||
|
0x03
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
channel_to_raw(types::Channel::Output(types::OutputChannel::Out1)).unwrap(),
|
||||||
|
0x04
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
channel_to_raw(types::Channel::Output(types::OutputChannel::Out8)).unwrap(),
|
||||||
|
0x0B
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
@@ -1045,9 +1044,15 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn peq_band_output_all_bands() {
|
fn peq_band_output_all_bands() {
|
||||||
let bands = [
|
let bands = [
|
||||||
types::PEQBand::B1, types::PEQBand::B2, types::PEQBand::B3,
|
types::PEQBand::B1,
|
||||||
types::PEQBand::B4, types::PEQBand::B5, types::PEQBand::B6,
|
types::PEQBand::B2,
|
||||||
types::PEQBand::B7, types::PEQBand::B8, types::PEQBand::B9,
|
types::PEQBand::B3,
|
||||||
|
types::PEQBand::B4,
|
||||||
|
types::PEQBand::B5,
|
||||||
|
types::PEQBand::B6,
|
||||||
|
types::PEQBand::B7,
|
||||||
|
types::PEQBand::B8,
|
||||||
|
types::PEQBand::B9,
|
||||||
];
|
];
|
||||||
let out_channel = types::Channel::Output(types::OutputChannel::Out1);
|
let out_channel = types::Channel::Output(types::OutputChannel::Out1);
|
||||||
for (i, band) in bands.into_iter().enumerate() {
|
for (i, band) in bands.into_iter().enumerate() {
|
||||||
@@ -1058,9 +1063,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn peq_band_input_allowed_bands() {
|
fn peq_band_input_allowed_bands() {
|
||||||
let bands = [
|
let bands = [
|
||||||
types::PEQBand::B1, types::PEQBand::B2, types::PEQBand::B3,
|
types::PEQBand::B1,
|
||||||
types::PEQBand::B4, types::PEQBand::B5, types::PEQBand::B6,
|
types::PEQBand::B2,
|
||||||
types::PEQBand::B7, types::PEQBand::B8,
|
types::PEQBand::B3,
|
||||||
|
types::PEQBand::B4,
|
||||||
|
types::PEQBand::B5,
|
||||||
|
types::PEQBand::B6,
|
||||||
|
types::PEQBand::B7,
|
||||||
|
types::PEQBand::B8,
|
||||||
];
|
];
|
||||||
let in_channel = types::Channel::Input(types::InputChannel::InA);
|
let in_channel = types::Channel::Input(types::InputChannel::InA);
|
||||||
for (i, band) in bands.into_iter().enumerate() {
|
for (i, band) in bands.into_iter().enumerate() {
|
||||||
@@ -1127,21 +1137,36 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn discrete_frequency_round_trip_all() {
|
fn discrete_frequency_round_trip_all() {
|
||||||
let freqs = [
|
let freqs = [
|
||||||
types::DiscreteFrequency::F20, types::DiscreteFrequency::F25,
|
types::DiscreteFrequency::F20,
|
||||||
types::DiscreteFrequency::F31_5, types::DiscreteFrequency::F40,
|
types::DiscreteFrequency::F25,
|
||||||
types::DiscreteFrequency::F50, types::DiscreteFrequency::F63,
|
types::DiscreteFrequency::F31_5,
|
||||||
types::DiscreteFrequency::F80, types::DiscreteFrequency::F100,
|
types::DiscreteFrequency::F40,
|
||||||
types::DiscreteFrequency::F125, types::DiscreteFrequency::F160,
|
types::DiscreteFrequency::F50,
|
||||||
types::DiscreteFrequency::F200, types::DiscreteFrequency::F250,
|
types::DiscreteFrequency::F63,
|
||||||
types::DiscreteFrequency::F315, types::DiscreteFrequency::F400,
|
types::DiscreteFrequency::F80,
|
||||||
types::DiscreteFrequency::F500, types::DiscreteFrequency::F630,
|
types::DiscreteFrequency::F100,
|
||||||
types::DiscreteFrequency::F800, types::DiscreteFrequency::F1000,
|
types::DiscreteFrequency::F125,
|
||||||
types::DiscreteFrequency::F1250, types::DiscreteFrequency::F1600,
|
types::DiscreteFrequency::F160,
|
||||||
types::DiscreteFrequency::F2000, types::DiscreteFrequency::F2500,
|
types::DiscreteFrequency::F200,
|
||||||
types::DiscreteFrequency::F3150, types::DiscreteFrequency::F4000,
|
types::DiscreteFrequency::F250,
|
||||||
types::DiscreteFrequency::F5000, types::DiscreteFrequency::F6300,
|
types::DiscreteFrequency::F315,
|
||||||
types::DiscreteFrequency::F8000, types::DiscreteFrequency::F10000,
|
types::DiscreteFrequency::F400,
|
||||||
types::DiscreteFrequency::F12500, types::DiscreteFrequency::F16000,
|
types::DiscreteFrequency::F500,
|
||||||
|
types::DiscreteFrequency::F630,
|
||||||
|
types::DiscreteFrequency::F800,
|
||||||
|
types::DiscreteFrequency::F1000,
|
||||||
|
types::DiscreteFrequency::F1250,
|
||||||
|
types::DiscreteFrequency::F1600,
|
||||||
|
types::DiscreteFrequency::F2000,
|
||||||
|
types::DiscreteFrequency::F2500,
|
||||||
|
types::DiscreteFrequency::F3150,
|
||||||
|
types::DiscreteFrequency::F4000,
|
||||||
|
types::DiscreteFrequency::F5000,
|
||||||
|
types::DiscreteFrequency::F6300,
|
||||||
|
types::DiscreteFrequency::F8000,
|
||||||
|
types::DiscreteFrequency::F10000,
|
||||||
|
types::DiscreteFrequency::F12500,
|
||||||
|
types::DiscreteFrequency::F16000,
|
||||||
types::DiscreteFrequency::F20000,
|
types::DiscreteFrequency::F20000,
|
||||||
];
|
];
|
||||||
for (i, f) in freqs.into_iter().enumerate() {
|
for (i, f) in freqs.into_iter().enumerate() {
|
||||||
@@ -1158,16 +1183,26 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn crossover_filter_round_trip_all() {
|
fn crossover_filter_round_trip_all() {
|
||||||
let filters = [
|
let filters = [
|
||||||
types::CrossoverFilter::Bypass, types::CrossoverFilter::Bw6,
|
types::CrossoverFilter::Bypass,
|
||||||
types::CrossoverFilter::Bl6, types::CrossoverFilter::Bw12,
|
types::CrossoverFilter::Bw6,
|
||||||
types::CrossoverFilter::Bl12, types::CrossoverFilter::Lk12,
|
types::CrossoverFilter::Bl6,
|
||||||
types::CrossoverFilter::Bw18, types::CrossoverFilter::Bl18,
|
types::CrossoverFilter::Bw12,
|
||||||
types::CrossoverFilter::Bw24, types::CrossoverFilter::Bl24,
|
types::CrossoverFilter::Bl12,
|
||||||
types::CrossoverFilter::Lk24, types::CrossoverFilter::Bw30,
|
types::CrossoverFilter::Lk12,
|
||||||
types::CrossoverFilter::Bl30, types::CrossoverFilter::Bw36,
|
types::CrossoverFilter::Bw18,
|
||||||
types::CrossoverFilter::Bl36, types::CrossoverFilter::Lk36,
|
types::CrossoverFilter::Bl18,
|
||||||
types::CrossoverFilter::Bw42, types::CrossoverFilter::Bl42,
|
types::CrossoverFilter::Bw24,
|
||||||
types::CrossoverFilter::Bw48, types::CrossoverFilter::Bl48,
|
types::CrossoverFilter::Bl24,
|
||||||
|
types::CrossoverFilter::Lk24,
|
||||||
|
types::CrossoverFilter::Bw30,
|
||||||
|
types::CrossoverFilter::Bl30,
|
||||||
|
types::CrossoverFilter::Bw36,
|
||||||
|
types::CrossoverFilter::Bl36,
|
||||||
|
types::CrossoverFilter::Lk36,
|
||||||
|
types::CrossoverFilter::Bw42,
|
||||||
|
types::CrossoverFilter::Bl42,
|
||||||
|
types::CrossoverFilter::Bw48,
|
||||||
|
types::CrossoverFilter::Bl48,
|
||||||
types::CrossoverFilter::Lk48,
|
types::CrossoverFilter::Lk48,
|
||||||
];
|
];
|
||||||
for (i, f) in filters.into_iter().enumerate() {
|
for (i, f) in filters.into_iter().enumerate() {
|
||||||
@@ -1207,10 +1242,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn peq_filter_round_trip_all() {
|
fn peq_filter_round_trip_all() {
|
||||||
let filters = [
|
let filters = [
|
||||||
types::PEQFilter::Peak, types::PEQFilter::LowShelf,
|
types::PEQFilter::Peak,
|
||||||
types::PEQFilter::HighShelf, types::PEQFilter::Lp6Db,
|
types::PEQFilter::LowShelf,
|
||||||
types::PEQFilter::Lp12Db, types::PEQFilter::Hp6Db,
|
types::PEQFilter::HighShelf,
|
||||||
types::PEQFilter::Hp12Db, types::PEQFilter::AllPass1,
|
types::PEQFilter::Lp6Db,
|
||||||
|
types::PEQFilter::Lp12Db,
|
||||||
|
types::PEQFilter::Hp6Db,
|
||||||
|
types::PEQFilter::Hp12Db,
|
||||||
|
types::PEQFilter::AllPass1,
|
||||||
types::PEQFilter::AllPass2,
|
types::PEQFilter::AllPass2,
|
||||||
];
|
];
|
||||||
for (i, f) in filters.into_iter().enumerate() {
|
for (i, f) in filters.into_iter().enumerate() {
|
||||||
@@ -1227,11 +1266,21 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn ratio_round_trip_all() {
|
fn ratio_round_trip_all() {
|
||||||
let ratios = [
|
let ratios = [
|
||||||
types::Ratio::Ratio1_1, types::Ratio::Ratio1_1_1, types::Ratio::Ratio1_1_3,
|
types::Ratio::Ratio1_1,
|
||||||
types::Ratio::Ratio1_1_5, types::Ratio::Ratio1_1_7, types::Ratio::Ratio1_2,
|
types::Ratio::Ratio1_1_1,
|
||||||
types::Ratio::Ratio1_2_5, types::Ratio::Ratio1_3, types::Ratio::Ratio1_3_5,
|
types::Ratio::Ratio1_1_3,
|
||||||
types::Ratio::Ratio1_4, types::Ratio::Ratio1_5, types::Ratio::Ratio1_6,
|
types::Ratio::Ratio1_1_5,
|
||||||
types::Ratio::Ratio1_8, types::Ratio::Ratio1_10, types::Ratio::Ratio1_20,
|
types::Ratio::Ratio1_1_7,
|
||||||
|
types::Ratio::Ratio1_2,
|
||||||
|
types::Ratio::Ratio1_2_5,
|
||||||
|
types::Ratio::Ratio1_3,
|
||||||
|
types::Ratio::Ratio1_3_5,
|
||||||
|
types::Ratio::Ratio1_4,
|
||||||
|
types::Ratio::Ratio1_5,
|
||||||
|
types::Ratio::Ratio1_6,
|
||||||
|
types::Ratio::Ratio1_8,
|
||||||
|
types::Ratio::Ratio1_10,
|
||||||
|
types::Ratio::Ratio1_20,
|
||||||
types::Ratio::Limit,
|
types::Ratio::Limit,
|
||||||
];
|
];
|
||||||
for (i, r) in ratios.into_iter().enumerate() {
|
for (i, r) in ratios.into_iter().enumerate() {
|
||||||
|
|||||||
@@ -16,20 +16,14 @@ pub const EQ_GAIN_DB_RANGE: RangeInclusive<f32> = -12.0..=12.0;
|
|||||||
pub const PEQ_Q_RANGE: RangeInclusive<f32> = 0.40..=128.0;
|
pub const PEQ_Q_RANGE: RangeInclusive<f32> = 0.40..=128.0;
|
||||||
pub fn peq_q_range(filter: PEQFilter) -> RangeInclusive<f32> {
|
pub fn peq_q_range(filter: PEQFilter) -> RangeInclusive<f32> {
|
||||||
match filter {
|
match filter {
|
||||||
PEQFilter::Peak
|
PEQFilter::Peak | PEQFilter::AllPass1 | PEQFilter::AllPass2 => 0.40..=128.0,
|
||||||
| PEQFilter::AllPass1
|
|
||||||
| PEQFilter::AllPass2 => {
|
|
||||||
0.40..=128.0
|
|
||||||
}
|
|
||||||
|
|
||||||
PEQFilter::LowShelf
|
PEQFilter::LowShelf
|
||||||
| PEQFilter::HighShelf
|
| PEQFilter::HighShelf
|
||||||
| PEQFilter::Lp6Db
|
| PEQFilter::Lp6Db
|
||||||
| PEQFilter::Lp12Db
|
| PEQFilter::Lp12Db
|
||||||
| PEQFilter::Hp6Db
|
| PEQFilter::Hp6Db
|
||||||
| PEQFilter::Hp12Db => {
|
| PEQFilter::Hp12Db => 0.40..=3.0,
|
||||||
0.40..=3.0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
use crate::common::errors::{
|
use crate::common::errors::{DecodingError, EncodingError};
|
||||||
DecodingError,
|
|
||||||
EncodingError,
|
|
||||||
};
|
|
||||||
|
|
||||||
// ------ STRING ------
|
// ------ STRING ------
|
||||||
|
|
||||||
pub fn encode_ascii(value: &str, length: usize, padding: u8) -> Result<Vec<u8>, EncodingError> {
|
pub fn encode_ascii(value: &str, length: usize, padding: u8) -> Result<Vec<u8>, EncodingError> {
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
return Err(EncodingError::new(format!("Invalid string length: {}", length)));
|
return Err(EncodingError::new(format!(
|
||||||
|
"Invalid string length: {}",
|
||||||
|
length
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !value.is_ascii() {
|
if !value.is_ascii() {
|
||||||
return Err(EncodingError::new("String must contain ASCII characters only"));
|
return Err(EncodingError::new(
|
||||||
|
"String must contain ASCII characters only",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let raw = value.as_bytes();
|
let raw = value.as_bytes();
|
||||||
@@ -38,7 +40,9 @@ pub fn decode_ascii(raw: &[u8], strip: bool) -> Result<String, DecodingError> {
|
|||||||
let result = String::from_utf8(raw.to_vec()).expect("raw is ASCII, so always valid UTF-8");
|
let result = String::from_utf8(raw.to_vec()).expect("raw is ASCII, so always valid UTF-8");
|
||||||
|
|
||||||
Ok(if strip {
|
Ok(if strip {
|
||||||
result.trim_end_matches(['\u{0020}', '\u{0000}']).to_string()
|
result
|
||||||
|
.trim_end_matches(['\u{0020}', '\u{0000}'])
|
||||||
|
.to_string()
|
||||||
} else {
|
} else {
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
@@ -65,15 +69,10 @@ pub fn decode_float16(lo: u8, hi: u8) -> Result<f32, DecodingError> {
|
|||||||
return Ok(f32::NAN);
|
return Ok(f32::NAN);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(1.0 + mantissa as f32 / 1024.0)
|
(1.0 + mantissa as f32 / 1024.0) * 2f32.powi(exponent as i32 - 15)
|
||||||
* 2f32.powi(exponent as i32 - 15)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if sign == 1 {
|
if sign == 1 { Ok(-result) } else { Ok(result) }
|
||||||
Ok(-result)
|
|
||||||
} else {
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -92,10 +91,7 @@ mod tests {
|
|||||||
fn test_encode_ascii_pads_short_string() {
|
fn test_encode_ascii_pads_short_string() {
|
||||||
let result = encode_ascii("hi", 5, b'\0');
|
let result = encode_ascii("hi", 5, b'\0');
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(result.unwrap(), vec![b'h', b'i', 0, 0, 0]);
|
||||||
result.unwrap(),
|
|
||||||
vec![b'h', b'i', 0, 0, 0]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -103,10 +99,7 @@ mod tests {
|
|||||||
let result = encode_ascii("hello", 0, b' ');
|
let result = encode_ascii("hello", 0, b' ');
|
||||||
|
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(
|
assert_eq!(result.unwrap_err().to_string(), "Invalid string length: 0");
|
||||||
result.unwrap_err().to_string(),
|
|
||||||
"Invalid string length: 0"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -165,13 +158,9 @@ mod tests {
|
|||||||
let result = decode_ascii(&raw, false);
|
let result = decode_ascii(&raw, false);
|
||||||
|
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(
|
assert_eq!(result.unwrap_err().to_string(), "Invalid ASCII data");
|
||||||
result.unwrap_err().to_string(),
|
|
||||||
"Invalid ASCII data"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------ FLOAT TESTS ------
|
// ------ FLOAT TESTS ------
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
+40
-57
@@ -1,25 +1,15 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{
|
||||||
errors::{DecodingError, MapperError},
|
errors::{DecodingError, MapperError},
|
||||||
validator::{
|
validator::{validate_ascii_padded, validate_range},
|
||||||
validate_ascii_padded,
|
|
||||||
validate_range,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
dsp408::{
|
dsp408::{
|
||||||
protocol::{
|
protocol::{constants as protocol_constants, validators},
|
||||||
constants as protocol_constants,
|
|
||||||
validators,
|
|
||||||
},
|
|
||||||
types,
|
types,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{constants, decode_ascii, encode_ascii};
|
||||||
constants,
|
|
||||||
decode_ascii,
|
|
||||||
encode_ascii,
|
|
||||||
};
|
|
||||||
|
|
||||||
// ------ DELAY ------
|
// ------ DELAY ------
|
||||||
|
|
||||||
@@ -40,55 +30,43 @@ fn raw_to_delay_unit_value(value: u8) -> Option<types::DelayUnit> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delay_unit_to_raw(
|
pub fn delay_unit_to_raw(unit: types::DelayUnit) -> Result<u8, MapperError> {
|
||||||
unit: types::DelayUnit,
|
|
||||||
) -> Result<u8, MapperError> {
|
|
||||||
Ok(delay_unit_to_raw_value(unit))
|
Ok(delay_unit_to_raw_value(unit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_delay_unit(
|
pub fn raw_to_delay_unit(value: u8) -> Result<types::DelayUnit, MapperError> {
|
||||||
value: u8,
|
|
||||||
) -> Result<types::DelayUnit, MapperError> {
|
|
||||||
validators::validate_raw_delay_unit(value)?;
|
validators::validate_raw_delay_unit(value)?;
|
||||||
|
|
||||||
raw_to_delay_unit_value(value)
|
raw_to_delay_unit_value(value).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown delay unit value: {:#04x}",
|
||||||
DecodingError::new(
|
value
|
||||||
format!("Unknown delay unit value: {:#04x}", value),
|
)))
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ PRESET INDEX ------
|
// ------ PRESET INDEX ------
|
||||||
|
|
||||||
pub fn preset_index_to_raw(index: usize) -> Result<u8, MapperError> {
|
pub fn preset_index_to_raw(index: usize) -> Result<u8, MapperError> {
|
||||||
validate_range(
|
validate_range(index, 0..=constants::PRESET_INDEX_MAX, || {
|
||||||
index,
|
format!("Invalid preset index {}", index)
|
||||||
0..=constants::PRESET_INDEX_MAX,
|
})?;
|
||||||
|| format!("Invalid preset index {}", index)
|
|
||||||
)?;
|
|
||||||
Ok(index as u8)
|
Ok(index as u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preset_index_without_factory_to_raw(index: usize) -> Result<u8, MapperError> {
|
pub fn preset_index_without_factory_to_raw(index: usize) -> Result<u8, MapperError> {
|
||||||
validate_range(
|
validate_range(index, 1..=constants::PRESET_INDEX_MAX, || {
|
||||||
index,
|
format!("Invalid preset index {}", index)
|
||||||
1..=constants::PRESET_INDEX_MAX,
|
})?;
|
||||||
|| format!("Invalid preset index {}", index)
|
|
||||||
)?;
|
|
||||||
Ok(index as u8 - 1)
|
Ok(index as u8 - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ CONFIG CHUNK ------
|
// ------ CONFIG CHUNK ------
|
||||||
|
|
||||||
pub fn config_chunk_to_raw(index: usize) -> Result<u8, MapperError> {
|
pub fn config_chunk_to_raw(index: usize) -> Result<u8, MapperError> {
|
||||||
validate_range(
|
validate_range(index, 0..=constants::CONFIG_CHUNK_INDEX_MAX, || {
|
||||||
index,
|
format!("Invalid config index: {:#04x}", index)
|
||||||
0..=constants::CONFIG_CHUNK_INDEX_MAX,
|
})?;
|
||||||
|| format!("Invalid config index: {:#04x}", index)
|
|
||||||
)?;
|
|
||||||
Ok(index as u8)
|
Ok(index as u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,10 +77,14 @@ pub fn preset_name_to_raw(name: &str) -> Result<Vec<u8>, MapperError> {
|
|||||||
name.as_bytes(),
|
name.as_bytes(),
|
||||||
protocol_constants::PRESET_NAME_LENGTH,
|
protocol_constants::PRESET_NAME_LENGTH,
|
||||||
protocol_constants::PRESET_NAME_PADDING,
|
protocol_constants::PRESET_NAME_PADDING,
|
||||||
"Preset name"
|
"Preset name",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(encode_ascii(name, protocol_constants::PRESET_NAME_LENGTH, protocol_constants::PRESET_NAME_PADDING)?)
|
Ok(encode_ascii(
|
||||||
|
name,
|
||||||
|
protocol_constants::PRESET_NAME_LENGTH,
|
||||||
|
protocol_constants::PRESET_NAME_PADDING,
|
||||||
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_to_preset_name(raw_name: &[u8]) -> Result<String, MapperError> {
|
pub fn raw_to_preset_name(raw_name: &[u8]) -> Result<String, MapperError> {
|
||||||
@@ -117,7 +99,9 @@ fn input_source_to_raw_value(source: types::InputType) -> u8 {
|
|||||||
match source {
|
match source {
|
||||||
types::InputType::Analog => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start(),
|
types::InputType::Analog => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start(),
|
||||||
types::InputType::PinkNoise => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 1,
|
types::InputType::PinkNoise => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 1,
|
||||||
types::InputType::WhiteNoise => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 2,
|
types::InputType::WhiteNoise => {
|
||||||
|
*protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 2
|
||||||
|
}
|
||||||
types::InputType::SineWave => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 3,
|
types::InputType::SineWave => *protocol_constants::INPUT_SOURCE_TYPE_RAW_RANGE.start() + 3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,13 +123,11 @@ pub fn input_source_to_raw(source: types::InputType) -> Result<u8, MapperError>
|
|||||||
pub fn raw_to_input_source(value: u8) -> Result<types::InputType, MapperError> {
|
pub fn raw_to_input_source(value: u8) -> Result<types::InputType, MapperError> {
|
||||||
validators::validate_raw_input_source(value)?;
|
validators::validate_raw_input_source(value)?;
|
||||||
|
|
||||||
raw_to_input_source_value(value)
|
raw_to_input_source_value(value).ok_or_else(|| {
|
||||||
.ok_or_else(|| {
|
MapperError::Decoding(DecodingError::new(format!(
|
||||||
MapperError::Decoding(
|
"Unknown input value: {:#04x}",
|
||||||
DecodingError::new(
|
value
|
||||||
format!("Unknown input value: {:#04x}", value)
|
)))
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,10 +138,14 @@ pub fn password_to_raw(password: &str) -> Result<Vec<u8>, MapperError> {
|
|||||||
password.as_bytes(),
|
password.as_bytes(),
|
||||||
protocol_constants::PASSWORD_LENGTH,
|
protocol_constants::PASSWORD_LENGTH,
|
||||||
protocol_constants::PASSWORD_PADDING,
|
protocol_constants::PASSWORD_PADDING,
|
||||||
"Password"
|
"Password",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(encode_ascii(password, protocol_constants::PASSWORD_LENGTH, protocol_constants::PASSWORD_PADDING)?)
|
Ok(encode_ascii(
|
||||||
|
password,
|
||||||
|
protocol_constants::PASSWORD_LENGTH,
|
||||||
|
protocol_constants::PASSWORD_PADDING,
|
||||||
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -237,10 +223,7 @@ mod tests {
|
|||||||
|
|
||||||
let raw = preset_name_to_raw(name).unwrap();
|
let raw = preset_name_to_raw(name).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(raw_to_preset_name(&raw).unwrap(), "DSP408",);
|
||||||
raw_to_preset_name(&raw).unwrap(),
|
|
||||||
"DSP408",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
mod channel;
|
mod channel;
|
||||||
|
pub(crate) mod constants;
|
||||||
mod encoding;
|
mod encoding;
|
||||||
mod global;
|
mod global;
|
||||||
pub(crate) mod response;
|
pub(crate) mod response;
|
||||||
pub(crate) mod constants;
|
|
||||||
|
|
||||||
pub use channel::*;
|
pub use channel::*;
|
||||||
pub use encoding::*;
|
pub use encoding::*;
|
||||||
|
|||||||
+39
-130
@@ -2,28 +2,13 @@ use crate::{
|
|||||||
common::errors::ProtocolError,
|
common::errors::ProtocolError,
|
||||||
dsp408::{
|
dsp408::{
|
||||||
protocol::{
|
protocol::{
|
||||||
constants::{
|
constants::{METERS_HIGH_STEP, METERS_LOW_STEP, METERS_SPLIT, METERS_SPLIT_LEVEL_DB},
|
||||||
METERS_HIGH_STEP,
|
|
||||||
METERS_LOW_STEP,
|
|
||||||
METERS_SPLIT,
|
|
||||||
METERS_SPLIT_LEVEL_DB,
|
|
||||||
},
|
|
||||||
frame::Frame,
|
frame::Frame,
|
||||||
},
|
},
|
||||||
types::{
|
types::{
|
||||||
Acknowledgement,
|
Acknowledgement, AuthenticationResult, ConfigChunk, CurrentPreset, DeviceFlags,
|
||||||
AuthenticationResult,
|
DeviceInfo, DeviceResponseType, HandshakeAck, Meters, ModifiedPreset, NotImplemented,
|
||||||
ConfigChunk,
|
PresetName, UnknownResponse,
|
||||||
CurrentPreset,
|
|
||||||
DeviceFlags,
|
|
||||||
DeviceInfo,
|
|
||||||
DeviceResponseType,
|
|
||||||
HandshakeAck,
|
|
||||||
Meters,
|
|
||||||
ModifiedPreset,
|
|
||||||
NotImplemented,
|
|
||||||
PresetName,
|
|
||||||
UnknownResponse,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -66,8 +51,7 @@ pub fn map_response(frame: &Frame) -> Result<DeviceResponseType, ProtocolError>
|
|||||||
return Err(ProtocolError::new("Missing device name payload"));
|
return Err(ProtocolError::new("Missing device name payload"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = decode_ascii(payload, false)
|
let name = decode_ascii(payload, false).map_err(|e| ProtocolError::new(e.0))?;
|
||||||
.map_err(|e| ProtocolError::new(e.0))?;
|
|
||||||
|
|
||||||
return Ok(DeviceResponseType::DeviceInfo(DeviceInfo { name }));
|
return Ok(DeviceResponseType::DeviceInfo(DeviceInfo { name }));
|
||||||
}
|
}
|
||||||
@@ -131,8 +115,7 @@ pub fn map_response(frame: &Frame) -> Result<DeviceResponseType, ProtocolError>
|
|||||||
return Err(ProtocolError::new("Missing preset index"));
|
return Err(ProtocolError::new("Missing preset index"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = decode_ascii(&payload[1..], false)
|
let name = decode_ascii(&payload[1..], false).map_err(|e| ProtocolError::new(e.0))?;
|
||||||
.map_err(|e| ProtocolError::new(e.0))?;
|
|
||||||
|
|
||||||
return Ok(DeviceResponseType::PresetName(PresetName {
|
return Ok(DeviceResponseType::PresetName(PresetName {
|
||||||
index: payload[0],
|
index: payload[0],
|
||||||
@@ -174,9 +157,11 @@ pub fn map_response(frame: &Frame) -> Result<DeviceResponseType, ProtocolError>
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(DeviceResponseType::AuthenticationResult(AuthenticationResult {
|
return Ok(DeviceResponseType::AuthenticationResult(
|
||||||
|
AuthenticationResult {
|
||||||
success: payload[1] == 0x01,
|
success: payload[1] == 0x01,
|
||||||
}));
|
},
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command `0x40` (Get Level Meters)
|
// Command `0x40` (Get Level Meters)
|
||||||
@@ -224,9 +209,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
DeviceResponseType::Acknowledgement(Acknowledgement {
|
DeviceResponseType::Acknowledgement(Acknowledgement { success: true })
|
||||||
success: true
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,10 +217,7 @@ mod tests {
|
|||||||
fn not_implemented() {
|
fn not_implemented() {
|
||||||
let result = map_response(&frame(0x02, vec![])).unwrap();
|
let result = map_response(&frame(0x02, vec![])).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(result, DeviceResponseType::NotImplemented(NotImplemented));
|
||||||
result,
|
|
||||||
DeviceResponseType::NotImplemented(NotImplemented)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -246,9 +226,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
DeviceResponseType::HandshakeAck(HandshakeAck {
|
DeviceResponseType::HandshakeAck(HandshakeAck { ack: Some(1) })
|
||||||
ack: Some(1)
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,8 +239,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn device_name() {
|
fn device_name() {
|
||||||
let result = map_response(&frame(0x13, b"TestDevice".to_vec()))
|
let result = map_response(&frame(0x13, b"TestDevice".to_vec())).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
@@ -274,47 +251,34 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn device_name_missing_payload() {
|
fn device_name_missing_payload() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x13, vec![])).is_err());
|
||||||
map_response(&frame(0x13, vec![]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn device_name_invalid_ascii() {
|
fn device_name_invalid_ascii() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x13, vec![0xff])).is_err());
|
||||||
map_response(&frame(0x13, vec![0xff]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn current_preset() {
|
fn current_preset() {
|
||||||
let result = map_response(&frame(0x14, vec![5]))
|
let result = map_response(&frame(0x14, vec![5])).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
DeviceResponseType::CurrentPreset(CurrentPreset {
|
DeviceResponseType::CurrentPreset(CurrentPreset { index: 5 })
|
||||||
index: 5
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn current_preset_wrong_length_errors() {
|
fn current_preset_wrong_length_errors() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x14, vec![1, 2])).is_err());
|
||||||
map_response(&frame(0x14, vec![1, 2]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn modified_preset() {
|
fn modified_preset() {
|
||||||
let payload = vec![0xff; 20];
|
let payload = vec![0xff; 20];
|
||||||
|
|
||||||
let result = map_response(&frame(0x22, payload))
|
let result = map_response(&frame(0x22, payload)).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
DeviceResponseType::ModifiedPreset(p) => {
|
DeviceResponseType::ModifiedPreset(p) => {
|
||||||
@@ -327,19 +291,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn modified_preset_invalid_length() {
|
fn modified_preset_invalid_length() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x22, vec![0; 19])).is_err());
|
||||||
map_response(&frame(0x22, vec![0; 19]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn config_chunk() {
|
fn config_chunk() {
|
||||||
let result = map_response(&frame(
|
let result = map_response(&frame(0x24, vec![3, 0xaa, 0xbb])).unwrap();
|
||||||
0x24,
|
|
||||||
vec![3, 0xaa, 0xbb],
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
@@ -352,8 +309,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn config_chunk_empty_payload_defaults_to_zero() {
|
fn config_chunk_empty_payload_defaults_to_zero() {
|
||||||
let result = map_response(&frame(0x24, vec![]))
|
let result = map_response(&frame(0x24, vec![])).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
@@ -366,19 +322,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn config_chunk_invalid_index() {
|
fn config_chunk_invalid_index() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x24, vec![0x1d])).is_err());
|
||||||
map_response(&frame(0x24, vec![0x1d]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn preset_name() {
|
fn preset_name() {
|
||||||
let result = map_response(&frame(
|
let result = map_response(&frame(0x29, vec![2, b'A', b'm', b'p'])).unwrap();
|
||||||
0x29,
|
|
||||||
vec![2, b'A', b'm', b'p'],
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
@@ -391,18 +340,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn preset_name_missing_index() {
|
fn preset_name_missing_index() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x29, vec![])).is_err());
|
||||||
map_response(&frame(0x29, vec![]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn preset_name_invalid_ascii() {
|
fn preset_name_invalid_ascii() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x29, vec![1, 0xff])).is_err());
|
||||||
map_response(&frame(0x29, vec![1, 0xff]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -410,23 +353,17 @@ mod tests {
|
|||||||
let mut payload = vec![0; 6];
|
let mut payload = vec![0; 6];
|
||||||
payload[5] = 1;
|
payload[5] = 1;
|
||||||
|
|
||||||
let result = map_response(&frame(0x2c, payload))
|
let result = map_response(&frame(0x2c, payload)).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
DeviceResponseType::DeviceFlags(DeviceFlags {
|
DeviceResponseType::DeviceFlags(DeviceFlags { is_locked: true })
|
||||||
is_locked: true
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn device_flags_invalid_length() {
|
fn device_flags_invalid_length() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x2c, vec![0; 5])).is_err());
|
||||||
map_response(&frame(0x2c, vec![0; 5]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -434,52 +371,32 @@ mod tests {
|
|||||||
let mut payload = vec![0; 6];
|
let mut payload = vec![0; 6];
|
||||||
payload[5] = 2;
|
payload[5] = 2;
|
||||||
|
|
||||||
assert!(
|
assert!(map_response(&frame(0x2c, payload)).is_err());
|
||||||
map_response(&frame(0x2c, payload))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn authentication_success() {
|
fn authentication_success() {
|
||||||
let result = map_response(&frame(
|
let result = map_response(&frame(0x2d, vec![0, 1])).unwrap();
|
||||||
0x2d,
|
|
||||||
vec![0, 1],
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
DeviceResponseType::AuthenticationResult(
|
DeviceResponseType::AuthenticationResult(AuthenticationResult { success: true })
|
||||||
AuthenticationResult {
|
|
||||||
success: true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn authentication_missing_result() {
|
fn authentication_missing_result() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x2d, vec![0])).is_err());
|
||||||
map_response(&frame(0x2d, vec![0]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn authentication_invalid_flag() {
|
fn authentication_invalid_flag() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x2d, vec![0, 2])).is_err());
|
||||||
map_response(&frame(0x2d, vec![0, 2]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn meters_invalid_length() {
|
fn meters_invalid_length() {
|
||||||
assert!(
|
assert!(map_response(&frame(0x40, vec![0; 10])).is_err());
|
||||||
map_response(&frame(0x40, vec![0; 10]))
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -494,8 +411,7 @@ mod tests {
|
|||||||
payload[i + 1] = 0x3c;
|
payload[i + 1] = 0x3c;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = map_response(&frame(0x40, payload))
|
let result = map_response(&frame(0x40, payload)).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
DeviceResponseType::Meters(m) => {
|
DeviceResponseType::Meters(m) => {
|
||||||
@@ -511,21 +427,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unknown_command_falls_through() {
|
fn unknown_command_falls_through() {
|
||||||
let result = map_response(&frame(
|
let result = map_response(&frame(0x99, vec![0xaa])).unwrap();
|
||||||
0x99,
|
|
||||||
vec![0xaa],
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
DeviceResponseType::UnknownResponse(r) => {
|
DeviceResponseType::UnknownResponse(r) => {
|
||||||
assert_eq!(r.cmd, "0x99");
|
assert_eq!(r.cmd, "0x99");
|
||||||
assert_eq!(r.raw, vec![0xaa]);
|
assert_eq!(r.raw, vec![0xaa]);
|
||||||
}
|
}
|
||||||
other => panic!(
|
other => panic!("expected UnknownResponse, got {:?}", other),
|
||||||
"expected UnknownResponse, got {:?}",
|
|
||||||
other
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
mod protocol;
|
|
||||||
mod client;
|
mod client;
|
||||||
mod mapper;
|
mod mapper;
|
||||||
|
mod protocol;
|
||||||
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
|||||||
+73
-380
@@ -1,15 +1,9 @@
|
|||||||
use crate::common::errors::ValidationError;
|
use crate::common::errors::ValidationError;
|
||||||
|
|
||||||
use super::{
|
use super::{frame::build_frame, validators::*};
|
||||||
frame::build_frame,
|
|
||||||
validators::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn split_u16(value: u16) -> [u8; 2] {
|
fn split_u16(value: u16) -> [u8; 2] {
|
||||||
[
|
[(value & 0xff) as u8, (value >> 8) as u8]
|
||||||
(value & 0xff) as u8,
|
|
||||||
(value >> 8) as u8,
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x10 Start
|
// Command 0x10 Start
|
||||||
@@ -38,41 +32,24 @@ pub fn build_get_current_preset() -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x15 Delay Unit
|
// Command 0x15 Delay Unit
|
||||||
pub fn build_set_delay_unit(
|
pub fn build_set_delay_unit(raw_unit: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_unit: u8
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
validate_raw_delay_unit(raw_unit)?;
|
validate_raw_delay_unit(raw_unit)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x15, &[raw_unit]))
|
||||||
0x15,
|
|
||||||
&[raw_unit]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x20 Recall
|
// Command 0x20 Recall
|
||||||
pub fn build_recall(
|
pub fn build_recall(raw_index: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_index:u8
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_preset_index(raw_index)?;
|
validate_raw_preset_index(raw_index)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x20, &[raw_index]))
|
||||||
0x20,
|
|
||||||
&[raw_index]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x21 Store preset
|
// Command 0x21 Store preset
|
||||||
pub fn build_store_current_configuration(
|
pub fn build_store_current_configuration(raw_index: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_index:u8
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_store_preset_index(raw_index)?;
|
validate_raw_store_preset_index(raw_index)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x21, &[raw_index]))
|
||||||
0x21,
|
|
||||||
&[raw_index]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x22
|
// Command 0x22
|
||||||
@@ -86,56 +63,31 @@ pub fn build_factory_reset() -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x26
|
// Command 0x26
|
||||||
pub fn build_store_current_preset_name(
|
pub fn build_store_current_preset_name(raw_name: &[u8]) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_name:&[u8]
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_preset_name(raw_name)?;
|
validate_raw_preset_name(raw_name)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x26, raw_name))
|
||||||
0x26,
|
|
||||||
raw_name
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x27
|
// Command 0x27
|
||||||
pub fn build_config_chunk(
|
pub fn build_config_chunk(raw_index: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_index:u8
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_config_chunk_index(raw_index)?;
|
validate_raw_config_chunk_index(raw_index)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x27, &[raw_index]))
|
||||||
0x27,
|
|
||||||
&[raw_index]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x29
|
// Command 0x29
|
||||||
pub fn build_get_preset_name(
|
pub fn build_get_preset_name(raw_index: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_index:u8
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_get_preset_index(raw_index)?;
|
validate_raw_get_preset_index(raw_index)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x29, &[raw_index]))
|
||||||
0x29,
|
|
||||||
&[raw_index]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x2A
|
// Command 0x2A
|
||||||
pub fn build_copy_channel(
|
pub fn build_copy_channel(source: u8, destination: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
source: u8,
|
|
||||||
destination: u8,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_copy_channels(source, destination)?;
|
validate_raw_copy_channels(source, destination)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x2A, &[source, destination]))
|
||||||
0x2A,
|
|
||||||
&[source, destination],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x2C
|
// Command 0x2C
|
||||||
@@ -144,29 +96,17 @@ pub fn build_get_device_flags() -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x2D
|
// Command 0x2D
|
||||||
pub fn build_unlock_device(
|
pub fn build_unlock_device(password: &[u8]) -> Result<Vec<u8>, ValidationError> {
|
||||||
password:&[u8]
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_password(password)?;
|
validate_raw_password(password)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x2d, password))
|
||||||
0x2d,
|
|
||||||
password
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x2F
|
// Command 0x2F
|
||||||
pub fn build_lock_device(
|
pub fn build_lock_device(password: &[u8]) -> Result<Vec<u8>, ValidationError> {
|
||||||
password:&[u8]
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_password(password)?;
|
validate_raw_password(password)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x2f, password))
|
||||||
0x2f,
|
|
||||||
password
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x30 (Set Compressor)
|
// Command 0x30 (Set Compressor)
|
||||||
@@ -178,7 +118,6 @@ pub fn build_set_compressor(
|
|||||||
raw_knee: u16,
|
raw_knee: u16,
|
||||||
raw_threshold: u16,
|
raw_threshold: u16,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_output_channel(raw_channel)?;
|
validate_raw_output_channel(raw_channel)?;
|
||||||
validate_raw_compressor_ratio(raw_ratio)?;
|
validate_raw_compressor_ratio(raw_ratio)?;
|
||||||
validate_raw_attack(raw_attack)?;
|
validate_raw_attack(raw_attack)?;
|
||||||
@@ -216,7 +155,6 @@ pub fn build_low_pass(
|
|||||||
raw_freq: u16,
|
raw_freq: u16,
|
||||||
raw_filter_type: u8,
|
raw_filter_type: u8,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_channel(raw_channel)?;
|
validate_raw_channel(raw_channel)?;
|
||||||
validate_raw_frequency(raw_freq)?;
|
validate_raw_frequency(raw_freq)?;
|
||||||
validate_raw_crossover_filter(raw_filter_type)?;
|
validate_raw_crossover_filter(raw_filter_type)?;
|
||||||
@@ -225,12 +163,7 @@ pub fn build_low_pass(
|
|||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(
|
||||||
0x31,
|
0x31,
|
||||||
&[
|
&[raw_channel, freq[0], freq[1], raw_filter_type],
|
||||||
raw_channel,
|
|
||||||
freq[0],
|
|
||||||
freq[1],
|
|
||||||
raw_filter_type,
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,7 +173,6 @@ pub fn build_high_pass(
|
|||||||
raw_freq: u16,
|
raw_freq: u16,
|
||||||
raw_filter_type: u8,
|
raw_filter_type: u8,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_channel(raw_channel)?;
|
validate_raw_channel(raw_channel)?;
|
||||||
validate_raw_frequency(raw_freq)?;
|
validate_raw_frequency(raw_freq)?;
|
||||||
validate_raw_crossover_filter(raw_filter_type)?;
|
validate_raw_crossover_filter(raw_filter_type)?;
|
||||||
@@ -249,12 +181,7 @@ pub fn build_high_pass(
|
|||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(
|
||||||
0x32,
|
0x32,
|
||||||
&[
|
&[raw_channel, freq[0], freq[1], raw_filter_type],
|
||||||
raw_channel,
|
|
||||||
freq[0],
|
|
||||||
freq[1],
|
|
||||||
raw_filter_type,
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +195,6 @@ pub fn build_peq_band(
|
|||||||
raw_filter_type: u8,
|
raw_filter_type: u8,
|
||||||
bypass: u8,
|
bypass: u8,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_channel(raw_channel)?;
|
validate_raw_channel(raw_channel)?;
|
||||||
validate_raw_peq_band(raw_channel, raw_band)?;
|
validate_raw_peq_band(raw_channel, raw_band)?;
|
||||||
validate_raw_eq_gain(raw_gain)?;
|
validate_raw_eq_gain(raw_gain)?;
|
||||||
@@ -297,115 +223,53 @@ pub fn build_peq_band(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x34 (Set Gain)
|
// Command 0x34 (Set Gain)
|
||||||
pub fn build_gain(
|
pub fn build_gain(channel: u8, raw_gain: u16) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel: u8,
|
|
||||||
raw_gain: u16,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_gain(raw_gain)?;
|
validate_raw_gain(raw_gain)?;
|
||||||
|
|
||||||
let gain = split_u16(raw_gain);
|
let gain = split_u16(raw_gain);
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x34, &[channel, gain[0], gain[1]]))
|
||||||
0x34,
|
|
||||||
&[
|
|
||||||
channel,
|
|
||||||
gain[0],
|
|
||||||
gain[1],
|
|
||||||
],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x35 (Set Mute)
|
// Command 0x35 (Set Mute)
|
||||||
pub fn build_mute(
|
pub fn build_mute(channel: u8, muted: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel:u8,
|
|
||||||
muted:u8,
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_bool(muted)?;
|
validate_raw_bool(muted)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x35, &[channel, muted]))
|
||||||
0x35,
|
|
||||||
&[
|
|
||||||
channel,
|
|
||||||
muted,
|
|
||||||
]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x36 (Invert Gain)
|
// Command 0x36 (Invert Gain)
|
||||||
pub fn build_inverted_gain(
|
pub fn build_inverted_gain(channel: u8, inverted: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel:u8,
|
|
||||||
inverted:u8,
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_bool(inverted)?;
|
validate_raw_bool(inverted)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x36, &[channel, inverted]))
|
||||||
0x36,
|
|
||||||
&[
|
|
||||||
channel,
|
|
||||||
inverted,
|
|
||||||
]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x38 (Set Delay)
|
// Command 0x38 (Set Delay)
|
||||||
pub fn build_set_delay(
|
pub fn build_set_delay(channel: u8, delay: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel:u8,
|
|
||||||
delay:u8,
|
|
||||||
)->Result<Vec<u8>,ValidationError>{
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_channel_delay(delay)?;
|
validate_raw_channel_delay(delay)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x38, &[channel, 0x00, delay]))
|
||||||
0x38,
|
|
||||||
&[
|
|
||||||
channel,
|
|
||||||
0x00,
|
|
||||||
delay
|
|
||||||
]
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x39 (Set Input Source)
|
// Command 0x39 (Set Input Source)
|
||||||
pub fn build_set_input_source(
|
pub fn build_set_input_source(input_type: u8, frequency: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
input_type: u8,
|
|
||||||
frequency: u8,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_input_source(input_type)?;
|
validate_raw_input_source(input_type)?;
|
||||||
validate_raw_discrete_frequency(frequency)?;
|
validate_raw_discrete_frequency(frequency)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x39, &[input_type, frequency]))
|
||||||
0x39,
|
|
||||||
&[
|
|
||||||
input_type,
|
|
||||||
frequency,
|
|
||||||
],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x3A (Matrix Routing)
|
// Command 0x3A (Matrix Routing)
|
||||||
pub fn build_matrix_routing(
|
pub fn build_matrix_routing(output: u8, input_bitmask: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
output: u8,
|
|
||||||
input_bitmask: u8,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_output_channel(output)?;
|
validate_raw_output_channel(output)?;
|
||||||
validate_raw_matrix_bitmask(input_bitmask)?;
|
validate_raw_matrix_bitmask(input_bitmask)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x3A, &[output, input_bitmask]))
|
||||||
0x3A,
|
|
||||||
&[
|
|
||||||
output,
|
|
||||||
input_bitmask,
|
|
||||||
],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x3B (Link Channel)
|
// Command 0x3B (Link Channel)
|
||||||
@@ -413,52 +277,31 @@ pub fn build_link_channel(
|
|||||||
source_channel: u8,
|
source_channel: u8,
|
||||||
destination_channel_bitmask: u8,
|
destination_channel_bitmask: u8,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_link_channel(source_channel, destination_channel_bitmask)?;
|
validate_raw_link_channel(source_channel, destination_channel_bitmask)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(
|
||||||
0x3B,
|
0x3B,
|
||||||
&[
|
&[source_channel, destination_channel_bitmask],
|
||||||
source_channel,
|
|
||||||
destination_channel_bitmask,
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x3C (Bypass EQ)
|
// Command 0x3C (Bypass EQ)
|
||||||
pub fn build_bypass_peq(
|
pub fn build_bypass_peq(channel: u8, bypass: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel: u8,
|
|
||||||
bypass: u8,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_bool(bypass)?;
|
validate_raw_bool(bypass)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x3C, &[channel, bypass]))
|
||||||
0x3C,
|
|
||||||
&[
|
|
||||||
channel,
|
|
||||||
bypass,
|
|
||||||
],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x3D (Store Channel Name)
|
// Command 0x3D (Store Channel Name)
|
||||||
pub fn build_store_channel_name(
|
pub fn build_store_channel_name(channel: u8, raw_name: &[u8]) -> Result<Vec<u8>, ValidationError> {
|
||||||
channel: u8,
|
|
||||||
raw_name: &[u8],
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_channel(channel)?;
|
validate_raw_channel(channel)?;
|
||||||
validate_raw_channel_name(raw_name)?;
|
validate_raw_channel_name(raw_name)?;
|
||||||
|
|
||||||
let mut payload = vec![channel];
|
let mut payload = vec![channel];
|
||||||
payload.extend_from_slice(raw_name);
|
payload.extend_from_slice(raw_name);
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x3D, &payload))
|
||||||
0x3D,
|
|
||||||
&payload,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x3E (Set Gate Parameters)
|
// Command 0x3E (Set Gate Parameters)
|
||||||
@@ -469,7 +312,6 @@ pub fn build_set_gate(
|
|||||||
raw_hold: u16,
|
raw_hold: u16,
|
||||||
raw_release: u16,
|
raw_release: u16,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_input_channel(raw_channel)?;
|
validate_raw_input_channel(raw_channel)?;
|
||||||
validate_raw_gate_threshold(raw_threshold)?;
|
validate_raw_gate_threshold(raw_threshold)?;
|
||||||
validate_raw_attack(raw_attack)?;
|
validate_raw_attack(raw_attack)?;
|
||||||
@@ -504,7 +346,6 @@ pub fn build_set_limiter(
|
|||||||
raw_attack: u16,
|
raw_attack: u16,
|
||||||
raw_release: u16,
|
raw_release: u16,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_output_channel(raw_channel)?;
|
validate_raw_output_channel(raw_channel)?;
|
||||||
validate_raw_threshold(raw_threshold)?;
|
validate_raw_threshold(raw_threshold)?;
|
||||||
validate_raw_attack(raw_attack)?;
|
validate_raw_attack(raw_attack)?;
|
||||||
@@ -532,10 +373,7 @@ pub fn build_set_limiter(
|
|||||||
|
|
||||||
// Command 0x40 (Get Level Meters)
|
// Command 0x40 (Get Level Meters)
|
||||||
pub fn build_get_meter_levels() -> Vec<u8> {
|
pub fn build_get_meter_levels() -> Vec<u8> {
|
||||||
build_frame(
|
build_frame(0x40, &[])
|
||||||
0x40,
|
|
||||||
&[],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x41 (Set Matrix Gain)
|
// Command 0x41 (Set Matrix Gain)
|
||||||
@@ -544,7 +382,6 @@ pub fn build_matrix_gain(
|
|||||||
raw_output_channel: u8,
|
raw_output_channel: u8,
|
||||||
raw_gain: u16,
|
raw_gain: u16,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_input_channel(raw_input_channel)?;
|
validate_raw_input_channel(raw_input_channel)?;
|
||||||
validate_raw_output_channel(raw_output_channel)?;
|
validate_raw_output_channel(raw_output_channel)?;
|
||||||
validate_raw_matrix_gain(raw_gain)?;
|
validate_raw_matrix_gain(raw_gain)?;
|
||||||
@@ -553,12 +390,7 @@ pub fn build_matrix_gain(
|
|||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(
|
||||||
0x41,
|
0x41,
|
||||||
&[
|
&[raw_output_channel, raw_input_channel, gain[0], gain[1]],
|
||||||
raw_output_channel,
|
|
||||||
raw_input_channel,
|
|
||||||
gain[0],
|
|
||||||
gain[1],
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -568,7 +400,6 @@ pub fn build_set_geq_band(
|
|||||||
raw_frequency_band: u8,
|
raw_frequency_band: u8,
|
||||||
raw_gain: u16,
|
raw_gain: u16,
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
) -> Result<Vec<u8>, ValidationError> {
|
||||||
|
|
||||||
validate_raw_input_channel(raw_channel)?;
|
validate_raw_input_channel(raw_channel)?;
|
||||||
validate_raw_discrete_frequency(raw_frequency_band)?;
|
validate_raw_discrete_frequency(raw_frequency_band)?;
|
||||||
validate_raw_eq_gain(raw_gain)?;
|
validate_raw_eq_gain(raw_gain)?;
|
||||||
@@ -577,43 +408,24 @@ pub fn build_set_geq_band(
|
|||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(
|
||||||
0x48,
|
0x48,
|
||||||
&[
|
&[raw_channel, raw_frequency_band, gain[0], gain[1]],
|
||||||
raw_channel,
|
|
||||||
raw_frequency_band,
|
|
||||||
gain[0],
|
|
||||||
gain[1],
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command 0x49 (Set GEQ Bypass)
|
// Command 0x49 (Set GEQ Bypass)
|
||||||
pub fn build_set_geq_bypass(
|
pub fn build_set_geq_bypass(raw_channel: u8, bypass: u8) -> Result<Vec<u8>, ValidationError> {
|
||||||
raw_channel: u8,
|
|
||||||
bypass: u8,
|
|
||||||
) -> Result<Vec<u8>, ValidationError> {
|
|
||||||
|
|
||||||
validate_raw_input_channel(raw_channel)?;
|
validate_raw_input_channel(raw_channel)?;
|
||||||
validate_raw_bool(bypass)?;
|
validate_raw_bool(bypass)?;
|
||||||
|
|
||||||
Ok(build_frame(
|
Ok(build_frame(0x49, &[raw_channel, bypass]))
|
||||||
0x49,
|
|
||||||
&[
|
|
||||||
raw_channel,
|
|
||||||
bypass,
|
|
||||||
],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
use super::super::constants::*;
|
use super::super::constants::*;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
fn assert_frame(
|
fn assert_frame(frame: Vec<u8>, expected_command: u8, expected_payload: &[u8]) {
|
||||||
frame: Vec<u8>,
|
|
||||||
expected_command: u8,
|
|
||||||
expected_payload: &[u8],
|
|
||||||
) {
|
|
||||||
assert_eq!(frame[5], expected_command);
|
assert_eq!(frame[5], expected_command);
|
||||||
assert_eq!(&frame[6..frame.len() - 3], expected_payload);
|
assert_eq!(&frame[6..frame.len() - 3], expected_payload);
|
||||||
}
|
}
|
||||||
@@ -627,86 +439,34 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple_commands() {
|
fn test_simple_commands() {
|
||||||
assert_frame(
|
assert_frame(build_handshake(), 0x10, &[]);
|
||||||
build_handshake(),
|
|
||||||
0x10,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_disconnect(), 0x11, &[]);
|
||||||
build_disconnect(),
|
|
||||||
0x11,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_acknowledgement(), 0x12, &[]);
|
||||||
build_acknowledgement(),
|
|
||||||
0x12,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_request_device_name(), 0x13, &[]);
|
||||||
build_request_device_name(),
|
|
||||||
0x13,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_get_current_preset(), 0x14, &[]);
|
||||||
build_get_current_preset(),
|
|
||||||
0x14,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_preset_modification_status(), 0x22, &[]);
|
||||||
build_preset_modification_status(),
|
|
||||||
0x22,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_factory_reset(), 0x23, &[]);
|
||||||
build_factory_reset(),
|
|
||||||
0x23,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_get_device_flags(), 0x2c, &[]);
|
||||||
build_get_device_flags(),
|
|
||||||
0x2c,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_get_meter_levels(), 0x40, &[]);
|
||||||
build_get_meter_levels(),
|
|
||||||
0x40,
|
|
||||||
&[],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_preset_commands() {
|
fn test_preset_commands() {
|
||||||
assert_frame(
|
assert_frame(build_recall(5).unwrap(), 0x20, &[5]);
|
||||||
build_recall(5).unwrap(),
|
|
||||||
0x20,
|
|
||||||
&[5],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_store_current_configuration(3).unwrap(), 0x21, &[3]);
|
||||||
build_store_current_configuration(3).unwrap(),
|
|
||||||
0x21,
|
|
||||||
&[3],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_get_preset_name(2).unwrap(), 0x29, &[2]);
|
||||||
build_get_preset_name(2).unwrap(),
|
|
||||||
0x29,
|
|
||||||
&[2],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_config_chunk(1).unwrap(), 0x27, &[1]);
|
||||||
build_config_chunk(1).unwrap(),
|
|
||||||
0x27,
|
|
||||||
&[1],
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_store_current_preset_name(b"TEST ").unwrap(),
|
build_store_current_preset_name(b"TEST ").unwrap(),
|
||||||
@@ -717,57 +477,27 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_device_lock_commands() {
|
fn test_device_lock_commands() {
|
||||||
assert_frame(
|
assert_frame(build_unlock_device(b"1234").unwrap(), 0x2d, b"1234");
|
||||||
build_unlock_device(b"1234").unwrap(),
|
|
||||||
0x2d,
|
|
||||||
b"1234",
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(build_lock_device(b"1234").unwrap(), 0x2f, b"1234");
|
||||||
build_lock_device(b"1234").unwrap(),
|
|
||||||
0x2f,
|
|
||||||
b"1234",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_delay_unit() {
|
fn test_set_delay_unit() {
|
||||||
assert_frame(
|
assert_frame(build_set_delay_unit(1).unwrap(), 0x15, &[1]);
|
||||||
build_set_delay_unit(1).unwrap(),
|
|
||||||
0x15,
|
|
||||||
&[1],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_copy_channel() {
|
fn test_copy_channel() {
|
||||||
assert_frame(
|
assert_frame(build_copy_channel(1, 3).unwrap(), 0x2a, &[1, 3]);
|
||||||
build_copy_channel(1, 3).unwrap(),
|
|
||||||
0x2a,
|
|
||||||
&[1, 3],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_compressor_frame() {
|
fn test_compressor_frame() {
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_set_compressor(
|
build_set_compressor(5, 1, 300, 400, 2, 11).unwrap(),
|
||||||
5,
|
|
||||||
1,
|
|
||||||
300,
|
|
||||||
400,
|
|
||||||
2,
|
|
||||||
11,
|
|
||||||
).unwrap(),
|
|
||||||
0x30,
|
0x30,
|
||||||
&[
|
&[5, 1, 0, 44, 1, 144, 1, 2, 0, 11, 0],
|
||||||
5,
|
|
||||||
1, 0,
|
|
||||||
44, 1,
|
|
||||||
144, 1,
|
|
||||||
2, 0,
|
|
||||||
11, 0,
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -869,17 +599,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_delay_and_input() {
|
fn test_delay_and_input() {
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_set_delay(
|
build_set_delay(*CHANNELS_RANGE.end(), *DELAY_RAW_RANGE.end()).unwrap(),
|
||||||
*CHANNELS_RANGE.end(),
|
|
||||||
*DELAY_RAW_RANGE.end(),
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x38,
|
0x38,
|
||||||
&[
|
&[*CHANNELS_RANGE.end(), 0x00, *DELAY_RAW_RANGE.end()],
|
||||||
*CHANNELS_RANGE.end(),
|
|
||||||
0x00,
|
|
||||||
*DELAY_RAW_RANGE.end(),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
@@ -935,11 +657,7 @@ mod tests {
|
|||||||
expected.extend_from_slice(&name);
|
expected.extend_from_slice(&name);
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_store_channel_name(
|
build_store_channel_name(*CHANNELS_RANGE.start(), &name).unwrap(),
|
||||||
*CHANNELS_RANGE.start(),
|
|
||||||
&name,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x3D,
|
0x3D,
|
||||||
&expected,
|
&expected,
|
||||||
);
|
);
|
||||||
@@ -953,14 +671,7 @@ mod tests {
|
|||||||
let release = *RELEASE_RAW_RANGE.end();
|
let release = *RELEASE_RAW_RANGE.end();
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_set_gate(
|
build_set_gate(*INPUT_CHANNELS.end(), threshold, attack, hold, release).unwrap(),
|
||||||
*INPUT_CHANNELS.end(),
|
|
||||||
threshold,
|
|
||||||
attack,
|
|
||||||
hold,
|
|
||||||
release,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x3E,
|
0x3E,
|
||||||
&[
|
&[
|
||||||
*INPUT_CHANNELS.end(),
|
*INPUT_CHANNELS.end(),
|
||||||
@@ -978,13 +689,7 @@ mod tests {
|
|||||||
let threshold = *THRESHOLD_RAW_RANGE.end();
|
let threshold = *THRESHOLD_RAW_RANGE.end();
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_set_limiter(
|
build_set_limiter(*OUTPUT_CHANNELS.end(), threshold, attack, release).unwrap(),
|
||||||
*OUTPUT_CHANNELS.end(),
|
|
||||||
threshold,
|
|
||||||
attack,
|
|
||||||
release,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x3F,
|
0x3F,
|
||||||
&[
|
&[
|
||||||
*OUTPUT_CHANNELS.end(),
|
*OUTPUT_CHANNELS.end(),
|
||||||
@@ -1005,12 +710,7 @@ mod tests {
|
|||||||
let gain = *MATRIX_GAIN_RAW_RANGE.end();
|
let gain = *MATRIX_GAIN_RAW_RANGE.end();
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_matrix_gain(
|
build_matrix_gain(*INPUT_CHANNELS.end(), *OUTPUT_CHANNELS.end(), gain).unwrap(),
|
||||||
*INPUT_CHANNELS.end(),
|
|
||||||
*OUTPUT_CHANNELS.end(),
|
|
||||||
gain,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x41,
|
0x41,
|
||||||
&[
|
&[
|
||||||
*OUTPUT_CHANNELS.end(),
|
*OUTPUT_CHANNELS.end(),
|
||||||
@@ -1042,16 +742,9 @@ mod tests {
|
|||||||
);
|
);
|
||||||
|
|
||||||
assert_frame(
|
assert_frame(
|
||||||
build_set_geq_bypass(
|
build_set_geq_bypass(*INPUT_CHANNELS.end(), 1).unwrap(),
|
||||||
*INPUT_CHANNELS.end(),
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
0x49,
|
0x49,
|
||||||
&[
|
&[*INPUT_CHANNELS.end(), 1],
|
||||||
*INPUT_CHANNELS.end(),
|
|
||||||
1,
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,8 @@ use crate::{
|
|||||||
common::errors::DSPError,
|
common::errors::DSPError,
|
||||||
dsp408::{
|
dsp408::{
|
||||||
mapper,
|
mapper,
|
||||||
|
protocol::{constants, reader::BinaryReader},
|
||||||
types,
|
types,
|
||||||
protocol::{
|
|
||||||
constants,
|
|
||||||
reader::BinaryReader,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,9 +34,7 @@ fn read_gate(r: &mut BinaryReader) -> Result<types::Gate, DSPError> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_geq(
|
fn read_geq(r: &mut BinaryReader) -> Result<types::GraphicEQ, DSPError> {
|
||||||
r: &mut BinaryReader,
|
|
||||||
) -> Result<types::GraphicEQ, DSPError> {
|
|
||||||
let gains = types::DiscreteFrequency::iter()
|
let gains = types::DiscreteFrequency::iter()
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
let raw = r.read_u16()?;
|
let raw = r.read_u16()?;
|
||||||
@@ -53,10 +48,7 @@ fn read_geq(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_peq_chain(
|
fn read_peq_chain(r: &mut BinaryReader, band_count: usize) -> Result<types::PEQChain, DSPError> {
|
||||||
r: &mut BinaryReader,
|
|
||||||
band_count: usize,
|
|
||||||
) -> Result<types::PEQChain, DSPError> {
|
|
||||||
let bands = (0..band_count)
|
let bands = (0..band_count)
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
Ok(types::PEQ {
|
Ok(types::PEQ {
|
||||||
@@ -75,9 +67,7 @@ fn read_peq_chain(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_crossover(
|
fn read_crossover(r: &mut BinaryReader) -> Result<types::CrossoverFilters, DSPError> {
|
||||||
r: &mut BinaryReader,
|
|
||||||
) -> Result<types::CrossoverFilters, DSPError> {
|
|
||||||
let hp_freq = r.read_u16()?;
|
let hp_freq = r.read_u16()?;
|
||||||
let lp_freq = r.read_u16()?;
|
let lp_freq = r.read_u16()?;
|
||||||
let hp_filter = r.read_u8()?;
|
let hp_filter = r.read_u8()?;
|
||||||
@@ -94,9 +84,7 @@ fn read_crossover(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_compressor(
|
fn read_compressor(r: &mut BinaryReader) -> Result<types::Compressor, DSPError> {
|
||||||
r: &mut BinaryReader,
|
|
||||||
) -> Result<types::Compressor, DSPError> {
|
|
||||||
Ok(types::Compressor {
|
Ok(types::Compressor {
|
||||||
ratio: mapper::raw_to_ratio(r.read_u16()?)?,
|
ratio: mapper::raw_to_ratio(r.read_u16()?)?,
|
||||||
attack: mapper::raw_to_attack_ms(r.read_u16()?)?,
|
attack: mapper::raw_to_attack_ms(r.read_u16()?)?,
|
||||||
@@ -106,9 +94,7 @@ fn read_compressor(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_limiter(
|
fn read_limiter(r: &mut BinaryReader) -> Result<types::Limiter, DSPError> {
|
||||||
r: &mut BinaryReader,
|
|
||||||
) -> Result<types::Limiter, DSPError> {
|
|
||||||
let attack = mapper::raw_to_attack_ms(r.read_u16()?)?;
|
let attack = mapper::raw_to_attack_ms(r.read_u16()?)?;
|
||||||
let release = mapper::raw_to_release_ms(r.read_u16()?)?;
|
let release = mapper::raw_to_release_ms(r.read_u16()?)?;
|
||||||
|
|
||||||
@@ -132,7 +118,8 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
|
|
||||||
let mut input_channels = vec![types::InputChannelState::default(); types::InputChannel::COUNT];
|
let mut input_channels = vec![types::InputChannelState::default(); types::InputChannel::COUNT];
|
||||||
|
|
||||||
let mut output_channels = vec![types::OutputChannelState::default(); types::OutputChannel::COUNT];
|
let mut output_channels =
|
||||||
|
vec![types::OutputChannelState::default(); types::OutputChannel::COUNT];
|
||||||
|
|
||||||
let mut input_delays_raw: Vec<u8> = Vec::new();
|
let mut input_delays_raw: Vec<u8> = Vec::new();
|
||||||
let mut output_delays_raw: Vec<u8> = Vec::new();
|
let mut output_delays_raw: Vec<u8> = Vec::new();
|
||||||
@@ -162,7 +149,10 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
|
|
||||||
r.skip(1)?;
|
r.skip(1)?;
|
||||||
|
|
||||||
inp.linked_channels = mapper::raw_to_link_channels(mapper::channel_to_raw(types::Channel::Input(channel))?, r.read_u8()?)?;
|
inp.linked_channels = mapper::raw_to_link_channels(
|
||||||
|
mapper::channel_to_raw(types::Channel::Input(channel))?,
|
||||||
|
r.read_u8()?,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
@@ -175,10 +165,7 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
r.skip(1)?;
|
r.skip(1)?;
|
||||||
|
|
||||||
out.matrix_routes = types::MatrixRoutes {
|
out.matrix_routes = types::MatrixRoutes {
|
||||||
connected:
|
connected: mapper::raw_to_matrix(r.read_u8()?)?,
|
||||||
mapper::raw_to_matrix(
|
|
||||||
r.read_u8()?
|
|
||||||
)?,
|
|
||||||
|
|
||||||
gains: (0..4)
|
gains: (0..4)
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
@@ -206,43 +193,34 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
|
|
||||||
r.skip(1)?;
|
r.skip(1)?;
|
||||||
|
|
||||||
out.linked_channels = mapper::raw_to_link_channels(mapper::channel_to_raw(types::Channel::Output(channel))?, r.read_u8()?)?;
|
out.linked_channels = mapper::raw_to_link_channels(
|
||||||
|
mapper::channel_to_raw(types::Channel::Output(channel))?,
|
||||||
|
r.read_u8()?,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
// MUTE FLAGS
|
// MUTE FLAGS
|
||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
apply_flag(
|
apply_flag(&mut r, &mut input_channels, |ch, value| ch.mute = value)?;
|
||||||
&mut r,
|
|
||||||
&mut input_channels,
|
|
||||||
|ch, value| ch.mute = value,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
apply_flag(
|
apply_flag(&mut r, &mut output_channels, |ch, value| ch.mute = value)?;
|
||||||
&mut r,
|
|
||||||
&mut output_channels,
|
|
||||||
|ch, value| ch.mute = value,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
// PEQ BYPASS FLAGS
|
// PEQ BYPASS FLAGS
|
||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
for inp in &mut input_channels {
|
for inp in &mut input_channels {
|
||||||
apply_flag(
|
apply_flag(&mut r, inp.peq_chain.bands.as_mut_slice(), |band, value| {
|
||||||
&mut r,
|
band.bypass = value
|
||||||
inp.peq_chain.bands.as_mut_slice(),
|
})?;
|
||||||
|band, value| band.bypass = value,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for out in &mut output_channels {
|
for out in &mut output_channels {
|
||||||
apply_flag(
|
apply_flag(&mut r, out.peq_chain.bands.as_mut_slice(), |band, value| {
|
||||||
&mut r,
|
band.bypass = value
|
||||||
out.peq_chain.bands.as_mut_slice(),
|
})?;
|
||||||
|band, value| band.bypass = value,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
@@ -250,41 +228,27 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
let input_source = types::InputSource {
|
let input_source = types::InputSource {
|
||||||
r#type: mapper::raw_to_input_source(
|
r#type: mapper::raw_to_input_source(r.read_u16()? as u8)?,
|
||||||
r.read_u16()? as u8,
|
|
||||||
)?,
|
|
||||||
|
|
||||||
frequency: mapper::raw_to_frequency_value(
|
frequency: mapper::raw_to_frequency_value(r.read_u16()? as u8)?,
|
||||||
r.read_u16()? as u8,
|
|
||||||
)?,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
// DELAY UNIT
|
// DELAY UNIT
|
||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
let delay_unit = mapper::raw_to_delay_unit(
|
let delay_unit = mapper::raw_to_delay_unit(r.read_u16()? as u8)?;
|
||||||
r.read_u16()? as u8,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
// APPLY DELAYS
|
// APPLY DELAYS
|
||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
for (inp, raw_delay) in input_channels.iter_mut().zip(input_delays_raw)
|
for (inp, raw_delay) in input_channels.iter_mut().zip(input_delays_raw) {
|
||||||
{
|
inp.delay = mapper::raw_to_delay(raw_delay, delay_unit)?;
|
||||||
inp.delay = mapper::raw_to_delay(
|
|
||||||
raw_delay,
|
|
||||||
delay_unit,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (out, raw_delay) in output_channels.iter_mut().zip(output_delays_raw)
|
for (out, raw_delay) in output_channels.iter_mut().zip(output_delays_raw) {
|
||||||
{
|
out.delay = mapper::raw_to_delay(raw_delay, delay_unit)?;
|
||||||
out.delay = mapper::raw_to_delay(
|
|
||||||
raw_delay,
|
|
||||||
delay_unit,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
@@ -301,7 +265,6 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let output_peq_bitmap = r.read_u16()?;
|
let output_peq_bitmap = r.read_u16()?;
|
||||||
|
|
||||||
for (index, out) in output_channels.iter_mut().enumerate() {
|
for (index, out) in output_channels.iter_mut().enumerate() {
|
||||||
@@ -325,19 +288,13 @@ pub fn decode_config(payload: &[u8]) -> Result<types::DSPConfigState, DSPError>
|
|||||||
let mut input_states = HashMap::new();
|
let mut input_states = HashMap::new();
|
||||||
|
|
||||||
for (channel, state) in types::InputChannel::iter().zip(input_channels) {
|
for (channel, state) in types::InputChannel::iter().zip(input_channels) {
|
||||||
input_states.insert(
|
input_states.insert(channel, state);
|
||||||
channel,
|
|
||||||
state,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output_states = HashMap::new();
|
let mut output_states = HashMap::new();
|
||||||
|
|
||||||
for (channel, state) in types::OutputChannel::iter().zip(output_channels) {
|
for (channel, state) in types::OutputChannel::iter().zip(output_channels) {
|
||||||
output_states.insert(
|
output_states.insert(channel, state);
|
||||||
channel,
|
|
||||||
state,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(types::DSPConfigState {
|
Ok(types::DSPConfigState {
|
||||||
|
|||||||
@@ -65,8 +65,7 @@ pub fn extract_frame(data: &[u8]) -> Result<Frame, FrameError> {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let footer = find_footer(data)
|
let footer = find_footer(data).ok_or_else(|| FrameError::new("Missing frame footer"))?;
|
||||||
.ok_or_else(|| FrameError::new("Missing frame footer"))?;
|
|
||||||
|
|
||||||
let expected = checksum(&data[2..footer]);
|
let expected = checksum(&data[2..footer]);
|
||||||
let received = data[data.len() - 1];
|
let received = data[data.len() - 1];
|
||||||
@@ -89,8 +88,7 @@ fn find_footer(data: &[u8]) -> Option<usize> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
(2..data.len() - 1)
|
(2..data.len() - 1).find(|&i| data[i] == 0x10 && data[i + 1] == 0x03)
|
||||||
.find(|&i| data[i] == 0x10 && data[i + 1] == 0x03)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
mod reader;
|
mod reader;
|
||||||
|
|
||||||
pub(crate) mod frame;
|
|
||||||
pub(crate) mod commands;
|
pub(crate) mod commands;
|
||||||
pub(crate) mod validators;
|
|
||||||
pub(crate) mod config;
|
pub(crate) mod config;
|
||||||
|
pub(crate) mod frame;
|
||||||
|
pub(crate) mod validators;
|
||||||
|
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
@@ -11,9 +11,7 @@ impl<'a> BinaryReader<'a> {
|
|||||||
|
|
||||||
pub fn pop(&mut self, n: usize) -> Result<&'a [u8], DSPError> {
|
pub fn pop(&mut self, n: usize) -> Result<&'a [u8], DSPError> {
|
||||||
if self.data.len() < n {
|
if self.data.len() < n {
|
||||||
return Err(DSPError::InvalidRequest(
|
return Err(DSPError::InvalidRequest("buffer underflow".into()));
|
||||||
"buffer underflow".into()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (value, rest) = self.data.split_at(n);
|
let (value, rest) = self.data.split_at(n);
|
||||||
@@ -27,9 +25,7 @@ impl<'a> BinaryReader<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_u16(&mut self) -> Result<u16, DSPError> {
|
pub fn read_u16(&mut self) -> Result<u16, DSPError> {
|
||||||
Ok(u16::from_le_bytes(
|
Ok(u16::from_le_bytes(self.pop(2)?.try_into().unwrap()))
|
||||||
self.pop(2)?.try_into().unwrap()
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip(&mut self, n: usize) -> Result<(), DSPError> {
|
pub fn skip(&mut self, n: usize) -> Result<(), DSPError> {
|
||||||
|
|||||||
+106
-179
@@ -1,45 +1,33 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{
|
||||||
errors::ValidationError,
|
errors::ValidationError,
|
||||||
validator::{
|
validator::{validate_ascii_padded, validate_bitmask, validate_range},
|
||||||
validate_ascii_padded,
|
|
||||||
validate_bitmask,
|
|
||||||
validate_range,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
dsp408::protocol::constants::*,
|
dsp408::protocol::constants::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn validate_raw_delay_unit(raw_unit: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_delay_unit(raw_unit: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_unit, DELAY_UNIT_RAW_RANGE, || {
|
||||||
raw_unit,
|
format!("Invalid delay unit: {:#04x}", raw_unit)
|
||||||
DELAY_UNIT_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid delay unit: {:#04x}", raw_unit)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_index, 0..=PRESET_INDEX_MAX, || {
|
||||||
raw_index,
|
format!("Invalid preset index {}", raw_index)
|
||||||
0..=PRESET_INDEX_MAX,
|
})
|
||||||
|| format!("Invalid preset index {}", raw_index)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_store_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_store_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_index, 1..=PRESET_INDEX_MAX, || {
|
||||||
raw_index,
|
format!("Invalid preset index {}", raw_index)
|
||||||
1..=PRESET_INDEX_MAX,
|
})
|
||||||
|| format!("Invalid preset index {}", raw_index)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_get_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_get_preset_index(raw_index: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_index, 0..PRESET_INDEX_MAX, || {
|
||||||
raw_index,
|
format!("Invalid preset index {}", raw_index)
|
||||||
0..PRESET_INDEX_MAX,
|
})
|
||||||
|| format!("Invalid preset index {}", raw_index)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_preset_name(raw_name: &[u8]) -> Result<(), ValidationError> {
|
pub fn validate_raw_preset_name(raw_name: &[u8]) -> Result<(), ValidationError> {
|
||||||
@@ -47,58 +35,46 @@ pub fn validate_raw_preset_name(raw_name: &[u8]) -> Result<(), ValidationError>
|
|||||||
raw_name,
|
raw_name,
|
||||||
PRESET_NAME_LENGTH,
|
PRESET_NAME_LENGTH,
|
||||||
PRESET_NAME_PADDING,
|
PRESET_NAME_PADDING,
|
||||||
"Preset name"
|
"Preset name",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_config_chunk_index(raw_index: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_config_chunk_index(raw_index: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_index, 0..=CONFIG_CHUNK_INDEX_MAX, || {
|
||||||
raw_index,
|
format!("Invalid config index: {:#04x}", raw_index)
|
||||||
0..=CONFIG_CHUNK_INDEX_MAX,
|
})
|
||||||
|| format!("Invalid config index: {:#04x}", raw_index)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_channel(channel: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_channel(channel: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(channel, CHANNELS_RANGE, || {
|
||||||
channel,
|
format!("Invalid Channel: {:#04x}", channel)
|
||||||
CHANNELS_RANGE,
|
})
|
||||||
|| format!("Invalid Channel: {:#04x}", channel),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_input_channel(channel: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_input_channel(channel: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(channel, INPUT_CHANNELS, || {
|
||||||
channel,
|
format!("Invalid Channel: {:#04x}", channel)
|
||||||
INPUT_CHANNELS,
|
})
|
||||||
|| format!("Invalid Channel: {:#04x}", channel),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_output_channel(channel: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_output_channel(channel: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(channel, OUTPUT_CHANNELS, || {
|
||||||
channel,
|
format!("Invalid Channel: {:#04x}", channel)
|
||||||
OUTPUT_CHANNELS,
|
})
|
||||||
|| format!("Invalid Channel: {:#04x}", channel),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_copy_channels(source: u8, destination: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_copy_channels(source: u8, destination: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(source, CHANNELS_RANGE, || {
|
||||||
source,
|
format!("Invalid source channel: {:#04x}", source)
|
||||||
CHANNELS_RANGE,
|
})?;
|
||||||
|| format!("Invalid source channel: {:#04x}", source),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
validate_range(
|
validate_range(destination, CHANNELS_RANGE, || {
|
||||||
destination,
|
format!("Invalid destination channel: {:#04x}", destination)
|
||||||
CHANNELS_RANGE,
|
})?;
|
||||||
|| format!("Invalid destination channel: {:#04x}", destination),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if source == destination {
|
if source == destination {
|
||||||
return Err(ValidationError::value(
|
return Err(ValidationError::value(
|
||||||
"Source channel and destination channel can't be the same"
|
"Source channel and destination channel can't be the same",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +83,7 @@ pub fn validate_raw_copy_channels(source: u8, destination: u8) -> Result<(), Val
|
|||||||
|
|
||||||
if source_is_input != destination_is_input {
|
if source_is_input != destination_is_input {
|
||||||
return Err(ValidationError::value(
|
return Err(ValidationError::value(
|
||||||
"Channels need to be both inputs or outputs"
|
"Channels need to be both inputs or outputs",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,150 +91,111 @@ pub fn validate_raw_copy_channels(source: u8, destination: u8) -> Result<(), Val
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_password(password: &[u8]) -> Result<(), ValidationError> {
|
pub fn validate_raw_password(password: &[u8]) -> Result<(), ValidationError> {
|
||||||
validate_ascii_padded(
|
validate_ascii_padded(password, PASSWORD_LENGTH, PASSWORD_PADDING, "Password")
|
||||||
password,
|
|
||||||
PASSWORD_LENGTH,
|
|
||||||
PASSWORD_PADDING,
|
|
||||||
"Password"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_compressor_ratio(raw_ratio: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_compressor_ratio(raw_ratio: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_ratio, RATIO_RAW_RANGE, || {
|
||||||
raw_ratio,
|
format!("Invalid compressor ratio: {:#06x}", raw_ratio)
|
||||||
RATIO_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid compressor ratio: {:#06x}", raw_ratio),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_attack(raw_attack: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_attack(raw_attack: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_attack, ATTACK_RAW_RANGE, || {
|
||||||
raw_attack,
|
format!("Invalid attack: {:#06x}", raw_attack)
|
||||||
ATTACK_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid attack: {:#06x}", raw_attack),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_release(raw_release: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_release(raw_release: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_release, RELEASE_RAW_RANGE, || {
|
||||||
raw_release,
|
format!("Invalid release: {:#06x}", raw_release)
|
||||||
RELEASE_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid release: {:#06x}", raw_release),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_compressor_knee(raw_knee: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_compressor_knee(raw_knee: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_knee, KNEE_RAW_RANGE, || {
|
||||||
raw_knee,
|
format!("Invalid compressor knee: {:#06x}", raw_knee)
|
||||||
KNEE_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid compressor knee: {:#06x}", raw_knee),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_threshold(raw_threshold: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_threshold(raw_threshold: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_threshold, THRESHOLD_RAW_RANGE, || {
|
||||||
raw_threshold,
|
format!("Invalid compressor threshold: {:#06x}", raw_threshold)
|
||||||
THRESHOLD_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid compressor threshold: {:#06x}", raw_threshold),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_frequency(raw_freq: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_frequency(raw_freq: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_freq, FREQUENCY_RAW_RANGE, || {
|
||||||
raw_freq,
|
format!("Invalid frequency: {:#06x}", raw_freq)
|
||||||
FREQUENCY_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid frequency: {:#06x}", raw_freq),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_crossover_filter(raw_filter_type: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_crossover_filter(raw_filter_type: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_filter_type, CROSSOVER_FILTER_RAW_RANGE, || {
|
||||||
raw_filter_type,
|
format!("Invalid filter: {:#04x}", raw_filter_type)
|
||||||
CROSSOVER_FILTER_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid filter: {:#04x}", raw_filter_type),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_peq_band(raw_channel: u8, raw_band: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_peq_band(raw_channel: u8, raw_band: u8) -> Result<(), ValidationError> {
|
||||||
if INPUT_CHANNELS.contains(&raw_channel) {
|
if INPUT_CHANNELS.contains(&raw_channel) {
|
||||||
validate_range(
|
validate_range(raw_band, PEQ_INPUT_BAND_RANGE, || {
|
||||||
raw_band,
|
format!("Invalid input PEQ band: {:#04x}", raw_band)
|
||||||
PEQ_INPUT_BAND_RANGE,
|
})?;
|
||||||
|| format!("Invalid input PEQ band: {:#04x}", raw_band),
|
|
||||||
)?;
|
|
||||||
} else if OUTPUT_CHANNELS.contains(&raw_channel) {
|
} else if OUTPUT_CHANNELS.contains(&raw_channel) {
|
||||||
validate_range(
|
validate_range(raw_band, PEQ_OUTPUT_BAND_RANGE, || {
|
||||||
raw_band,
|
format!("Invalid output PEQ band: {:#04x}", raw_band)
|
||||||
PEQ_OUTPUT_BAND_RANGE,
|
})?;
|
||||||
|| format!("Invalid output PEQ band: {:#04x}", raw_band),
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_peq_q(raw_q: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_peq_q(raw_q: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_q, PEQ_Q_RAW_RANGE, || {
|
||||||
raw_q,
|
format!("Invalid PEQ Q: {:#04x}", raw_q)
|
||||||
PEQ_Q_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid PEQ Q: {:#04x}", raw_q),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_peq_filter(raw_filter_type: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_peq_filter(raw_filter_type: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_filter_type, PEQ_FILTER_RAW_RANGE, || {
|
||||||
raw_filter_type,
|
format!("Invalid PEQ filter: {:#04x}", raw_filter_type)
|
||||||
PEQ_FILTER_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid PEQ filter: {:#04x}", raw_filter_type),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_eq_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_eq_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_gain, EQ_GAIN_RAW_RANGE, || {
|
||||||
raw_gain,
|
format!("Invalid EQ gain: {:#06x}", raw_gain)
|
||||||
EQ_GAIN_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid EQ gain: {:#06x}", raw_gain),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_gain, CHANNEL_GAIN_RAW_RANGE, || {
|
||||||
raw_gain,
|
format!("Invalid channel gain: {:#06x}", raw_gain)
|
||||||
CHANNEL_GAIN_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid channel gain: {:#06x}", raw_gain),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_channel_delay(delay: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_channel_delay(delay: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(delay, DELAY_RAW_RANGE, || {
|
||||||
delay,
|
format!("Invalid channel delay: {:#04x}", delay)
|
||||||
DELAY_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid channel delay: {:#04x}", delay)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_input_source(input_type: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_input_source(input_type: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(input_type, INPUT_SOURCE_TYPE_RAW_RANGE, || {
|
||||||
input_type,
|
format!("Invalid source type: {:#04x}", input_type)
|
||||||
INPUT_SOURCE_TYPE_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid source type: {:#04x}", input_type),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_discrete_frequency(frequency: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_discrete_frequency(frequency: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(frequency, DISCRETE_FREQUENCY_RAW_RANGE, || {
|
||||||
frequency,
|
format!("Invalid frequency: {:#04x}", frequency)
|
||||||
DISCRETE_FREQUENCY_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid frequency: {:#04x}", frequency),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_matrix_bitmask(input_bitmask: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_matrix_bitmask(input_bitmask: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(input_bitmask, MATRIX_INPUT_MASK_RANGE, || {
|
||||||
input_bitmask,
|
format!("Invalid matrix bitmask: {:#04x}", input_bitmask)
|
||||||
MATRIX_INPUT_MASK_RANGE,
|
})
|
||||||
|| format!("Invalid matrix bitmask: {:#04x}", input_bitmask),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_link_channel(
|
pub fn validate_raw_link_channel(
|
||||||
@@ -273,20 +210,18 @@ pub fn validate_raw_link_channel(
|
|||||||
output_link_mask(source_channel)
|
output_link_mask(source_channel)
|
||||||
}
|
}
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
ValidationError::value(
|
ValidationError::value(format!(
|
||||||
format!("Invalid link source channel: {:#04x}", source_channel)
|
"Invalid link source channel: {:#04x}",
|
||||||
)
|
source_channel
|
||||||
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
validate_bitmask(
|
validate_bitmask(destination_channel_bitmask, allowed_mask, || {
|
||||||
destination_channel_bitmask,
|
format!(
|
||||||
allowed_mask,
|
|
||||||
|| format!(
|
|
||||||
"Invalid link bitmask {:#04x} for source channel {:#04x}",
|
"Invalid link bitmask {:#04x} for source channel {:#04x}",
|
||||||
destination_channel_bitmask,
|
destination_channel_bitmask, source_channel
|
||||||
source_channel
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_channel_name(raw_name: &[u8]) -> Result<(), ValidationError> {
|
pub fn validate_raw_channel_name(raw_name: &[u8]) -> Result<(), ValidationError> {
|
||||||
@@ -299,35 +234,27 @@ pub fn validate_raw_channel_name(raw_name: &[u8]) -> Result<(), ValidationError>
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_gate_threshold(raw_threshold: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_gate_threshold(raw_threshold: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_threshold, GATE_THRESHOLD_RAW_RANGE, || {
|
||||||
raw_threshold,
|
format!("Invalid gate threshold: {:#06x}", raw_threshold)
|
||||||
GATE_THRESHOLD_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid gate threshold: {:#06x}", raw_threshold),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_hold(raw_hold: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_hold(raw_hold: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_hold, HOLD_RAW_RANGE, || {
|
||||||
raw_hold,
|
format!("Invalid hold: {:#06x}", raw_hold)
|
||||||
HOLD_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid hold: {:#06x}", raw_hold),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_matrix_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
pub fn validate_raw_matrix_gain(raw_gain: u16) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(raw_gain, MATRIX_GAIN_RAW_RANGE, || {
|
||||||
raw_gain,
|
format!("Invalid matrix gain: {:#06x}", raw_gain)
|
||||||
MATRIX_GAIN_RAW_RANGE,
|
})
|
||||||
|| format!("Invalid matrix gain: {:#06x}", raw_gain),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_raw_bool(value: u8) -> Result<(), ValidationError> {
|
pub fn validate_raw_bool(value: u8) -> Result<(), ValidationError> {
|
||||||
validate_range(
|
validate_range(value, 0..=1, || {
|
||||||
value,
|
format!("Invalid bool value: {:#04x}", value)
|
||||||
0..=1,
|
})
|
||||||
|| format!("Invalid bool value: {:#04x}", value),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
+3
-9
@@ -1,5 +1,5 @@
|
|||||||
use std::{collections::HashMap, net::IpAddr};
|
use std::{collections::HashMap, net::IpAddr};
|
||||||
use strum_macros::{EnumIter, EnumCount};
|
use strum_macros::{EnumCount, EnumIter};
|
||||||
|
|
||||||
// ------ Enums ------
|
// ------ Enums ------
|
||||||
|
|
||||||
@@ -354,19 +354,13 @@ pub struct DSPConfigState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DSPConfigState {
|
impl DSPConfigState {
|
||||||
pub fn input_state_mut(
|
pub fn input_state_mut(&mut self, channel: InputChannel) -> &mut InputChannelState {
|
||||||
&mut self,
|
|
||||||
channel: InputChannel,
|
|
||||||
) -> &mut InputChannelState {
|
|
||||||
self.input_states
|
self.input_states
|
||||||
.get_mut(&channel)
|
.get_mut(&channel)
|
||||||
.expect("Invalid input channel state")
|
.expect("Invalid input channel state")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn output_state_mut(
|
pub fn output_state_mut(&mut self, channel: OutputChannel) -> &mut OutputChannelState {
|
||||||
&mut self,
|
|
||||||
channel: OutputChannel,
|
|
||||||
) -> &mut OutputChannelState {
|
|
||||||
self.output_states
|
self.output_states
|
||||||
.get_mut(&channel)
|
.get_mut(&channel)
|
||||||
.expect("Invalid output channel state")
|
.expect("Invalid output channel state")
|
||||||
|
|||||||
+1
-7
@@ -5,14 +5,9 @@ use dsp_thomann::dsp408::DSP408;
|
|||||||
use dsp_thomann::dsp408::types;
|
use dsp_thomann::dsp408::types;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
let host = IpAddr::from_str("192.168.49.30")?;
|
let host = IpAddr::from_str("192.168.49.30")?;
|
||||||
|
|
||||||
let mut dsp = DSP408::new(
|
let mut dsp = DSP408::new(host, 9761, 1)?;
|
||||||
host,
|
|
||||||
9761,
|
|
||||||
1,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
println!("Connecting to DSP...");
|
println!("Connecting to DSP...");
|
||||||
match dsp.connect() {
|
match dsp.connect() {
|
||||||
@@ -35,6 +30,5 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
dsp.disconnect();
|
dsp.disconnect();
|
||||||
println!("Disconnected");
|
println!("Disconnected");
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user