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