Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2136 - 1
/**
2
 * a simple Fifo
3
 * @file fifo.c
4
 * @author Pascal Schnurr
5
*/
6
 
7
//############################################################################
8
//# HISTORY  fifo.c
9
//#
10
//# 26.06.2014 OG
11
//# - add: fifo_isstr()
12
//#
13
//# 16.04.2013 Cebra
14
//# - chg: PROGMEM angepasst auf neue avr-libc und GCC, prog_char depreciated
15
//#
16
//############################################################################
17
 
18
 
19
#include <string.h>
20
#include "fifo.h"
21
//#if defined HWVERSION1_3W || defined HWVERSION3_9 || defined HWVERSION1_2W
22
 
23
 
24
//--------------------------------------------------------------
25
void fifo_init (fifo_t * fifo, uint8_t * buffer, uint16_t size)
26
{
27
    fifo->size = size;
28
    fifo->buffer = buffer;
29
    fifo->head = 0;
30
    fifo->count = 0;
31
}
32
//--------------------------------------------------------------
33
uint16_t fifo_getcount (const fifo_t * fifo)
34
{
35
 
36
      return  fifo->count;
37
}
38
//--------------------------------------------------------------
39
bool fifo_is_empty (const fifo_t * fifo)
40
{
41
    return (fifo->count == 0);
42
}
43
 
44
//--------------------------------------------------------------
45
bool fifo_is_full (const fifo_t * fifo)
46
{
47
    return (fifo->size - fifo->count == 0);
48
}
49
 
50
//--------------------------------------------------------------
51
bool fifo_read (fifo_t * fifo, char *data)
52
{
53
    if (fifo_is_empty (fifo))
54
        return false;
55
 
56
    uint8_t *head = fifo->buffer + fifo->head;
57
 
58
    *data = (char) * head;
59
 
60
    fifo->head = ( (fifo->head + 1) % fifo->size);
61
 
62
    fifo->count--;
63
 
64
    return true;
65
}
66
 
67
//--------------------------------------------------------------
68
bool fifo_write (fifo_t * fifo, const char data)
69
{
70
    if (fifo_is_full (fifo))
71
        return false;
72
 
73
    uint8_t *end = fifo->buffer + ( (fifo->head + fifo->count) % fifo->size);
74
 
75
    *end = (uint8_t) data;
76
 
77
    fifo->count++;
78
 
79
    return true;
80
}
81
 
82
//--------------------------------------------------------------
83
bool fifo_clear (fifo_t * fifo)
84
{
85
    fifo->count = 0;
86
    fifo->head = 0;
87
    return true;
88
}
89
 
90
//--------------------------------------------------------------
91
static bool fifo_cmp_pgm_at (fifo_t * fifo, const char * pgm, const uint16_t index)
92
{
93
    uint16_t i;
94
    uint8_t fifo_byte;
95
    uint8_t pgm_byte;
96
 
97
    for (i = 0; pgm_read_byte (pgm + i) != 0; i++)
98
    {
99
        if (fifo->count <= (i + index))
100
            return false;
101
 
102
        pgm_byte = pgm_read_byte (pgm + i);
103
 
104
        fifo_byte = * (fifo->buffer + ( (fifo->head + i + index) % fifo->size));
105
 
106
        if (fifo_byte != pgm_byte)
107
            return false;
108
    }
109
 
110
    // We found the string, lets move the pointer
111
    fifo->head = ( (fifo->head + i + index) % fifo->size);
112
 
113
    fifo->count -= (i + index);
114
 
115
    return true;
116
}
117
//--------------------------------------------------------------
118
bool fifo_search (fifo_t * fifo, const char * pgm)
119
{
120
        uint16_t i;
121
        uint8_t fifo_byte;
122
        uint8_t pgm_byte;
123
 
124
        for (i = 0; pgm_read_byte (pgm + i) != 0; i++)
125
        {
126
                if (fifo->count <= i)
127
                        return false;
128
 
129
                pgm_byte = pgm_read_byte (pgm + i);
130
 
131
                fifo_byte = * (fifo->buffer + ( (fifo->head + i) % fifo->size));
132
 
133
                if (fifo_byte != pgm_byte)
134
                        return false;
135
        }
136
 
137
//        // We found the string, lets move the pointer
138
//        fifo->head = ( (fifo->head + i + index) % fifo->size);
139
//
140
//        fifo->count -= (i + index);
141
 
142
        return true;
143
}
144
//--------------------------------------------------------------
145
/** \brief searches a string in the whole fifo
146
 * starts at the beginning and searches for the pgm string in the fifo,
147
 *
148
 * @param fifo pointer to your initialized fifo_t structure
149
 * @param pgm a prog_char with the search string
150
 * @return true if found, false otherwise
151
 */
152
 
153
bool fifo_cmp_pgm (fifo_t * fifo, const char * pgm)
154
{
155
    return fifo_cmp_pgm_at (fifo, pgm, 0);
156
}
157
//--------------------------------------------------------------
158
/** \brief searches a string in the whole fifo
159
* starts at the beginning and searches for the pgm string in the fifo,
160
* if they are found previous entrys and the string are removed from the fifo
161
* @param fifo pointer to your initialized fifo_t structure
162
* @param pgm a prog_char with the search string
163
* @return true if found, false otherwise
164
*/
165
 
166
bool fifo_strstr_pgm (fifo_t * fifo, const char * pgm)
167
{
168
    for (uint16_t i = 0; i < fifo->count; i++)
169
    {
170
        if (fifo_cmp_pgm_at (fifo, pgm, i))
171
            return true;
172
    }
173
 
174
    return false;
175
}
176
 
177
 
178
 
179
//--------------------------------------------------------------
180
// Idee: buffer nach str durchsuchen ohne die fifo Zeiger zu aendern
181
// ungetestet
182
//--------------------------------------------------------------
183
bool fifo_isstr( fifo_t * fifo, const char * str)
184
{
185
    /*
186
    void *memmem(const void *s1,
187
                 size_t len1,
188
                 const void *s2,
189
                 size_t len2);
190
    The memmem() function finds the start of the first occurrence of the substring s2 of length len2 in the memory area s1 of length len1.
191
    */
192
 
193
    return memmem( fifo->buffer + fifo->head, fifo->count, str, strlen(str) ) != NULL;
194
}
195
 
196
 
197
//#endif