| 552 | | Pour implémenter la FIFO, nous allons utiliser un tableau circulaire et des pointeurs |
| 553 | | {{{#!c |
| | 552 | Pour implémenter la FIFO, nous allons utiliser un tableau circulaire et des pointeurs. Il y a une structure et 2 fonctions d'accès. |
| | 553 | {{{#!c |
| | 554 | /** |
| | 555 | /** |
| | 556 | * Simple fifo (1 writer - 1 reader) |
| | 557 | * - data buffer of data |
| | 558 | * - pt_write write pointer for L fifos (0 at the beginning) |
| | 559 | * - pt_read read pointer for L fifos (0 at the beginning) |
| | 560 | * |
| | 561 | * data[] is used as a circular array. At the beginning (pt_read == pt_write) means an empty fifo |
| | 562 | * then when we push a data, we write it at pt_write, the we increment pt_write % fifo_size. |
| | 563 | * The fifo is full when it remains only one free cell, then when (pt_write + 1)%size == pt_read |
| | 564 | */ |
| 576 | | } |
| 577 | | }}} |
| 578 | | |
| 579 | | Les schémas ci-dessous |
| | 588 | if (fifo->pt_read != fifo->pt_write) { |
| | 589 | *c = fifo->data [fifo->pt_read]; |
| | 590 | fifo->pt_read = (fifo->pt_read + 1)% sizeof(fifo->data); |
| | 591 | return 1; |
| | 592 | } |
| | 593 | return 0; |
| | 594 | } |
| | 595 | }}} |
| | 596 | |
| | 597 | Les schémas ci-dessous le comportement de la FIFO. |
| | 598 | 1. A l'initialisation comme la structure est dans les variables globales, les pointeur `pt_read` et `pt_write` sont à 0. La fifo est vide puisque `pt_read == pt_write`. |
| | 599 | 1. On écrit `A` et on incrémente `pt_write`, `pt_read` ne bouge pas puisque l'on de lit pas. |
| | 600 | 1. On écrit `B` et `C`. |
| | 601 | 1. On lit `A` et on incrémente `pt_read`, on peut lire parce que `pt_read != pt_write` et donc la FIFO n'est pas vide |
| | 602 | 1. On écrit `D` et `E`. Lors de l'incrément de `pt_write` on revient à 0 à cause du modulo `size` |
| | 603 | 1. On écrit `F` et ce sera fini parce la fifo est pleine `(pt_write + 1)%size == pt_read`, si on veut écrire à nouveau, il faut lire. |
| | 604 | |
| 600 | | |
| 601 | | /** |
| 602 | | * \brief read from the FIFO |
| 603 | | * \param fifo structure of fifo to store data |
| 604 | | * \param c pointer on char to put the read char |
| 605 | | * \return 1 on success, 0 on fealure |
| 606 | | */ |
| 607 | | static int tty_fifo_pull (struct tty_fifo_s *fifo, int *c) |
| 608 | | { |
| 609 | | if (fifo->pt_read != fifo->pt_write) { |
| 610 | | *c = fifo->data [fifo->pt_read]; |
| 611 | | fifo->pt_read = (fifo->pt_read + 1)% sizeof(fifo->data); |
| 612 | | return 1; |
| 613 | | } |
| 614 | | return 0; |
| 615 | | } |
| 616 | | }}} |
| 617 | | }}} |
| | 620 | }}} |
| | 621 | }}} |
| | 622 | |