소스 검색

improved ui; nice litte dial controls :) calc curve with offset und factor;

Zoadian 11 년 전
부모
커밋
139ba585db
8개의 변경된 파일284개의 추가작업 그리고 121개의 파일을 삭제
  1. 52 22
      drumduino/channel.cpp
  2. 3 2
      drumduino/channel.h
  3. 147 85
      drumduino/channel.ui
  4. 7 3
      drumduino/curve.h
  5. 16 1
      drumduino/drumduino.cpp
  6. 40 1
      drumduino/drumduino.h
  7. 1 0
      drumduino/drumduino.vcxproj
  8. 18 7
      drumduino/settings.h

+ 52 - 22
drumduino/channel.cpp

@@ -12,6 +12,8 @@ Channel::Channel(int channel, ChannelSettings& channelSettings, QWidget* parent)
 	, _curvePlot(new QCustomPlot(this))
 {
 	ui.setupUi(this);
+
+	_curvePlot->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 	ui.layoutCurvePlot->addWidget(_curvePlot);
 
 	QStringList notes;
@@ -31,26 +33,33 @@ Channel::Channel(int channel, ChannelSettings& channelSettings, QWidget* parent)
 	_curvePlot->yAxis->setRange(0, 127);
 	_curvePlot->setMinimumHeight(60);
 
+	//auto thresoldCurve = _curvePlot->addGraph(_curvePlot->xAxis, _curvePlot->yAxis);
+	//thresoldCurve->setPen(QPen(Qt::red));
+
 	_curvePlot->axisRect()->setAutoMargins(QCP::msNone);
 	_curvePlot->axisRect()->setMargins(QMargins(1, 1, 1, 1));
 
-	ui.leName->setPlaceholderText("Channel " + QString::number(channel));
+	ui.leName->setPlaceholderText("Channel " + QString::number(channel + 1));
+
+	update();
 
-	updateUi();
+	connect(ui.cbSensor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {_channelSettings.type = (Type)index; update(); });
 
-	connect(ui.cbSensor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {_channelSettings.type = (Type)index; updateUi(); });
+	connect(ui.cbNote, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {_channelSettings.note = index; update(); });
 
-	connect(ui.cbNote, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {_channelSettings.note = index; updateUi(); });
+	connect(ui.dialThresold, &QDial::valueChanged, [this](int value) {_channelSettings.thresold = value; update(); });
 
-	connect(ui.dialThresold, &QDial::valueChanged, [this](int value) {_channelSettings.thresold = value; updateUi(); });
+	connect(ui.dialScanTime, &QDial::valueChanged, [this](int value) {_channelSettings.scanTime = value; update(); });
 
-	connect(ui.dialScanTime, &QDial::valueChanged, [this](int value) {_channelSettings.scanTime = value; updateUi(); });
+	connect(ui.dialMaskTime, &QDial::valueChanged, [this](int value) {_channelSettings.maskTime = value; update(); });
 
-	connect(ui.dialMaskTime, &QDial::valueChanged, [this](int value) {_channelSettings.maskTime = value; updateUi(); });
+	connect(ui.cbCurveType, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) { _channelSettings.curve.type = (Curve)index; update(); });
 
-	connect(ui.cbCurveType, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) { _channelSettings.curveType = (Curve)index; updateUi(); });
+	connect(ui.dialValue, &QDial::valueChanged, [this](int value) {_channelSettings.curve.value = value; update(); });
 
-	connect(ui.dialOffset, &QDial::valueChanged, [this](int value) {_channelSettings.curveValue = value; updateUi(); });
+	connect(ui.dialOffset, &QDial::valueChanged, [this](int value) {_channelSettings.curve.offset = value; update(); });
+
+	connect(ui.dialFactor, &QDial::valueChanged, [this](int value) {_channelSettings.curve.factor = value; update(); });
 
 }
 
@@ -59,34 +68,55 @@ Channel::~Channel()
 
 }
 
-void Channel::updateUi()
+void Channel::update()
 {
 	ui.cbSensor->setCurrentIndex(_channelSettings.type);
 
 	ui.cbNote->setCurrentIndex(_channelSettings.note);
 
-	ui.labelThresold->setText(QString::number(_channelSettings.thresold));
+	//ui.labelThresold->setText(QString::number(_channelSettings.thresold));
 	ui.dialThresold->setValue(_channelSettings.thresold);
 
-	ui.labelScanTime->setText(QString::number(_channelSettings.scanTime));
+	//ui.labelScanTime->setText(QString::number(_channelSettings.scanTime));
 	ui.dialScanTime->setValue(_channelSettings.scanTime);
 
-	ui.labelMaskTime->setText(QString::number(_channelSettings.maskTime));
+	//ui.labelMaskTime->setText(QString::number(_channelSettings.maskTime));
 	ui.dialMaskTime->setValue(_channelSettings.maskTime);
 
-	ui.cbCurveType->setCurrentIndex(_channelSettings.curveType);
+	ui.cbCurveType->setCurrentIndex(_channelSettings.curve.type);
+
+	//ui.labelValue->setText(QString::number(_channelSettings.curveValue));
+	ui.dialValue->setValue(_channelSettings.curve.value);
 
-	ui.labelOffset->setText(QString::number(_channelSettings.curveValue));
-	ui.dialOffset->setValue(_channelSettings.curveValue);
+	//ui.labelOffset->setText(QString::number(_channelSettings.curveOffset));
+	ui.dialOffset->setValue(_channelSettings.curve.offset);
 
-	QVector<qreal> x(127);
-	QVector<qreal> y(127);
+	//ui.labelFactor->setText(QString::number(_channelSettings.curveFactor));
+	ui.dialFactor->setValue(_channelSettings.curve.factor);
 
-	for(auto i = 0; i < 127; ++i) {
-		x[i] = i;
-		y[i] = calcCurve(_channelSettings.curveType, i, _channelSettings.curveValue);
+	{
+		QVector<qreal> x(128);
+		QVector<qreal> y(128);
+
+		for(auto i = 0; i < 128; ++i) {
+			x[i] = i;
+			y[i] = calcCurve(_channelSettings.curve, i) ;
+		}
+
+		_curvePlot->graph(0)->setData(x, y);
 	}
 
-	_curvePlot->graph(0)->setData(x, y);
+	//{
+	//  QVector<qreal> x(2);
+	//  QVector<qreal> y(2);
+	//
+	//  x[0] = 0;
+	//  x[1] = 127;
+	//  y[0] = _channelSettings.thresold;
+	//  y[1] = _channelSettings.thresold;
+
+	//  _curvePlot->graph(1)->setData(x, y);
+	//}
+
 	_curvePlot->replot();
 }

+ 3 - 2
drumduino/channel.h

@@ -16,13 +16,14 @@ private:
 	QCustomPlot* _curvePlot;
 
 public:
-	Channel(int channel, ChannelSettings& channelSettings, QWidget *parent = 0);
+	Channel(int channel, ChannelSettings& channelSettings, QWidget* parent = 0);
 	~Channel();
 
 private:
 	Ui::channel ui;
 
-	void updateUi();
+public:
+	void update();
 };
 
 #endif // CHANNEL_H

+ 147 - 85
drumduino/channel.ui

@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>112</width>
-    <height>559</height>
+    <width>144</width>
+    <height>492</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -80,57 +80,53 @@
       <string>Sensor</string>
      </property>
      <layout class="QGridLayout" name="gridLayout_3">
-      <item row="5" column="0">
-       <widget class="QDial" name="dialMaskTime">
-        <property name="maximum">
-         <number>127</number>
-        </property>
-       </widget>
-      </item>
       <item row="1" column="0">
        <widget class="QDial" name="dialThresold">
-        <property name="maximum">
-         <number>127</number>
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
         </property>
-       </widget>
-      </item>
-      <item row="3" column="0">
-       <widget class="QDial" name="dialScanTime">
         <property name="maximum">
          <number>127</number>
         </property>
-       </widget>
-      </item>
-      <item row="1" column="1">
-       <widget class="QLabel" name="labelThresold">
-        <property name="text">
-         <string>50</string>
-        </property>
-        <property name="scaledContents">
-         <bool>false</bool>
+        <property name="notchTarget">
+         <double>8.000000000000000</double>
         </property>
-        <property name="alignment">
-         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        <property name="notchesVisible">
+         <bool>true</bool>
         </property>
        </widget>
       </item>
-      <item row="5" column="1">
-       <widget class="QLabel" name="labelMaskTime">
-        <property name="text">
-         <string>127</string>
+      <item row="3" column="0">
+       <widget class="QDial" name="dialScanTime">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
         </property>
-        <property name="alignment">
-         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
         </property>
-       </widget>
-      </item>
-      <item row="3" column="1">
-       <widget class="QLabel" name="labelScanTime">
-        <property name="text">
-         <string>127</string>
+        <property name="maximum">
+         <number>127</number>
         </property>
-        <property name="alignment">
-         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        <property name="notchTarget">
+         <double>8.000000000000000</double>
+        </property>
+        <property name="notchesVisible">
+         <bool>true</bool>
         </property>
        </widget>
       </item>
@@ -147,17 +143,42 @@
       <item row="2" column="0">
        <widget class="QLabel" name="label_8">
         <property name="text">
-         <string>Scan Time</string>
+         <string>ScanTime</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
-      <item row="4" column="0">
+      <item row="3" column="1">
+       <widget class="QDial" name="dialMaskTime">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
+        </property>
+        <property name="maximum">
+         <number>127</number>
+        </property>
+        <property name="notchTarget">
+         <double>8.000000000000000</double>
+        </property>
+        <property name="notchesVisible">
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
+      <item row="2" column="1">
        <widget class="QLabel" name="label_11">
         <property name="text">
-         <string>Mask Time</string>
+         <string>MaskTime</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
@@ -169,11 +190,8 @@
      <zorder>dialThresold</zorder>
      <zorder>label_8</zorder>
      <zorder>dialScanTime</zorder>
-     <zorder>label_11</zorder>
      <zorder>dialMaskTime</zorder>
-     <zorder>labelThresold</zorder>
-     <zorder>labelMaskTime</zorder>
-     <zorder>labelScanTime</zorder>
+     <zorder>label_11</zorder>
     </widget>
    </item>
    <item>
@@ -182,13 +200,56 @@
       <string>Curve</string>
      </property>
      <layout class="QGridLayout" name="gridLayout_2">
-      <item row="3" column="0">
-       <widget class="QLabel" name="label_2">
-        <property name="text">
-         <string>Factor</string>
+      <item row="2" column="0">
+       <widget class="QDial" name="dialValue">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
         </property>
-        <property name="alignment">
-         <set>Qt::AlignCenter</set>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
+        </property>
+        <property name="maximum">
+         <number>127</number>
+        </property>
+        <property name="notchTarget">
+         <double>8.000000000000000</double>
+        </property>
+        <property name="notchesVisible">
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
+      <item row="5" column="0">
+       <widget class="QDial" name="dialOffset">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
+        </property>
+        <property name="minimum">
+         <number>-127</number>
+        </property>
+        <property name="maximum">
+         <number>127</number>
+        </property>
+        <property name="notchTarget">
+         <double>16.000000000000000</double>
+        </property>
+        <property name="notchesVisible">
+         <bool>true</bool>
         </property>
        </widget>
       </item>
@@ -221,50 +282,40 @@
         </item>
        </widget>
       </item>
-      <item row="2" column="1">
-       <widget class="QLabel" name="labelOffset">
+      <item row="1" column="0">
+       <widget class="QLabel" name="label_3">
         <property name="text">
-         <string>127</string>
+         <string>Value</string>
         </property>
         <property name="alignment">
-         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+         <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
-      <item row="4" column="1">
-       <widget class="QLabel" name="labelFactor">
+      <item row="8" column="0" colspan="2">
+       <layout class="QVBoxLayout" name="layoutCurvePlot"/>
+      </item>
+      <item row="4" column="0">
+       <widget class="QLabel" name="label">
         <property name="text">
-         <string>127</string>
+         <string>Y-Offset</string>
         </property>
         <property name="alignment">
-         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="0">
-       <widget class="QDial" name="dialOffset">
-        <property name="sizePolicy">
-         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
-          <horstretch>0</horstretch>
-          <verstretch>0</verstretch>
-         </sizepolicy>
-        </property>
-        <property name="maximum">
-         <number>127</number>
+         <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
-      <item row="1" column="0">
-       <widget class="QLabel" name="label">
+      <item row="4" column="1">
+       <widget class="QLabel" name="label_2">
         <property name="text">
-         <string>Offset</string>
+         <string>Y-Factor</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
-      <item row="4" column="0">
+      <item row="5" column="1">
        <widget class="QDial" name="dialFactor">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
@@ -272,23 +323,34 @@
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
+        <property name="maximumSize">
+         <size>
+          <width>50</width>
+          <height>50</height>
+         </size>
+        </property>
+        <property name="minimum">
+         <number>0</number>
+        </property>
         <property name="maximum">
          <number>127</number>
         </property>
+        <property name="notchTarget">
+         <double>8.000000000000000</double>
+        </property>
+        <property name="notchesVisible">
+         <bool>true</bool>
+        </property>
        </widget>
       </item>
-      <item row="5" column="0" colspan="2">
-       <layout class="QVBoxLayout" name="layoutCurvePlot"/>
-      </item>
      </layout>
      <zorder>cbCurveType</zorder>
      <zorder>dialOffset</zorder>
+     <zorder>label_3</zorder>
+     <zorder>dialValue</zorder>
      <zorder>label</zorder>
-     <zorder>labelOffset</zorder>
-     <zorder>dialFactor</zorder>
      <zorder>label_2</zorder>
-     <zorder>labelFactor</zorder>
-     <zorder>verticalLayoutWidget</zorder>
+     <zorder>dialFactor</zorder>
     </widget>
    </item>
   </layout>

+ 7 - 3
drumduino/curve.h

@@ -1,14 +1,16 @@
 #pragma once
 
 
-inline byte calcCurve(Curve curve, int Value, int Form)
+#include "settings.h"
+
+inline byte calcCurve(CurveSettings curveSettings, int Value)
 {
 	int ret = 0;
 
 	float x = Value * 8.0;
-	float f = ((float)Form) / 64.0; //[1;127]->[0.;2.0]
+	float f = ((float)curveSettings.value) / 64.0; //[1;127]->[0.;2.0]
 
-	switch(curve) {
+	switch(curveSettings.type) {
 		//[0-1023]x[0-127]
 		case 0:
 			ret = x * f / 16.0;
@@ -35,6 +37,8 @@ inline byte calcCurve(Curve curve, int Value, int Form)
 
 	}
 
+	ret = ret * (curveSettings.factor / 127.0) + curveSettings.offset;
+
 	if(ret <= 0) { return 0; }
 
 	if(ret >= 127) { return 127; } //127

+ 16 - 1
drumduino/drumduino.cpp

@@ -21,7 +21,7 @@ bool readNextFrame(std::shared_ptr<Serial>& serial, DrumduinoProc& proc)
 AGAIN:
 	auto available = serial->available();
 
-	if(available < 2 + PORT_CNT * CHAN_CNT) {
+	if(available < sizeof(byte) + sizeof(byte) + sizeof(unsigned long) + PORT_CNT * CHAN_CNT) {
 		return false;
 	}
 
@@ -35,6 +35,9 @@ AGAIN:
 	byte manufacturer;
 	serial->readBytes(&manufacturer, 1);
 
+	unsigned long time;
+	serial->readBytes((byte*)&time, sizeof(unsigned long));
+
 	auto& frame = proc.frameBuffer[proc.frameCounter % BufferSize];
 	serial->readBytes(frame.data(), frame.size());
 
@@ -152,6 +155,12 @@ Drumduino::Drumduino(QWidget* parent)
 			auto channel = port * CHAN_CNT + pin;
 			auto wgtChannel = new Channel(channel, _settings.channelSettings[channel], wgtPort);
 			wgtPort->layout()->addWidget(wgtChannel);
+			_channels[channel] = wgtChannel;
+
+			for(auto dial : wgtChannel->findChildren<QDial*>()) {
+				dial->installEventFilter(this);
+			}
+
 		}
 	}
 
@@ -160,6 +169,7 @@ Drumduino::Drumduino(QWidget* parent)
 		auto fileName = QFileDialog::getSaveFileName(this, "Save", 0, tr("drumduino (*.edrum)"));
 
 		QFile file(fileName);
+		file.open(QIODevice::WriteOnly);
 		file.write((const char*)&_settings, sizeof(_settings));
 		file.close();
 	});
@@ -168,8 +178,13 @@ Drumduino::Drumduino(QWidget* parent)
 	connect(ui.actionLoad, &QAction::triggered, [this]() {
 		auto fileName = QFileDialog::getOpenFileName(this, "Open", 0, tr("drumduino (*.edrum)"));
 		QFile file(fileName);
+		file.open(QIODevice::ReadOnly);
 		file.read((char*)&_settings, sizeof(_settings));
 		file.close();
+
+		for(auto& channel : _channels) {
+			channel->update();
+		}
 	});
 
 

+ 40 - 1
drumduino/drumduino.h

@@ -9,7 +9,7 @@
 #include "qcustomplot.h"
 #include "settings.h"
 
-enum { BufferSize = 1024 };
+enum { BufferSize = 512 };
 
 enum State {
 	StateAwait,
@@ -18,6 +18,7 @@ enum State {
 };
 
 class DrumduinoThread;
+class Channel;
 
 struct DrumduinoProc {
 	uint64_t frameCounter;
@@ -55,6 +56,7 @@ public:
 
 private:
 	Ui::drumduinoClass ui;
+	std::array<Channel*, PORT_CNT* CHAN_CNT> _channels;
 
 	std::shared_ptr<Serial> _serial;
 	std::shared_ptr<MidiOut> _midiOut;
@@ -69,7 +71,44 @@ private:
 
 private:
 
+	bool eventFilter(QObject* o, QEvent* e)
+	{
+		auto dial = qobject_cast<QDial*>(o);
+
+		if(dial && e->type() == QEvent::Paint) {
+			QPaintEvent* paintEvent = static_cast<QPaintEvent*>(e);
+
+
+			QStylePainter p(dial);
+
+			QStyleOptionSlider option;
+
+			option.initFrom(dial);
+			option.minimum = dial->minimum();
+			option.maximum = dial->maximum();
+			option.sliderPosition = dial->value();
+			option.sliderValue = dial->value();
+			option.singleStep = dial->singleStep();
+			option.pageStep = dial->pageStep();
+			option.upsideDown = !dial->invertedAppearance();
+			option.notchTarget = 0;
+			option.dialWrapping = dial->wrapping();
+			option.subControls = QStyle::SC_All;
+			option.activeSubControls = QStyle::SC_None;
 
+			//option.subControls &= ~QStyle::SC_DialTickmarks;
+			//option.tickPosition = QSlider::TicksAbove;
+			option.tickPosition = QSlider::NoTicks;
+
+			option.tickInterval = dial->notchSize();
+
+			p.drawComplexControl(QStyle::CC_Dial, option);
+			p.drawText(dial->rect(), Qt::AlignCenter, QString::number(dial->value()));
+			return true;
+		}
+
+		return QMainWindow::eventFilter(o, e);
+	}
 
 
 #if 0

+ 1 - 0
drumduino/drumduino.vcxproj

@@ -224,6 +224,7 @@
       <Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Uic%27ing %(Identity)...</Message>
       <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\GeneratedFiles\ui_%(Filename).h;%(Outputs)</Outputs>
       <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(QTDIR)\bin\uic.exe" -o ".\GeneratedFiles\ui_%(Filename).h" "%(FullPath)"</Command>
+      <SubType>Designer</SubType>
     </CustomBuild>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 18 - 7
drumduino/settings.h

@@ -19,23 +19,34 @@ enum Curve {
 	eXTRA,
 };
 
+struct CurveSettings {
+	Curve type;
+	uint8_t value;
+	int8_t offset;
+	uint8_t factor;
+
+	CurveSettings()
+		: type(Normal)
+		, value(127)
+		, offset(0)
+		, factor(127)
+	{}
+};
+
 struct ChannelSettings {
 	Type type;
 	uint8_t note;
 	uint8_t thresold;
 	qint64 scanTime;
 	qint64 maskTime;
-	Curve curveType;
-	int curveValue;
+	CurveSettings curve;
 
 	ChannelSettings()
 		: type(TypeDisabled)
 		, note(35)
-		, thresold(70)
-		, scanTime(2)
-		, maskTime(5)
-		, curveType(Normal)
-		, curveValue(127)
+		, thresold(25)
+		, scanTime(4)
+		, maskTime(10)
 	{}
 };