1
1
#include <furi.h>
2
2
#include <furi_hal.h>
3
- #include <gui/gui.h>
4
- #include <input/input.h>
5
- #include <lib/toolbox/args.h>
6
3
#include <furi_hal_usb_cdc.h>
7
4
#include <storage/storage.h>
8
5
#include "bc_scanner_script.h"
9
- #include <dolphin/dolphin.h>
10
6
#include "cli/cli_vcp.h"
11
7
#include "cli/cli.h"
12
8
13
9
#define TAG "BarCodeScanner"
14
10
#define WORKER_TAG TAG "Worker"
15
- #define FILE_BUFFER_LEN 1
11
+ #define FILE_BUFFER_LEN 50
16
12
17
13
#define SCRIPT_STATE_ERROR (-1)
18
14
#define SCRIPT_STATE_END (-2)
19
15
#define SCRIPT_STATE_NEXT_LINE (-3)
20
16
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
26
19
27
20
typedef enum {
28
21
WorkerEvtToggle = (1 << 0 ),
@@ -34,19 +27,10 @@ typedef enum {
34
27
struct BarCodeScript {
35
28
BarCodeState st ;
36
29
FuriString * file_path ;
37
- uint32_t defdelay ;
38
30
FuriThread * thread ;
39
31
uint8_t file_buf [FILE_BUFFER_LEN ];
40
- uint8_t buf_start ;
41
32
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 ;
50
34
};
51
35
52
36
@@ -57,7 +41,7 @@ static void usb_uart_serial_init() {
57
41
furi_record_close (RECORD_CLI );
58
42
furi_check (furi_hal_usb_set_config (& usb_cdc_single , NULL ) == true);
59
43
furi_hal_console_disable ();
60
- furi_hal_uart_set_br (FuriHalUartIdUSART1 , 19200 );
44
+ furi_hal_uart_set_br (FuriHalUartIdUSART1 , UART_BAUD );
61
45
}
62
46
63
47
static void usb_uart_serial_deinit () {
@@ -69,16 +53,22 @@ static void usb_uart_serial_deinit() {
69
53
furi_hal_console_enable ();
70
54
}
71
55
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
-
78
56
static bool is_bc_end (const char chr ) {
79
57
return ((chr == '\0' ) || (chr == '\r' ) || (chr == '\n' ));//TODO SPACE NEED???
80
58
}
81
59
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
+
82
72
static int32_t bc_scanner_worker (void * context ){
83
73
BarCodeScript * bc_script = context ;
84
74
@@ -104,14 +94,18 @@ static int32_t bc_scanner_worker(void* context){
104
94
furi_string_get_cstr (bc_script -> file_path ),
105
95
FSAM_READ ,
106
96
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 ) {
108
100
if (1 ) { //TODO Check USB Connect
109
101
worker_state = BarCodeStateIdle ; // Ready to run
110
102
} else {
111
103
//worker_state = BadUsbStateNotConnected; // USB not connected
112
104
}
113
105
} 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 ;
115
109
}
116
110
} else {
117
111
FURI_LOG_E (WORKER_TAG , "File open error" );
@@ -130,29 +124,31 @@ static int32_t bc_scanner_worker(void* context){
130
124
worker_state = BarCodeStateIdle ; // Ready to run
131
125
} else if (flags & WorkerEvtToggle ) {
132
126
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
+ }
150
136
worker_state = BarCodeStateIdle ;
151
137
bc_script -> st .state = BarCodeStateDone ;
152
138
storage_file_seek (script_file , 0 , true);
139
+ bc_script -> is_file_end = false;
153
140
continue ;
154
141
}
155
142
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
+ }
156
152
}
157
153
158
154
}
0 commit comments