2022-03-03 12:48:56 +03:00
|
|
|
#include "encoder.h"
|
2022-06-01 15:17:21 +03:00
|
|
|
#include "math.h"
|
2022-07-20 13:56:33 +03:00
|
|
|
#include <core/check.h>
|
2022-03-03 12:48:56 +03:00
|
|
|
|
|
|
|
#define TAG "SubGhzBlockEncoder"
|
2022-06-01 15:17:21 +03:00
|
|
|
|
|
|
|
void subghz_protocol_blocks_set_bit_array(
|
|
|
|
bool bit_value,
|
|
|
|
uint8_t data_array[],
|
|
|
|
size_t set_index_bit,
|
|
|
|
size_t max_size_array) {
|
2024-03-25 13:53:32 +03:00
|
|
|
furi_check(set_index_bit < max_size_array * 8);
|
2022-06-01 15:17:21 +03:00
|
|
|
bit_write(data_array[set_index_bit >> 3], 7 - (set_index_bit & 0x7), bit_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_protocol_blocks_get_bit_array(uint8_t data_array[], size_t read_index_bit) {
|
|
|
|
return bit_read(data_array[read_index_bit >> 3], 7 - (read_index_bit & 0x7));
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:48:06 +03:00
|
|
|
size_t subghz_protocol_blocks_get_upload_from_bit_array(
|
2022-06-01 15:17:21 +03:00
|
|
|
uint8_t data_array[],
|
|
|
|
size_t count_bit_data_array,
|
|
|
|
LevelDuration* upload,
|
|
|
|
size_t max_size_upload,
|
2023-02-09 07:48:06 +03:00
|
|
|
uint32_t duration_bit,
|
|
|
|
SubGhzProtocolBlockAlignBit align_bit) {
|
|
|
|
size_t bias_bit = 0;
|
2022-06-01 15:17:21 +03:00
|
|
|
size_t size_upload = 0;
|
|
|
|
uint32_t duration = duration_bit;
|
2023-02-09 07:48:06 +03:00
|
|
|
|
|
|
|
if(align_bit == SubGhzProtocolBlockAlignBitRight) {
|
|
|
|
if(count_bit_data_array & 0x7) {
|
|
|
|
bias_bit = 8 - (count_bit_data_array & 0x7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t index_bit = bias_bit;
|
|
|
|
|
2022-06-01 15:17:21 +03:00
|
|
|
bool last_bit = subghz_protocol_blocks_get_bit_array(data_array, index_bit++);
|
2023-02-09 07:48:06 +03:00
|
|
|
for(size_t i = 1 + bias_bit; i < count_bit_data_array + bias_bit; i++) {
|
2022-06-01 15:17:21 +03:00
|
|
|
if(last_bit == subghz_protocol_blocks_get_bit_array(data_array, index_bit)) {
|
|
|
|
duration += duration_bit;
|
|
|
|
} else {
|
2023-02-09 07:48:06 +03:00
|
|
|
if(size_upload > max_size_upload) {
|
|
|
|
furi_crash("SubGhz: Encoder buffer overflow");
|
|
|
|
}
|
2022-06-01 15:17:21 +03:00
|
|
|
upload[size_upload++] = level_duration_make(
|
|
|
|
subghz_protocol_blocks_get_bit_array(data_array, index_bit - 1), duration);
|
|
|
|
last_bit = !last_bit;
|
|
|
|
duration = duration_bit;
|
|
|
|
}
|
|
|
|
index_bit++;
|
|
|
|
}
|
|
|
|
upload[size_upload++] = level_duration_make(
|
|
|
|
subghz_protocol_blocks_get_bit_array(data_array, index_bit - 1), duration);
|
|
|
|
return size_upload;
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|