Skip to content

Commit

Permalink
Format code. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
r12f authored Jul 18, 2022
1 parent 5d1bb4d commit 69fde5e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
31 changes: 21 additions & 10 deletions divoom/src/animation/animation_builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::animation::animation_builder_error::*;
use crate::animation::animation_frame_builder::DivoomAnimationFrameBuilder;
use crate::dto::*;
use std::collections::BTreeMap;
use std::iter::once;
use std::time::Duration;
use crate::dto::*;
use crate::animation::animation_builder_error::*;
use crate::animation::animation_frame_builder::DivoomAnimationFrameBuilder;
use tiny_skia::{BlendMode, Pixmap};

pub struct DivoomAnimationBuilder {
Expand Down Expand Up @@ -110,17 +110,21 @@ impl DivoomAnimationBuilder {
}

fn build_divoom_animation_frame_buffer(frame: &Pixmap) -> DivoomImageAnimationFrameData {
let divoom_frame_buffer: Vec<u8> = frame.pixels().iter().flat_map(|p| once(p.red()).chain(once(p.green())).chain(once(p.blue()))).collect();
let divoom_frame_buffer: Vec<u8> = frame
.pixels()
.iter()
.flat_map(|p| once(p.red()).chain(once(p.green())).chain(once(p.blue())))
.collect();
let encoded_buffer = base64::encode(divoom_frame_buffer);
encoded_buffer
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;
use crate::animation::*;
use crate::test_utils;
use std::time::Duration;

#[test]
fn divoom_animation_builder_can_be_created() {
Expand Down Expand Up @@ -150,18 +154,25 @@ mod tests {

let builder = DivoomAnimationBuilder::new(16, Duration::from_millis(100)).unwrap();
let animation = builder.draw_frames(&frames, 0).build();
test_utils::assert_object_equal_with_baseline(&animation, "test_data/animation_builder_tests/single_frame_animation_expected.json");
test_utils::assert_object_equal_with_baseline(
&animation,
"test_data/animation_builder_tests/single_frame_animation_expected.json",
);
}

#[test]
fn divoom_animation_builder_can_build_multi_frame_animation() {
let frames =
DivoomAnimationResourceLoader::gif("test_data/animation_builder_tests/logo-16-rotate-4-frames.gif")
.unwrap();
let frames = DivoomAnimationResourceLoader::gif(
"test_data/animation_builder_tests/logo-16-rotate-4-frames.gif",
)
.unwrap();
assert_eq!(frames.len(), 4);

let builder = DivoomAnimationBuilder::new(16, Duration::from_millis(100)).unwrap();
let animation = builder.draw_frames(&frames, 0).build();
test_utils::assert_object_equal_with_baseline(&animation, "test_data/animation_builder_tests/multi_frames_animation_expected.json");
test_utils::assert_object_equal_with_baseline(
&animation,
"test_data/animation_builder_tests/multi_frames_animation_expected.json",
);
}
}
12 changes: 7 additions & 5 deletions divoom/src/clients/pixoo/pixoo_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,21 @@ impl PixooClient {
&self,
animation: DivoomImageAnimation,
) -> DivoomAPIResult<()> {
self.send_image_animation_with_id(DIVOOM_IMAGE_ANIMATION_ID_AUTO, animation).await
self.send_image_animation_with_id(DIVOOM_IMAGE_ANIMATION_ID_AUTO, animation)
.await
}

#[doc = "Send image animation with specific id."]
#[doc = "The `id` parameter should be the id that returned by `get_next_animation_id`."]
#[doc = "If `DIVOOM_IMAGE_ANIMATION_ID_AUTO` is used, we will automatically get the latest animation id and use it."]
#[doc = include_str!("../../divoom_contracts/pixoo/animation/api_send_image_animation_frame.md")]
pub async fn send_image_animation_with_id(
&self,
id: i32,
animation: DivoomImageAnimation,
) -> DivoomAPIResult<()> {
let animation_id = if id == DIVOOM_IMAGE_ANIMATION_ID_AUTO { self.get_next_animation_id().await? } else { id };
let animation_id = if id == DIVOOM_IMAGE_ANIMATION_ID_AUTO {
self.get_next_animation_id().await?
} else {
id
};

let response: DivoomPixooCommandBatchExecuteCommandsResponse =
PixooCommandBuilder::start_batch(self.client.clone())
Expand Down
9 changes: 6 additions & 3 deletions divoom/src/clients/pixoo/pixoo_command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,14 @@ impl PixooCommandBuilder {
);

#[doc = include_str!("../../divoom_contracts/pixoo/animation/api_send_image_animation_frame.md")]
pub fn send_image_animation(mut self, id: i32, animation: DivoomImageAnimation) -> PixooCommandBuilder {
pub fn send_image_animation(
mut self,
id: i32,
animation: DivoomImageAnimation,
) -> PixooCommandBuilder {
let payloads =
DivoomPixooCommandAnimationSendImageAnimationFrameRequestPayload::create_frames(
id,
animation,
id, animation,
);
payloads.into_iter().for_each(|payload| {
let request = DivoomPixooCommandAnimationSendImageAnimationFrameRequest::new(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This API sends the image animation definitions to Pixoo devices and trigger it p

To create an animation, the first request to draw the frame should always have with `PicOffset` set to 0, because it creates the image, otherwise other request cannot go through correctly.

When using the API in our library, we will ask for the `id` in the parameter, which maps to `PicId` in the final request. Note that:

- This `id` should be the id that returned by `get_next_animation_id`.
- If `DIVOOM_IMAGE_ANIMATION_ID_AUTO` is used, we will automatically get the latest animation id and use it.

Official doc: <http://doc.divoom-gz.com/web/#/12?page_id=93>

This API will generate requests like below:
Expand Down
13 changes: 8 additions & 5 deletions divoom/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::{env, fs};
use std::fmt::Debug;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::Debug;
use std::{env, fs};

pub fn assert_object_equal_with_baseline<T: Serialize + DeserializeOwned + PartialEq + Debug>(actual: &T, reference_file_path: &str) {
pub fn assert_object_equal_with_baseline<T: Serialize + DeserializeOwned + PartialEq + Debug>(
actual: &T,
reference_file_path: &str,
) {
if env::var("DIVOOM_API_GENERATE_TEST_DATA").is_ok() {
let actual_in_json_text =
serde_json::to_string_pretty(&actual).expect("Serialize actual data into json failed!");
Expand All @@ -25,4 +28,4 @@ pub fn assert_object_equal_with_baseline<T: Serialize + DeserializeOwned + Parti
serde_json::from_str(&expected_in_json_text).expect("Parsing reference data failed!");

assert_eq!(actual, &expected);
}
}

0 comments on commit 69fde5e

Please sign in to comment.