fbt format

This commit is contained in:
MX 2023-04-04 21:59:05 +03:00
parent b122db27cc
commit 61ec8818db
No known key found for this signature in database
GPG key ID: 7CCC66B7DBDD1C83

View file

@ -10,35 +10,29 @@
#define COLS 128 #define COLS 128
#define ROWS 8 #define ROWS 8
typedef struct typedef struct {
{ bool autoscale;
bool autoscale; uint16_t us_per_sample;
uint16_t us_per_sample; size_t timings_cnt;
size_t timings_cnt; uint32_t* timings;
uint32_t* timings; uint32_t timings_sum;
uint32_t timings_sum; FuriMutex* mutex;
FuriMutex* mutex;
} IRScopeState; } IRScopeState;
static void state_set_autoscale(IRScopeState* state) static void state_set_autoscale(IRScopeState* state) {
{ if(state->autoscale) state->us_per_sample = state->timings_sum / (ROWS * COLS);
if (state->autoscale)
state->us_per_sample = state->timings_sum / (ROWS * COLS);
} }
static void canvas_draw_str_outline(Canvas* canvas, int x, int y, const char* str) static void canvas_draw_str_outline(Canvas* canvas, int x, int y, const char* str) {
{
canvas_set_color(canvas, ColorWhite); canvas_set_color(canvas, ColorWhite);
for (int y1 = -1; y1 <= 1; ++y1) for(int y1 = -1; y1 <= 1; ++y1)
for (int x1 = -1; x1 <= 1; ++x1) for(int x1 = -1; x1 <= 1; ++x1) canvas_draw_str(canvas, x + x1, y + y1, str);
canvas_draw_str(canvas, x + x1, y + y1, str);
canvas_set_color(canvas, ColorBlack); canvas_set_color(canvas, ColorBlack);
canvas_draw_str(canvas, x, y, str); canvas_draw_str(canvas, x, y, str);
} }
static void render_callback(Canvas* canvas, void* ctx) static void render_callback(Canvas* canvas, void* ctx) {
{
const IRScopeState* state = (IRScopeState*)ctx; const IRScopeState* state = (IRScopeState*)ctx;
furi_mutex_acquire(state->mutex, FuriWaitForever); furi_mutex_acquire(state->mutex, FuriWaitForever);
@ -51,19 +45,16 @@ static void render_callback(Canvas* canvas, void* ctx)
bool done = false; bool done = false;
size_t ix = 0; size_t ix = 0;
int timing_cols = -1; // Count of columns used to draw the current timing int timing_cols = -1; // Count of columns used to draw the current timing
for (size_t row = 0; row < ROWS && !done; ++row) for(size_t row = 0; row < ROWS && !done; ++row) {
{ for(size_t col = 0; col < COLS && !done; ++col) {
for (size_t col = 0; col < COLS && !done; ++col)
{
done = ix >= state->timings_cnt; done = ix >= state->timings_cnt;
if (!done && timing_cols < 0) if(!done && timing_cols < 0) {
{
timing_cols = state->timings[ix] / state->us_per_sample; timing_cols = state->timings[ix] / state->us_per_sample;
on = !on; on = !on;
} }
if (timing_cols == 0) ++ix; if(timing_cols == 0) ++ix;
int y = row * 8 + 7; int y = row * 8 + 7;
canvas_draw_line(canvas, col, y, col, y - (on ? 5 : 0)); canvas_draw_line(canvas, col, y, col, y - (on ? 5 : 0));
@ -72,10 +63,9 @@ static void render_callback(Canvas* canvas, void* ctx)
} }
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
if (state->autoscale) if(state->autoscale)
canvas_draw_str_outline(canvas, 100, 64, "Auto"); canvas_draw_str_outline(canvas, 100, 64, "Auto");
else else {
{
char buf[20]; char buf[20];
snprintf(buf, sizeof(buf), "%uus", state->us_per_sample); snprintf(buf, sizeof(buf), "%uus", state->us_per_sample);
canvas_draw_str_outline(canvas, 100, 64, buf); canvas_draw_str_outline(canvas, 100, 64, buf);
@ -84,14 +74,12 @@ static void render_callback(Canvas* canvas, void* ctx)
furi_mutex_release(state->mutex); furi_mutex_release(state->mutex);
} }
static void input_callback(InputEvent* input_event, void* ctx) static void input_callback(InputEvent* input_event, void* ctx) {
{
FuriMessageQueue* event_queue = ctx; FuriMessageQueue* event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever); furi_message_queue_put(event_queue, input_event, FuriWaitForever);
} }
static void ir_received_callback(void* ctx, InfraredWorkerSignal* signal) static void ir_received_callback(void* ctx, InfraredWorkerSignal* signal) {
{
furi_check(signal); furi_check(signal);
IRScopeState* state = (IRScopeState*)ctx; IRScopeState* state = (IRScopeState*)ctx;
@ -100,8 +88,7 @@ static void ir_received_callback(void* ctx, InfraredWorkerSignal* signal)
const uint32_t* timings; const uint32_t* timings;
infrared_worker_get_raw_signal(signal, &timings, &state->timings_cnt); infrared_worker_get_raw_signal(signal, &timings, &state->timings_cnt);
if (state->timings) if(state->timings) {
{
free(state->timings); free(state->timings);
state->timings_sum = 0; state->timings_sum = 0;
} }
@ -109,8 +96,7 @@ static void ir_received_callback(void* ctx, InfraredWorkerSignal* signal)
state->timings = malloc(state->timings_cnt * sizeof(uint32_t)); state->timings = malloc(state->timings_cnt * sizeof(uint32_t));
// Copy and sum. // Copy and sum.
for (size_t i = 0; i < state->timings_cnt; ++i) for(size_t i = 0; i < state->timings_cnt; ++i) {
{
state->timings[i] = timings[i]; state->timings[i] = timings[i];
state->timings_sum += timings[i]; state->timings_sum += timings[i];
} }
@ -120,24 +106,21 @@ static void ir_received_callback(void* ctx, InfraredWorkerSignal* signal)
furi_mutex_release(state->mutex); furi_mutex_release(state->mutex);
} }
int32_t ir_scope_app(void* p) int32_t ir_scope_app(void* p) {
{
UNUSED(p); UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent)); FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
furi_check(event_queue); furi_check(event_queue);
if(furi_hal_infrared_is_busy()) if(furi_hal_infrared_is_busy()) {
{
FURI_LOG_E(TAG, "Infrared is busy."); FURI_LOG_E(TAG, "Infrared is busy.");
return -1; return -1;
} }
IRScopeState state = { .autoscale = false, .us_per_sample = 200, IRScopeState state = {
.timings = NULL, .timings_cnt = 0, .mutex = NULL }; .autoscale = false, .us_per_sample = 200, .timings = NULL, .timings_cnt = 0, .mutex = NULL};
state.mutex = furi_mutex_alloc(FuriMutexTypeNormal); state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!state.mutex) if(!state.mutex) {
{
FURI_LOG_E(TAG, "Cannot create mutex."); FURI_LOG_E(TAG, "Cannot create mutex.");
return -1; return -1;
} }
@ -157,30 +140,22 @@ int32_t ir_scope_app(void* p)
InputEvent event; InputEvent event;
bool processing = true; bool processing = true;
while(processing && furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) while(processing &&
{ furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
if (event.type == InputTypeRelease) if(event.type == InputTypeRelease) {
{
furi_mutex_acquire(state.mutex, FuriWaitForever); furi_mutex_acquire(state.mutex, FuriWaitForever);
if (event.key == InputKeyBack) if(event.key == InputKeyBack) {
{
processing = false; processing = false;
} } else if(event.key == InputKeyUp) {
else if (event.key == InputKeyUp)
{
state.us_per_sample = MIN(1000, state.us_per_sample + 25); state.us_per_sample = MIN(1000, state.us_per_sample + 25);
state.autoscale = false; state.autoscale = false;
} } else if(event.key == InputKeyDown) {
else if (event.key == InputKeyDown)
{
state.us_per_sample = MAX(25, state.us_per_sample - 25); state.us_per_sample = MAX(25, state.us_per_sample - 25);
state.autoscale = false; state.autoscale = false;
} } else if(event.key == InputKeyOk) {
else if (event.key == InputKeyOk)
{
state.autoscale = !state.autoscale; state.autoscale = !state.autoscale;
if (state.autoscale) if(state.autoscale)
state_set_autoscale(&state); state_set_autoscale(&state);
else else
state.us_per_sample = 200; state.us_per_sample = 200;
@ -195,7 +170,7 @@ int32_t ir_scope_app(void* p)
infrared_worker_rx_stop(worker); infrared_worker_rx_stop(worker);
infrared_worker_free(worker); infrared_worker_free(worker);
if (state.timings) free(state.timings); if(state.timings) free(state.timings);
view_port_enabled_set(view_port, false); view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port); gui_remove_view_port(gui, view_port);