QJsonArray to Byte Array
-
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 fileprivate: // 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 intom_Gradient[256]this array? -
Loop through the JSON array and put each value at different index of your C++ array.
-
@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 } -
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
0000255000but im missing the commas! how to add them?
-
-
@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)
-
@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)
@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) to0000255000value (but in binary, of course).If you want to "pretty print it", do what @Christian-Ehrlicher recommended.