Skip to content

Commit

Permalink
Merge pull request #11 from ElectronicCats/dev
Browse files Browse the repository at this point in the history
Player & OBD2 functionalities added
  • Loading branch information
sabas1080 authored Oct 29, 2024
2 parents cb0cb77 + e5769c6 commit a85ca9d
Show file tree
Hide file tree
Showing 23 changed files with 2,579 additions and 128 deletions.
13 changes: 13 additions & 0 deletions Canbus_app/app_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ static App* app_alloc() {

app->text = furi_string_alloc();
app->data = furi_string_alloc();
app->path = furi_string_alloc();

furi_string_reset(app->data);
furi_string_cat_printf(app->data, "---");

app->file_browser = file_browser_alloc(app->path);
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserView, file_browser_get_view(app->file_browser));

app->mcp_can = mcp_alloc(MCP_NORMAL, MCP_16MHZ, MCP_500KBPS);

Expand All @@ -69,6 +77,8 @@ static App* app_alloc() {

app->frame_to_send = malloc(sizeof(CANFRAME));

app->obdii.bitrate = app->mcp_can->bitRate;

makePaths(app);

return app;
Expand All @@ -82,6 +92,7 @@ static void app_free(App* app) {
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, FileBrowserView);

scene_manager_free(app->scene_manager);
view_dispatcher_free(app->view_dispatcher);
Expand All @@ -90,9 +101,11 @@ static void app_free(App* app) {
submenu_free(app->submenu);
text_box_free(app->textBox);
byte_input_free(app->input_byte_value);
file_browser_free(app->file_browser);

furi_string_free(app->text);
furi_string_free(app->data);
furi_string_free(app->path);

if(app->log_file && storage_file_is_open(app->log_file)) {
storage_file_close(app->log_file);
Expand Down
48 changes: 43 additions & 5 deletions Canbus_app/app_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@
#include <gui/modules/text_input.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
#include <gui/modules/file_browser.h>
#include <gui/modules/file_browser_worker.h>
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>
#include <storage/storage.h>

#include "scenes_config/app_scene_functions.h"

#include "libraries/mcp_can_2515.h"
#include "libraries/pid_library.h"

#include "canbus_app_icons.h"

#define PATHAPP "apps_data/canbus"
#define PATHAPPEXT EXT_PATH(PATHAPP)
#define PATHLOGS PATHAPPEXT "/logs"

#define DEVICE_NO_CONNECTED (0xFF)

#define MESSAGE_ERROR 0xF0

typedef enum {
WorkerflagStop = (1 << 0),
WorkerflagReceived = (1 << 1),
Expand All @@ -36,6 +43,8 @@ typedef struct {
CANFRAME* frameArray;
CANFRAME* frame_to_send;

OBDII obdii;

uint32_t time;
uint32_t times[100];
uint32_t current_time[100];
Expand All @@ -48,9 +57,11 @@ typedef struct {
VariableItemList* varList;
TextBox* textBox;
ByteInput* input_byte_value;
FileBrowser* file_browser;

FuriString* text;
FuriString* data;
FuriString* path;

Storage* storage;
DialogsApp* dialogs;
Expand All @@ -62,27 +73,39 @@ typedef struct {
uint32_t sniffer_index;
uint32_t sniffer_index_aux;

uint8_t config_timing_index;

uint8_t num_of_devices;
uint8_t sender_selected_item;
uint8_t sender_id_compose[4];

uint32_t obdii_aux_index;
uint8_t flags;

uint64_t size_of_storage;

uint8_t request_data;
} App;

// This is for the menu Options
typedef enum {
SniffingTestOption,
SenderOption,
ObdiiOption,
ReadLOGOption,
PlayLOGOption,
SettingsOption,
AboutUsOption,
} MainMenuOptions;

// These are the events on the main menu
typedef enum {
SniffingOptionEvent,
SenderOptionEvent,
SettingsOptionEvent,
ObdiiOptionEvent,
ReadLOGOptionEvent,
PlayLOGOptionEvent,
AboutUsEvent,
} MainMenuEvents;

Expand All @@ -92,16 +115,26 @@ typedef enum {
CristyalClkOption,
SaveLogsOption
} OptionSettings;

// These are the events on the settings menu
typedef enum {
BitrateOptionEvent,
CristyalClkOptionEvent
} SettingsMenuEvent;

// These are the sender events
typedef enum {
ChooseIdEvent,
SetIdEvent,
ReturnEvent
} SenderEvents;

// These are the player events
typedef enum {
ChooseTimingEvent,
ReturnTimingEvent
} PlayerEvents;

// These are the options to save
typedef enum {
NoSave,
Expand Down Expand Up @@ -129,10 +162,15 @@ typedef enum {
TextBoxView,
DialogInfoView,
InputByteView,
FileBrowserView,
} scenesViews;

char* sequential_file_resolve_path(
Storage* storage,
const char* dir,
const char* prefix,
const char* extension);
/**
* These functions works in other scenes and widget
*/

void draw_in_development(App* app);
void draw_device_no_connected(App* app);
void draw_transmition_failure(App* app);
void draw_send_ok(App* app);
void draw_send_wrong(App* app);
3 changes: 2 additions & 1 deletion Canbus_app/application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ App(
requires=["gui"],
stack_size=30 * 1024,
fap_icon="icon.png",
fap_category="Misc",
fap_category="ElectronicCats",
fap_icon_assets="assets",
)
Binary file added Canbus_app/assets/qrcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Canbus_app/dist/canbus_app.fap
Binary file not shown.
Binary file modified Canbus_app/dist/debug/canbus_app_d.elf
Binary file not shown.
48 changes: 48 additions & 0 deletions Canbus_app/draw_functions/drawWidgets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "../app_user.h"

// Draws a developmet
void draw_in_development(App* app) {
widget_reset(app->widget);

widget_add_string_element(
app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "SCENE IN");

widget_add_string_element(
app->widget, 65, 35, AlignCenter, AlignBottom, FontPrimary, "DEVELOPMENT");
}

// Draws device not connected
void draw_device_no_connected(App* app) {
widget_reset(app->widget);

widget_add_string_element(
app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "DEVICE NOT");

widget_add_string_element(
app->widget, 65, 35, AlignCenter, AlignBottom, FontPrimary, "CONNECTED");
}

// draw when a message is not recognized
void draw_transmition_failure(App* app) {
widget_reset(app->widget);

widget_add_string_element(
app->widget, 65, 20, AlignCenter, AlignBottom, FontPrimary, "TRANSMITION");

widget_add_string_element(
app->widget, 65, 35, AlignCenter, AlignBottom, FontPrimary, "FAILURE");
}

// draw when a message is send OK
void draw_send_wrong(App* app) {
widget_reset(app->widget);
widget_add_string_element(
app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "MESSAGE SEND ERROR");
}

// draw when a message is send ok
void draw_send_ok(App* app) {
widget_reset(app->widget);
widget_add_string_element(
app->widget, 65, 20, AlignCenter, AlignCenter, FontPrimary, "MESSAGE SEND OK");
}
44 changes: 17 additions & 27 deletions Canbus_app/libraries/mcp_can_2515.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ void read_Id(FuriHalSpiBusHandle* spi, uint8_t addr, uint32_t* id, uint8_t* ext)
// get actual mode of the MCP2515
uint8_t get_mode(FuriHalSpiBusHandle* spi) {
uint8_t data = 0;
read_register(spi, MCP_CANSTAT, &data);

uint8_t instruction[] = {INSTRUCTION_READ, MCP_CANSTAT};
furi_hal_spi_acquire(spi);
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
furi_hal_spi_bus_rx(spi, &data, 1, TIMEOUT_SPI);

furi_hal_spi_release(spi);

return data & CANSTAT_OPM;
}

Expand Down Expand Up @@ -128,6 +135,8 @@ bool set_new_mode(MCP2515* mcp_can, MCP_MODE new_mode) {
read_status &= CANSTAT_OPM;
if(read_status == MODE_CONFIG) ret = true;

furi_delay_us(1);

} while((ret != true) && ((furi_get_tick() - time_out) < 50));

time_out = furi_get_tick();
Expand All @@ -138,6 +147,9 @@ bool set_new_mode(MCP2515* mcp_can, MCP_MODE new_mode) {

read_status &= CANSTAT_OPM;
if(read_status == new_mode) return true;

furi_delay_us(1);

} while((furi_get_tick() - time_out) < 50);

return false;
Expand All @@ -148,38 +160,27 @@ bool set_config_mode(MCP2515* mcp_can) {
bool ret = true;
ret = set_new_mode(mcp_can, MODE_CONFIG);

if(ret) mcp_can->mode = MODE_CONFIG;

return ret;
}

// To set Normal Mode
bool set_normal_mode(MCP2515* mcp_can) {
bool ret = true;
ret = set_new_mode(mcp_can, MCP_NORMAL);

if(ret) mcp_can->mode = MCP_NORMAL;

return ret;
}

// To set ListenOnly Mode
bool set_listen_only_mode(MCP2515* mcp_can) {
bool ret = true;
ret = set_new_mode(mcp_can, MCP_LISTENONLY);

if(ret) mcp_can->mode = MCP_LISTENONLY;

return ret;
}

// To set Sleep Mode
bool set_sleep_mode(MCP2515* mcp_can) {
bool ret = true;
ret = set_new_mode(mcp_can, MCP_SLEEP);

if(ret) mcp_can->mode = MCP_SLEEP;

return ret;
}

Expand Down Expand Up @@ -322,10 +323,7 @@ void write_mf(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t ext, uint32_t i
furi_hal_spi_acquire(spi);
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);

for(uint8_t i = 0; i < 4; i++) {
furi_hal_spi_bus_tx(spi, &bufData[i], 1, TIMEOUT_SPI);
}

furi_hal_spi_bus_tx(spi, bufData, 4, TIMEOUT_SPI);
furi_hal_spi_release(spi);
}

Expand All @@ -335,8 +333,6 @@ void init_mask(MCP2515* mcp_can, uint8_t num_mask, uint32_t mask) {

uint8_t ext = 0;

MCP_MODE last_mode = mcp_can->mode;

set_config_mode(mcp_can);

if(num_mask > 1) return;
Expand All @@ -350,8 +346,8 @@ void init_mask(MCP2515* mcp_can, uint8_t num_mask, uint32_t mask) {
if(num_mask == 1) {
write_mf(spi, MCP_RXM1SIDH, ext, mask);
}
mcp_can->mode = last_mode;
set_new_mode(mcp_can, last_mode);

set_new_mode(mcp_can, mcp_can->mode);
}

// To set a Filter
Expand All @@ -360,8 +356,6 @@ void init_filter(MCP2515* mcp_can, uint8_t num_filter, uint32_t filter) {

uint8_t ext = 0;

MCP_MODE last_mode = mcp_can->mode;

set_config_mode(mcp_can);

if(num_filter > 6) return;
Expand Down Expand Up @@ -396,8 +390,7 @@ void init_filter(MCP2515* mcp_can, uint8_t num_filter, uint32_t filter) {
break;
}

mcp_can->mode = last_mode;
set_new_mode(mcp_can, last_mode);
set_new_mode(mcp_can, mcp_can->mode);
}

// This function works to know if there is any message waiting
Expand Down Expand Up @@ -635,7 +628,6 @@ ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame, uint8_t tx
read_register(spi, free_buffer - 1, &is_send_it);
if(is_send_it == 0) res = ERROR_OK;

furi_delay_us(1);
} while((res != ERROR_OK) && ((furi_get_tick() - time_waiting) < 1));

if(is_send_it) return res;
Expand All @@ -652,8 +644,6 @@ ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) {
if(free_buffer == 0xFF) return ERROR_ALLTXBUSY;

return send_can_message(spi, frame, free_buffer);

return ERROR_OK;
}

// This function works to alloc the struct
Expand Down
Loading

0 comments on commit a85ca9d

Please sign in to comment.