【STM32H7S78-DK】 六 可设置时钟及代码分析
<div class='showpostmsg'> 本帖最后由 damiaa 于 2024-11-3 22:17 编辑<div><span style="font-size:22px;"><strong> 【STM32H7S78-DK】 六 可设置时钟及代码分析</strong></span></div>
<div> </div>
<div><a href="https://bbs.eeworld.com.cn/thread-1294069-1-1.html"><strong>【STM32H7S78-DK】 一 开箱贴</strong></a></div>
<div><a href="https://bbs.eeworld.com.cn/thread-1294071-1-1.html"><strong>【STM32H7S78-DK】 二 touchgxf环境搭建和基本测试</strong></a></div>
<div><a href="https://bbs.eeworld.com.cn/thread-1294464-1-1.html"><strong>【STM32H7S78-DK】 三 touchgxf和stm32cubeide和led按键测试</strong></a></div>
<div><a href="https://bbs.eeworld.com.cn/thread-1298127-1-1.html"><strong>【STM32H7S78-DK】 四 下载失败问题及解决、计数器实现及分析</strong></a></div>
<div><a href="https://bbs.eeworld.com.cn/thread-1298132-1-1.html"><strong>【STM32H7S78-DK】 五 </strong><strong>乱序键盘</strong><strong>实现及分析</strong></a> <strong>之后</strong></div>
<div> </div>
<p><strong>一、打开</strong><strong>touchgfx</strong><strong> designer ,加上提示文本</strong><strong>数字时钟显示文本和模拟显示界面</strong><strong>,</strong><strong>这里直接使用例子里面的界面导过来。</strong><br />
<strong>加上时钟的时分秒设置的加减键,时钟设置的开始键,OK</strong><strong>键其实</strong><strong>没什么用,这里页懒得删除了。如下图所示:</strong><br />
</p>
<p> </p>
<p><strong>二、给每个</strong><strong>按钮设置Interactions为按下后调用虚函数</strong><strong>。每个虚函数名字不一样 如下。</strong><br />
</p>
<p> </p>
<p><strong>三、打开代码</strong><strong>Main</strong><strong>View.hpp中加入虚函数和</strong><strong>一些时钟变量和时钟设置标记</strong><strong>如下:</strong><br />
</p>
<p> </p>
<p><strong>四、Main</strong><strong>View.cpp中加入虚函数和更新按键函数的具体实现如下:</strong></p>
<div><strong>每秒更新实现</strong></div>
<div></div>
<div><strong>时分秒设置按键实现</strong></div>
<div></div>
<div></div>
<div><strong>允许设置和OK键 这里其实OK键没什么用懒得删除了,允许设置在</strong><strong>没选中</strong></div>
<div><strong>也可以设置时间,只是时间还是走,设置允许就时钟不走,可以专注设置。</strong></div>
<div></div>
<div> </div>
<div><strong>五、主要代码:</strong></div>
<div><strong>MainView.hpp:</strong></div>
<div>
<pre>
<code class="language-cpp">#ifndef MAIN_VIEW_HPP
#define MAIN_VIEW_HPP
#include <gui_generated/main_screen/MainViewBase.hpp>
#include <gui/main_screen/MainPresenter.hpp>
class MainView : public MainViewBase
{
public:
MainView();
virtual ~MainView() {}
virtual void setupScreen();
virtual void tearDownScreen();
virtual void handleTickEvent();
virtual void bt1_function();
virtual void bt2_function();
virtual void bt3_function();
virtual void bt4_function();
virtual void bt5_function();
virtual void bt6_function();
virtual void toggle_function();
virtual void OK_function();
protected:
int tickCounter;
int analogHours;
int analogMinutes;
int analogSeconds;
int digitalHours;
int digitalMinutes;
int digitalSeconds;
bool setflag;
};
#endif // MAIN_VIEW_HPP
</code></pre>
<p><strong>MainView.cpp:</strong></p>
</div>
<div>
<pre>
<code class="language-cpp">#include <gui/main_screen/MainView.hpp>
MainView::MainView() :
tickCounter(0)
{
}
void MainView::setupScreen()
{
MainViewBase::setupScreen();
digitalHours = digitalClock.getCurrentHour();
digitalMinutes = digitalClock.getCurrentMinute();
digitalSeconds = digitalClock.getCurrentSecond();
analogHours = analogClock.getCurrentHour();
analogMinutes = analogClock.getCurrentMinute();
analogSeconds = analogClock.getCurrentSecond();
}
void MainView::tearDownScreen()
{
MainViewBase::tearDownScreen();
}
void MainView::handleTickEvent()
{
if(true != setflag )
{
tickCounter++;
if (tickCounter % 60 == 0)
{
if (++digitalSeconds >= 60)
{
digitalSeconds = 0;
if (++digitalMinutes >= 60)
{
digitalMinutes = 0;
if (++digitalHours >= 24)
{
digitalHours = 0;
}
}
}
if (++analogSeconds >= 60)
{
analogSeconds = 0;
if (++analogMinutes >= 60)
{
analogMinutes = 0;
if (++analogHours >= 24)
{
analogHours = 0;
}
}
}
// Update the clocks
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
}
}
void MainView::bt1_function()
{
if(digitalHours >=23)
digitalHours =0;
else
digitalHours++;
analogHours =digitalHours;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::bt2_function()
{
if(digitalHours ==0)
digitalHours =23;
else
digitalHours--;
analogHours =digitalHours;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::bt3_function()
{
if(digitalMinutes >=59)
digitalMinutes =0;
else
digitalMinutes++;
analogMinutes =digitalMinutes;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::bt4_function()
{
if(digitalMinutes == 0)
digitalMinutes=59;
else
digitalMinutes--;
analogMinutes =digitalMinutes;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::bt5_function()
{
if(digitalSeconds >=59)
digitalSeconds=0;
else
digitalSeconds++;
analogSeconds =digitalSeconds;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::bt6_function()
{
if(0 ==digitalSeconds)
digitalSeconds=59;
else
digitalSeconds--;
analogSeconds =digitalSeconds;
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
}
void MainView::toggle_function()
{
// Override and implement this function in Main
//setflag
setflag =toggleButton1.getState();
}
void MainView::OK_function()
{
digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
toggleButton1.forceState(false);
digitalClock.invalidate();
toggleButton1.invalidate();
}</code></pre>
<p> </p>
</div>
<div><strong>六、编译运行,效果如下:</strong></div>
<div><strong></strong></div>
<div>a0fa491ad928c0f1bc4f3705f108be83<br />
</div>
<div><strong>好,先分析到这里。</strong></div>
<div><strong> 谢谢</strong></div>
<p><!--importdoc--></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]