Permalink
Browse files

Add SdFat support.

  • Loading branch information...
dulsi committed Jun 16, 2018
1 parent 78ce854 commit 76ad30898889f3406e6fb66e2296c387d8701029
Showing with 126 additions and 1 deletion.
  1. +46 −0 include/SdFat.h
  2. +79 −0 src/SdFat.cpp
  3. +1 −1 tinyscreensim
@@ -0,0 +1,46 @@
#ifndef __SDFAT_H__
#define __SDFAT_H__
#include <stdio.h>
#include <inttypes.h>
#include <string>
uint8_t const SPI_FULL_SPEED = 2;
uint8_t const O_READ = 0X01;
uint8_t const O_WRITE = 0X02;
uint8_t const O_TRUNC = 0X10;
uint8_t const O_CREAT = 0X40;
class SdFat
{
friend class SdFile;
public:
bool begin(uint8_t csPin, uint8_t divisor);
bool exists(const char* path);
protected:
static std::string savePath;
};
class SdFile
{
public:
SdFile() : f(NULL) { }
bool close();
bool open(const char* path, uint8_t oflag = O_READ);
int read(void* buf, size_t nbyte);
bool sync();
size_t write(const uint8_t *buf, size_t size);
protected:
FILE *f;
};
#endif
@@ -0,0 +1,79 @@
#include <SdFat.h>
#include <boost/filesystem.hpp>
std::string SdFat::savePath;
bool SdFat::begin(uint8_t csPin, uint8_t divisor)
{
#ifdef _WIN32
std::string appName("sdfat");
#else
std::string appName(".sdfat");
#endif
char *home = getenv("HOME");
if (home)
{
savePath = home;
if (savePath[savePath.length() - 1] != '/')
{
savePath += "/";
}
savePath += appName;
}
else
{
savePath = appName;
}
if (!boost::filesystem::is_directory(savePath))
{
if (!boost::filesystem::create_directory(savePath))
{
fprintf(stderr, "Error creating directory %s\n", savePath);
exit(2);
}
}
return 1;
}
bool SdFat::exists(const char* path)
{
std::string fullPath = savePath + "/" + path;
return boost::filesystem::exists(fullPath);
}
bool SdFile::close()
{
fclose(f);
return true;
}
bool SdFile::open(const char* path, uint8_t oflag /*= O_READ*/)
{
std::string fullPath = SdFat::savePath + "/" + path;
if (oflag == O_READ)
{
f = fopen(fullPath.c_str(), "rb");
return (f != NULL);
}
else if (oflag == (O_CREAT | O_WRITE | O_TRUNC))
{
f = fopen(fullPath.c_str(), "wb");
return (f != NULL);
}
return 0; //failure
}
int SdFile::read(void* buf, size_t nbyte)
{
return fread(buf, 1, nbyte, f);
}
bool SdFile::sync()
{
return true;
}
size_t SdFile::write(const uint8_t *buf, size_t size)
{
return fwrite(buf, 1, size, f);
}
@@ -9,6 +9,6 @@ output=$1
cppfile=$mytemp/$2.cpp
cp $2 $mytemp/$2.cpp
shift 2
g++ -o $output -DTINYSCREENSIM=1 $cppfile $myscriptpath/src/Main.cpp "$@" -I$myscriptpath/include -I$myinopath -lGL -lglfw
g++ -o $output -DTINYSCREENSIM=1 $cppfile $myscriptpath/src/Main.cpp $myscriptpath/src/SdFat.cpp "$@" -I$myscriptpath/include -I$myinopath -lGL -lglfw -lboost_filesystem -lboost_system
rm $cppfile
rmdir $mytemp

0 comments on commit 76ad308

Please sign in to comment.