Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1470 | - | 1 | /** |
2 | * a simple Fifo |
||
3 | * @file fifo.c |
||
4 | * @author Pascal Schnurr |
||
5 | */ |
||
6 | |||
7 | #include "fifo.h" |
||
8 | |||
9 | //-------------------------------------------------------------- |
||
10 | void fifo_init (fifo_t * fifo, uint8_t * buffer, uint16_t size) |
||
11 | { |
||
12 | fifo->size = size; |
||
13 | fifo->buffer = buffer; |
||
14 | fifo->head = 0; |
||
15 | fifo->count = 0; |
||
16 | } |
||
17 | |||
18 | //-------------------------------------------------------------- |
||
19 | bool fifo_is_empty (const fifo_t * fifo) |
||
20 | { |
||
21 | return (fifo->count == 0); |
||
22 | } |
||
23 | |||
24 | //-------------------------------------------------------------- |
||
25 | bool fifo_is_full (const fifo_t * fifo) |
||
26 | { |
||
27 | return (fifo->size - fifo->count == 0); |
||
28 | } |
||
29 | |||
30 | //-------------------------------------------------------------- |
||
31 | bool fifo_read (fifo_t * fifo, char *data) |
||
32 | { |
||
33 | if (fifo_is_empty (fifo)) |
||
34 | return false; |
||
35 | |||
36 | uint8_t *head = fifo->buffer + fifo->head; |
||
37 | |||
38 | *data = (char) * head; |
||
39 | |||
40 | fifo->head = ( (fifo->head + 1) % fifo->size); |
||
41 | |||
42 | fifo->count--; |
||
43 | |||
44 | return true; |
||
45 | } |
||
46 | |||
47 | //-------------------------------------------------------------- |
||
48 | bool fifo_write (fifo_t * fifo, const char data) |
||
49 | { |
||
50 | if (fifo_is_full (fifo)) |
||
51 | return false; |
||
52 | |||
53 | uint8_t *end = fifo->buffer + ( (fifo->head + fifo->count) % fifo->size); |
||
54 | |||
55 | *end = (uint8_t) data; |
||
56 | |||
57 | fifo->count++; |
||
58 | |||
59 | return true; |
||
60 | } |
||
61 | |||
62 | //-------------------------------------------------------------- |
||
63 | bool fifo_clear (fifo_t * fifo) |
||
64 | { |
||
65 | fifo->count = 0; |
||
66 | fifo->head = 0; |
||
67 | return true; |
||
68 | } |
||
69 | |||
70 | //-------------------------------------------------------------- |
||
71 | static bool fifo_cmp_pgm_at (fifo_t * fifo, const prog_char * pgm, const uint16_t index) |
||
72 | { |
||
73 | uint16_t i; |
||
74 | uint8_t fifo_byte; |
||
75 | uint8_t pgm_byte; |
||
76 | |||
77 | for (i = 0; pgm_read_byte (pgm + i) != 0; i++) |
||
78 | { |
||
79 | if (fifo->count <= (i + index)) |
||
80 | return false; |
||
81 | |||
82 | pgm_byte = pgm_read_byte (pgm + i); |
||
83 | |||
84 | fifo_byte = * (fifo->buffer + ( (fifo->head + i + index) % fifo->size)); |
||
85 | |||
86 | if (fifo_byte != pgm_byte) |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | // We found the string, lets move the pointer |
||
91 | fifo->head = ( (fifo->head + i + index) % fifo->size); |
||
92 | |||
93 | fifo->count -= (i + index); |
||
94 | |||
95 | return true; |
||
96 | } |
||
97 | |||
98 | //-------------------------------------------------------------- |
||
99 | bool fifo_cmp_pgm (fifo_t * fifo, const prog_char * pgm) |
||
100 | { |
||
101 | return fifo_cmp_pgm_at (fifo, pgm, 0); |
||
102 | } |
||
103 | |||
104 | //-------------------------------------------------------------- |
||
105 | bool fifo_strstr_pgm (fifo_t * fifo, const prog_char * pgm) |
||
106 | { |
||
107 | for (uint16_t i = 0; i < fifo->count; i++) |
||
108 | { |
||
109 | if (fifo_cmp_pgm_at (fifo, pgm, i)) |
||
110 | return true; |
||
111 | } |
||
112 | |||
113 | return false; |
||
114 | } |