unleashed-firmware/applications/plugins/flipper_i2ctools/i2ctools.c

183 lines
7.2 KiB
C
Raw Normal View History

2022-10-28 18:46:40 +03:00
#include "i2ctools_i.h"
2022-10-20 21:02:27 +03:00
void i2ctools_draw_callback(Canvas* canvas, void* ctx) {
2022-10-28 18:46:40 +03:00
i2cTools* i2ctools = acquire_mutex((ValueMutex*)ctx, 25);
2022-10-20 21:02:27 +03:00
2022-10-28 18:46:40 +03:00
switch(i2ctools->main_view->current_view) {
2022-10-20 21:02:27 +03:00
case MAIN_VIEW:
2022-10-28 18:46:40 +03:00
draw_main_view(canvas, i2ctools->main_view);
2022-10-20 21:02:27 +03:00
break;
case SCAN_VIEW:
2022-10-28 18:46:40 +03:00
draw_scanner_view(canvas, i2ctools->scanner);
2022-10-20 21:02:27 +03:00
break;
case SNIFF_VIEW:
2022-10-28 18:46:40 +03:00
draw_sniffer_view(canvas, i2ctools->sniffer);
2022-10-20 21:02:27 +03:00
break;
2022-10-28 18:46:40 +03:00
2022-10-20 21:02:27 +03:00
case SEND_VIEW:
2022-10-28 18:46:40 +03:00
draw_sender_view(canvas, i2ctools->sender);
2022-10-20 21:02:27 +03:00
break;
2022-10-28 18:46:40 +03:00
2022-10-20 21:02:27 +03:00
default:
break;
}
2022-10-28 18:46:40 +03:00
release_mutex((ValueMutex*)ctx, i2ctools);
2022-10-20 21:02:27 +03:00
}
void i2ctools_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
}
int32_t i2ctools_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
2022-10-28 18:46:40 +03:00
// Alloc i2ctools
i2cTools* i2ctools = malloc(sizeof(i2cTools));
ValueMutex i2ctools_mutex;
if(!init_mutex(&i2ctools_mutex, i2ctools, sizeof(i2cTools))) {
2022-10-20 21:02:27 +03:00
FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
2022-10-28 18:46:40 +03:00
free(i2ctools);
2022-10-20 21:02:27 +03:00
return -1;
}
2022-10-28 18:46:40 +03:00
// Alloc viewport
i2ctools->view_port = view_port_alloc();
view_port_draw_callback_set(i2ctools->view_port, i2ctools_draw_callback, &i2ctools_mutex);
view_port_input_callback_set(i2ctools->view_port, i2ctools_input_callback, event_queue);
2022-10-20 21:02:27 +03:00
// Register view port in GUI
Gui* gui = furi_record_open(RECORD_GUI);
2022-10-28 18:46:40 +03:00
gui_add_view_port(gui, i2ctools->view_port, GuiLayerFullscreen);
2022-10-20 21:02:27 +03:00
InputEvent event;
2022-10-28 18:46:40 +03:00
i2ctools->main_view = i2c_main_view_alloc();
i2ctools->sniffer = i2c_sniffer_alloc();
i2ctools->sniffer->menu_index = 0;
2022-10-20 21:02:27 +03:00
2022-10-28 18:46:40 +03:00
i2ctools->scanner = i2c_scanner_alloc();
i2ctools->sender = i2c_sender_alloc();
// Share scanner with sender
i2ctools->sender->scanner = i2ctools->scanner;
2022-10-20 21:02:27 +03:00
while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
if(event.key == InputKeyBack && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == MAIN_VIEW) {
2022-10-20 21:02:27 +03:00
break;
} else {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == SNIFF_VIEW) {
2022-10-20 21:02:27 +03:00
stop_interrupts();
2022-10-28 18:46:40 +03:00
i2ctools->sniffer->started = false;
i2ctools->sniffer->state = I2C_BUS_FREE;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
i2ctools->main_view->current_view = MAIN_VIEW;
2022-10-20 21:02:27 +03:00
}
} else if(event.key == InputKeyUp && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == MAIN_VIEW) {
if((i2ctools->main_view->menu_index > SCAN_VIEW)) {
i2ctools->main_view->menu_index--;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SCAN_VIEW) {
if(i2ctools->scanner->menu_index > 0) {
i2ctools->scanner->menu_index--;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value < 0xFF) {
i2ctools->sender->value++;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
}
} else if(
event.key == InputKeyUp &&
(event.type == InputTypeLong || event.type == InputTypeRepeat)) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value < 0xF9) {
i2ctools->sender->value += 5;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
}
} else if(event.key == InputKeyDown && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == MAIN_VIEW) {
2022-10-29 03:36:18 +03:00
if(i2ctools->main_view->menu_index < 3) {
2022-10-28 18:46:40 +03:00
i2ctools->main_view->menu_index++;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SCAN_VIEW) {
if(i2ctools->scanner->menu_index < ((int)i2ctools->scanner->nb_found / 3)) {
i2ctools->scanner->menu_index++;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value > 0x00) {
i2ctools->sender->value--;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
}
} else if(event.key == InputKeyDown && event.type == InputTypeLong) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value > 0x05) {
i2ctools->sender->value -= 5;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
}
} else if(event.key == InputKeyOk && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == MAIN_VIEW) {
i2ctools->main_view->current_view = i2ctools->main_view->menu_index;
} else if(i2ctools->main_view->current_view == SCAN_VIEW) {
scan_i2c_bus(i2ctools->scanner);
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
i2ctools->sender->must_send = true;
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->started) {
2022-10-20 21:02:27 +03:00
stop_interrupts();
2022-10-28 18:46:40 +03:00
i2ctools->sniffer->started = false;
i2ctools->sniffer->state = I2C_BUS_FREE;
2022-10-20 21:02:27 +03:00
} else {
2022-10-28 18:46:40 +03:00
start_interrupts(i2ctools->sniffer);
i2ctools->sniffer->started = true;
i2ctools->sniffer->state = I2C_BUS_FREE;
2022-10-20 21:02:27 +03:00
}
}
} else if(event.key == InputKeyRight && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->address_idx < (i2ctools->scanner->nb_found - 1)) {
i2ctools->sender->address_idx++;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->menu_index < i2ctools->sniffer->frame_index) {
i2ctools->sniffer->menu_index++;
2022-10-20 21:02:27 +03:00
}
}
} else if(event.key == InputKeyLeft && event.type == InputTypeRelease) {
2022-10-28 18:46:40 +03:00
if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->address_idx > 0) {
i2ctools->sender->address_idx--;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->menu_index > 0) {
i2ctools->sniffer->menu_index--;
2022-10-20 21:02:27 +03:00
}
}
}
2022-10-28 18:46:40 +03:00
view_port_update(i2ctools->view_port);
2022-10-20 21:02:27 +03:00
}
2022-10-28 18:46:40 +03:00
gui_remove_view_port(gui, i2ctools->view_port);
view_port_free(i2ctools->view_port);
2022-10-20 21:02:27 +03:00
furi_message_queue_free(event_queue);
2022-10-28 18:46:40 +03:00
i2c_sniffer_free(i2ctools->sniffer);
i2c_scanner_free(i2ctools->scanner);
i2c_sender_free(i2ctools->sender);
i2c_main_view_free(i2ctools->main_view);
free(i2ctools);
2022-10-20 21:02:27 +03:00
furi_record_close(RECORD_GUI);
return 0;
}