portapack-mayhem/firmware/common/message.hpp

518 lines
10 KiB
C++
Raw Normal View History

2015-07-08 15:39:24 +00:00
/*
* Copyright (C) 2015 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 __MESSAGE_H__
#define __MESSAGE_H__
#include <cstdint>
#include <cstddef>
#include <array>
#include <functional>
2016-01-31 08:34:24 +00:00
#include "baseband_packet.hpp"
#include "ert_packet.hpp"
2016-04-07 00:04:40 +00:00
#include "tpms_packet.hpp"
2016-01-31 08:34:24 +00:00
#include "dsp_fir_taps.hpp"
#include "dsp_iir.hpp"
2016-01-31 08:34:24 +00:00
#include "fifo.hpp"
2015-07-08 15:39:24 +00:00
#include "utility.hpp"
#include "ch.h"
2015-07-08 15:39:24 +00:00
class Message {
public:
static constexpr size_t MAX_SIZE = 512;
2016-01-31 08:34:24 +00:00
enum class ID : uint32_t {
2015-07-08 15:39:24 +00:00
/* Assign consecutive IDs. IDs are used to index array. */
RSSIStatistics = 0,
BasebandStatistics = 1,
ChannelStatistics = 2,
DisplayFrameSync = 3,
2016-01-31 08:34:24 +00:00
AudioStatistics = 4,
BasebandConfiguration = 5,
TPMSPacket = 6,
Shutdown = 8,
2016-01-31 08:34:24 +00:00
AISPacket = 7,
ERTPacket = 9,
UpdateSpectrum = 10,
NBFMConfigure = 11,
WFMConfigure = 12,
AMConfigure = 13,
ChannelSpectrumConfig = 14,
SpectrumStreamingConfig = 15,
DisplaySleep = 16,
TXDone = 17,
Retune = 18,
ReadyForSwitch = 19,
AFSKData = 20,
ModuleID = 21,
FIFOSignal = 22,
FIFOData = 23,
2015-07-08 15:39:24 +00:00
MAX
};
constexpr Message(
ID id
) : id { id }
2015-07-08 15:39:24 +00:00
{
}
const ID id;
};
struct RSSIStatistics {
uint32_t accumulator { 0 };
uint32_t min { 0 };
uint32_t max { 0 };
uint32_t count { 0 };
};
class RSSIStatisticsMessage : public Message {
public:
constexpr RSSIStatisticsMessage(
2015-11-11 17:35:28 +00:00
const RSSIStatistics& statistics
2015-07-08 15:39:24 +00:00
) : Message { ID::RSSIStatistics },
2015-11-11 17:35:28 +00:00
statistics { statistics }
2015-07-08 15:39:24 +00:00
{
}
RSSIStatistics statistics;
};
struct BasebandStatistics {
uint32_t idle_ticks { 0 };
uint32_t main_ticks { 0 };
uint32_t rssi_ticks { 0 };
2015-07-08 15:39:24 +00:00
uint32_t baseband_ticks { 0 };
bool saturation { false };
};
class BasebandStatisticsMessage : public Message {
public:
constexpr BasebandStatisticsMessage(
2015-11-11 17:35:28 +00:00
const BasebandStatistics& statistics
2015-07-08 15:39:24 +00:00
) : Message { ID::BasebandStatistics },
2015-11-11 17:35:28 +00:00
statistics { statistics }
2015-07-08 15:39:24 +00:00
{
}
BasebandStatistics statistics;
};
struct ChannelStatistics {
int32_t max_db;
size_t count;
constexpr ChannelStatistics(
2015-11-11 17:16:20 +00:00
int32_t max_db = -120,
size_t count = 0
2015-07-08 15:39:24 +00:00
) : max_db { max_db },
count { count }
{
}
};
class ChannelStatisticsMessage : public Message {
public:
constexpr ChannelStatisticsMessage(
2016-01-31 08:34:24 +00:00
const ChannelStatistics& statistics
) : Message { ID::ChannelStatistics },
statistics { statistics }
2015-07-08 15:39:24 +00:00
{
}
ChannelStatistics statistics;
};
2016-01-31 08:34:24 +00:00
class DisplayFrameSyncMessage : public Message {
public:
constexpr DisplayFrameSyncMessage(
) : Message { ID::DisplayFrameSync }
{
}
};
2015-07-08 15:39:24 +00:00
struct AudioStatistics {
int32_t rms_db;
int32_t max_db;
size_t count;
constexpr AudioStatistics(
) : rms_db { -120 },
max_db { -120 },
count { 0 }
{
}
constexpr AudioStatistics(
int32_t rms_db,
int32_t max_db,
size_t count
) : rms_db { rms_db },
max_db { max_db },
count { count }
{
}
};
2016-01-31 08:34:24 +00:00
class DisplaySleepMessage : public Message {
public:
constexpr DisplaySleepMessage(
) : Message { ID::DisplaySleep }
{
}
};
2015-07-08 15:39:24 +00:00
class AudioStatisticsMessage : public Message {
public:
constexpr AudioStatisticsMessage(
2016-01-31 08:34:24 +00:00
const AudioStatistics& statistics
2015-07-08 15:39:24 +00:00
) : Message { ID::AudioStatistics },
2016-01-31 08:34:24 +00:00
statistics { statistics }
2015-07-08 15:39:24 +00:00
{
}
AudioStatistics statistics;
};
struct BasebandConfiguration {
2016-01-31 08:34:24 +00:00
int32_t mode;
2015-07-08 15:39:24 +00:00
uint32_t sampling_rate;
size_t decimation_factor;
2015-11-11 18:30:35 +00:00
constexpr BasebandConfiguration(
2016-01-31 08:34:24 +00:00
int32_t mode,
uint32_t sampling_rate,
2015-11-11 18:30:35 +00:00
size_t decimation_factor = 1
) : mode { mode },
sampling_rate { sampling_rate },
decimation_factor { decimation_factor }
{
}
2016-01-31 08:34:24 +00:00
constexpr BasebandConfiguration(
) : BasebandConfiguration { -1, 0, 1 }
{
}
2015-07-08 15:39:24 +00:00
};
class BasebandConfigurationMessage : public Message {
public:
constexpr BasebandConfigurationMessage(
2016-01-31 08:34:24 +00:00
const BasebandConfiguration& configuration
2015-07-08 15:39:24 +00:00
) : Message { ID::BasebandConfiguration },
2016-01-31 08:34:24 +00:00
configuration { configuration }
2015-07-08 15:39:24 +00:00
{
}
BasebandConfiguration configuration;
};
2016-01-31 08:34:24 +00:00
class SpectrumStreamingConfigMessage : public Message {
public:
enum class Mode : uint32_t {
Stopped = 0,
Running = 1,
};
constexpr SpectrumStreamingConfigMessage(
Mode mode
) : Message { ID::SpectrumStreamingConfig },
mode { mode }
{
}
Mode mode { Mode::Stopped };
};
2015-07-08 15:39:24 +00:00
struct ChannelSpectrum {
std::array<uint8_t, 256> db { { 0 } };
uint32_t sampling_rate { 0 };
uint32_t channel_filter_pass_frequency { 0 };
uint32_t channel_filter_stop_frequency { 0 };
2015-07-08 15:39:24 +00:00
};
using ChannelSpectrumFIFO = FIFO<ChannelSpectrum>;
2016-01-31 08:34:24 +00:00
class ChannelSpectrumConfigMessage : public Message {
2015-07-08 15:39:24 +00:00
public:
static constexpr size_t fifo_k = 2;
2016-01-31 08:34:24 +00:00
constexpr ChannelSpectrumConfigMessage(
ChannelSpectrumFIFO* fifo
) : Message { ID::ChannelSpectrumConfig },
fifo { fifo }
2015-07-08 15:39:24 +00:00
{
}
2016-01-31 08:34:24 +00:00
ChannelSpectrumFIFO* fifo { nullptr };
2015-07-08 15:39:24 +00:00
};
2016-01-31 08:34:24 +00:00
class AISPacketMessage : public Message {
public:
constexpr AISPacketMessage(
const baseband::Packet& packet
) : Message { ID::AISPacket },
packet { packet }
{
}
2015-07-08 15:39:24 +00:00
2016-01-31 08:34:24 +00:00
baseband::Packet packet;
2015-07-08 15:39:24 +00:00
};
2016-01-31 08:34:24 +00:00
class TPMSPacketMessage : public Message {
2015-07-08 15:39:24 +00:00
public:
2016-01-31 08:34:24 +00:00
constexpr TPMSPacketMessage(
2016-04-07 00:04:40 +00:00
const tpms::SignalType signal_type,
2016-01-31 08:34:24 +00:00
const baseband::Packet& packet
) : Message { ID::TPMSPacket },
2016-04-07 00:04:40 +00:00
signal_type { signal_type },
2016-01-31 08:34:24 +00:00
packet { packet }
2015-07-08 15:39:24 +00:00
{
}
2016-04-07 00:04:40 +00:00
tpms::SignalType signal_type;
2016-01-31 08:34:24 +00:00
baseband::Packet packet;
2015-07-08 15:39:24 +00:00
};
2016-01-31 08:34:24 +00:00
class ShutdownMessage : public Message {
public:
constexpr ShutdownMessage(
) : Message { ID::Shutdown }
{
}
};
2015-07-08 15:39:24 +00:00
2016-01-31 08:34:24 +00:00
class ERTPacketMessage : public Message {
public:
2016-01-31 08:34:24 +00:00
constexpr ERTPacketMessage(
const ert::Packet::Type type,
const baseband::Packet& packet
) : Message { ID::ERTPacket },
type { type },
packet { packet }
{
}
2016-01-31 08:34:24 +00:00
ert::Packet::Type type;
baseband::Packet packet;
};
2015-07-08 15:39:24 +00:00
2016-01-31 08:34:24 +00:00
class UpdateSpectrumMessage : public Message {
public:
2016-01-31 08:34:24 +00:00
constexpr UpdateSpectrumMessage(
) : Message { ID::UpdateSpectrum }
{
}
2016-01-31 08:34:24 +00:00
};
2016-01-31 08:34:24 +00:00
class NBFMConfigureMessage : public Message {
public:
constexpr NBFMConfigureMessage(
const fir_taps_real<24> decim_0_filter,
const fir_taps_real<32> decim_1_filter,
const fir_taps_real<32> channel_filter,
const size_t channel_decimation,
const size_t deviation,
const iir_biquad_config_t audio_hpf_config,
const iir_biquad_config_t audio_deemph_config
2016-01-31 08:34:24 +00:00
) : Message { ID::NBFMConfigure },
decim_0_filter(decim_0_filter),
decim_1_filter(decim_1_filter),
channel_filter(channel_filter),
channel_decimation { channel_decimation },
deviation { deviation },
audio_hpf_config(audio_hpf_config),
audio_deemph_config(audio_deemph_config)
2016-01-31 08:34:24 +00:00
{
}
const fir_taps_real<24> decim_0_filter;
const fir_taps_real<32> decim_1_filter;
const fir_taps_real<32> channel_filter;
const size_t channel_decimation;
2016-01-31 08:34:24 +00:00
const size_t deviation;
const iir_biquad_config_t audio_hpf_config;
const iir_biquad_config_t audio_deemph_config;
};
2016-01-31 08:34:24 +00:00
class WFMConfigureMessage : public Message {
public:
2016-01-31 08:34:24 +00:00
constexpr WFMConfigureMessage(
const fir_taps_real<24> decim_0_filter,
const fir_taps_real<16> decim_1_filter,
const fir_taps_real<64> audio_filter,
const size_t deviation,
const iir_biquad_config_t audio_hpf_config,
const iir_biquad_config_t audio_deemph_config
2016-01-31 08:34:24 +00:00
) : Message { ID::WFMConfigure },
decim_0_filter(decim_0_filter),
decim_1_filter(decim_1_filter),
audio_filter(audio_filter),
deviation { deviation },
audio_hpf_config(audio_hpf_config),
audio_deemph_config(audio_deemph_config)
2015-07-08 15:39:24 +00:00
{
}
2016-01-31 08:34:24 +00:00
const fir_taps_real<24> decim_0_filter;
const fir_taps_real<16> decim_1_filter;
const fir_taps_real<64> audio_filter;
const size_t deviation;
const iir_biquad_config_t audio_hpf_config;
const iir_biquad_config_t audio_deemph_config;
2015-07-08 15:39:24 +00:00
};
2016-01-31 08:34:24 +00:00
class AMConfigureMessage : public Message {
2015-07-08 15:39:24 +00:00
public:
enum class Modulation : int32_t {
2016-01-31 02:17:29 +00:00
DSB = 0,
SSB = 1,
};
2016-01-31 08:34:24 +00:00
constexpr AMConfigureMessage(
const fir_taps_real<24> decim_0_filter,
const fir_taps_real<32> decim_1_filter,
2016-01-31 01:28:11 +00:00
const fir_taps_real<32> decim_2_filter,
const fir_taps_complex<64> channel_filter,
const Modulation modulation,
const iir_biquad_config_t audio_hpf_config
2016-01-31 08:34:24 +00:00
) : Message { ID::AMConfigure },
decim_0_filter(decim_0_filter),
decim_1_filter(decim_1_filter),
2016-01-31 01:28:11 +00:00
decim_2_filter(decim_2_filter),
channel_filter(channel_filter),
modulation { modulation },
audio_hpf_config(audio_hpf_config)
2015-07-08 15:39:24 +00:00
{
}
2016-01-31 08:34:24 +00:00
const fir_taps_real<24> decim_0_filter;
const fir_taps_real<32> decim_1_filter;
2016-01-31 01:28:11 +00:00
const fir_taps_real<32> decim_2_filter;
const fir_taps_complex<64> channel_filter;
const Modulation modulation;
const iir_biquad_config_t audio_hpf_config;
2015-07-08 15:39:24 +00:00
};
class TXDoneMessage : public Message {
public:
TXDoneMessage(
) : Message { ID::TXDone }
{
}
int n = 0;
};
class ModuleIDMessage : public Message {
public:
ModuleIDMessage(
) : Message { ID::ModuleID }
{
}
bool query;
char md5_signature[16];
};
class ReadyForSwitchMessage : public Message {
public:
ReadyForSwitchMessage(
) : Message { ID::ReadyForSwitch }
{
}
};
class RetuneMessage : public Message {
public:
RetuneMessage(
) : Message { ID::Retune }
{
}
int64_t freq = 0;
};
class AFSKDataMessage : public Message {
public:
constexpr AFSKDataMessage(
) : Message { ID::AFSKData }
{
}
int16_t data[128] = {0};
};
class FIFOSignalMessage : public Message {
public:
FIFOSignalMessage(
) : Message { ID::FIFOSignal }
{
}
char signaltype = 0;
};
class FIFODataMessage : public Message {
public:
FIFODataMessage(
) : Message { ID::FIFOData }
{
}
int8_t * data;
};
2015-07-08 15:39:24 +00:00
class MessageHandlerMap {
public:
using MessageHandler = std::function<void(Message* const p)>;
2015-07-08 15:39:24 +00:00
void register_handler(const Message::ID id, MessageHandler&& handler) {
if( map_[toUType(id)] != nullptr ) {
chDbgPanic("MsgDblReg");
}
map_[toUType(id)] = std::move(handler);
2015-07-08 15:39:24 +00:00
}
void unregister_handler(const Message::ID id) {
map_[toUType(id)] = nullptr;
2015-07-08 15:39:24 +00:00
}
void send(Message* const message) {
if( message->id < Message::ID::MAX ) {
auto& fn = map_[toUType(message->id)];
if( fn ) {
fn(message);
}
}
2015-07-08 15:39:24 +00:00
}
private:
using MapType = std::array<MessageHandler, toUType(Message::ID::MAX)>;
MapType map_;
};
#endif/*__MESSAGE_H__*/