ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
esp_sd.h
Go to the documentation of this file.
1 /*
2  esp_sd.h - ESP3D SD support class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef _ESP_SD_H
22 #define _ESP_SD_H
23 #include "../../include/esp3d_config.h"
24 #include "../../core/esp3doutput.h"
25 #include <time.h>
26 
27 #define ESP_SD_FS_HEADER "/SD"
28 
29 #define ESP_MAX_SD_OPENHANDLE 4
30 
32 {
33 public:
34  ESP_SDFile(void * handle = nullptr, bool isdir =false, bool iswritemode = false, const char * path = nullptr);
35  ESP_SDFile(const char * name, const char * filename, bool isdir = true, size_t size =0);
36  ~ESP_SDFile();
37  operator bool() const;
38  bool isDirectory();
39  const char* name() const;
40  const char* shortname() const;
41  const char* filename() const;
42  void close();
43  bool isOpen();
44  ESP_SDFile & operator=(const ESP_SDFile & other);
45  size_t size();
46  time_t getLastWrite();
47  int available();
48  size_t write(uint8_t i);
49  size_t write(const uint8_t *buf, size_t size);
50  int read();
51  size_t read(uint8_t* buf, size_t size);
52  void flush();
54 private:
55  String _dirlist;
56  bool _isdir;
57  bool _iswritemode;
58  int8_t _index;
59  String _filename;
60  String _name;
61  size_t _size;
62  time_t _lastwrite;
63 };
64 
65 class ESP_SD
66 {
67 public:
68  static String & formatBytes (uint64_t bytes);
69  ESP_SD();
70  ~ESP_SD();
71  static bool begin();
72  static void handle();
73  static void end();
74  static uint8_t getState(bool refresh);
75  static uint8_t setState(uint8_t state);
76  static uint64_t totalBytes();
77  static uint64_t usedBytes();
78  static uint64_t freeBytes();
79  static const char * FilesystemName();
80  static bool format(ESP3DOutput * output = nullptr);
81  static ESP_SDFile open(const char* path, uint8_t mode = ESP_FILE_READ);
82  static bool exists(const char* path);
83  static bool remove(const char *path);
84  static bool mkdir(const char *path);
85  static bool rmdir(const char *path);
86  static bool rename(const char *oldpath, const char *newpath);
87  static void closeAll();
88  static uint8_t getSPISpeedDivider()
89  {
90  return _spi_speed_divider;
91  }
92  static bool setSPISpeedDivider(uint8_t speeddivider);
93 private:
94  static bool _started;
95  static uint8_t _state;
96  static uint8_t _spi_speed_divider;
97  static bool _sizechanged;
98 };
99 
100 
101 #endif //_ESP_SD_H
ESP_SDFile::isDirectory
bool isDirectory()
Definition: esp_sd.cpp:125
ESP_SD::closeAll
static void closeAll()
ESP_SD::open
static ESP_SDFile open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_SDFile::name
const char * name() const
Definition: esp_sd.cpp:115
ESP_FILE_READ
#define ESP_FILE_READ
Definition: defines.h:120
ESP_SD::totalBytes
static uint64_t totalBytes()
ESP_SDFile::available
int available()
Definition: esp_sd.cpp:140
ESP_SDFile
Definition: esp_sd.h:31
ESP_SD::freeBytes
static uint64_t freeBytes()
ESP_SD::ESP_SD
ESP_SD()
Definition: esp_sd.cpp:53
ESP_SD::remove
static bool remove(const char *path)
ESP_SDFile::write
size_t write(uint8_t i)
Definition: esp_sd.cpp:148
ESP_SDFile::size
size_t size()
Definition: esp_sd.cpp:130
ESP_SD
Definition: esp_sd.h:65
ESP_SD::setState
static uint8_t setState(uint8_t state)
Definition: esp_sd.cpp:45
ESP_SD::begin
static bool begin()
ESP_SD::handle
static void handle()
Definition: esp_sd.cpp:62
ESP_SD::rmdir
static bool rmdir(const char *path)
ESP_SD::mkdir
static bool mkdir(const char *path)
ESP_SDFile::operator=
ESP_SDFile & operator=(const ESP_SDFile &other)
Definition: esp_sd.cpp:188
ESP_SDFile::getLastWrite
time_t getLastWrite()
Definition: esp_sd.cpp:135
ESP_SD::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_sd.cpp:68
ESP_SDFile::openNextFile
ESP_SDFile openNextFile()
ESP_SD::getState
static uint8_t getState(bool refresh)
ESP_SDFile::read
int read()
Definition: esp_sd.cpp:164
ESP_SDFile::isOpen
bool isOpen()
Definition: esp_sd.cpp:110
ESP_SDFile::ESP_SDFile
ESP_SDFile(void *handle=nullptr, bool isdir=false, bool iswritemode=false, const char *path=nullptr)
ESP_SD::FilesystemName
static const char * FilesystemName()
ESP_SD::format
static bool format(ESP3DOutput *output=nullptr)
ESP_SDFile::shortname
const char * shortname() const
ESP_SDFile::flush
void flush()
Definition: esp_sd.cpp:180
ESP_SDFile::close
void close()
ESP_SD::rename
static bool rename(const char *oldpath, const char *newpath)
ESP_SD::exists
static bool exists(const char *path)
ESP_SD::~ESP_SD
~ESP_SD()
Definition: esp_sd.cpp:58
ESP_SDFile::~ESP_SDFile
~ESP_SDFile()
Definition: esp_sd.cpp:95
ESP3DOutput
Definition: esp3doutput.h:48
ESP_SDFile::filename
const char * filename() const
Definition: esp_sd.cpp:120
ESP_SD::getSPISpeedDivider
static uint8_t getSPISpeedDivider()
Definition: esp_sd.h:88
ESP_SD::usedBytes
static uint64_t usedBytes()
ESP_SD::setSPISpeedDivider
static bool setSPISpeedDivider(uint8_t speeddivider)
Definition: esp_sd.cpp:202
ESP_SD::end
static void end()