mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-07-24 07:21:18 -04:00
Don't use raw new/delete (#1398)
* Use unique_ptr in ui_btngrid * Use unique_ptr for ui_menu * Use unique_ptr for rssi_dma * Use unique_ptr for painter
This commit is contained in:
parent
95a48e5693
commit
a476647d70
7 changed files with 61 additions and 71 deletions
|
@ -25,10 +25,12 @@
|
|||
#include "random.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
// TODO move to class members SpectrumPainterProcessor
|
||||
complex16_t* current_line_data = nullptr;
|
||||
complex16_t* volatile next_line_data = nullptr;
|
||||
std::unique_ptr<complex16_t[]> current_line_data;
|
||||
std::unique_ptr<complex16_t[]> next_line_data;
|
||||
uint32_t current_line_index = 0;
|
||||
uint32_t current_line_width = 0;
|
||||
int32_t current_bw = 0;
|
||||
|
@ -41,20 +43,17 @@ int max_val = 127;
|
|||
// This is called at 3072000/2048 = 1500Hz
|
||||
void SpectrumPainterProcessor::execute(const buffer_c8_t& buffer) {
|
||||
for (uint32_t i = 0; i < buffer.count; i++) {
|
||||
if (current_line_data != nullptr) {
|
||||
if (current_line_data) {
|
||||
auto data = current_line_data[(current_line_index++ * current_bw / 3072) % current_line_width];
|
||||
buffer.p[i] = {(int8_t)data.real(), (int8_t)data.imag()};
|
||||
} else
|
||||
buffer.p[i] = {0, 0};
|
||||
}
|
||||
|
||||
// collect new data
|
||||
if (next_line_data != nullptr) {
|
||||
if (current_line_data != nullptr)
|
||||
delete current_line_data;
|
||||
|
||||
current_line_data = next_line_data;
|
||||
next_line_data = nullptr;
|
||||
// Move "next line" into "current line" if set.
|
||||
if (next_line_data) {
|
||||
current_line_data = std::move(next_line_data);
|
||||
next_line_data.reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,7 +64,7 @@ void SpectrumPainterProcessor::run() {
|
|||
init_genrand(22267);
|
||||
|
||||
while (true) {
|
||||
if (fifo.is_empty() == false && next_line_data == nullptr) {
|
||||
if (fifo.is_empty() == false && !next_line_data) {
|
||||
std::vector<uint8_t> data;
|
||||
fifo.out(data);
|
||||
|
||||
|
@ -74,8 +73,9 @@ void SpectrumPainterProcessor::run() {
|
|||
auto fft_width = picture_width * 2;
|
||||
auto qu = fft_width / 4;
|
||||
|
||||
complex16_t* v = new complex16_t[fft_width];
|
||||
complex16_t* tmp = new complex16_t[fft_width];
|
||||
// TODO: can these be statically allocated?
|
||||
auto v = std::make_unique<complex16_t[]>(fft_width);
|
||||
auto tmp = std::make_unique<complex16_t[]>(fft_width);
|
||||
|
||||
for (uint32_t fft_index = 0; fft_index < fft_width; fft_index++) {
|
||||
if (fft_index < qu) {
|
||||
|
@ -103,10 +103,10 @@ void SpectrumPainterProcessor::run() {
|
|||
}
|
||||
}
|
||||
|
||||
ifft<complex16_t>(v, fft_width, tmp);
|
||||
ifft<complex16_t>(v.get(), fft_width, tmp.get());
|
||||
|
||||
// normalize
|
||||
volatile int32_t maximum = 1;
|
||||
int32_t maximum = 1;
|
||||
for (uint32_t i = 0; i < fft_width; i++) {
|
||||
if (v[i].real() > maximum)
|
||||
maximum = v[i].real();
|
||||
|
@ -127,8 +127,7 @@ void SpectrumPainterProcessor::run() {
|
|||
}
|
||||
}
|
||||
|
||||
delete tmp;
|
||||
next_line_data = v;
|
||||
next_line_data = std::move(v);
|
||||
ui++;
|
||||
} else {
|
||||
chThdSleepMilliseconds(1);
|
||||
|
|
|
@ -21,9 +21,10 @@
|
|||
|
||||
#include "rssi_dma.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
#include "hal.h"
|
||||
#include "gpdma.hpp"
|
||||
|
@ -98,8 +99,8 @@ struct buffers_config_t {
|
|||
|
||||
static buffers_config_t buffers_config;
|
||||
|
||||
static sample_t* samples{nullptr};
|
||||
static gpdma::channel::LLI* lli{nullptr};
|
||||
static std::unique_ptr<sample_t[]> samples;
|
||||
static std::unique_ptr<gpdma::channel::LLI[]> lli;
|
||||
|
||||
static ThreadWait thread_wait;
|
||||
|
||||
|
@ -128,8 +129,8 @@ void allocate(size_t buffer_count, size_t items_per_buffer) {
|
|||
const auto peripheral = reinterpret_cast<uint32_t>(&LPC_ADC1->DR[portapack::adc1_rssi_input]) + 1;
|
||||
const auto control_value = control(gpdma::buffer_words(buffers_config.items_per_buffer, 1));
|
||||
|
||||
samples = new sample_t[buffers_config.count * buffers_config.items_per_buffer];
|
||||
lli = new gpdma::channel::LLI[buffers_config.count];
|
||||
samples = std::make_unique<sample_t[]>(buffers_config.count * buffers_config.items_per_buffer);
|
||||
lli = std::make_unique<gpdma::channel::LLI[]>(buffers_config.count);
|
||||
|
||||
for (size_t i = 0; i < buffers_config.count; i++) {
|
||||
const auto memory = reinterpret_cast<uint32_t>(&samples[i * buffers_config.items_per_buffer]);
|
||||
|
@ -141,8 +142,8 @@ void allocate(size_t buffer_count, size_t items_per_buffer) {
|
|||
}
|
||||
|
||||
void free() {
|
||||
delete samples;
|
||||
delete lli;
|
||||
samples.reset();
|
||||
lli.reset();
|
||||
}
|
||||
|
||||
void enable() {
|
||||
|
|
|
@ -97,8 +97,6 @@ void init() {
|
|||
}
|
||||
|
||||
void allocate() {
|
||||
// samples = new sample_t[channel_samples_per_frame];
|
||||
// lli = new gpdma::channel::LLI;
|
||||
lli.srcaddr = reinterpret_cast<uint32_t>(&LPC_ADC0->DR[0]);
|
||||
lli.destaddr = reinterpret_cast<uint32_t>(&shared_memory.touch_adc_frame.dr[0]);
|
||||
lli.lli = lli_pointer(&lli);
|
||||
|
@ -106,8 +104,6 @@ void allocate() {
|
|||
}
|
||||
|
||||
void free() {
|
||||
// delete samples;
|
||||
// delete lli;
|
||||
}
|
||||
|
||||
void enable() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue