-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtm_reader.cpp
More file actions
131 lines (103 loc) · 2.16 KB
/
dtm_reader.cpp
File metadata and controls
131 lines (103 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "hw_config.h"
#include "f_util.h"
#include "ff.h"
#include "dtm_reader.hpp"
#include <cstdint>
#include <cstring>
#include <string>
void DTMReader::init()
{
FRESULT fr = f_mount(&_fs, "", 1);
if (fr != FR_OK)
{
_error_message = "MOUNT ERROR";
return;
}
auto filename = "REC.DTM";
fr = f_open(&_fil, filename, FA_OPEN_EXISTING | FA_READ);
if (fr != FR_OK && fr != FR_EXIST)
{
_error_message = "FILE NOT FOUND";
return;
}
fr = f_lseek(&_fil, INPUT_COUNT_POS);
if (fr != FR_OK)
{
_error_message = "SEEK ERROR 0X015";
return;
}
uint8_t byte_read = 0;
uint bytes_read = 0;
for (auto i = 0; i < INPUT_SIZE_BYTES; ++i)
{
fr = f_read(&_fil, &byte_read, 1, &bytes_read);
_input_count |= (uint64_t(byte_read) << (8 * i));
}
_file_open = true;
reset_file_pos();
}
DTMReader::DTMReader()
{
init();
}
DTMReader::~DTMReader()
{
close_file();
}
void DTMReader::reset_file_pos()
{
if (!_file_open)
{
_error_message = "RESET:FILE NOT OPEN";
return;
}
FRESULT fr = f_lseek(&_fil, INPUT_START_POS);
if (fr != FR_OK)
{
_error_message = "SEEK ERROR 0X100";
}
_inputs_read = 0;
}
bool DTMReader::get_next_input(uint8_t* input_bytes)
{
if (!_file_open)
{
_error_message = "GET:FILE NOT OPEN";
return false;
}
uint bytes_read = 0;
FRESULT fr = f_read(&_fil, input_bytes, INPUT_SIZE_BYTES, &bytes_read);
if (fr != FR_OK)
{
_error_message = "READ INPUT ERROR " + std::to_string((uint8_t)fr);
return false;
}
if (bytes_read != 8)
{
_error_message = "READ SIZE NOT 8";
return false;
}
++_inputs_read;
return true;
}
bool DTMReader::close_file()
{
if (!_file_open)
{
return true;
}
FRESULT fr = f_close(&_fil);
if (fr != FR_OK)
{
_error_message = "CLOSE ERROR";
return false;
}
fr = f_unmount("");
if (fr != FR_OK)
{
_error_message = "UNMOUNT ERROR";
return false;
}
_file_open = false;
return true;
}