90 lines
1.8 KiB
C
90 lines
1.8 KiB
C
#include "filo.h"
|
||
#include "stdio.h"
|
||
|
||
// if (FILO_get(filo, byte) == 0) {
|
||
// OK
|
||
// } else {
|
||
// FILO ERROR
|
||
// }
|
||
|
||
int FILO_isUsable(filo_s* filo);
|
||
|
||
// Инициализация filo
|
||
int FILO_init(filo_s* filo) {
|
||
if (filo != NULL) {
|
||
for (size_t i = 0; i < FILO_DATA_SIZE; ++i) {
|
||
filo->data[i] = 0;
|
||
}
|
||
filo->head = 0;
|
||
filo->state = FILOS_empty;
|
||
filo->isInitialized = 1;
|
||
if (FILO_isUsable(filo) == 0) {
|
||
return 0;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
// Заполнение filo
|
||
int FILO_put(filo_s* filo, uint8_t data) {
|
||
if (FILO_isUsable(filo) == 0) { // Не заполнено
|
||
if ((filo->state == FILOS_empty) || (filo->state == FILOS_ready)) {
|
||
filo->data[filo->head] = data;
|
||
|
||
if (filo->head < FILO_DATA_SIZE-1) {
|
||
filo->head++;
|
||
filo->state = FILOS_ready;
|
||
} else {
|
||
filo->head = FILO_DATA_SIZE-1;
|
||
filo->state = FILOS_full;
|
||
}
|
||
return 0;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
// Получение из filo
|
||
int FILO_get(filo_s* filo, uint8_t *data) {
|
||
if (FILO_isUsable(filo) == 0) {
|
||
if ((filo->state == FILOS_full) || (filo->state == FILOS_ready)) {
|
||
*data = filo->data[filo->head-1];
|
||
filo->data[filo->head-1] = 0;
|
||
if (filo->head > 1) {
|
||
filo->head--;
|
||
filo->state = FILOS_ready;
|
||
} else {
|
||
filo->head = 0;
|
||
filo->state = FILOS_empty;
|
||
}
|
||
return 0;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
int FILO_print(filo_s* filo) {
|
||
if (FILO_isUsable(filo) == 0) {
|
||
printf("FILO: ");
|
||
for (size_t i = 0; i < FILO_DATA_SIZE; i++) {
|
||
printf("%d", filo->data[i]);
|
||
}
|
||
printf("\n");
|
||
return 0;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
// Проверка доступности filo
|
||
int FILO_isUsable(filo_s* filo) {
|
||
if (filo != NULL) {
|
||
if (filo->isInitialized == 1) {
|
||
return 0;
|
||
}
|
||
}
|
||
return -1;
|
||
} |