2021-04-15 11:47:52 +03:00
|
|
|
#include "subghz_static.h"
|
|
|
|
#include "subghz_i.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include <api-hal.h>
|
|
|
|
#include <input/input.h>
|
2021-05-24 16:44:14 +03:00
|
|
|
#include <notification/notification-messages.h>
|
2021-04-15 11:47:52 +03:00
|
|
|
|
|
|
|
static const uint8_t subghz_static_keys[][4] = {
|
2021-04-27 19:00:03 +03:00
|
|
|
{0x74, 0xBA, 0xDE},
|
|
|
|
{0x74, 0xBA, 0xDD},
|
|
|
|
{0x74, 0xBA, 0xDB},
|
|
|
|
{0xE3, 0x4A, 0x4E},
|
2021-04-15 11:47:52 +03:00
|
|
|
};
|
|
|
|
|
2021-04-27 19:00:03 +03:00
|
|
|
#define SUBGHZ_PT_ONE 376
|
|
|
|
#define SUBGHZ_PT_ZERO (SUBGHZ_PT_ONE * 3)
|
|
|
|
#define SUBGHZ_PT_GUARD 10600
|
|
|
|
|
2021-04-15 11:47:52 +03:00
|
|
|
struct SubghzStatic {
|
|
|
|
View* view;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
SubghzStaticStatusRx,
|
|
|
|
SubghzStaticStatusTx,
|
|
|
|
} SubghzStaticStatus;
|
|
|
|
|
|
|
|
typedef struct {
|
2021-04-19 19:46:00 +03:00
|
|
|
uint8_t frequency;
|
2021-04-15 11:47:52 +03:00
|
|
|
uint32_t real_frequency;
|
|
|
|
ApiHalSubGhzPath path;
|
|
|
|
uint8_t button;
|
|
|
|
} SubghzStaticModel;
|
|
|
|
|
|
|
|
void subghz_static_draw(Canvas* canvas, SubghzStaticModel* model) {
|
|
|
|
char buffer[64];
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
canvas_draw_str(canvas, 2, 12, "CC1101 Static");
|
|
|
|
|
|
|
|
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);
|
|
|
|
canvas_draw_str(canvas, 2, 24, buffer);
|
|
|
|
// Path
|
|
|
|
char* path_name = "Unknown";
|
|
|
|
if(model->path == ApiHalSubGhzPathIsolate) {
|
|
|
|
path_name = "isolate";
|
2021-05-25 13:19:07 +03:00
|
|
|
} else if(model->path == ApiHalSubGhzPath433) {
|
2021-04-15 11:47:52 +03:00
|
|
|
path_name = "433MHz";
|
2021-05-25 13:19:07 +03:00
|
|
|
} else if(model->path == ApiHalSubGhzPath315) {
|
2021-04-15 11:47:52 +03:00
|
|
|
path_name = "315MHz";
|
2021-05-25 13:19:07 +03:00
|
|
|
} else if(model->path == ApiHalSubGhzPath868) {
|
2021-04-15 11:47:52 +03:00
|
|
|
path_name = "868MHz";
|
|
|
|
}
|
|
|
|
snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
|
|
|
|
canvas_draw_str(canvas, 2, 36, buffer);
|
|
|
|
snprintf(buffer, sizeof(buffer), "Key: %d", model->button);
|
|
|
|
canvas_draw_str(canvas, 2, 48, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_static_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzStatic* subghz_static = context;
|
|
|
|
|
|
|
|
if(event->key == InputKeyBack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
subghz_static->view, (SubghzStaticModel * model) {
|
2021-04-19 19:46:00 +03:00
|
|
|
bool reconfigure = false;
|
2021-04-15 11:47:52 +03:00
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyLeft) {
|
2021-04-19 19:46:00 +03:00
|
|
|
if(model->frequency > 0) model->frequency--;
|
|
|
|
reconfigure = true;
|
2021-04-15 11:47:52 +03:00
|
|
|
} else if(event->key == InputKeyRight) {
|
2021-04-19 19:46:00 +03:00
|
|
|
if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
|
|
|
|
reconfigure = true;
|
2021-04-15 11:47:52 +03:00
|
|
|
} else if(event->key == InputKeyDown) {
|
2021-04-19 19:46:00 +03:00
|
|
|
if(model->button > 0) model->button--;
|
2021-04-15 11:47:52 +03:00
|
|
|
} else if(event->key == InputKeyUp) {
|
2021-04-27 19:00:03 +03:00
|
|
|
if(model->button < 3) model->button++;
|
2021-04-15 11:47:52 +03:00
|
|
|
}
|
2021-05-25 13:19:07 +03:00
|
|
|
model->path = subghz_frequencies[model->frequency].path;
|
2021-04-19 19:46:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(reconfigure) {
|
|
|
|
api_hal_subghz_idle();
|
|
|
|
model->real_frequency =
|
2021-05-25 13:19:07 +03:00
|
|
|
api_hal_subghz_set_frequency(subghz_frequencies[model->frequency].frequency);
|
2021-04-15 11:47:52 +03:00
|
|
|
api_hal_subghz_set_path(model->path);
|
2021-04-19 19:46:00 +03:00
|
|
|
api_hal_subghz_tx();
|
2021-04-15 11:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(event->key == InputKeyOk) {
|
|
|
|
if(event->type == InputTypePress) {
|
|
|
|
const uint8_t* key = subghz_static_keys[model->button];
|
|
|
|
|
2021-05-24 16:44:14 +03:00
|
|
|
NotificationApp* notification = furi_record_open("notification");
|
|
|
|
notification_message_block(notification, &sequence_set_red_255);
|
2021-04-15 11:47:52 +03:00
|
|
|
__disable_irq();
|
2021-04-27 19:00:03 +03:00
|
|
|
for(uint8_t r = 0; r < 20; r++) {
|
|
|
|
//Payload
|
|
|
|
for(uint8_t i = 0; i < 24; i++) {
|
2021-04-15 11:47:52 +03:00
|
|
|
uint8_t byte = i / 8;
|
|
|
|
uint8_t bit = i % 8;
|
|
|
|
bool value = (key[byte] >> (7 - bit)) & 1;
|
2021-04-27 19:00:03 +03:00
|
|
|
// Payload send
|
2021-05-08 22:24:05 +03:00
|
|
|
hal_gpio_write(&gpio_cc1101_g0, false);
|
2021-04-27 19:00:03 +03:00
|
|
|
delay_us(value ? SUBGHZ_PT_ONE : SUBGHZ_PT_ZERO);
|
2021-05-08 22:24:05 +03:00
|
|
|
hal_gpio_write(&gpio_cc1101_g0, true);
|
2021-04-27 19:00:03 +03:00
|
|
|
delay_us(value ? SUBGHZ_PT_ZERO : SUBGHZ_PT_ONE);
|
2021-04-15 11:47:52 +03:00
|
|
|
}
|
2021-04-27 19:00:03 +03:00
|
|
|
// Last bit
|
2021-05-08 22:24:05 +03:00
|
|
|
hal_gpio_write(&gpio_cc1101_g0, false);
|
2021-04-27 19:00:03 +03:00
|
|
|
delay_us(SUBGHZ_PT_ONE);
|
2021-05-08 22:24:05 +03:00
|
|
|
hal_gpio_write(&gpio_cc1101_g0, true);
|
2021-04-27 19:00:03 +03:00
|
|
|
// Guard time
|
|
|
|
delay_us(10600);
|
2021-04-15 11:47:52 +03:00
|
|
|
}
|
|
|
|
__enable_irq();
|
2021-05-24 16:44:14 +03:00
|
|
|
notification_message(notification, &sequence_reset_red);
|
|
|
|
furi_record_close("notification");
|
2021-04-15 11:47:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_static_enter(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubghzStatic* subghz_static = context;
|
|
|
|
|
|
|
|
api_hal_subghz_reset();
|
|
|
|
api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync);
|
|
|
|
|
2021-05-08 22:24:05 +03:00
|
|
|
hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
hal_gpio_write(&gpio_cc1101_g0, true);
|
2021-04-15 11:47:52 +03:00
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
subghz_static->view, (SubghzStaticModel * model) {
|
2021-05-25 13:19:07 +03:00
|
|
|
model->frequency = subghz_frequencies_433_92;
|
2021-04-19 19:46:00 +03:00
|
|
|
model->real_frequency =
|
2021-05-25 13:19:07 +03:00
|
|
|
api_hal_subghz_set_frequency(subghz_frequencies[model->frequency].frequency);
|
|
|
|
model->path = subghz_frequencies[model->frequency].path;
|
2021-04-15 11:47:52 +03:00
|
|
|
model->button = 0;
|
2021-04-19 19:46:00 +03:00
|
|
|
api_hal_subghz_set_path(model->path);
|
2021-04-15 11:47:52 +03:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
api_hal_subghz_tx();
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_static_exit(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
// SubghzStatic* subghz_static = context;
|
|
|
|
|
|
|
|
// Reinitialize IC to default state
|
|
|
|
api_hal_subghz_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t subghz_static_back(void* context) {
|
|
|
|
return SubGhzViewMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubghzStatic* subghz_static_alloc() {
|
|
|
|
SubghzStatic* subghz_static = furi_alloc(sizeof(SubghzStatic));
|
|
|
|
|
|
|
|
// View allocation and configuration
|
|
|
|
subghz_static->view = view_alloc();
|
|
|
|
view_allocate_model(subghz_static->view, ViewModelTypeLockFree, sizeof(SubghzStaticModel));
|
|
|
|
view_set_context(subghz_static->view, subghz_static);
|
|
|
|
view_set_draw_callback(subghz_static->view, (ViewDrawCallback)subghz_static_draw);
|
|
|
|
view_set_input_callback(subghz_static->view, subghz_static_input);
|
|
|
|
view_set_enter_callback(subghz_static->view, subghz_static_enter);
|
|
|
|
view_set_exit_callback(subghz_static->view, subghz_static_exit);
|
|
|
|
view_set_previous_callback(subghz_static->view, subghz_static_back);
|
|
|
|
|
|
|
|
return subghz_static;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_static_free(SubghzStatic* subghz_static) {
|
|
|
|
furi_assert(subghz_static);
|
|
|
|
view_free(subghz_static->view);
|
|
|
|
free(subghz_static);
|
|
|
|
}
|
|
|
|
|
|
|
|
View* subghz_static_get_view(SubghzStatic* subghz_static) {
|
|
|
|
furi_assert(subghz_static);
|
|
|
|
return subghz_static->view;
|
|
|
|
}
|