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.

pbuff.h 1.2KB

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