embedded-charts 0.3.0

A rich graph framework for embedded systems using embedded-graphics with std/no_std support
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Time abstraction layer for animation systems.
//!
//! This module provides a time abstraction that works in both std and no_std environments.
//! It allows the animation system to work with different timer sources while maintaining
//! a consistent API.

/// Time value in milliseconds.
pub type Milliseconds = u32;

/// Time value in microseconds.
pub type Microseconds = u64;

/// Trait for providing time information to the animation system.
///
/// This trait abstracts time operations to support both std and no_std environments.
/// Implementations can use system clocks, hardware timers, or external time sources.
pub trait TimeProvider {
    /// Get the current time in milliseconds since some reference point.
    ///
    /// The reference point doesn't matter as long as it's consistent.
    /// This is typically used for calculating elapsed time between calls.
    fn current_time_ms(&self) -> Milliseconds;

    /// Get the current time in microseconds since some reference point.
    ///
    /// This provides higher precision timing for fine-grained animations.
    /// The reference point should be the same as `current_time_ms()`.
    fn current_time_us(&self) -> Microseconds;

    /// Calculate elapsed time in milliseconds since the last call.
    ///
    /// This is a convenience method that handles the delta calculation.
    /// The implementation should track the last time internally.
    fn elapsed_ms(&mut self) -> Milliseconds {
        let current = self.current_time_ms();
        let last = self.last_time_ms();
        self.update_last_time_ms(current);
        current.saturating_sub(last)
    }

    /// Calculate elapsed time in microseconds since the last call.
    ///
    /// This is a convenience method that handles the delta calculation.
    /// The implementation should track the last time internally.
    fn elapsed_us(&mut self) -> Microseconds {
        let current = self.current_time_us();
        let last = self.last_time_us();
        self.update_last_time_us(current);
        current.saturating_sub(last)
    }

    /// Get the last recorded time in milliseconds.
    ///
    /// This is used internally by `elapsed_ms()`.
    fn last_time_ms(&self) -> Milliseconds;

    /// Get the last recorded time in microseconds.
    ///
    /// This is used internally by `elapsed_us()`.
    fn last_time_us(&self) -> Microseconds;

    /// Update the last recorded time in milliseconds.
    ///
    /// This is used internally by `elapsed_ms()`.
    fn update_last_time_ms(&mut self, time: Milliseconds);

    /// Update the last recorded time in microseconds.
    ///
    /// This is used internally by `elapsed_us()`.
    fn update_last_time_us(&mut self, time: Microseconds);

    /// Reset the time provider to start fresh timing.
    ///
    /// This resets any internal state and starts timing from the current moment.
    fn reset(&mut self) {
        let current_ms = self.current_time_ms();
        let current_us = self.current_time_us();
        self.update_last_time_ms(current_ms);
        self.update_last_time_us(current_us);
    }
}

/// A monotonic time provider for no_std environments.
///
/// This implementation uses a user-provided timer function to get the current time.
/// It's designed to work with hardware timers or other monotonic time sources.
#[derive(Debug)]
pub struct MonotonicTimeProvider<F>
where
    F: Fn() -> Microseconds,
{
    /// Function to get current time in microseconds.
    timer_fn: F,
    /// Last recorded time in milliseconds.
    last_ms: Milliseconds,
    /// Last recorded time in microseconds.
    last_us: Microseconds,
}

impl<F> MonotonicTimeProvider<F>
where
    F: Fn() -> Microseconds,
{
    /// Create a new monotonic time provider with the given timer function.
    ///
    /// The timer function should return the current time in microseconds
    /// from a monotonic source (e.g., hardware timer, system tick counter).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use embedded_charts::time::MonotonicTimeProvider;
    ///
    /// // Example with a hypothetical hardware timer
    /// fn hardware_timer_get_us() -> u64 {
    ///     // Mock implementation - in real use, this would read from hardware
    ///     1000
    /// }
    ///
    /// let timer = MonotonicTimeProvider::new(|| {
    ///     // Get microseconds from hardware timer
    ///     hardware_timer_get_us()
    /// });
    /// ```
    pub fn new(timer_fn: F) -> Self {
        let current_us = timer_fn();
        Self {
            timer_fn,
            last_ms: (current_us / 1000) as Milliseconds,
            last_us: current_us,
        }
    }

    /// Create a new monotonic time provider with a tick-based timer.
    ///
    /// This is useful when you have a timer that increments at a known frequency.
    ///
    /// # Arguments
    ///
    /// * `get_ticks` - Function that returns the current tick count
    /// * `ticks_per_second` - Number of ticks per second (timer frequency)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use embedded_charts::time::{MonotonicTimeProvider, Microseconds};
    ///
    /// // Mock hardware timer function
    /// fn hardware_timer_get_ticks() -> u64 {
    ///     // Mock implementation - in real use, this would read from hardware
    ///     1000
    /// }
    ///
    /// // Example with a 1MHz timer (1,000,000 ticks per second)
    /// // let get_ticks = || hardware_timer_get_ticks();
    /// // let timer = MonotonicTimeProvider::from_ticks(get_ticks, 1_000_000);
    ///
    /// // Use the timer to get current time
    /// // let current_time = timer.now();
    /// ```
    pub fn from_ticks<G>(
        get_ticks: G,
        ticks_per_second: u32,
    ) -> MonotonicTimeProvider<impl Fn() -> Microseconds>
    where
        G: Fn() -> u64,
    {
        let timer_fn = move || {
            let ticks = get_ticks();
            // Convert ticks to microseconds
            (ticks * 1_000_000) / ticks_per_second as u64
        };

        let current_us = timer_fn();
        MonotonicTimeProvider {
            timer_fn,
            last_ms: (current_us / 1000) as Milliseconds,
            last_us: current_us,
        }
    }
}

impl<F> TimeProvider for MonotonicTimeProvider<F>
where
    F: Fn() -> Microseconds,
{
    fn current_time_ms(&self) -> Milliseconds {
        ((self.timer_fn)() / 1000) as Milliseconds
    }

    fn current_time_us(&self) -> Microseconds {
        (self.timer_fn)()
    }

    fn last_time_ms(&self) -> Milliseconds {
        self.last_ms
    }

    fn last_time_us(&self) -> Microseconds {
        self.last_us
    }

    fn update_last_time_ms(&mut self, time: Milliseconds) {
        self.last_ms = time;
    }

    fn update_last_time_us(&mut self, time: Microseconds) {
        self.last_us = time;
    }
}

/// A simple time provider for testing and simulation.
///
/// This provider allows manual control of time, useful for testing animations
/// or when you want to control timing externally.
#[derive(Debug, Clone)]
pub struct ManualTimeProvider {
    /// Current time in microseconds.
    current_us: Microseconds,
    /// Last recorded time in milliseconds.
    last_ms: Milliseconds,
    /// Last recorded time in microseconds.
    last_us: Microseconds,
}

impl ManualTimeProvider {
    /// Create a new manual time provider starting at time zero.
    pub fn new() -> Self {
        Self {
            current_us: 0,
            last_ms: 0,
            last_us: 0,
        }
    }

    /// Create a new manual time provider starting at the specified time.
    pub fn with_start_time(start_us: Microseconds) -> Self {
        Self {
            current_us: start_us,
            last_ms: (start_us / 1000) as Milliseconds,
            last_us: start_us,
        }
    }

    /// Advance time by the specified number of milliseconds.
    pub fn advance_ms(&mut self, delta_ms: Milliseconds) {
        self.current_us += (delta_ms as Microseconds) * 1000;
    }

    /// Advance time by the specified number of microseconds.
    pub fn advance_us(&mut self, delta_us: Microseconds) {
        self.current_us += delta_us;
    }

    /// Set the current time to the specified value in microseconds.
    pub fn set_time_us(&mut self, time_us: Microseconds) {
        self.current_us = time_us;
    }

    /// Set the current time to the specified value in milliseconds.
    pub fn set_time_ms(&mut self, time_ms: Milliseconds) {
        self.current_us = (time_ms as Microseconds) * 1000;
    }
}

impl Default for ManualTimeProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl TimeProvider for ManualTimeProvider {
    fn current_time_ms(&self) -> Milliseconds {
        (self.current_us / 1000) as Milliseconds
    }

    fn current_time_us(&self) -> Microseconds {
        self.current_us
    }

    fn last_time_ms(&self) -> Milliseconds {
        self.last_ms
    }

    fn last_time_us(&self) -> Microseconds {
        self.last_us
    }

    fn update_last_time_ms(&mut self, time: Milliseconds) {
        self.last_ms = time;
    }

    fn update_last_time_us(&mut self, time: Microseconds) {
        self.last_us = time;
    }
}

/// Standard library time provider using `std::time::Instant`.
///
/// This provider uses the system's monotonic clock and is only available
/// when the `std` feature is enabled.
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StdTimeProvider {
    /// Reference point for time calculations.
    start_time: std::time::Instant,
    /// Last recorded time in milliseconds.
    last_ms: Milliseconds,
    /// Last recorded time in microseconds.
    last_us: Microseconds,
}

#[cfg(feature = "std")]
impl StdTimeProvider {
    /// Create a new standard library time provider.
    pub fn new() -> Self {
        let now = std::time::Instant::now();
        Self {
            start_time: now,
            last_ms: 0,
            last_us: 0,
        }
    }
}

#[cfg(feature = "std")]
impl Default for StdTimeProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "std")]
impl TimeProvider for StdTimeProvider {
    fn current_time_ms(&self) -> Milliseconds {
        self.start_time.elapsed().as_millis() as Milliseconds
    }

    fn current_time_us(&self) -> Microseconds {
        self.start_time.elapsed().as_micros() as Microseconds
    }

    fn last_time_ms(&self) -> Milliseconds {
        self.last_ms
    }

    fn last_time_us(&self) -> Microseconds {
        self.last_us
    }

    fn update_last_time_ms(&mut self, time: Milliseconds) {
        self.last_ms = time;
    }

    fn update_last_time_us(&mut self, time: Microseconds) {
        self.last_us = time;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_manual_time_provider() {
        let mut provider = ManualTimeProvider::new();

        assert_eq!(provider.current_time_ms(), 0);
        assert_eq!(provider.current_time_us(), 0);

        provider.advance_ms(100);
        assert_eq!(provider.current_time_ms(), 100);
        assert_eq!(provider.current_time_us(), 100_000);

        provider.advance_us(500);
        assert_eq!(provider.current_time_ms(), 100);
        assert_eq!(provider.current_time_us(), 100_500);
    }

    #[test]
    fn test_manual_time_provider_elapsed() {
        let mut provider = ManualTimeProvider::new();

        // First call should return 0 (no time has passed)
        assert_eq!(provider.elapsed_ms(), 0);

        provider.advance_ms(50);
        assert_eq!(provider.elapsed_ms(), 50);

        provider.advance_ms(25);
        assert_eq!(provider.elapsed_ms(), 25);
    }

    #[test]
    fn test_monotonic_time_provider() {
        use core::cell::RefCell;

        let counter = RefCell::new(0u64);
        let timer_fn = || {
            let mut c = counter.borrow_mut();
            *c += 1000; // Advance by 1ms each call
            *c
        };

        let provider = MonotonicTimeProvider::new(timer_fn);

        let first_time = provider.current_time_us();
        let second_time = provider.current_time_us();

        assert!(second_time > first_time);
        assert_eq!(second_time - first_time, 1000); // 1ms difference
    }

    // #[test]
    // fn test_monotonic_time_provider_from_ticks() {
    //     // TODO: Fix type inference issues with closure types
    //     // This test is temporarily disabled due to complex type inference
    // }

    #[cfg(feature = "std")]
    #[test]
    fn test_std_time_provider() {
        let provider = StdTimeProvider::new();

        let first_time = provider.current_time_ms();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let second_time = provider.current_time_ms();

        assert!(second_time >= first_time + 10);
    }

    #[test]
    fn test_time_provider_reset() {
        let mut provider = ManualTimeProvider::new();

        provider.advance_ms(100);
        let _ = provider.elapsed_ms(); // This should be 100

        provider.advance_ms(50);
        provider.reset();

        provider.advance_ms(25);
        let elapsed = provider.elapsed_ms();

        assert_eq!(elapsed, 25); // Should only count from reset point
    }
}