MUP1+Øvelse+9+Qt

toc =QT Multimeter=

HandleADC
HandleADC skal være en tråd, så vi anvender QThread. Anvendes på samme måde som vores linuxwrap. Bemærk Q_OBJECT, samt signals.

HandleADC.h
code format="cpp"
 * 1) include 

class HandleADC : public QThread {	Q_OBJECT public: HandleADC; virtual ~HandleADC; void run; signals: void valueChanged(int); }; code

HandleADC.cpp
code format="cpp" void HandleADC::run {	unsigned int valueADC; while (1) {		if (valueADC == 2048) valueADC = 0; else valueADC++; emit valueChanged(valueADC); usleep(1000 * 200); // 5Hz } } code

HandleDAC
Bemærk vi har både signals og slots.

HandleDAC.h
code format="cpp"
 * 1) include 

class HandleDAC : public QThread {	Q_OBJECT public: HandleDAC; virtual ~HandleDAC; void run; public slots: void updateValue(int); signals: void valueChanged(int); private: int value; }; code

HandleDAC.cpp
code format="cpp" HandleDAC::HandleDAC { value = 0; }

void HandleDAC::run {	while (1) {		emit valueChanged(value); usleep(200*1000); // 5Hz } }

void HandleDAC::updateValue(int value) {	this->value = value; } code

SignalGen.h
code format="cpp"
 * 1) include 


 * 1) define PI 3.14159265

class SignalGen : public QObject { Q_OBJECT public: enum WAVEFORMTYPE { TRIANGLE, SQUARE };	SignalGen; virtual ~SignalGen; public slots: void setWaveformType(int); void calcSample; void setFrequency(int); void setVoltage(int);

signals: void valueChanged(int);

private: double counter; int value; WAVEFORMTYPE waveformType; int frequency; int voltage; }; code

SignalGen.cpp
code format="cpp"
 * 1) include

SignalGen::SignalGen {	counter = 0; value = 0; waveformType = TRIANGLE; frequency = 5; voltage = 1; }

SignalGen::~SignalGen {	// TODO Auto-generated destructor stub }

void SignalGen::setWaveformType(int waveformType) {	switch (waveformType) {	case 0: this->waveformType = TRIANGLE; break; case 1: this->waveformType = SQUARE; break; default: this->waveformType = TRIANGLE; } }

void SignalGen::setFrequency(int frequency) {	this->frequency = frequency; }

void SignalGen::setVoltage(int voltage) {	this->voltage = voltage; }

void SignalGen::calcSample {	double angelFreq = 2 * PI * frequency; value = (voltage / 2) * (sin(angelFreq * counter) + 1);

if (counter == 2 * PI) counter = 0; else counter += 0.01;

if (waveformType == SQUARE) {		if (value > 1024) value = 2048; else value = 0; }	emit(valueChanged(value)); } code

Connect
Nu mangler vi bare at forbinde signals til slots. code format="cpp" // freqDial -> signalGen connect(ui.freqDial, SIGNAL(valueChanged(int)), signalGen, SLOT(setFrequency(int)));

// voltageDial -> signalGen connect(ui.voltageDial, SIGNAL(valueChanged(int)), signalGen, SLOT(setVoltage(int)));

// combobox waveformtype -> signalGen connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), signalGen, SLOT(setWaveformType(int)));

// signalGen -> handleDAC connect(signalGen, SIGNAL(valueChanged(int)), handleDAC, SLOT(updateValue(int)));

// handleDAC -> lcdDAC connect(handleDAC, SIGNAL(valueChanged(int)), ui.lcdDAC, SLOT(display(int)));

// signalGen -> lcdADC connect(signalGen, SIGNAL(valueChanged(int)), ui.lcdADC, SLOT(display(int))); code

QTimer
Nu skal vi bruge en QTimer til at generere et signal. Først opretter vi en timer og starter den. code format="cpp" timer = new QTimer(this); timer->start(250); code Hvorefter vi forbinder timerens timeout-signal til vores slot. code format="cpp" // timer -> signalGen connect(timer, SIGNAL(timeout), signalGen, SLOT(calcSample)); code