Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Filler derive macro for FillExt #47

Merged
merged 10 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 11 additions & 174 deletions nidhogg/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Convenience types used to make interacting with the NAO more convenient.
//!

use nidhogg_derive::Builder;
use nidhogg_derive::{Builder, Filler};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -31,7 +31,7 @@ pub trait FillExt<T> {
/// Struct representing the LEDs on top of the NAO robot's head.
///
/// Each value represents the intensity of a white LED.
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Skull {
pub left_front_0: f32,
Expand All @@ -49,25 +49,6 @@ pub struct Skull {
pub right_rear_2: f32,
}

impl FillExt<f32> for Skull {
fn fill(intensity: f32) -> Skull {
Skull {
left_front_0: intensity,
left_front_1: intensity,
left_middle_0: intensity,
left_rear_0: intensity,
left_rear_1: intensity,
left_rear_2: intensity,
right_front_0: intensity,
right_front_1: intensity,
right_middle_0: intensity,
right_rear_0: intensity,
right_rear_1: intensity,
right_rear_2: intensity,
}
}
}

/// Struct representing the LED intensities in the left ear of the robot.
///
/// ## LED order:
Expand All @@ -80,7 +61,7 @@ impl FillExt<f32> for Skull {
/// 216 144
/// 180
/// ```
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LeftEar {
pub intensity_0_deg: f32,
Expand All @@ -95,23 +76,6 @@ pub struct LeftEar {
pub intensity_324_deg: f32,
}

impl FillExt<f32> for LeftEar {
fn fill(intensity: f32) -> LeftEar {
LeftEar {
intensity_0_deg: intensity,
intensity_36_deg: intensity,
intensity_72_deg: intensity,
intensity_108_deg: intensity,
intensity_144_deg: intensity,
intensity_180_deg: intensity,
intensity_216_deg: intensity,
intensity_252_deg: intensity,
intensity_288_deg: intensity,
intensity_324_deg: intensity,
}
}
}

/// Struct representing the LED intensities in the right ear of the robot.
///
/// ## LED order:
Expand All @@ -124,7 +88,7 @@ impl FillExt<f32> for LeftEar {
/// 216 144
/// 180
/// ```
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RightEar {
pub intensity_0_deg: f32,
Expand All @@ -139,23 +103,6 @@ pub struct RightEar {
pub intensity_324_deg: f32,
}

impl FillExt<f32> for RightEar {
fn fill(intensity: f32) -> RightEar {
RightEar {
intensity_0_deg: intensity,
intensity_36_deg: intensity,
intensity_72_deg: intensity,
intensity_108_deg: intensity,
intensity_144_deg: intensity,
intensity_180_deg: intensity,
intensity_216_deg: intensity,
intensity_252_deg: intensity,
intensity_288_deg: intensity,
intensity_324_deg: intensity,
}
}
}

/// Struct representing an RGB color.
#[derive(Debug, Default, Clone, Copy, Builder)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -314,7 +261,7 @@ impl Color {
/// 135 225
/// 180
/// ```
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LeftEye {
pub color_0_deg: Color,
Expand All @@ -327,21 +274,6 @@ pub struct LeftEye {
pub color_315_deg: Color,
}

impl FillExt<Color> for LeftEye {
fn fill(color: Color) -> LeftEye {
LeftEye {
color_0_deg: color,
color_45_deg: color,
color_90_deg: color,
color_135_deg: color,
color_180_deg: color,
color_225_deg: color,
color_270_deg: color,
color_315_deg: color,
}
}
}

/// Struct representing the RGB LEDs in the left eye of the robot.
/// ## LED order:
/// These LEDs are placed in the following order:
Expand All @@ -352,7 +284,7 @@ impl FillExt<Color> for LeftEye {
/// 135 225
/// 180
/// ```
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RightEye {
pub color_0_deg: Color,
Expand All @@ -365,23 +297,8 @@ pub struct RightEye {
pub color_315_deg: Color,
}

impl FillExt<Color> for RightEye {
fn fill(color: Color) -> RightEye {
RightEye {
color_0_deg: color,
color_45_deg: color,
color_90_deg: color,
color_135_deg: color,
color_180_deg: color,
color_225_deg: color,
color_270_deg: color,
color_315_deg: color,
}
}
}

/// Struct containing values of type `T` for all the joints
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct JointArray<T> {
/// The yaw joint of the robot's head, allowing rotation horizontally.
Expand Down Expand Up @@ -517,38 +434,6 @@ impl<T> JointArray<T> {
}
}

impl<T: Clone> FillExt<T> for JointArray<T> {
fn fill(value: T) -> JointArray<T> {
JointArray {
head_yaw: value.clone(),
head_pitch: value.clone(),
left_shoulder_pitch: value.clone(),
left_shoulder_roll: value.clone(),
left_elbow_yaw: value.clone(),
left_elbow_roll: value.clone(),
left_wrist_yaw: value.clone(),
left_hip_yaw_pitch: value.clone(),
left_hip_roll: value.clone(),
left_hip_pitch: value.clone(),
left_knee_pitch: value.clone(),
left_ankle_pitch: value.clone(),
left_ankle_roll: value.clone(),
right_shoulder_pitch: value.clone(),
right_shoulder_roll: value.clone(),
right_elbow_yaw: value.clone(),
right_elbow_roll: value.clone(),
right_wrist_yaw: value.clone(),
right_hip_roll: value.clone(),
right_hip_pitch: value.clone(),
right_knee_pitch: value.clone(),
right_ankle_pitch: value.clone(),
right_ankle_roll: value.clone(),
left_hand: value.clone(),
right_hand: value.clone(),
}
}
}

/// Struct representing the battery status of the robot.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -666,23 +551,14 @@ pub struct Touch {
}

/// Wrapper struct containing the head joints of the robot.
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
pub struct HeadJoints<T> {
pub yaw: T,
pub pitch: T,
}

impl<T: Clone> FillExt<T> for HeadJoints<T> {
fn fill(value: T) -> HeadJoints<T> {
HeadJoints {
yaw: value.clone(),
pitch: value.clone(),
}
}
}

/// Wrapper struct containing the left leg joints of the robot.
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
pub struct LeftLegJoints<T> {
pub hip_yaw_pitch: T,
pub hip_roll: T,
Expand All @@ -692,21 +568,8 @@ pub struct LeftLegJoints<T> {
pub ankle_roll: T,
}

impl<T: Clone> FillExt<T> for LeftLegJoints<T> {
fn fill(value: T) -> LeftLegJoints<T> {
LeftLegJoints {
hip_yaw_pitch: value.clone(),
hip_roll: value.clone(),
hip_pitch: value.clone(),
knee_pitch: value.clone(),
ankle_pitch: value.clone(),
ankle_roll: value.clone(),
}
}
}

/// Wrapper struct containing right left leg joints of the robot.
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
pub struct RightLegJoints<T> {
// This value does not exist
// pub hip_yaw_pitch: T,
Expand All @@ -717,19 +580,6 @@ pub struct RightLegJoints<T> {
pub ankle_roll: T,
}

impl<T: Clone> FillExt<T> for RightLegJoints<T> {
fn fill(value: T) -> RightLegJoints<T> {
RightLegJoints {
// hip_yaw_pitch: value.clone(),
hip_roll: value.clone(),
hip_pitch: value.clone(),
knee_pitch: value.clone(),
ankle_pitch: value.clone(),
ankle_roll: value.clone(),
}
}
}

/// Wrapper struct containing joint values for both legs of the robot.
#[derive(Builder, Clone, Debug, Default)]
pub struct LegJoints<T> {
Expand All @@ -747,7 +597,7 @@ impl<T: Clone> FillExt<T> for LegJoints<T> {
}

/// Wrapper struct containing the joints for a single arm of the robot.
#[derive(Builder, Clone, Debug, Default)]
#[derive(Builder, Clone, Debug, Default, Filler)]
pub struct SingleArmJoints<T> {
pub shoulder_pitch: T,
pub shoulder_roll: T,
Expand All @@ -757,19 +607,6 @@ pub struct SingleArmJoints<T> {
pub hand: T,
}

impl<T: Clone> FillExt<T> for SingleArmJoints<T> {
fn fill(value: T) -> SingleArmJoints<T> {
SingleArmJoints {
shoulder_pitch: value.clone(),
shoulder_roll: value.clone(),
elbow_yaw: value.clone(),
elbow_roll: value.clone(),
wrist_yaw: value.clone(),
hand: value.clone(),
}
}
}

/// Type definition for the left arm joints of the robot.
/// Introduced for api consistenty with [`LeftLegJoints`].
pub type LeftArmJoints<T> = SingleArmJoints<T>;
Expand Down
Loading
Loading