/* * Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc. * * This file is part of PortaPack. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */ #ifndef __AUDIO_THREAD_H__ #define __AUDIO_THREAD_H__ #include "ch.h" #include "file.hpp" #include "event_m0.hpp" #include "portapack_shared_memory.hpp" #include "hackrf_gpio.hpp" using namespace hackrf::one; #include class StreamOutput { public: StreamOutput( FIFO* const fifo ) : fifo { fifo } { } size_t available() { return fifo->len(); } size_t read(void* const data, const size_t length) { return fifo->out(reinterpret_cast(data), length); } private: FIFO* const fifo; }; class AudioThread { public: AudioThread( std::string file_path ) : file_path { std::move(file_path) } { // Need significant stack for FATFS thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, AudioThread::static_fn, this); } ~AudioThread() { chThdTerminate(thread); chThdWait(thread); } private: const std::string file_path; File file; static Thread* thread; static msg_t static_fn(void* arg) { auto obj = static_cast(arg); obj->run(); return 0; } void run() { auto write_buffer = std::make_unique>(); if( !write_buffer ) { return; } if( !file.open_for_append(file_path) ) { return; } while( !chThdShouldTerminate() ) { // SUCH A HACK!!! auto fifo = reinterpret_cast*>(shared_memory.FIFO_HACK); if( !fifo ) { break; } StreamOutput stream { fifo }; if( stream.available() >= write_buffer->size() ) { led_usb.on(); const auto bytes_to_write = stream.read(write_buffer->data(), write_buffer->size()); if( bytes_to_write ) { if( !file.write(write_buffer->data(), bytes_to_write) ) { led_tx.on(); break; } } led_usb.off(); } chThdSleepMilliseconds(25); } file.close(); } }; #endif/*__AUDIO_THREAD_H__*/