-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSD_card.h
More file actions
117 lines (82 loc) · 3.34 KB
/
SD_card.h
File metadata and controls
117 lines (82 loc) · 3.34 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
/*
SD_card.h
# Depends on:
- defaults.h
# Description:
- This header contains the implementation of the synchronous SD card class "MySDcard", which is a singleton
and must be only referenced using the appropriate GET(MySDcard) as defaults.h defines it.
- This is an sync wrapper on the SD library from Arduino, with pins set from defaults.h, that allows any thread
to call SD functions.
*/
#pragma once
#include "defaults.h"
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#include <deque>
#include <string>
#include <mutex>
#include <memory>
#include <future>
enum class SD_type {C_OFFLINE, C_MMC, C_SD, C_SDHC};
struct file_info {
std::string fp_name;
uint32_t fp_size;
bool is_file;
};
// use GET(MySDcard) to get its singleton ref!
// MySDcard is an async SD card manager that allows any thread to append tasks of write / read to the card. It also tests for the SD card periodically.
MAKE_SINGLETON_CLASS(MySDcard, {
volatile bool m_initialized = false;
std::deque<std::packaged_task<void(void)>> m_tasks;
std::mutex m_tasks_mtx;
SD_type m_last_type = SD_type::C_OFFLINE;
void async_sdcard_caller();
// assists on pushing to queue. False only if queue is too large already (super rare if you have SD card)
bool push_task(std::packaged_task<void(void)>&&);
public:
MySDcard();
// Removes a file from SD card.
bool remove_file(const char* who);
// Get file size
size_t get_file_size(const char* path);
// Renames a file from SD card.
bool rename_file(const char* who, const char* to);
// Append to the end of a file, or create if not exists, data.
size_t append_on(const char* path, const char* what, const size_t len);
// Create or replace a file with data.
size_t overwrite_on(const char* path, const char* what, const size_t len);
// Read some data from file, with offset.
size_t read_from(const char* path, char* buffer, const size_t len, const uint32_t seek = 0);
// Abuse the sequential async queue to append to the end of a file, or create if not exists, data.
void async_append_on(const char* path, const char* what, const size_t len);
// Abuse the sequential async queue to create or replace a file with data.
void async_overwrite_on(const char* path, const char* what, const size_t len);
// Remove a directory.
bool remove_dir(const char* dir);
// Make a directory.
bool make_dir(const char* dir);
// Check if file exists (must not be a directory).
bool file_exists(const char* who);
// Check if directory exists (must be directory).
bool dir_exists(const char* dir);
// List files and dirs under this path:
std::deque<file_info> ls(const char* dir);
// Check SD card size.
size_t sd_card_size() const;
// Check max bytes that you can have in this SD card.
size_t sd_max_bytes() const;
// Check how much has been used in bytes so far.
size_t sd_used_bytes() const;
// Get used by max percentage, [0..1].
float sd_used_perc() const;
// Get SD card type currently. May change over time (inserting new one / removing one)
SD_type sd_type() const;
// Get current tasks size pending to be worked on.
size_t tasks_size() const;
// Get if it is online already
bool is_running() const;
// Get if SD card is functioning
bool is_online() const;
});
//#include "SD_card.ipp"