DataHelper.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "DataHelper.h"
  2. /**
  3. * 初始化一个先入先出缓冲
  4. *
  5. * @author lxz (041620 15:15:44)
  6. *
  7. * @param fifo 缓冲对象
  8. * @param buffer 数据缓冲
  9. * @param total 总数据大小
  10. */
  11. void dh_fifo_init(fifo_buffer_t *fifo, unsigned char *buffer, int total) {
  12. fifo->buffer = buffer;
  13. fifo->total = total;
  14. fifo->count = 0;
  15. fifo->head = 0;
  16. fifo->tail = 0;
  17. }
  18. /**
  19. * 缓冲保存一个数据
  20. *
  21. * @author lxz (041620 15:16:14)
  22. *
  23. * @param fifo 缓冲对象
  24. * @param dat 缓冲数据
  25. */
  26. void dh_fifo_push(fifo_buffer_t *fifo, unsigned char dat) {
  27. if (fifo->count + 1 < fifo->total) {
  28. DH_ENTER_CRITCAL();
  29. fifo->buffer[fifo->head] = dat;
  30. if (++fifo->head >= fifo->total) {
  31. fifo->head = 0;
  32. }
  33. fifo->count++;
  34. DH_EXIT_CRITCAL();
  35. }
  36. }
  37. /**
  38. *
  39. *
  40. * @author lxz (041620 15:16:43)
  41. *
  42. * @param fifo
  43. * @param dat
  44. *
  45. * @return int
  46. */
  47. int dh_fifo_pop(fifo_buffer_t *fifo, unsigned char *dat) {
  48. if (fifo->count > 0) {
  49. DH_ENTER_CRITCAL();
  50. *dat = fifo->buffer[fifo->tail];
  51. if (++fifo->tail >= fifo->total) {
  52. fifo->tail = 0;
  53. }
  54. fifo->count--;
  55. DH_EXIT_CRITCAL();
  56. return 1;
  57. }
  58. return 0;
  59. }