1
1

drumduino.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef DRUMDUINO_H
  2. #define DRUMDUINO_H
  3. #include <QtWidgets/QMainWindow>
  4. #include "ui_drumduino.h"
  5. #include "midi.h"
  6. #include "serial.h"
  7. #include "qcustomplot.h"
  8. #include "settings.h"
  9. enum { BufferSize = 1024 };
  10. enum State {
  11. StateAwait,
  12. StateScan,
  13. StateMask,
  14. };
  15. class DrumduinoThread;
  16. struct DrumduinoProc {
  17. uint64_t frameCounter;
  18. std::array<std::array<byte, PORT_CNT* CHAN_CNT>, BufferSize> frameBuffer;
  19. std::array<State, PORT_CNT* CHAN_CNT> states;
  20. std::array<uint64_t, PORT_CNT* CHAN_CNT> triggers;
  21. std::array<byte, PORT_CNT* CHAN_CNT> maxs;
  22. std::array<std::array<State, PORT_CNT* CHAN_CNT>, BufferSize> stateBuffer;
  23. DrumduinoProc()
  24. : frameCounter(0)
  25. {
  26. for(auto& fb : frameBuffer) {
  27. fb.fill(0);
  28. }
  29. states.fill(StateAwait);
  30. triggers.fill(0);
  31. maxs.fill(0);
  32. for(auto& sb : stateBuffer) {
  33. sb.fill(StateAwait);
  34. }
  35. }
  36. };
  37. class Drumduino : public QMainWindow
  38. {
  39. Q_OBJECT
  40. public:
  41. Drumduino(QWidget* parent = 0);
  42. ~Drumduino();
  43. private:
  44. Ui::drumduinoClass ui;
  45. std::shared_ptr<Serial> _serial;
  46. std::shared_ptr<MidiOut> _midiOut;
  47. DrumduinoThread* _drumduinoThread;
  48. Settings _settings;
  49. DrumduinoProc _proc;
  50. private:
  51. bool readFrame(std::array<byte, PORT_CNT* CHAN_CNT>& frame);
  52. private:
  53. #if 0
  54. public:
  55. std::vector<QCustomPlot*> _plots;
  56. bool _updateGraph;
  57. qint64 _lasttime;
  58. qint64 _startTime;
  59. private:
  60. uint64_t _currentFrame;
  61. std::array<std::array<byte, BufferSize>, PORT_CNT* CHAN_CNT> _frameBuffer;
  62. std::array<std::array<State, BufferSize>, PORT_CNT* CHAN_CNT> _stateBuffer;
  63. std::array<State, PORT_CNT* CHAN_CNT> _states;
  64. std::array<uint64_t, PORT_CNT* CHAN_CNT> _triggers;
  65. std::array<byte, PORT_CNT* CHAN_CNT> _max;
  66. std::array<byte, PORT_CNT* CHAN_CNT> _maxVal;
  67. public:
  68. void serialRead();
  69. void updateGraph();
  70. void handleFrame(const std::array<byte, PORT_CNT* CHAN_CNT>& frame, const uint64_t currentIndex);
  71. #endif
  72. };
  73. class DrumduinoThread : public QThread
  74. {
  75. Q_OBJECT
  76. private:
  77. Drumduino* _drumduino;
  78. bool _run;
  79. std::function<void()> _fnCall;
  80. public:
  81. DrumduinoThread(Drumduino* drumduino, std::function<void()> fnCall)
  82. : QThread(drumduino)
  83. , _drumduino(drumduino)
  84. , _run(true)
  85. , _fnCall(fnCall)
  86. {}
  87. void run() Q_DECL_OVERRIDE {
  88. for(; _run;)
  89. {
  90. _fnCall();
  91. }
  92. }
  93. void stop()
  94. {
  95. _run = false;
  96. }
  97. };
  98. #endif // DRUMDUINO_H