diff --git a/.gitignore b/.gitignore index 600d2d3..722d5e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.vscode \ No newline at end of file +.vscode diff --git a/Canbus_app/app_user.c b/Canbus_app/app_user.c index 518eaa0..c85ad5a 100644 --- a/Canbus_app/app_user.c +++ b/Canbus_app/app_user.c @@ -1,126 +1,135 @@ #include "app_user.h" void makePaths(App* app) { - furi_assert(app); - if(!storage_simply_mkdir(app->storage, PATHAPPEXT)) { - dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder"); - } - if(!storage_simply_mkdir(app->storage, PATHLOGS)) { - dialog_message_show_storage_error(app->dialogs, "Cannot create\nlogs folder"); - } + furi_assert(app); + if (!storage_simply_mkdir(app->storage, PATHAPPEXT)) { + dialog_message_show_storage_error(app->dialogs, + "Cannot create\napp folder"); + } + if (!storage_simply_mkdir(app->storage, PATHLOGS)) { + dialog_message_show_storage_error(app->dialogs, + "Cannot create\nlogs folder"); + } } static bool app_scene_costum_callback(void* context, uint32_t costum_event) { - furi_assert(context); - App* app = context; - return scene_manager_handle_custom_event(app->scene_manager, costum_event); + furi_assert(context); + App* app = context; + return scene_manager_handle_custom_event(app->scene_manager, costum_event); } static bool app_scene_back_event(void* context) { - furi_assert(context); - App* app = context; - return scene_manager_handle_back_event(app->scene_manager); + furi_assert(context); + App* app = context; + return scene_manager_handle_back_event(app->scene_manager); } static void app_tick_event(void* context) { - furi_assert(context); - App* app = context; - UNUSED(app); + furi_assert(context); + App* app = context; + UNUSED(app); } static App* app_alloc() { - App* app = malloc(sizeof(App)); - app->scene_manager = scene_manager_alloc(&app_scene_handlers, app); - app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); - view_dispatcher_set_custom_event_callback(app->view_dispatcher, app_scene_costum_callback); - view_dispatcher_set_event_callback_context(app->view_dispatcher, app); - view_dispatcher_set_navigation_event_callback(app->view_dispatcher, app_scene_back_event); - view_dispatcher_set_tick_event_callback(app->view_dispatcher, app_tick_event, 100); + App* app = malloc(sizeof(App)); + app->scene_manager = scene_manager_alloc(&app_scene_handlers, app); + app->view_dispatcher = view_dispatcher_alloc(); + view_dispatcher_enable_queue(app->view_dispatcher); + view_dispatcher_set_custom_event_callback(app->view_dispatcher, + app_scene_costum_callback); + view_dispatcher_set_event_callback_context(app->view_dispatcher, app); + view_dispatcher_set_navigation_event_callback(app->view_dispatcher, + app_scene_back_event); + view_dispatcher_set_tick_event_callback(app->view_dispatcher, app_tick_event, + 100); - app->widget = widget_alloc(); - view_dispatcher_add_view(app->view_dispatcher, ViewWidget, widget_get_view(app->widget)); + app->widget = widget_alloc(); + view_dispatcher_add_view(app->view_dispatcher, ViewWidget, + widget_get_view(app->widget)); - app->submenu = submenu_alloc(); - view_dispatcher_add_view(app->view_dispatcher, SubmenuView, submenu_get_view(app->submenu)); + app->submenu = submenu_alloc(); + view_dispatcher_add_view(app->view_dispatcher, SubmenuView, + submenu_get_view(app->submenu)); - app->varList = variable_item_list_alloc(); - view_dispatcher_add_view( - app->view_dispatcher, VarListView, variable_item_list_get_view(app->varList)); + app->varList = variable_item_list_alloc(); + view_dispatcher_add_view(app->view_dispatcher, VarListView, + variable_item_list_get_view(app->varList)); - app->textBox = text_box_alloc(); - view_dispatcher_add_view(app->view_dispatcher, TextBoxView, text_box_get_view(app->textBox)); + app->textBox = text_box_alloc(); + view_dispatcher_add_view(app->view_dispatcher, TextBoxView, + text_box_get_view(app->textBox)); - app->input_byte_value = byte_input_alloc(); - view_dispatcher_add_view( - app->view_dispatcher, InputByteView, byte_input_get_view(app->input_byte_value)); + app->input_byte_value = byte_input_alloc(); + view_dispatcher_add_view(app->view_dispatcher, InputByteView, + byte_input_get_view(app->input_byte_value)); - app->dialogs = furi_record_open(RECORD_DIALOGS); - app->storage = furi_record_open(RECORD_STORAGE); - app->log_file = storage_file_alloc(app->storage); + app->dialogs = furi_record_open(RECORD_DIALOGS); + app->storage = furi_record_open(RECORD_STORAGE); + app->log_file = storage_file_alloc(app->storage); - app->text = furi_string_alloc(); - app->data = furi_string_alloc(); + app->text = furi_string_alloc(); + app->data = furi_string_alloc(); - app->mcp_can = mcp_alloc(MCP_NORMAL, MCP_16MHZ, MCP_500KBPS); + app->mcp_can = mcp_alloc(MCP_NORMAL, MCP_16MHZ, MCP_500KBPS); - app->frameArray = (CANFRAME*)malloc(100 * sizeof(CANFRAME)); + app->frameArray = (CANFRAME*) malloc(100 * sizeof(CANFRAME)); - app->frame_to_send = malloc(sizeof(CANFRAME)); + app->frame_to_send = malloc(sizeof(CANFRAME)); - makePaths(app); + makePaths(app); - return app; + return app; } static void app_free(App* app) { - furi_assert(app); + furi_assert(app); - view_dispatcher_remove_view(app->view_dispatcher, SubmenuView); - view_dispatcher_remove_view(app->view_dispatcher, ViewWidget); - view_dispatcher_remove_view(app->view_dispatcher, TextBoxView); - view_dispatcher_remove_view(app->view_dispatcher, VarListView); - view_dispatcher_remove_view(app->view_dispatcher, InputByteView); + view_dispatcher_remove_view(app->view_dispatcher, SubmenuView); + view_dispatcher_remove_view(app->view_dispatcher, ViewWidget); + view_dispatcher_remove_view(app->view_dispatcher, TextBoxView); + view_dispatcher_remove_view(app->view_dispatcher, VarListView); + view_dispatcher_remove_view(app->view_dispatcher, InputByteView); - scene_manager_free(app->scene_manager); - view_dispatcher_free(app->view_dispatcher); + scene_manager_free(app->scene_manager); + view_dispatcher_free(app->view_dispatcher); - widget_free(app->widget); - submenu_free(app->submenu); - text_box_free(app->textBox); - byte_input_free(app->input_byte_value); + widget_free(app->widget); + submenu_free(app->submenu); + text_box_free(app->textBox); + byte_input_free(app->input_byte_value); - furi_string_free(app->text); - furi_string_free(app->data); + furi_string_free(app->text); + furi_string_free(app->data); - if(app->log_file && storage_file_is_open(app->log_file)) { - storage_file_close(app->log_file); - } + if (app->log_file && storage_file_is_open(app->log_file)) { + storage_file_close(app->log_file); + } - storage_file_free(app->log_file); - furi_record_close(RECORD_STORAGE); - furi_record_close(RECORD_DIALOGS); + storage_file_free(app->log_file); + furi_record_close(RECORD_STORAGE); + furi_record_close(RECORD_DIALOGS); - free(app->frameArray); + free(app->frameArray); - free(app); + free(app); } int app_main(void* p) { - UNUSED(p); + UNUSED(p); - App* app = app_alloc(); + App* app = app_alloc(); - Gui* gui = furi_record_open(RECORD_GUI); + Gui* gui = furi_record_open(RECORD_GUI); - view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen); + view_dispatcher_attach_to_gui(app->view_dispatcher, gui, + ViewDispatcherTypeFullscreen); - scene_manager_next_scene(app->scene_manager, app_scene_main_menu); + scene_manager_next_scene(app->scene_manager, app_scene_main_menu); - view_dispatcher_run(app->view_dispatcher); - furi_record_close(RECORD_GUI); + view_dispatcher_run(app->view_dispatcher); + furi_record_close(RECORD_GUI); - app_free(app); + app_free(app); - return 0; -} \ No newline at end of file + return 0; +} diff --git a/Canbus_app/app_user.h b/Canbus_app/app_user.h index c95b933..7f7e835 100644 --- a/Canbus_app/app_user.h +++ b/Canbus_app/app_user.h @@ -2,92 +2,96 @@ #include #include -#include -#include -#include #include -#include +#include #include -#include #include -#include #include +#include +#include +#include +#include +#include #include "scenes_config/app_scene_functions.h" #include "libraries/mcp_can_2515.h" -#define PATHAPP "apps_data/canbus" +#define PATHAPP "apps_data/canbus" #define PATHAPPEXT EXT_PATH(PATHAPP) -#define PATHLOGS PATHAPPEXT "/logs" +#define PATHLOGS PATHAPPEXT "/logs" #define DEVICE_NO_CONNECTED (0xFF) typedef enum { - WorkerflagStop = (1 << 0), - WorkerflagReceived = (1 << 1), + WorkerflagStop = (1 << 0), + WorkerflagReceived = (1 << 1), } WorkerEvtFlags; #define WORKER_ALL_RX_EVENTS (WorkerflagStop | WorkerflagReceived) typedef struct { - MCP2515* mcp_can; - CANFRAME can_frame; - CANFRAME* frameArray; - CANFRAME* frame_to_send; - - uint32_t time; - uint32_t times[100]; - uint32_t current_time[100]; - - FuriThread* thread; - SceneManager* scene_manager; - ViewDispatcher* view_dispatcher; - Widget* widget; - Submenu* submenu; - VariableItemList* varList; - TextBox* textBox; - ByteInput* input_byte_value; - - FuriString* text; - FuriString* data; - - Storage* storage; - DialogsApp* dialogs; - File* log_file; - char log_file_path[100]; - bool log_file_ready; - uint8_t save_logs; - - uint32_t sniffer_index; - uint32_t sniffer_index_aux; - - uint8_t num_of_devices; - uint8_t sender_selected_item; - uint8_t sender_id_compose[4]; - - uint64_t size_of_storage; + MCP2515* mcp_can; + CANFRAME can_frame; + CANFRAME* frameArray; + CANFRAME* frame_to_send; + + uint32_t time; + uint32_t times[100]; + uint32_t current_time[100]; + + FuriThread* thread; + SceneManager* scene_manager; + ViewDispatcher* view_dispatcher; + Widget* widget; + Submenu* submenu; + VariableItemList* varList; + TextBox* textBox; + ByteInput* input_byte_value; + + FuriString* text; + FuriString* data; + + Storage* storage; + DialogsApp* dialogs; + File* log_file; + char log_file_path[100]; + bool log_file_ready; + uint8_t save_logs; + + uint32_t sniffer_index; + uint32_t sniffer_index_aux; + + uint8_t num_of_devices; + uint8_t sender_selected_item; + uint8_t sender_id_compose[4]; + + uint64_t size_of_storage; } App; // This is for the menu Options typedef enum { - SniffingTestOption, - SenderOption, - ReadLOGOption, - SettingsOption, - AboutUsOption, + SniffingTestOption, + SenderOption, + ReadLOGOption, + SettingsOption, + AboutUsOption, } MainMenuOptions; typedef enum { - SniffingOptionEvent, - SenderOptionEvent, - SettingsOptionEvent, - ReadLOGOptionEvent, - AboutUsEvent, + SniffingOptionEvent, + SenderOptionEvent, + SettingsOptionEvent, + ReadLOGOptionEvent, + AboutUsEvent, } MainMenuEvents; // This is for the Setting Options -typedef enum { BitrateOption, CristyalClkOption, SaveLogsOption } OptionSettings; +typedef enum { + BitrateOption, + CristyalClkOption, + SaveLogsOption +} OptionSettings; typedef enum { BitrateOptionEvent, CristyalClkOptionEvent } SettingsMenuEvent; typedef enum { ChooseIdEvent, SetIdEvent, ReturnEvent } SenderEvents; @@ -102,16 +106,15 @@ typedef enum { ButtonGetPressed } ButtonPressedEvent; // Views in the App typedef enum { - SubmenuView, - ViewWidget, - VarListView, - TextBoxView, - DialogInfoView, - InputByteView, + SubmenuView, + ViewWidget, + VarListView, + TextBoxView, + DialogInfoView, + InputByteView, } scenesViews; -char* sequential_file_resolve_path( - Storage* storage, - const char* dir, - const char* prefix, - const char* extension); +char* sequential_file_resolve_path(Storage* storage, + const char* dir, + const char* prefix, + const char* extension); diff --git a/Canbus_app/libraries/Spi_lib.c b/Canbus_app/libraries/Spi_lib.c index 6645c33..48ce6f1 100644 --- a/Canbus_app/libraries/Spi_lib.c +++ b/Canbus_app/libraries/Spi_lib.c @@ -5,79 +5,72 @@ inline static void furi_hal_spi_bus_r_handle_event_callback( FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event, const LL_SPI_InitTypeDef* preset) { - if(event == FuriHalSpiBusHandleEventInit) { - furi_hal_gpio_write(handle->cs, true); - furi_hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); - } else if(event == FuriHalSpiBusHandleEventDeinit) { - furi_hal_gpio_write(handle->cs, true); - furi_hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow); - } else if(event == FuriHalSpiBusHandleEventActivate) { - LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset); - LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER); - LL_SPI_Enable(handle->bus->spi); + if (event == FuriHalSpiBusHandleEventInit) { + furi_hal_gpio_write(handle->cs, true); + furi_hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullNo, + GpioSpeedVeryHigh); + } else if (event == FuriHalSpiBusHandleEventDeinit) { + furi_hal_gpio_write(handle->cs, true); + furi_hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow); + } else if (event == FuriHalSpiBusHandleEventActivate) { + LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*) preset); + LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER); + LL_SPI_Enable(handle->bus->spi); - furi_hal_gpio_init_ex( - handle->miso, - GpioModeAltFunctionPushPull, - GpioPullNo, - GpioSpeedVeryHigh, - GpioAltFn5SPI1); - furi_hal_gpio_init_ex( - handle->mosi, - GpioModeAltFunctionPushPull, - GpioPullNo, - GpioSpeedVeryHigh, - GpioAltFn5SPI1); - furi_hal_gpio_init_ex( - handle->sck, - GpioModeAltFunctionPushPull, - GpioPullNo, - GpioSpeedVeryHigh, - GpioAltFn5SPI1); + furi_hal_gpio_init_ex(handle->miso, GpioModeAltFunctionPushPull, GpioPullNo, + GpioSpeedVeryHigh, GpioAltFn5SPI1); + furi_hal_gpio_init_ex(handle->mosi, GpioModeAltFunctionPushPull, GpioPullNo, + GpioSpeedVeryHigh, GpioAltFn5SPI1); + furi_hal_gpio_init_ex(handle->sck, GpioModeAltFunctionPushPull, GpioPullNo, + GpioSpeedVeryHigh, GpioAltFn5SPI1); - furi_hal_gpio_write(handle->cs, false); - } else if(event == FuriHalSpiBusHandleEventDeactivate) { - furi_hal_gpio_write(handle->cs, true); + furi_hal_gpio_write(handle->cs, false); + } else if (event == FuriHalSpiBusHandleEventDeactivate) { + furi_hal_gpio_write(handle->cs, true); - furi_hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow); - furi_hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow); - furi_hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow); + furi_hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow); + furi_hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow); + furi_hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow); - LL_SPI_Disable(handle->bus->spi); - } + LL_SPI_Disable(handle->bus->spi); + } } // Here is the CALLBACK -static void spi_bus_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) { - furi_hal_spi_bus_r_handle_event_callback(handle, event, SPEED_8MHZ); +static void spi_bus_callback(FuriHalSpiBusHandle* handle, + FuriHalSpiBusHandleEvent event) { + furi_hal_spi_bus_r_handle_event_callback(handle, event, SPEED_8MHZ); } // This is to Init the SPI Communication FuriHalSpiBusHandle* spi_alloc() { - FuriHalSpiBusHandle* spi = malloc(sizeof(FuriHalSpiBusHandle)); - spi->bus = BUS; - spi->callback = &spi_bus_callback; - spi->cs = CS; - spi->miso = MISO; - spi->mosi = MOSI; - spi->sck = SCK; - return spi; + FuriHalSpiBusHandle* spi = malloc(sizeof(FuriHalSpiBusHandle)); + spi->bus = BUS; + spi->callback = &spi_bus_callback; + spi->cs = CS; + spi->miso = MISO; + spi->mosi = MOSI; + spi->sck = SCK; + return spi; } // Function to send data bool spi_send(FuriHalSpiBusHandle* spi, uint8_t* buffer) { - furi_hal_spi_acquire(spi); - bool ret = furi_hal_spi_bus_tx(spi, buffer, sizeof(buffer), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; + furi_hal_spi_acquire(spi); + bool ret = furi_hal_spi_bus_tx(spi, buffer, sizeof(buffer), TIMEOUT_SPI); + furi_hal_spi_release(spi); + return ret; } // Function to read register -bool spi_send_and_read(FuriHalSpiBusHandle* spi, uint8_t* action_address, uint8_t* data_read) { - furi_hal_spi_acquire(spi); - bool ret = - (furi_hal_spi_bus_tx(spi, action_address, sizeof(action_address), TIMEOUT_SPI) && - furi_hal_spi_bus_rx(spi, data_read, sizeof(data_read), TIMEOUT_SPI)); - furi_hal_spi_release(spi); - return ret; +bool spi_send_and_read(FuriHalSpiBusHandle* spi, + uint8_t* action_address, + uint8_t* data_read) { + furi_hal_spi_acquire(spi); + bool ret = + (furi_hal_spi_bus_tx(spi, action_address, sizeof(action_address), + TIMEOUT_SPI) && + furi_hal_spi_bus_rx(spi, data_read, sizeof(data_read), TIMEOUT_SPI)); + furi_hal_spi_release(spi); + return ret; } diff --git a/Canbus_app/libraries/Spi_lib.h b/Canbus_app/libraries/Spi_lib.h index bd57407..da159ed 100644 --- a/Canbus_app/libraries/Spi_lib.h +++ b/Canbus_app/libraries/Spi_lib.h @@ -2,22 +2,22 @@ #define _SPI_LIB_H_ #include -#include -#include #include +#include +#include #define TIMEOUT_SPI 100 -#define CS &gpio_ext_pa4 -#define SCK &gpio_ext_pb3 -#define MOSI &gpio_ext_pa7 -#define MISO &gpio_ext_pa6 +#define CS &gpio_ext_pa4 +#define SCK &gpio_ext_pb3 +#define MOSI &gpio_ext_pa7 +#define MISO &gpio_ext_pa6 -#define BUS &furi_hal_spi_bus_r -#define SPEED_8MHZ &furi_hal_spi_preset_1edge_low_8m // 8 MHZ -#define SPEED_4MHZ &furi_hal_spi_preset_1edge_low_4m // 4 MHZ -#define SPEED_2MHZ &furi_hal_spi_preset_1edge_low_2m // 2 MHZ +#define BUS &furi_hal_spi_bus_r +#define SPEED_8MHZ &furi_hal_spi_preset_1edge_low_8m // 8 MHZ +#define SPEED_4MHZ &furi_hal_spi_preset_1edge_low_4m // 4 MHZ +#define SPEED_2MHZ &furi_hal_spi_preset_1edge_low_2m // 2 MHZ // FUNCTIONS FuriHalSpiBusHandle* spi_alloc(); -#endif \ No newline at end of file +#endif diff --git a/Canbus_app/libraries/log_user.h b/Canbus_app/libraries/log_user.h index f887e8d..90270ee 100644 --- a/Canbus_app/libraries/log_user.h +++ b/Canbus_app/libraries/log_user.h @@ -2,19 +2,22 @@ #define __LOG_USER_H_ #include -#include #include +#include -#define LOG_TAG "INFO_WRITTEN" +#define LOG_TAG "INFO_WRITTEN" #define LOG_SHOW true #define log_info(format, ...) \ - if(LOG_SHOW) FURI_LOG_I(LOG_TAG, format, ##__VA_ARGS__) + if (LOG_SHOW) \ + FURI_LOG_I(LOG_TAG, format, ##__VA_ARGS__) #define log_exception(format, ...) \ - if(LOG_SHOW) FURI_LOG_E(LOG_TAG, format, ##__VA_ARGS__) + if (LOG_SHOW) \ + FURI_LOG_E(LOG_TAG, format, ##__VA_ARGS__) #define log_warning(format, ...) \ - if(LOG_SHOW) FURI_LOG_W(LOG_TAG, format, ##__VA_ARGS__) + if (LOG_SHOW) \ + FURI_LOG_W(LOG_TAG, format, ##__VA_ARGS__) -#endif \ No newline at end of file +#endif diff --git a/Canbus_app/libraries/mcp_can_2515.c b/Canbus_app/libraries/mcp_can_2515.c index 350e0cc..a4de92a 100644 --- a/Canbus_app/libraries/mcp_can_2515.c +++ b/Canbus_app/libraries/mcp_can_2515.c @@ -1,577 +1,606 @@ #include "mcp_can_2515.h" // To read a register with SPI protocol -static bool read_register(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t* data) { - bool ret = true; - uint8_t instruction[] = {INSTRUCTION_READ, address}; - furi_hal_spi_acquire(spi); - ret = - (furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI) && +static bool read_register(FuriHalSpiBusHandle* spi, + uint8_t address, + uint8_t* data) { + bool ret = true; + uint8_t instruction[] = {INSTRUCTION_READ, address}; + furi_hal_spi_acquire(spi); + ret = (furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), + TIMEOUT_SPI) && furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI)); - furi_hal_spi_release(spi); - return ret; + furi_hal_spi_release(spi); + return ret; } // To read a register with SPI Protocol -static bool set_register(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t data) { - bool ret = true; - uint8_t instruction[] = {INSTRUCTION_WRITE, address, data}; - furi_hal_spi_acquire(spi); - ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; +static bool set_register(FuriHalSpiBusHandle* spi, + uint8_t address, + uint8_t data) { + bool ret = true; + uint8_t instruction[] = {INSTRUCTION_WRITE, address, data}; + furi_hal_spi_acquire(spi); + ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + furi_hal_spi_release(spi); + return ret; } // To write multiple registers -static bool set_registerS(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t* data) { - bool ret = true; - uint8_t instruction[] = {INSTRUCTION_WRITE, address}; - uint8_t count = sizeof(data); - - for(int i = 0; i < count; i++) { - instruction[i + 2] = data[i]; - } - - furi_hal_spi_acquire(spi); - ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; +static bool set_registerS(FuriHalSpiBusHandle* spi, + uint8_t address, + uint8_t* data) { + bool ret = true; + uint8_t instruction[] = {INSTRUCTION_WRITE, address}; + uint8_t count = sizeof(data); + + for (int i = 0; i < count; i++) { + instruction[i + 2] = data[i]; + } + + furi_hal_spi_acquire(spi); + ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + furi_hal_spi_release(spi); + return ret; } // To modify the value of one bit from a register -static bool - modify_register(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t mask, uint8_t data) { - uint8_t instruction[] = {INSTRUCTION_BITMOD, address, mask, data}; - bool ret = true; - furi_hal_spi_acquire(spi); - ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; +static bool modify_register(FuriHalSpiBusHandle* spi, + uint8_t address, + uint8_t mask, + uint8_t data) { + uint8_t instruction[] = {INSTRUCTION_BITMOD, address, mask, data}; + bool ret = true; + furi_hal_spi_acquire(spi); + ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + furi_hal_spi_release(spi); + return ret; } // To reset the MCP2515 bool mcp_reset(FuriHalSpiBusHandle* spi) { - uint8_t buff[1] = {INSTRUCTION_RESET}; - bool ret = true; - furi_hal_spi_acquire(spi); - ret = furi_hal_spi_bus_tx(spi, buff, sizeof(buff), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; + uint8_t buff[1] = {INSTRUCTION_RESET}; + bool ret = true; + furi_hal_spi_acquire(spi); + ret = furi_hal_spi_bus_tx(spi, buff, sizeof(buff), TIMEOUT_SPI); + furi_hal_spi_release(spi); + return ret; } // To get the MCP2515 status bool mcp_get_status(FuriHalSpiBusHandle* spi, uint8_t* data) { - uint8_t buff[1] = {INSTRUCTION_READ_STATUS}; - bool ret = true; - furi_hal_spi_acquire(spi); - ret = - (furi_hal_spi_bus_tx(spi, buff, sizeof(buff), TIMEOUT_SPI) && + uint8_t buff[1] = {INSTRUCTION_READ_STATUS}; + bool ret = true; + furi_hal_spi_acquire(spi); + ret = (furi_hal_spi_bus_tx(spi, buff, sizeof(buff), TIMEOUT_SPI) && furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI)); - furi_hal_spi_release(spi); - return ret; + furi_hal_spi_release(spi); + return ret; } // To init the buffer -void write_mf(FuriHalSpiBusHandle* spi, uint8_t adress, uint8_t ext, uint8_t id) { - uint16_t canId = (uint16_t)(id & 0x0FFFF); - uint8_t bufData[4]; - - if(ext) { - bufData[MCP_EID0] = (uint8_t)(canId & 0xFF); - bufData[MCP_EID8] = (uint8_t)(canId >> 8); - canId = (uint16_t)(id >> 16); - bufData[MCP_SIDL] = (uint8_t)(canId & 0x03); - bufData[MCP_SIDL] += (uint8_t)((canId & 0x1C) << 3); - bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M; - bufData[MCP_SIDH] = (uint8_t)(canId >> 5); - } else { - bufData[MCP_EID0] = (uint8_t)(canId & 0xFF); - bufData[MCP_EID8] = (uint8_t)(canId >> 8); - canId = (uint16_t)(id >> 16); - bufData[MCP_SIDL] = (uint8_t)((canId & 0x07) << 5); - bufData[MCP_SIDH] = (uint8_t)(canId >> 3); - } - - set_registerS(spi, adress, bufData); +void write_mf(FuriHalSpiBusHandle* spi, + uint8_t adress, + uint8_t ext, + uint8_t id) { + uint16_t canId = (uint16_t) (id & 0x0FFFF); + uint8_t bufData[4]; + + if (ext) { + bufData[MCP_EID0] = (uint8_t) (canId & 0xFF); + bufData[MCP_EID8] = (uint8_t) (canId >> 8); + canId = (uint16_t) (id >> 16); + bufData[MCP_SIDL] = (uint8_t) (canId & 0x03); + bufData[MCP_SIDL] += (uint8_t) ((canId & 0x1C) << 3); + bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M; + bufData[MCP_SIDH] = (uint8_t) (canId >> 5); + } else { + bufData[MCP_EID0] = (uint8_t) (canId & 0xFF); + bufData[MCP_EID8] = (uint8_t) (canId >> 8); + canId = (uint16_t) (id >> 16); + bufData[MCP_SIDL] = (uint8_t) ((canId & 0x07) << 5); + bufData[MCP_SIDH] = (uint8_t) (canId >> 3); + } + + set_registerS(spi, adress, bufData); } // This function works to get the Can Id from the buffer -void read_Id(FuriHalSpiBusHandle* spi, uint8_t addr, uint32_t* id, uint8_t* ext) { - uint8_t tbufdata[4] = {0, 0, 0, 0}; - *ext = 0; - *id = 0; - - read_register(spi, addr, tbufdata); - - *id = (tbufdata[MCP_SIDH] << 3) + (tbufdata[MCP_SIDL] >> 5); - - if((tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M) { - *id = (*id << 2) + (tbufdata[MCP_SIDL] & 0x03); - *id = (*id << 8) + tbufdata[MCP_EID8]; - *id = (*id << 8) + tbufdata[MCP_EID0]; - *ext = 1; - } +void read_Id(FuriHalSpiBusHandle* spi, + uint8_t addr, + uint32_t* id, + uint8_t* ext) { + uint8_t tbufdata[4] = {0, 0, 0, 0}; + *ext = 0; + *id = 0; + + read_register(spi, addr, tbufdata); + + *id = (tbufdata[MCP_SIDH] << 3) + (tbufdata[MCP_SIDL] >> 5); + + if ((tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M) { + *id = (*id << 2) + (tbufdata[MCP_SIDL] & 0x03); + *id = (*id << 8) + tbufdata[MCP_EID8]; + *id = (*id << 8) + tbufdata[MCP_EID0]; + *ext = 1; + } } // To set the Mode bool set_mode(FuriHalSpiBusHandle* spi, MCP_MODE new_mode) { - bool ret = false; - uint8_t read_status = 0; - uint8_t time_out = 0; - - do { - modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, MODE_CONFIG); - read_register(spi, MCP_CANSTAT, &read_status); - - read_status &= CANSTAT_OPM; - if(read_status == MODE_CONFIG) ret = true; - furi_delay_ms(1); - time_out++; - } while((ret != true) && (time_out < 50)); - - if((ret != true) || (time_out >= 50)) { - return false; - } - - time_out = 0; - - do { - modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, new_mode); - read_register(spi, MCP_CANSTAT, &read_status); - - read_status &= CANSTAT_OPM; - if(read_status == new_mode) return true; - furi_delay_ms(1); - time_out++; - } while((ret != true) && (time_out < 50)); - - return ret; + bool ret = false; + uint8_t read_status = 0; + uint8_t time_out = 0; + + do { + modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, MODE_CONFIG); + read_register(spi, MCP_CANSTAT, &read_status); + + read_status &= CANSTAT_OPM; + if (read_status == MODE_CONFIG) + ret = true; + furi_delay_ms(1); + time_out++; + } while ((ret != true) && (time_out < 50)); + + if ((ret != true) || (time_out >= 50)) { + return false; + } + + time_out = 0; + + do { + modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, new_mode); + read_register(spi, MCP_CANSTAT, &read_status); + + read_status &= CANSTAT_OPM; + if (read_status == new_mode) + return true; + furi_delay_ms(1); + time_out++; + } while ((ret != true) && (time_out < 50)); + + return ret; } // To set Config mode bool set_config_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MODE_CONFIG); - return ret; + FuriHalSpiBusHandle* spi = mcp_can->spi; + bool ret = true; + ret = set_mode(spi, MODE_CONFIG); + return ret; } // To set Normal Mode bool set_normal_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MCP_NORMAL); - return ret; + FuriHalSpiBusHandle* spi = mcp_can->spi; + bool ret = true; + ret = set_mode(spi, MCP_NORMAL); + return ret; } -//To set ListenOnly Mode +// To set ListenOnly Mode bool set_listen_only_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MCP_LISTENONLY); - return ret; + FuriHalSpiBusHandle* spi = mcp_can->spi; + bool ret = true; + ret = set_mode(spi, MCP_LISTENONLY); + return ret; } // To set Sleep Mode bool set_sleep_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MCP_SLEEP); - return ret; + FuriHalSpiBusHandle* spi = mcp_can->spi; + bool ret = true; + ret = set_mode(spi, MCP_SLEEP); + return ret; } // To set LoopBackMode bool set_loop_back_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MCP_LOOPBACK); - return ret; + FuriHalSpiBusHandle* spi = mcp_can->spi; + bool ret = true; + ret = set_mode(spi, MCP_LOOPBACK); + return ret; } void init_can_buffer(FuriHalSpiBusHandle* spi) { - uint8_t a1 = 0, a2 = 0, a3 = 0; + uint8_t a1 = 0, a2 = 0, a3 = 0; - uint8_t std = 0; - uint8_t ext = 1; + uint8_t std = 0; + uint8_t ext = 1; - uint32_t ulMask = 0x00, ulFilt = 0x00; + uint32_t ulMask = 0x00, ulFilt = 0x00; - // ulMask --------------------------- + // ulMask --------------------------- - write_mf(spi, MCP_RXM0SIDH, ext, ulMask); + write_mf(spi, MCP_RXM0SIDH, ext, ulMask); - write_mf(spi, MCP_RXM1SIDH, ext, ulMask); + write_mf(spi, MCP_RXM1SIDH, ext, ulMask); - // ulFilt -------------------------- + // ulFilt -------------------------- - write_mf(spi, MCP_RXF0SIDH, ext, ulFilt); + write_mf(spi, MCP_RXF0SIDH, ext, ulFilt); - write_mf(spi, MCP_RXF1SIDH, std, ulFilt); + write_mf(spi, MCP_RXF1SIDH, std, ulFilt); - write_mf(spi, MCP_RXF2SIDH, ext, ulFilt); + write_mf(spi, MCP_RXF2SIDH, ext, ulFilt); - write_mf(spi, MCP_RXF3SIDH, std, ulFilt); + write_mf(spi, MCP_RXF3SIDH, std, ulFilt); - write_mf(spi, MCP_RXF4SIDH, ext, ulFilt); + write_mf(spi, MCP_RXF4SIDH, ext, ulFilt); - write_mf(spi, MCP_RXF5SIDH, std, ulFilt); + write_mf(spi, MCP_RXF5SIDH, std, ulFilt); - a1 = MCP_TXB0CTRL; - a2 = MCP_TXB1CTRL; - a3 = MCP_TXB2CTRL; + a1 = MCP_TXB0CTRL; + a2 = MCP_TXB1CTRL; + a3 = MCP_TXB2CTRL; - for(int i = 0; i < 14; i++) { - set_register(spi, a1, 0); - set_register(spi, a2, 0); - set_register(spi, a3, 0); - a1++; - a2++; - a3++; - } + for (int i = 0; i < 14; i++) { + set_register(spi, a1, 0); + set_register(spi, a2, 0); + set_register(spi, a3, 0); + a1++; + a2++; + a3++; + } - set_register(spi, MCP_RXB0CTRL, 0); - set_register(spi, MCP_RXB1CTRL, 0); + set_register(spi, MCP_RXB0CTRL, 0); + set_register(spi, MCP_RXB1CTRL, 0); } // This function works to set Registers to initialize the MCP2515 void set_registers_init(FuriHalSpiBusHandle* spi) { - set_register(spi, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); + set_register(spi, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); - set_register(spi, MCP_BFPCTRL, MCP_BxBFS_MASK | MCP_BxBFE_MASK); + set_register(spi, MCP_BFPCTRL, MCP_BxBFS_MASK | MCP_BxBFE_MASK); - set_register(spi, MCP_TXRTSCTRL, 0x00); + set_register(spi, MCP_TXRTSCTRL, 0x00); } // This function Works to set the Clock and Bitrate of the MCP2515 -void mcp_set_bitrate(FuriHalSpiBusHandle* spi, MCP_BITRATE bitrate, MCP_CLOCK clk) { - uint8_t cfg1 = 0, cfg2 = 0, cfg3 = 0; +void mcp_set_bitrate(FuriHalSpiBusHandle* spi, + MCP_BITRATE bitrate, + MCP_CLOCK clk) { + uint8_t cfg1 = 0, cfg2 = 0, cfg3 = 0; - switch(clk) { + switch (clk) { case MCP_8MHZ: - switch(bitrate) { + switch (bitrate) { case MCP_125KPS: - cfg1 = MCP_8MHz_125kBPS_CFG1; - cfg2 = MCP_8MHz_125kBPS_CFG2; - cfg3 = MCP_8MHz_125kBPS_CFG3; - break; + cfg1 = MCP_8MHz_125kBPS_CFG1; + cfg2 = MCP_8MHz_125kBPS_CFG2; + cfg3 = MCP_8MHz_125kBPS_CFG3; + break; case MCP_250KBPS: - cfg1 = MCP_8MHz_250kBPS_CFG1; - cfg2 = MCP_8MHz_250kBPS_CFG2; - cfg3 = MCP_8MHz_250kBPS_CFG3; - break; + cfg1 = MCP_8MHz_250kBPS_CFG1; + cfg2 = MCP_8MHz_250kBPS_CFG2; + cfg3 = MCP_8MHz_250kBPS_CFG3; + break; case MCP_500KBPS: - cfg1 = MCP_8MHz_500kBPS_CFG1; - cfg2 = MCP_8MHz_500kBPS_CFG2; - cfg3 = MCP_8MHz_500kBPS_CFG3; - break; + cfg1 = MCP_8MHz_500kBPS_CFG1; + cfg2 = MCP_8MHz_500kBPS_CFG2; + cfg3 = MCP_8MHz_500kBPS_CFG3; + break; case MCP_1000KBPS: - cfg1 = MCP_8MHz_1000kBPS_CFG1; - cfg2 = MCP_8MHz_1000kBPS_CFG2; - cfg3 = MCP_8MHz_1000kBPS_CFG3; - break; - } - break; + cfg1 = MCP_8MHz_1000kBPS_CFG1; + cfg2 = MCP_8MHz_1000kBPS_CFG2; + cfg3 = MCP_8MHz_1000kBPS_CFG3; + break; + } + break; case MCP_16MHZ: - switch(bitrate) { + switch (bitrate) { case MCP_125KPS: - cfg1 = MCP_16MHz_125kBPS_CFG1; - cfg2 = MCP_16MHz_125kBPS_CFG2; - cfg3 = MCP_16MHz_125kBPS_CFG3; - break; + cfg1 = MCP_16MHz_125kBPS_CFG1; + cfg2 = MCP_16MHz_125kBPS_CFG2; + cfg3 = MCP_16MHz_125kBPS_CFG3; + break; case MCP_250KBPS: - cfg1 = MCP_16MHz_250kBPS_CFG1; - cfg2 = MCP_16MHz_250kBPS_CFG2; - cfg3 = MCP_16MHz_250kBPS_CFG3; - break; + cfg1 = MCP_16MHz_250kBPS_CFG1; + cfg2 = MCP_16MHz_250kBPS_CFG2; + cfg3 = MCP_16MHz_250kBPS_CFG3; + break; case MCP_500KBPS: - cfg1 = MCP_16MHz_500kBPS_CFG1; - cfg2 = MCP_16MHz_500kBPS_CFG2; - cfg3 = MCP_16MHz_500kBPS_CFG3; - break; + cfg1 = MCP_16MHz_500kBPS_CFG1; + cfg2 = MCP_16MHz_500kBPS_CFG2; + cfg3 = MCP_16MHz_500kBPS_CFG3; + break; case MCP_1000KBPS: - cfg1 = MCP_16MHz_1000kBPS_CFG1; - cfg2 = MCP_16MHz_1000kBPS_CFG2; - cfg3 = MCP_16MHz_1000kBPS_CFG3; - break; - } - break; + cfg1 = MCP_16MHz_1000kBPS_CFG1; + cfg2 = MCP_16MHz_1000kBPS_CFG2; + cfg3 = MCP_16MHz_1000kBPS_CFG3; + break; + } + break; case MCP_20MHZ: - switch(bitrate) { + switch (bitrate) { case MCP_125KPS: - cfg1 = MCP_20MHz_125kBPS_CFG1; - cfg2 = MCP_20MHz_125kBPS_CFG2; - cfg3 = MCP_20MHz_125kBPS_CFG3; - break; + cfg1 = MCP_20MHz_125kBPS_CFG1; + cfg2 = MCP_20MHz_125kBPS_CFG2; + cfg3 = MCP_20MHz_125kBPS_CFG3; + break; case MCP_250KBPS: - cfg1 = MCP_20MHz_250kBPS_CFG1; - cfg2 = MCP_20MHz_250kBPS_CFG2; - cfg3 = MCP_20MHz_250kBPS_CFG3; - break; + cfg1 = MCP_20MHz_250kBPS_CFG1; + cfg2 = MCP_20MHz_250kBPS_CFG2; + cfg3 = MCP_20MHz_250kBPS_CFG3; + break; case MCP_500KBPS: - cfg1 = MCP_20MHz_500kBPS_CFG1; - cfg2 = MCP_20MHz_500kBPS_CFG2; - cfg3 = MCP_20MHz_500kBPS_CFG3; - break; + cfg1 = MCP_20MHz_500kBPS_CFG1; + cfg2 = MCP_20MHz_500kBPS_CFG2; + cfg3 = MCP_20MHz_500kBPS_CFG3; + break; case MCP_1000KBPS: - cfg1 = MCP_20MHz_1000kBPS_CFG1; - cfg2 = MCP_20MHz_1000kBPS_CFG2; - cfg3 = MCP_20MHz_1000kBPS_CFG3; - break; - } - break; - } - - set_register(spi, MCP_CNF1, cfg1); - set_register(spi, MCP_CNF2, cfg2); - set_register(spi, MCP_CNF3, cfg3); + cfg1 = MCP_20MHz_1000kBPS_CFG1; + cfg2 = MCP_20MHz_1000kBPS_CFG2; + cfg3 = MCP_20MHz_1000kBPS_CFG3; + break; + } + break; + } + + set_register(spi, MCP_CNF1, cfg1); + set_register(spi, MCP_CNF2, cfg2); + set_register(spi, MCP_CNF3, cfg3); } // This function Works to read the can frame from the buffer -void read_canframe(FuriHalSpiBusHandle* spi, const uint8_t addr, CANFRAME* frame) { - uint8_t ctrl = 0, len = 0; - static uint8_t data = 0; +void read_canframe(FuriHalSpiBusHandle* spi, + const uint8_t addr, + CANFRAME* frame) { + uint8_t ctrl = 0, len = 0; + static uint8_t data = 0; - read_Id(spi, addr, &frame->canId, &frame->ext); + read_Id(spi, addr, &frame->canId, &frame->ext); - read_register(spi, addr - 1, &ctrl); + read_register(spi, addr - 1, &ctrl); - read_register(spi, addr + 4, &(len)); + read_register(spi, addr + 4, &(len)); - if(ctrl & 0x08) - frame->req = 1; - else - frame->req = 0; + if (ctrl & 0x08) + frame->req = 1; + else + frame->req = 0; - len &= MCP_DLC_MASK; + len &= MCP_DLC_MASK; - frame->data_lenght = len; + frame->data_lenght = len; - for(uint8_t i = 0; i < len; i++) { - read_register(spi, addr + 5 + i, &data); - frame->buffer[i] = data; - } + for (uint8_t i = 0; i < len; i++) { + read_register(spi, addr + 5 + i, &data); + frame->buffer[i] = data; + } } // This function Works to get the Can message ERROR_CAN read_can_message(MCP2515* mcp_can, CANFRAME* frame) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - ERROR_CAN ret = ERROR_OK; - static uint8_t status = 0; + FuriHalSpiBusHandle* spi = mcp_can->spi; + ERROR_CAN ret = ERROR_OK; + static uint8_t status = 0; - mcp_get_status(spi, &status); + mcp_get_status(spi, &status); - if(status & MCP_STAT_RX0IF) { - read_canframe(spi, MCP_RXB0SIDH, frame); - modify_register(spi, MCP_CANINTF, MCP_RX0IF, 0); - } + if (status & MCP_STAT_RX0IF) { + read_canframe(spi, MCP_RXB0SIDH, frame); + modify_register(spi, MCP_CANINTF, MCP_RX0IF, 0); + } - else if(status & MCP_STAT_RX1IF) { - read_canframe(spi, MCP_RXB1SIDH, frame); - modify_register(spi, MCP_CANINTF, MCP_RX1IF, 0); - } else { - ret = ERROR_NOMSG; - } + else if (status & MCP_STAT_RX1IF) { + read_canframe(spi, MCP_RXB1SIDH, frame); + modify_register(spi, MCP_CANINTF, MCP_RX1IF, 0); + } else { + ret = ERROR_NOMSG; + } - return ret; + return ret; } // This function return the error in the can bus network uint8_t get_error(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - uint8_t err = 0; - read_register(spi, MCP_EFLG, &err); - modify_register(spi, MCP_EFLG, MCP_EFLG_RX0OVR, 0); - modify_register(spi, MCP_EFLG, MCP_EFLG_RX1OVR, 0); - return err; + FuriHalSpiBusHandle* spi = mcp_can->spi; + uint8_t err = 0; + read_register(spi, MCP_EFLG, &err); + modify_register(spi, MCP_EFLG, MCP_EFLG_RX0OVR, 0); + modify_register(spi, MCP_EFLG, MCP_EFLG_RX1OVR, 0); + return err; } // This function works to check if there is an error in the CANBUS network ERROR_CAN check_error(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; + FuriHalSpiBusHandle* spi = mcp_can->spi; - uint8_t eflg = 0; + uint8_t eflg = 0; - read_register(spi, MCP_EFLG, &eflg); + read_register(spi, MCP_EFLG, &eflg); - if(eflg & MCP_EFLG_ERRORMASK) { - return ERROR_FAIL; - } else { - return ERROR_OK; - } + if (eflg & MCP_EFLG_ERRORMASK) { + return ERROR_FAIL; + } else { + return ERROR_OK; + } } // This function works to get ERROR_CAN check_receive(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - static uint8_t status = 0; - mcp_get_status(spi, &status); + FuriHalSpiBusHandle* spi = mcp_can->spi; + static uint8_t status = 0; + mcp_get_status(spi, &status); - if(status & MCP_STAT_RXIF_MASK) - return ERROR_OK; - else - return ERROR_NOMSG; + if (status & MCP_STAT_RXIF_MASK) + return ERROR_OK; + else + return ERROR_NOMSG; } // To get the next buffer -ERROR_CAN get_next_buffer_free(FuriHalSpiBusHandle* spi, uint8_t* buffer_address) { - static uint8_t number_of_buffers[3] = {MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL}; - - uint8_t buffer_control_value = 0; - - for(uint8_t i = 0; i < 3; i++) { - read_register(spi, number_of_buffers[i], &buffer_control_value); - if(buffer_control_value == 0) { - *buffer_address = number_of_buffers[i] + 1; - return ERROR_OK; - } +ERROR_CAN get_next_buffer_free(FuriHalSpiBusHandle* spi, + uint8_t* buffer_address) { + static uint8_t number_of_buffers[3] = {MCP_TXB0CTRL, MCP_TXB1CTRL, + MCP_TXB2CTRL}; + + uint8_t buffer_control_value = 0; + + for (uint8_t i = 0; i < 3; i++) { + read_register(spi, number_of_buffers[i], &buffer_control_value); + if (buffer_control_value == 0) { + *buffer_address = number_of_buffers[i] + 1; + return ERROR_OK; } - return ERROR_ALLTXBUSY; + } + return ERROR_ALLTXBUSY; } void write_id(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { - uint32_t can_id = frame->canId; - - if(can_id > (0x7FF)) frame->ext = 1; - - uint8_t extension = frame->ext; - uint16_t canid; - uint8_t tbufdata[4]; - - canid = (uint16_t)(can_id & 0x0FFFF); - - if(extension == 1) { - tbufdata[MCP_EID0] = (uint8_t)(canid & 0xFF); - tbufdata[MCP_EID8] = (uint8_t)(canid >> 8); - canid = (uint16_t)(can_id >> 16); - tbufdata[MCP_SIDL] = (uint8_t)(canid & 0x03); - tbufdata[MCP_SIDL] += (uint8_t)((canid & 0x1C) << 3); - tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; - tbufdata[MCP_SIDH] = (uint8_t)(canid >> 5); - } else { - tbufdata[MCP_SIDH] = (uint8_t)(canid >> 3); - tbufdata[MCP_SIDL] = (uint8_t)((canid & 0x07) << 5); - tbufdata[MCP_EID0] = 0; - tbufdata[MCP_EID8] = 0; - } - - for(uint8_t i = 0; i < 4; i++) { - set_register(spi, address + i, tbufdata[i]); - } + uint32_t can_id = frame->canId; + + if (can_id > (0x7FF)) + frame->ext = 1; + + uint8_t extension = frame->ext; + uint16_t canid; + uint8_t tbufdata[4]; + + canid = (uint16_t) (can_id & 0x0FFFF); + + if (extension == 1) { + tbufdata[MCP_EID0] = (uint8_t) (canid & 0xFF); + tbufdata[MCP_EID8] = (uint8_t) (canid >> 8); + canid = (uint16_t) (can_id >> 16); + tbufdata[MCP_SIDL] = (uint8_t) (canid & 0x03); + tbufdata[MCP_SIDL] += (uint8_t) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (uint8_t) (canid >> 5); + } else { + tbufdata[MCP_SIDH] = (uint8_t) (canid >> 3); + tbufdata[MCP_SIDL] = (uint8_t) ((canid & 0x07) << 5); + tbufdata[MCP_EID0] = 0; + tbufdata[MCP_EID8] = 0; + } + + for (uint8_t i = 0; i < 4; i++) { + set_register(spi, address + i, tbufdata[i]); + } } -void write_dlc_register(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { - uint8_t data_lenght = frame->data_lenght; - uint8_t request = frame->req; +void write_dlc_register(FuriHalSpiBusHandle* spi, + uint8_t address, + CANFRAME* frame) { + uint8_t data_lenght = frame->data_lenght; + uint8_t request = frame->req; - if(request == 1) data_lenght |= MCP_RTR_MASK; - set_register(spi, address + 4, data_lenght); + if (request == 1) + data_lenght |= MCP_RTR_MASK; + set_register(spi, address + 4, data_lenght); } void write_buffer(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { - uint8_t data_lenght = frame->data_lenght; + uint8_t data_lenght = frame->data_lenght; - address = address + 5; + address = address + 5; - for(uint8_t i = 0; i < data_lenght; i++) { - set_register(spi, address + i, frame->buffer[i]); - } + for (uint8_t i = 0; i < data_lenght; i++) { + set_register(spi, address + i, frame->buffer[i]); + } } ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame) { - static CANFRAME auxiliar_frame; - memset(&auxiliar_frame, 0, sizeof(CANFRAME)); - auxiliar_frame.canId = frame->canId; - auxiliar_frame.data_lenght = frame->data_lenght; - auxiliar_frame.ext = frame->ext; - auxiliar_frame.req = frame->req; - - for(uint8_t i = 0; i < auxiliar_frame.data_lenght; i++) { - auxiliar_frame.buffer[i] = frame->buffer[i]; - } + static CANFRAME auxiliar_frame; + memset(&auxiliar_frame, 0, sizeof(CANFRAME)); + auxiliar_frame.canId = frame->canId; + auxiliar_frame.data_lenght = frame->data_lenght; + auxiliar_frame.ext = frame->ext; + auxiliar_frame.req = frame->req; - ERROR_CAN res; - uint8_t is_send_it = 0; - uint8_t free_buffer = 0; - uint16_t time_waiting = 0; + for (uint8_t i = 0; i < auxiliar_frame.data_lenght; i++) { + auxiliar_frame.buffer[i] = frame->buffer[i]; + } - do { - res = get_next_buffer_free(spi, &free_buffer); - furi_delay_us(1); - time_waiting++; - } while((res == ERROR_ALLTXBUSY) && (time_waiting < 1000)); + ERROR_CAN res; + uint8_t is_send_it = 0; + uint8_t free_buffer = 0; + uint16_t time_waiting = 0; - if(res != ERROR_OK) return res; + do { + res = get_next_buffer_free(spi, &free_buffer); + furi_delay_us(1); + time_waiting++; + } while ((res == ERROR_ALLTXBUSY) && (time_waiting < 1000)); - write_id(spi, free_buffer, &auxiliar_frame); + if (res != ERROR_OK) + return res; - write_dlc_register(spi, free_buffer, &auxiliar_frame); + write_id(spi, free_buffer, &auxiliar_frame); - write_buffer(spi, free_buffer, &auxiliar_frame); + write_dlc_register(spi, free_buffer, &auxiliar_frame); - modify_register(spi, free_buffer - 1, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M); + write_buffer(spi, free_buffer, &auxiliar_frame); - time_waiting = 0; - res = ERROR_ALLTXBUSY; + modify_register(spi, free_buffer - 1, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M); - do { - read_register(spi, free_buffer - 1, &is_send_it); - if(is_send_it == 0) res = ERROR_OK; - furi_delay_us(1); - time_waiting++; - } while((res != ERROR_OK) && (time_waiting < TIMEOUT)); + time_waiting = 0; + res = ERROR_ALLTXBUSY; + do { read_register(spi, free_buffer - 1, &is_send_it); + if (is_send_it == 0) + res = ERROR_OK; + furi_delay_us(1); + time_waiting++; + } while ((res != ERROR_OK) && (time_waiting < TIMEOUT)); - if(time_waiting == TIMEOUT) return ERROR_FAILTX; + read_register(spi, free_buffer - 1, &is_send_it); - return ERROR_OK; + if (time_waiting == TIMEOUT) + return ERROR_FAILTX; + + return ERROR_OK; } // This function is to sent a can message ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - return send_can_message(spi, frame); + FuriHalSpiBusHandle* spi = mcp_can->spi; + return send_can_message(spi, frame); } // This function works to alloc the struct MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate) { - MCP2515* mcp_can = malloc(sizeof(MCP2515)); - mcp_can->spi = spi_alloc(); - mcp_can->mode = mode; - mcp_can->bitRate = bitrate; - mcp_can->clck = clck; - return mcp_can; + MCP2515* mcp_can = malloc(sizeof(MCP2515)); + mcp_can->spi = spi_alloc(); + mcp_can->mode = mode; + mcp_can->bitRate = bitrate; + mcp_can->clck = clck; + return mcp_can; } // To free void free_mcp2515(MCP2515* mcp_can) { - mcp_reset(mcp_can->spi); - furi_hal_spi_bus_handle_deinit(mcp_can->spi); + mcp_reset(mcp_can->spi); + furi_hal_spi_bus_handle_deinit(mcp_can->spi); } // This function starts the SPI communication and set the MCP2515 device ERROR_CAN mcp2515_start(MCP2515* mcp_can) { - furi_hal_spi_bus_handle_init(mcp_can->spi); + furi_hal_spi_bus_handle_init(mcp_can->spi); - bool ret = true; + bool ret = true; - mcp_reset(mcp_can->spi); + mcp_reset(mcp_can->spi); - mcp_set_bitrate(mcp_can->spi, mcp_can->bitRate, mcp_can->clck); + mcp_set_bitrate(mcp_can->spi, mcp_can->bitRate, mcp_can->clck); - init_can_buffer(mcp_can->spi); + init_can_buffer(mcp_can->spi); - set_registers_init(mcp_can->spi); + set_registers_init(mcp_can->spi); - ret = set_mode(mcp_can->spi, mcp_can->mode); - if(!ret) return ERROR_FAILINIT; + ret = set_mode(mcp_can->spi, mcp_can->mode); + if (!ret) + return ERROR_FAILINIT; - return ERROR_OK; + return ERROR_OK; } // Init the mcp2515 device ERROR_CAN mcp2515_init(MCP2515* mcp_can) { - return mcp2515_start(mcp_can); -} \ No newline at end of file + return mcp2515_start(mcp_can); +} diff --git a/Canbus_app/libraries/mcp_can_2515.h b/Canbus_app/libraries/mcp_can_2515.h index 605aa6d..407eec1 100644 --- a/Canbus_app/libraries/mcp_can_2515.h +++ b/Canbus_app/libraries/mcp_can_2515.h @@ -1,28 +1,28 @@ #ifndef __MCP_CAN_2515_H_ #define __MCP_CAN_2515_H_ -#include "log_user.h" #include "Spi_lib.h" +#include "log_user.h" #ifdef __LOG_USER_H_ -// for debugging errors -#define DEBUG -#define DEEPDEBUG + // for debugging errors + #define DEBUG + #define DEEPDEBUG -// If you want to debug errors from the code comment the undef DEEPDEBUG -//#undef DEBUG -//#undef DEEPDEBUG + // If you want to debug errors from the code comment the undef DEEPDEBUG + // #undef DEBUG + // #undef DEEPDEBUG -#define PROVE -#undef PROVE + #define PROVE + #undef PROVE #endif // This is to know if the frame is extended or standard -#define CAN_IS_EXTENDED 0x80000000 +#define CAN_IS_EXTENDED 0x80000000 #define CAN_IS_REMOTE_REQUEST 0x40000000 -#define CAN_EXTENDED_ID 0x1FFFFFFF -#define MAX_LEN 8 +#define CAN_EXTENDED_ID 0x1FFFFFFF +#define MAX_LEN 8 // Mask of the bits static const uint8_t CANCTRL_REQOP = 0xE0; @@ -35,22 +35,23 @@ static const uint8_t CANSTAT_OPM = 0xE0; static const uint8_t CANSTAT_ICOD = 0x0E; // Instructions for the CLKOUT -#define CLKOUT_ENABLE 0x04 +#define CLKOUT_ENABLE 0x04 #define CLKOUT_DISABLE 0x00 -#define CLKOUT_PS1 0x00 -#define CLKOUT_PS2 0x01 -#define CLKOUT_PS4 0x02 -#define CLKOUT_PS8 0x03 -#define TIMEOUT 2500 +#define CLKOUT_PS1 0x00 +#define CLKOUT_PS2 0x01 +#define CLKOUT_PS4 0x02 +#define CLKOUT_PS8 0x03 +#define TIMEOUT 2500 /* -* CAN CLOCK CONFIGURATION -*/ + * CAN CLOCK CONFIGURATION + */ -/* -* The clock frequency of the MCP2515 module is 16MHZ but if anyone change of module you can use another clock value -* At the moment the library only have a bitrate of 1000KBPS, 500KBPS, 250 KBPS, 125 KBPS -*/ +/* + * The clock frequency of the MCP2515 module is 16MHZ but if anyone change of + * module you can use another clock value At the moment the library only have a + * bitrate of 1000KBPS, 500KBPS, 250 KBPS, 125 KBPS + */ // These are to config the clock in 16MHz #define MCP_16MHz_1000kBPS_CFG1 (0x00) @@ -104,242 +105,242 @@ static const uint8_t CANSTAT_ICOD = 0x0E; #define MCP_20MHz_125kBPS_CFG3 (0x87) /* -* Begin mt -*/ + * Begin mt + */ -#define TIMEOUTVALUE 1000 // In microseconds -#define MCP_SIDH 0 -#define MCP_SIDL 1 -#define MCP_EID8 2 -#define MCP_EID0 3 -#define MPC_DLC 4 -#define MPC_DATA 5 +#define TIMEOUTVALUE 1000 // In microseconds +#define MCP_SIDH 0 +#define MCP_SIDL 1 +#define MCP_EID8 2 +#define MCP_EID0 3 +#define MPC_DLC 4 +#define MPC_DATA 5 #define MCP_TXB_EXIDE_M 0x08 /* In TXBnSIDL */ -#define MCP_DLC_MASK 0x0F /* 4 LSBits */ -#define MCP_RTR_MASK 0x40 /* (1<<6) Bit 6 */ +#define MCP_DLC_MASK 0x0F /* 4 LSBits */ +#define MCP_RTR_MASK 0x40 /* (1<<6) Bit 6 */ -#define MCP_RXB_RX_ANY 0x60 -#define MCP_RXB_RX_EXT 0x40 -#define MCP_RXB_RX_STD 0x20 +#define MCP_RXB_RX_ANY 0x60 +#define MCP_RXB_RX_EXT 0x40 +#define MCP_RXB_RX_STD 0x20 #define MCP_RXB_RX_STDEXT 0x00 -#define MCP_RXB_RX_MASK 0x60 +#define MCP_RXB_RX_MASK 0x60 #define MCP_RXB_BUKT_MASK (1 << 2) /* -* Bits in the TXBnCRTL -*/ + * Bits in the TXBnCRTL + */ typedef enum { - MCP_TXB_TXBUFE_M = 0x80, - MCP_TXB_ABTF_M = 0x40, - MCP_TXB_MLOA_M = 0x20, - MCP_TXB_TXERR_M = 0x10, - MCP_TXB_TXREQ_M = 0x08, - MCP_TXB_TXIE_M = 0x04, - MCP_TXB_TXP10_M = 0x03, - // - MCP_TXB_RTR_M = 0x40, /* In TXBnDLC */ - MCP_RXB_IDE_M = 0x08, /* In RXBnSIDL */ - MCP_RXB_RTR_M = 0x40, /* In RXBnDLC */ - // - MCP_STAT_RXIF_MASK = (0x03), - MCP_STAT_RX0IF = (1 << 0), - MCP_STAT_RX1IF = (1 << 1), - // For erro detection - MCP_EFLG_RX1OVR = (1 << 7), - MCP_EFLG_RX0OVR = (1 << 6), - // - MCP_EFLG_TXBO = (1 << 5), - MCP_EFLG_TXEP = (1 << 4), - MCP_EFLG_RXEP = (1 << 3), - MCP_EFLG_TXWAR = (1 << 2), - MCP_EFLG_RXWAR = (1 << 1), - MCP_EFLG_EWARN = (1 << 0), - MCP_EFLG_ERRORMASK = (0xF8), /* 5 MS-Bits */ - // - MCP_BxBFS_MASK = 0x30, - MCP_BxBFE_MASK = 0x0C, - MCP_BxBFM_MASK = 0x03, - // - MCP_BxRTS_MASK = 0x38, - MCP_BxRTSM_MASK = 0x07, + MCP_TXB_TXBUFE_M = 0x80, + MCP_TXB_ABTF_M = 0x40, + MCP_TXB_MLOA_M = 0x20, + MCP_TXB_TXERR_M = 0x10, + MCP_TXB_TXREQ_M = 0x08, + MCP_TXB_TXIE_M = 0x04, + MCP_TXB_TXP10_M = 0x03, + // + MCP_TXB_RTR_M = 0x40, /* In TXBnDLC */ + MCP_RXB_IDE_M = 0x08, /* In RXBnSIDL */ + MCP_RXB_RTR_M = 0x40, /* In RXBnDLC */ + // + MCP_STAT_RXIF_MASK = (0x03), + MCP_STAT_RX0IF = (1 << 0), + MCP_STAT_RX1IF = (1 << 1), + // For erro detection + MCP_EFLG_RX1OVR = (1 << 7), + MCP_EFLG_RX0OVR = (1 << 6), + // + MCP_EFLG_TXBO = (1 << 5), + MCP_EFLG_TXEP = (1 << 4), + MCP_EFLG_RXEP = (1 << 3), + MCP_EFLG_TXWAR = (1 << 2), + MCP_EFLG_RXWAR = (1 << 1), + MCP_EFLG_EWARN = (1 << 0), + MCP_EFLG_ERRORMASK = (0xF8), /* 5 MS-Bits */ + // + MCP_BxBFS_MASK = 0x30, + MCP_BxBFE_MASK = 0x0C, + MCP_BxBFM_MASK = 0x03, + // + MCP_BxRTS_MASK = 0x38, + MCP_BxRTSM_MASK = 0x07, } TXBnCRTL; // CANINTF bits typedef enum { - MCP_RX0IF = 0x01, - MCP_RX1IF = 0x02, - MCP_TX0IF = 0x04, - MCP_TX1IF = 0x08, - MCP_TX2IF = 0x10, - MCP_ERRIF = 0x20, - MCP_WAKIF = 0x40, - MCP_MERRF = 0x80, + MCP_RX0IF = 0x01, + MCP_RX1IF = 0x02, + MCP_TX0IF = 0x04, + MCP_TX1IF = 0x08, + MCP_TX2IF = 0x10, + MCP_ERRIF = 0x20, + MCP_WAKIF = 0x40, + MCP_MERRF = 0x80, } CANINTF; // MCP2515 MODES typedef enum { - MCP_NORMAL = 0x00, - MCP_SLEEP = 0x20, - MCP_LOOPBACK = 0x40, - MCP_LISTENONLY = 0x60, - MODE_CONFIG = 0x80, - MODE_POWERUP = 0xE0, - MODE_MASK = 0xE0, - MODE_ONESHOT = 0x08, + MCP_NORMAL = 0x00, + MCP_SLEEP = 0x20, + MCP_LOOPBACK = 0x40, + MCP_LISTENONLY = 0x60, + MODE_CONFIG = 0x80, + MODE_POWERUP = 0xE0, + MODE_MASK = 0xE0, + MODE_ONESHOT = 0x08, } MCP_MODE; // MCP2515 registers enum Registers { - MCP_RXF0SIDH = 0x00, - MCP_RXF0SIDL = 0x01, - MCP_RXF0EID8 = 0x02, - MCP_RXF0EID0 = 0x03, - MCP_RXF1SIDH = 0x04, - MCP_RXF1SIDL = 0x05, - MCP_RXF1EID8 = 0x06, - MCP_RXF1EID0 = 0x07, - MCP_RXF2SIDH = 0x08, - MCP_RXF2SIDL = 0x09, - MCP_RXF2EID8 = 0x0A, - MCP_RXF2EID0 = 0x0B, - MCP_BFPCTRL = 0x0C, - MCP_TXRTSCTRL = 0x0D, - MCP_CANSTAT = 0x0E, - MCP_CANCTRL = 0x0F, - MCP_RXF3SIDH = 0x10, - MCP_RXF3SIDL = 0x11, - MCP_RXF3EID8 = 0x12, - MCP_RXF3EID0 = 0x13, - MCP_RXF4SIDH = 0x14, - MCP_RXF4SIDL = 0x15, - MCP_RXF4EID8 = 0x16, - MCP_RXF4EID0 = 0x17, - MCP_RXF5SIDH = 0x18, - MCP_RXF5SIDL = 0x19, - MCP_RXF5EID8 = 0x1A, - MCP_RXF5EID0 = 0x1B, - MCP_TEC = 0x1C, - MCP_REC = 0x1D, - MCP_RXM0SIDH = 0x20, - MCP_RXM0SIDL = 0x21, - MCP_RXM0EID8 = 0x22, - MCP_RXM0EID0 = 0x23, - MCP_RXM1SIDH = 0x24, - MCP_RXM1SIDL = 0x25, - MCP_RXM1EID8 = 0x26, - MCP_RXM1EID0 = 0x27, - MCP_CNF3 = 0x28, - MCP_CNF2 = 0x29, - MCP_CNF1 = 0x2A, - MCP_CANINTE = 0x2B, - MCP_CANINTF = 0x2C, - MCP_EFLG = 0x2D, - MCP_TXB0CTRL = 0x30, - MCP_TXB0SIDH = 0x31, - MCP_TXB0SIDL = 0x32, - MCP_TXB0EID8 = 0x33, - MCP_TXB0EID0 = 0x34, - MCP_TXB0DLC = 0x35, - MCP_TXB0DATA = 0x36, - MCP_TXB1CTRL = 0x40, - MCP_TXB1SIDH = 0x41, - MCP_TXB1SIDL = 0x42, - MCP_TXB1EID8 = 0x43, - MCP_TXB1EID0 = 0x44, - MCP_TXB1DLC = 0x45, - MCP_TXB1DATA = 0x46, - MCP_TXB2CTRL = 0x50, - MCP_TXB2SIDH = 0x51, - MCP_TXB2SIDL = 0x52, - MCP_TXB2EID8 = 0x53, - MCP_TXB2EID0 = 0x54, - MCP_TXB2DLC = 0x55, - MCP_TXB2DATA = 0x56, - MCP_RXB0CTRL = 0x60, - MCP_RXB0SIDH = 0x61, - MCP_RXB0SIDL = 0x62, - MCP_RXB0EID8 = 0x63, - MCP_RXB0EID0 = 0x64, - MCP_RXB0DLC = 0x65, - MCP_RXB0DATA = 0x66, - MCP_RXB1CTRL = 0x70, - MCP_RXB1SIDH = 0x71, - MCP_RXB1SIDL = 0x72, - MCP_RXB1EID8 = 0x73, - MCP_RXB1EID0 = 0x74, - MCP_RXB1DLC = 0x75, - MCP_RXB1DATA = 0x76 + MCP_RXF0SIDH = 0x00, + MCP_RXF0SIDL = 0x01, + MCP_RXF0EID8 = 0x02, + MCP_RXF0EID0 = 0x03, + MCP_RXF1SIDH = 0x04, + MCP_RXF1SIDL = 0x05, + MCP_RXF1EID8 = 0x06, + MCP_RXF1EID0 = 0x07, + MCP_RXF2SIDH = 0x08, + MCP_RXF2SIDL = 0x09, + MCP_RXF2EID8 = 0x0A, + MCP_RXF2EID0 = 0x0B, + MCP_BFPCTRL = 0x0C, + MCP_TXRTSCTRL = 0x0D, + MCP_CANSTAT = 0x0E, + MCP_CANCTRL = 0x0F, + MCP_RXF3SIDH = 0x10, + MCP_RXF3SIDL = 0x11, + MCP_RXF3EID8 = 0x12, + MCP_RXF3EID0 = 0x13, + MCP_RXF4SIDH = 0x14, + MCP_RXF4SIDL = 0x15, + MCP_RXF4EID8 = 0x16, + MCP_RXF4EID0 = 0x17, + MCP_RXF5SIDH = 0x18, + MCP_RXF5SIDL = 0x19, + MCP_RXF5EID8 = 0x1A, + MCP_RXF5EID0 = 0x1B, + MCP_TEC = 0x1C, + MCP_REC = 0x1D, + MCP_RXM0SIDH = 0x20, + MCP_RXM0SIDL = 0x21, + MCP_RXM0EID8 = 0x22, + MCP_RXM0EID0 = 0x23, + MCP_RXM1SIDH = 0x24, + MCP_RXM1SIDL = 0x25, + MCP_RXM1EID8 = 0x26, + MCP_RXM1EID0 = 0x27, + MCP_CNF3 = 0x28, + MCP_CNF2 = 0x29, + MCP_CNF1 = 0x2A, + MCP_CANINTE = 0x2B, + MCP_CANINTF = 0x2C, + MCP_EFLG = 0x2D, + MCP_TXB0CTRL = 0x30, + MCP_TXB0SIDH = 0x31, + MCP_TXB0SIDL = 0x32, + MCP_TXB0EID8 = 0x33, + MCP_TXB0EID0 = 0x34, + MCP_TXB0DLC = 0x35, + MCP_TXB0DATA = 0x36, + MCP_TXB1CTRL = 0x40, + MCP_TXB1SIDH = 0x41, + MCP_TXB1SIDL = 0x42, + MCP_TXB1EID8 = 0x43, + MCP_TXB1EID0 = 0x44, + MCP_TXB1DLC = 0x45, + MCP_TXB1DATA = 0x46, + MCP_TXB2CTRL = 0x50, + MCP_TXB2SIDH = 0x51, + MCP_TXB2SIDL = 0x52, + MCP_TXB2EID8 = 0x53, + MCP_TXB2EID0 = 0x54, + MCP_TXB2DLC = 0x55, + MCP_TXB2DATA = 0x56, + MCP_RXB0CTRL = 0x60, + MCP_RXB0SIDH = 0x61, + MCP_RXB0SIDL = 0x62, + MCP_RXB0EID8 = 0x63, + MCP_RXB0EID0 = 0x64, + MCP_RXB0DLC = 0x65, + MCP_RXB0DATA = 0x66, + MCP_RXB1CTRL = 0x70, + MCP_RXB1SIDH = 0x71, + MCP_RXB1SIDL = 0x72, + MCP_RXB1EID8 = 0x73, + MCP_RXB1EID0 = 0x74, + MCP_RXB1DLC = 0x75, + MCP_RXB1DATA = 0x76 }; // COMMAND INSTRUCTIONS enum INSTRUCTION { - INSTRUCTION_WRITE = 0x02, - INSTRUCTION_READ = 0x03, - INSTRUCTION_BITMOD = 0x05, - INSTRUCTION_LOAD_TX0 = 0x40, - INSTRUCTION_LOAD_TX1 = 0x42, - INSTRUCTION_LOAD_TX2 = 0x44, - INSTRUCTION_RTS_TX0 = 0x81, - INSTRUCTION_RTS_TX1 = 0x82, - INSTRUCTION_RTS_TX2 = 0x84, - INSTRUCTION_RTS_ALL = 0x87, - INSTRUCTION_READ_RX0 = 0x90, - INSTRUCTION_READ_RX1 = 0x94, - INSTRUCTION_READ_STATUS = 0xA0, - INSTRUCTION_RX_STATUS = 0xB0, - INSTRUCTION_RESET = 0xC0 + INSTRUCTION_WRITE = 0x02, + INSTRUCTION_READ = 0x03, + INSTRUCTION_BITMOD = 0x05, + INSTRUCTION_LOAD_TX0 = 0x40, + INSTRUCTION_LOAD_TX1 = 0x42, + INSTRUCTION_LOAD_TX2 = 0x44, + INSTRUCTION_RTS_TX0 = 0x81, + INSTRUCTION_RTS_TX1 = 0x82, + INSTRUCTION_RTS_TX2 = 0x84, + INSTRUCTION_RTS_ALL = 0x87, + INSTRUCTION_READ_RX0 = 0x90, + INSTRUCTION_READ_RX1 = 0x94, + INSTRUCTION_READ_STATUS = 0xA0, + INSTRUCTION_RX_STATUS = 0xB0, + INSTRUCTION_RESET = 0xC0 }; // CAN ERRORS typedef enum { - ERROR_OK = 0, - ERROR_FAIL = 1, - ERROR_ALLTXBUSY = 2, - ERROR_FAILINIT = 3, - ERROR_FAILTX = 4, - ERROR_NOMSG = 5, - ERROR_GET_TXB_FTIMEOUT = 6, - ERROR_SEND_MSG_TIMEOUT = 7, + ERROR_OK = 0, + ERROR_FAIL = 1, + ERROR_ALLTXBUSY = 2, + ERROR_FAILINIT = 3, + ERROR_FAILTX = 4, + ERROR_NOMSG = 5, + ERROR_GET_TXB_FTIMEOUT = 6, + ERROR_SEND_MSG_TIMEOUT = 7, } ERROR_CAN; // MCP2515 BITRATES VALUES typedef enum { - MCP_125KPS, - MCP_250KBPS, - MCP_500KBPS, - MCP_1000KBPS, + MCP_125KPS, + MCP_250KBPS, + MCP_500KBPS, + MCP_1000KBPS, } MCP_BITRATE; // MCP CLOCKS typedef enum { - MCP_8MHZ, - MCP_16MHZ, - MCP_20MHZ, + MCP_8MHZ, + MCP_16MHZ, + MCP_20MHZ, } MCP_CLOCK; // This a struct to define the Can Frame typedef struct { - uint32_t canId; - uint8_t ext; - uint8_t req; - uint8_t data_lenght; - uint8_t buffer[MAX_LEN]; + uint32_t canId; + uint8_t ext; + uint8_t req; + uint8_t data_lenght; + uint8_t buffer[MAX_LEN]; } CANFRAME; // This Struct is to cinfig and work with the MCP2515 device typedef struct { - MCP_MODE mode; - FuriHalSpiBusHandle* spi; - MCP_BITRATE bitRate; - MCP_CLOCK clck; + MCP_MODE mode; + FuriHalSpiBusHandle* spi; + MCP_BITRATE bitRate; + MCP_CLOCK clck; } MCP2515; /* This are the function that we can work -* in the main code to config, write and read -* the CANBUS network. -*/ + * in the main code to config, write and read + * the CANBUS network. + */ // Config the paramaters of MCP2515 MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate); @@ -364,7 +365,9 @@ bool set_loop_back_mode(MCP2515* mcp_can); uint8_t get_error(MCP2515* mcp_can); ERROR_CAN check_error(MCP2515* mcp_can); ERROR_CAN check_receive(MCP2515* mcp_can); -ERROR_CAN read_can_message(MCP2515* mcp_can, CANFRAME* frame); // Read a CAN BUS message +ERROR_CAN read_can_message(MCP2515* mcp_can, + CANFRAME* frame); // Read a CAN BUS message -ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame); // Send a CANBUS Frame -#endif \ No newline at end of file +ERROR_CAN send_can_frame(MCP2515* mcp_can, + CANFRAME* frame); // Send a CANBUS Frame +#endif diff --git a/Canbus_app/scenes/AboutUs.c b/Canbus_app/scenes/AboutUs.c index 4509d9b..a2242c7 100644 --- a/Canbus_app/scenes/AboutUs.c +++ b/Canbus_app/scenes/AboutUs.c @@ -2,116 +2,126 @@ static uint32_t state = 0; -static void button_callback(GuiButtonType result, InputType input, void* context) { - App* app = context; - - if(input == InputTypePress) { - if(result == GuiButtonTypeRight) { - state = state + 1; - scene_manager_handle_custom_event(app->scene_manager, ButtonGetPressed); - } - if(result == GuiButtonTypeLeft) { - state = state - 1; - scene_manager_handle_custom_event(app->scene_manager, ButtonGetPressed); - } +static void button_callback(GuiButtonType result, + InputType input, + void* context) { + App* app = context; + + if (input == InputTypePress) { + if (result == GuiButtonTypeRight) { + state = state + 1; + scene_manager_handle_custom_event(app->scene_manager, ButtonGetPressed); } + if (result == GuiButtonTypeLeft) { + state = state - 1; + scene_manager_handle_custom_event(app->scene_manager, ButtonGetPressed); + } + } } static void draw_present_view(App* app) { - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "ELECTRONIC CATS"); - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignCenter, FontPrimary, "Presents:"); - - widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", button_callback, app); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "ELECTRONIC CATS"); + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignCenter, + FontPrimary, "Presents:"); + + widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", + button_callback, app); } static void draw_can_app_view(App* app) { - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "CANBUS APP"); - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignCenter, FontSecondary, "By: Adonai Diaz"); - - widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", button_callback, app); - widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", button_callback, app); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "CANBUS APP"); + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignCenter, + FontSecondary, "By: Adonai Diaz"); + + widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", + button_callback, app); + widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", + button_callback, app); } static void draw_version_view(App* app) { - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "Version:"); - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignCenter, FontSecondary, "CANBUS APP V1.1.0.0"); - widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", button_callback, app); - widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", button_callback, app); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "Version:"); + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignCenter, + FontSecondary, "CANBUS APP V1.1.0.0"); + widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", + button_callback, app); + widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", + button_callback, app); } static void draw_license_view(App* app) { - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "MIT LICENSE"); - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignCenter, FontSecondary, "Copyright(c) 2024"); - widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", button_callback, app); - widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", button_callback, app); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "MIT LICENSE"); + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignCenter, + FontSecondary, "Copyright(c) 2024"); + widget_add_button_element(app->widget, GuiButtonTypeRight, "Next", + button_callback, app); + widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", + button_callback, app); } void draw_more_info_view(App* app) { - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 10, AlignCenter, AlignCenter, FontPrimary, "More info:"); - widget_add_string_element( - app->widget, 65, 25, AlignCenter, AlignCenter, FontSecondary, "https://github.com"); - - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignCenter, FontSecondary, "/ElectronicCats"); - - widget_add_string_element( - app->widget, 65, 45, AlignCenter, AlignCenter, FontSecondary, "/flipper-MCP2515-CANBUS"); - widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", button_callback, app); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 10, AlignCenter, AlignCenter, + FontPrimary, "More info:"); + widget_add_string_element(app->widget, 65, 25, AlignCenter, AlignCenter, + FontSecondary, "https://github.com"); + + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignCenter, + FontSecondary, "/ElectronicCats"); + + widget_add_string_element(app->widget, 65, 45, AlignCenter, AlignCenter, + FontSecondary, "/flipper-MCP2515-CANBUS"); + widget_add_button_element(app->widget, GuiButtonTypeLeft, "Prev", + button_callback, app); } void app_scene_about_us_on_enter(void* context) { - App* app = context; - state = 0; - draw_present_view(app); - view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); + App* app = context; + state = 0; + draw_present_view(app); + view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); } bool app_scene_about_us_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - - uint32_t state_2 = state; - - if(event.type == SceneManagerEventTypeCustom) { - switch(state_2) { - case 0: - draw_present_view(app); - break; - case 1: - draw_can_app_view(app); - break; - case 2: - draw_version_view(app); - break; - case 3: - draw_license_view(app); - break; - case 4: - draw_more_info_view(app); - break; - default: - break; - } - consumed = true; + App* app = context; + bool consumed = false; + + uint32_t state_2 = state; + + if (event.type == SceneManagerEventTypeCustom) { + switch (state_2) { + case 0: + draw_present_view(app); + break; + case 1: + draw_can_app_view(app); + break; + case 2: + draw_version_view(app); + break; + case 3: + draw_license_view(app); + break; + case 4: + draw_more_info_view(app); + break; + default: + break; } - return consumed; + consumed = true; + } + return consumed; } void app_scene_about_us_on_exit(void* context) { - App* app = context; - widget_reset(app->widget); -} \ No newline at end of file + App* app = context; + widget_reset(app->widget); +} diff --git a/Canbus_app/scenes/SnifferOption.c b/Canbus_app/scenes/SnifferOption.c index edab1db..a7ba205 100644 --- a/Canbus_app/scenes/SnifferOption.c +++ b/Canbus_app/scenes/SnifferOption.c @@ -2,393 +2,390 @@ bool condition = true; -// --------------------- Functons to save the logs -------------------------------------------------- -char* sequential_file_resolve_path( - Storage* storage, - const char* dir, - const char* prefix, - const char* extension) { - if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) { - return NULL; +// --------------------- Functons to save the logs +// -------------------------------------------------- +char* sequential_file_resolve_path(Storage* storage, + const char* dir, + const char* prefix, + const char* extension) { + if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) { + return NULL; + } + + char file_path[256]; + int file_index = 0; + + do { + if (snprintf(file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, + file_index, extension) < 0) { + return NULL; } + file_index++; + } while (storage_file_exists(storage, file_path)); - char file_path[256]; - int file_index = 0; - - do { - if(snprintf( - file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, file_index, extension) < - 0) { - return NULL; - } - file_index++; - } while(storage_file_exists(storage, file_path)); - - return strdup(file_path); + return strdup(file_path); } -char* sequential_file_resolve_path_address( - Storage* storage, - uint32_t address, - const char* dir, - const char* prefix, - const char* extension) { - FuriHalRtcDateTime datatime; - furi_hal_rtc_get_datetime(&datatime); - char file_path[256]; - int file_index = 0; - uint16_t year = datatime.year; - uint8_t month = datatime.month; - uint8_t day = datatime.day; - - if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) { - return NULL; +char* sequential_file_resolve_path_address(Storage* storage, + uint32_t address, + const char* dir, + const char* prefix, + const char* extension) { + FuriHalRtcDateTime datatime; + furi_hal_rtc_get_datetime(&datatime); + char file_path[256]; + int file_index = 0; + uint16_t year = datatime.year; + uint8_t month = datatime.month; + uint8_t day = datatime.day; + + if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) { + return NULL; + } + + do { + if (snprintf(file_path, sizeof(file_path), "%s/%s_0x%lx_%u_%u_%i_%d.%s", + dir, prefix, address, day, month, year, file_index, + extension) < 0) { + return NULL; } + file_index++; + } while (storage_file_exists(storage, file_path)); - do { - if(snprintf( - file_path, - sizeof(file_path), - "%s/%s_0x%lx_%u_%u_%i_%d.%s", - dir, - prefix, - address, - day, - month, - year, - file_index, - extension) < 0) { - return NULL; - } - file_index++; - } while(storage_file_exists(storage, file_path)); - - return strdup(file_path); + return strdup(file_path); } void save_data_on_log(App* app) { - strcpy(app->log_file_path, sequential_file_resolve_path(app->storage, PATHLOGS, "Log", "log")); - if(app->log_file_path != NULL) { - if(storage_file_open(app->log_file, app->log_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) { - app->log_file_ready = true; - } else { - dialog_message_show_storage_error(app->dialogs, "Cannot open log file"); - } + strcpy(app->log_file_path, + sequential_file_resolve_path(app->storage, PATHLOGS, "Log", "log")); + if (app->log_file_path != NULL) { + if (storage_file_open(app->log_file, app->log_file_path, FSAM_WRITE, + FSOM_CREATE_ALWAYS)) { + app->log_file_ready = true; } else { - dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path"); + dialog_message_show_storage_error(app->dialogs, "Cannot open log file"); } + } else { + dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path"); + } } void save_address_data_on_log(App* app) { - uint32_t can_id = app->frameArray[app->sniffer_index].canId; - strcpy( - app->log_file_path, - sequential_file_resolve_path_address(app->storage, can_id, PATHLOGS, "L", "log")); - if(app->log_file_path != NULL) { - if(storage_file_open(app->log_file, app->log_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) { - app->log_file_ready = true; - } else { - dialog_message_show_storage_error(app->dialogs, "Cannot open log file"); - } + uint32_t can_id = app->frameArray[app->sniffer_index].canId; + strcpy(app->log_file_path, sequential_file_resolve_path_address( + app->storage, can_id, PATHLOGS, "L", "log")); + if (app->log_file_path != NULL) { + if (storage_file_open(app->log_file, app->log_file_path, FSAM_WRITE, + FSOM_CREATE_ALWAYS)) { + app->log_file_ready = true; } else { - dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path"); + dialog_message_show_storage_error(app->dialogs, "Cannot open log file"); } + } else { + dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path"); + } } void close_file_on_data_log(App* app) { - app->log_file_ready = false; - if(app->log_file && storage_file_is_open(app->log_file)) { - storage_file_close(app->log_file); - } + app->log_file_ready = false; + if (app->log_file && storage_file_is_open(app->log_file)) { + storage_file_close(app->log_file); + } } static void write_data_on_file(CANFRAME frame, File* file, uint32_t time) { - FuriString* text_file = furi_string_alloc(); - furi_string_cat_printf(text_file, "id:%lx \t\tlen: %u \t", frame.canId, frame.data_lenght); - for(uint8_t i = 0; i < (frame.data_lenght); i++) { - furi_string_cat_printf(text_file, "[%u]:%u \t\t", i, frame.buffer[i]); - } - furi_string_cat_printf(text_file, " Time(ms): %li\n", time); - storage_file_write(file, furi_string_get_cstr(text_file), furi_string_size(text_file)); - furi_string_reset(text_file); - furi_string_free(text_file); + FuriString* text_file = furi_string_alloc(); + furi_string_cat_printf(text_file, "id:%lx \t\tlen: %u \t", frame.canId, + frame.data_lenght); + for (uint8_t i = 0; i < (frame.data_lenght); i++) { + furi_string_cat_printf(text_file, "[%u]:%u \t\t", i, frame.buffer[i]); + } + furi_string_cat_printf(text_file, " Time(ms): %li\n", time); + storage_file_write(file, furi_string_get_cstr(text_file), + furi_string_size(text_file)); + furi_string_reset(text_file); + furi_string_free(text_file); } // Call backs void sniffing_callback(void* context, uint32_t index) { - App* app = context; - app->sniffer_index = index; - scene_manager_handle_custom_event(app->scene_manager, EntryEvent); + App* app = context; + app->sniffer_index = index; + scene_manager_handle_custom_event(app->scene_manager, EntryEvent); } void draw_box_text(App* app) { - furi_string_reset(app->text); - - furi_string_cat_printf( - app->text, - "ADDR: %lx DLC: %u \n", - app->frameArray[app->sniffer_index].canId, - app->frameArray[app->sniffer_index].data_lenght); - - for(uint8_t i = 0; i < (app->frameArray[app->sniffer_index].data_lenght); i++) { - furi_string_cat_printf( - app->text, "[%u]: %x ", i, app->frameArray[app->sniffer_index].buffer[i]); - } - - furi_string_cat_printf(app->text, "\ntime: %li ms", app->times[app->sniffer_index]); - text_box_set_text(app->textBox, furi_string_get_cstr(app->text)); - text_box_set_focus(app->textBox, TextBoxFocusEnd); + furi_string_reset(app->text); + + furi_string_cat_printf(app->text, "ADDR: %lx DLC: %u \n", + app->frameArray[app->sniffer_index].canId, + app->frameArray[app->sniffer_index].data_lenght); + + for (uint8_t i = 0; i < (app->frameArray[app->sniffer_index].data_lenght); + i++) { + furi_string_cat_printf(app->text, "[%u]: %x ", i, + app->frameArray[app->sniffer_index].buffer[i]); + } + + furi_string_cat_printf(app->text, "\ntime: %li ms", + app->times[app->sniffer_index]); + text_box_set_text(app->textBox, furi_string_get_cstr(app->text)); + text_box_set_focus(app->textBox, TextBoxFocusEnd); } -// --------------------------- Thread on work ------------------------------------------------------ +// --------------------------- Thread on work +// ------------------------------------------------------ static void callback_interrupt(void* context) { - App* app = context; - furi_thread_flags_set(furi_thread_get_id(app->thread), WorkerflagReceived); + App* app = context; + furi_thread_flags_set(furi_thread_get_id(app->thread), WorkerflagReceived); } static int32_t worker_sniffing(void* context) { - App* app = context; - MCP2515* mcp_can = app->mcp_can; - CANFRAME frame = app->can_frame; - FuriString* text_label = furi_string_alloc(); - - uint8_t num_of_devices = 0; - uint32_t time_select = 0; - UNUSED(time_select); - - bool run = true; - bool first_address = true; - - furi_hal_gpio_init(&gpio_swclk, GpioModeInterruptFall, GpioPullNo, GpioSpeedVeryHigh); - furi_hal_gpio_add_int_callback(&gpio_swclk, callback_interrupt, app); - - mcp_can->mode = MCP_LISTENONLY; - ERROR_CAN debugStatus = mcp2515_init(mcp_can); + App* app = context; + MCP2515* mcp_can = app->mcp_can; + CANFRAME frame = app->can_frame; + FuriString* text_label = furi_string_alloc(); + + uint8_t num_of_devices = 0; + uint32_t time_select = 0; + UNUSED(time_select); + + bool run = true; + bool first_address = true; + + furi_hal_gpio_init(&gpio_swclk, GpioModeInterruptFall, GpioPullNo, + GpioSpeedVeryHigh); + furi_hal_gpio_add_int_callback(&gpio_swclk, callback_interrupt, app); + + mcp_can->mode = MCP_LISTENONLY; + ERROR_CAN debugStatus = mcp2515_init(mcp_can); + + if (debugStatus != ERROR_OK) { + run = false; + view_dispatcher_send_custom_event(app->view_dispatcher, + DEVICE_NO_CONNECTED); + } + + while (run) { + bool new = true; + + uint32_t events = furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, + FuriFlagWaitAny, FuriWaitForever); + if (events & WorkerflagStop) + break; + if (events & WorkerflagReceived) { + read_can_message(mcp_can, &frame); + + if (first_address) { + app->frameArray[num_of_devices] = frame; + app->times[num_of_devices] = 0; + app->sniffer_index_aux = num_of_devices; + app->times[num_of_devices] = 0; + app->time = 0; + app->current_time[num_of_devices] = furi_get_tick(); + num_of_devices++; + first_address = false; + + furi_string_reset(text_label); + furi_string_cat_printf(text_label, "0x%lx", + app->frameArray[app->sniffer_index_aux].canId); + + submenu_add_item(app->submenu, furi_string_get_cstr(text_label), + app->sniffer_index_aux, sniffing_callback, app); + + if (app->save_logs == SaveAll) { + save_data_on_log(app); + } + } + + if (condition) { + for (uint8_t i = 0; i < num_of_devices; i++) { + if (frame.canId == app->frameArray[i].canId) { + app->frameArray[i] = frame; + app->times[i] = furi_get_tick() - app->current_time[i]; + app->time = app->times[i]; + app->current_time[i] = furi_get_tick(); + new = false; + break; + } + } - if(debugStatus != ERROR_OK) { - run = false; - view_dispatcher_send_custom_event(app->view_dispatcher, DEVICE_NO_CONNECTED); - } + if (new && (num_of_devices < 100)) { + app->frameArray[num_of_devices] = frame; + app->times[num_of_devices] = 0; + app->sniffer_index_aux = num_of_devices; + app->times[num_of_devices] = 0; + app->time = 0; + app->current_time[num_of_devices] = furi_get_tick(); + num_of_devices++; + + furi_string_reset(text_label); + furi_string_cat_printf(text_label, "0x%lx", + app->frameArray[app->sniffer_index_aux].canId); + + submenu_add_item(app->submenu, furi_string_get_cstr(text_label), + app->sniffer_index_aux, sniffing_callback, app); + } - while(run) { - bool new = true; - - uint32_t events = - furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever); - if(events & WorkerflagStop) break; - if(events & WorkerflagReceived) { - read_can_message(mcp_can, &frame); - - if(first_address) { - app->frameArray[num_of_devices] = frame; - app->times[num_of_devices] = 0; - app->sniffer_index_aux = num_of_devices; - app->times[num_of_devices] = 0; - app->time = 0; - app->current_time[num_of_devices] = furi_get_tick(); - num_of_devices++; - first_address = false; - - furi_string_reset(text_label); - furi_string_cat_printf( - text_label, "0x%lx", app->frameArray[app->sniffer_index_aux].canId); - - submenu_add_item( - app->submenu, - furi_string_get_cstr(text_label), - app->sniffer_index_aux, - sniffing_callback, - app); - - if(app->save_logs == SaveAll) { - save_data_on_log(app); - } - } - - if(condition) { - for(uint8_t i = 0; i < num_of_devices; i++) { - if(frame.canId == app->frameArray[i].canId) { - app->frameArray[i] = frame; - app->times[i] = furi_get_tick() - app->current_time[i]; - app->time = app->times[i]; - app->current_time[i] = furi_get_tick(); - new = false; - break; - } - } - - if(new && (num_of_devices < 100)) { - app->frameArray[num_of_devices] = frame; - app->times[num_of_devices] = 0; - app->sniffer_index_aux = num_of_devices; - app->times[num_of_devices] = 0; - app->time = 0; - app->current_time[num_of_devices] = furi_get_tick(); - num_of_devices++; - - furi_string_reset(text_label); - furi_string_cat_printf( - text_label, "0x%lx", app->frameArray[app->sniffer_index_aux].canId); - - submenu_add_item( - app->submenu, - furi_string_get_cstr(text_label), - app->sniffer_index_aux, - sniffing_callback, - app); - } - - if(app->log_file_ready && (app->save_logs == SaveAll)) { - app->can_frame = frame; - write_data_on_file(app->can_frame, app->log_file, app->time); - } - } - - if(!condition) { - if(frame.canId == app->frameArray[app->sniffer_index].canId) { - app->frameArray[app->sniffer_index] = frame; - app->times[app->sniffer_index] = - furi_get_tick() - app->current_time[app->sniffer_index]; - app->current_time[app->sniffer_index] = furi_get_tick(); - draw_box_text(app); - - if(app->log_file_ready && (app->save_logs == OnlyAddress)) { - write_data_on_file( - app->frameArray[app->sniffer_index], - app->log_file, - app->times[app->sniffer_index]); - } - } - } - - app->num_of_devices = num_of_devices; + if (app->log_file_ready && (app->save_logs == SaveAll)) { + app->can_frame = frame; + write_data_on_file(app->can_frame, app->log_file, app->time); } - } + } + + if (!condition) { + if (frame.canId == app->frameArray[app->sniffer_index].canId) { + app->frameArray[app->sniffer_index] = frame; + app->times[app->sniffer_index] = + furi_get_tick() - app->current_time[app->sniffer_index]; + app->current_time[app->sniffer_index] = furi_get_tick(); + draw_box_text(app); + + if (app->log_file_ready && (app->save_logs == OnlyAddress)) { + write_data_on_file(app->frameArray[app->sniffer_index], + app->log_file, app->times[app->sniffer_index]); + } + } + } - if((app->save_logs == SaveAll) && (app->log_file_ready)) { - close_file_on_data_log(app); - app->log_file_ready = false; + app->num_of_devices = num_of_devices; } + } - furi_string_free(text_label); - furi_hal_gpio_remove_int_callback(&gpio_swclk); - free_mcp2515(mcp_can); - return 0; + if ((app->save_logs == SaveAll) && (app->log_file_ready)) { + close_file_on_data_log(app); + app->log_file_ready = false; + } + + furi_string_free(text_label); + furi_hal_gpio_remove_int_callback(&gpio_swclk); + free_mcp2515(mcp_can); + return 0; } -// ------------------------------------------------------ SNIFFING MENU SCENE --------------------------- +// ------------------------------------------------------ SNIFFING MENU SCENE +// --------------------------- void app_scene_sniffing_on_enter(void* context) { - App* app = context; - - if(condition) { - app->thread = furi_thread_alloc_ex("SniffingWork", 1024, worker_sniffing, app); - furi_thread_start(app->thread); - submenu_reset(app->submenu); - submenu_set_header(app->submenu, "CANBUS ADDRESS"); - } - - condition = true; - - if(scene_manager_get_scene_state(app->scene_manager, app_scene_sniffing_option) == 1) { - scene_manager_previous_scene(app->scene_manager); - scene_manager_set_scene_state(app->scene_manager, app_scene_sniffing_option, 0); - } - - view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); + App* app = context; + + if (condition) { + app->thread = + furi_thread_alloc_ex("SniffingWork", 1024, worker_sniffing, app); + furi_thread_start(app->thread); + submenu_reset(app->submenu); + submenu_set_header(app->submenu, "CANBUS ADDRESS"); + } + + condition = true; + + if (scene_manager_get_scene_state(app->scene_manager, + app_scene_sniffing_option) == 1) { + scene_manager_previous_scene(app->scene_manager); + scene_manager_set_scene_state(app->scene_manager, app_scene_sniffing_option, + 0); + } + + view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); } bool app_scene_sniffing_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - switch(event.type) { + App* app = context; + bool consumed = false; + switch (event.type) { case SceneManagerEventTypeCustom: - switch(event.event) { + switch (event.event) { case EntryEvent: - condition = false; - scene_manager_next_scene(app->scene_manager, app_scene_box_sniffing); - consumed = true; - break; + condition = false; + scene_manager_next_scene(app->scene_manager, app_scene_box_sniffing); + consumed = true; + break; case DEVICE_NO_CONNECTED: - scene_manager_next_scene(app->scene_manager, app_scene_device_no_connected); - consumed = true; - break; + scene_manager_next_scene(app->scene_manager, + app_scene_device_no_connected); + consumed = true; + break; default: - break; - } - break; + break; + } + break; default: - break; - } - return consumed; + break; + } + return consumed; } void app_scene_sniffing_on_exit(void* context) { - App* app = context; - if(condition) { - furi_thread_flags_set(furi_thread_get_id(app->thread), WorkerflagStop); - furi_thread_join(app->thread); - furi_thread_free(app->thread); - submenu_reset(app->submenu); - } + App* app = context; + if (condition) { + furi_thread_flags_set(furi_thread_get_id(app->thread), WorkerflagStop); + furi_thread_join(app->thread); + furi_thread_free(app->thread); + submenu_reset(app->submenu); + } } -//-------------------------- FOR THE SNIFFING BOX -------------------------------------------------------- +//-------------------------- FOR THE SNIFFING BOX +//-------------------------------------------------------- void app_scene_box_sniffing_on_enter(void* context) { - App* app = context; + App* app = context; - text_box_set_font(app->textBox, TextBoxFontText); + text_box_set_font(app->textBox, TextBoxFontText); - if(app->save_logs == OnlyAddress) save_address_data_on_log(app); + if (app->save_logs == OnlyAddress) + save_address_data_on_log(app); - text_box_reset(app->textBox); - draw_box_text(app); - view_dispatcher_switch_to_view(app->view_dispatcher, TextBoxView); + text_box_reset(app->textBox); + draw_box_text(app); + view_dispatcher_switch_to_view(app->view_dispatcher, TextBoxView); } bool app_scene_box_sniffing_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - UNUSED(app); - UNUSED(event); - return consumed; + App* app = context; + bool consumed = false; + UNUSED(app); + UNUSED(event); + return consumed; } void app_scene_box_sniffing_on_exit(void* context) { - App* app = context; - close_file_on_data_log(app); - furi_string_reset(app->text); - text_box_reset(app->textBox); + App* app = context; + close_file_on_data_log(app); + furi_string_reset(app->text); + text_box_reset(app->textBox); } -//-------------------------- FOR THE UNPLUG DEVICE -------------------------------------------------------- +//-------------------------- FOR THE UNPLUG DEVICE +//-------------------------------------------------------- void app_scene_device_no_connected_on_enter(void* context) { - App* app = context; - widget_reset(app->widget); + App* app = context; + widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "DEVICE NO"); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignBottom, + FontPrimary, "DEVICE NO"); - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignBottom, FontPrimary, "CONNECTED"); + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignBottom, + FontPrimary, "CONNECTED"); - view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); + view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); } -bool app_scene_device_no_connected_on_event(void* context, SceneManagerEvent event) { - UNUSED(context); - UNUSED(event); - bool consumed = false; +bool app_scene_device_no_connected_on_event(void* context, + SceneManagerEvent event) { + UNUSED(context); + UNUSED(event); + bool consumed = false; - return consumed; + return consumed; } void app_scene_device_no_connected_on_exit(void* context) { - App* app = context; - widget_reset(app->widget); - scene_manager_set_scene_state(app->scene_manager, app_scene_sniffing_option, 1); -} \ No newline at end of file + App* app = context; + widget_reset(app->widget); + scene_manager_set_scene_state(app->scene_manager, app_scene_sniffing_option, + 1); +} diff --git a/Canbus_app/scenes/mainMenu.c b/Canbus_app/scenes/mainMenu.c index 70be157..f212e2d 100644 --- a/Canbus_app/scenes/mainMenu.c +++ b/Canbus_app/scenes/mainMenu.c @@ -1,157 +1,169 @@ #include "../app_user.h" -// This variable works to get the value of the selector and set it when the user enter at the Scene +// This variable works to get the value of the selector and set it when the user +// enter at the Scene static uint8_t menu_selector = 0; // This is to open the files bool OpenLogFile(App* app) { - // browse for files - FuriString* predefined_filepath = furi_string_alloc_set_str(PATHLOGS); - FuriString* selected_filepath = furi_string_alloc(); - DialogsFileBrowserOptions browser_options; - dialog_file_browser_set_basic_options(&browser_options, ".log", NULL); - if(!dialog_file_browser_show( - app->dialogs, selected_filepath, predefined_filepath, &browser_options)) { - return false; - } - if(storage_file_open( - app->log_file, furi_string_get_cstr(selected_filepath), FSAM_READ, FSOM_OPEN_EXISTING)) { - app->size_of_storage = storage_file_size(app->log_file); - } else { - dialog_message_show_storage_error(app->dialogs, "Cannot open File"); - return false; - } - - if(app->size_of_storage > 20000) { - dialog_message_show_storage_error( - app->dialogs, "Cannot open File\nLarge Memory size\nOpen in a computer"); - storage_file_close(app->log_file); - return false; - } - - furi_string_reset(app->text); - char buf[storage_file_size(app->log_file)]; - storage_file_read(app->log_file, buf, sizeof(buf)); - buf[sizeof(buf)] = '\0'; - furi_string_cat_str(app->text, buf); - + // browse for files + FuriString* predefined_filepath = furi_string_alloc_set_str(PATHLOGS); + FuriString* selected_filepath = furi_string_alloc(); + DialogsFileBrowserOptions browser_options; + dialog_file_browser_set_basic_options(&browser_options, ".log", NULL); + if (!dialog_file_browser_show(app->dialogs, selected_filepath, + predefined_filepath, &browser_options)) { + return false; + } + if (storage_file_open(app->log_file, furi_string_get_cstr(selected_filepath), + FSAM_READ, FSOM_OPEN_EXISTING)) { + app->size_of_storage = storage_file_size(app->log_file); + } else { + dialog_message_show_storage_error(app->dialogs, "Cannot open File"); + return false; + } + + if (app->size_of_storage > 20000) { + dialog_message_show_storage_error( + app->dialogs, + "Cannot open File\nLarge Memory size\nOpen in a computer"); storage_file_close(app->log_file); - furi_string_free(selected_filepath); - furi_string_free(predefined_filepath); - return true; + return false; + } + + furi_string_reset(app->text); + char buf[storage_file_size(app->log_file)]; + storage_file_read(app->log_file, buf, sizeof(buf)); + buf[sizeof(buf)] = '\0'; + furi_string_cat_str(app->text, buf); + + storage_file_close(app->log_file); + furi_string_free(selected_filepath); + furi_string_free(predefined_filepath); + return true; } // This function works to reset the values in the sender Option void reset_sender_values(void* context) { - App* app = context; - app->sender_selected_item = 0; - app->frame_to_send->canId = 0; - app->frame_to_send->data_lenght = 0; - app->frame_to_send->req = 0; - app->frame_to_send->ext = 0; - for(uint8_t i = 0; i < 8; i++) app->frame_to_send->buffer[i] = 0; - - for(uint8_t i = 0; i < 4; i++) app->sender_id_compose[i] = 0; + App* app = context; + app->sender_selected_item = 0; + app->frame_to_send->canId = 0; + app->frame_to_send->data_lenght = 0; + app->frame_to_send->req = 0; + app->frame_to_send->ext = 0; + for (uint8_t i = 0; i < 8; i++) + app->frame_to_send->buffer[i] = 0; + + for (uint8_t i = 0; i < 4; i++) + app->sender_id_compose[i] = 0; } void basic_scenes_menu_callback(void* context, uint32_t index) { - App* app = context; + App* app = context; - menu_selector = index; + menu_selector = index; - switch(index) { + switch (index) { case SniffingTestOption: - scene_manager_handle_custom_event(app->scene_manager, SniffingOptionEvent); - break; + scene_manager_handle_custom_event(app->scene_manager, + SniffingOptionEvent); + break; case SenderOption: - scene_manager_handle_custom_event(app->scene_manager, SenderOptionEvent); - break; + scene_manager_handle_custom_event(app->scene_manager, SenderOptionEvent); + break; case SettingsOption: - scene_manager_handle_custom_event(app->scene_manager, SettingsOptionEvent); - break; + scene_manager_handle_custom_event(app->scene_manager, + SettingsOptionEvent); + break; case ReadLOGOption: - if(OpenLogFile(app)) { - scene_manager_next_scene(app->scene_manager, app_scene_read_logs); - } - break; + if (OpenLogFile(app)) { + scene_manager_next_scene(app->scene_manager, app_scene_read_logs); + } + break; case AboutUsOption: - scene_manager_handle_custom_event(app->scene_manager, AboutUsEvent); - break; + scene_manager_handle_custom_event(app->scene_manager, AboutUsEvent); + break; default: - break; - } + break; + } } void app_scene_menu_on_enter(void* context) { - App* app = context; - submenu_reset(app->submenu); + App* app = context; + submenu_reset(app->submenu); - submenu_set_header(app->submenu, "MENU CANBUS"); + submenu_set_header(app->submenu, "MENU CANBUS"); - submenu_add_item( - app->submenu, "Sniffing", SniffingTestOption, basic_scenes_menu_callback, app); + submenu_add_item(app->submenu, "Sniffing", SniffingTestOption, + basic_scenes_menu_callback, app); - submenu_add_item(app->submenu, "Sender", SenderOption, basic_scenes_menu_callback, app); + submenu_add_item(app->submenu, "Sender", SenderOption, + basic_scenes_menu_callback, app); - submenu_add_item(app->submenu, "Read LOG", ReadLOGOption, basic_scenes_menu_callback, app); + submenu_add_item(app->submenu, "Read LOG", ReadLOGOption, + basic_scenes_menu_callback, app); - submenu_add_item(app->submenu, "Settings", SettingsOption, basic_scenes_menu_callback, app); + submenu_add_item(app->submenu, "Settings", SettingsOption, + basic_scenes_menu_callback, app); - submenu_add_item(app->submenu, "About Us", AboutUsOption, basic_scenes_menu_callback, app); + submenu_add_item(app->submenu, "About Us", AboutUsOption, + basic_scenes_menu_callback, app); - submenu_set_selected_item(app->submenu, menu_selector); + submenu_set_selected_item(app->submenu, menu_selector); - view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); + view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); - reset_sender_values(app); + reset_sender_values(app); } bool app_scene_menu_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; + App* app = context; + bool consumed = false; - switch(event.type) { + switch (event.type) { case SceneManagerEventTypeCustom: - switch(event.event) { + switch (event.event) { case SniffingOptionEvent: - scene_manager_next_scene(app->scene_manager, app_scene_sniffing_option); - consumed = true; - break; + scene_manager_next_scene(app->scene_manager, + app_scene_sniffing_option); + consumed = true; + break; case SenderOptionEvent: - scene_manager_next_scene(app->scene_manager, app_scene_sender_option); - break; + scene_manager_next_scene(app->scene_manager, app_scene_sender_option); + break; case SettingsOptionEvent: - scene_manager_next_scene(app->scene_manager, app_scene_settings_option); - consumed = true; - break; + scene_manager_next_scene(app->scene_manager, + app_scene_settings_option); + consumed = true; + break; case ReadLOGOptionEvent: - scene_manager_next_scene(app->scene_manager, app_scene_read_logs); - consumed = true; - break; + scene_manager_next_scene(app->scene_manager, app_scene_read_logs); + consumed = true; + break; case AboutUsEvent: - scene_manager_next_scene(app->scene_manager, app_scene_about_us); - consumed = true; - break; + scene_manager_next_scene(app->scene_manager, app_scene_about_us); + consumed = true; + break; default: - break; - } - break; + break; + } + break; default: - break; - } - return consumed; + break; + } + return consumed; } void app_scene_menu_on_exit(void* context) { - App* app = context; - submenu_reset(app->submenu); -} \ No newline at end of file + App* app = context; + submenu_reset(app->submenu); +} diff --git a/Canbus_app/scenes/readLogs.c b/Canbus_app/scenes/readLogs.c index 19e482e..e69b520 100644 --- a/Canbus_app/scenes/readLogs.c +++ b/Canbus_app/scenes/readLogs.c @@ -1,23 +1,23 @@ #include "../app_user.h" void app_scene_read_logs_on_enter(void* context) { - App* app = context; - text_box_set_font(app->textBox, TextBoxFontText); - text_box_reset(app->textBox); - view_dispatcher_switch_to_view(app->view_dispatcher, TextBoxView); - text_box_set_text(app->textBox, furi_string_get_cstr(app->text)); + App* app = context; + text_box_set_font(app->textBox, TextBoxFontText); + text_box_reset(app->textBox); + view_dispatcher_switch_to_view(app->view_dispatcher, TextBoxView); + text_box_set_text(app->textBox, furi_string_get_cstr(app->text)); } bool app_scene_read_logs_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - UNUSED(app); - UNUSED(event); - return consumed; + App* app = context; + bool consumed = false; + UNUSED(app); + UNUSED(event); + return consumed; } void app_scene_read_logs_on_exit(void* context) { - App* app = context; - furi_string_reset(app->text); - text_box_reset(app->textBox); -} \ No newline at end of file + App* app = context; + furi_string_reset(app->text); + text_box_reset(app->textBox); +} diff --git a/Canbus_app/scenes/senderOption.c b/Canbus_app/scenes/senderOption.c index 6ddb40a..bf0bc5e 100644 --- a/Canbus_app/scenes/senderOption.c +++ b/Canbus_app/scenes/senderOption.c @@ -2,519 +2,529 @@ // LIST typedef enum { - SEND_MESSAGE, - CHOOSE_ID, - SET_ID, - SET_DATA_LENGHT, - SET_REQ, - SET_DATA, - SET_FIRST_DATA, - SET_SECOND_DATA, - SET_THIRD_DATA, - SET_FOURTH_DATA, - SET_FIFTH_DATA, - SET_SIXTH_DATA, - SET_SEVENTH_DATA, - SET_EIGHTH_DATA + SEND_MESSAGE, + CHOOSE_ID, + SET_ID, + SET_DATA_LENGHT, + SET_REQ, + SET_DATA, + SET_FIRST_DATA, + SET_SECOND_DATA, + SET_THIRD_DATA, + SET_FOURTH_DATA, + SET_FIFTH_DATA, + SET_SIXTH_DATA, + SET_SEVENTH_DATA, + SET_EIGHTH_DATA } list_of_items; typedef enum { - SEND_OK, - SEND_ERROR, + SEND_OK, + SEND_ERROR, } sender_status; -// Threads To work ------------------------------------------------------------------------------------ +// Threads To work +// ------------------------------------------------------------------------------------ static int32_t sender_on_work(void* context) { - App* app = context; - app->mcp_can->mode = MCP_NORMAL; - ERROR_CAN debug = ERROR_OK; - ERROR_CAN error = ERROR_OK; - debug = mcp2515_init(app->mcp_can); - - furi_delay_ms(10); - - if(debug == ERROR_OK) { - error = send_can_frame(app->mcp_can, app->frame_to_send); - furi_delay_ms(500); - - if(error != ERROR_OK) - scene_manager_handle_custom_event(app->scene_manager, SEND_ERROR); - else - scene_manager_handle_custom_event(app->scene_manager, SEND_OK); - } else { - scene_manager_handle_custom_event(app->scene_manager, DEVICE_NO_CONNECTED); - } - - free_mcp2515(app->mcp_can); - return 0; + App* app = context; + app->mcp_can->mode = MCP_NORMAL; + ERROR_CAN debug = ERROR_OK; + ERROR_CAN error = ERROR_OK; + debug = mcp2515_init(app->mcp_can); + + furi_delay_ms(10); + + if (debug == ERROR_OK) { + error = send_can_frame(app->mcp_can, app->frame_to_send); + furi_delay_ms(500); + + if (error != ERROR_OK) + scene_manager_handle_custom_event(app->scene_manager, SEND_ERROR); + else + scene_manager_handle_custom_event(app->scene_manager, SEND_OK); + } else { + scene_manager_handle_custom_event(app->scene_manager, DEVICE_NO_CONNECTED); + } + + free_mcp2515(app->mcp_can); + return 0; } -// Scenes --------------------------------------------------------------------------------------------- +// Scenes +// --------------------------------------------------------------------------------------------- // Option callback using button OK void callback_input_sender_options(void* context, uint32_t index) { - App* app = context; - app->sender_selected_item = index; - switch(index) { + App* app = context; + app->sender_selected_item = index; + switch (index) { case SEND_MESSAGE: - scene_manager_next_scene(app->scene_manager, app_scene_send_message); - break; + scene_manager_next_scene(app->scene_manager, app_scene_send_message); + break; case CHOOSE_ID: - if(app->num_of_devices > 0) { - scene_manager_next_scene(app->scene_manager, app_scene_id_list_option); - } else { - scene_manager_next_scene(app->scene_manager, app_scene_warning_log_sender); - } - break; + if (app->num_of_devices > 0) { + scene_manager_next_scene(app->scene_manager, app_scene_id_list_option); + } else { + scene_manager_next_scene(app->scene_manager, + app_scene_warning_log_sender); + } + break; case SET_ID: - scene_manager_set_scene_state(app->scene_manager, app_scene_input_text_option, SET_ID); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state(app->scene_manager, + app_scene_input_text_option, SET_ID); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_DATA: - scene_manager_set_scene_state(app->scene_manager, app_scene_input_text_option, SET_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state(app->scene_manager, + app_scene_input_text_option, SET_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_FIRST_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_FIRST_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_FIRST_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_SECOND_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_SECOND_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_SECOND_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_THIRD_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_THIRD_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_THIRD_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_FOURTH_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_FOURTH_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_FOURTH_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_FIFTH_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_FIFTH_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_FIFTH_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_SIXTH_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_SIXTH_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_SIXTH_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_SEVENTH_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_SEVENTH_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_SEVENTH_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; case SET_EIGHTH_DATA: - scene_manager_set_scene_state( - app->scene_manager, app_scene_input_text_option, SET_EIGHTH_DATA); - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - break; + scene_manager_set_scene_state( + app->scene_manager, app_scene_input_text_option, SET_EIGHTH_DATA); + scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); + break; default: - break; - } + break; + } } // Options Callback void callback_sender_options(VariableItem* item) { - App* app = variable_item_get_context(item); - uint8_t selected_index = variable_item_list_get_selected_item_index(app->varList); - uint8_t index_item = variable_item_get_current_value_index(item); + App* app = variable_item_get_context(item); + uint8_t selected_index = + variable_item_list_get_selected_item_index(app->varList); + uint8_t index_item = variable_item_get_current_value_index(item); - switch(selected_index) { + switch (selected_index) { case SET_DATA_LENGHT: - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "%u", index_item); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - app->frame_to_send->data_lenght = index_item; - break; + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "%u", index_item); + variable_item_set_current_value_text(item, + furi_string_get_cstr(app->text)); + app->frame_to_send->data_lenght = index_item; + break; case SET_REQ: - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "%u", index_item); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - app->frame_to_send->req = index_item; - break; + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "%u", index_item); + variable_item_set_current_value_text(item, + furi_string_get_cstr(app->text)); + app->frame_to_send->req = index_item; + break; default: - break; - } + break; + } } // Sender Scene On enter void app_scene_sender_on_enter(void* context) { - App* app = context; - VariableItem* item; - - uint8_t data_lenght = app->frame_to_send->data_lenght; - uint32_t can_id = app->frame_to_send->canId; - uint8_t request = app->frame_to_send->req; - - app->sender_id_compose[3] = can_id; - app->sender_id_compose[2] = can_id >> 8; - app->sender_id_compose[1] = can_id >> 16; - app->sender_id_compose[0] = can_id >> 24; - - variable_item_list_reset(app->varList); - - // 0 - item = variable_item_list_add(app->varList, "SEND MESSAGE", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - // 1 - item = variable_item_list_add(app->varList, "Choose Id", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - variable_item_set_current_value_text(item, "SEARCH"); - - // 2 - item = variable_item_list_add(app->varList, "Id", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%lx", can_id); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 3 - item = variable_item_list_add(app->varList, "DLC", 9, callback_sender_options, app); - variable_item_set_current_value_index(item, data_lenght); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "%u", data_lenght); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 4 - item = variable_item_list_add(app->varList, "Request", 2, callback_sender_options, app); - variable_item_set_current_value_index(item, request); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "%u", request); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 5 - item = - variable_item_list_add(app->varList, "CLIC TO SET DATA", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - // 6 - item = variable_item_list_add(app->varList, "BYTE [0]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[0]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 7 - item = variable_item_list_add(app->varList, "BYTE [1]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[1]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 8 - item = variable_item_list_add(app->varList, "BYTE [2]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[2]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 9 - item = variable_item_list_add(app->varList, "BYTE [3]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[3]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 10 - item = variable_item_list_add(app->varList, "BYTE [4]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[4]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 11 - item = variable_item_list_add(app->varList, "BYTE [5]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[5]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 12 - item = variable_item_list_add(app->varList, "BYTE [6]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[6]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - // 13 - item = variable_item_list_add(app->varList, "BYTE [7]", 0, callback_sender_options, app); - variable_item_set_current_value_index(item, 0); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[7]); - variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); - - variable_item_list_set_enter_callback(app->varList, callback_input_sender_options, app); - variable_item_list_set_selected_item(app->varList, app->sender_selected_item); - view_dispatcher_switch_to_view(app->view_dispatcher, VarListView); + App* app = context; + VariableItem* item; + + uint8_t data_lenght = app->frame_to_send->data_lenght; + uint32_t can_id = app->frame_to_send->canId; + uint8_t request = app->frame_to_send->req; + + app->sender_id_compose[3] = can_id; + app->sender_id_compose[2] = can_id >> 8; + app->sender_id_compose[1] = can_id >> 16; + app->sender_id_compose[0] = can_id >> 24; + + variable_item_list_reset(app->varList); + + // 0 + item = variable_item_list_add(app->varList, "SEND MESSAGE", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + // 1 + item = variable_item_list_add(app->varList, "Choose Id", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + variable_item_set_current_value_text(item, "SEARCH"); + + // 2 + item = variable_item_list_add(app->varList, "Id", 0, callback_sender_options, + app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%lx", can_id); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 3 + item = variable_item_list_add(app->varList, "DLC", 9, callback_sender_options, + app); + variable_item_set_current_value_index(item, data_lenght); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "%u", data_lenght); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 4 + item = variable_item_list_add(app->varList, "Request", 2, + callback_sender_options, app); + variable_item_set_current_value_index(item, request); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "%u", request); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 5 + item = variable_item_list_add(app->varList, "CLIC TO SET DATA", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + // 6 + item = variable_item_list_add(app->varList, "BYTE [0]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[0]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 7 + item = variable_item_list_add(app->varList, "BYTE [1]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[1]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 8 + item = variable_item_list_add(app->varList, "BYTE [2]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[2]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 9 + item = variable_item_list_add(app->varList, "BYTE [3]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[3]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 10 + item = variable_item_list_add(app->varList, "BYTE [4]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[4]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 11 + item = variable_item_list_add(app->varList, "BYTE [5]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[5]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 12 + item = variable_item_list_add(app->varList, "BYTE [6]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[6]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + // 13 + item = variable_item_list_add(app->varList, "BYTE [7]", 0, + callback_sender_options, app); + variable_item_set_current_value_index(item, 0); + + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%x", app->frame_to_send->buffer[7]); + variable_item_set_current_value_text(item, furi_string_get_cstr(app->text)); + + variable_item_list_set_enter_callback(app->varList, + callback_input_sender_options, app); + variable_item_list_set_selected_item(app->varList, app->sender_selected_item); + view_dispatcher_switch_to_view(app->view_dispatcher, VarListView); } bool app_scene_sender_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - if(event.type == SceneManagerEventTypeCustom) { - switch(event.event) { - case SET_ID: - scene_manager_next_scene(app->scene_manager, app_scene_input_text_option); - consumed = true; - break; - - default: - break; - } + App* app = context; + bool consumed = false; + if (event.type == SceneManagerEventTypeCustom) { + switch (event.event) { + case SET_ID: + scene_manager_next_scene(app->scene_manager, + app_scene_input_text_option); + consumed = true; + break; + + default: + break; } - return consumed; + } + return consumed; } void app_scene_sender_on_exit(void* context) { - App* app = context; - variable_item_list_reset(app->varList); + App* app = context; + variable_item_list_reset(app->varList); } // ----------------------------- SCENE TO SEND THE FRAME --------------------- void app_scene_send_message_on_enter(void* context) { - App* app = context; + App* app = context; - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "Wait to send it Message..."); + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "Wait to send it Message..."); - view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); + view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); - app->thread = furi_thread_alloc_ex("Sender_on_work", 1024, sender_on_work, app); - furi_thread_start(app->thread); + app->thread = + furi_thread_alloc_ex("Sender_on_work", 1024, sender_on_work, app); + furi_thread_start(app->thread); } bool app_scene_send_message_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - - if(event.type == SceneManagerEventTypeCustom) { - switch(event.event) { - case SEND_OK: - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "MESSAGE SEND OK"); - break; - - case SEND_ERROR: - widget_reset(app->widget); - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "MESSAGE SEND ERROR"); - break; - - case DEVICE_NO_CONNECTED: - widget_reset(app->widget); - - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "DEVICE NO"); - - widget_add_string_element( - app->widget, 65, 35, AlignCenter, AlignBottom, FontPrimary, "CONNECTED"); - break; - - default: - break; - } + App* app = context; + bool consumed = false; + + if (event.type == SceneManagerEventTypeCustom) { + switch (event.event) { + case SEND_OK: + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "MESSAGE SEND OK"); + break; + + case SEND_ERROR: + widget_reset(app->widget); + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignCenter, + FontPrimary, "MESSAGE SEND ERROR"); + break; + + case DEVICE_NO_CONNECTED: + widget_reset(app->widget); + + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignBottom, + FontPrimary, "DEVICE NO"); + + widget_add_string_element(app->widget, 65, 35, AlignCenter, AlignBottom, + FontPrimary, "CONNECTED"); + break; + + default: + break; } + } - return consumed; + return consumed; } void app_scene_send_message_on_exit(void* context) { - App* app = context; - furi_thread_join(app->thread); - furi_thread_free(app->thread); + App* app = context; + furi_thread_join(app->thread); + furi_thread_free(app->thread); - widget_reset(app->widget); + widget_reset(app->widget); } // ---------------------------- WARNING TO GET THE ID'S ------------------------ void app_scene_warning_log_on_enter(void* context) { - App* app = context; - widget_reset(app->widget); - - widget_add_string_element( - app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "W A R N I N G"); - - widget_add_string_element( - app->widget, - 65, - 40, - AlignCenter, - AlignBottom, - FontSecondary, - "First go to the Sniffing option"); - widget_add_string_element( - app->widget, - 65, - 50, - AlignCenter, - AlignBottom, - FontSecondary, - "in the menu and get the Id's"); - - view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); + App* app = context; + widget_reset(app->widget); + + widget_add_string_element(app->widget, 65, 20, AlignCenter, AlignBottom, + FontPrimary, "W A R N I N G"); + + widget_add_string_element(app->widget, 65, 40, AlignCenter, AlignBottom, + FontSecondary, "First go to the Sniffing option"); + widget_add_string_element(app->widget, 65, 50, AlignCenter, AlignBottom, + FontSecondary, "in the menu and get the Id's"); + + view_dispatcher_switch_to_view(app->view_dispatcher, ViewWidget); } bool app_scene_warning_log_on_event(void* context, SceneManagerEvent event) { - UNUSED(context); - UNUSED(event); - bool consumed = false; + UNUSED(context); + UNUSED(event); + bool consumed = false; - return consumed; + return consumed; } void app_scene_warning_log_on_exit(void* context) { - App* app = context; - widget_reset(app->widget); + App* app = context; + widget_reset(app->widget); } // ---------------------------- MENU OF THE CAN ID's --------------------------- void menu_ids_callback(void* context, uint32_t index) { - App* app = context; - app->frame_to_send->canId = app->frameArray[index].canId; - app->frame_to_send->data_lenght = app->frameArray[index].data_lenght; - app->frame_to_send->ext = app->frameArray[index].ext; - app->frame_to_send->req = app->frameArray[index].req; - - for(uint8_t i = 0; i < (app->frame_to_send->data_lenght); i++) { - app->frame_to_send->buffer[i] = app->frameArray[index].buffer[i]; - } + App* app = context; + app->frame_to_send->canId = app->frameArray[index].canId; + app->frame_to_send->data_lenght = app->frameArray[index].data_lenght; + app->frame_to_send->ext = app->frameArray[index].ext; + app->frame_to_send->req = app->frameArray[index].req; + + for (uint8_t i = 0; i < (app->frame_to_send->data_lenght); i++) { + app->frame_to_send->buffer[i] = app->frameArray[index].buffer[i]; + } - scene_manager_handle_custom_event(app->scene_manager, ReturnEvent); + scene_manager_handle_custom_event(app->scene_manager, ReturnEvent); } void app_scene_id_list_on_enter(void* context) { - App* app = context; - submenu_reset(app->submenu); - submenu_set_header(app->submenu, "CAN ADDRESS TO SEND"); + App* app = context; + submenu_reset(app->submenu); + submenu_set_header(app->submenu, "CAN ADDRESS TO SEND"); - for(uint8_t i = 0; i < app->num_of_devices; i++) { - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "0x%lx", app->frameArray[i].canId); - submenu_add_item(app->submenu, furi_string_get_cstr(app->text), i, menu_ids_callback, app); - } + for (uint8_t i = 0; i < app->num_of_devices; i++) { + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "0x%lx", app->frameArray[i].canId); + submenu_add_item(app->submenu, furi_string_get_cstr(app->text), i, + menu_ids_callback, app); + } - view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); + view_dispatcher_switch_to_view(app->view_dispatcher, SubmenuView); } bool app_scene_id_list_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - - if(event.type == SceneManagerEventTypeCustom) { - scene_manager_previous_scene(app->scene_manager); - consumed = true; - } - return consumed; + App* app = context; + bool consumed = false; + + if (event.type == SceneManagerEventTypeCustom) { + scene_manager_previous_scene(app->scene_manager); + consumed = true; + } + return consumed; } void app_scene_id_list_on_exit(void* context) { - App* app = context; - submenu_reset(app->submenu); + App* app = context; + submenu_reset(app->submenu); } // ---------------------------- TO SET THE VALUE OF THE FRAME ------------------ void input_byte_sender_callback(void* context) { - App* app = context; - app->frame_to_send->canId = app->sender_id_compose[3] | (app->sender_id_compose[2] << 8) | - (app->sender_id_compose[1] << 16) | - (app->sender_id_compose[0] << 24); - scene_manager_handle_custom_event(app->scene_manager, ReturnEvent); + App* app = context; + app->frame_to_send->canId = + app->sender_id_compose[3] | (app->sender_id_compose[2] << 8) | + (app->sender_id_compose[1] << 16) | (app->sender_id_compose[0] << 24); + scene_manager_handle_custom_event(app->scene_manager, ReturnEvent); } void app_scene_input_text_on_enter(void* context) { - App* app = context; - ByteInput* scene = app->input_byte_value; + App* app = context; + ByteInput* scene = app->input_byte_value; - uint32_t state = - scene_manager_get_scene_state(app->scene_manager, app_scene_input_text_option); + uint32_t state = scene_manager_get_scene_state(app->scene_manager, + app_scene_input_text_option); - app->sender_selected_item = state; + app->sender_selected_item = state; - switch(state) { + switch (state) { case SET_ID: - byte_input_set_result_callback( - scene, input_byte_sender_callback, NULL, app, app->sender_id_compose, 4); - byte_input_set_header_text(app->input_byte_value, "SET ADDRESS"); - break; + byte_input_set_result_callback(scene, input_byte_sender_callback, NULL, + app, app->sender_id_compose, 4); + byte_input_set_header_text(app->input_byte_value, "SET ADDRESS"); + break; case SET_DATA: - byte_input_set_result_callback( - scene, input_byte_sender_callback, NULL, app, app->frame_to_send->buffer, 8); - byte_input_set_header_text(app->input_byte_value, "SET ALL DATA"); - break; + byte_input_set_result_callback(scene, input_byte_sender_callback, NULL, + app, app->frame_to_send->buffer, 8); + byte_input_set_header_text(app->input_byte_value, "SET ALL DATA"); + break; default: - if(state > 5) { - byte_input_set_result_callback( - scene, - input_byte_sender_callback, - NULL, - app, - &(app->frame_to_send->buffer[state - 6]), - 1); - - furi_string_reset(app->text); - furi_string_cat_printf(app->text, "SET BYTE [%lu]", state - 6); - byte_input_set_header_text(app->input_byte_value, furi_string_get_cstr(app->text)); - } - break; - } + if (state > 5) { + byte_input_set_result_callback( + scene, input_byte_sender_callback, NULL, app, + &(app->frame_to_send->buffer[state - 6]), 1); - view_dispatcher_switch_to_view(app->view_dispatcher, InputByteView); + furi_string_reset(app->text); + furi_string_cat_printf(app->text, "SET BYTE [%lu]", state - 6); + byte_input_set_header_text(app->input_byte_value, + furi_string_get_cstr(app->text)); + } + break; + } + + view_dispatcher_switch_to_view(app->view_dispatcher, InputByteView); } bool app_scene_input_text_on_event(void* context, SceneManagerEvent event) { - App* app = context; - bool consumed = false; - - if(event.type == SceneManagerEventTypeCustom) { - switch(event.event) { - case ReturnEvent: - scene_manager_previous_scene(app->scene_manager); - break; - - default: - break; - } + App* app = context; + bool consumed = false; + + if (event.type == SceneManagerEventTypeCustom) { + switch (event.event) { + case ReturnEvent: + scene_manager_previous_scene(app->scene_manager); + break; + + default: + break; } - return consumed; + } + return consumed; } void app_scene_input_text_on_exit(void* context) { - UNUSED(context); -} \ No newline at end of file + UNUSED(context); +} diff --git a/Canbus_app/scenes/settingsOptionScene.c b/Canbus_app/scenes/settingsOptionScene.c index c1c8524..87cdec7 100644 --- a/Canbus_app/scenes/settingsOptionScene.c +++ b/Canbus_app/scenes/settingsOptionScene.c @@ -3,76 +3,80 @@ static uint8_t currentClock = MCP_16MHZ; static uint8_t currentBitrate = MCP_500KBPS; -static const char* bitratesValues[] = {"125KBPS", "250KBPS", "500KBPS", "1000KBPS"}; +static const char* bitratesValues[] = {"125KBPS", "250KBPS", "500KBPS", + "1000KBPS"}; static const char* clockValues[] = {"8MHz", "16MHz", "20MHz"}; static const char* save_logs[] = {"No Save", "Save All", "Only Address"}; void callback_options(VariableItem* item) { - App* app = variable_item_get_context(item); - uint8_t index = variable_item_get_current_value_index(item); - uint8_t selected_index = variable_item_list_get_selected_item_index(app->varList); + App* app = variable_item_get_context(item); + uint8_t index = variable_item_get_current_value_index(item); + uint8_t selected_index = + variable_item_list_get_selected_item_index(app->varList); - switch(selected_index) { + switch (selected_index) { case BitrateOption: - variable_item_set_current_value_text(item, bitratesValues[index]); - currentBitrate = index; - app->mcp_can->bitRate = index; - break; + variable_item_set_current_value_text(item, bitratesValues[index]); + currentBitrate = index; + app->mcp_can->bitRate = index; + break; case CristyalClkOption: - variable_item_set_current_value_text(item, clockValues[index]); - currentClock = index; - app->mcp_can->clck = index; + variable_item_set_current_value_text(item, clockValues[index]); + currentClock = index; + app->mcp_can->clck = index; - break; + break; case SaveLogsOption: - variable_item_set_current_value_text(item, save_logs[index]); - app->save_logs = index; - break; + variable_item_set_current_value_text(item, save_logs[index]); + app->save_logs = index; + break; default: - break; - } + break; + } } void app_scene_settings_on_enter(void* context) { - App* app = context; - VariableItem* item; - - variable_item_list_reset(app->varList); - - // First Item - item = variable_item_list_add( - app->varList, "Bitrate", COUNT_OF(bitratesValues), callback_options, app); - variable_item_set_current_value_index(item, currentBitrate); - variable_item_set_current_value_text(item, bitratesValues[currentBitrate]); - - // Second Item - item = variable_item_list_add(app->varList, "Clock", 0, callback_options, app); - variable_item_set_current_value_index(item, 0); - variable_item_set_current_value_text(item, clockValues[currentClock]); - - // Third Item - item = variable_item_list_add(app->varList, "Save LOGS?", 3, callback_options, app); - variable_item_set_current_value_index(item, app->save_logs); - variable_item_set_current_value_text(item, save_logs[app->save_logs]); - - variable_item_list_set_selected_item(app->varList, 0); - view_dispatcher_switch_to_view(app->view_dispatcher, VarListView); + App* app = context; + VariableItem* item; + + variable_item_list_reset(app->varList); + + // First Item + item = variable_item_list_add( + app->varList, "Bitrate", COUNT_OF(bitratesValues), callback_options, app); + variable_item_set_current_value_index(item, currentBitrate); + variable_item_set_current_value_text(item, bitratesValues[currentBitrate]); + + // Second Item + item = + variable_item_list_add(app->varList, "Clock", 0, callback_options, app); + variable_item_set_current_value_index(item, 0); + variable_item_set_current_value_text(item, clockValues[currentClock]); + + // Third Item + item = variable_item_list_add(app->varList, "Save LOGS?", 3, callback_options, + app); + variable_item_set_current_value_index(item, app->save_logs); + variable_item_set_current_value_text(item, save_logs[app->save_logs]); + + variable_item_list_set_selected_item(app->varList, 0); + view_dispatcher_switch_to_view(app->view_dispatcher, VarListView); } bool app_scene_settings_on_event(void* context, SceneManagerEvent event) { - App* app = context; - UNUSED(event); - UNUSED(app); - bool consumed = false; - return consumed; + App* app = context; + UNUSED(event); + UNUSED(app); + bool consumed = false; + return consumed; } void app_scene_settings_on_exit(void* context) { - App* app = context; - variable_item_list_reset(app->varList); -} \ No newline at end of file + App* app = context; + variable_item_list_reset(app->varList); +} diff --git a/Canbus_app/scenes_config/app_scene_config.h b/Canbus_app/scenes_config/app_scene_config.h index 4ce2978..23e92a8 100644 --- a/Canbus_app/scenes_config/app_scene_config.h +++ b/Canbus_app/scenes_config/app_scene_config.h @@ -21,4 +21,4 @@ ADD_SCENE(app, id_list, id_list_option) ADD_SCENE(app, input_text, input_text_option) ADD_SCENE(app, read_logs, read_logs) ADD_SCENE(app, settings, settings_option) -ADD_SCENE(app, about_us, about_us) \ No newline at end of file +ADD_SCENE(app, about_us, about_us) diff --git a/Canbus_app/scenes_config/app_scene_functions.c b/Canbus_app/scenes_config/app_scene_functions.c index 89414b8..48f056e 100644 --- a/Canbus_app/scenes_config/app_scene_functions.c +++ b/Canbus_app/scenes_config/app_scene_functions.c @@ -18,7 +18,8 @@ void (*const app_on_enter_handlers[])(void*) = { // Generate scene on_event handlers array #define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event, -bool (*const app_on_event_handlers[])(void* context, SceneManagerEvent event) = { +bool (*const app_on_event_handlers[])(void* context, + SceneManagerEvent event) = { #include "app_scene_config.h" }; #undef ADD_SCENE @@ -45,4 +46,4 @@ const SceneManagerHandlers app_scene_handlers = { .on_event_handlers = app_on_event_handlers, .on_exit_handlers = app_on_exit_handlers, .scene_num = app_scene_enum, -}; \ No newline at end of file +}; diff --git a/Canbus_app/scenes_config/app_scene_functions.h b/Canbus_app/scenes_config/app_scene_functions.h index ee7c1c0..d110dc4 100644 --- a/Canbus_app/scenes_config/app_scene_functions.h +++ b/Canbus_app/scenes_config/app_scene_functions.h @@ -8,7 +8,7 @@ #define ADD_SCENE(prefix, name, id) app_scene_##id, typedef enum { #include "app_scene_config.h" - app_scene_enum, + app_scene_enum, } Appscenes; #undef ADD_SCENE @@ -19,13 +19,15 @@ typedef enum { //--------------------------------------------------------------------- //--------------------------------------------------------------------- -extern const SceneManagerHandlers app_scene_handlers; // We define the scene manager handler +extern const SceneManagerHandlers + app_scene_handlers; // We define the scene manager handler //--------------------------------------------------------------------- // This defines and name of the "on_enter" functions //--------------------------------------------------------------------- -#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*); +#define ADD_SCENE(prefix, name, id) \ + void prefix##_scene_##name##_on_enter(void*); #include "app_scene_config.h" #undef ADD_SCENE @@ -34,7 +36,7 @@ extern const SceneManagerHandlers app_scene_handlers; // We define the scene man //--------------------------------------------------------------------- #define ADD_SCENE(prefix, name, id) \ - bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event); + bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event); #include "app_scene_config.h" #undef ADD_SCENE @@ -42,6 +44,7 @@ extern const SceneManagerHandlers app_scene_handlers; // We define the scene man // This defines and name of the "on_exit" functions //--------------------------------------------------------------------- -#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context); +#define ADD_SCENE(prefix, name, id) \ + void prefix##_scene_##name##_on_exit(void* context); #include "app_scene_config.h" #undef ADD_SCENE