1
1

drumduino.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. class Channel;
  17. struct DrumduinoProc {
  18. uint64_t frameCounter;
  19. std::array<std::array<byte, PORT_CNT* CHAN_CNT>, BufferSize> frameBuffer;
  20. std::array<State, PORT_CNT* CHAN_CNT> states;
  21. std::array<uint64_t, PORT_CNT* CHAN_CNT> triggers;
  22. std::array<byte, PORT_CNT* CHAN_CNT> maxs;
  23. std::array<uint64_t, PORT_CNT* CHAN_CNT> sums;
  24. std::array<std::array<State, PORT_CNT* CHAN_CNT>, BufferSize> stateBuffer;
  25. DrumduinoProc()
  26. : frameCounter(0)
  27. {
  28. for(auto& fb : frameBuffer) {
  29. fb.fill(0);
  30. }
  31. states.fill(StateAwait);
  32. triggers.fill(0);
  33. maxs.fill(0);
  34. for(auto& sb : stateBuffer) {
  35. sb.fill(StateAwait);
  36. }
  37. }
  38. };
  39. class Drumduino : public QMainWindow
  40. {
  41. Q_OBJECT
  42. public:
  43. Drumduino(QWidget* parent = 0);
  44. ~Drumduino();
  45. private:
  46. Ui::drumduinoClass ui;
  47. std::array<Channel*, PORT_CNT* CHAN_CNT> _channels;
  48. std::shared_ptr<Serial> _serial;
  49. std::shared_ptr<MidiOut> _midiOut;
  50. DrumduinoThread* _drumduinoThread;
  51. Settings _settings;
  52. DrumduinoProc _proc;
  53. private:
  54. bool readFrame(std::array<byte, PORT_CNT* CHAN_CNT>& frame);
  55. private:
  56. bool eventFilter(QObject* o, QEvent* e)
  57. {
  58. auto dial = qobject_cast<QDial*>(o);
  59. if(dial && e->type() == QEvent::Paint) {
  60. QPaintEvent* paintEvent = static_cast<QPaintEvent*>(e);
  61. QStylePainter p(dial);
  62. QStyleOptionSlider option;
  63. option.initFrom(dial);
  64. option.minimum = dial->minimum();
  65. option.maximum = dial->maximum();
  66. option.sliderPosition = dial->value();
  67. option.sliderValue = dial->value();
  68. option.singleStep = dial->singleStep();
  69. option.pageStep = dial->pageStep();
  70. option.upsideDown = !dial->invertedAppearance();
  71. option.notchTarget = 0;
  72. option.dialWrapping = dial->wrapping();
  73. option.subControls = QStyle::SC_All;
  74. option.activeSubControls = QStyle::SC_None;
  75. //option.subControls &= ~QStyle::SC_DialTickmarks;
  76. //option.tickPosition = QSlider::TicksAbove;
  77. option.tickPosition = QSlider::NoTicks;
  78. option.tickInterval = dial->notchSize();
  79. /*
  80. p.setPen(qApp->palette().buttonText().color());
  81. p.setBrush(qApp->palette().buttonText().color());*/
  82. p.drawComplexControl(QStyle::CC_Dial, option);
  83. p.setPen(qApp->palette().buttonText().color());
  84. p.drawText(dial->rect(), Qt::AlignCenter, QString::number(dial->value()));
  85. return true;
  86. }
  87. return QMainWindow::eventFilter(o, e);
  88. }
  89. signals:
  90. void updateChannelProgess(size_t channel, byte maxValue, byte sumValue, byte calcValue);
  91. public slots:
  92. void slotUpdateChannelProgress(size_t channel, byte maxValue, byte sumValue, byte calcValue);
  93. };
  94. class DrumduinoThread : public QThread
  95. {
  96. Q_OBJECT
  97. private:
  98. Drumduino* _drumduino;
  99. bool _run;
  100. std::function<void()> _fnCall;
  101. public:
  102. DrumduinoThread(Drumduino* drumduino, std::function<void()> fnCall)
  103. : QThread(drumduino)
  104. , _drumduino(drumduino)
  105. , _run(true)
  106. , _fnCall(fnCall)
  107. {}
  108. void run() Q_DECL_OVERRIDE {
  109. for(; _run;)
  110. {
  111. _fnCall();
  112. }
  113. }
  114. void stop()
  115. {
  116. _run = false;
  117. }
  118. };
  119. #endif // DRUMDUINO_H