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

232 lines
9.5 KiB
C
Raw Normal View History

2022-10-28 15:46:40 +00:00
#include "i2ctools_i.h"
2022-10-20 18:02:27 +00:00
void i2ctools_draw_callback(Canvas* canvas, void* ctx) {
furi_assert(ctx);
i2cTools* i2ctools = ctx;
furi_mutex_acquire(i2ctools->mutex, FuriWaitForever);
2022-10-20 18:02:27 +00:00
2022-10-28 15:46:40 +00:00
switch(i2ctools->main_view->current_view) {
2022-10-20 18:02:27 +00:00
case MAIN_VIEW:
2022-10-28 15:46:40 +00:00
draw_main_view(canvas, i2ctools->main_view);
2022-10-20 18:02:27 +00:00
break;
case SCAN_VIEW:
2022-10-28 15:46:40 +00:00
draw_scanner_view(canvas, i2ctools->scanner);
2022-10-20 18:02:27 +00:00
break;
case SNIFF_VIEW:
2022-10-28 15:46:40 +00:00
draw_sniffer_view(canvas, i2ctools->sniffer);
2022-10-20 18:02:27 +00:00
break;
2022-10-28 15:46:40 +00:00
2022-10-20 18:02:27 +00:00
case SEND_VIEW:
2022-10-28 15:46:40 +00:00
draw_sender_view(canvas, i2ctools->sender);
2022-10-20 18:02:27 +00:00
break;
2022-10-28 15:46:40 +00:00
2023-07-05 17:07:43 +00:00
case INFOS_VIEW:
draw_infos_view(canvas);
break;
2022-10-20 18:02:27 +00:00
default:
break;
}
furi_mutex_release(i2ctools->mutex);
2022-10-20 18:02:27 +00: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 15:46:40 +00:00
// Alloc i2ctools
i2cTools* i2ctools = malloc(sizeof(i2cTools));
i2ctools->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!i2ctools->mutex) {
2022-10-20 18:02:27 +00:00
FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
2022-10-28 15:46:40 +00:00
free(i2ctools);
2022-10-20 18:02:27 +00:00
return -1;
}
2022-10-28 15:46:40 +00:00
// Alloc viewport
i2ctools->view_port = view_port_alloc();
view_port_draw_callback_set(i2ctools->view_port, i2ctools_draw_callback, i2ctools);
2022-10-28 15:46:40 +00:00
view_port_input_callback_set(i2ctools->view_port, i2ctools_input_callback, event_queue);
2022-10-20 18:02:27 +00:00
// Register view port in GUI
Gui* gui = furi_record_open(RECORD_GUI);
2022-10-28 15:46:40 +00:00
gui_add_view_port(gui, i2ctools->view_port, GuiLayerFullscreen);
2022-10-20 18:02:27 +00:00
InputEvent event;
2022-10-28 15:46:40 +00:00
i2ctools->main_view = i2c_main_view_alloc();
i2ctools->sniffer = i2c_sniffer_alloc();
i2ctools->sniffer->menu_index = 0;
2022-10-20 18:02:27 +00:00
2022-10-28 15:46:40 +00:00
i2ctools->scanner = i2c_scanner_alloc();
i2ctools->sender = i2c_sender_alloc();
// Share scanner with sender
i2ctools->sender->scanner = i2ctools->scanner;
2022-10-20 18:02:27 +00:00
2023-07-24 21:11:56 +00:00
while(1) {
if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
// Back
if(event.key == InputKeyBack && event.type == InputTypeRelease) {
if(i2ctools->main_view->current_view == MAIN_VIEW) {
break;
} else {
2023-07-24 21:11:56 +00:00
if(i2ctools->main_view->current_view == SNIFF_VIEW) {
stop_interrupts();
i2ctools->sniffer->started = false;
i2ctools->sniffer->state = I2C_BUS_FREE;
}
i2ctools->main_view->current_view = MAIN_VIEW;
2022-10-20 18:02:27 +00:00
}
}
2023-07-24 21:11:56 +00:00
// Up
else if(event.key == InputKeyUp && event.type == InputTypeRelease) {
if(i2ctools->main_view->current_view == MAIN_VIEW) {
if((i2ctools->main_view->menu_index > SCAN_VIEW)) {
i2ctools->main_view->menu_index--;
}
} else if(i2ctools->main_view->current_view == SCAN_VIEW) {
if(i2ctools->scanner->menu_index > 0) {
i2ctools->scanner->menu_index--;
}
} else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
if(i2ctools->sniffer->row_index > 0) {
i2ctools->sniffer->row_index--;
}
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value < 0xFF) {
i2ctools->sender->value++;
i2ctools->sender->sended = false;
}
2022-10-20 18:02:27 +00:00
}
}
2023-07-24 21:11:56 +00:00
// Long Up
else if(
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) {
if(i2ctools->sender->value < 0xF9) {
i2ctools->sender->value += 5;
i2ctools->sender->sended = false;
}
} 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 18:02:27 +00:00
}
}
2023-07-24 21:11:56 +00:00
// Down
else if(event.key == InputKeyDown && event.type == InputTypeRelease) {
if(i2ctools->main_view->current_view == MAIN_VIEW) {
if(i2ctools->main_view->menu_index < MENU_SIZE - 1) {
i2ctools->main_view->menu_index++;
}
} else if(i2ctools->main_view->current_view == SCAN_VIEW) {
if(i2ctools->scanner->menu_index < ((int)i2ctools->scanner->nb_found / 3)) {
i2ctools->scanner->menu_index++;
}
} 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++;
}
} else if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->value > 0x00) {
i2ctools->sender->value--;
i2ctools->sender->sended = false;
}
2022-10-20 18:02:27 +00:00
}
}
2023-07-24 21:11:56 +00:00
// Long Down
else if(
event.key == InputKeyDown &&
(event.type == InputTypeLong || event.type == InputTypeRepeat)) {
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;
}
}
} else if(event.key == InputKeyOk && event.type == InputTypeRelease) {
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) {
stop_interrupts();
i2ctools->sniffer->started = false;
i2ctools->sniffer->state = I2C_BUS_FREE;
} else {
start_interrupts(i2ctools->sniffer);
i2ctools->sniffer->started = true;
i2ctools->sniffer->state = I2C_BUS_FREE;
}
}
} else if(event.key == InputKeyRight && event.type == InputTypeRelease) {
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;
}
} 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;
}
}
} else if(event.key == InputKeyLeft && event.type == InputTypeRelease) {
if(i2ctools->main_view->current_view == SEND_VIEW) {
if(i2ctools->sender->address_idx > 0) {
i2ctools->sender->address_idx--;
i2ctools->sender->sended = false;
}
} 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 18:02:27 +00:00
}
}
}
2022-10-28 15:46:40 +00:00
view_port_update(i2ctools->view_port);
2022-10-20 18:02:27 +00:00
}
2022-10-28 15:46:40 +00:00
gui_remove_view_port(gui, i2ctools->view_port);
view_port_free(i2ctools->view_port);
2022-10-20 18:02:27 +00:00
furi_message_queue_free(event_queue);
2022-10-28 15:46:40 +00: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 15:46:40 +00:00
free(i2ctools);
2022-10-20 18:02:27 +00:00
furi_record_close(RECORD_GUI);
return 0;
}