upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

ring_buffer.h 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef RING_BUFFER_H
  2. #define RING_BUFFER_H
  3. /*--------------------------------------------------------------------
  4. * Ring buffer to store scan codes from keyboard
  5. *------------------------------------------------------------------*/
  6. #define RBUF_SIZE 32
  7. static uint8_t rbuf[RBUF_SIZE];
  8. static uint8_t rbuf_head = 0;
  9. static uint8_t rbuf_tail = 0;
  10. static inline void rbuf_enqueue(uint8_t data)
  11. {
  12. uint8_t sreg = SREG;
  13. cli();
  14. uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
  15. if (next != rbuf_tail) {
  16. rbuf[rbuf_head] = data;
  17. rbuf_head = next;
  18. } else {
  19. print("rbuf: full\n");
  20. }
  21. SREG = sreg;
  22. }
  23. static inline uint8_t rbuf_dequeue(void)
  24. {
  25. uint8_t val = 0;
  26. uint8_t sreg = SREG;
  27. cli();
  28. if (rbuf_head != rbuf_tail) {
  29. val = rbuf[rbuf_tail];
  30. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  31. }
  32. SREG = sreg;
  33. return val;
  34. }
  35. static inline bool rbuf_has_data(void)
  36. {
  37. uint8_t sreg = SREG;
  38. cli();
  39. bool has_data = (rbuf_head != rbuf_tail);
  40. SREG = sreg;
  41. return has_data;
  42. }
  43. static inline void rbuf_clear(void)
  44. {
  45. uint8_t sreg = SREG;
  46. cli();
  47. rbuf_head = rbuf_tail = 0;
  48. SREG = sreg;
  49. }
  50. #endif /* RING_BUFFER_H */