lab 7

Dependencies:   SDFileSystem mbed

Committer:
jedh
Date:
Sat Dec 10 21:08:30 2016 +0000
Revision:
0:f6d3b930f382
jgk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jedh 0:f6d3b930f382 1 #ifndef SIMPLE_FIFO_H
jedh 0:f6d3b930f382 2 #define SIMPLE_FIFO_H
jedh 0:f6d3b930f382 3
jedh 0:f6d3b930f382 4 /*
jedh 0:f6d3b930f382 5 SIMPLE_FIFO was designed as an example of a FIFO construct using "char" data.
jedh 0:f6d3b930f382 6 The defined type structure called SIMPLE_FIFO uses an array of type "char" and
jedh 0:f6d3b930f382 7 two integer pointers "write_pointer" and "read_pointer" to form a circuular buffer.
jedh 0:f6d3b930f382 8 The number of elements in the circular buffer is defined by SIMPLE_FIFO_SIZE.
jedh 0:f6d3b930f382 9
jedh 0:f6d3b930f382 10 The following functions are defined for the SIMPLE_FIFO type:
jedh 0:f6d3b930f382 11 void simple_fifo_init(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 12 int simple_fifo_writeable(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 13 int simple_fifo_readable(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 14 int simple_fifo_write(SIMPLE_FIFO *f, char c)
jedh 0:f6d3b930f382 15 int simple_fifo_read(SIMPLE_FIFO *f, char *c)
jedh 0:f6d3b930f382 16 char simple_fifo_putc(SIMPLE_FIFO *f, char c)
jedh 0:f6d3b930f382 17 char simple_fifo_getc(SIMPLE_FIFO *f, char *c)
jedh 0:f6d3b930f382 18 void simple_fifo_puts(SIMPLE_FIFO *f, char str[])
jedh 0:f6d3b930f382 19 void simple_fifo_gets(SIMPLE_FIFO *f, char str[], int count)
jedh 0:f6d3b930f382 20
jedh 0:f6d3b930f382 21 The function simple_fifo_init initializes both pointers to 0 and initializes all data in the fifo to 0.
jedh 0:f6d3b930f382 22 The function simple_fifo_writeable returns true if space is available in the fifo and returns false if the fifo is full.
jedh 0:f6d3b930f382 23 The function simple_fifo_readable returns true if data is available in the fifo and returns false if the fifo is empty.
jedh 0:f6d3b930f382 24 The function simple_fifo_write returns true if if data was successfully written to the fifo and returns false if the fifo is full.
jedh 0:f6d3b930f382 25 The function simple_fifo_read returns true if if data was successfully read from the fifo and returns false if the fifo is empty.
jedh 0:f6d3b930f382 26 The function simple_fifo_putc blocks until the fifo is writeable then writes a single character to the fifo. The return value is the character written.
jedh 0:f6d3b930f382 27 The function simple_fifo_getc blocks until the fifo is readable then reads a single character from the fifo. The return value is the character read.
jedh 0:f6d3b930f382 28 The function simple_fifo_puts blocks until the fifo is writeable then writes a single character to the fifo. This is repeated until all characters in the null-terminated string out written to the fifo.
jedh 0:f6d3b930f382 29 The function simple_fifo_getc blocks until the fifo is readable then reads a single character from the fifo. This is repeated until the carriage return character is encountered. The resulting string is returned as null-terminated without the carriage return caracter included.
jedh 0:f6d3b930f382 30 */
jedh 0:f6d3b930f382 31
jedh 0:f6d3b930f382 32 #ifndef TRUE
jedh 0:f6d3b930f382 33 #define TRUE ( 1 == 1 )
jedh 0:f6d3b930f382 34 #endif
jedh 0:f6d3b930f382 35
jedh 0:f6d3b930f382 36 #ifndef FALSE
jedh 0:f6d3b930f382 37 #define FALSE ( 1 == 0 )
jedh 0:f6d3b930f382 38 #endif
jedh 0:f6d3b930f382 39
jedh 0:f6d3b930f382 40 #ifndef SIMPLE_FIFO_SIZE
jedh 0:f6d3b930f382 41 #define SIMPLE_FIFO_SIZE 16
jedh 0:f6d3b930f382 42 #endif
jedh 0:f6d3b930f382 43
jedh 0:f6d3b930f382 44 #ifndef SIMPLE_FIFO
jedh 0:f6d3b930f382 45 typedef struct simple_fifo
jedh 0:f6d3b930f382 46 {
jedh 0:f6d3b930f382 47 char buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 48 int write_pointer;
jedh 0:f6d3b930f382 49 int read_pointer;
jedh 0:f6d3b930f382 50 } SIMPLE_FIFO;
jedh 0:f6d3b930f382 51 #endif
jedh 0:f6d3b930f382 52
jedh 0:f6d3b930f382 53 void simple_fifo_init(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 54 {
jedh 0:f6d3b930f382 55 f->write_pointer=0;
jedh 0:f6d3b930f382 56 f->read_pointer=0;
jedh 0:f6d3b930f382 57 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 58 {
jedh 0:f6d3b930f382 59 f->buffer[i]=0;
jedh 0:f6d3b930f382 60 }
jedh 0:f6d3b930f382 61 }
jedh 0:f6d3b930f382 62
jedh 0:f6d3b930f382 63 int simple_fifo_writeable(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 64 {
jedh 0:f6d3b930f382 65 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 66 {
jedh 0:f6d3b930f382 67 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 68 }
jedh 0:f6d3b930f382 69 return(TRUE);
jedh 0:f6d3b930f382 70 }
jedh 0:f6d3b930f382 71
jedh 0:f6d3b930f382 72 int simple_fifo_readable(SIMPLE_FIFO *f)
jedh 0:f6d3b930f382 73 {
jedh 0:f6d3b930f382 74 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 75 {
jedh 0:f6d3b930f382 76 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 77 }
jedh 0:f6d3b930f382 78 return(TRUE);
jedh 0:f6d3b930f382 79 }
jedh 0:f6d3b930f382 80
jedh 0:f6d3b930f382 81 int simple_fifo_write(SIMPLE_FIFO *f, char c)
jedh 0:f6d3b930f382 82 {
jedh 0:f6d3b930f382 83 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 84 {
jedh 0:f6d3b930f382 85 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 86 }
jedh 0:f6d3b930f382 87 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 88 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 89 return(TRUE);
jedh 0:f6d3b930f382 90 }
jedh 0:f6d3b930f382 91
jedh 0:f6d3b930f382 92 int simple_fifo_read(SIMPLE_FIFO *f, char *c)
jedh 0:f6d3b930f382 93 {
jedh 0:f6d3b930f382 94 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 95 {
jedh 0:f6d3b930f382 96 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 97 }
jedh 0:f6d3b930f382 98 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 99 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 100 return(TRUE);
jedh 0:f6d3b930f382 101 }
jedh 0:f6d3b930f382 102
jedh 0:f6d3b930f382 103 char simple_fifo_putc(SIMPLE_FIFO *f, char c)
jedh 0:f6d3b930f382 104 {
jedh 0:f6d3b930f382 105 while(!simple_fifo_writeable(f)) {}
jedh 0:f6d3b930f382 106 simple_fifo_write(f, c);
jedh 0:f6d3b930f382 107 return(c);
jedh 0:f6d3b930f382 108 }
jedh 0:f6d3b930f382 109
jedh 0:f6d3b930f382 110 char simple_fifo_getc(SIMPLE_FIFO *f, char *c)
jedh 0:f6d3b930f382 111 {
jedh 0:f6d3b930f382 112 while(!simple_fifo_readable(f)) {}
jedh 0:f6d3b930f382 113 simple_fifo_read(f, c);
jedh 0:f6d3b930f382 114 return(*c);
jedh 0:f6d3b930f382 115
jedh 0:f6d3b930f382 116 }
jedh 0:f6d3b930f382 117
jedh 0:f6d3b930f382 118 void simple_fifo_puts(SIMPLE_FIFO *f, char str[])
jedh 0:f6d3b930f382 119 {
jedh 0:f6d3b930f382 120 for(int i=0; str[i]!='\0';i++)
jedh 0:f6d3b930f382 121 {
jedh 0:f6d3b930f382 122 simple_fifo_putc(f, str[i]);
jedh 0:f6d3b930f382 123 }
jedh 0:f6d3b930f382 124 }
jedh 0:f6d3b930f382 125
jedh 0:f6d3b930f382 126 void simple_fifo_gets(SIMPLE_FIFO *f, char str[], int count)
jedh 0:f6d3b930f382 127 {
jedh 0:f6d3b930f382 128 for(int i=0; i<count; i++)
jedh 0:f6d3b930f382 129 {
jedh 0:f6d3b930f382 130 simple_fifo_getc(f, &str[i]);
jedh 0:f6d3b930f382 131 if(str[i] == '\r' || str[i] == '\n')
jedh 0:f6d3b930f382 132 {
jedh 0:f6d3b930f382 133 str[i]='\0';
jedh 0:f6d3b930f382 134 break;
jedh 0:f6d3b930f382 135 }
jedh 0:f6d3b930f382 136 }
jedh 0:f6d3b930f382 137 str[count-1]='\0';
jedh 0:f6d3b930f382 138 }
jedh 0:f6d3b930f382 139
jedh 0:f6d3b930f382 140 /*
jedh 0:f6d3b930f382 141 In addition to the SIMPLE_FIFO type, a number of similar fifo types are constructed for various data types.
jedh 0:f6d3b930f382 142 The following fifo types are defined:
jedh 0:f6d3b930f382 143 FIFO_CHAR //for character data, similar to SIMPLE_FIFO above
jedh 0:f6d3b930f382 144 FIFO_INT //for default integer type
jedh 0:f6d3b930f382 145 FIFO_U8 //for uint8_t data
jedh 0:f6d3b930f382 146 FIFO_U16 //for uint16_t data
jedh 0:f6d3b930f382 147 FIFO_U32 //for uint32_t data
jedh 0:f6d3b930f382 148 FIFO_I8 //for int8_t data
jedh 0:f6d3b930f382 149 FIFO_I16 //for int16_t data
jedh 0:f6d3b930f382 150 FIFO_I32 //for int32_t data
jedh 0:f6d3b930f382 151 FIFO_FLOAT //for float data
jedh 0:f6d3b930f382 152 FIFO_DOUBLE //for double data
jedh 0:f6d3b930f382 153
jedh 0:f6d3b930f382 154 The following functions are defined for the FIFO_?? types:
jedh 0:f6d3b930f382 155 void fifo_??_init(FIFO_?? *f)
jedh 0:f6d3b930f382 156 int fifo_??_writeable(FIFO_?? *f)
jedh 0:f6d3b930f382 157 int fifo_??_readable(FIFO_?? *f)
jedh 0:f6d3b930f382 158 int fifo_??_write(FIFO_?? *f, char c)
jedh 0:f6d3b930f382 159 int fifo_??_read(FIFO_?? *f, char *c)
jedh 0:f6d3b930f382 160
jedh 0:f6d3b930f382 161 The function fifo_??_init initializes both pointers to 0 and initializes all data in the fifo to 0.
jedh 0:f6d3b930f382 162 The function fifo_??_writeable returns true if space is available in the fifo and returns false if the fifo is full.
jedh 0:f6d3b930f382 163 The function fifo_??_readable returns true if data is available in the fifo and returns false if the fifo is empty.
jedh 0:f6d3b930f382 164 The function fifo_??_write returns true if if data was successfully written to the fifo and returns false if the fifo is full.
jedh 0:f6d3b930f382 165 The function sfifo_??_read returns true if if data was successfully read from the fifo and returns false if the fifo is empty.
jedh 0:f6d3b930f382 166 */
jedh 0:f6d3b930f382 167
jedh 0:f6d3b930f382 168 #include <stdint.h>
jedh 0:f6d3b930f382 169
jedh 0:f6d3b930f382 170
jedh 0:f6d3b930f382 171 #ifndef FIFO_CHAR
jedh 0:f6d3b930f382 172 typedef struct fifo_char
jedh 0:f6d3b930f382 173 {
jedh 0:f6d3b930f382 174 char buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 175 int write_pointer;
jedh 0:f6d3b930f382 176 int read_pointer;
jedh 0:f6d3b930f382 177 } FIFO_CHAR;
jedh 0:f6d3b930f382 178 #endif
jedh 0:f6d3b930f382 179
jedh 0:f6d3b930f382 180 void fifo_char_init(FIFO_CHAR *f)
jedh 0:f6d3b930f382 181 {
jedh 0:f6d3b930f382 182 f->write_pointer=0;
jedh 0:f6d3b930f382 183 f->read_pointer=0;
jedh 0:f6d3b930f382 184 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 185 {
jedh 0:f6d3b930f382 186 f->buffer[i]=0;
jedh 0:f6d3b930f382 187 }
jedh 0:f6d3b930f382 188 }
jedh 0:f6d3b930f382 189
jedh 0:f6d3b930f382 190 int fifo_char_writeable(FIFO_CHAR *f)
jedh 0:f6d3b930f382 191 {
jedh 0:f6d3b930f382 192 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 193 {
jedh 0:f6d3b930f382 194 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 195 }
jedh 0:f6d3b930f382 196 return(TRUE);
jedh 0:f6d3b930f382 197 }
jedh 0:f6d3b930f382 198
jedh 0:f6d3b930f382 199 int fifo_char_readable(FIFO_CHAR *f)
jedh 0:f6d3b930f382 200 {
jedh 0:f6d3b930f382 201 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 202 {
jedh 0:f6d3b930f382 203 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 204 }
jedh 0:f6d3b930f382 205 return(TRUE);
jedh 0:f6d3b930f382 206 }
jedh 0:f6d3b930f382 207
jedh 0:f6d3b930f382 208 int fifo_char_write(FIFO_CHAR *f, char c)
jedh 0:f6d3b930f382 209 {
jedh 0:f6d3b930f382 210 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 211 {
jedh 0:f6d3b930f382 212 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 213 }
jedh 0:f6d3b930f382 214 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 215 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 216 return(TRUE);
jedh 0:f6d3b930f382 217 }
jedh 0:f6d3b930f382 218
jedh 0:f6d3b930f382 219 int fifo_char_read(FIFO_CHAR *f, char *c)
jedh 0:f6d3b930f382 220 {
jedh 0:f6d3b930f382 221 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 222 {
jedh 0:f6d3b930f382 223 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 224 }
jedh 0:f6d3b930f382 225 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 226 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 227 return(TRUE);
jedh 0:f6d3b930f382 228 }
jedh 0:f6d3b930f382 229
jedh 0:f6d3b930f382 230 char fifo_char_putc(FIFO_CHAR *f, char c)
jedh 0:f6d3b930f382 231 {
jedh 0:f6d3b930f382 232 while(!fifo_char_writeable(f)) {}
jedh 0:f6d3b930f382 233 fifo_char_write(f, c);
jedh 0:f6d3b930f382 234 return(c);
jedh 0:f6d3b930f382 235 }
jedh 0:f6d3b930f382 236
jedh 0:f6d3b930f382 237 char fifo_char_getc(FIFO_CHAR *f, char *c)
jedh 0:f6d3b930f382 238 {
jedh 0:f6d3b930f382 239 while(!fifo_char_readable(f)) {}
jedh 0:f6d3b930f382 240 fifo_char_read(f, c);
jedh 0:f6d3b930f382 241 return(*c);
jedh 0:f6d3b930f382 242
jedh 0:f6d3b930f382 243 }
jedh 0:f6d3b930f382 244
jedh 0:f6d3b930f382 245 void fifo_char_puts(FIFO_CHAR *f, char str[])
jedh 0:f6d3b930f382 246 {
jedh 0:f6d3b930f382 247 for(int i=0; str[i]!='\0';i++)
jedh 0:f6d3b930f382 248 {
jedh 0:f6d3b930f382 249 fifo_char_putc(f, str[i]);
jedh 0:f6d3b930f382 250 }
jedh 0:f6d3b930f382 251 }
jedh 0:f6d3b930f382 252
jedh 0:f6d3b930f382 253 void fifo_char_gets(FIFO_CHAR *f, char str[], int count)
jedh 0:f6d3b930f382 254 {
jedh 0:f6d3b930f382 255 for(int i=0; i<count; i++)
jedh 0:f6d3b930f382 256 {
jedh 0:f6d3b930f382 257 fifo_char_getc(f, &str[i]);
jedh 0:f6d3b930f382 258 if(str[i] == '\r' || str[i] == '\n')
jedh 0:f6d3b930f382 259 {
jedh 0:f6d3b930f382 260 str[i]='\0';
jedh 0:f6d3b930f382 261 break;
jedh 0:f6d3b930f382 262 }
jedh 0:f6d3b930f382 263 }
jedh 0:f6d3b930f382 264 str[count-1]='\0';
jedh 0:f6d3b930f382 265 }
jedh 0:f6d3b930f382 266
jedh 0:f6d3b930f382 267
jedh 0:f6d3b930f382 268
jedh 0:f6d3b930f382 269 #ifndef FIFO_INT
jedh 0:f6d3b930f382 270 typedef struct fifo_int
jedh 0:f6d3b930f382 271 {
jedh 0:f6d3b930f382 272 int buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 273 int write_pointer;
jedh 0:f6d3b930f382 274 int read_pointer;
jedh 0:f6d3b930f382 275 } FIFO_INT;
jedh 0:f6d3b930f382 276 #endif
jedh 0:f6d3b930f382 277
jedh 0:f6d3b930f382 278 void fifo_int_init(FIFO_INT *f)
jedh 0:f6d3b930f382 279 {
jedh 0:f6d3b930f382 280 f->write_pointer=0;
jedh 0:f6d3b930f382 281 f->read_pointer=0;
jedh 0:f6d3b930f382 282 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 283 {
jedh 0:f6d3b930f382 284 f->buffer[i]=0;
jedh 0:f6d3b930f382 285 }
jedh 0:f6d3b930f382 286 }
jedh 0:f6d3b930f382 287
jedh 0:f6d3b930f382 288 int fifo_int_writeable(FIFO_INT *f)
jedh 0:f6d3b930f382 289 {
jedh 0:f6d3b930f382 290 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 291 {
jedh 0:f6d3b930f382 292 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 293 }
jedh 0:f6d3b930f382 294 return(TRUE);
jedh 0:f6d3b930f382 295 }
jedh 0:f6d3b930f382 296
jedh 0:f6d3b930f382 297 int fifo_int_readable(FIFO_INT *f)
jedh 0:f6d3b930f382 298 {
jedh 0:f6d3b930f382 299 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 300 {
jedh 0:f6d3b930f382 301 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 302 }
jedh 0:f6d3b930f382 303 return(TRUE);
jedh 0:f6d3b930f382 304 }
jedh 0:f6d3b930f382 305
jedh 0:f6d3b930f382 306 int fifo_int_write(FIFO_INT *f, int c)
jedh 0:f6d3b930f382 307 {
jedh 0:f6d3b930f382 308 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 309 {
jedh 0:f6d3b930f382 310 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 311 }
jedh 0:f6d3b930f382 312 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 313 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 314 return(TRUE);
jedh 0:f6d3b930f382 315 }
jedh 0:f6d3b930f382 316
jedh 0:f6d3b930f382 317 int fifo_int_read(FIFO_INT *f, int *c)
jedh 0:f6d3b930f382 318 {
jedh 0:f6d3b930f382 319 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 320 {
jedh 0:f6d3b930f382 321 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 322 }
jedh 0:f6d3b930f382 323 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 324 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 325 return(TRUE);
jedh 0:f6d3b930f382 326 }
jedh 0:f6d3b930f382 327
jedh 0:f6d3b930f382 328 #ifndef FIFO_U8
jedh 0:f6d3b930f382 329 typedef struct fifo_u8
jedh 0:f6d3b930f382 330 {
jedh 0:f6d3b930f382 331 uint8_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 332 int write_pointer;
jedh 0:f6d3b930f382 333 int read_pointer;
jedh 0:f6d3b930f382 334 } FIFO_U8;
jedh 0:f6d3b930f382 335 #endif
jedh 0:f6d3b930f382 336
jedh 0:f6d3b930f382 337 void fifo_u8_init(FIFO_U8 *f)
jedh 0:f6d3b930f382 338 {
jedh 0:f6d3b930f382 339 f->write_pointer=0;
jedh 0:f6d3b930f382 340 f->read_pointer=0;
jedh 0:f6d3b930f382 341 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 342 {
jedh 0:f6d3b930f382 343 f->buffer[i]=0;
jedh 0:f6d3b930f382 344 }
jedh 0:f6d3b930f382 345 }
jedh 0:f6d3b930f382 346
jedh 0:f6d3b930f382 347 int fifo_u8_writeable(FIFO_U8 *f)
jedh 0:f6d3b930f382 348 {
jedh 0:f6d3b930f382 349 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 350 {
jedh 0:f6d3b930f382 351 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 352 }
jedh 0:f6d3b930f382 353 return(TRUE);
jedh 0:f6d3b930f382 354 }
jedh 0:f6d3b930f382 355
jedh 0:f6d3b930f382 356 int fifo_u8_readable(FIFO_U8 *f)
jedh 0:f6d3b930f382 357 {
jedh 0:f6d3b930f382 358 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 359 {
jedh 0:f6d3b930f382 360 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 361 }
jedh 0:f6d3b930f382 362 return(TRUE);
jedh 0:f6d3b930f382 363 }
jedh 0:f6d3b930f382 364
jedh 0:f6d3b930f382 365 int fifo_u8_write(FIFO_U8 *f, uint8_t c)
jedh 0:f6d3b930f382 366 {
jedh 0:f6d3b930f382 367 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 368 {
jedh 0:f6d3b930f382 369 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 370 }
jedh 0:f6d3b930f382 371 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 372 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 373 return(TRUE);
jedh 0:f6d3b930f382 374 }
jedh 0:f6d3b930f382 375
jedh 0:f6d3b930f382 376 int fifo_u8_read(FIFO_U8 *f, uint8_t *c)
jedh 0:f6d3b930f382 377 {
jedh 0:f6d3b930f382 378 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 379 {
jedh 0:f6d3b930f382 380 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 381 }
jedh 0:f6d3b930f382 382 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 383 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 384 return(TRUE);
jedh 0:f6d3b930f382 385 }
jedh 0:f6d3b930f382 386
jedh 0:f6d3b930f382 387 #ifndef FIFO_U16
jedh 0:f6d3b930f382 388 typedef struct fifo_u16
jedh 0:f6d3b930f382 389 {
jedh 0:f6d3b930f382 390 uint16_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 391 int write_pointer;
jedh 0:f6d3b930f382 392 int read_pointer;
jedh 0:f6d3b930f382 393 } FIFO_U16;
jedh 0:f6d3b930f382 394 #endif
jedh 0:f6d3b930f382 395
jedh 0:f6d3b930f382 396 void fifo_u16_init(FIFO_U16 *f)
jedh 0:f6d3b930f382 397 {
jedh 0:f6d3b930f382 398 f->write_pointer=0;
jedh 0:f6d3b930f382 399 f->read_pointer=0;
jedh 0:f6d3b930f382 400 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 401 {
jedh 0:f6d3b930f382 402 f->buffer[i]=0;
jedh 0:f6d3b930f382 403 }
jedh 0:f6d3b930f382 404 }
jedh 0:f6d3b930f382 405
jedh 0:f6d3b930f382 406 int fifo_u16_writeable(FIFO_U16 *f)
jedh 0:f6d3b930f382 407 {
jedh 0:f6d3b930f382 408 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 409 {
jedh 0:f6d3b930f382 410 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 411 }
jedh 0:f6d3b930f382 412 return(TRUE);
jedh 0:f6d3b930f382 413 }
jedh 0:f6d3b930f382 414
jedh 0:f6d3b930f382 415 int fifo_u16_readable(FIFO_U16 *f)
jedh 0:f6d3b930f382 416 {
jedh 0:f6d3b930f382 417 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 418 {
jedh 0:f6d3b930f382 419 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 420 }
jedh 0:f6d3b930f382 421 return(TRUE);
jedh 0:f6d3b930f382 422 }
jedh 0:f6d3b930f382 423
jedh 0:f6d3b930f382 424 int fifo_u16_write(FIFO_U16 *f, uint16_t c)
jedh 0:f6d3b930f382 425 {
jedh 0:f6d3b930f382 426 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 427 {
jedh 0:f6d3b930f382 428 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 429 }
jedh 0:f6d3b930f382 430 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 431 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 432 return(TRUE);
jedh 0:f6d3b930f382 433 }
jedh 0:f6d3b930f382 434
jedh 0:f6d3b930f382 435 int fifo_u16_read(FIFO_U16 *f, uint16_t *c)
jedh 0:f6d3b930f382 436 {
jedh 0:f6d3b930f382 437 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 438 {
jedh 0:f6d3b930f382 439 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 440 }
jedh 0:f6d3b930f382 441 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 442 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 443 return(TRUE);
jedh 0:f6d3b930f382 444 }
jedh 0:f6d3b930f382 445
jedh 0:f6d3b930f382 446 #ifndef FIFO_U32
jedh 0:f6d3b930f382 447 typedef struct fifo_u32
jedh 0:f6d3b930f382 448 {
jedh 0:f6d3b930f382 449 uint32_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 450 int write_pointer;
jedh 0:f6d3b930f382 451 int read_pointer;
jedh 0:f6d3b930f382 452 } FIFO_U32;
jedh 0:f6d3b930f382 453 #endif
jedh 0:f6d3b930f382 454
jedh 0:f6d3b930f382 455 void fifo_u32_init(FIFO_U32 *f)
jedh 0:f6d3b930f382 456 {
jedh 0:f6d3b930f382 457 f->write_pointer=0;
jedh 0:f6d3b930f382 458 f->read_pointer=0;
jedh 0:f6d3b930f382 459 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 460 {
jedh 0:f6d3b930f382 461 f->buffer[i]=0;
jedh 0:f6d3b930f382 462 }
jedh 0:f6d3b930f382 463 }
jedh 0:f6d3b930f382 464
jedh 0:f6d3b930f382 465 int fifo_u32_writeable(FIFO_U32 *f)
jedh 0:f6d3b930f382 466 {
jedh 0:f6d3b930f382 467 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 468 {
jedh 0:f6d3b930f382 469 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 470 }
jedh 0:f6d3b930f382 471 return(TRUE);
jedh 0:f6d3b930f382 472 }
jedh 0:f6d3b930f382 473
jedh 0:f6d3b930f382 474 int fifo_u32_readable(FIFO_U32 *f)
jedh 0:f6d3b930f382 475 {
jedh 0:f6d3b930f382 476 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 477 {
jedh 0:f6d3b930f382 478 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 479 }
jedh 0:f6d3b930f382 480 return(TRUE);
jedh 0:f6d3b930f382 481 }
jedh 0:f6d3b930f382 482
jedh 0:f6d3b930f382 483 int fifo_u32_write(FIFO_U32 *f, uint32_t c)
jedh 0:f6d3b930f382 484 {
jedh 0:f6d3b930f382 485 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 486 {
jedh 0:f6d3b930f382 487 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 488 }
jedh 0:f6d3b930f382 489 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 490 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 491 return(TRUE);
jedh 0:f6d3b930f382 492 }
jedh 0:f6d3b930f382 493
jedh 0:f6d3b930f382 494 int fifo_u32_read(FIFO_U32 *f, uint32_t *c)
jedh 0:f6d3b930f382 495 {
jedh 0:f6d3b930f382 496 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 497 {
jedh 0:f6d3b930f382 498 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 499 }
jedh 0:f6d3b930f382 500 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 501 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 502 return(TRUE);
jedh 0:f6d3b930f382 503 }
jedh 0:f6d3b930f382 504
jedh 0:f6d3b930f382 505 #ifndef FIFO_I8
jedh 0:f6d3b930f382 506 typedef struct fifo_i8
jedh 0:f6d3b930f382 507 {
jedh 0:f6d3b930f382 508 int8_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 509 int write_pointer;
jedh 0:f6d3b930f382 510 int read_pointer;
jedh 0:f6d3b930f382 511 } FIFO_I8;
jedh 0:f6d3b930f382 512 #endif
jedh 0:f6d3b930f382 513
jedh 0:f6d3b930f382 514 void fifo_i8_init(FIFO_I8 *f)
jedh 0:f6d3b930f382 515 {
jedh 0:f6d3b930f382 516 f->write_pointer=0;
jedh 0:f6d3b930f382 517 f->read_pointer=0;
jedh 0:f6d3b930f382 518 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 519 {
jedh 0:f6d3b930f382 520 f->buffer[i]=0;
jedh 0:f6d3b930f382 521 }
jedh 0:f6d3b930f382 522 }
jedh 0:f6d3b930f382 523
jedh 0:f6d3b930f382 524 int fifo_i8_writeable(FIFO_I8 *f)
jedh 0:f6d3b930f382 525 {
jedh 0:f6d3b930f382 526 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 527 {
jedh 0:f6d3b930f382 528 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 529 }
jedh 0:f6d3b930f382 530 return(TRUE);
jedh 0:f6d3b930f382 531 }
jedh 0:f6d3b930f382 532
jedh 0:f6d3b930f382 533 int fifo_i8_readable(FIFO_I8 *f)
jedh 0:f6d3b930f382 534 {
jedh 0:f6d3b930f382 535 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 536 {
jedh 0:f6d3b930f382 537 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 538 }
jedh 0:f6d3b930f382 539 return(TRUE);
jedh 0:f6d3b930f382 540 }
jedh 0:f6d3b930f382 541
jedh 0:f6d3b930f382 542 int fifo_i8_write(FIFO_I8 *f, int8_t c)
jedh 0:f6d3b930f382 543 {
jedh 0:f6d3b930f382 544 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 545 {
jedh 0:f6d3b930f382 546 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 547 }
jedh 0:f6d3b930f382 548 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 549 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 550 return(TRUE);
jedh 0:f6d3b930f382 551 }
jedh 0:f6d3b930f382 552
jedh 0:f6d3b930f382 553 int fifo_i8_read(FIFO_I8 *f, int8_t *c)
jedh 0:f6d3b930f382 554 {
jedh 0:f6d3b930f382 555 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 556 {
jedh 0:f6d3b930f382 557 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 558 }
jedh 0:f6d3b930f382 559 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 560 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 561 return(TRUE);
jedh 0:f6d3b930f382 562 }
jedh 0:f6d3b930f382 563
jedh 0:f6d3b930f382 564
jedh 0:f6d3b930f382 565 #ifndef FIFO_I16
jedh 0:f6d3b930f382 566 typedef struct fifo_i16
jedh 0:f6d3b930f382 567 {
jedh 0:f6d3b930f382 568 int16_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 569 int write_pointer;
jedh 0:f6d3b930f382 570 int read_pointer;
jedh 0:f6d3b930f382 571 } FIFO_I16;
jedh 0:f6d3b930f382 572 #endif
jedh 0:f6d3b930f382 573
jedh 0:f6d3b930f382 574 void fifo_i16_init(FIFO_I16 *f)
jedh 0:f6d3b930f382 575 {
jedh 0:f6d3b930f382 576 f->write_pointer=0;
jedh 0:f6d3b930f382 577 f->read_pointer=0;
jedh 0:f6d3b930f382 578 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 579 {
jedh 0:f6d3b930f382 580 f->buffer[i]=0;
jedh 0:f6d3b930f382 581 }
jedh 0:f6d3b930f382 582 }
jedh 0:f6d3b930f382 583
jedh 0:f6d3b930f382 584 int fifo_i16_writeable(FIFO_I16 *f)
jedh 0:f6d3b930f382 585 {
jedh 0:f6d3b930f382 586 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 587 {
jedh 0:f6d3b930f382 588 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 589 }
jedh 0:f6d3b930f382 590 return(TRUE);
jedh 0:f6d3b930f382 591 }
jedh 0:f6d3b930f382 592
jedh 0:f6d3b930f382 593 int fifo_i16_readable(FIFO_I16 *f)
jedh 0:f6d3b930f382 594 {
jedh 0:f6d3b930f382 595 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 596 {
jedh 0:f6d3b930f382 597 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 598 }
jedh 0:f6d3b930f382 599 return(TRUE);
jedh 0:f6d3b930f382 600 }
jedh 0:f6d3b930f382 601
jedh 0:f6d3b930f382 602 int fifo_i16_write(FIFO_I16 *f, int16_t c)
jedh 0:f6d3b930f382 603 {
jedh 0:f6d3b930f382 604 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 605 {
jedh 0:f6d3b930f382 606 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 607 }
jedh 0:f6d3b930f382 608 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 609 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 610 return(TRUE);
jedh 0:f6d3b930f382 611 }
jedh 0:f6d3b930f382 612
jedh 0:f6d3b930f382 613 int fifo_i16_read(FIFO_I16 *f, int16_t *c)
jedh 0:f6d3b930f382 614 {
jedh 0:f6d3b930f382 615 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 616 {
jedh 0:f6d3b930f382 617 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 618 }
jedh 0:f6d3b930f382 619 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 620 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 621 return(TRUE);
jedh 0:f6d3b930f382 622 }
jedh 0:f6d3b930f382 623
jedh 0:f6d3b930f382 624 #ifndef FIFO_I32
jedh 0:f6d3b930f382 625 typedef struct fifo_i32
jedh 0:f6d3b930f382 626 {
jedh 0:f6d3b930f382 627 int32_t buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 628 int write_pointer;
jedh 0:f6d3b930f382 629 int read_pointer;
jedh 0:f6d3b930f382 630 } FIFO_I32;
jedh 0:f6d3b930f382 631 #endif
jedh 0:f6d3b930f382 632
jedh 0:f6d3b930f382 633 void fifo_i32_init(FIFO_I32 *f)
jedh 0:f6d3b930f382 634 {
jedh 0:f6d3b930f382 635 f->write_pointer=0;
jedh 0:f6d3b930f382 636 f->read_pointer=0;
jedh 0:f6d3b930f382 637 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 638 {
jedh 0:f6d3b930f382 639 f->buffer[i]=0;
jedh 0:f6d3b930f382 640 }
jedh 0:f6d3b930f382 641 }
jedh 0:f6d3b930f382 642
jedh 0:f6d3b930f382 643 int fifo_i32_writeable(FIFO_I32 *f)
jedh 0:f6d3b930f382 644 {
jedh 0:f6d3b930f382 645 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 646 {
jedh 0:f6d3b930f382 647 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 648 }
jedh 0:f6d3b930f382 649 return(TRUE);
jedh 0:f6d3b930f382 650 }
jedh 0:f6d3b930f382 651
jedh 0:f6d3b930f382 652 int fifo_i32_readable(FIFO_I32 *f)
jedh 0:f6d3b930f382 653 {
jedh 0:f6d3b930f382 654 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 655 {
jedh 0:f6d3b930f382 656 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 657 }
jedh 0:f6d3b930f382 658 return(TRUE);
jedh 0:f6d3b930f382 659 }
jedh 0:f6d3b930f382 660
jedh 0:f6d3b930f382 661 int fifo_i32_write(FIFO_I32 *f, int32_t c)
jedh 0:f6d3b930f382 662 {
jedh 0:f6d3b930f382 663 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 664 {
jedh 0:f6d3b930f382 665 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 666 }
jedh 0:f6d3b930f382 667 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 668 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 669 return(TRUE);
jedh 0:f6d3b930f382 670 }
jedh 0:f6d3b930f382 671
jedh 0:f6d3b930f382 672 int fifo_i32_read(FIFO_I32 *f, int32_t *c)
jedh 0:f6d3b930f382 673 {
jedh 0:f6d3b930f382 674 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 675 {
jedh 0:f6d3b930f382 676 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 677 }
jedh 0:f6d3b930f382 678 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 679 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 680 return(TRUE);
jedh 0:f6d3b930f382 681 }
jedh 0:f6d3b930f382 682
jedh 0:f6d3b930f382 683
jedh 0:f6d3b930f382 684 #ifndef FIFO_FLOAT
jedh 0:f6d3b930f382 685 typedef struct fifo_float
jedh 0:f6d3b930f382 686 {
jedh 0:f6d3b930f382 687 float buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 688 int write_pointer;
jedh 0:f6d3b930f382 689 int read_pointer;
jedh 0:f6d3b930f382 690 } FIFO_FLOAT;
jedh 0:f6d3b930f382 691 #endif
jedh 0:f6d3b930f382 692
jedh 0:f6d3b930f382 693 void fifo_float_init(FIFO_FLOAT *f)
jedh 0:f6d3b930f382 694 {
jedh 0:f6d3b930f382 695 f->write_pointer=0;
jedh 0:f6d3b930f382 696 f->read_pointer=0;
jedh 0:f6d3b930f382 697 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 698 {
jedh 0:f6d3b930f382 699 f->buffer[i]=0;
jedh 0:f6d3b930f382 700 }
jedh 0:f6d3b930f382 701 }
jedh 0:f6d3b930f382 702
jedh 0:f6d3b930f382 703 int fifo_float_writeable(FIFO_FLOAT *f)
jedh 0:f6d3b930f382 704 {
jedh 0:f6d3b930f382 705 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 706 {
jedh 0:f6d3b930f382 707 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 708 }
jedh 0:f6d3b930f382 709 return(TRUE);
jedh 0:f6d3b930f382 710 }
jedh 0:f6d3b930f382 711
jedh 0:f6d3b930f382 712 int fifo_float_readable(FIFO_FLOAT *f)
jedh 0:f6d3b930f382 713 {
jedh 0:f6d3b930f382 714 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 715 {
jedh 0:f6d3b930f382 716 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 717 }
jedh 0:f6d3b930f382 718 return(TRUE);
jedh 0:f6d3b930f382 719 }
jedh 0:f6d3b930f382 720
jedh 0:f6d3b930f382 721 int fifo_float_write(FIFO_FLOAT *f, float c)
jedh 0:f6d3b930f382 722 {
jedh 0:f6d3b930f382 723 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 724 {
jedh 0:f6d3b930f382 725 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 726 }
jedh 0:f6d3b930f382 727 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 728 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 729 return(TRUE);
jedh 0:f6d3b930f382 730 }
jedh 0:f6d3b930f382 731
jedh 0:f6d3b930f382 732 int fifo_float_read(FIFO_FLOAT *f, float *c)
jedh 0:f6d3b930f382 733 {
jedh 0:f6d3b930f382 734 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 735 {
jedh 0:f6d3b930f382 736 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 737 }
jedh 0:f6d3b930f382 738 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 739 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 740 return(TRUE);
jedh 0:f6d3b930f382 741 }
jedh 0:f6d3b930f382 742
jedh 0:f6d3b930f382 743
jedh 0:f6d3b930f382 744 #ifndef FIFO_DOUBLE
jedh 0:f6d3b930f382 745 typedef struct fifo_double
jedh 0:f6d3b930f382 746 {
jedh 0:f6d3b930f382 747 double buffer[SIMPLE_FIFO_SIZE];
jedh 0:f6d3b930f382 748 int write_pointer;
jedh 0:f6d3b930f382 749 int read_pointer;
jedh 0:f6d3b930f382 750 } FIFO_DOUBLE;
jedh 0:f6d3b930f382 751 #endif
jedh 0:f6d3b930f382 752
jedh 0:f6d3b930f382 753 void fifo_double_init(FIFO_DOUBLE *f)
jedh 0:f6d3b930f382 754 {
jedh 0:f6d3b930f382 755 f->write_pointer=0;
jedh 0:f6d3b930f382 756 f->read_pointer=0;
jedh 0:f6d3b930f382 757 for(int i=0; i<SIMPLE_FIFO_SIZE; i++)
jedh 0:f6d3b930f382 758 {
jedh 0:f6d3b930f382 759 f->buffer[i]=0;
jedh 0:f6d3b930f382 760 }
jedh 0:f6d3b930f382 761 }
jedh 0:f6d3b930f382 762
jedh 0:f6d3b930f382 763 int fifo_double_writeable(FIFO_DOUBLE *f)
jedh 0:f6d3b930f382 764 {
jedh 0:f6d3b930f382 765 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 766 {
jedh 0:f6d3b930f382 767 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 768 }
jedh 0:f6d3b930f382 769 return(TRUE);
jedh 0:f6d3b930f382 770 }
jedh 0:f6d3b930f382 771
jedh 0:f6d3b930f382 772 int fifo_double_readable(FIFO_DOUBLE *f)
jedh 0:f6d3b930f382 773 {
jedh 0:f6d3b930f382 774 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 775 {
jedh 0:f6d3b930f382 776 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 777 }
jedh 0:f6d3b930f382 778 return(TRUE);
jedh 0:f6d3b930f382 779 }
jedh 0:f6d3b930f382 780
jedh 0:f6d3b930f382 781 int fifo_double_write(FIFO_DOUBLE *f, double c)
jedh 0:f6d3b930f382 782 {
jedh 0:f6d3b930f382 783 if(((f->write_pointer+1)% SIMPLE_FIFO_SIZE) == f->read_pointer)
jedh 0:f6d3b930f382 784 {
jedh 0:f6d3b930f382 785 return(FALSE); /* buffer full, can't write */
jedh 0:f6d3b930f382 786 }
jedh 0:f6d3b930f382 787 f->buffer[f->write_pointer] = c;
jedh 0:f6d3b930f382 788 f->write_pointer = (f->write_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 789 return(TRUE);
jedh 0:f6d3b930f382 790 }
jedh 0:f6d3b930f382 791
jedh 0:f6d3b930f382 792 int fifo_double_read(FIFO_DOUBLE *f, double *c)
jedh 0:f6d3b930f382 793 {
jedh 0:f6d3b930f382 794 if(f->read_pointer == f->write_pointer)
jedh 0:f6d3b930f382 795 {
jedh 0:f6d3b930f382 796 return(FALSE); /* buffer empty, can't read */
jedh 0:f6d3b930f382 797 }
jedh 0:f6d3b930f382 798 *c = f->buffer[f->read_pointer];
jedh 0:f6d3b930f382 799 f->read_pointer = (f->read_pointer+1)% SIMPLE_FIFO_SIZE;
jedh 0:f6d3b930f382 800 return(TRUE);
jedh 0:f6d3b930f382 801 }
jedh 0:f6d3b930f382 802
jedh 0:f6d3b930f382 803
jedh 0:f6d3b930f382 804
jedh 0:f6d3b930f382 805
jedh 0:f6d3b930f382 806
jedh 0:f6d3b930f382 807
jedh 0:f6d3b930f382 808 #endif /* #ifndef SIMPLE_FIFO_H */