1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use tokio::net::UdpSocket;
use tokio::time::{sleep, Duration};

use crate::errors::{Result, TelloError};
use crate::wifi::wait_for_wifi;

const DEFAULT_DRONE_HOST:&str = "192.168.10.1";

const CONTROL_UDP_PORT:i32 = 8889;
// const STATE_UDP_PORT = 8890

/// Initial state - no WiFi network
#[derive(Debug)]
pub struct NoWifi;

/// The drone WiFi has been joined, but no UDP messages have been sent or received.
#[derive(Debug)]
pub struct Disconnected;

/// The connection exchange has been completed and the drone is ready to fly.
#[derive(Debug)]
pub struct Connected {
    sock: UdpSocket,
}

/// For interacting with the Tello EDU drone using the simple text-based UDP protocol.
///
/// The basic flow from the user's point of view is
///
///    SEND `command` → drone does something → RECEIVE `response` when it's finished
///
/// Messages are plain ASCII text, eg command `forward 10` → response `ok`
///
/// ```
/// use tello_edu::{Tello, Result};
/// 
/// #[tokio::main]
/// async fn main() {
///     fly().await.unwrap();
/// }
/// 
/// async fn fly() -> Result<()> {
///     // create a new drone in the `NoWifi` state     
///     let drone = Tello::new();
///
///     // wait until the host computer joins the drone's WiFi network
///     // (joining the network is not automatic - how it happens is up to you)
///     let drone = drone.wait_for_wifi().await?;
/// 
///     // establish connection and put the drone in "command" mode
///     let drone = drone.connect().await?;
/// 
///     // fly!
///     drone.take_off().await?;
///     drone.turn_clockwise(360).await?;
///     drone.land().await?;
/// 
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct Tello<S = NoWifi> {
    /// The connection state of the drone.
    state: S
}

impl Tello<NoWifi> {
    /// Create a new drone in a completely unconnected state.
    pub fn new() -> Self {
        Self { state: NoWifi }
    }

    /// Wait until the host joins the drone's WiFi network
    ///
    /// *nb* exactly how the the network is joined is up to you
    ///
    pub async fn wait_for_wifi(&self) -> Result<Tello<Disconnected>>  {
        println!("[Tello] waiting for WiFi...");
        wait_for_wifi("TELLO").await?;
        Ok(Tello { state: Disconnected })
    }
}

impl Tello<Disconnected> {
    pub async fn connect(&self) -> Result<Tello<Connected>> {
        let local_address = format!("0.0.0.0:{CONTROL_UDP_PORT}");

        let drone_host = DEFAULT_DRONE_HOST;
        let drone_address = format!("{drone_host}:{CONTROL_UDP_PORT}");

        println!("[Tello] CONNECT {local_address} → {drone_address}");

        // bind local socket
        println!("[Tello] binding local {local_address}...");
        let sock = UdpSocket::bind(&local_address).await?;
        
        // connect to drone
        println!("[Tello] connecting to drone at {drone_address}...");
        let mut i = 0;
        loop {
            i = i + 1;
            match sock.connect(&drone_address).await {
                Ok(_) => {
                    println!("[Tello] CONNECTED");
                    break;
                }
                Err(err) => {
                    println!("[Tello] connection attempt #{i} failed ({err}), retrying...");
                    sleep(Duration::from_millis(100)).await;
                }
            }
        }

        let drone = Tello { state: Connected { sock } };

        // tell drone to expect text SDK commands (not the private binary protocol)
        println!("[Tello] putting drone in command mode...");
        drone.send_expect_ok("command").await?;

        // check battery
        let b = drone.query_battery().await?;
        if b < 10 {
            println!("[Tello] WARNING low battery: {b}%");
        }
        else {
            println!("[Tello] battery: {b}%");  
        }

        Ok(drone)
    } 
}

impl Tello<Connected> {
    pub fn disconnect(&self) -> Tello<Disconnected> {
        println!("[Tello] DISCONNECT");
        Tello { state: Disconnected }
    }

    /// Sends a command to the drone using the simple Tello UDP protocol, returning the reponse.
    ///
    /// The basic flow from the user's point of view is
    ///
    ///    SEND command → drone does something → RECEIVE response when it's finished
    ///
    /// Messages are plain ASCII text, eg command `forward 10` → response `ok`
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send(&self, command:&str) -> Result<String> {
        println!("[Tello] SEND {command}");

        let s = &self.state.sock;
        s.send(command.as_bytes()).await?;

        let mut buf = vec![0; 256];        
        let n = s.recv(&mut buf).await?;

        buf.truncate(n);
        let r = String::from_utf8(buf)?;
        let response = r.trim().to_string();

        println!("[Tello] RECEIVED {response}");

        Ok(response)
    }

    /// Sends a command, resolving to an error if the response is not "ok"
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send_expect_ok(&self, command:&str) -> Result<()> {
        match self.send(command).await {
            Ok(response) => {
                if response == "ok" {
                    Ok(())
                }
                else {
                    Err(TelloError::NotOkResponse { response })
                }
            }
            Err(err) => Err(err)
        }
    }

    /// Sends a command, expecting no response at all from the drone.
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send_expect_nothing(&self, command:&str) -> Result<()> {
        println!("[Tello] SEND {command}");

        let s = &self.state.sock;
        s.send(command.as_bytes()).await?;

        Ok(())
    }

    /// Queries the drone battery level as a percentage.
    pub async fn query_battery(&self) -> Result<u8> {
        let response = self.send("battery?").await?;
        let battery = response.parse::<u8>()?;
        Ok(battery)
    }

    /// Immediately stop all motors
    ///
    /// warning! this will make the drone drop like a brick
    pub async fn emergency_stop(&self) -> Result<()> {
        self.send_expect_nothing("emergency").await
    }

    /// Take off and hover.
    pub async fn take_off(&self) -> Result<()> {
        self.send_expect_ok("takeoff").await
    }

    /// Land and stop motors
    pub async fn land(&self) -> Result<()> {
        self.send_expect_ok("land").await
    }

    /// Turn clockwise.
    ///
    /// - `degrees` Angle in degrees 1-360°
    ///
    pub async fn turn_clockwise(&self, degrees: u16) -> Result<()> {
        self.send_expect_ok(&format!("cw {degrees}")).await   
    }

    /// Turn counter-clockwise.
    ///
    /// - `degrees` Angle in degrees 1-360°
    pub async fn turn_counterclockwise(&self, degrees: u16) -> Result<()> {
        self.send_expect_ok(&format!("ccw {degrees}")).await   
    }

    /// Move straight up.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_up(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("up {distance}")).await
    }

    /// Move straight down.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_down(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("down {distance}")).await
    }
    
    /// Move straight left.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_left(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("left {distance}")).await
    }
    
    /// Move straight right.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_right(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("right {distance}")).await
    }
    
    /// Move straight forwards.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_forward(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("forward {distance}")).await
    }
    
    /// Move straight backwards.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_back(&self, distance: i16) -> Result<()> {
        self.send_expect_ok(&format!("back {distance}")).await
    }

}