Skip to content

Commit

Permalink
Site updated: 2024-05-19 12:18:59
Browse files Browse the repository at this point in the history
  • Loading branch information
levi52 committed May 19, 2024
1 parent 971b0f3 commit 4ad31a1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<meta property="og:description" content="Hi there 👋 🔭 I’m currently working on … 🌱 I’m currently learning … 👯 I’m looking to collaborate on … 🤔 I’m looking for help with … 💬 Ask me about … 📫 How to reach me: … 😄 Pronouns: … ⚡ Fun fac">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-05-15T07:41:33.000Z">
<meta property="article:modified_time" content="2024-05-17T11:38:48.535Z">
<meta property="article:modified_time" content="2024-05-18T13:19:25.897Z">
<meta property="article:author" content="Levi52">
<meta property="article:tag" content="Hardware,C,CPP,Python">
<meta name="twitter:card" content="summary">
Expand Down
2 changes: 1 addition & 1 deletion atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<link href="https://levi52.github.io/posts/c4d6bdd0/"/>
<id>https://levi52.github.io/posts/c4d6bdd0/</id>
<published>2024-05-16T03:20:34.000Z</published>
<updated>2024-05-18T12:47:51.275Z</updated>
<updated>2024-05-19T04:18:25.313Z</updated>


<summary type="html">无触摸,使用按键来控制屏幕控件</summary>
Expand Down
2 changes: 1 addition & 1 deletion css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ pre code {
vertical-align: middle;
}
.links-of-author a::before {
background: #ff9365;
background: #a30bff;
display: inline-block;
margin-right: 3px;
transform: translateY(-2px);
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ <h2 class="post-title" itemprop="name headline">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-05-18 20:47:51" itemprop="dateModified" datetime="2024-05-18T20:47:51+08:00">2024-05-18</time>
<time title="修改时间:2024-05-19 12:18:25" itemprop="dateModified" datetime="2024-05-19T12:18:25+08:00">2024-05-19</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
Expand Down
66 changes: 36 additions & 30 deletions posts/c4d6bdd0/index.html

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
<p>硬件:ESP32S3N16R8开发板 + 2.8寸TFT显示屏(ST7789) + 拨轮<br>软件:<font style="color: #36b0f3">Visual Studio Code</font> + <font style="color: #e57200">PlatformIO</font> + Squareline Studio<br>代码:<a href="https://pan.baidu.com/s/1xKbrW781ydQgRGv5lPvfVA?pwd=levi">百度网盘:ESP32S3_KEYPAD(提取码:levi)</a> </p>
</blockquote>
<h1 id="UI界面"><a href="#UI界面" class="headerlink" title="UI界面"></a>UI界面</h1><p>使用<code>Squareline Studio</code>设计如下简单界面</p>
<p><img src="/posts/c4d6bdd0/sq.png" alt="SQ工程"></p>
<p>设置工程导出路径,然后导出文件</p>
<p><img src="/posts/c4d6bdd0/setting.png" alt="工程设置"></p>
<p>导出文件结构如下</p>
<p><img src="/posts/c4d6bdd0/export.png" alt="导出"></p>
<!-- ![SQ工程](sq.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/sq.png"><br>设置工程导出路径,然后导出文件</p>
<!-- ![工程设置](setting.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/setting.png"><br>导出文件结构如下</p>
<!-- ![导出](export.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/export.png"></p>
<h1 id="PlatforIO工程"><a href="#PlatforIO工程" class="headerlink" title="PlatforIO工程"></a>PlatforIO工程</h1><h2 id="复制工程"><a href="#复制工程" class="headerlink" title="复制工程"></a>复制工程</h2><p>复制前面写好的触摸屏工程</p>
<p>删除原工程中的UI文件,将新UI复制过来</p>
<p><img src="/posts/c4d6bdd0/copy.png" alt="创建工程"></p>
<!-- ![创建工程](copy.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/copy.png"></p>
<h2 id="优化结构"><a href="#优化结构" class="headerlink" title="优化结构"></a>优化结构</h2><p><code>VS Code</code>打开工程</p>
<p>为了让<code>main.cpp</code>更简洁,这里将屏幕初始化的内容写到单独的<code>screen.cpp</code>文件中,然后在<code>main.cpp</code>中引入<code>screen.h</code>头文件</p>
<p>修改后<code>screen.h</code>文件内容如下</p>
Expand All @@ -25,8 +27,8 @@
<h2 id="输入设备"><a href="#输入设备" class="headerlink" title="输入设备"></a>输入设备</h2><p><a href="https://lvgl.100ask.net/8.2/overview/indev.html">百问网:Input devices(输入设备)</a></p>
<p>打开如下目录文件</p>
<p><img src="/posts/c4d6bdd0/port.png" alt="port indev"></p>
<p>打开<code>lv_port_indev_template.c</code>文件后,可以看到一共有五种输入方式</p>
<!-- ![port indev](port.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/port.png"><br>打开<code>lv_port_indev_template.c</code>文件后,可以看到一共有五种输入方式</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">lv_indev_t</span> * indev_touchpad;</span><br><span class="line"><span class="type">lv_indev_t</span> * indev_mouse;</span><br><span class="line"><span class="type">lv_indev_t</span> * indev_keypad;</span><br><span class="line"><span class="type">lv_indev_t</span> * indev_encoder;</span><br><span class="line"><span class="type">lv_indev_t</span> * indev_button;</span><br></pre></td></tr></table></figure>
<p>分别是触摸,鼠标,键盘,编码器,按键,具体内容请查看上方<font color="#32a929">百问网lvgl中文文档</font>链接</p>
Expand All @@ -44,10 +46,10 @@
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*------------------</span></span><br><span class="line"><span class="comment"> * Keypad</span></span><br><span class="line"><span class="comment"> * -----------------*/</span></span><br><span class="line"><span class="comment">/*Initialize your keypad*/</span></span><br><span class="line"><span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">keypad_init</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="comment">/*Your code comes here*/</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*Will be called by the library to read the mouse*/</span></span><br><span class="line"><span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">keypad_read</span><span class="params">(<span class="type">lv_indev_drv_t</span> * indev_drv, <span class="type">lv_indev_data_t</span> * data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="type">static</span> <span class="type">uint32_t</span> last_key = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">/*Get the current x and y coordinates*/</span></span><br><span class="line"> <span class="built_in">mouse_get_xy</span>(&amp;data-&gt;point.x, &amp;data-&gt;point.y);</span><br><span class="line"> <span class="comment">/*Get whether the a key is pressed and save the pressed key*/</span></span><br><span class="line"> <span class="type">uint32_t</span> act_key = <span class="built_in">keypad_get_key</span>();</span><br><span class="line"> <span class="keyword">if</span>(act_key != <span class="number">0</span>) &#123;</span><br><span class="line"> data-&gt;state = LV_INDEV_STATE_PR;</span><br><span class="line"> <span class="comment">/*Translate the keys to LVGL control characters according to your key definitions*/</span></span><br><span class="line"> <span class="keyword">switch</span>(act_key) &#123;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line"> act_key = LV_KEY_NEXT;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line"> act_key = LV_KEY_PREV;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line"> act_key = LV_KEY_LEFT;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line"> act_key = LV_KEY_RIGHT;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line"> act_key = LV_KEY_ENTER;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> last_key = act_key;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">else</span> &#123;</span><br><span class="line"> data-&gt;state = LV_INDEV_STATE_REL;</span><br><span class="line"> &#125;</span><br><span class="line"> data-&gt;key = last_key;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*Get the currently being pressed key. 0 if no key is pressed*/</span></span><br><span class="line"><span class="function"><span class="type">static</span> <span class="type">uint32_t</span> <span class="title">keypad_get_key</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="comment">/*Your code comes here*/</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在我自制的ESP32S3N16R8开发板中使用的是如下图所示的<a href="https://item.szlcsc.com/300043.html">多功能开关</a></p>
<p><img src="/posts/c4d6bdd0/key.png" alt="多功能开关"></p>
<p>原理图如下,三个按键被下拉,IO为低电平,当按键按下时IO将检测到高电平</p>
<p><img src="/posts/c4d6bdd0/keyd.png" alt="原理图"></p>
<p>在<code>screen.cpp</code>中最开始定义如下</p>
<!-- ![多功能开关](key.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/key.png"><br>原理图如下,三个按键被下拉,IO为低电平,当按键按下时IO将检测到高电平</p>
<!-- ![原理图](keyd.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/keyd.png"><br>在<code>screen.cpp</code>中最开始定义如下</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">lv_indev_drv_t</span> indev_drv;</span><br><span class="line"><span class="type">lv_indev_t</span> * indev_keypad1;</span><br><span class="line"><span class="type">lv_group_t</span> * group1;</span><br><span class="line"><span class="comment">// 按键引脚</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> btn1 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> btn2 6</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> btn3 7</span></span><br></pre></td></tr></table></figure>
<p><code>keypad_init</code>内容如下</p>
Expand Down Expand Up @@ -91,10 +93,14 @@
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Arduino.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;lvgl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;freertos/FreeRTOS.h&gt;</span></span></span><br><span class="line"><span class="comment">// SCREEN</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./screen.h&quot;</span></span></span><br><span class="line"><span class="comment">// UI</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./lvgl_gui/ui.h&quot;</span></span></span><br><span class="line"><span class="comment">// 任务</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">lvgl_task</span><span class="params">(<span class="type">void</span> *pt)</span></span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> Serial.<span class="built_in">begin</span>( <span class="number">115200</span> ); <span class="comment">// 串口初始化</span></span><br><span class="line"> <span class="built_in">lv_init</span>(); <span class="comment">// lvgl初始化</span></span><br><span class="line"> <span class="built_in">screen_init</span>(); <span class="comment">// 屏幕初始化</span></span><br><span class="line"> <span class="built_in">ui_init</span>(); <span class="comment">// UI初始化</span></span><br><span class="line"> <span class="built_in">xTaskCreatePinnedToCore</span>(lvgl_task, <span class="string">&quot;lvgl display&quot;</span>, <span class="number">1024</span> * <span class="number">15</span>, <span class="literal">NULL</span>, <span class="number">2</span>, <span class="literal">NULL</span>, <span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">lvgl_task</span><span class="params">(<span class="type">void</span> *pt)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="built_in">lv_timer_handler</span>(); <span class="comment">/* let the GUI do its work */</span></span><br><span class="line"> <span class="built_in">vTaskDelay</span>(<span class="number">5</span>);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h1><p>修改程序后,编译烧录</p>
<p><img src="/posts/c4d6bdd0/key1.png" alt="拨轮"></p>
<p><img src="/posts/c4d6bdd0/screen.png" alt="显示"></p>
<p><img src="/posts/c4d6bdd0/switch.gif" alt="切换"></p>
<p><img src="/posts/c4d6bdd0/enter.gif" alt="选中"></p>
<!-- ![拨轮](key1.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/key1.png"></p>
<!-- ![显示](screen.png) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/screen.png"></p>
<!-- ![切换](switch.gif) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/switch.gif"></p>
<!-- ![选中](enter.gif) -->
<p><img src="https://gitee.com/levi-05/pic1/raw/master/esp32s3_keypad/enter.gif"></p>
<h1 id="END"><a href="#END" class="headerlink" title="END"></a>END</h1><center> 2024 <font color="#00bcd9">Levi5</font></center>]]></content>
<categories>
<category>ESP32</category>
Expand Down

0 comments on commit 4ad31a1

Please sign in to comment.