How Optimize Memory Usage of this Code?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm writing application that GET/SET string value in a main string named "_pacchetto".
//example: 

before: _pacchetto "$#_______________________________________$"
SET_Tipo_Mittente("03"); 
after: _pacchetto "$#03#_______________________________________$"

before: _pacchetto "$#_______________________________________$"
SET_Mittente("ABCDE"); 
after: _pacchetto "$#___ABCDE#_______________________________________$"

Could you suggest better way to do it?
Better means less memory usage

 

 

//==================================================================================
//FILE .h
//==================================================================================
class Pacchetto_DT
{

public:
   //Costruttore
   Pacchetto_DT(void);

   //Mittente
   void SET_Tipo_Mittente(String tipo_mittente);
   String GET_Tipo_Mittente(void);
   void SET_Mittente(String mittente);
   String GET_Mittente(void);

private:
   String _pacchetto;
   byte _indice_di_scavalcamento;
};
//==================================================================================
//FINE FILE .h
//==================================================================================

//==================================================================================
//FILE .cpp
//==================================================================================

//Costruttore.......................................................................
Pacchetto_DT::Pacchetto_DT(void)
{
_pacchetto"$#_______________________________________$"
}
//...............................................................................

//SET Tipo Mittente..................................................................
void Pacchetto_DT::SET_Tipo_Mittente(String tipo_mittente)
{
      _pacchetto = _pacchetto.substring(0,10) + tipo_mittente + _pacchetto.substring(20,10);
}//.................................................................................

//GET Tipo Mittente.................................................................
String Pacchetto_DT::GET_Tipo_Mittente(void)
{
   return _pacchetto.substring(2, 2);
}//.................................................................................

//SET Mittente......................................................................
void Pacchetto_DT::SET_Mittente(String mittente)
{
   _pacchetto = _pacchetto.substring(0, 10) + mittente + _pacchetto.substring(20, 10);
}//.................................................................................

//GET Mittente.....................................................................
String Pacchetto_DT::GET_Mittente(void)
{
   return _pacchetto.substring(4, 5);
}//.................................................................................

//==================================================================================
//FINE FILE-cpp
//==================================================================================

//==================================================================================
//PROGRAMMA PRINCIPALE
//==================================================================================
#include "Arduino.h"
Pacchetto_DT DT;
String temp;

void setup()
{

  
        Serial.begin(9600);
        Serial.println("----------------");
        Serial.println("Test Classe DT 7");
        Serial.println("----------------");
  
        //SET Mittente
        Serial.println("SET Tipo Mittente[2]= 03");
        DT.SET_Tipo_Mittente("03");

        Serial.println("SET Mittente[4]= ABCDE");
        DT.SET_Mittente("ABCDE");

        //SET Destinatario
        Serial.println("SET Tipo Destinatario[10] = 00");
        DT.SET_Tipo_Destinatario("00");
        
        Serial.println("SET Destinatario[12] = FGHIL");
        DT.SET_Destinatario("FGHIL");   
  
 

}

void loop()
{

   /* add main program code here */

}
 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sprintf() ?

 

(though to be honest I don't really understand your requirement as I don't think the specification works well in a variable width font).

 

Just to say though that:

$ cat printf.c
#include <stdio.h>

char text[] = "hello everyone\n";

int main(void) {
    sprintf(&text[6], "world\n");
    printf(text);
    return 0;
}
$ gcc printf.c -o print
$ ./print
hello world

As you can see I used sprintf() to replace "everyone" with "world"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry

 

I have forgot to say that all Serial.println(".......") are only for debug.

 

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OP said:

"Better means less memory usage"

 

Which memory are you referring to?  RAM or Flash ?

 

Why would less be better?

 

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"