Using lambda functions

pull/88/head
Marcin Kondej 2019-09-27 01:02:17 +02:00
rodzic f5762bdc98
commit 3a68f2f3e8
2 zmienionych plików z 36 dodań i 45 usunięć

Wyświetl plik

@ -287,8 +287,12 @@ void Transmitter::transmit(WaveReader &reader, float frequency, float bandwidth,
output = initClockOutput(); output = initClockOutput();
} }
bool errorCatched = false; auto finally = [&]() {
std::string errorMessage; if (!preserveCarrier) {
closeClockOutput(output);
output = nullptr;
}
};
try { try {
if (dmaChannel != 0xFF) { if (dmaChannel != 0xFF) {
@ -298,18 +302,11 @@ void Transmitter::transmit(WaveReader &reader, float frequency, float bandwidth,
} }
} catch (std::runtime_error &catched) { } catch (std::runtime_error &catched) {
preserveCarrier = false; preserveCarrier = false;
errorMessage = catched.what(); finally();
errorCatched = true; throw catched;
} }
if (!preserveCarrier) { finally();
closeClockOutput(output);
output = nullptr;
}
if (errorCatched) {
throw std::runtime_error(errorMessage);
}
} }
void Transmitter::transmitViaCpu(WaveReader &reader, uint32_t bufferSize) void Transmitter::transmitViaCpu(WaveReader &reader, uint32_t bufferSize)
@ -322,12 +319,16 @@ void Transmitter::transmitViaCpu(WaveReader &reader, uint32_t bufferSize)
sampleOffset = 0; sampleOffset = 0;
loadedSamples = &samples; loadedSamples = &samples;
bool eof = samples.size() < bufferSize, errorCatched = false; bool eof = samples.size() < bufferSize;
std::thread txThread(Transmitter::transmitThread); std::thread txThread(Transmitter::transmitThread);
std::string errorMessage;
usleep(BUFFER_TIME / 2); usleep(BUFFER_TIME / 2);
auto finally = [&]() {
transmitting = false;
txThread.join();
};
try { try {
while (!eof && transmitting) { while (!eof && transmitting) {
if (loadedSamples == nullptr) { if (loadedSamples == nullptr) {
@ -344,14 +345,11 @@ void Transmitter::transmitViaCpu(WaveReader &reader, uint32_t bufferSize)
usleep(BUFFER_TIME / 2); usleep(BUFFER_TIME / 2);
} }
} catch (std::runtime_error &catched) { } catch (std::runtime_error &catched) {
errorMessage = catched.what(); finally();
errorCatched = true; throw catched;
}
transmitting = false;
txThread.join();
if (errorCatched) {
throw std::runtime_error(errorMessage);
} }
finally();
} }
void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_t dmaChannel) void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_t dmaChannel)
@ -410,11 +408,22 @@ void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_
*pwmFifoData = 0x00000000; *pwmFifoData = 0x00000000;
volatile DMARegisters *dma = startDma(dmaMemory, dmaCb, dmaChannel); volatile DMARegisters *dma = startDma(dmaMemory, dmaCb, dmaChannel);
bool errorCatched = false;
std::string errorMessage;
usleep(BUFFER_TIME / 4); usleep(BUFFER_TIME / 4);
auto finally = [&]() {
dmaCb[(cbOffset < 2 * bufferSize) ? cbOffset : 0].nextCbAddress = 0x00000000;
while (dma->cbAddress != 0x00000000) {
usleep(1000);
}
closeDma(dma);
closePwmController(pwm);
freeMemory(dmaMemory);
transmitting = false;
};
try { try {
while (!eof && transmitting) { while (!eof && transmitting) {
samples = reader.getSamples(bufferSize, transmitting); samples = reader.getSamples(bufferSize, transmitting);
@ -436,24 +445,11 @@ void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_
} }
} }
} catch (std::runtime_error &catched) { } catch (std::runtime_error &catched) {
errorMessage = catched.what(); finally();
errorCatched = true; throw catched;
} }
dmaCb[(cbOffset < 2 * bufferSize) ? cbOffset : 0].nextCbAddress = 0x00000000; finally();
while (dma->cbAddress != 0x00000000) {
usleep(1000);
}
closeDma(dma);
closePwmController(pwm);
freeMemory(dmaMemory);
transmitting = false;
if (errorCatched) {
throw std::runtime_error(errorMessage);
}
} }
void Transmitter::transmitThread() void Transmitter::transmitThread()

Wyświetl plik

@ -142,12 +142,7 @@ std::vector<Sample> WaveReader::getSamples(uint32_t quantity, bool &continueFlag
quantity = bytesToRead / bytesPerSample; quantity = bytesToRead / bytesPerSample;
} }
std::vector<uint8_t> data; std::vector<uint8_t> data = readData(bytesToRead, false, continueFlag);
try {
data = readData(bytesToRead, false, continueFlag);
} catch (std::runtime_error &error) {
throw error;
}
if (data.size() < bytesToRead) { if (data.size() < bytesToRead) {
quantity = data.size() / bytesPerSample; quantity = data.size() / bytesPerSample;
} }