embassy-ssd1306-graphics 0.1.0

Primitives graphiques no_std pour écrans OLED SSD1306, ajoutant lignes, cercles, ellipses, triangles et courbes Bézier sans unsafe ni allocation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#![no_std]
#![forbid(unsafe_code)]
//! # embassy-ssd1306-graphics
//!
//! Couche graphique 2D `no_std` pour écrans OLED SSD1306 (128×64),
//! construite au-dessus de `embassy-ssd1306`.
//!
//! ## Rôle exact de ce crate
//!
//! Le driver `embassy-ssd1306` fournit déjà :
//! - `draw_pixel()`, `draw_hline()`, `draw_vline()`
//! - `draw_rect()`, `draw_filled_rect()`
//! - `draw_char()`, `draw_str()`, `draw_i16()`
//! - `draw_bitmap()`
//! - `clear()`, `fill()`, `flush()`
//!
//! Ce crate **ne duplique rien**. Il ajoute uniquement les primitives
//! que le driver ne propose pas :
//!
//! | Fonction          | Algorithme              |
//! |-------------------|-------------------------|
//! | [`line()`]          | Bresenham integer-only  |
//! | [`circle`]        | Midpoint integer-only   |
//! | [`fill_circle`]   | Midpoint + hlines       |
//! | [`triangle`]      | 3 appels à `line()`     |
//! | [`ellipse`]       | Midpoint généralisé     |
//! | [`bezier_quad`]   | De Casteljau integer-only |
//! | [`fill_triangle`] | Scanline integer-only   |
//! ## Architecture
//!
//! ```text
//! ┌──────────────────────────────────────┐
//! │          Votre application           │
//! │  line() / circle() / triangle() …   │
//! │  oled.draw_str() / oled.draw_i16()  │  ← driver direct pour le texte
//! └──────────┬───────────────────────────┘
//!            │ &mut Graphics       │ &mut Ssd1306
//! ┌──────────▼───────────┐         │
//! │  Graphics (ce crate) │         │
//! │  clipping · pixel()  │         │
//! └──────────┬───────────┘         │
//!            └─────────────────────┘
//!                    │ draw_pixel()
//! ┌──────────────────▼───────────────────┐
//! │       embassy-ssd1306 (driver)       │
//! │  framebuffer · I2C · flush()         │
//! └──────────────────────────────────────┘
//! ```
//!
//! ## Patron de borrow
//!
//! `Graphics` tient un `&mut Ssd1306` pour toute sa durée de vie.
//! Pour appeler `oled.flush()`, `oled.clear()` ou `oled.draw_str()`,
//! `gfx` doit être sorti de portée au préalable.
//!
//! ```rust,no_run
//! loop {
//!     oled.clear();
//!     {
//!         let mut gfx = Graphics::new(&mut oled);
//!         line(&mut gfx, 0, 0, 127, 63, true);
//!         circle(&mut gfx, 64, 32, 20, true);
//!     } // ← borrow libéré
//!     oled.draw_str(40, 3, b"RPi2350");
//!     oled.flush().await.unwrap();
//! }
//! ```

use embassy_ssd1306::Ssd1306;
use embedded_hal_async::i2c::I2c;

// ─────────────────────────────────────────────────────────────────────────────
// Contexte graphique
// ─────────────────────────────────────────────────────────────────────────────

/// Contexte graphique.
///
/// Wraps minimalement un `&mut Ssd1306<I>` pour :
/// - centraliser le **clipping** des coordonnées
/// - fournir un `pixel()` signé (`i32`) aux algorithmes Bresenham / midpoint
///
/// Le driver reste propriétaire du framebuffer et du bus I2C.
pub struct Graphics<'a, I: I2c> {
    display: &'a mut Ssd1306<I>,
}

impl<'a, I: I2c> Graphics<'a, I> {
    /// Crée un contexte graphique pour un écran 128×64.
    #[inline]
    pub fn new(display: &'a mut Ssd1306<I>) -> Self {
        Self { display }
    }

    /// Dessine un pixel avec clipping automatique.
    ///
    /// Les coordonnées négatives ou hors de `[0, 128[` × `[0, 64[`
    /// sont silencieusement ignorées aucun panic, aucun wrapping.
    ///
    /// Le driver gère lui-même un second clipping sur `u8` ;
    /// ce niveau-ci permet aux algorithmes de travailler en `i32`
    /// sans conversions coûteuses.
    #[inline(always)]
    pub fn pixel(&mut self, x: i32, y: i32, on: bool) {
        if x >= 0 && y >= 0 && x < 128 && y < 64 {
            self.display.draw_pixel(x as u8, y as u8, on);
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Ligne Bresenham
// ─────────────────────────────────────────────────────────────────────────────

/// Trace une ligne entre `(x0, y0)` et `(x1, y1)`.
///
/// **Algorithme :** Bresenham integer-only.  
/// Zéro division flottante, zéro multiplication,safe sur tout MCU sans FPU.
///
/// # Exemple
///
/// ```rust,no_run
/// line(&mut gfx, 0, 0, 127, 63, true);  // diagonale complète
/// line(&mut gfx, 0, 0, 127, 63, false); // efface la diagonale
/// ```
pub fn line<I: I2c>(
    gfx: &mut Graphics<'_, I>,
    mut x0: i32,
    mut y0: i32,
    x1: i32,
    y1: i32,
    on: bool,
) {
    let dx = (x1 - x0).abs();
    let sx = if x0 < x1 { 1 } else { -1 };
    let dy = -(y1 - y0).abs();
    let sy = if y0 < y1 { 1 } else { -1 };
    let mut err = dx + dy;

    loop {
        gfx.pixel(x0, y0, on);
        if x0 == x1 && y0 == y1 {
            break;
        }
        let e2 = 2 * err;
        if e2 >= dy {
            err += dy;
            x0 += sx;
        }
        if e2 <= dx {
            err += dx;
            y0 += sy;
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Cercle midpoint
// ─────────────────────────────────────────────────────────────────────────────

/// Trace le **contour** d'un cercle.
///
/// **Algorithme :** midpoint circle integer-only.  
/// Exploite la symétrie 8-octants : chaque itération dessine 8 pixels
/// symétriques, ce qui minimise le nombre d'appels à `pixel()`.
///
/// # Paramètres
///
/// - `(cx, cy)` : centre
/// - `r` : rayon en pixels
///
/// # Exemple
///
/// ```rust,no_run
/// circle(&mut gfx, 64, 32, 20, true);
/// ```
pub fn circle<I: I2c>(gfx: &mut Graphics<'_, I>, cx: i32, cy: i32, r: i32, on: bool) {
    if r <= 0 {
        gfx.pixel(cx, cy, on);
        return;
    }
    let mut x = r;
    let mut y = 0;
    let mut err = 0;

    while x >= y {
        gfx.pixel(cx + x, cy + y, on);
        gfx.pixel(cx + y, cy + x, on);
        gfx.pixel(cx - y, cy + x, on);
        gfx.pixel(cx - x, cy + y, on);
        gfx.pixel(cx - x, cy - y, on);
        gfx.pixel(cx - y, cy - x, on);
        gfx.pixel(cx + y, cy - x, on);
        gfx.pixel(cx + x, cy - y, on);

        y += 1;
        if err <= 0 {
            err += 2 * y + 1;
        } else {
            x -= 1;
            err += 2 * (y - x) + 1;
        }
    }
}

/// **Remplit** un cercle (disque plein).
///
/// Utilise le même algorithme midpoint, mais dessine des lignes
/// horizontales entre les points symétriques à chaque rangée.
/// Beaucoup plus rapide que d'appeler `circle()` en spirale.
///
/// # Exemple
///
/// ```rust,no_run
/// fill_circle(&mut gfx, 64, 32, 15, true);
/// ```
pub fn fill_circle<I: I2c>(gfx: &mut Graphics<'_, I>, cx: i32, cy: i32, r: i32, on: bool) {
    if r <= 0 {
        gfx.pixel(cx, cy, on);
        return;
    }
    let mut x = r;
    let mut y = 0;
    let mut err = 0;

    while x >= y {
        // Lignes horizontales symétriques (haut/bas, gauche/droite)
        for px in (cx - x)..=(cx + x) {
            gfx.pixel(px, cy + y, on);
            gfx.pixel(px, cy - y, on);
        }
        for px in (cx - y)..=(cx + y) {
            gfx.pixel(px, cy + x, on);
            gfx.pixel(px, cy - x, on);
        }

        y += 1;
        if err <= 0 {
            err += 2 * y + 1;
        } else {
            x -= 1;
            err += 2 * (y - x) + 1;
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Triangle
// ─────────────────────────────────────────────────────────────────────────────

/// Trace le **contour** d'un triangle défini par trois sommets.
///
/// Implémenté comme trois appels à [`line()`], aucune logique propre.
///
/// # Exemple
///
/// ```rust,no_run
/// triangle(&mut gfx, 64, 4, 20, 59, 108, 59, true);
/// ```
#[inline]
pub fn triangle<I: I2c>(
    gfx: &mut Graphics<'_, I>,
    x0: i32, y0: i32,
    x1: i32, y1: i32,
    x2: i32, y2: i32,
    on: bool,
) {
    line(gfx, x0, y0, x1, y1, on);
    line(gfx, x1, y1, x2, y2, on);
    line(gfx, x2, y2, x0, y0, on);
}




// ─────────────────────────────────────────────────────────────────────────────
// Ellipse midpoint généralisé
// ─────────────────────────────────────────────────────────────────────────────

/// Trace le **contour** d'une ellipse.
///
/// **Algorithme :** midpoint ellipse integer-only (Bresenham généralisé).
/// Deux phases : région 1 (pente < -1) puis région 2 (pente > -1).
///
/// # Paramètres
///
/// - `(cx, cy)` : centre
/// - `rx` : demi-axe horizontal
/// - `ry` : demi-axe vertical
///
/// # Exemple
///
/// ```rust,no_run
/// ellipse(&mut gfx, 64, 32, 40, 20, true); // ellipse large
/// ellipse(&mut gfx, 64, 32, 10, 10, true); // cercle (rx == ry)
/// ```
pub fn ellipse<I: I2c>(gfx: &mut Graphics<'_, I>, cx: i32, cy: i32, rx: i32, ry: i32, on: bool) {
    if rx <= 0 || ry <= 0 {
        gfx.pixel(cx, cy, on);
        return;
    }

    let rx2 = rx * rx;
    let ry2 = ry * ry;

    let mut x = 0i32;
    let mut y = ry;

    // Région 1
    let mut d1 = ry2 - rx2 * ry + rx2 / 4;
    let mut dx = 2 * ry2 * x;
    let mut dy = 2 * rx2 * y;

    while dx < dy {
        gfx.pixel(cx + x, cy + y, on);
        gfx.pixel(cx - x, cy + y, on);
        gfx.pixel(cx + x, cy - y, on);
        gfx.pixel(cx - x, cy - y, on);

        x += 1;
        dx += 2 * ry2;
        if d1 < 0 {
            d1 += dx + ry2;
        } else {
            y -= 1;
            dy -= 2 * rx2;
            d1 += dx - dy + ry2;
        }
    }

    // Région 2
    let mut d2 = ry2 * (x * x + x) + rx2 * (y * y - 2 * y + 1) - rx2 * ry2 + rx2;

    while y >= 0 {
        gfx.pixel(cx + x, cy + y, on);
        gfx.pixel(cx - x, cy + y, on);
        gfx.pixel(cx + x, cy - y, on);
        gfx.pixel(cx - x, cy - y, on);

        y -= 1;
        dy -= 2 * rx2;
        if d2 > 0 {
            d2 += rx2 - dy;
        } else {
            x += 1;
            dx += 2 * ry2;
            d2 += dx - dy + rx2;
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Courbe de Bézier quadratique De Casteljau
// ─────────────────────────────────────────────────────────────────────────────

/// Trace une **courbe de Bézier quadratique** (3 points de contrôle).
///
/// **Algorithme :** De Casteljau integer-only avec subdivision fixe.
/// `steps` contrôle la finesse du tracé (16–32 suffisent pour 128×64).
///
/// Les interpolations sont faites en entiers avec précision ×1024
/// pour éviter tout flottant.
///
/// # Paramètres
///
/// - `(x0, y0)` : point de départ
/// - `(x1, y1)` : point de contrôle
/// - `(x2, y2)` : point d'arrivée
/// - `steps` : nombre de segments (recommandé : 16 à 32)
///
/// # Exemple
///
/// ```rust,no_run
/// bezier_quad(&mut gfx, 10, 50, 64, 5, 118, 50, 24, true); // arche
/// ```
pub fn bezier_quad<I: I2c>(
    gfx: &mut Graphics<'_, I>,
    x0: i32, y0: i32,
    x1: i32, y1: i32,
    x2: i32, y2: i32,
    steps: i32,
    on: bool,
) {
    if steps <= 0 {
        return;
    }

    let mut px = x0;
    let mut py = y0;

    for i in 1..=steps {
        // t = i / steps en virgule fixe ×1024
        let t  = (i * 1024) / steps;         // t  ∈ [0, 1024]
        let t1 = 1024 - t;                   // 1-t

        // B(t) = (1-t)²·P0 + 2(1-t)t·P1 + t²·P2  (tout ×1024²)
        let nx = (t1 * t1 * x0 + 2 * t1 * t * x1 + t * t * x2) / (1024 * 1024);
        let ny = (t1 * t1 * y0 + 2 * t1 * t * y1 + t * t * y2) / (1024 * 1024);

        line(gfx, px, py, nx, ny, on);
        px = nx;
        py = ny;
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Triangle plein  scanline
// ─────────────────────────────────────────────────────────────────────────────

/// **Remplit** un triangle défini par trois sommets.
///
/// **Algorithme :** scanline — tri des sommets par Y, puis
/// interpolation linéaire integer-only des bords gauche/droit
/// à chaque rangée horizontale.
///
/// # Exemple
///
/// ```rust,no_run
/// fill_triangle(&mut gfx, 64, 4, 20, 59, 108, 59, true);
/// ```
pub fn fill_triangle<I: I2c>(
    gfx: &mut Graphics<'_, I>,
    x0: i32, mut y0: i32,
    x1: i32, mut y1: i32,
    x2: i32, mut y2: i32,
    on: bool,
) {
    // Tri des sommets par Y croissant (bubble sort sur 3 éléments)
    let (mut x0, mut x1, mut x2) = (x0, x1, x2);
    if y0 > y1 { core::mem::swap(&mut y0, &mut y1); core::mem::swap(&mut x0, &mut x1); }
    if y1 > y2 { core::mem::swap(&mut y1, &mut y2); core::mem::swap(&mut x1, &mut x2); }
    if y0 > y1 { core::mem::swap(&mut y0, &mut y1); core::mem::swap(&mut x0, &mut x1); }

    let total_h = y2 - y0;
    if total_h == 0 {
        // Triangle dégénéré tracer une seule ligne
        let xmin = x0.min(x1).min(x2);
        let xmax = x0.max(x1).max(x2);
        for x in xmin..=xmax {
            gfx.pixel(x, y0, on);
        }
        return;
    }

    let upper_h = y1 - y0;
    let lower_h = y2 - y1;

    // Moitié supérieure : y0 → y1
    for y in y0..=y1 {
        let dy = y - y0;
        // Interpolation integer-only ×total_h pour éviter la division
        let xa = x0 + (x2 - x0) * dy / total_h;
        let xb = if upper_h == 0 {
            x1
        } else {
            x0 + (x1 - x0) * dy / upper_h
        };
        let (xmin, xmax) = if xa < xb { (xa, xb) } else { (xb, xa) };
        for x in xmin..=xmax {
            gfx.pixel(x, y, on);
        }
    }

    // Moitié inférieure : y1 → y2
    for y in y1..=y2 {
        let dy = y - y0;
        let xa = x0 + (x2 - x0) * dy / total_h;
        let xb = if lower_h == 0 {
            x1
        } else {
            x1 + (x2 - x1) * (y - y1) / lower_h
        };
        let (xmin, xmax) = if xa < xb { (xa, xb) } else { (xb, xa) };
        for x in xmin..=xmax {
            gfx.pixel(x, y, on);
        }
    }
}