rust_powered_lego/
lib.rs

1//! This crate aims to control Lego hub and its various peripherals with the Rust programming language.
2//! 
3//! ConnectionManager is the object to start with.
4//! Ask the CM for a Hub, with its name or address.
5//! Then, kindly ask the Hub to get a port of specific type (sorry, currently only the motor).
6//! 
7//! This crate is far from completion - more types and functionality will be added in the future.
8//! 
9//! Below are the main traits. More located at lego/mod.rs
10//! 
11
12use std::pin::Pin;
13
14use anyhow::Result;
15use async_trait::async_trait;
16
17use btleplug::api::ValueNotification;
18use hub::{
19    PortInfoValueReply, PortInfoModeReply, PortInfoCombinationsReply
20};
21use lego::{
22    message_parameters::{
23        PortModeInformationType,
24        PortOutputCommandParams, 
25        StartupAndCompletionInfo,
26    },
27    consts:: {
28        EndState, 
29        Profile,
30    }
31};
32use ports::{
33    Motor,
34};
35use tokio_stream::Stream;
36
37pub mod connection_manager;
38pub mod hub;
39pub mod lego;
40pub mod ports;
41
42
43/* Hubs type */
44
45#[async_trait]
46pub trait HubType {
47    
48    async fn shut_down_hub(&self) -> Result<()>;
49
50    async fn get_notification(&self) -> Result<Pin<Box<dyn Stream<Item = ValueNotification> + Send>>>;
51
52    async fn get_port_info_value(
53        &self, 
54        port_id: u8,
55    ) -> Result<PortInfoValueReply>;
56
57    async fn get_port_info_raw_value(
58        &self,
59        port_id: u8
60    ) -> Result<i32>;
61
62    async fn get_port_info_mode(
63        &self, 
64        port_id: u8,
65    ) -> Result<PortInfoModeReply>;
66
67    async fn get_port_info_combinations(
68        &self, 
69        port_id: u8,
70    ) -> Result<PortInfoCombinationsReply>;
71
72    async fn get_mode_information(
73        &self, 
74        port_id: u8, 
75        mode_id: u8, 
76        info_type: PortModeInformationType
77    ) -> Result<Vec<u8>>;
78
79    async fn setup_port_input_format(
80        &self,
81        port_id:                u8,
82        mode_id:                u8,
83        delta:                  u32,
84        enable_notifications:   bool,
85    ) -> Result<()>;
86
87    async fn send_output_command(&self, subcommand: PortOutputCommandParams)-> Result<Vec<u8>>;
88
89    async fn get_motor(&self, port_id: u8) -> Result<Motor>;
90}
91
92
93
94/* Ports type */
95
96#[async_trait]
97pub trait MotorType {
98    
99    async fn set_acceleration_time(
100        &self, 
101        time: i16,
102        start_up_info: StartupAndCompletionInfo,
103    ) -> Result<Vec<u8>>;
104
105    async fn set_deceleration_time(
106        &self,
107        time: i16,
108        start_up_info: StartupAndCompletionInfo,
109    ) -> Result<Vec<u8>>;
110
111    async fn start_power(
112        &self, 
113        power: i8, 
114        start_up_info: StartupAndCompletionInfo
115    ) -> Result<Vec<u8>>;
116
117    async fn start_speed(
118        &self, 
119        speed: i8, 
120        max_power: i8, 
121        use_profile: Profile, 
122        start_up_info: StartupAndCompletionInfo,
123    ) -> Result<Vec<u8>>;
124
125    async fn stop_motor(
126        &self,
127        end_state: EndState,
128        use_profile: Profile, 
129        start_up_info: StartupAndCompletionInfo
130    ) -> Result<Vec<u8>>;
131
132    async fn set_abs_position(
133        &self, 
134        position: i32, 
135        start_up_info: StartupAndCompletionInfo
136    ) -> Result<Vec<u8>>;
137
138    async fn go_to_abs_position(
139        &self, 
140        abs_pos: i32,
141        speed: i8,
142        max_power: i8,
143        end_state: EndState,
144        use_profile: Profile,
145        start_up_info: StartupAndCompletionInfo,
146    ) -> Result<Vec<u8>>;
147
148    async fn start_speed_for_deg (
149        &self, 
150        degrees: i32,
151        speed: i8,
152        max_power: i8,
153        end_state: EndState,
154        use_profile: Profile,
155        start_up_info: StartupAndCompletionInfo,
156    ) -> Result<Vec<u8>>;
157}