Keyboard firmwares for Atmel AVR and Cortex-M
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef CIRCBUFFER_H
  19. #define CIRCBUFFER_H
  20. template <class T, int Size>
  21. class CircBuffer {
  22. public:
  23. CircBuffer():write(0), read(0){}
  24. bool isFull() {
  25. return ((write + 1) % size == read);
  26. };
  27. bool isEmpty() {
  28. return (read == write);
  29. };
  30. void queue(T k) {
  31. if (isFull()) {
  32. read++;
  33. read %= size;
  34. }
  35. buf[write++] = k;
  36. write %= size;
  37. }
  38. uint16_t available() {
  39. return (write >= read) ? write - read : size - read + write;
  40. };
  41. bool dequeue(T * c) {
  42. bool empty = isEmpty();
  43. if (!empty) {
  44. *c = buf[read++];
  45. read %= size;
  46. }
  47. return(!empty);
  48. };
  49. private:
  50. volatile uint16_t write;
  51. volatile uint16_t read;
  52. static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram.
  53. T buf[Size];
  54. };
  55. #endif