CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/examples/File_IO/File_IO.cpp
Views: 1799
1
//Simple File I/O example
2
3
#include <AP_HAL/AP_HAL.h>
4
#include <AP_BoardConfig/AP_BoardConfig.h>
5
#include <AP_Filesystem/AP_Filesystem.h>
6
#include <stdio.h>
7
8
//set according to the data size in file
9
#define BUFFER_SIZE 50
10
11
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
12
13
void setup();
14
void loop();
15
16
static AP_BoardConfig board_config;
17
18
int file_count = 0;
19
const char *dir_path = nullptr;
20
static void write_file(const char *);
21
static void read_file(const char *);
22
23
void setup()
24
{
25
board_config.init();
26
27
//creating a test directory to store all files
28
dir_path = "./test_dir";
29
hal.console->printf("Creating directory - %s\n", dir_path);
30
int ret = AP::FS().mkdir(dir_path);
31
if (ret == -1) {
32
if (errno == EEXIST) {
33
hal.console->printf("Directory - %s already exist\n", dir_path);
34
} else {
35
hal.console->printf("Failed to create directory %s - %s\n", dir_path, strerror(errno));
36
}
37
}
38
}
39
40
void loop()
41
{
42
//creating 10 files in test directory
43
if (file_count == 10) {
44
hal.console->printf("Finished creating files\n\n");
45
hal.scheduler->delay(1000);
46
return;
47
}
48
49
file_count += 1;
50
char file_path[100];
51
snprintf(file_path, sizeof(file_path), "%s/test_%d.txt", dir_path, file_count);
52
write_file(file_path);
53
hal.scheduler->delay(1000);
54
read_file(file_path);
55
hal.scheduler->delay(1000);
56
}
57
58
static void write_file(const char *file_path)
59
{
60
char write_buf[BUFFER_SIZE];
61
62
//creating or opening a file in write mode
63
const int open_fd = AP::FS().open(file_path, O_WRONLY | O_CREAT | O_TRUNC);
64
if (open_fd == -1) {
65
hal.console->printf("Open %s failed - %s\n", file_path, strerror(errno));
66
return;
67
}
68
69
//writing to file
70
const char *s = "Hello_World";
71
hal.console->printf("Writing to file %s => %s_%d\n", file_path, s, file_count);
72
snprintf(write_buf, sizeof(write_buf), "%s_%d\n", s, file_count);
73
const ssize_t to_write = strlen(write_buf);
74
const ssize_t write_size = AP::FS().write(open_fd, write_buf, to_write);
75
if (write_size == -1 or write_size != to_write) {
76
hal.console->printf("Write failed - %s\n", strerror(errno));
77
return;
78
}
79
80
//closing file after writing
81
AP::FS().close(open_fd);
82
}
83
84
static void read_file(const char *file_path)
85
{
86
char read_buf[BUFFER_SIZE];
87
88
//opening a file in read mode
89
const int open_fd = AP::FS().open(file_path, O_RDONLY);
90
if (open_fd == -1) {
91
hal.console->printf("Open %s failed\n", file_path);
92
return;
93
}
94
95
//reading from file
96
const ssize_t read_size = AP::FS().read(open_fd, read_buf, sizeof(read_buf));
97
if (read_size == -1) {
98
hal.console->printf("Read failed - %s\n", strerror(errno));
99
return;
100
}
101
102
hal.console->printf("Reading from file %s => %s\n", file_path, read_buf);
103
104
//closing file after reading
105
AP::FS().close(open_fd);
106
}
107
108
AP_HAL_MAIN();
109
110