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.

event_queue.hpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2011 Emilie Gillet.
  2. //
  3. // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Event queue.
  19. #pragma once
  20. #include <stmlib/utils/ring_buffer.h>
  21. namespace streams
  22. {
  23. enum ControlType
  24. {
  25. CONTROL_POT = 0,
  26. CONTROL_ENCODER = 1,
  27. CONTROL_ENCODER_CLICK = 2,
  28. CONTROL_ENCODER_LONG_CLICK = 3,
  29. CONTROL_SWITCH = 4,
  30. CONTROL_SWITCH_HOLD = 5,
  31. CONTROL_REFRESH = 0xff
  32. };
  33. struct Event
  34. {
  35. ControlType control_type;
  36. uint16_t control_id;
  37. int32_t data;
  38. };
  39. template<uint16_t size = 32>
  40. class EventQueue
  41. {
  42. public:
  43. EventQueue() { }
  44. void Init()
  45. {
  46. events_.Init();
  47. time_ = 0;
  48. }
  49. void Flush()
  50. {
  51. events_.Flush();
  52. };
  53. void AddEvent(ControlType control_type, uint16_t id, int32_t data)
  54. {
  55. Event e;
  56. e.control_type = control_type;
  57. e.control_id = id;
  58. e.data = data;
  59. events_.Overwrite(e);
  60. Touch();
  61. }
  62. void Touch()
  63. {
  64. last_event_time_ = time_;
  65. }
  66. size_t available()
  67. {
  68. return events_.readable();
  69. }
  70. uint32_t idle_time()
  71. {
  72. uint32_t now = time_;
  73. return now - last_event_time_;
  74. }
  75. Event PullEvent()
  76. {
  77. return events_.ImmediateRead();
  78. }
  79. void StepTime(uint32_t step)
  80. {
  81. time_ += step;
  82. }
  83. private:
  84. uint32_t last_event_time_;
  85. stmlib::RingBuffer<Event, size> events_;
  86. uint32_t time_;
  87. };
  88. }