|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en-US"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 6 | + <title>Await | EloquentArduino</title> |
| 7 | + <meta name="description" content="A collection of eloquent APIs for Arduino"> |
| 8 | + |
| 9 | + |
| 10 | + <link rel="preload" href="/assets/css/0.styles.3e1d0bd1.css" as="style"><link rel="preload" href="/assets/js/app.a75b5dd7.js" as="script"><link rel="preload" href="/assets/js/3.7c513c20.js" as="script"><link rel="prefetch" href="/assets/js/2.d8f505d4.js"><link rel="prefetch" href="/assets/js/4.facae2f5.js"><link rel="prefetch" href="/assets/js/5.19001f11.js"><link rel="prefetch" href="/assets/js/6.993e1634.js"><link rel="prefetch" href="/assets/js/7.d71211a0.js"> |
| 11 | + <link rel="stylesheet" href="/assets/css/0.styles.3e1d0bd1.css"> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">EloquentArduino</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><!----> <!----> </div> <div class="page"> <div class="content"><h1 id="await"><a href="#await" aria-hidden="true" class="header-anchor">#</a> Await</h1> <p>Sometimes you may need to wait for a certain condition to become true, but you don't want |
| 15 | +to wait forever: it may be awaiting for Serial, for the Wifi to connect to a network, or |
| 16 | +the response from a SoftwareSerial peripheral.</p> <p>Most often, you see example code of this kind:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code>Serial<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Attempting to connect to open SSID: "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
| 17 | +Serial<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ssid<span class="token punctuation">)</span><span class="token punctuation">;</span> |
| 18 | + |
| 19 | +<span class="token keyword">while</span> <span class="token punctuation">(</span>WiFi<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> WL_CONNECTED<span class="token punctuation">)</span> <span class="token punctuation">{</span> |
| 20 | + Serial<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
| 21 | + <span class="token function">delay</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
| 22 | +<span class="token punctuation">}</span> |
| 23 | +</code></pre></div><p>If the connection doesn't succeed, you're stuck in the loop. A proper way for handling |
| 24 | +such situations is with a timeout that gets you out of the loop with an error status |
| 25 | +so you can handle the failure.</p> <p><code>Await</code> is exactly this: a straightforward construct that eloquently does what it is named |
| 26 | +after: waits for a condition to become true until a timeout expires, returning true or false |
| 27 | +as a response.</p> <h3 id="import"><a href="#import" aria-hidden="true" class="header-anchor">#</a> Import</h3> <p><code>#include "EloquentTimeUtils.h"</code></p> <h3 id="how-to-use"><a href="#how-to-use" aria-hidden="true" class="header-anchor">#</a> How to use</h3> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">bool</span> wifiConnected <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span>WiFi<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> WL_CONNECTED<span class="token punctuation">,</span> <span class="token number">10000</span><span class="token punctuation">)</span> |
| 28 | + |
| 29 | +<span class="token comment">// or with the Eloquent utils:</span> |
| 30 | +<span class="token keyword">bool</span> wifiConnected <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span>WiFi<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> WL_CONNECTED<span class="token punctuation">,</span> <span class="token number">10</span> Seconds<span class="token punctuation">)</span> |
| 31 | +</code></pre></div><p>It needs two arguments:</p> <ol><li>the condition to wait for</li> <li>the timeout, in milliseconds</li></ol> <p>It means that it will await 10 seconds for the wifi to connect: on failure, <code>wifiConnected</code> |
| 32 | +will be false and you can gently fail.</p> <p>You can use it for any kind of check, like waiting for <code>Serial</code></p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">bool</span> serialAvailable <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span>Serial<span class="token punctuation">,</span> <span class="token number">5</span> Seconds<span class="token punctuation">)</span> |
| 33 | +<span class="token keyword">bool</span> serialHasCharacters <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span>Serial<span class="token punctuation">.</span><span class="token function">available</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">5</span> Seconds<span class="token punctuation">)</span> |
| 34 | +</code></pre></div><p>The default interval between checks is 10 milliseconds: if you need a custom delay interval |
| 35 | +you can use the more verbose <code>await_with_interval</code>:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token comment">// await WiFi for 10 seconds, check if connected every 500 millis</span> |
| 36 | +<span class="token keyword">bool</span> wifiConnected <span class="token operator">=</span> <span class="token function">await_with_interval</span><span class="token punctuation">(</span>WiFi<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> WL_CONNECTED<span class="token punctuation">,</span> <span class="token number">10</span> Seconds<span class="token punctuation">,</span> <span class="token number">500</span> Millis<span class="token punctuation">)</span> |
| 37 | +</code></pre></div></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div> |
| 38 | + <script src="/assets/js/app.a75b5dd7.js" defer></script><script src="/assets/js/3.7c513c20.js" defer></script> |
| 39 | + </body> |
| 40 | +</html> |
0 commit comments