cancel
Showing results for 
Search instead for 
Did you mean: 

TouchGFX: I cannot get any data to appear on the display

amigafan
Associate III

I'm displaying weather data in TouchGFX. The weather data is in JSON format and comes from main.c and must be copied over to Model.cpp which decodes the weather data. To then be displayed using Screen1View.cpp. But I cannot get any data to appear on the display. It is as if Temperature and Humidity are not written to float temperature and int humidity in Model.hpp. Any help is appreciated.

Part of main.c:

 

	/* USER CODE BEGIN 2 */
	const char *json_data = "{\"temperature\": 25.0, \"humidity\": 60}";
	 processJsonData(json_data);
	/* USER CODE END 2 */

 

Model.cpp

 

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
#include "cJSON.h"

Model::Model() : modelListener(nullptr), temperature(0), humidity(0) {}

// The static method to process JSON data (called from C)
extern "C" void processJsonData(const char *json_str) {
	// Use the singleton instance of Model
	Model::getInstance().processJsonData(json_str);
}

void Model::processJsonData(const char *json_str) {
	// Parse JSON using cJSON
	cJSON *json = cJSON_Parse(json_str);
	if (json == NULL) {
		return;  // Handle error if parsing fails
	}

	// Extract temperature and humidity from the JSON object
	cJSON *temp = cJSON_GetObjectItem(json, "temperature");
	cJSON *hum = cJSON_GetObjectItem(json, "humidity");

	if (cJSON_IsNumber(temp)) {
		setTemperature(temp->valuedouble); // Update temperature
	}

	if (cJSON_IsNumber(hum)) {
		setHumidity(hum->valueint);  // Update humidity
	}

	cJSON_Delete(json);  // Free the JSON object
}

void Model::tick() {
	if (modelListener != nullptr) {
		modelListener->updateWeatherUI(getTemperature(), 0, 0, getHumidity(), 0, 0);  // Notify listener
	}
}

 

Model.hpp

 

#ifndef MODEL_HPP
#define MODEL_HPP

class ModelListener;

class Model {
public:
	Model();

	void bind(ModelListener *listener) {
		modelListener = listener;
	}

	// Singleton pattern: static method to access the single instance
	static Model& getInstance() {
		static Model instance;  // Lazily initialized instance
		return instance;
	}

	// Method to process JSON data and update internal state
	void processJsonData(const char *json_str);

	void setTemperature(float temp) {
		temperature = temp;
	}

	void setHumidity(int hum) {
		humidity = hum;
	}

	// Methods to get temperature and humidity
	float getTemperature() const {
		return temperature;
	}
	int getHumidity() const {
		return humidity;
	}

	void tick();

private:
	ModelListener *modelListener;

// Internal data
	float temperature;
	int humidity;

};

#endif // MODEL_HPP

 

Screen1Presenter.cpp

 

#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>

Screen1Presenter::Screen1Presenter(Screen1View& v)
    : view(v)
{

}

void Screen1Presenter::activate()
{

}

void Screen1Presenter::deactivate()
{

}

void Screen1Presenter::updateWeatherUI(float temperature, float min, float max, float humidity, float pressure, char *city)
{
	view.updateWeatherUI(temperature, min, max, humidity, pressure, city);
}

 

Screen1View.cpp

 

#include <gui/screen1_screen/Screen1View.hpp>

Screen1View::Screen1View()
{

}

void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
}

void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}

void Screen1View::updateWeatherUI(float temperature, float min, float max, float humidity, float pressure, char *city) {
    Unicode::snprintfFloat(tempTextBuffer, TEMPTEXT_SIZE, "%.1f", temperature);
    Unicode::snprintfFloat(minTextBuffer, MINTEXT_SIZE, "%.1f", min);
    Unicode::snprintfFloat(maxTextBuffer, MINTEXT_SIZE, "%.1f", max);
    Unicode::snprintfFloat(humidityTextBuffer, HUMIDITYTEXT_SIZE, "%.0f", humidity);
    Unicode::snprintfFloat(pressureTextBuffer, PRESSURETEXT_SIZE, "%.0f", pressure);
    Unicode::fromUTF8((const uint8_t*)city, cityTextBuffer,  CITYTEXT_SIZE);

    tempText.invalidate();
    minText.invalidate();
    maxText.invalidate();
    humidityText.invalidate();
    pressureText.invalidate();
    cityText.invalidate();
}

 

What am I doing wrong?
Sincerely, Stig Hansen

1 REPLY 1
SofLit
ST Employee

Hello @amigafan ,

Please kindly don't use too short titles for threads. "TouchGFX" as title doesn't mean anything. The tiltle needs to be short and concise.

Thank you for inderstanding.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.