Skip to content

Commit 5368670

Browse files
authored
Merge pull request #2 from polarikus/dev
Dev
2 parents 4b8a15d + 0fe0430 commit 5368670

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

bc_scanner_script.c

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
#include <furi.h>
22
#include <furi_hal.h>
3-
#include <gui/gui.h>
4-
#include <input/input.h>
5-
#include <lib/toolbox/args.h>
63
#include <furi_hal_usb_cdc.h>
74
#include <storage/storage.h>
85
#include "bc_scanner_script.h"
9-
#include <dolphin/dolphin.h>
106
#include "cli/cli_vcp.h"
117
#include "cli/cli.h"
128

139
#define TAG "BarCodeScanner"
1410
#define WORKER_TAG TAG "Worker"
15-
#define FILE_BUFFER_LEN 1
11+
#define FILE_BUFFER_LEN 50
1612

1713
#define SCRIPT_STATE_ERROR (-1)
1814
#define SCRIPT_STATE_END (-2)
1915
#define SCRIPT_STATE_NEXT_LINE (-3)
2016

21-
#define USB_CDC_PKT_LEN CDC_DATA_SZ
22-
#define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
23-
24-
#define USB_CDC_BIT_DTR (1 << 0)
25-
#define USB_CDC_BIT_RTS (1 << 1)
17+
#define UART_BAUD 19200
18+
#define UART_PORT 0
2619

2720
typedef enum {
2821
WorkerEvtToggle = (1 << 0),
@@ -34,19 +27,10 @@ typedef enum {
3427
struct BarCodeScript {
3528
BarCodeState st;
3629
FuriString* file_path;
37-
uint32_t defdelay;
3830
FuriThread* thread;
3931
uint8_t file_buf[FILE_BUFFER_LEN];
40-
uint8_t buf_start;
4132
uint8_t buf_len;
42-
bool file_end;
43-
FuriString* line;
44-
45-
UartConfig cfg;
46-
UartConfig cfg_new;
47-
48-
FuriString* line_prev;
49-
uint32_t repeat_cnt;
33+
bool is_file_end;
5034
};
5135

5236

@@ -57,7 +41,7 @@ static void usb_uart_serial_init() {
5741
furi_record_close(RECORD_CLI);
5842
furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
5943
furi_hal_console_disable();
60-
furi_hal_uart_set_br(FuriHalUartIdUSART1, 19200);
44+
furi_hal_uart_set_br(FuriHalUartIdUSART1, UART_BAUD);
6145
}
6246

6347
static void usb_uart_serial_deinit() {
@@ -69,16 +53,22 @@ static void usb_uart_serial_deinit() {
6953
furi_hal_console_enable();
7054
}
7155

72-
static bool bc_script_preload(BarCodeScript* bc_script, File* script_file){
73-
bc_script->st.line_nb = 1;
74-
UNUSED(script_file);
75-
return true;
76-
}
77-
7856
static bool is_bc_end(const char chr) {
7957
return ((chr == '\0') || (chr == '\r') || (chr == '\n'));//TODO SPACE NEED???
8058
}
8159

60+
static uint16_t bc_script_read_file(BarCodeScript* bc_script, File* script_file){
61+
UNUSED(is_bc_end);
62+
bc_script->st.line_nb = 0;
63+
uint16_t ret = storage_file_read(script_file, bc_script->file_buf, FILE_BUFFER_LEN);
64+
if(storage_file_eof(script_file)) {
65+
bc_script->is_file_end = true;
66+
}
67+
bc_script->st.line_nb += ret;
68+
return ret;
69+
}
70+
71+
8272
static int32_t bc_scanner_worker(void* context){
8373
BarCodeScript* bc_script = context;
8474

@@ -104,14 +94,18 @@ static int32_t bc_scanner_worker(void* context){
10494
furi_string_get_cstr(bc_script->file_path),
10595
FSAM_READ,
10696
FSOM_OPEN_EXISTING)) {
107-
if((bc_script_preload(bc_script, script_file)) && (bc_script->st.line_nb > 0)) {
97+
uint64_t size = storage_file_size(script_file);
98+
bc_script->st.line_nb = size;
99+
if(size > 0) {
108100
if(1) { //TODO Check USB Connect
109101
worker_state = BarCodeStateIdle; // Ready to run
110102
} else {
111103
//worker_state = BadUsbStateNotConnected; // USB not connected
112104
}
113105
} else {
114-
worker_state = BarCodeStateScriptError; // Script preload error
106+
FURI_LOG_E(WORKER_TAG, "File empty error");
107+
worker_state = BarCodeStateFileError;
108+
bc_script->st.error_line = 0;
115109
}
116110
} else {
117111
FURI_LOG_E(WORKER_TAG, "File open error");
@@ -130,29 +124,31 @@ static int32_t bc_scanner_worker(void* context){
130124
worker_state = BarCodeStateIdle; // Ready to run
131125
} else if(flags & WorkerEvtToggle) {
132126
FURI_LOG_I(WORKER_TAG, "SendUART_MSG");
133-
uint16_t ret = 0;
134-
do {
135-
ret = storage_file_read(script_file, bc_script->file_buf, FILE_BUFFER_LEN);
136-
if (is_bc_end((char)bc_script->file_buf[0]))
137-
{
138-
bc_script->st.line_nb++;
139-
break;
140-
}
141-
142-
furi_hal_cdc_send(0, bc_script->file_buf, FILE_BUFFER_LEN);
143-
furi_delay_ms(10);
144-
if(storage_file_eof(script_file)) {
145-
bc_script->st.line_nb++;
146-
break;
147-
}
148-
}while (ret > 0);
149-
127+
bc_script->st.state = BarCodeStateRunning;
128+
bc_script->st.line_cur = 0;
129+
furi_delay_ms(500);
130+
while(!bc_script->is_file_end){
131+
bc_script->st.state = BarCodeStateRunning;
132+
uint16_t size = bc_script_read_file(bc_script, script_file);
133+
bc_script->st.line_cur = size;
134+
furi_hal_cdc_send(UART_PORT, bc_script->file_buf, size);
135+
}
150136
worker_state = BarCodeStateIdle;
151137
bc_script->st.state = BarCodeStateDone;
152138
storage_file_seek(script_file, 0, true);
139+
bc_script->is_file_end = false;
153140
continue;
154141
}
155142
bc_script->st.state = worker_state;
143+
}else if(
144+
(worker_state == BarCodeStateFileError) ||
145+
(worker_state == BarCodeStateScriptError)) { // State: error
146+
uint32_t flags = furi_thread_flags_wait(
147+
WorkerEvtEnd, FuriFlagWaitAny, FuriWaitForever); // Waiting for exit command
148+
furi_check((flags & FuriFlagError) == 0);
149+
if(flags & WorkerEvtEnd) {
150+
break;
151+
}
156152
}
157153

158154
}

bc_scanner_script.h

-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ typedef struct {
2727
char error[64];
2828
} BarCodeState;
2929

30-
typedef struct {
31-
uint8_t vcp_ch;
32-
uint8_t baudrate_mode;
33-
uint32_t baudrate;
34-
} UartConfig;
35-
3630
BarCodeScript* bc_scanner_script_open(FuriString* file_path);
3731
void bc_scanner_script_close(BarCodeScript* bc_script);
3832

views/bc_scanner_view.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void bc_scanner_draw_callback(Canvas* canvas, void* _model) {
6666
}
6767
canvas_set_font(canvas, FontBigNumbers);
6868
furi_string_printf(
69-
disp_str, "%u", ((model->state.line_cur - 1) * 100) / model->state.line_nb);
69+
disp_str, "%u", ((model->state.line_cur) * 100) / model->state.line_nb);
7070
canvas_draw_str_aligned(
7171
canvas, 114, 36, AlignRight, AlignBottom, furi_string_get_cstr(disp_str));
7272
furi_string_reset(disp_str);

0 commit comments

Comments
 (0)