blog.flxzt.netMy personal blogZola2022-12-18T00:00:00+00:00https://blog.flxzt.net/atom.xmlMotion & Gesture Recognition for low-res TOF-Sensors2022-12-18T00:00:00+00:002022-12-18T00:00:00+00:00
Unknown
https://blog.flxzt.net/posts/gesture-recognition/<h1 id="motivation">Motivation</h1>
<p>Low-cost Time-Of-Flight sensors like the "ST VL53L5CX" usually have low resolution SPAD arrays (e.g. 4x4 or 8x8) with
individual zones, each measuring a distance to an object located in a section of the FOV of the sensor.
These measurement can be processed on a MCU like a STM32 to recognize motion and gestures. For this purpose I created a
no-std, no-alloc Rust library with C-bindings to be able to integrate it into C-based embedded codebases, such as the
ones created by STM32CubeMX.<br />
This blog post explains the algorithms how this library calculates positions and recognizes motions and gestures from
the sensor values.</p>
<h1 id="coordinates">Coordinates</h1>
<p>First, let's establish how positions in relation to the sensor are represented as coordinates.</p>
<p>$$
C_{cart} =
\begin{pmatrix}
x: \text{distance to origin on x-axis}\newline
y: \text{distance to origin on y-axis}\newline
z: \text{distance to origin on z-axis}\newline
\end{pmatrix}
$$</p>
<p>$$
C_{spher} =
\begin{pmatrix}
r: \text{distance to the origin}\newline
\theta \text{ (theta): angle to x-axis (azimuth)}\newline
\phi \text{(phi): angle to y-axis (zenith)}\newline
\end{pmatrix}
$$</p>
<p><a href="https://blog.flxzt.net/posts/gesture-recognition/./coordinates.svg"><img src="https://blog.flxzt.net/posts/gesture-recognition/./coordinates.svg" alt="" /></a></p>
<p>This spherical notation is the mathematical convention.<br />
(reference: <a href="https://mathworld.wolfram.com/SphericalCoordinates.html">mathworld.wolfram.com/SphericalCoordinates.html</a>)</p>
<h2 id="conversion">Conversion</h2>
<p>Convert between the two: From spherical to cartesian coordinates
$$
\begin{align}
x &= r \cdot \cos(\theta) \cdot \sin(\phi)\newline
y &= r \cdot \sin(\theta) \cdot \sin(\phi)\newline
z &= r \cdot \cos(\phi)
\end{align}
$$</p>
<p>and from cartesian to spherial
$$
\begin{align}
\textcolor{green}{r} &= \sqrt{x^2 + y^2 + z^2}\newline
\theta &= \arctan(y / x)\newline
\phi &= \arccos( z / \textcolor{green}{r} )
\end{align}
$$</p>
<h1 id="sensor-grid">Sensor Grid</h1>
<aside style="max-width: 50%;">
<div class="aside-inner">
<a href="./sensor_grid.svg"><img src="./sensor_grid.svg" alt="" /></a>
</div>
</aside>
<p>We have for each zone:</p>
<p>$$
\begin{align}
dist_{i_x, i_y} &: \text{the measured distance}\newline
i_x &: \text{horizontal index in the sensor grid}\newline
i_y &: \text{vertical index in the sensor grid}\newline
\end{align}
$$</p>
<p>To determine the position of the measurement in each zone, we need to calculate it with the Sensor-FOV:</p>
<p>$$
\begin{align}
Res_{hor} &: \text{horizontal resolution}\newline
Res_{vert} &: \text{vertical resolution}\newline
Fov_{hor} &: \text{horizontal sensor field-of-view}\newline
Fov_{vert} &: \text{vertical sensor field-of-view}
\end{align}
$$</p>
<div class="clear"></div>
<p>We can determine the zone angle deltas:</p>
<p>$$
\begin{align}
a_{zone, hor} &= \frac{Fov_{hor}}{Res_{hor}}\newline
a_{zone, vert} &= \frac{Fov_{vert}}{Res_{vert}}\newline
\end{align}
$$
and then we can get the spherical coordinates:
$$
C_{spher, i_x, i_y} =
\begin{pmatrix}
\begin{align}
r &= dist_{i_x, i_y}\newline
\theta &= (i_x - \frac{Res_{hor}}{2}) \cdot a_{zone, hor}\newline
\phi &= \frac{\pi}{2} - (i_y - \frac{Res_{vert}}{2}) \cdot a_{zone, vert}\newline
\end{align}
\end{pmatrix}
$$</p>
<h1 id="object-position">Object Position</h1>
<p>To determine the position of an object in front of the sensor relatively accurately even with the low resolution, we use
the following method:<br />
We calculate the coordinates for all zones. Then we take the one with the smallest distance $r$ as the initial position
$C_{min}$. Because that alone is not very accurate, we need to weigh in the other measurements. We use the weighted
average mean:</p>
<p>$$
C_{obj} =
\frac{
\sum_{i_x = 0, i_y = 0}^{n_x = Res_{hor}, n_y = Res_{vert}} \omega_{i_x, i_y} \cdot C_{i_x, i_y}
}{
\sum_{i_x = 0, i_y = 0}^{n_x = Res_{hor}, n_y = Res_{vert}} \omega_{i_x, i_y}
}
$$</p>
<p>Now, how should this weight $\omega_{i_x, i_y}$ look like? Because the object is likely larger than the FOV of a single
zone, it should count in neighboring zones as well. The further away a measurement is to $ C_{min} $, the less it should
be weighted. With this criteria, we can construct a formula like this:</p>
<p>$$
\omega_{i_x, i_y}(C_{min}, C_{i_x, i_y}) = f \cdot \frac{1}{| C_{min} - C_{i_x, i_y}|}
$$</p>
<p>$f$ is a factor that determines how much the distance weighs in. This can be adjusted based on the size and distance of
the to be detected objects and is an entirely subjective value.</p>
<h1 id="motion-and-gesture-recognition">Motion and Gesture Recognition</h1>
<p>Now that we have determined the object position, we save it for every sensor readout in a vector, with the time when it
was measured. To be able to reliably recognize gestures, this vector should hold at least ca 2 seconds of position data,
so for a 15Hz sensor it should hold at least 30 elements, for a 60Hz sensor it should at least hold 120 elements. The
size of course also depends on the memory constraints of the device that runs the library.<br />
To avoid accidental recognition of far away objects, detection can be aborted if the distances are above a certain
threshold.</p>
<h1 id="swipes">Swipes</h1>
<p>To detect swipes, we look at the measurements newer than a specified time (ca 600ms) and look if the object position has
moved horizontally more than a certain distance. To make this more robust, we only recognize the gesture if the object
also has not moved much towards or away from the sensor.</p>
<p><a href="https://blog.flxzt.net/posts/gesture-recognition/./swipe_gesture.svg"><img src="https://blog.flxzt.net/posts/gesture-recognition/./swipe_gesture.svg" alt="" /></a><br />
<span class="caption">
The recognition of a swipe gesture
</span></p>
<h1 id="static-holds">Static Holds</h1>
<p>Static holds are determined when the object position has not changed above a distance threshold for a specified amount
of time.</p>
<p><a href="https://blog.flxzt.net/posts/gesture-recognition/./hold_gesture.svg"><img src="https://blog.flxzt.net/posts/gesture-recognition/./hold_gesture.svg" alt="" /></a><br />
<span class="caption">
The recognition of a hold gesture
</span></p>
<h1 id="implementation">Implementation</h1>
<p>stay tuned for another post that shows the implementation as a no-std, no-alloc Rust library with integration into an
existing STM32 C-based project. It will also show how HID keyboard reports are sent when a gesture is recognized, and
how to debug the Rust library on an STM32-F4 MCU.</p>
<p>The code (a STM32CubeIDE project) is already available <a href="https://github.com/flxzt/gestures_w_rust">here</a>.</p>
TinyUSB on STM32 MCU's with STM32CubeIDE2022-08-28T00:00:00+00:002022-08-28T00:00:00+00:00
Unknown
https://blog.flxzt.net/posts/tinyusb-w-stm32cubeide/<p>When using the USB peripheral on STM32 MCU's, you can choose between the vendor-provided USB stack and a third-party
one like <a href="https://github.com/hathach/tinyusb">TinyUSB</a>. The vendor-provided is inconvenient to use because even though
it provides a selection of predefined classes, the code generation always overwrites any changes one might need to make
to the stack to customize it or even to fix bugs in the implementation.
TinyUSB has the advantage to be hardware-independent, is easy-to-use and highly customizable. Its disadvantage is that
its integration into STM32CubeIDE and the HAL is not straight-forward. Hence this guide.</p>
<h1 id="stmcubemx-configuration">STMCubeMX configuration</h1>
<aside style="max-width: 50%;">
<div class="aside-inner">
<a href="./cubeide_config_usb_connectivity.png"><img src="./cubeide_config_usb_connectivity.png" alt="" /></a>
</div>
</aside>
<p>TinyUSB uses the abstractions of the lower-level HAL for STM32 MCU's. This is configured in CubeMX. First, the
<code>USB_OTG_FS</code> peripheral in the <code>Connectivity</code> section needs to be enabled. Set it to <code>Device Only</code> and most importantly
check the global USB interrupt in <code>NVIC Settings</code>. This generates the <code>OTG_FS_IRQHandler()</code> interrupt callback in the
<code>stm32f<x>xx_it.c</code> file, it is needed later.</p>
<p>Make sure that the <code>USB Device</code> in <code>Middleware</code> is <strong>not</strong> enabled.</p>
<p>Then the clock solver needs to be run the in <code>Clock configuration</code>. After that work is done in the CubeMX tool and the
code can be generated.</p>
<div class="clear"></div><h1 id="stm32cubeide-integration">STM32CubeIDE integration</h1>
<p>First, the TinyUSB repository needs to be cloned into the project and enabled for linking and compilation.</p>
<p>In the project root directory, execute:</p>
<pre data-lang="bash" style="background-color:#0f1419;color:#bfbab0;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#ffb454;">git</span><span> clone https://github.com/hathach/tinyusb
</span><span style="font-style:italic;color:#5c6773;"># Or when the repository itself is managed with git
</span><span style="color:#ffb454;">git</span><span> submodule add https://github.com/hathach/tinyusb
</span></code></pre>
<p>TinyUSB itself uses some library submodules in <code>lib</code>, which need to be initialized.</p>
<pre data-lang="bash" style="background-color:#0f1419;color:#bfbab0;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#f07178;">cd</span><span> tinyusb
</span><span style="color:#ffb454;">git</span><span> submodule update</span><span style="color:#f29718;"> --init --recursive</span><span> lib
</span></code></pre>
<p>It is recommended to use a released version, so the repository needs to be checked out to a release tag (<code>0.14.0</code> at the
time of writing):</p>
<pre data-lang="bash" style="background-color:#0f1419;color:#bfbab0;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#ffb454;">git</span><span> fetch</span><span style="color:#f29718;"> --all --tags
</span><span style="color:#ffb454;">git</span><span> checkout 0.14.0
</span></code></pre>
<aside style="max-width: 50%;">
<div class="aside-inner">
<a href="./cubeide_properties_tinyusb_include.png"><img src="./cubeide_properties_tinyusb_include.png" alt="" /></a>
</div>
</aside>
<p>Then the linker and compiler need to be configured. First, open the projects properties window under
<code>Project->Properties</code> and navigate to <code>C/C++ Build->Settings</code>. In the <code>MCU GCC Compiler->Include Paths</code> view you can add
the path to the TinyUSB source directory to include its header files.<br />
Make sure it is added for all configurations.</p>
<p>Then navigate to <code>C/C++ General->Paths and Symbols</code> and in <code>Source Location</code> add the TinyUSB source directory as well
for all configurations. </p>
<div class="clear"></div>
<p>At the time of writing with <code>v0.14.0</code> there are two drivers for STM32 devices. Leaving them both enabled would result in
<code>Symbol already defined</code> compilation errors. To prevent this, one of them needs to be disabled. This can be done by
excluding its file with a filter. Select the added path and click on <code>Edit Filters</code> on the right side. Then add the
<code>portable/st/synopsys/dcd_synopsys.c</code> to the filter.<br />
This disables the older driver, and the newer driver in <code>portable/synopsys/dwc2/dcd_dwc2.c</code> gets used. By
<a href="https://github.com/hathach/tinyusb/discussions/1436#discussioncomment-2592135">developer comment</a> the older one will be
removed at some point so this might not be necessary anymore in the future.</p>
<p><a href="https://blog.flxzt.net/posts/tinyusb-w-stm32cubeide/./cubeide_properties_tinyusb_source_location_w_filter.png"><img src="https://blog.flxzt.net/posts/tinyusb-w-stm32cubeide/./cubeide_properties_tinyusb_source_location_w_filter.png" alt="" /></a></p>
<h1 id="code-cdc-dual-ports-example">Code (CDC dual ports example)</h1>
<p>To be able to use TinyUSB, it needs to be integrated in the existing code.
For demonstration purposes the <a href="https://github.com/hathach/tinyusb/tree/master/examples/device/cdc_dual_ports">CDC dual ports example</a>
will get used.</p>
<h2 id="tusb-config-h">tusb_config.h</h2>
<p>TinyUSB expects the <code>tusb_config.h</code> configuration file to be present. In there the library and the peripheral is
configured. Create it in <code>Core/Inc</code>.</p>
<p>The <code>CFG_TUSB_MCU</code> and <code>CFG_TUSB_OS</code> definitions are mandatory. Valid values for both definitions can be looked up in
<code>tusb_option.h</code>.</p>
<pre data-lang="c" style="background-color:#0f1419;color:#bfbab0;" class="language-c "><code class="language-c" data-lang="c"><span style="font-style:italic;color:#5c6773;">// tusb_config.h
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// Debugging
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">CFG_TUSB_DEBUG </span><span style="color:#f29718;">3
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// Enable device stack
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">CFG_TUD_ENABLED </span><span style="color:#f29718;">1
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// Endpoint size for full-speed
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">CFG_TUD_ENDPOINT0_SIZE </span><span style="color:#f29718;">64
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// Enables 2 interfaces of class CDC
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">CFG_TUD_CDC </span><span style="color:#f29718;">2
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span></code></pre>
<h2 id="usb-descriptor-c">usb_descriptor.c</h2>
<p>Add <code>usb_descriptors.c</code> in <code>Core/Src/</code>. This file is for configuring the USB descriptors.</p>
<pre data-lang="c" style="background-color:#0f1419;color:#bfbab0;" class="language-c "><code class="language-c" data-lang="c"><span style="font-style:italic;color:#5c6773;">// usb_descriptor.c
</span><span>
</span><span style="color:#ff7733;">#include </span><span style="color:#c2d94c;">"tusb.h"
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span style="color:#ff7733;">#define </span><span style="color:#ffb454;">_PID_MAP</span><span>(</span><span style="color:#f29718;">itf</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">n</span><span>) ( (CFG_TUD_##itf) </span><span style="color:#f29668;"><< </span><span>(n) )
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">USB_PID </span><span>(</span><span style="color:#f29718;">0x4000 </span><span style="color:#f29668;">| </span><span style="color:#ffb454;">_PID_MAP</span><span>(CDC</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">0</span><span>) </span><span style="color:#f29668;">| </span><span style="color:#ffb454;">_PID_MAP</span><span>(MSC</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">1</span><span>) </span><span style="color:#f29668;">| </span><span style="color:#ffb454;">_PID_MAP</span><span>(HID</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">2</span><span>) </span><span style="color:#f29668;">|</span><span> \
</span><span> </span><span style="color:#ffb454;">_PID_MAP</span><span>(MIDI</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">3</span><span>) </span><span style="color:#f29668;">| </span><span style="color:#ffb454;">_PID_MAP</span><span>(VENDOR</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">4</span><span>) )
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">USB_VID </span><span style="color:#f29718;">0xCafe
</span><span style="color:#ff7733;">#define </span><span style="color:#59c2ff;">USB_BCD </span><span style="color:#f29718;">0x0200
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#ff7733;">const</span><span> desc_fs_configuration[] </span><span style="color:#f29668;">=
</span><span>{
</span><span> </span><span style="font-style:italic;color:#5c6773;">// Config number, interface count, string index, total length, attribute, power in mA
</span><span> </span><span style="color:#ffb454;">TUD_CONFIG_DESCRIPTOR</span><span>(</span><span style="color:#f29718;">1</span><span style="color:#bfbab0cc;">,</span><span> ITF_NUM_TOTAL</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">0</span><span style="color:#bfbab0cc;">,</span><span> CONFIG_TOTAL_LEN</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">0x00</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">100</span><span>)</span><span style="color:#bfbab0cc;">,
</span><span>
</span><span> </span><span style="font-style:italic;color:#5c6773;">// 1st CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size.
</span><span> </span><span style="color:#ffb454;">TUD_CDC_DESCRIPTOR</span><span>(ITF_NUM_CDC_0</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">4</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_0_NOTIF</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">8</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_0_OUT</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_0_IN</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">64</span><span>)</span><span style="color:#bfbab0cc;">,
</span><span>
</span><span> </span><span style="font-style:italic;color:#5c6773;">// 2nd CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size.
</span><span> </span><span style="color:#ffb454;">TUD_CDC_DESCRIPTOR</span><span>(ITF_NUM_CDC_1</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">4</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_1_NOTIF</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">8</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_1_OUT</span><span style="color:#bfbab0cc;">,</span><span> EPNUM_CDC_1_IN</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">64</span><span>)</span><span style="color:#bfbab0cc;">,
</span><span>}</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// array of pointer to string descriptors
</span><span style="color:#ff7733;">char const</span><span style="color:#f29668;">*</span><span> string_desc_arr [] </span><span style="color:#f29668;">=
</span><span>{
</span><span> (</span><span style="color:#ff7733;">const char</span><span>[]) { </span><span style="color:#f29718;">0x09</span><span style="color:#bfbab0cc;">, </span><span style="color:#f29718;">0x04 </span><span>}</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#5c6773;">// 0: is supported language is English (0x0409)
</span><span> </span><span style="color:#c2d94c;">"TinyUSB"</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#5c6773;">// 1: Manufacturer
</span><span> </span><span style="color:#c2d94c;">"Private Inc"</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#5c6773;">// 2: Product
</span><span> </span><span style="color:#c2d94c;">"123456"</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#5c6773;">// 3: Serials, should use chip ID
</span><span> </span><span style="color:#c2d94c;">"TinyUSB CDC"</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#5c6773;">// 4: CDC Interface
</span><span>}</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span></code></pre>
<h2 id="stm32fxxx-it-c">stm32fxxx_it.c</h2>
<p>As mentioned in the beginning, the code inside the global interrupt handler needs to be modified.
In there <code>tud_int_handler()</code> is called to hand the handling of the interrupt to TinyUSB. By returning early, the
"normal" interrupt handler code is prevented to be run, because it could interfere with TinyUSB.</p>
<pre data-lang="c" style="background-color:#0f1419;color:#bfbab0;" class="language-c "><code class="language-c" data-lang="c"><span style="font-style:italic;color:#5c6773;">// stm32fxxx_it.c
</span><span>
</span><span style="font-style:italic;color:#5c6773;">//...
</span><span>
</span><span style="color:#ff7733;">void </span><span style="color:#ffb454;">OTG_FS_IRQHandler</span><span>(</span><span style="color:#ff7733;">void</span><span>)
</span><span>{
</span><span> </span><span style="font-style:italic;color:#5c6773;">/* USER CODE BEGIN OTG_FS_IRQn 0 */
</span><span> </span><span style="color:#ff7733;">return </span><span style="color:#ffb454;">tud_int_handler</span><span>(BOARD_DEVICE_RHPORT_NUM)</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span> </span><span style="font-style:italic;color:#5c6773;">/* USER CODE END OTG_FS_IRQn 0 */
</span><span> </span><span style="color:#ffb454;">HAL_PCD_IRQHandler</span><span>(</span><span style="color:#f29668;">&</span><span>hpcd_USB_OTG_FS)</span><span style="color:#bfbab0cc;">;
</span><span> </span><span style="font-style:italic;color:#5c6773;">/* USER CODE BEGIN OTG_FS_IRQn 1 */
</span><span>
</span><span> </span><span style="font-style:italic;color:#5c6773;">/* USER CODE END OTG_FS_IRQn 1 */
</span><span>}
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span></code></pre>
<h2 id="main-c">main.c</h2>
<p>Then finally in <code>main.c</code> the library is initialized with <code>tusb_init()</code> after the clock and peripheral initialization
of the HAL. In the main loop <code>tud_task()</code> is repeatedly called to handle all pending events in TinyUSB's internal event
queue.</p>
<p>For the CDC example there is also some code to echo the received input from either CDC interface to both in either
lower- and uppercase.</p>
<pre data-lang="c" style="background-color:#0f1419;color:#bfbab0;" class="language-c "><code class="language-c" data-lang="c"><span style="font-style:italic;color:#5c6773;">// main.c
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span style="color:#ff7733;">#include </span><span style="color:#c2d94c;">"ctype.h"
</span><span style="color:#ff7733;">#include </span><span style="color:#c2d94c;">"tusb_config.h"
</span><span style="color:#ff7733;">#include </span><span style="color:#c2d94c;">"tusb.h"
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span style="color:#ff7733;">static void </span><span style="color:#ffb454;">echo_serial_port</span><span>(</span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#f29718;">itf</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#f29718;">buf</span><span>[]</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#39bae6;">uint32_t </span><span style="color:#f29718;">count</span><span>)</span><span style="color:#bfbab0cc;">;
</span><span style="color:#ff7733;">static void </span><span style="color:#ffb454;">cdc_task</span><span>(</span><span style="color:#ff7733;">void</span><span>)</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span style="color:#ff7733;">int </span><span style="color:#ffb454;">main</span><span>(</span><span style="color:#ff7733;">void</span><span>) {
</span><span> </span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span> </span><span style="color:#ffb454;">tusb_init</span><span>()</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span> </span><span style="color:#ff7733;">while </span><span>(</span><span style="color:#f29718;">1</span><span>) {
</span><span> </span><span style="color:#ffb454;">tud_task</span><span>()</span><span style="color:#bfbab0cc;">; </span><span style="font-style:italic;color:#5c6773;">// TinyUSB device task
</span><span>
</span><span> </span><span style="color:#ffb454;">cdc_task</span><span>()</span><span style="color:#bfbab0cc;">; </span><span style="font-style:italic;color:#5c6773;">// CDC example
</span><span> </span><span style="font-style:italic;color:#5c6773;">// ...
</span><span> }
</span><span>}
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ***
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// echo to either Serial0 or Serial1.
</span><span style="font-style:italic;color:#5c6773;">// with Serial0 as all lower case, Serial1 as all upper case.
</span><span style="color:#ff7733;">static void </span><span style="color:#ffb454;">echo_serial_port</span><span>(</span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#f29718;">itf</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#f29718;">buf</span><span>[]</span><span style="color:#bfbab0cc;">, </span><span style="font-style:italic;color:#39bae6;">uint32_t </span><span style="color:#f29718;">count</span><span>) {
</span><span> </span><span style="font-style:italic;color:#39bae6;">uint8_t </span><span style="color:#ff7733;">const</span><span> case_diff </span><span style="color:#f29668;">= </span><span style="color:#c2d94c;">'a' </span><span style="color:#f29668;">- </span><span style="color:#c2d94c;">'A'</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span> </span><span style="color:#ff7733;">for </span><span>(</span><span style="font-style:italic;color:#39bae6;">uint32_t</span><span> i </span><span style="color:#f29668;">= </span><span style="color:#f29718;">0</span><span style="color:#bfbab0cc;">;</span><span> i </span><span style="color:#f29668;"><</span><span> count</span><span style="color:#bfbab0cc;">;</span><span> i</span><span style="color:#f29668;">++</span><span>) {
</span><span> </span><span style="color:#ff7733;">if </span><span>(itf </span><span style="color:#f29668;">== </span><span style="color:#f29718;">0</span><span>) {
</span><span> </span><span style="font-style:italic;color:#5c6773;">// echo back 1st port as lower case
</span><span> </span><span style="color:#ff7733;">if </span><span>(</span><span style="color:#f07178;">isupper</span><span>(buf[i]))
</span><span> buf[i] </span><span style="color:#f29668;">+=</span><span> case_diff</span><span style="color:#bfbab0cc;">;
</span><span> } </span><span style="color:#ff7733;">else </span><span>{
</span><span> </span><span style="font-style:italic;color:#5c6773;">// echo back 2nd port as upper case
</span><span> </span><span style="color:#ff7733;">if </span><span>(</span><span style="color:#f07178;">islower</span><span>(buf[i]))
</span><span> buf[i] </span><span style="color:#f29668;">-=</span><span> case_diff</span><span style="color:#bfbab0cc;">;
</span><span> }
</span><span>
</span><span> </span><span style="color:#ffb454;">tud_cdc_n_write_char</span><span>(itf</span><span style="color:#bfbab0cc;">,</span><span> buf[i])</span><span style="color:#bfbab0cc;">;
</span><span> }
</span><span> </span><span style="color:#ffb454;">tud_cdc_n_write_flush</span><span>(itf)</span><span style="color:#bfbab0cc;">;
</span><span>}
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// USB CDC
</span><span style="color:#ff7733;">static void </span><span style="color:#ffb454;">cdc_task</span><span>(</span><span style="color:#ff7733;">void</span><span>) {
</span><span> </span><span style="font-style:italic;color:#39bae6;">uint8_t</span><span> itf</span><span style="color:#bfbab0cc;">;
</span><span>
</span><span> </span><span style="color:#ff7733;">for </span><span>(itf </span><span style="color:#f29668;">= </span><span style="color:#f29718;">0</span><span style="color:#bfbab0cc;">;</span><span> itf </span><span style="color:#f29668;"><</span><span> CFG_TUD_CDC</span><span style="color:#bfbab0cc;">;</span><span> itf</span><span style="color:#f29668;">++</span><span>) {
</span><span> </span><span style="font-style:italic;color:#5c6773;">// connected() check for DTR bit
</span><span> </span><span style="font-style:italic;color:#5c6773;">// Most but not all terminal client set this when making connection
</span><span> </span><span style="font-style:italic;color:#5c6773;">// if ( tud_cdc_n_connected(itf) )
</span><span> {
</span><span> </span><span style="color:#ff7733;">if </span><span>(</span><span style="color:#ffb454;">tud_cdc_n_available</span><span>(itf)) {
</span><span> </span><span style="font-style:italic;color:#39bae6;">uint8_t</span><span> buf[</span><span style="color:#f29718;">64</span><span>]</span><span style="color:#bfbab0cc;">;
</span><span> </span><span style="font-style:italic;color:#39bae6;">uint32_t</span><span> count </span><span style="color:#f29668;">= </span><span style="color:#ffb454;">tud_cdc_n_read</span><span>(itf</span><span style="color:#bfbab0cc;">,</span><span> buf</span><span style="color:#bfbab0cc;">, </span><span style="color:#ff7733;">sizeof</span><span>(buf))</span><span style="color:#bfbab0cc;">;
</span><span> </span><span style="font-style:italic;color:#5c6773;">// echo back to both serial ports
</span><span> </span><span style="color:#ffb454;">echo_serial_port</span><span>(</span><span style="color:#f29718;">0</span><span style="color:#bfbab0cc;">,</span><span> buf</span><span style="color:#bfbab0cc;">,</span><span> count)</span><span style="color:#bfbab0cc;">;
</span><span> </span><span style="color:#ffb454;">echo_serial_port</span><span>(</span><span style="color:#f29718;">1</span><span style="color:#bfbab0cc;">,</span><span> buf</span><span style="color:#bfbab0cc;">,</span><span> count)</span><span style="color:#bfbab0cc;">;
</span><span> }
</span><span> }
</span><span> }
</span><span>}
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span></code></pre>
<h1 id="testing">Testing</h1>
<p>Before running the code, it is advisable to enable debugging and logging at first by setting <code>CFG_TUSB_DEBUG</code> to a value
greater than zero in the configuration file. By default TinyUSB is logging with <code>printf()</code>. To be able to inspect these
entries over e.g. UART, <code>_write()</code> can be overwritten with something like:</p>
<pre data-lang="c" style="background-color:#0f1419;color:#bfbab0;" class="language-c "><code class="language-c" data-lang="c"><span style="font-style:italic;color:#5c6773;">// main.c
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span><span>
</span><span style="color:#ff7733;">int </span><span style="color:#ffb454;">_write</span><span>(</span><span style="color:#ff7733;">int </span><span style="color:#f29718;">file</span><span style="color:#bfbab0cc;">, </span><span style="color:#ff7733;">char </span><span style="color:#f29668;">*</span><span style="color:#f29718;">ptr</span><span style="color:#bfbab0cc;">, </span><span style="color:#ff7733;">int </span><span style="color:#f29718;">len</span><span>) {
</span><span> </span><span style="color:#ff7733;">return </span><span style="color:#ffb454;">HAL_UART_Transmit</span><span>(</span><span style="color:#f29668;">&</span><span>huart2</span><span style="color:#bfbab0cc;">, </span><span>(</span><span style="font-style:italic;color:#39bae6;">uint8_t</span><span style="color:#f29668;">*</span><span>) ptr</span><span style="color:#bfbab0cc;">,</span><span> len</span><span style="color:#bfbab0cc;">,</span><span> HAL_MAX_DELAY)</span><span style="color:#bfbab0cc;">;
</span><span>}
</span><span>
</span><span style="font-style:italic;color:#5c6773;">// ...
</span></code></pre>
<aside style="max-width: 70%;">
<div class="aside-inner">
<a href="./cdc_example_works.png"><img src="./cdc_example_works.png" alt="" /></a>
</div>
</aside>
<p>This is it! The code should now compile and run on the MCU. Once the USB peripheral is connected to the host,
a USB device should appear with Vendor ID <code>0xCafe</code> and two CDC devices. They should appear as virtual-COM Ports in the
"Device Manager" on Windows or by inspecting the output of <code>lsusb</code> on Linux.</p>
<p>To connect to both CDC's a serial terminal tool can be used, for example <code>tio</code>. On linux execute <code>tio /dev/ttyACM<x></code>
for both devices.<br />
Then, whenever something is typed into one of the terminals it should be echoed by both in lower- and uppercase.</p>
hello_world2022-01-02T00:00:00+00:002022-01-02T00:00:00+00:00
Unknown
https://blog.flxzt.net/posts/hello-world/<p>Hallo Welt!</p>
<pre data-lang="rust" style="background-color:#0f1419;color:#bfbab0;" class="language-rust "><code class="language-rust" data-lang="rust"><span style="color:#ff7733;">fn </span><span style="color:#ffb454;">main</span><span>() {
</span><span> </span><span style="color:#f07178;">println!</span><span>(</span><span style="color:#c2d94c;">"hello world"</span><span>)</span><span style="color:#bfbab0cc;">;
</span><span>}
</span></code></pre>
Markdown Syntax2022-01-01T00:00:00+00:002022-01-01T00:00:00+00:00
Unknown
https://blog.flxzt.net/posts/markdown-syntax/<p>Taken from <a href="https://github.com/thephpleague/commonmark/blob/2.3/tests/benchmark/sample.md">github.com/thephpleague/commonmark/blob/2.3/tests/benchmark/sample.md</a></p>
<h2 id="overview">Overview</h2>
<h3 id="philosophy">Philosophy</h3>
<p>Markdown is intended to be as easy-to-read and easy-to-write as is feasible.</p>
<p>Readability, however, is emphasized above all else. A Markdown-formatted
document should be publishable as-is, as plain text, without looking
like it's been marked up with tags or formatting instructions. While
Markdown's syntax has been influenced by several existing text-to-HTML
filters -- including [Setext] <a href="http://docutils.sourceforge.net/mirror/setext.html">1</a>, [atx] <a href="http://www.aaronsw.com/2002/atx/">2</a>, [Textile] <a href="http://textism.com/tools/textile/">3</a>, [reStructuredText] <a href="http://docutils.sourceforge.net/rst.html">4</a>,
[Grutatext] <a href="http://www.triptico.com/software/grutatxt.html">5</a>, and [EtText] <a href="http://ettext.taint.org/doc/">6</a> -- the single biggest source of
inspiration for Markdown's syntax is the format of plain text email.</p>
<p>To this end, Markdown's syntax is comprised entirely of punctuation
characters, which punctuation characters have been carefully chosen so
as to look like what they mean. E.g., asterisks around a word actually
look like *emphasis*. Markdown lists look like, well, lists. Even
blockquotes look like quoted passages of text, assuming you've ever
used email.</p>
<h3 id="html">Inline HTML</h3>
<p>Markdown's syntax is intended for one purpose: to be used as a
format for <em>writing</em> for the web.</p>
<p>Markdown is not a replacement for HTML, or even close to it. Its
syntax is very small, corresponding only to a very small subset of
HTML tags. The idea is <em>not</em> to create a syntax that makes it easier
to insert HTML tags. In my opinion, HTML tags are already easy to
insert. The idea for Markdown is to make it easy to read, write, and
edit prose. HTML is a <em>publishing</em> format; Markdown is a <em>writing</em>
format. Thus, Markdown's formatting syntax only addresses issues that
can be conveyed in plain text.</p>
<p>For any markup that is not covered by Markdown's syntax, you simply
use HTML itself. There's no need to preface it or delimit it to
indicate that you're switching from Markdown to HTML; you just use
the tags.</p>
<p>The only restrictions are that block-level HTML elements -- e.g. <code><div></code>,
<code><table></code>, <code><pre></code>, <code><p></code>, etc. -- must be separated from surrounding
content by blank lines, and the start and end tags of the block should
not be indented with tabs or spaces. Markdown is smart enough not
to add extra (unwanted) <code><p></code> tags around HTML block-level tags.</p>
<p>For example, to add an HTML table to a Markdown article:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is a regular paragraph.
</span><span>
</span><span><table>
</span><span> <tr>
</span><span> <td>Foo</td>
</span><span> </tr>
</span><span></table>
</span><span>
</span><span>This is another regular paragraph.
</span></code></pre>
<p>Note that Markdown formatting syntax is not processed within block-level
HTML tags. E.g., you can't use Markdown-style <code>*emphasis*</code> inside an
HTML block.</p>
<p>Span-level HTML tags -- e.g. <code><span></code>, <code><cite></code>, or <code><del></code> -- can be
used anywhere in a Markdown paragraph, list item, or header. If you
want, you can even use HTML tags instead of Markdown formatting; e.g. if
you'd prefer to use HTML <code><a></code> or <code><img></code> tags instead of Markdown's
link or image syntax, go right ahead.</p>
<p>Unlike block-level HTML tags, Markdown syntax <em>is</em> processed within
span-level tags.</p>
<h3 id="autoescape">Automatic Escaping for Special Characters</h3>
<p>In HTML, there are two characters that demand special treatment: <code><</code>
and <code>&</code>. Left angle brackets are used to start tags; ampersands are
used to denote HTML entities. If you want to use them as literal
characters, you must escape them as entities, e.g. <code>&lt;</code>, and
<code>&amp;</code>.</p>
<p>Ampersands in particular are bedeviling for web writers. If you want to
write about 'AT&T', you need to write '<code>AT&amp;T</code>'. You even need to
escape ampersands within URLs. Thus, if you want to link to:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>http://images.google.com/images?num=30&q=larry+bird
</span></code></pre>
<p>you need to encode the URL as:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>http://images.google.com/images?num=30&amp;q=larry+bird
</span></code></pre>
<p>in your anchor tag <code>href</code> attribute. Needless to say, this is easy to
forget, and is probably the single most common source of HTML validation
errors in otherwise well-marked-up web sites.</p>
<p>Markdown allows you to use these characters naturally, taking care of
all the necessary escaping for you. If you use an ampersand as part of
an HTML entity, it remains unchanged; otherwise it will be translated
into <code>&amp;</code>.</p>
<p>So, if you want to include a copyright symbol in your article, you can write:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>&copy;
</span></code></pre>
<p>and Markdown will leave it alone. But if you write:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>AT&T
</span></code></pre>
<p>Markdown will translate it to:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>AT&amp;T
</span></code></pre>
<p>Similarly, because Markdown supports <a href="https://blog.flxzt.net/posts/markdown-syntax/#html">inline HTML</a>, if you use
angle brackets as delimiters for HTML tags, Markdown will treat them as
such. But if you write:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>4 < 5
</span></code></pre>
<p>Markdown will translate it to:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>4 &lt; 5
</span></code></pre>
<p>However, inside Markdown code spans and blocks, angle brackets and
ampersands are <em>always</em> encoded automatically. This makes it easy to use
Markdown to write about HTML code. (As opposed to raw HTML, which is a
terrible format for writing about HTML syntax, because every single <code><</code>
and <code>&</code> in your example code needs to be escaped.)</p>
<hr />
<h2 id="block">Block Elements</h2>
<h3 id="p">Paragraphs and Line Breaks</h3>
<p>A paragraph is simply one or more consecutive lines of text, separated
by one or more blank lines. (A blank line is any line that looks like a
blank line -- a line containing nothing but spaces or tabs is considered
blank.) Normal paragraphs should not be indented with spaces or tabs.</p>
<p>The implication of the "one or more consecutive lines of text" rule is
that Markdown supports "hard-wrapped" text paragraphs. This differs
significantly from most other text-to-HTML formatters (including Movable
Type's "Convert Line Breaks" option) which translate every line break
character in a paragraph into a <code><br /></code> tag.</p>
<p>When you <em>do</em> want to insert a <code><br /></code> break tag using Markdown, you
end a line with two or more spaces, then type return.</p>
<p>Yes, this takes a tad more effort to create a <code><br /></code>, but a simplistic
"every line break is a <code><br /></code>" rule wouldn't work for Markdown.
Markdown's email-style <a href="https://blog.flxzt.net/posts/markdown-syntax/#blockquote">blockquoting</a> and multi-paragraph <a href="https://blog.flxzt.net/posts/markdown-syntax/#list">list items</a>
work best -- and look better -- when you format them with hard breaks.</p>
<h3 id="header">Headers</h3>
<p>Markdown supports two styles of headers, [Setext] <a href="http://docutils.sourceforge.net/mirror/setext.html">1</a> and [atx] <a href="http://www.aaronsw.com/2002/atx/">2</a>.</p>
<p>Setext-style headers are "underlined" using equal signs (for first-level
headers) and dashes (for second-level headers). For example:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is an H1
</span><span>=============
</span><span>
</span><span>This is an H2
</span><span>-------------
</span></code></pre>
<p>Any number of underlining <code>=</code>'s or <code>-</code>'s will work.</p>
<p>Atx-style headers use 1-6 hash characters at the start of the line,
corresponding to header levels 1-6. For example:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span># This is an H1
</span><span>
</span><span>## This is an H2
</span><span>
</span><span>###### This is an H6
</span></code></pre>
<p>Optionally, you may "close" atx-style headers. This is purely
cosmetic -- you can use this if you think it looks better. The
closing hashes don't even need to match the number of hashes
used to open the header. (The number of opening hashes
determines the header level.) :</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span># This is an H1 #
</span><span>
</span><span>## This is an H2 ##
</span><span>
</span><span>### This is an H3 ######
</span></code></pre>
<h3 id="blockquote">Blockquotes</h3>
<p>Markdown uses email-style <code>></code> characters for blockquoting. If you're
familiar with quoting passages of text in an email message, then you
know how to create a blockquote in Markdown. It looks best if you hard
wrap the text and put a <code>></code> before every line:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
</span><span>> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
</span><span>> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
</span><span>>
</span><span>> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
</span><span>> id sem consectetuer libero luctus adipiscing.
</span></code></pre>
<p>Markdown allows you to be lazy and only put the <code>></code> before the first
line of a hard-wrapped paragraph:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
</span><span>consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
</span><span>Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
</span><span>
</span><span>> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
</span><span>id sem consectetuer libero luctus adipiscing.
</span></code></pre>
<p>Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by
adding additional levels of <code>></code>:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>> This is the first level of quoting.
</span><span>>
</span><span>> > This is nested blockquote.
</span><span>>
</span><span>> Back to the first level.
</span></code></pre>
<p>Blockquotes can contain other Markdown elements, including headers, lists,
and code blocks:</p>
<blockquote>
<h2 id="this-is-a-header">This is a header.</h2>
<ol>
<li>This is the first list item.</li>
<li>This is the second list item.</li>
</ol>
<p>Here's some example code:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span> return shell_exec("echo $input | $markdown_script");
</span></code></pre>
</blockquote>
<p>Any decent text editor should make email-style quoting easy. For
example, with BBEdit, you can make a selection and choose Increase
Quote Level from the Text menu.</p>
<h3 id="list">Lists</h3>
<p>Markdown supports ordered (numbered) and unordered (bulleted) lists.</p>
<p>Unordered lists use asterisks, pluses, and hyphens -- interchangeably
-- as list markers:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* Red
</span><span>* Green
</span><span>* Blue
</span></code></pre>
<p>is equivalent to:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>+ Red
</span><span>+ Green
</span><span>+ Blue
</span></code></pre>
<p>and:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>- Red
</span><span>- Green
</span><span>- Blue
</span></code></pre>
<p>Ordered lists use numbers followed by periods:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>1. Bird
</span><span>2. McHale
</span><span>3. Parish
</span></code></pre>
<p>It's important to note that the actual numbers you use to mark the
list have no effect on the HTML output Markdown produces. The HTML
Markdown produces from the above list is:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><ol>
</span><span><li>Bird</li>
</span><span><li>McHale</li>
</span><span><li>Parish</li>
</span><span></ol>
</span></code></pre>
<p>If you instead wrote the list in Markdown like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>1. Bird
</span><span>1. McHale
</span><span>1. Parish
</span></code></pre>
<p>or even:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>3. Bird
</span><span>1. McHale
</span><span>8. Parish
</span></code></pre>
<p>you'd get the exact same HTML output. The point is, if you want to,
you can use ordinal numbers in your ordered Markdown lists, so that
the numbers in your source match the numbers in your published HTML.
But if you want to be lazy, you don't have to.</p>
<p>If you do use lazy list numbering, however, you should still start the
list with the number 1. At some point in the future, Markdown may support
starting ordered lists at an arbitrary number.</p>
<p>List markers typically start at the left margin, but may be indented by
up to three spaces. List markers must be followed by one or more spaces
or a tab.</p>
<p>To make lists look nice, you can wrap items with hanging indents:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</span><span> Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
</span><span> viverra nec, fringilla in, laoreet vitae, risus.
</span><span>* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
</span><span> Suspendisse id sem consectetuer libero luctus adipiscing.
</span></code></pre>
<p>But if you want to be lazy, you don't have to:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</span><span>Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
</span><span>viverra nec, fringilla in, laoreet vitae, risus.
</span><span>* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
</span><span>Suspendisse id sem consectetuer libero luctus adipiscing.
</span></code></pre>
<p>If list items are separated by blank lines, Markdown will wrap the
items in <code><p></code> tags in the HTML output. For example, this input:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* Bird
</span><span>* Magic
</span></code></pre>
<p>will turn into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><ul>
</span><span><li>Bird</li>
</span><span><li>Magic</li>
</span><span></ul>
</span></code></pre>
<p>But this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* Bird
</span><span>
</span><span>* Magic
</span></code></pre>
<p>will turn into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><ul>
</span><span><li><p>Bird</p></li>
</span><span><li><p>Magic</p></li>
</span><span></ul>
</span></code></pre>
<p>List items may consist of multiple paragraphs. Each subsequent
paragraph in a list item must be indented by either 4 spaces
or one tab:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>1. This is a list item with two paragraphs. Lorem ipsum dolor
</span><span> sit amet, consectetuer adipiscing elit. Aliquam hendrerit
</span><span> mi posuere lectus.
</span><span>
</span><span> Vestibulum enim wisi, viverra nec, fringilla in, laoreet
</span><span> vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
</span><span> sit amet velit.
</span><span>
</span><span>2. Suspendisse id sem consectetuer libero luctus adipiscing.
</span></code></pre>
<p>It looks nice if you indent every line of the subsequent
paragraphs, but here again, Markdown will allow you to be
lazy:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* This is a list item with two paragraphs.
</span><span>
</span><span> This is the second paragraph in the list item. You're
</span><span>only required to indent the first line. Lorem ipsum dolor
</span><span>sit amet, consectetuer adipiscing elit.
</span><span>
</span><span>* Another item in the same list.
</span></code></pre>
<p>To put a blockquote within a list item, the blockquote's <code>></code>
delimiters need to be indented:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* A list item with a blockquote:
</span><span>
</span><span> > This is a blockquote
</span><span> > inside a list item.
</span></code></pre>
<p>To put a code block within a list item, the code block needs
to be indented <em>twice</em> -- 8 spaces or two tabs:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* A list item with a code block:
</span><span>
</span><span> <code goes here>
</span></code></pre>
<p>It's worth noting that it's possible to trigger an ordered list by
accident, by writing something like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>1986. What a great season.
</span></code></pre>
<p>In other words, a <em>number-period-space</em> sequence at the beginning of a
line. To avoid this, you can backslash-escape the period:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>1986\. What a great season.
</span></code></pre>
<h3 id="precode">Code Blocks</h3>
<p>Pre-formatted code blocks are used for writing about programming or
markup source code. Rather than forming normal paragraphs, the lines
of a code block are interpreted literally. Markdown wraps a code block
in both <code><pre></code> and <code><code></code> tags.</p>
<p>To produce a code block in Markdown, simply indent every line of the
block by at least 4 spaces or 1 tab. For example, given this input:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is a normal paragraph:
</span><span>
</span><span> This is a code block.
</span></code></pre>
<p>Markdown will generate:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>This is a normal paragraph:</p>
</span><span>
</span><span><pre><code>This is a code block.
</span><span></code></pre>
</span></code></pre>
<p>One level of indentation -- 4 spaces or 1 tab -- is removed from each
line of the code block. For example, this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>Here is an example of AppleScript:
</span><span>
</span><span> tell application "Foo"
</span><span> beep
</span><span> end tell
</span></code></pre>
<p>will turn into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>Here is an example of AppleScript:</p>
</span><span>
</span><span><pre><code>tell application "Foo"
</span><span> beep
</span><span>end tell
</span><span></code></pre>
</span></code></pre>
<p>A code block continues until it reaches a line that is not indented
(or the end of the article).</p>
<p>Within a code block, ampersands (<code>&</code>) and angle brackets (<code><</code> and <code>></code>)
are automatically converted into HTML entities. This makes it very
easy to include example HTML source code using Markdown -- just paste
it and indent it, and Markdown will handle the hassle of encoding the
ampersands and angle brackets. For example, this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span> <div class="footer">
</span><span> &copy; 2004 Foo Corporation
</span><span> </div>
</span></code></pre>
<p>will turn into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><pre><code>&lt;div class="footer"&gt;
</span><span> &amp;copy; 2004 Foo Corporation
</span><span>&lt;/div&gt;
</span><span></code></pre>
</span></code></pre>
<p>Regular Markdown syntax is not processed within code blocks. E.g.,
asterisks are just literal asterisks within a code block. This means
it's also easy to use Markdown to write about Markdown's own syntax.</p>
<h3 id="hr">Horizontal Rules</h3>
<p>You can produce a horizontal rule tag (<code><hr /></code>) by placing three or
more hyphens, asterisks, or underscores on a line by themselves. If you
wish, you may use spaces between the hyphens or asterisks. Each of the
following lines will produce a horizontal rule:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>* * *
</span><span>
</span><span>***
</span><span>
</span><span>*****
</span><span>
</span><span>- - -
</span><span>
</span><span>---------------------------------------
</span></code></pre>
<hr />
<h2 id="span">Span Elements</h2>
<h3 id="link">Links</h3>
<p>Markdown supports two style of links: <em>inline</em> and <em>reference</em>.</p>
<p>In both styles, the link text is delimited by [square brackets].</p>
<p>To create an inline link, use a set of regular parentheses immediately
after the link text's closing square bracket. Inside the parentheses,
put the URL where you want the link to point, along with an <em>optional</em>
title for the link, surrounded in quotes. For example:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is [an example](http://example.com/ "Title") inline link.
</span><span>
</span><span>[This link](http://example.net/) has no title attribute.
</span></code></pre>
<p>Will produce:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>This is <a href="http://example.com/" title="Title">
</span><span>an example</a> inline link.</p>
</span><span>
</span><span><p><a href="http://example.net/">This link</a> has no
</span><span>title attribute.</p>
</span></code></pre>
<p>If you're referring to a local resource on the same server, you can
use relative paths:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>See my [About](/about/) page for details.
</span></code></pre>
<p>Reference-style links use a second set of square brackets, inside
which you place a label of your choosing to identify the link:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is [an example][id] reference-style link.
</span></code></pre>
<p>You can optionally use a space to separate the sets of brackets:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>This is [an example] [id] reference-style link.
</span></code></pre>
<p>Then, anywhere in the document, you define your link label like this,
on a line by itself:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[id]: http://example.com/ "Optional Title Here"
</span></code></pre>
<p>That is:</p>
<ul>
<li>Square brackets containing the link identifier (optionally
indented from the left margin using up to three spaces);</li>
<li>followed by a colon;</li>
<li>followed by one or more spaces (or tabs);</li>
<li>followed by the URL for the link;</li>
<li>optionally followed by a title attribute for the link, enclosed
in double or single quotes, or enclosed in parentheses.</li>
</ul>
<p>The following three link definitions are equivalent:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[foo]: http://example.com/ "Optional Title Here"
</span><span>[foo]: http://example.com/ 'Optional Title Here'
</span><span>[foo]: http://example.com/ (Optional Title Here)
</span></code></pre>
<p><strong>Note:</strong> There is a known bug in Markdown.pl 1.0.1 which prevents
single quotes from being used to delimit link titles.</p>
<p>The link URL may, optionally, be surrounded by angle brackets:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[id]: <http://example.com/> "Optional Title Here"
</span></code></pre>
<p>You can put the title attribute on the next line and use extra spaces
or tabs for padding, which tends to look better with longer URLs:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[id]: http://example.com/longish/path/to/resource/here
</span><span> "Optional Title Here"
</span></code></pre>
<p>Link definitions are only used for creating links during Markdown
processing, and are stripped from your document in the HTML output.</p>
<p>Link definition names may consist of letters, numbers, spaces, and
punctuation -- but they are <em>not</em> case sensitive. E.g. these two
links:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[link text][a]
</span><span>[link text][A]
</span></code></pre>
<p>are equivalent.</p>
<p>The <em>implicit link name</em> shortcut allows you to omit the name of the
link, in which case the link text itself is used as the name.
Just use an empty set of square brackets -- e.g., to link the word
"Google" to the google.com web site, you could simply write:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[Google][]
</span></code></pre>
<p>And then define the link:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[Google]: http://google.com/
</span></code></pre>
<p>Because link names may contain spaces, this shortcut even works for
multiple words in the link text:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>Visit [Daring Fireball][] for more information.
</span></code></pre>
<p>And then define the link:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[Daring Fireball]: http://daringfireball.net/
</span></code></pre>
<p>Link definitions can be placed anywhere in your Markdown document. I
tend to put them immediately after each paragraph in which they're
used, but if you want, you can put them all at the end of your
document, sort of like footnotes.</p>
<p>Here's an example of reference links in action:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>I get 10 times more traffic from [Google] [1] than from
</span><span>[Yahoo] [2] or [MSN] [3].
</span><span>
</span><span> [1]: http://google.com/ "Google"
</span><span> [2]: http://search.yahoo.com/ "Yahoo Search"
</span><span> [3]: http://search.msn.com/ "MSN Search"
</span></code></pre>
<p>Using the implicit link name shortcut, you could instead write:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>I get 10 times more traffic from [Google][] than from
</span><span>[Yahoo][] or [MSN][].
</span><span>
</span><span> [google]: http://google.com/ "Google"
</span><span> [yahoo]: http://search.yahoo.com/ "Yahoo Search"
</span><span> [msn]: http://search.msn.com/ "MSN Search"
</span></code></pre>
<p>Both of the above examples will produce the following HTML output:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>I get 10 times more traffic from <a href="http://google.com/"
</span><span>title="Google">Google</a> than from
</span><span><a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a>
</span><span>or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>
</span></code></pre>
<p>For comparison, here is the same paragraph written using
Markdown's inline link style:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>I get 10 times more traffic from [Google](http://google.com/ "Google")
</span><span>than from [Yahoo](http://search.yahoo.com/ "Yahoo Search") or
</span><span>[MSN](http://search.msn.com/ "MSN Search").
</span></code></pre>
<p>The point of reference-style links is not that they're easier to
write. The point is that with reference-style links, your document
source is vastly more readable. Compare the above examples: using
reference-style links, the paragraph itself is only 81 characters
long; with inline-style links, it's 176 characters; and as raw HTML,
it's 234 characters. In the raw HTML, there's more markup than there
is text.</p>
<p>With Markdown's reference-style links, a source document much more
closely resembles the final output, as rendered in a browser. By
allowing you to move the markup-related metadata out of the paragraph,
you can add links without interrupting the narrative flow of your
prose.</p>
<h3 id="em">Emphasis</h3>
<p>Markdown treats asterisks (<code>*</code>) and underscores (<code>_</code>) as indicators of
emphasis. Text wrapped with one <code>*</code> or <code>_</code> will be wrapped with an
HTML <code><em></code> tag; double <code>*</code>'s or <code>_</code>'s will be wrapped with an HTML
<code><strong></code> tag. E.g., this input:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>*single asterisks*
</span><span>
</span><span>_single underscores_
</span><span>
</span><span>**double asterisks**
</span><span>
</span><span>__double underscores__
</span></code></pre>
<p>will produce:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><em>single asterisks</em>
</span><span>
</span><span><em>single underscores</em>
</span><span>
</span><span><strong>double asterisks</strong>
</span><span>
</span><span><strong>double underscores</strong>
</span></code></pre>
<p>You can use whichever style you prefer; the lone restriction is that
the same character must be used to open and close an emphasis span.</p>
<p>Emphasis can be used in the middle of a word:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>un*frigging*believable
</span></code></pre>
<p>But if you surround an <code>*</code> or <code>_</code> with spaces, it'll be treated as a
literal asterisk or underscore.</p>
<p>To produce a literal asterisk or underscore at a position where it
would otherwise be used as an emphasis delimiter, you can backslash
escape it:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>\*this text is surrounded by literal asterisks\*
</span></code></pre>
<h3 id="code">Code</h3>
<p>To indicate a span of code, wrap it with backtick quotes (<code>`</code>).
Unlike a pre-formatted code block, a code span indicates code within a
normal paragraph. For example:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>Use the `printf()` function.
</span></code></pre>
<p>will produce:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>Use the <code>printf()</code> function.</p>
</span></code></pre>
<p>To include a literal backtick character within a code span, you can use
multiple backticks as the opening and closing delimiters:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>``There is a literal backtick (`) here.``
</span></code></pre>
<p>which will produce this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p><code>There is a literal backtick (`) here.</code></p>
</span></code></pre>
<p>The backtick delimiters surrounding a code span may include spaces --
one after the opening, one before the closing. This allows you to place
literal backtick characters at the beginning or end of a code span:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>A single backtick in a code span: `` ` ``
</span><span>
</span><span>A backtick-delimited string in a code span: `` `foo` ``
</span></code></pre>
<p>will produce:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>A single backtick in a code span: <code>`</code></p>
</span><span>
</span><span><p>A backtick-delimited string in a code span: <code>`foo`</code></p>
</span></code></pre>
<p>With a code span, ampersands and angle brackets are encoded as HTML
entities automatically, which makes it easy to include example HTML
tags. Markdown will turn this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>Please don't use any `<blink>` tags.
</span></code></pre>
<p>into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>
</span></code></pre>
<p>You can write this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>`&#8212;` is the decimal-encoded equivalent of `&mdash;`.
</span></code></pre>
<p>to produce:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><p><code>&amp;#8212;</code> is the decimal-encoded
</span><span>equivalent of <code>&amp;mdash;</code>.</p>
</span></code></pre>
<h3 id="img">Images</h3>
<p>Admittedly, it's fairly difficult to devise a "natural" syntax for
placing images into a plain text document format.</p>
<p>Markdown uses an image syntax that is intended to resemble the syntax
for links, allowing for two styles: <em>inline</em> and <em>reference</em>.</p>
<p>Inline image syntax looks like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>![Alt text](/path/to/img.jpg)
</span><span>
</span><span>![Alt text](/path/to/img.jpg "Optional title")
</span></code></pre>
<p>That is:</p>
<ul>
<li>An exclamation mark: <code>!</code>;</li>
<li>followed by a set of square brackets, containing the <code>alt</code>
attribute text for the image;</li>
<li>followed by a set of parentheses, containing the URL or path to
the image, and an optional <code>title</code> attribute enclosed in double
or single quotes.</li>
</ul>
<p>Reference-style image syntax looks like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>![Alt text][id]
</span></code></pre>
<p>Where "id" is the name of a defined image reference. Image references
are defined using syntax identical to link references:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>[id]: url/to/image "Optional title attribute"
</span></code></pre>
<p>As of this writing, Markdown has no syntax for specifying the
dimensions of an image; if this is important to you, you can simply
use regular HTML <code><img></code> tags.</p>
<hr />
<h2 id="misc">Miscellaneous</h2>
<h3 id="autolink">Automatic Links</h3>
<p>Markdown supports a shortcut style for creating "automatic" links for URLs and email addresses: simply surround the URL
or email address with angle brackets. What this means is that if you want to show the actual text of a URL or email
address, and also have it be a clickable link, you can do this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><http://example.com/>
</span></code></pre>
<p>Markdown will turn this into:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><a href="http://example.com/">http://example.com/</a>
</span></code></pre>
<p>Automatic links for email addresses work similarly, except that
Markdown will also perform a bit of randomized decimal and hex
entity-encoding to help obscure your address from address-harvesting
spambots. For example, Markdown will turn this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><address@example.com>
</span></code></pre>
<p>into something like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span><a href="&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:&#x61;&#x64;&#x64;&#x72;&#x65;
</span><span>&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;
</span><span>&#109;">&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;
</span><span>&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;</a>
</span></code></pre>
<p>which will render in a browser as a clickable link to "address@example.com".</p>
<p>(This sort of entity-encoding trick will indeed fool many, if not
most, address-harvesting bots, but it definitely won't fool all of
them. It's better than nothing, but an address published in this way
will probably eventually start receiving spam.)</p>
<h3 id="backslash">Backslash Escapes</h3>
<p>Markdown allows you to use backslash escapes to generate literal
characters which would otherwise have special meaning in Markdown's
formatting syntax. For example, if you wanted to surround a word
with literal asterisks (instead of an HTML <code><em></code> tag), you can use
backslashes before the asterisks, like this:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>\*literal asterisks\*
</span></code></pre>
<p>Markdown provides backslash escapes for the following characters:</p>
<pre style="background-color:#0f1419;color:#bfbab0;"><code><span>\ backslash
</span><span>` backtick
</span><span>* asterisk
</span><span>_ underscore
</span><span>{} curly braces
</span><span>[] square brackets
</span><span>() parentheses
</span><span># hash mark
</span><span>+ plus sign
</span><span>- minus sign (hyphen)
</span><span>. dot
</span><span>! exclamation mark
</span></code></pre>