Jan-Langevad

FIFO Buffer Queue ESP32FORTH

Mar 30th, 2024
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | Software | 0 0
  1. ( ESP32-FIFO.TXT )
  2. \ INTEGER FIFO BUFFER / QUEQE
  3. 3 VALUE BUFFER-SIZE \ <*********** Small test size!
  4. CREATE FIFOBUFFER BUFFER-SIZE CELLS ALLOT
  5. 0 VALUE NEXT-IN
  6. 0 VALUE NEXT-OUT
  7. 0 VALUE BUFFER-COUNT
  8.  
  9. : CLEAR-FIFOBUFFER \ clear buffer
  10. 0 is NEXT-IN
  11. 0 is NEXT-OUT
  12. 0 is BUFFER-COUNT
  13. ;
  14. : >FIFOBUFFER ( n -- )
  15. BUFFER-COUNT BUFFER-SIZE =
  16. IF CR ." BUFFER is Full! IGNORED!" CR
  17. DROP \ !!!
  18. ELSE FIFOBUFFER NEXT-IN CELLS + !
  19. BUFFER-COUNT 1+ IS BUFFER-COUNT
  20. NEXT-IN 1+ is NEXT-IN
  21. NEXT-IN BUFFER-SIZE =
  22. IF 0 is NEXT-IN
  23. THEN
  24. THEN ;
  25. : >Q >FIFOBUFFER ;
  26.  
  27. : FIFOBUFFER> ( -- n )
  28. BUFFER-COUNT 0 =
  29. IF CR ." BUFFER is Empty! IGNORED!" CR
  30. ELSE FIFOBUFFER NEXT-OUT CELLS + @
  31. BUFFER-COUNT 1- IS BUFFER-COUNT
  32. NEXT-OUT 1+ is NEXT-OUT
  33. NEXT-OUT BUFFER-SIZE =
  34. IF 0 is NEXT-OUT THEN
  35. THEN ;
  36. : Q> FIFOBUFFER> ;
  37.  
  38. : BUFFER-PEEK ( -- n )
  39. BUFFER-COUNT 0 =
  40. IF CR ." BUFFER is Empty! IGNORED!" CR
  41. ELSE FIFOBUFFER NEXT-OUT CELLS + @
  42. THEN ;
  43. : PQ BUFFER-PEEK ; \ Peek Queue
  44.  
  45. : BUFFER-FULL? ( -- flag ) BUFFER-COUNT BUFFER-SIZE = ;
  46.  
  47. : BUFFER-EMPTY? ( -- flag ) BUFFER-COUNT 0= ;
  48.  
  49. : BUFFER-DISPLAY ( -- )
  50. BUFFER-COUNT 0=
  51. IF CR ." Buffer is empty!" CR
  52. ELSE BUFFER-COUNT 0
  53. DO NEXT-OUT I + BUFFER-SIZE MOD CELLS
  54. FIFOBUFFER + @
  55. CR .
  56. LOOP
  57. THEN ;
  58. : ZQ BUFFER-DISPLAY ; \ "See Queue"
  59.  
  60. \ EOF
  61. FIFO (First-In-First-Out) buffers are commonly used in computer programs,
  62. especially in data processing applications where data needs to be stored
  63. and retrieved in the order it was received.
  64.  
  65. FIFO buffers are particularly useful in scenarios such as:
  66.  
  67. Sensor data processing: FIFO buffers are used in sensor data processing applications
  68. where data from multiple sensors needs to be processed in real-time.
  69. The FIFO buffer helps to synchronize and organize the incoming sensor data.
  70.  
  71. Data transmission: In communication systems, FIFO buffers are often employed
  72. to temporarily store incoming data before it is processed or transmitted further.
  73. This ensures that data is processed in the same order it was received,
  74. preventing data loss or misalignment.
  75.  
  76. Task scheduling: FIFO buffers can be used in task scheduling algorithms where tasks are
  77. executed in the order they were received.
  78. This ensures fairness and maintains the order of task execution.
  79.  
  80. Overall, FIFO buffers are a fundamental data structure in computer programming and are widely used in
  81. various applications to manage and process data in a sequential manner.
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment