mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-14 03:37:40 +00:00
Formatted code (#1007)
* Updated style * Updated files * fixed new line * Updated spacing * File fix WIP * Updated to clang 13 * updated comment style * Removed old comment code
This commit is contained in:
@@ -27,257 +27,231 @@
|
||||
#include "event_m4.hpp"
|
||||
|
||||
void NRFRxProcessor::execute(const buffer_c8_t& buffer) {
|
||||
if (!configured) return;
|
||||
|
||||
// FM demodulation
|
||||
if (!configured) return;
|
||||
|
||||
const auto decim_0_out = decim_0.execute(buffer, dst_buffer);
|
||||
feed_channel_stats(decim_0_out);
|
||||
|
||||
auto audio_oversampled = demod.execute(decim_0_out, work_audio_buffer);
|
||||
// Audio signal processing
|
||||
for (size_t c = 0; c < audio_oversampled.count; c++) {
|
||||
int g_srate = 4; //4 for 250KPS
|
||||
//int g_srate = 1; //1 for 1MPS, not working yet
|
||||
int32_t current_sample = audio_oversampled.p[c]; //if I directly use this, some results can pass crc but not correct.
|
||||
rb_head++;
|
||||
rb_head=(rb_head)%RB_SIZE;
|
||||
// FM demodulation
|
||||
|
||||
rb_buf[rb_head] = current_sample;
|
||||
const auto decim_0_out = decim_0.execute(buffer, dst_buffer);
|
||||
feed_channel_stats(decim_0_out);
|
||||
|
||||
skipSamples = skipSamples - 1;
|
||||
auto audio_oversampled = demod.execute(decim_0_out, work_audio_buffer);
|
||||
// Audio signal processing
|
||||
for (size_t c = 0; c < audio_oversampled.count; c++) {
|
||||
int g_srate = 4; // 4 for 250KPS
|
||||
// int g_srate = 1; //1 for 1MPS, not working yet
|
||||
int32_t current_sample = audio_oversampled.p[c]; // if I directly use this, some results can pass crc but not correct.
|
||||
rb_head++;
|
||||
rb_head = (rb_head) % RB_SIZE;
|
||||
|
||||
if (skipSamples<1)
|
||||
{
|
||||
int32_t threshold_tmp=0;
|
||||
for (int c=0;c<8*g_srate;c++)
|
||||
{
|
||||
threshold_tmp = threshold_tmp + (int32_t)rb_buf[(rb_head+c)%RB_SIZE];
|
||||
}
|
||||
rb_buf[rb_head] = current_sample;
|
||||
|
||||
g_threshold = (int32_t)threshold_tmp/(8*g_srate);
|
||||
|
||||
int transitions=0;
|
||||
if (rb_buf[(rb_head + 9*g_srate)%RB_SIZE] > g_threshold)
|
||||
{
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head + c*g_srate)%RB_SIZE] > rb_buf[(rb_head + (c+1)*g_srate)%RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head + c*g_srate)%RB_SIZE] < rb_buf[(rb_head + (c+1)*g_srate)%RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool packet_detected=false;
|
||||
//if ( transitions==4 && abs(g_threshold)<15500)
|
||||
if ( transitions==4 && abs(g_threshold)<15500)
|
||||
{
|
||||
int packet_length = 0;
|
||||
uint8_t tmp_buf[10];
|
||||
uint8_t packet_data[500];
|
||||
uint8_t packet_packed[50];
|
||||
uint16_t pcf;
|
||||
uint32_t packet_crc;
|
||||
uint32_t calced_crc;
|
||||
uint64_t packet_addr_l;
|
||||
skipSamples = skipSamples - 1;
|
||||
|
||||
/* extract address */
|
||||
packet_addr_l=0;
|
||||
if (skipSamples < 1) {
|
||||
int32_t threshold_tmp = 0;
|
||||
for (int c = 0; c < 8 * g_srate; c++) {
|
||||
threshold_tmp = threshold_tmp + (int32_t)rb_buf[(rb_head + c) % RB_SIZE];
|
||||
}
|
||||
|
||||
for (int t=0;t<5;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(1*8+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
g_threshold = (int32_t)threshold_tmp / (8 * g_srate);
|
||||
|
||||
for (int t=0;t<5;t++) packet_addr_l|=((uint64_t)tmp_buf[t])<<(4-t)*8;
|
||||
int transitions = 0;
|
||||
if (rb_buf[(rb_head + 9 * g_srate) % RB_SIZE] > g_threshold) {
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + c * g_srate) % RB_SIZE] > rb_buf[(rb_head + (c + 1) * g_srate) % RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
} else {
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + c * g_srate) % RB_SIZE] < rb_buf[(rb_head + (c + 1) * g_srate) % RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//channel_number = 26;
|
||||
bool packet_detected = false;
|
||||
// if ( transitions==4 && abs(g_threshold)<15500)
|
||||
if (transitions == 4 && abs(g_threshold) < 15500) {
|
||||
int packet_length = 0;
|
||||
uint8_t tmp_buf[10];
|
||||
uint8_t packet_data[500];
|
||||
uint8_t packet_packed[50];
|
||||
uint16_t pcf;
|
||||
uint32_t packet_crc;
|
||||
uint32_t calced_crc;
|
||||
uint64_t packet_addr_l;
|
||||
|
||||
|
||||
/* extract pcf */
|
||||
for (int t=0;t<2;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(6*8+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
/* extract address */
|
||||
packet_addr_l = 0;
|
||||
|
||||
pcf = tmp_buf[0]<<8 | tmp_buf[1];
|
||||
pcf >>=7;
|
||||
for (int t = 0; t < 5; t++) {
|
||||
bool current_bit;
|
||||
uint8_t byte = 0;
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + (1 * 8 + t * 8 + c) * g_srate) % RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7 - c);
|
||||
}
|
||||
tmp_buf[t] = byte;
|
||||
}
|
||||
|
||||
/* extract packet length, avoid excessive length packets */
|
||||
if(packet_length == 0)
|
||||
packet_length=(int)pcf>>3;
|
||||
if (packet_length>32)
|
||||
packet_detected = false;
|
||||
for (int t = 0; t < 5; t++) packet_addr_l |= ((uint64_t)tmp_buf[t]) << (4 - t) * 8;
|
||||
|
||||
/* extract data */
|
||||
for (int t=0;t<packet_length;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(6*8+9+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
packet_data[t]=byte;
|
||||
}
|
||||
// channel_number = 26;
|
||||
|
||||
/* Prepare packed bit stream for CRC calculation */
|
||||
uint64_t packet_header=packet_addr_l;
|
||||
packet_header<<=9;
|
||||
packet_header|=pcf;
|
||||
for (int c=0;c<7;c++){
|
||||
packet_packed[c]=(packet_header>>((6-c)*8))&0xFF;
|
||||
}
|
||||
/* extract pcf */
|
||||
for (int t = 0; t < 2; t++) {
|
||||
bool current_bit;
|
||||
uint8_t byte = 0;
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + (6 * 8 + t * 8 + c) * g_srate) % RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7 - c);
|
||||
}
|
||||
tmp_buf[t] = byte;
|
||||
}
|
||||
|
||||
for (int c=0;c<packet_length;c++){
|
||||
packet_packed[c+7]=packet_data[c];
|
||||
}
|
||||
pcf = tmp_buf[0] << 8 | tmp_buf[1];
|
||||
pcf >>= 7;
|
||||
|
||||
/* calculate packet crc */
|
||||
const uint8_t* data = packet_packed;
|
||||
size_t data_len = 7+packet_length;
|
||||
bool bit;
|
||||
uint8_t cc;
|
||||
uint_fast16_t crc=0x3C18;
|
||||
while (data_len--) {
|
||||
cc = *data++;
|
||||
for (uint8_t i = 0x80; i > 0; i >>= 1)
|
||||
{
|
||||
bit = crc & 0x8000;
|
||||
if (cc & i)
|
||||
{
|
||||
bit = !bit;
|
||||
}
|
||||
crc <<= 1;
|
||||
if (bit)
|
||||
{
|
||||
crc ^= 0x1021;
|
||||
}
|
||||
}
|
||||
crc &= 0xffff;
|
||||
}
|
||||
calced_crc = (uint16_t)(crc & 0xffff);
|
||||
/* extract packet length, avoid excessive length packets */
|
||||
if (packet_length == 0)
|
||||
packet_length = (int)pcf >> 3;
|
||||
if (packet_length > 32)
|
||||
packet_detected = false;
|
||||
|
||||
/* extract crc */
|
||||
for (int t=0;t<2;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+((6+packet_length)*8+9+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
packet_crc = tmp_buf[0]<<8 | tmp_buf[1];
|
||||
/* extract data */
|
||||
for (int t = 0; t < packet_length; t++) {
|
||||
bool current_bit;
|
||||
uint8_t byte = 0;
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + (6 * 8 + 9 + t * 8 + c) * g_srate) % RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7 - c);
|
||||
}
|
||||
packet_data[t] = byte;
|
||||
}
|
||||
|
||||
/* NRF24L01+ packet found, dump information */
|
||||
//if (packet_addr_l==0xE7E7E7E7)
|
||||
if (packet_crc==calced_crc)
|
||||
{
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'A';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
/* Prepare packed bit stream for CRC calculation */
|
||||
uint64_t packet_header = packet_addr_l;
|
||||
packet_header <<= 9;
|
||||
packet_header |= pcf;
|
||||
for (int c = 0; c < 7; c++) {
|
||||
packet_packed[c] = (packet_header >> ((6 - c) * 8)) & 0xFF;
|
||||
}
|
||||
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
for (int c = 0; c < packet_length; c++) {
|
||||
packet_packed[c + 7] = packet_data[c];
|
||||
}
|
||||
|
||||
for (int c=0;c<7;c++)
|
||||
{
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
/*data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
/* calculate packet crc */
|
||||
const uint8_t* data = packet_packed;
|
||||
size_t data_len = 7 + packet_length;
|
||||
bool bit;
|
||||
uint8_t cc;
|
||||
uint_fast16_t crc = 0x3C18;
|
||||
while (data_len--) {
|
||||
cc = *data++;
|
||||
for (uint8_t i = 0x80; i > 0; i >>= 1) {
|
||||
bit = crc & 0x8000;
|
||||
if (cc & i) {
|
||||
bit = !bit;
|
||||
}
|
||||
crc <<= 1;
|
||||
if (bit) {
|
||||
crc ^= 0x1021;
|
||||
}
|
||||
}
|
||||
crc &= 0xffff;
|
||||
}
|
||||
calced_crc = (uint16_t)(crc & 0xffff);
|
||||
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);*/
|
||||
/* extract crc */
|
||||
for (int t = 0; t < 2; t++) {
|
||||
bool current_bit;
|
||||
uint8_t byte = 0;
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (rb_buf[(rb_head + ((6 + packet_length) * 8 + 9 + t * 8 + c) * g_srate) % RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7 - c);
|
||||
}
|
||||
tmp_buf[t] = byte;
|
||||
}
|
||||
packet_crc = tmp_buf[0] << 8 | tmp_buf[1];
|
||||
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'B';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
/* NRF24L01+ packet found, dump information */
|
||||
// if (packet_addr_l==0xE7E7E7E7)
|
||||
if (packet_crc == calced_crc) {
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'A';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
for (int c=0;c<packet_length;c++)
|
||||
{
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_data[c];
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'C';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
for (int c = 0; c < 7; c++) {
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
/*data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);*/
|
||||
|
||||
packet_detected = true;
|
||||
}
|
||||
else
|
||||
packet_detected = false;
|
||||
}
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'B';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
if (packet_detected)
|
||||
{
|
||||
skipSamples=20;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int c = 0; c < packet_length; c++) {
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_data[c];
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'C';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
packet_detected = true;
|
||||
} else
|
||||
packet_detected = false;
|
||||
}
|
||||
|
||||
if (packet_detected) {
|
||||
skipSamples = 20;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NRFRxProcessor::on_message(const Message* const message) {
|
||||
if (message->id == Message::ID::NRFRxConfigure)
|
||||
configure(*reinterpret_cast<const NRFRxConfigureMessage*>(message));
|
||||
if (message->id == Message::ID::NRFRxConfigure)
|
||||
configure(*reinterpret_cast<const NRFRxConfigureMessage*>(message));
|
||||
}
|
||||
|
||||
void NRFRxProcessor::configure(const NRFRxConfigureMessage& message) {
|
||||
(void)message; //avoir unused warning
|
||||
decim_0.configure(taps_200k_wfm_decim_0.taps, 33554432);
|
||||
decim_1.configure(taps_200k_wfm_decim_1.taps, 131072);
|
||||
demod.configure(audio_fs, 5000);
|
||||
void NRFRxProcessor::configure(const NRFRxConfigureMessage& message) {
|
||||
(void)message; // avoir unused warning
|
||||
decim_0.configure(taps_200k_wfm_decim_0.taps, 33554432);
|
||||
decim_1.configure(taps_200k_wfm_decim_1.taps, 131072);
|
||||
demod.configure(audio_fs, 5000);
|
||||
|
||||
configured = true;
|
||||
configured = true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
EventDispatcher event_dispatcher { std::make_unique<NRFRxProcessor>() };
|
||||
event_dispatcher.run();
|
||||
return 0;
|
||||
EventDispatcher event_dispatcher{std::make_unique<NRFRxProcessor>()};
|
||||
event_dispatcher.run();
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user