Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

TinyGsmFifo.h 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef TinyGsmFifo_h
  2. #define TinyGsmFifo_h
  3. template <class T, unsigned N>
  4. class TinyGsmFifo
  5. {
  6. public:
  7. TinyGsmFifo()
  8. {
  9. clear();
  10. }
  11. void clear()
  12. {
  13. _r = 0;
  14. _w = 0;
  15. }
  16. // writing thread/context API
  17. //-------------------------------------------------------------
  18. bool writeable(void)
  19. {
  20. return free() > 0;
  21. }
  22. int free(void)
  23. {
  24. int s = _r - _w;
  25. if (s <= 0) {
  26. s += N;
  27. }
  28. return s - 1;
  29. }
  30. bool put(const T& c)
  31. {
  32. int i = _w;
  33. int j = i;
  34. i = _inc(i);
  35. if (i == _r) { // !writeable()
  36. return false;
  37. }
  38. _b[j] = c;
  39. _w = i;
  40. return true;
  41. }
  42. int put(const T* p, int n, bool t = false)
  43. {
  44. int c = n;
  45. while (c) {
  46. int f;
  47. while ((f = free()) == 0) { // wait for space
  48. if (!t) {
  49. return n - c; // no more space and not blocking
  50. }
  51. /* nothing / just wait */;
  52. }
  53. // check free space
  54. if (c < f) {
  55. f = c;
  56. }
  57. int w = _w;
  58. int m = N - w;
  59. // check wrap
  60. if (f > m) {
  61. f = m;
  62. }
  63. memcpy(&_b[w], p, f);
  64. _w = _inc(w, f);
  65. c -= f;
  66. p += f;
  67. }
  68. return n - c;
  69. }
  70. // reading thread/context API
  71. // --------------------------------------------------------
  72. bool readable(void)
  73. {
  74. return (_r != _w);
  75. }
  76. size_t size(void)
  77. {
  78. int s = _w - _r;
  79. if (s < 0) {
  80. s += N;
  81. }
  82. return s;
  83. }
  84. bool get(T* p)
  85. {
  86. int r = _r;
  87. if (r == _w) { // !readable()
  88. return false;
  89. }
  90. *p = _b[r];
  91. _r = _inc(r);
  92. return true;
  93. }
  94. int get(T* p, int n, bool t = false)
  95. {
  96. int c = n;
  97. while (c) {
  98. int f;
  99. for (;;) { // wait for data
  100. f = size();
  101. if (f) {
  102. break; // free space
  103. }
  104. if (!t) {
  105. return n - c; // no space and not blocking
  106. }
  107. /* nothing / just wait */;
  108. }
  109. // check available data
  110. if (c < f) {
  111. f = c;
  112. }
  113. int r = _r;
  114. int m = N - r;
  115. // check wrap
  116. if (f > m) {
  117. f = m;
  118. }
  119. memcpy(p, &_b[r], f);
  120. _r = _inc(r, f);
  121. c -= f;
  122. p += f;
  123. }
  124. return n - c;
  125. }
  126. private:
  127. int _inc(int i, int n = 1)
  128. {
  129. return (i + n) % N;
  130. }
  131. T _b[N];
  132. int _w;
  133. int _r;
  134. };
  135. #endif