bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.63k stars 1.1k forks source link

load an object into an absolute memory address, Cortex-M. #2101

Closed autoprog closed 3 weeks ago

autoprog commented 1 month ago

hi, Im tested example on Pi Pico. String input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; run ok.

I need to load an object into an absolute memory address,

example pseude code:

define PIN102 (((volatile unsigned long ) 0x2000000))

PIN102 = 123; PIN102 += 1024... ok

JsonDocument doc (((JsonDocument *) 0x20020008)) <---- or other address.

deserializeJson(doc, input); long time = doc[String("time")]; time += 3000; doc[String("time")] = time; bla, bla, bla

I need to work with memory outside of the main program's RAM area.

thanks, Carlos.

bblanchon commented 1 month ago

Hi Carlos,

I need to work with memory outside of the main program's RAM area.

It sounds like you want to write a custom allocator.

Best regards, Benoit

autoprog commented 1 month ago

hi bblanchon, Thank you for your quick help.

Im tested other solution, with Class, MyClass::MyClass() {..... //------------------------------------ int MyClass::get_doc( int idx) { int temp1 = doc[String("val1")]; temp1 += 8; doc[String("val1")] = temp1; return doc[String("val1")]; } //----------------------------------- String MyClass::get_str( int idx) { String sensor = doc["sensor"]; sensor += "@"; doc["sensor"] = sensor;
return doc["sensor"]; } //------------------------------- MyClass *myInstancePtr;

int ptr1=(int ) 0x20020008; pointer1 in ram int ptr2=(int ) 0x20030000; pointer2 in ram //--------------------

loop:

  myInstancePtr02 = new  (ptr2) MyClass(10);
  myInstancePtr01 = new  (ptr1) MyClass(10);

   while( true ){

  myInstancePtr01->run();
  Serial.print("GET: "); Serial.println( myInstancePtr01->get_count(), HEX); address &runCounter; in class
  Serial.print("GETdoc: "); Serial.println( myInstancePtr01->get_doc(1));
  Serial.print("GETstr: "); Serial.println( myInstancePtr01->get_str(1));
  Serial.println("");
  myInstancePtr02->run();
  Serial.print("GET2: "); Serial.println( myInstancePtr02->get_count(), HEX);
  Serial.print("GET2doc: "); Serial.println( myInstancePtr02->get_doc(1));
  Serial.print("GET2str: "); Serial.println( myInstancePtr02->get_str(1));
   Serial.println("__________________");       

    if( ++creat_doc > 10 ){

           Serial.println("Add instance \n\n\n\n");

                             if try, delete myInstancePtr02 = error

           delay(4000);
           myInstancePtr02 = new  (ptr2) MyClass(10); long loop error               
           myInstancePtr01 = new  (ptr1) MyClass(10);
           creat_doc = 0;
      }

       delay(4000);
     }

//--------------

With the class I would already be able to work, but I will try the best practice. since you want to work with several json at the same time.

Now I'm trying your solution but it's giving errors, but the problem is with the compiler, But if you have a code example I would appreciate it.

thanks, Carlos.

bblanchon commented 3 weeks ago

Hi Carlos,

The placement new won't work with ArduinoJson 7 because JsonDocument will allocate memory on the heap. However, it would work with ArduinoJson 6's StaticJsonDocument.

Still, I recommend customizing the allocator if you want control over the memory allocation.

Best regards, Benoit

autoprog commented 3 weeks ago

hi bblanchon, but it would be great to have an option to control addresses, as for those who are going to deal with many documents at the same time it would be very good. with Malloc it works well, but without control of the memory blocks, In my json test inside Class new with Malloc it works and using free() and resuming everything is perfect.

thanks, Carlos.