This is my code,
Main.cpp:
it first main is to create a way to test the class :
Code: Select all
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_spiffs.h"
#include "esp_log.h"
#include "PresetIO.h"
static const char *TAG = "example";
extern "C" void app_main(void)
{
PresetIO PresetLoadSave;
//----- CREATE EFFECT------
singleEffect_t Effect1={1,10,20,30,40};
singleEffect_t Effect2={1,50,60,70,80};
singleEffect_t Effect3={0,90,100,110,120};
singleEffect_t Effect4={0,130,140,150,160};
singleEffect_t Effect5={1,170,180,190,200};
singleEffect_t Effect6={0,210,220,230,240};
//----- CREATE PRESET------
MultiEffect_t Preset_1 ={Effect1,Effect2,Effect3,Effect4,Effect5,Effect6};
MultiEffect_t Preset_2 ={Effect2,Effect2,Effect3,Effect4,Effect5,Effect6};
MultiEffect_t Preset_3 ={Effect1,Effect1,Effect3,Effect6,Effect5,Effect6};
MultiEffect_t ExtractPreset={};
//---------- Initialize File partition
ESP_LOGI(TAG, "Initializing SPIFFS");
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true
};
// Use settings defined above to initialize and mount SPIFFS filesystem.
// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
} else {
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
return;
}
size_t total = 0, used = 0;
ret = esp_spiffs_info(NULL, &total, &used);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
} else {
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
}
//---------- END Initialize File partition
vTaskDelay(1000 / portTICK_PERIOD_MS);
//----- Show PresetLoadSave banks we would use
cout<<"Show Preset 1";
PresetLoadSave.showPresetBank(1);
cout<<"Show Preset 5";
PresetLoadSave.showPresetBank(5);
cout<<"Show Preset 99";
PresetLoadSave.showPresetBank(99);
//-----
cout<<"\n---------------INSERT EFFECTS-----------------------\n";
PresetLoadSave.insertMultiEffect(uint8_t(1),Preset_1);
PresetLoadSave.insertMultiEffect(uint8_t(5),Preset_2);
PresetLoadSave.insertMultiEffect(uint8_t(99),Preset_3);
//----- Show PresetLoadSave banks we would use
cout<<"\nShow Inserted Preset 1\n";
PresetLoadSave.showPresetBank(1);
cout<<"\nShow Inserted Preset 5\n";
PresetLoadSave.showPresetBank(5);
cout<<"\nShow Inserted Preset 99\n";
PresetLoadSave.showPresetBank(99);
//-----
cout<<"\n---------------SAVE EFFECTS-----------------------\n";
PresetLoadSave.savePresetBankFile();
//-----
cout<<"\n---------------EXTRACT EFFECTS-----------------------\n";
PresetLoadSave.savePresetBankFile();
ExtractPreset =PresetLoadSave.getMultiEffect(5);
cout << "\nExtractPreset from position 5: ";
for (uint8_t i=0; i<NUMB_PED; i++)
{
cout << "\nStatus: ";
printf("%3d",ExtractPreset.Pedal[i].status);
cout << "\tPot1 Value: ";
printf("%3d",ExtractPreset.Pedal[i].pot_1);
cout << "\tPot2 Value: ";
printf("%3d",ExtractPreset.Pedal[i].pot_2);
cout << "\tPot3 Value: ";
printf("%3d",ExtractPreset.Pedal[i].pot_3);
cout << "\tPot4 Value: ";
printf("%3d",ExtractPreset.Pedal[i].pot_4);
}
//-----
PresetLoadSave.insertMultiEffect(uint8_t(10),ExtractPreset);
cout<<"\nShow Inserted Preset 10\n";
PresetLoadSave.showPresetBank(10);
//-----
cout<<"\n---------------LOAD EFFECTS-----------------------\n";
PresetLoadSave.loadPresetBankFile();
vTaskDelay(1000 / portTICK_PERIOD_MS);
//-----
cout<<"\n---------------CHECK EFFECTS-----------------------\n";
cout<<"\nPosition 10 should be empty after load\n";
cout<<"\nShow Inserted Preset 1";
PresetLoadSave.showPresetBank(1);
cout<<"\nShow Inserted Preset 5";
PresetLoadSave.showPresetBank(5);
cout<<"\nShow Inserted Preset 99";
PresetLoadSave.showPresetBank(99);
cout<<"\nShow Inserted Preset 10";
PresetLoadSave.showPresetBank(10);
}
The class PresetIO.cpp is used to manage structure and to save and load data from file using fstream:
Code: Select all
#include "PresetIO.h"
PresetIO::PresetIO()
{
//ctor
}
PresetIO::~PresetIO()
{
//dtor
}
void PresetIO::printPedal(singleEffect_t sp)
{
cout << "\nStatus: ";
printf("%3d",sp.status);
cout << "\tPot1 Value: ";
printf("%3d",sp.pot_1);
cout << "\tPot2 Value: ";
printf("%3d",sp.pot_2);
cout << "\tPot3 Value: ";
printf("%3d",sp.pot_3);
cout << "\tPot4 Value: ";
printf("%3d",sp.pot_4);
}
/*-----------------------------------------------*/
void PresetIO::printMultiEffect(MultiEffect_t me)
{
for (uint8_t i=0; i<NUMB_PED; i++)
{
printPedal(me.Pedal[i]);
}
}
/*-----------------------------------------------*/
void PresetIO::insertMultiEffect(uint8_t id,MultiEffect_t me)
{
PresetBanks[id] = me;
printf("\nMulti Effect saved in %d location\n",id);
printMultiEffect(PresetBanks[id]);
}
/*-----------------------------------------------*/
void PresetIO::showPresetBank(uint8_t id)
{
printf("\nPrtint Effect saved in location %d",id);
printMultiEffect(PresetBanks[id]);
cout<<endl;
}
/*-----------------------------------------------*/
MultiEffect_t PresetIO::getMultiEffect(uint8_t id)
{
return PresetBanks[id];
}
/*-----------------------------------------------*/
uint8_t PresetIO::savePresetBankFile()
{
uint8_t result;
ofstream output_file(SAVE_FILE_NAME, ios::binary);
result=output_file.rdstate(); //check if file is correcyly opened
if(!result)
{
output_file.write((char*)&PresetBanks, sizeof(PresetBanks));
output_file.close();
printf("\nFile Saved Correctly, tot %d KB",sizeof(PresetBanks)/1024);
}else
{
cout<<"\nError While saving File";
}
return result;
}
/*-----------------------------------------------*/
uint8_t PresetIO::loadPresetBankFile()
{
uint8_t result;
ifstream input_file(SAVE_FILE_NAME, ios::binary);
result=input_file.rdstate(); //check if file is correcyly opened
if(!result)
{
input_file.read((char*)&PresetBanks, sizeof(PresetBanks));
printf("\nFile Correctly Loaded,tot %d KB",sizeof(PresetBanks)/1024);
}else
{
cout<<"Error While saving File";
}
return result;
}
and this is the PrestIO.h :
Code: Select all
#ifndef PRESETIO_H
#define PRESETIO_H
#include "sdv_t.h"
#include <iostream>
#include <fstream>
#include <string.h>
#define SAVE_FILE_NAME "/spiffs/effects.data"
using namespace std;
class PresetIO
{
public:
PresetIO();
virtual ~PresetIO();
/**
* @brief insertMultiEffect is used to insert a generic MultiEffect Variabile in defined position
* @param id value from 0 to NUMB_PRESET-1 identify the position where store data
* @param me Multieffect variable identify values of all 6 Effects
*/
void insertMultiEffect(uint8_t id,MultiEffect_t me);
/**
* @brief showPresetBank is used to print on screen what is written in specific PresetBanks position
* @param id value from 0 to NUMB_PRESET-1 identify the position from where read data
*/
void showPresetBank(uint8_t id);
/**
* @brief getMultiEffect is used to extract a MultiEffect Structure from saved PresetBanks
* @param id value from 0 to NUMB_PRESET-1 identify the position from where read data
*
* @return MultiEffect_t, the structure extracted from PresetBanks
*/
MultiEffect_t getMultiEffect(uint8_t id);
/**
* @brief savePresetBankFile is a function used to save PresetBanks on file
* the file name is SAVE_FILE_NAME, it uses ofstream output_file
*
* @return returns the output_file.rdstate status, if 0 OK else there is a problem
*/
uint8_t savePresetBankFile();
/**
* @brief loadPresetBankFile is a function used to Load PresetBanks from file
* the file name is SAVE_FILE_NAME, it uses ifstream input_file
*
* @return returns the input_file.rdstate status, if 0 OK else there is a problem
*/
uint8_t loadPresetBankFile();
protected:
private:
/**
*@brief PresetBanks is an array of NUMB_PRESET(default 100) possible Preset
* used to save and load info from files
* PresetBank:
* o 99*Multi Effect
* o 6*single Effects
*/
MultiEffect_t PresetBanks[NUMB_PRESET]={};
/**
* @brief printPedal is used to print on screen what is inside one of MultiEffect Pedals
* @param sp is the singleEffect passed the the function
*/
void printPedal(singleEffect_t sp);
/**
* @brief printPedal is used to print on screen what is inside a MultiEffect
* @param me is the MultiEffect passed the the function
*/
void printMultiEffect(MultiEffect_t me);
};
#endif // PRESETIO_H
this last file is an header" sdv_t.h"used to create structures types :
Code: Select all
#ifndef SDV_T_H_INCLUDED
#define SDV_T_H_INCLUDED
#include <iostream>
#define NUMB_PED 6 //NUMBER OF PEDAL USED
#define NUMB_PRESET 100 //NUMBER OF PRESET
/**
* singleEffect_t is a structure identify the single Pedal made of:
* Status (on/off)
* 4* Pot values
*/
struct singleEffect_t
{
uint8_t status; //on off status of the pedal
uint8_t pot_1; //potentiometer 1 value
uint8_t pot_2; //potentiometer 2 value
uint8_t pot_3; //potentiometer 3 value
uint8_t pot_4; //potentiometer 4 value
};
/**
* MultiEffect_t is a structure identify the single preset of all NUMB_PED pedals
*/
struct MultiEffect_t
{
singleEffect_t Pedal[NUMB_PED];
};
#endif // SDV_T_H_INCLUDED
and this last file is the SPIFFS partition table setup "partitions.csv" :
Code: Select all
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, spiffs, , 0xF0000,
I have attached the entrire project I have written.
The only configuration to do is in menuconfig :
- Partition Table \ Custom Partition Table,
-select partitions.csv
-Offset 0x8000
I have already used the partition.csv and correctly set menuconfig to test spiffs before.
I think the wrong part is on fstream because is the only new thing i'm using in this sketch.
Thakyou for your help.