Following is a complete source code.
The intent is to use LittleFS to save and recall data to and from program memory on a T4.0. The directory listing shows the file is created but that it has zero size.
What I am doing incorrectly?
Thank you
The intent is to use LittleFS to save and recall data to and from program memory on a T4.0. The directory listing shows the file is created but that it has zero size.
What I am doing incorrectly?
Thank you
Code:
#include "Arduino.h"
//#include <digitalWriteFast.h>
#include <limits.h>
#include <ADC.h>
#include <ADC_util.h>
#include <EEPROM.h>
#include <LittleFS.h>
LittleFS_Program filesys;
bool filesys_ready = false;
#define DATALENGTH 2048
uint16_t buffer[DATALENGTH];
#define RCVLEN 256
char rcvbuffer[RCVLEN];
uint16_t nrcvbuf = 0;
bool readfromfile( char *name ) {
File file;
if (!filesys_ready) {
Serial.println( "Error: filesys not ready" );
return false;
}
if ( !(file = filesys.open( name, FILE_READ )) ) {
Serial.println( "Error: not able to open for read" );
return false;
}
file.read( (char *) &buffer[0], DATALENGTH*2 );
file.close();
return true;
}
bool writetofile( char *name ) {
File file;
if (!filesys_ready) {
Serial.println( "Error: filesys not ready" );
return false;
}
if ( !(file = filesys.open( name, FILE_WRITE_BEGIN )) ) {
Serial.println( "Error: not able to open for write" );
return false;
}
file.write( (char *) &buffer[0], DATALENGTH*2 );
Serial.print( "saved to ");
Serial.print( file.name() );
Serial.print( " ");
Serial.print( DATALENGTH*2 );
Serial.print( " ");
Serial.println( file.size() );
file.close();
return true;
}
void listfiles() {
File dir, entry;
if (filesys_ready) {
Serial.print("Space Used = ");
Serial.println(filesys.usedSize());
Serial.print("Filesystem Size = ");
Serial.println(filesys.totalSize());
dir = filesys.open( "/" );
while( (entry = dir.openNextFile()) ) {
Serial.print( entry.name() );
Serial.print( " " );
Serial.print( entry.size(), DEC );
Serial.println();
entry.close();
}
dir.close();
}
else {
Serial.println( "Error: filesys not ready" );
}
}
char *startsWith( char *s, const char *key ) {
int n = strlen(key);
if ( !strncmp( s, key, n ) ) {
return s + n;
}
return 0;
}
char *parseUint( char *s, unsigned int *u ) {
unsigned long int l;
char *p = s;
l = strtoul( s, &p, 0 );
if ( (p > s) && (l <= UINT_MAX) ) {
*u = (unsigned int) l;
return p;
}
return 0;
}
void setup() {
if (!filesys.begin( DATALENGTH*2 + 1024 ) ) {
Serial.println( "not able to setup filesys" );
}
else {
filesys_ready = true;
Serial.println("filesystem ready");
listfiles();
}
}
void loop() {
uint16_t nlen = 0;
char *pc;
char c;
while ( Serial.available() ) {
c = Serial.read();
if ( c ) {
if ( iscntrl( c ) ) {
nlen = nrcvbuf;
rcvbuffer[nrcvbuf] = 0;
nrcvbuf = 0;
break;
}
else if ( nrcvbuf || !isspace(c) ) {
rcvbuffer[nrcvbuf++] = c;
}
if ( nrcvbuf >= RCVLEN ) {
Serial.println( (char *)"Error: buffer overflow" );
nrcvbuf = 0;
}
}
}
if ( nlen > 0 ) {
if ( (pc = startsWith( rcvbuffer, "save" )) ) {
while( pc && isspace(*pc) ) pc++;
writetofile( (char *) pc );
}
else if ( (pc = startsWith( rcvbuffer, "recall" )) ) {
while( pc && isspace(*pc) ) pc++;
if ( readfromfile( (char *) pc ) ) {
for ( int i = 0; i < DATALENGTH; i++ ) {
Serial.println( buffer[i] );
}
}
}
else if ( (pc = startsWith( rcvbuffer, "quickformat" )) ) {
filesys.quickFormat();
listfiles();
}
else if ( (pc = startsWith( rcvbuffer, "list files" )) ) {
listfiles();
}
else if ( (pc = startsWith( rcvbuffer, "remove" )) ) {
while( pc && isspace(*pc) ) pc++;
filesys.remove(pc);
listfiles();
}
else if ( (pc = startsWith( rcvbuffer, "send" )) ) {
for ( int i =0; i < DATALENGTH; i++ ) {
Serial.println( buffer[i] );
}
}
}
}