C++ 如何使用TouchGFX在LCD屏幕上显示ADC值?

C++ 如何使用TouchGFX在LCD屏幕上显示ADC值?,c++,stm32f7,C++,Stm32f7,我想在stm32f746G-DISCO板上的lcd屏幕上显示adc值。主要挑战来自集成TouchGFX软件。这将生成输出小部件和通配符所需的模型、视图和演示者文件。我相信我已经正确设置了adc引脚PF10,因为我能够使用以下方法获得main.cpp中的adc值: while (1) { HAL_ADC_Start(&hadc3); HAL_ADC_PollForConversion(&hadc3, 500); hullVoltage =

我想在stm32f746G-DISCO板上的lcd屏幕上显示adc值。主要挑战来自集成TouchGFX软件。这将生成输出小部件和通配符所需的模型、视图和演示者文件。我相信我已经正确设置了adc引脚PF10,因为我能够使用以下方法获得main.cpp中的adc值:

while (1)
   {
      HAL_ADC_Start(&hadc3);
      HAL_ADC_PollForConversion(&hadc3, 500);
      hullVoltage = HAL_ADC_GetValue(&hadc3)*0.00080586;
      HAL_Delay(1000);
   }
但我的主要目标是在我的lcd屏幕上显示这个。我有touchGFX设置,可以在View.cpp中向通配符输入浮点。例如:

void Monitor_ScreenView::handleTickEvent()
{
     Unicode::snprintfFloat(textArea2Buffer, 4, "%f", 3.14f);
     textArea2.invalidate();
}
我将向您展示我的模型、视图和演示者cpp文件,以演示我的问题所在

型号.cpp

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Model::getHullVoltage()
{
    HAL_ADC_Start(&hadc3);
    HAL_ADC_PollForConversion(&hadc3, 500);
    hullVoltage = HAL_ADC_GetValue(&hadc3);
}
#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/model/Model.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Monitor_ScreenView::handleTickEvent()
{
     Unicode::snprintfFloat(textArea2Buffer, 4, "%f", presenter->getHullVoltage());
     textArea2.invalidate();
}

#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/monitor_screen_screen/Monitor_ScreenPresenter.hpp>

// my functions
float Monitor_ScreenPresenter::getHullVoltage()
{
    return(model->hullVoltage);
}
#包括
#包括
#ifndef模拟器
#包括“stm32746g_discovery.h”
#恩迪夫
void模型::getHullVoltage()
{
HAL_ADC_启动(&hadc3);
HAL_ADC_PollForConversion(&hadc3500);
hullVoltage=HAL\u ADC\u GetValue(&hadc3);
}
查看.cpp

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Model::getHullVoltage()
{
    HAL_ADC_Start(&hadc3);
    HAL_ADC_PollForConversion(&hadc3, 500);
    hullVoltage = HAL_ADC_GetValue(&hadc3);
}
#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/model/Model.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Monitor_ScreenView::handleTickEvent()
{
     Unicode::snprintfFloat(textArea2Buffer, 4, "%f", presenter->getHullVoltage());
     textArea2.invalidate();
}

#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/monitor_screen_screen/Monitor_ScreenPresenter.hpp>

// my functions
float Monitor_ScreenPresenter::getHullVoltage()
{
    return(model->hullVoltage);
}
#包括
#包括
#ifndef模拟器
#包括“stm32746g_discovery.h”
#恩迪夫
无效监视器屏幕视图::handleTickEvent()
{
Unicode::snprintfloat(textArea2Buffer,4,“%f”,presenter->getHullVoltage());
textArea2.invalidate();
}
演示者.cpp

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Model::getHullVoltage()
{
    HAL_ADC_Start(&hadc3);
    HAL_ADC_PollForConversion(&hadc3, 500);
    hullVoltage = HAL_ADC_GetValue(&hadc3);
}
#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/model/Model.hpp>

#ifndef SIMULATOR
#include "stm32746g_discovery.h"
#endif

void Monitor_ScreenView::handleTickEvent()
{
     Unicode::snprintfFloat(textArea2Buffer, 4, "%f", presenter->getHullVoltage());
     textArea2.invalidate();
}

#include <gui/monitor_screen_screen/Monitor_ScreenView.hpp>
#include <gui/monitor_screen_screen/Monitor_ScreenPresenter.hpp>

// my functions
float Monitor_ScreenPresenter::getHullVoltage()
{
    return(model->hullVoltage);
}

#包括
#包括
//我的职能
浮点监视器\u屏幕演示器::getHullVoltage()
{
返回(型号->hullVoltage);
}
我从这个构建中得到的唯一错误是model.cpp中的“hadc3未在此范围内声明”


如果我能从我的代码中获得任何见解,我将非常感激。除此之外,我的代码工作正常,因为我可以通过触摸屏上的按钮打开和关闭LED,我可以在屏幕上想要的地方打印一个浮点,我可以在main.cpp中获得adc值。我只需要它在屏幕上显示每一个滴答声

我相信我已经找到了问题的答案。在View.cpp中,我将hadc3声明为外部类型:

void Monitor_ScreenView::handleTickEvent()
{
    extern ADC_HandleTypeDef hadc3;
    float hullVoltage;


     HAL_ADC_Start(&hadc3);
     HAL_ADC_PollForConversion(&hadc3, 500);
     hullVoltage = HAL_ADC_GetValue(&hadc3)*0.00080586;

     Unicode::snprintfFloat(textArea2Buffer, 4, "%f", hullVoltage);
     textArea2.invalidate();
}

我认为不需要任何其他文件!欢呼声接近,堆栈溢出

在TouchGFX应用程序的视图类中编写的代码应该是可移植的,使其能够在模拟器或目标上运行。例如,如果要通过Windows上的TouchGFX设计器使用
gcc
运行应用程序,则
HAL\u ADC
的符号将无法解析。用调用views presenter然后调用模型来替换代码将提高可移植性,并更好地封装功能,而不是将其保存在事件处理程序中


将特定于目标的代码保留在模型内,并使用ifdef模拟器对其进行保护,将允许您通过键盘事件、勾号事件或硬件的外部事件触发模拟器和目标版本的代码。

如果您使用HAL库,您可以使用
#ifndef模拟器
block

在声明了
hadc3
的地方发布代码。如果你找不到这样的代码——这就是问题所在。你能详细说明一下这是如何解决特定错误的,“hadc3未在此范围内声明”,以及OP的代码需要做哪些具体更改吗?我明白你的意思,谢谢。从那以后,我对“答案”的格式有了更多的了解。当时,由于声誉不佳,我无法在@Marks post上分享我的一般观察结果作为评论。我很好奇,如果图书馆及其
#include
在某个地方定义了该实例,为什么它们不提供此声明。因此,我不确定这是否是一个完整的解决方案。从视图代码调用特定于目标的函数(例如,
HAL.*
函数)被认为是“不好”的做法,因为这会降低代码的可移植性。我会调用
presenter->doStuff()
,然后调用后端(
Model
)。或者至少使用
#ifndef模拟器
进行排序,以便gui可以通过设计器或环境在SDL上运行。