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.

110 lines
2.6KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.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. #ifndef AVRLIB_UI_EVENT_QUEUE_H_
  20. #define AVRLIB_UI_EVENT_QUEUE_H_
  21. #include "avrlib/base.h"
  22. #include "avrlib/op.h"
  23. #include "avrlib/ring_buffer.h"
  24. #include "avrlib/time.h"
  25. namespace avrlib {
  26. enum ControlType {
  27. CONTROL_POT = 0,
  28. CONTROL_ENCODER = 1,
  29. CONTROL_ENCODER_CLICK = 2,
  30. CONTROL_SWITCH = 3
  31. };
  32. struct Event {
  33. uint8_t control_type;
  34. uint8_t control_id;
  35. uint8_t value;
  36. };
  37. template<uint8_t size = 32>
  38. class EventQueue {
  39. public:
  40. enum {
  41. buffer_size = size,
  42. data_size = 16,
  43. };
  44. typedef uint16_t Value;
  45. typedef EventQueue<size> Me;
  46. EventQueue() { }
  47. static void Flush() {
  48. events_.Flush();
  49. };
  50. static void AddEvent(uint8_t control_type, uint8_t id, uint8_t data) {
  51. Word v;
  52. v.bytes[0] = (U8ShiftLeft4(control_type) << 2) | (id & 0x3f);
  53. v.bytes[1] = data;
  54. events_.Overwrite(v.value);
  55. }
  56. static uint8_t available() {
  57. return events_.readable();
  58. }
  59. static uint16_t idle_time() {
  60. uint32_t now = milliseconds();
  61. return static_cast<uint16_t>(now - last_event_time_) >> 8;
  62. }
  63. static uint16_t idle_time_ms() {
  64. uint32_t now = milliseconds();
  65. return static_cast<uint16_t>(now - last_event_time_);
  66. }
  67. static void Touch() {
  68. last_event_time_ = milliseconds();
  69. }
  70. static Event PullEvent() {
  71. Event e;
  72. Word v;
  73. v.value = events_.ImmediateRead();
  74. e.control_type = U8ShiftRight4(v.bytes[0]) >> 2;
  75. e.control_id = v.bytes[0] & 0x3f;
  76. e.value = v.bytes[1];
  77. return e;
  78. }
  79. private:
  80. static uint32_t last_event_time_;
  81. static RingBuffer<Me> events_;
  82. };
  83. /* static */
  84. template<uint8_t size>
  85. RingBuffer<EventQueue<size> > EventQueue<size>::events_;
  86. /* static */
  87. template<uint8_t size>
  88. uint32_t EventQueue<size>::last_event_time_;
  89. } // namespace avrlib
  90. #endif // AVRLIB_UI_EVENT_QUEUE_H_