Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonArray to Byte Array

QJsonArray to Byte Array

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 888 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    Qt Side of things

    void Gradient::packGradient()
    {
        // Always clear the QJsonArray befor resetting it!
        m_PaletteArray = QJsonArray();
    
        // Loop the _gradient and insert each point into the QJsonArray
        for (const auto &point : qAsConst(_gradient))
        {
            m_PaletteArray << round(point.stop * 255) << point.color.red() << point.color.green() << point.color.blue();
        }
    }
    

    This outputs

    QJsonArray([0,255,255,255,116,0,0,0,255,0,0,0])
    

    Then i send this via socket like this to the microcontroller ESP32

    socket.sendCommandStrip(QString("colorGradient"), ui->L_GradientDisplay->getGradient());
    

    ESP32 Microcontroller side of things
    Header file

        private:
            // Gradient Color
            byte m_Gradient[256] = { 0,0,0,0,255,0,0,0 };
    
        public:
            void setGradient(byte gradientIn[256]);
    

    Cpp File

    void StripSettings::setGradient(byte gradientIn[])
    {
        for (int i= 0; i < 256; i++) {
            m_Gradient[i]= gradientIn[i];
        }
    }
    

    MySocket.cpp

        if (doc["colorGradient"])
        {
            JsonArray array = doc["colorGradient"].as<JsonArray>();
            for(JsonVariant v : array) {
                Serial.println(v.as<int>()); 
            }
    
            //Strips[m_selectedStrip]->setGradient();
        }
    

    How can i put the doc["colorGradient"] array into m_Gradient[256] this array?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Loop through the JSON array and put each value at different index of your C++ array.

      (Z(:^

      K 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Loop through the JSON array and put each value at different index of your C++ array.

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

        @sierdzio so basicaly like i did in the class setter?

        void StripSettings::setGradient(byte gradientIn[])
        {
            for (int i= 0; i < 256; i++) {
                m_Gradient[i]= gradientIn[i];
            }
        }
        

        but how do i get it into the setter function?

            if (doc["colorGradient"])
            {
                JsonArray array = doc["colorGradient"].as<JsonArray>();
                for(JsonVariant v : array) {
                    Serial.println(v.as<int>()); 
                }
        
                //Strips[m_selectedStrip]->setGradient(); <--------------------- This
            }
        

        I can't do this

                JsonArray array = doc["colorGradient"].as<JsonArray>();
                for(JsonVariant v : array) {
                    Serial.println(v.as<int>());
                   Strips[m_selectedStrip]->setGradient(v.as<int>());  // <------------ this is invalid
                }
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #4

          UPDATE

          i did this to my ESP32 side but im still missing some stuff!

          Header file

                  // Gradient Color
                  byte m_Gradient[256] = { 0,0,0,0,255,0,0,0 };
                  // My Palette
                  CRGBPalette32 m_myPal = m_Gradient;
          
              public:
                  void setGradient(JsonArray array);
          

          CPP file

          void StripSettings::setGradient(JsonArray array)
          {
              int i = 0;
              memset(m_Gradient, 0, sizeof(m_Gradient));
              for(JsonVariant v : array)
              {
                  if (i >= 32) break;
                      auto x = v.as<byte>();
                      m_Gradient[i++] = x;
                      Serial.print(x);
              }
          }
          

          This gives me

          0000255000
          

          but im missing the commas! how to add them?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Kris-Revi said in QJsonArray to Byte Array:

            how to add them?

            Maybe by Serial.print(',') ?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            K 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Kris-Revi said in QJsonArray to Byte Array:

              how to add them?

              Maybe by Serial.print(',') ?

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in QJsonArray to Byte Array:

              @Kris-Revi said in QJsonArray to Byte Array:

              how to add them?

              Maybe by Serial.print(',') ?

              i dont need the comma in the Serial.print but in the actual byte array (like i've done in the header file)

              sierdzioS 1 Reply Last reply
              -1
              • K Kris Revi

                @Christian-Ehrlicher said in QJsonArray to Byte Array:

                @Kris-Revi said in QJsonArray to Byte Array:

                how to add them?

                Maybe by Serial.print(',') ?

                i dont need the comma in the Serial.print but in the actual byte array (like i've done in the header file)

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                @Kris-Revi said in QJsonArray to Byte Array:

                i dont need the comma in the Serial.print but in the actual byte array (like i've done in the header file)

                An array is a row of values, with nothing in between. In your header file, when you write byte m_Gradient[256] = { 0,0,0,0,255,0,0,0 }; this sets the array (in computer memory) to 0000255000 value (but in binary, of course).

                If you want to "pretty print it", do what @Christian-Ehrlicher recommended.

                (Z(:^

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved