2021-03-31 17:52:26 +00:00
|
|
|
#include "subghz_test_packet.h"
|
2021-06-29 21:19:20 +00:00
|
|
|
#include "../subghz_i.h"
|
2021-03-31 17:52:26 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include <api-hal.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
|
|
|
|
static const uint8_t subghz_test_packet_data[] = {
|
|
|
|
0x30, // 48bytes to transmit
|
|
|
|
'F', 'L', 'I', 'P', 'P', 'E', 'R', ' ', 'T', 'E', 'S', 'T', ' ', 'P', 'A', 'C',
|
|
|
|
'K', 'E', 'T', ' ', 'D', 'A', 'T', 'A', ' ', 'A', 'N', 'D', ' ', 'P', 'A', 'D',
|
|
|
|
'F', 'L', 'I', 'P', 'P', 'E', 'R', ' ', 'T', 'E', 'S', 'T', ' ', 'P', 'A', 'C',
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SubghzTestPacket {
|
|
|
|
View* view;
|
|
|
|
osTimerId timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
SubghzTestPacketModelStatusRx,
|
|
|
|
SubghzTestPacketModelStatusTx,
|
|
|
|
} SubghzTestPacketModelStatus;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t frequency;
|
|
|
|
uint32_t real_frequency;
|
|
|
|
ApiHalSubGhzPath path;
|
|
|
|
float rssi;
|
|
|
|
SubghzTestPacketModelStatus status;
|
|
|
|
} SubghzTestPacketModel;
|
|
|
|
|
|
|
|
void subghz_test_packet_draw(Canvas* canvas, SubghzTestPacketModel* model) {
|
|
|
|
char buffer[64];
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
2021-07-04 16:01:16 +00:00
|
|
|
canvas_draw_str(canvas, 0, 8, "CC1101 Packet Test");
|
2021-03-31 17:52:26 +00:00
|
|
|
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
// Frequency
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"Freq: %03ld.%03ld.%03ld Hz",
|
|
|
|
model->real_frequency / 1000000 % 1000,
|
|
|
|
model->real_frequency / 1000 % 1000,
|
|
|
|
model->real_frequency % 1000);
|
2021-07-04 16:01:16 +00:00
|
|
|
canvas_draw_str(canvas, 0, 20, buffer);
|
2021-03-31 17:52:26 +00:00
|
|
|
// Path
|
|
|
|
char* path_name = "Unknown";
|
|
|
|
if(model->path == ApiHalSubGhzPathIsolate) {
|
|
|
|
path_name = "isolate";
|
2021-05-25 10:19:07 +00:00
|
|
|
} else if(model->path == ApiHalSubGhzPath433) {
|
2021-03-31 17:52:26 +00:00
|
|
|
path_name = "433MHz";
|
2021-05-25 10:19:07 +00:00
|
|
|
} else if(model->path == ApiHalSubGhzPath315) {
|
2021-03-31 17:52:26 +00:00
|
|
|
path_name = "315MHz";
|
2021-05-25 10:19:07 +00:00
|
|
|
} else if(model->path == ApiHalSubGhzPath868) {
|
2021-03-31 17:52:26 +00:00
|
|
|
path_name = "868MHz";
|
|
|
|
}
|
|
|
|
snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
|
2021-07-04 16:01:16 +00:00
|
|
|
canvas_draw_str(canvas, 0, 31, buffer);
|
2021-03-31 17:52:26 +00:00
|
|
|
if(model->status == SubghzTestPacketModelStatusRx) {
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"RSSI: %ld.%ld dBm",
|
|
|
|
(int32_t)(model->rssi),
|
|
|
|
(int32_t)fabs(model->rssi * 10) % 10);
|
2021-07-04 16:01:16 +00:00
|
|
|
canvas_draw_str(canvas, 0, 42, buffer);
|
2021-03-31 17:52:26 +00:00
|
|
|
} else {
|
2021-07-04 16:01:16 +00:00
|
|
|
canvas_draw_str(canvas, 0, 42, "TX");
|
2021-03-31 17:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_test_packet_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzTestPacket* subghz_test_packet = context;
|
|
|
|
|
|
|
|
if(event->key == InputKeyBack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
subghz_test_packet->view, (SubghzTestPacketModel * model) {
|
|
|
|
osTimerStop(subghz_test_packet->timer);
|
|
|
|
api_hal_subghz_idle();
|
|
|
|
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyLeft) {
|
|
|
|
if(model->frequency > 0) model->frequency--;
|
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
if(model->path > 0) model->path--;
|
|
|
|
} else if(event->key == InputKeyUp) {
|
2021-05-25 10:19:07 +00:00
|
|
|
if(model->path < ApiHalSubGhzPath868) model->path++;
|
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
if(model->status == SubghzTestPacketModelStatusTx) {
|
|
|
|
model->status = SubghzTestPacketModelStatusRx;
|
|
|
|
} else {
|
|
|
|
model->status = SubghzTestPacketModelStatusTx;
|
|
|
|
}
|
2021-03-31 17:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
model->real_frequency =
|
2021-06-29 21:19:20 +00:00
|
|
|
api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
|
2021-03-31 17:52:26 +00:00
|
|
|
api_hal_subghz_set_path(model->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(model->status == SubghzTestPacketModelStatusRx) {
|
|
|
|
api_hal_subghz_rx();
|
|
|
|
osTimerStart(subghz_test_packet->timer, 1024 / 4);
|
|
|
|
} else {
|
|
|
|
api_hal_subghz_write_packet(
|
|
|
|
subghz_test_packet_data, sizeof(subghz_test_packet_data));
|
|
|
|
api_hal_subghz_tx();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_test_packet_enter(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzTestPacket* subghz_test_packet = context;
|
|
|
|
|
|
|
|
api_hal_subghz_reset();
|
|
|
|
api_hal_subghz_load_preset(ApiHalSubGhzPreset2FskPacket);
|
|
|
|
|
2021-05-08 19:24:05 +00:00
|
|
|
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
2021-03-31 17:52:26 +00:00
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
subghz_test_packet->view, (SubghzTestPacketModel * model) {
|
2021-05-25 10:19:07 +00:00
|
|
|
model->frequency = subghz_frequencies_433_92;
|
2021-03-31 17:52:26 +00:00
|
|
|
model->real_frequency =
|
2021-06-29 21:19:20 +00:00
|
|
|
api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
|
2021-03-31 17:52:26 +00:00
|
|
|
model->path = ApiHalSubGhzPathIsolate; // isolate
|
|
|
|
model->rssi = 0.0f;
|
|
|
|
model->status = SubghzTestPacketModelStatusRx;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
api_hal_subghz_rx();
|
|
|
|
|
|
|
|
osTimerStart(subghz_test_packet->timer, 1024 / 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_test_packet_exit(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzTestPacket* subghz_test_packet = context;
|
|
|
|
|
|
|
|
osTimerStop(subghz_test_packet->timer);
|
|
|
|
|
|
|
|
// Reinitialize IC to default state
|
2021-07-15 13:54:11 +00:00
|
|
|
api_hal_subghz_sleep();
|
2021-03-31 17:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_test_packet_rssi_timer_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzTestPacket* subghz_test_packet = context;
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
subghz_test_packet->view, (SubghzTestPacketModel * model) {
|
|
|
|
model->rssi = api_hal_subghz_get_rssi();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t subghz_test_packet_back(void* context) {
|
|
|
|
return SubGhzViewMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubghzTestPacket* subghz_test_packet_alloc() {
|
|
|
|
SubghzTestPacket* subghz_test_packet = furi_alloc(sizeof(SubghzTestPacket));
|
|
|
|
|
|
|
|
// View allocation and configuration
|
|
|
|
subghz_test_packet->view = view_alloc();
|
|
|
|
view_allocate_model(
|
|
|
|
subghz_test_packet->view, ViewModelTypeLockFree, sizeof(SubghzTestPacketModel));
|
|
|
|
view_set_context(subghz_test_packet->view, subghz_test_packet);
|
|
|
|
view_set_draw_callback(subghz_test_packet->view, (ViewDrawCallback)subghz_test_packet_draw);
|
|
|
|
view_set_input_callback(subghz_test_packet->view, subghz_test_packet_input);
|
|
|
|
view_set_enter_callback(subghz_test_packet->view, subghz_test_packet_enter);
|
|
|
|
view_set_exit_callback(subghz_test_packet->view, subghz_test_packet_exit);
|
|
|
|
view_set_previous_callback(subghz_test_packet->view, subghz_test_packet_back);
|
|
|
|
|
|
|
|
subghz_test_packet->timer = osTimerNew(
|
|
|
|
subghz_test_packet_rssi_timer_callback, osTimerPeriodic, subghz_test_packet, NULL);
|
|
|
|
|
|
|
|
return subghz_test_packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_test_packet_free(SubghzTestPacket* subghz_test_packet) {
|
|
|
|
furi_assert(subghz_test_packet);
|
|
|
|
osTimerDelete(subghz_test_packet->timer);
|
|
|
|
view_free(subghz_test_packet->view);
|
|
|
|
free(subghz_test_packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
View* subghz_test_packet_get_view(SubghzTestPacket* subghz_test_packet) {
|
|
|
|
furi_assert(subghz_test_packet);
|
|
|
|
return subghz_test_packet->view;
|
|
|
|
}
|