add digitalWrite functions
This commit is contained in:
parent
5e27c5f359
commit
b284e2ebab
8 changed files with 295 additions and 19 deletions
|
@ -241,6 +241,79 @@
|
|||
<span id="241">241</span>
|
||||
<span id="242">242</span>
|
||||
<span id="243">243</span>
|
||||
<span id="244">244</span>
|
||||
<span id="245">245</span>
|
||||
<span id="246">246</span>
|
||||
<span id="247">247</span>
|
||||
<span id="248">248</span>
|
||||
<span id="249">249</span>
|
||||
<span id="250">250</span>
|
||||
<span id="251">251</span>
|
||||
<span id="252">252</span>
|
||||
<span id="253">253</span>
|
||||
<span id="254">254</span>
|
||||
<span id="255">255</span>
|
||||
<span id="256">256</span>
|
||||
<span id="257">257</span>
|
||||
<span id="258">258</span>
|
||||
<span id="259">259</span>
|
||||
<span id="260">260</span>
|
||||
<span id="261">261</span>
|
||||
<span id="262">262</span>
|
||||
<span id="263">263</span>
|
||||
<span id="264">264</span>
|
||||
<span id="265">265</span>
|
||||
<span id="266">266</span>
|
||||
<span id="267">267</span>
|
||||
<span id="268">268</span>
|
||||
<span id="269">269</span>
|
||||
<span id="270">270</span>
|
||||
<span id="271">271</span>
|
||||
<span id="272">272</span>
|
||||
<span id="273">273</span>
|
||||
<span id="274">274</span>
|
||||
<span id="275">275</span>
|
||||
<span id="276">276</span>
|
||||
<span id="277">277</span>
|
||||
<span id="278">278</span>
|
||||
<span id="279">279</span>
|
||||
<span id="280">280</span>
|
||||
<span id="281">281</span>
|
||||
<span id="282">282</span>
|
||||
<span id="283">283</span>
|
||||
<span id="284">284</span>
|
||||
<span id="285">285</span>
|
||||
<span id="286">286</span>
|
||||
<span id="287">287</span>
|
||||
<span id="288">288</span>
|
||||
<span id="289">289</span>
|
||||
<span id="290">290</span>
|
||||
<span id="291">291</span>
|
||||
<span id="292">292</span>
|
||||
<span id="293">293</span>
|
||||
<span id="294">294</span>
|
||||
<span id="295">295</span>
|
||||
<span id="296">296</span>
|
||||
<span id="297">297</span>
|
||||
<span id="298">298</span>
|
||||
<span id="299">299</span>
|
||||
<span id="300">300</span>
|
||||
<span id="301">301</span>
|
||||
<span id="302">302</span>
|
||||
<span id="303">303</span>
|
||||
<span id="304">304</span>
|
||||
<span id="305">305</span>
|
||||
<span id="306">306</span>
|
||||
<span id="307">307</span>
|
||||
<span id="308">308</span>
|
||||
<span id="309">309</span>
|
||||
<span id="310">310</span>
|
||||
<span id="311">311</span>
|
||||
<span id="312">312</span>
|
||||
<span id="313">313</span>
|
||||
<span id="314">314</span>
|
||||
<span id="315">315</span>
|
||||
<span id="316">316</span>
|
||||
</pre><pre class="rust"><code><span class="doccomment">//! This module contains all the structures and functions related to
|
||||
//! interacting with the B15 on a high level. If you are writing code
|
||||
//! for the B15, this is the module you want to use.
|
||||
|
@ -282,6 +355,14 @@
|
|||
/// program; calling `B15F::new()` more than once might lead to unexpected
|
||||
/// behaviour.
|
||||
///
|
||||
/// # Returns
|
||||
/// A new B15F object is returned. It contains an already active USART connection,
|
||||
/// so calling this function multiple times will create an Error
|
||||
///
|
||||
/// # Errors
|
||||
/// An `error::Error` is generated if the connection to the board cannot be
|
||||
/// established, or if testing of that connection fails.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use b15f::B15F;
|
||||
|
@ -305,6 +386,11 @@
|
|||
<span class="prelude-val">Err</span>(<span class="kw">_</span>) => {} <span class="comment">// Do nothing
|
||||
</span>};
|
||||
|
||||
<span class="kw">match </span>drv.test_int_conv() {
|
||||
<span class="prelude-val">Ok</span>(()) => <span class="kw">break</span>,
|
||||
<span class="prelude-val">Err</span>(<span class="kw">_</span>) => {}
|
||||
}
|
||||
|
||||
tries -= <span class="number">1</span>;
|
||||
}
|
||||
|
||||
|
@ -346,11 +432,46 @@
|
|||
<span class="prelude-val">Ok</span>(port)
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Sets the value of the specified port
|
||||
///
|
||||
/// # Errors
|
||||
/// `port` can either be 0 or 1, other values will cause a compile-time
|
||||
/// error. Otherwise an `error::Error` is generated if communication
|
||||
/// with the B15 fails.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
</span><span class="kw">pub fn </span>digital_write<<span class="kw">const </span>port: u8> (<span class="kw-2">&mut </span><span class="self">self</span>, value: u8) -> <span class="prelude-ty">Result</span><(), Error> {
|
||||
<span class="macro">assert!</span>(port == <span class="number">0 </span>|| port == <span class="number">1</span>);
|
||||
|
||||
<span class="kw">let </span>reversed = value.reverse_bits();
|
||||
<span class="kw">let </span>request = <span class="kw">if </span>port == <span class="number">0 </span>{ Request::DigitalWrite0 } <span class="kw">else </span>{ Request::DigitalWrite1 };
|
||||
|
||||
<span class="self">self</span>.usart.write(<span class="macro">build_request!</span>[request, reversed])<span class="question-mark">?</span>;
|
||||
|
||||
<span class="kw">let </span><span class="kw-2">mut </span>aw: [u8; <span class="number">1</span>] = [<span class="number">0</span>; <span class="number">1</span>];
|
||||
<span class="self">self</span>.usart.read(<span class="kw-2">&mut </span>aw)<span class="question-mark">?</span>;
|
||||
|
||||
<span class="kw">if </span>aw[<span class="number">0</span>] != B15F::MSG_OK {
|
||||
<span class="kw">return </span><span class="prelude-val">Err</span>(<span class="macro">format!</span>(<span class="string">"Setting Port {} failed"</span>, port).into());
|
||||
}
|
||||
|
||||
<span class="prelude-val">Ok</span>(())
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Yields information about the installed firmware on the B15
|
||||
///
|
||||
/// Returns an array of strings, where each string contains a piece
|
||||
/// of information stored on the B15
|
||||
///
|
||||
/// # Returns
|
||||
/// A list of strings where each string contains a piece of information
|
||||
/// about the board. What string contains what information is determined,
|
||||
/// but not explicitly listed.
|
||||
///
|
||||
/// # Errors
|
||||
/// An `error::Error` is generated if the communication with the board fails.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use b15f::B15F;
|
||||
|
@ -412,13 +533,38 @@
|
|||
<span class="prelude-val">Ok</span>(())
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Test the integer conversion of the USART connection
|
||||
///
|
||||
/// # Errors
|
||||
/// If an error occurs in the conversion or the communication with the
|
||||
/// board, an `error::Error` will be returned.
|
||||
</span><span class="kw">pub fn </span>test_int_conv(<span class="kw-2">&mut </span><span class="self">self</span>) -> <span class="prelude-ty">Result</span><(), Error> {
|
||||
<span class="kw">let </span>dummy: u16 = rand::thread_rng().gen_range(<span class="number">0x0000</span>..=(<span class="number">0xFFFF </span>/ <span class="number">3</span>));
|
||||
|
||||
<span class="self">self</span>.usart.write(<span class="macro">build_request!</span>(Request::IntTest, dummy & <span class="number">0xFF</span>, dummy >> <span class="number">8</span>))<span class="question-mark">?</span>;
|
||||
|
||||
<span class="kw">let </span><span class="kw-2">mut </span>aw: [u8; <span class="number">2</span>] = [<span class="number">0</span>; <span class="number">2</span>];
|
||||
<span class="self">self</span>.usart.read(<span class="kw-2">&mut </span>aw)<span class="question-mark">?</span>;
|
||||
|
||||
<span class="kw">let </span>result = u16::from_le_bytes(aw);
|
||||
<span class="kw">if </span>result != dummy * <span class="number">3 </span>{
|
||||
<span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Int conversion failed"</span>.into());
|
||||
}
|
||||
|
||||
<span class="prelude-val">Ok</span>(())
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Tests the connetion to the B15
|
||||
///
|
||||
/// To test the connection a `Request::Test` request will be sent
|
||||
/// to the board together with a randomly generated value. If the
|
||||
/// board returns that value the connection is working correctly.
|
||||
///
|
||||
/// ## Examples
|
||||
/// # Errors
|
||||
/// An `error::Error` is returned if the test fails, or if the
|
||||
/// communication itself fails.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use b15f::B15F;
|
||||
///
|
||||
|
|
|
@ -21,6 +21,10 @@
|
|||
<span id="21">21</span>
|
||||
<span id="22">22</span>
|
||||
<span id="23">23</span>
|
||||
<span id="24">24</span>
|
||||
<span id="25">25</span>
|
||||
<span id="26">26</span>
|
||||
<span id="27">27</span>
|
||||
</pre><pre class="rust"><code><span class="doccomment">//! This module contains the request data used to communicate
|
||||
//! with the B15 via USART.
|
||||
//!
|
||||
|
@ -40,8 +44,12 @@
|
|||
|
||||
<span class="attribute">#[repr(u8)]
|
||||
</span><span class="kw">pub enum </span>Request {
|
||||
Discard = <span class="number">0</span>,
|
||||
Test = <span class="number">1</span>,
|
||||
Info = <span class="number">2
|
||||
</span>}</code></pre></div>
|
||||
Discard = <span class="number">0</span>,
|
||||
Test = <span class="number">1</span>,
|
||||
Info = <span class="number">2</span>,
|
||||
IntTest = <span class="number">3</span>,
|
||||
|
||||
DigitalWrite0 = <span class="number">5</span>,
|
||||
DigitalWrite1 = <span class="number">6</span>,
|
||||
}</code></pre></div>
|
||||
</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="b15f" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12)" ></div></body></html>
|
Loading…
Add table
Add a link
Reference in a new issue