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

226 lines
8.9 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) {
furi_assert(ctx);
i2cTools* i2ctools = ctx;
furi_mutex_acquire(i2ctools->mutex, FuriWaitForever);
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;
}
furi_mutex_release(i2ctools->mutex);
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));
i2ctools->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!i2ctools->mutex) {
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);
2022-10-28 18:46:40 +03:00
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) {
// Back
2022-10-20 21:02:27 +03:00
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
}
}
// Up
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
}
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->row_index > 0) {
i2ctools->sniffer->row_index--;
}
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
}
}
}
// Long Up
else if(
2022-10-20 21:02:27 +03:00
event.key == InputKeyUp &&
(event.type == InputTypeLong || event.type == InputTypeRepeat)) {
if(i2ctools->main_view->current_view == SCAN_VIEW) {
if(i2ctools->scanner->menu_index > 5) {
i2ctools->scanner->menu_index -= 5;
}
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
2022-10-28 18:46:40 +03:00
if(i2ctools->sender->value < 0xF9) {
i2ctools->sender->value += 5;
i2ctools->sender->sended = false;
2022-10-20 21:02:27 +03:00
}
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->row_index > 5) {
i2ctools->sniffer->row_index -= 5;
} else {
i2ctools->sniffer->row_index = 0;
}
2022-10-20 21:02:27 +03:00
}
}
// Down
else if(event.key == InputKeyDown && 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 < MENU_SIZE - 1) {
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
}
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if((i2ctools->sniffer->row_index + 3) <
(int)i2ctools->sniffer->frames[i2ctools->sniffer->menu_index].data_index) {
i2ctools->sniffer->row_index++;
}
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
}
}
}
// Long Down
else if(
event.key == InputKeyDown &&
(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 > 0x05) {
i2ctools->sender->value -= 5;
i2ctools->sender->sended = false;
} else {
i2ctools->sender->value = 0;
i2ctools->sender->sended = false;
}
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if((i2ctools->sniffer->row_index + 8) <
(int)i2ctools->sniffer->frames[i2ctools->sniffer->menu_index].data_index) {
i2ctools->sniffer->row_index += 5;
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++;
i2ctools->sniffer->row_index = 0;
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--;
i2ctools->sniffer->row_index = 0;
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);
furi_mutex_free(i2ctools->mutex);
2022-10-28 18:46:40 +03:00
free(i2ctools);
2022-10-20 21:02:27 +03:00
furi_record_close(RECORD_GUI);
return 0;
}