Skip to content

Commit

Permalink
refactor: delete unnecesary code
Browse files Browse the repository at this point in the history
  • Loading branch information
AdonaiDiazEsparza committed Oct 22, 2024
1 parent 0299b27 commit 858933a
Showing 1 changed file with 2 additions and 250 deletions.
252 changes: 2 additions & 250 deletions Canbus_app/scenes/playLogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ char* custom_strtok_r(char* str, const char* delim, char** saveptr) {
return start;
}

void path_file_name(const char* path, FuriString* file_name);
void path_file_name(const char* path, FuriString* file_name) {
uint8_t last_pos = 0;
for(uint8_t i = 0; path[i] != '\0'; i++) {
Expand All @@ -82,254 +81,10 @@ void path_file_name(const char* path, FuriString* file_name) {
}
}

void play_data_frames(App* app, UniqueId* unique_ids, int unique_id_count);
// For a time
void play_data_frames_bk(void* context, int frame_interval);

// Function to play data frames
void play_data_frames(App* app, UniqueId* unique_ids, int unique_id_count) {
app->mcp_can->mode = MCP_NORMAL;
ERROR_CAN debug = mcp2515_init(app->mcp_can);

if(debug != ERROR_OK) {
scene_manager_handle_custom_event(app->scene_manager, DEVICE_NO_CONNECTED);
return;
}

char buffer[256];
size_t buffer_index = 0;
char c;
uint32_t previous_timing = 0;

while(storage_file_read(app->log_file, &c, 1) == 1) {
if(c == '\n' || buffer_index >= sizeof(buffer) - 1) {
buffer[buffer_index] = '\0';

CANFRAME frame_to_send = {0};
char* saveptr;
char* endptr;
char* token;
int time_to_next_frame = 0;
double timestamp;

// Parse timestamp
token = custom_strtok_r(buffer, "() ", &saveptr);
if(!token) continue;
timestamp = strtod(token, &endptr);
uint32_t current_timing = (uint32_t)((timestamp - previous_timing) * 1000);
previous_timing = timestamp;

// Parse CAN ID
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) continue;
frame_to_send.canId = (uint32_t)strtoul(token, NULL, 16);

// Check if this ID is enabled
bool id_enabled = false;
for(int i = 0; i < unique_id_count; i++) {
if(unique_ids[i].id == frame_to_send.canId && unique_ids[i].enabled) {
id_enabled = true;
break;
}
}
if(!id_enabled) continue;

// Parse data length
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) continue;
frame_to_send.data_lenght = (uint8_t)atoi(token);

// Parse data
for(int i = 0; i < frame_to_send.data_lenght && i < MAX_LEN; i++) {
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) break;
frame_to_send.buffer[i] = (uint8_t)strtoul(token, NULL, 16);
}

// Parse custom timing if available
token = custom_strtok_r(NULL, ",", &saveptr);
if(token) {
time_to_next_frame = atoi(token);
}

ERROR_CAN error = send_can_frame(app->mcp_can, &frame_to_send);

switch(app->config_timing_index) {
case TIMING_TIMESTAMP:
furi_delay_ms(current_timing);
break;
case TIMING_CUSTOM:
furi_delay_ms(time_to_next_frame);
break;
case TIMING_DEFAULT:
furi_delay_ms(500);
break;
}

if(error != ERROR_OK) {
scene_manager_handle_custom_event(app->scene_manager, PLAY_ERROR);
} else {
scene_manager_handle_custom_event(app->scene_manager, PLAY_OK);
}

buffer_index = 0;
} else {
buffer[buffer_index++] = c;
}
}

storage_file_seek(app->log_file, 0, true);
}

/*
void play_data_frames_bk(void* context, int frame_interval) {
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);
if(storage_file_open(
app->log_file, furi_string_get_cstr(app->data), FSAM_READ, FSOM_OPEN_EXISTING)) {
char buffer[256];
size_t buffer_index = 0;
size_t bytes_read;
char c;
uint32_t previous_timing = 0;
unsigned int unique_ids[MAX_UNIQUE_IDS] = {0};
int unique_id_count = 0;
// Primera pasada: recolectar IDs únicos
while((bytes_read = storage_file_read(app->log_file, &c, 1)) > 0) {
if(c == '\n' || buffer_index >= 256 - 1) {
buffer[buffer_index] = '\0';
char* saveptr;
char* token;
// Pass timestamp
token = custom_strtok_r(buffer, "() ", &saveptr);
if(!token) continue;
// Get ID
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) continue;
uint32_t can_id = hex_to_int(token);
// Add ID to unique ID list if not present
bool id_exists = false;
for(int i = 0; i < unique_id_count; i++) {
if(unique_ids[i] == can_id) {
id_exists = true;
break;
}
}
if(!id_exists && unique_id_count < MAX_UNIQUE_IDS) {
unique_ids[unique_id_count++] = can_id;
}
buffer_index = 0;
} else {
buffer[buffer_index++] = c;
}
}
// Print unique IDs
FURI_LOG_I(TAG, "IDs únicos encontrados:");
for(int i = 0; i < unique_id_count; i++) {
FURI_LOG_I(TAG, "0x%X", unique_ids[i]);
}
FURI_LOG_I(TAG, "Total de IDs únicos: %d", unique_id_count);
// Restart file
storage_file_seek(app->log_file, 0, true);
buffer_index = 0;
while((bytes_read = storage_file_read(app->log_file, &c, 1)) >
0) { // && model->flag_signal) {
if(c == '\n' || buffer_index >= 256 - 1) {
buffer[buffer_index] = '\0';
FURI_LOG_E(TAG, "%s\n", buffer);
buffer[sizeof(buffer) - 1] = '\0'; // Ensure the string is null-terminated
CANFRAME frame_to_send = {0}; // Initialize all fields to 0
char* saveptr;
char* endptr;
char* token;
int time_to_next_frame = 0;
double timestamp;
// Timestamp
token = custom_strtok_r(buffer, "() ", &saveptr);
if(!token) return;
timestamp = strtod(token, &endptr);
uint32_t current_timing = (uint32_t)((timestamp - previous_timing) * 1000);
previous_timing = timestamp;
// CAN bus ID
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) return;
frame_to_send.canId = hex_to_int(token);
// Data length
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) return;
frame_to_send.data_lenght = (uint8_t)atoi(token);
// Fill the data buffer
for(int i = 0; i < frame_to_send.data_lenght && i < MAX_LEN; i++) {
token = custom_strtok_r(NULL, " ", &saveptr);
if(!token) break;
frame_to_send.buffer[i] = (uint8_t)hex_to_int(token);
}
token = custom_strtok_r(NULL, ",", &saveptr);
if(token) {
time_to_next_frame = atoi(token);
}
if(debug == ERROR_OK) {
error = send_can_frame(app->mcp_can, app->frame_to_send);
// TODO: choose TIMING
switch(frame_interval) {
case TIMING_TIMESTAMP:
furi_delay_ms((uint32_t)(current_timing * 1000));
break;
case TIMING_CUSTOM:
furi_delay_ms(time_to_next_frame);
break;
case TIMING_DEFAULT:
furi_delay_ms(500);
break;
}
if(error != ERROR_OK)
scene_manager_handle_custom_event(app->scene_manager, PLAY_ERROR);
else
scene_manager_handle_custom_event(app->scene_manager, PLAY_OK);
} else {
scene_manager_handle_custom_event(app->scene_manager, DEVICE_NO_CONNECTED);
}
buffer_index = 0;
} else {
buffer[buffer_index++] = c;
}
}
} else {
}
storage_file_close(app->log_file);
}
*/

// Play frames
void play_data_frames_bk(void* context, int frame_interval) {
App* app = context;

Expand Down Expand Up @@ -464,9 +219,6 @@ void play_data_frames_bk(void* context, int frame_interval) {

// TODO: choose TIMING
switch(frame_interval) {
case TIMING_TIMESTAMP:
furi_delay_ms((uint32_t)(current_timing));
break;
case TIMING_CUSTOM:
furi_delay_ms(time_to_next_frame);
break;
Expand Down

0 comments on commit 858933a

Please sign in to comment.