mirror of
https://github.com/anklimov/lighthub
synced 2025-12-09 21:29:49 +03:00
EEPROM refactoring& http API. Mega&due tested
This commit is contained in:
134
lighthub/flashstream.h
Normal file
134
lighthub/flashstream.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
#ifndef _FLASHSTREAM_H_
|
||||
#define _FLASHSTREAM_H_
|
||||
|
||||
#include <main.h>
|
||||
|
||||
#if defined(ESP32)
|
||||
#include <FS.h>
|
||||
#include <SPIFFS.h>
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#include <EEPROM.h>
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#include <ESP_EEPROM.h>
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__SAM3X8E__)
|
||||
#include <DueFlashStorage.h>
|
||||
extern DueFlashStorage EEPROM;
|
||||
#endif
|
||||
|
||||
#ifdef NRF5
|
||||
#include <NRFFlashStorage.h> //STUB
|
||||
extern NRFFlashStorage EEPROM;
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO_ARCH_STM32
|
||||
#include <NRFFlashStorage.h> //STUB
|
||||
extern NRFFlashStorage EEPROM;
|
||||
#endif
|
||||
|
||||
|
||||
#include <Stream.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class seekableStream : public Stream
|
||||
{
|
||||
unsigned int streamSize;
|
||||
public:
|
||||
seekableStream(unsigned int size):Stream(),streamSize(size) {};
|
||||
unsigned int getSize() {return streamSize;}
|
||||
virtual unsigned int seek(unsigned int _pos = 0) = 0;
|
||||
};
|
||||
|
||||
#if defined(ESP32)
|
||||
class flashStream : public seekableStream
|
||||
{
|
||||
private:
|
||||
File fs;
|
||||
public:
|
||||
flashStream(String _filename):seekableStream(65535) {fs = SPIFFS.open(_filename, "r+");}
|
||||
virtual int available() { return 1; };
|
||||
virtual int read() {return fs.read();};
|
||||
virtual int peek() { return fs.peek();};
|
||||
virtual unsigned int seek(unsigned int _pos = 0){return fs.seek(_pos,SeekSet);};
|
||||
virtual void flush() {};
|
||||
virtual size_t write(uint8_t ch) {return fs.write(ch);};
|
||||
void putEOF(){write (255);};
|
||||
virtual ~flashStream () {if (fs) fs.close();} ;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
|
||||
class flashStream : public seekableStream
|
||||
{
|
||||
protected:
|
||||
unsigned int pos;
|
||||
unsigned int startPos;
|
||||
unsigned int size;
|
||||
|
||||
public:
|
||||
flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_size)
|
||||
{
|
||||
pos = _startPos; startPos = _startPos; size = _size;
|
||||
#if defined(ESP8266)
|
||||
size_t len = EEPROM.length();
|
||||
if (len) EEPROM.commit();
|
||||
EEPROM.begin(len+size); //Re-init
|
||||
#endif
|
||||
};
|
||||
|
||||
virtual unsigned int seek(unsigned int _pos = 0)
|
||||
{ pos=min(startPos+_pos, startPos+size);
|
||||
debugSerial<<F("Seek:")<<pos<<endl;
|
||||
return pos;
|
||||
};
|
||||
virtual int available() { return 1;};
|
||||
virtual int read() {int ch = peek(); pos++; return ch;};
|
||||
virtual int peek()
|
||||
{
|
||||
return EEPROM.read(pos);
|
||||
};
|
||||
virtual void flush() {
|
||||
#if defined(ESP8266)
|
||||
EEPROM.commit();
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined(__SAM3X8E__)
|
||||
virtual size_t write(const uint8_t *buffer, size_t size) override
|
||||
{
|
||||
debugSerial<<("Write from:")<<pos<<" "<<size<<" bytes"<<endl;
|
||||
EEPROM.write(pos,(byte*)buffer,size);
|
||||
pos+=size;
|
||||
return size;
|
||||
};
|
||||
#endif
|
||||
virtual size_t write(uint8_t ch)
|
||||
{
|
||||
#if defined(__AVR__)
|
||||
//Serial.print (ch);
|
||||
EEPROM.update(pos++,(char)ch);
|
||||
return 1;
|
||||
#endif
|
||||
|
||||
#if defined(__SAM3X8E__)
|
||||
return EEPROM.write(pos++,(char)ch);
|
||||
#endif
|
||||
};
|
||||
void putEOF(){write (255);
|
||||
#if defined(ESP8266)
|
||||
EEPROM.commit();
|
||||
#endif
|
||||
};
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user