Adam Heinrich2023-05-27T21:56:59+02:00https://www.adamh.czAdam Heinrichadam@adamh.czParametric stand for synthesizers in OpenSCAD2020-11-28T00:00:00+01:00https://www.adamh.cz/blog/2020/11/parametric-stand-for-synthesizers-in-openscad<p>When looking for a suitable stand for my beloved <a href="https://www.vectorsynth.com">Vector synthesizer</a>
on Thingiverse and other repositories, none of them suited my needs.
Most importantly I was not impressed by the traditional construction of stands
made of two pieces connected by threaded rods – the stand had to be able to
fit the limited space of my drawer when not being used.</p>
<p>Therefore I designed yet another parametric stand which consists of two
identical parts. Because the parts are wide enough, they can stand upright on
they own and do not require any supporting hardware in between.
The stand also has rubber feet both on the bottom of the stand and on the
hypotenuse to increase friction.</p>
<p><a href="/public/img/parametric-stand-for-synthesizers-in-openscad/vector.jpg">
<img src="/public/img/parametric-stand-for-synthesizers-in-openscad/vector_thumb.jpg" class="half" /></a>
<a href="/public/img/parametric-stand-for-synthesizers-in-openscad/stand.jpg">
<img src="/public/img/parametric-stand-for-synthesizers-in-openscad/stand_thumb.jpg" class="half" /></a></p>
<!--more-->
<p>Although the stand is designed to be printed on a 3D printer (for this
particular print I used the awesome transparent red PETG by <a href="https://www.filament-pm.cz">Plasty Mladeč</a>
with 3 perimeters for increased strength and equalized number of top and bottom
layers for consistent transparency), it is also possible to generate a 2D
cross-section which can be used for manufacturing by other means.</p>
<p>The GPL-licensed OpenSCAD source code and detailed instructions can be
found on <a href="https://github.com/adamheinrich/gear_stand">GitHub</a>. The model is parametric so it is possible to change the
dimensions, angle or the geometry of the rubber feet used.</p>
<p><img src="/public/img/parametric-stand-for-synthesizers-in-openscad/drawing.png" alt="Drawing with main parameters" /></p>
C cheatsheet2018-02-25T00:00:00+01:00https://www.adamh.cz/blog/2018/02/c-cheatsheet<p>This is a short collection of notes on the C language which are important
especially when dealing with hardware.</p>
<!--more-->
<h2 id="data-types">Data types</h2>
<h3 id="type-sizes">Type sizes</h3>
<p>The exact size of integer data types is implementation-specific. Only minimum
<em>ranges</em> are defined (§5.2.4.2.1):</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Signed range</th>
<th>Unsigned range</th>
<th>Size*</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="language-plaintext highlighter-rouge">char</code></td>
<td>±127</td>
<td>0 to 255</td>
<td>8 bits</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">short</code></td>
<td>±32767</td>
<td>0 to 65535</td>
<td>16 bits</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">long</code></td>
<td>±2147483647</td>
<td>0 to (2<sup>32</sup>-1)</td>
<td>32 bits</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">long long</code></td>
<td>±(2<sup>63</sup>-1)</td>
<td>0 to (2<sup>64</sup>-1)</td>
<td>64 bits</td>
</tr>
</tbody>
</table>
<p>Important notes:</p>
<ul>
<li>Whether a <code class="language-plaintext highlighter-rouge">char</code> is treated as <code class="language-plaintext highlighter-rouge">signed char</code> or <code class="language-plaintext highlighter-rouge">unsigned char</code> is
up to the implementation (see “Implementation-defined behavior”).</li>
<li>The <strong>size in bits</strong> is not defined by the standard (ditto).</li>
<li>There is no implicit assumption that negative numbers are represented by
<strong>two’s complement</strong> (which is the most common case). Consequences:
<ul>
<li>Range is <code class="language-plaintext highlighter-rouge">±(2^(N-1) - 1)</code> rather than <code class="language-plaintext highlighter-rouge">-2^(N-1)</code> to <code class="language-plaintext highlighter-rouge">2^(N-1) - 1</code>.</li>
<li>Undefined behavior for signed overflow.</li>
</ul>
</li>
</ul>
<p>The <code class="language-plaintext highlighter-rouge">int</code> type usually represents the <em>natural</em> processor word. However, this
is not the case for 8-bit architectures or for some 64-bit systems.</p>
<p>To write portable code, use types defined in <code class="language-plaintext highlighter-rouge"><stdint.h></code> (e.g. <code class="language-plaintext highlighter-rouge">uint32_t</code>)
where size matters.</p>
<h3 id="the-sizeof-operator">The sizeof operator</h3>
<p>The <code class="language-plaintext highlighter-rouge">sizeof</code> operator (not a function) gives object size in “bytes” (§6.5.3.4).
However, because the standard mandates that <code class="language-plaintext highlighter-rouge">sizeof(char)</code> equals to 1, the
term “byte” is not understood as an octet (8 bits) – simply because a <code class="language-plaintext highlighter-rouge">char</code>
can be represented by more than 8 bits. To get size in bits, multiply the result
of the <code class="language-plaintext highlighter-rouge">sizeof</code> operator by <code class="language-plaintext highlighter-rouge">CHAR_BITS</code> (defined in <code class="language-plaintext highlighter-rouge"><limits.h></code>).</p>
<p>Its result is an unsigned integer of type <code class="language-plaintext highlighter-rouge">size_t</code> (defined in <code class="language-plaintext highlighter-rouge"><stddef.h></code>).
Therefore, the <code class="language-plaintext highlighter-rouge">size_t</code> type is guaranteed to hold size of any possible object
including arrays. This makes it useful for portable array indexing.</p>
<h3 id="integer-promotion">Integer promotion</h3>
<p>The following code prints <code class="language-plaintext highlighter-rouge">c != 0xff</code> given that it is compiled on a platform
where <code class="language-plaintext highlighter-rouge">char</code> is treated as signed:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>char c = 0xff;
if (c == 0xff)
printf("c == 0xff\n");
else
printf("c != 0xff\n");
</code></pre></div></div>
<p>This is the effect of <em>integer promotion</em> (§6.3.1.1):</p>
<blockquote>
<p>If an <code class="language-plaintext highlighter-rouge">int</code> can represent all values of the original type, the value is
converted to an <code class="language-plaintext highlighter-rouge">int</code>; otherwise, it is converted to an <code class="language-plaintext highlighter-rouge">unsigned int</code>.
These are called the <em>integer promotions</em>. All other types are unchanged by
the integer promotions.</p>
</blockquote>
<p>The value <code class="language-plaintext highlighter-rouge">0xff</code> (255 in decimal) assigned to <code class="language-plaintext highlighter-rouge">c</code> is outside the range ±127
defined for <code class="language-plaintext highlighter-rouge">signed char</code>; given that the machine uses two’s complement
representation, the value of <code class="language-plaintext highlighter-rouge">c</code> is interpreted as -1.</p>
<p>Because all values of <code class="language-plaintext highlighter-rouge">signed char</code> are representable in <code class="language-plaintext highlighter-rouge">int</code>, both operands
of the comparison <code class="language-plaintext highlighter-rouge">(c == 0xff)</code> are promoted to <code class="language-plaintext highlighter-rouge">int</code>. While the <code class="language-plaintext highlighter-rouge">0xff</code> literal
is represented by <code class="language-plaintext highlighter-rouge">0x000000ff</code> on a machine with 32-bit <code class="language-plaintext highlighter-rouge">int</code>, the negative
value of <code class="language-plaintext highlighter-rouge">c</code> will be represented as <code class="language-plaintext highlighter-rouge">0xffffffff</code> after the promotion.
And obviously, <code class="language-plaintext highlighter-rouge">0x000000ff</code> and <code class="language-plaintext highlighter-rouge">0xffffffff</code> do not match.</p>
<p>Possible fixes to print <code class="language-plaintext highlighter-rouge">c == 0xff</code>:</p>
<ul>
<li>Declare <code class="language-plaintext highlighter-rouge">c</code> as <code class="language-plaintext highlighter-rouge">unsigned char</code> or <code class="language-plaintext highlighter-rouge">uint8_t</code>. In this case, both operands are
interpreted as 255 and <code class="language-plaintext highlighter-rouge">0x000000ff</code> will be compared to <code class="language-plaintext highlighter-rouge">0x000000ff</code>.</li>
<li>Cast <code class="language-plaintext highlighter-rouge">0xff</code> to char in the comparison: <code class="language-plaintext highlighter-rouge">(chat)0xff</code>. In this case, both
operands are interpreted as -1 and <code class="language-plaintext highlighter-rouge">0xffffffff</code> is be compared to
<code class="language-plaintext highlighter-rouge">0xffffffff</code>.</li>
</ul>
<h3 id="structures">Structures</h3>
<p>General rules:</p>
<ol>
<li>The address of the structure is the address of its first member, i.e. the
first member’s offset is always 0.</li>
<li>Ordering of members is preserved in memory, i.e. a member’s offset is greater
than offset of previously declared members.</li>
<li>The compiler might add <em>padding</em> between two consecutive members or at the
end of the structure.</li>
</ol>
<h4 id="designated-initializers">Designated initializers</h4>
<p>It is often desirable to only set a subset of structure members and zero the
others (e.g. for structures with automatic storage which are allocated on
stack or for compatibility with future versions of APIs).</p>
<p>A common approach is to use <code class="language-plaintext highlighter-rouge">memset()</code> from <code class="language-plaintext highlighter-rouge"><string.h></code> to clear the structure
before assigning individual members:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>struct foo {
int f1;
int f2;
} s;
memset(&s, 0, sizeof(s));
s.f1 = 1;
</code></pre></div></div>
<p>However, a better option is to use a <em>designated initializer</em>. In this case,
members not assigned by the initializer will be automatically initialized to
zero:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>struct foo s = { .f1 = 1 };
</code></pre></div></div>
<p>Or, when assigning a new value:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>s = (struct foo) { .f1 = 1 };
</code></pre></div></div>
<h2 id="behavior">Behavior</h2>
<p>The following lists present the most important examples or undefined behavior,
unspecified behavior and implementation-defined behavior. For a complete list,
refer to annex J of the <a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf">C standard</a>.</p>
<h3 id="undefined-behavior">Undefined behavior</h3>
<ul>
<li>Using uninitialized variables with automatic storage which
<a href="https://stackoverflow.com/a/11965368">never had its address taken</a> (§6.3.2.1). If the address has been
taken, the value is “just” indeterminate</li>
<li>Using object outside its lifetime (§6.2.4)</li>
<li>Signed integer overflow</li>
<li>Buffer overflow (accessing array elements outside bounds)</li>
<li>Dereferencing <code class="language-plaintext highlighter-rouge">NULL</code> pointer (§6.3.2)</li>
<li>Modification of string literals (§6.4.5) and <code class="language-plaintext highlighter-rouge">const</code> objects (§6.7.3)</li>
<li>Left shifting past bit-width (e.g. <code class="language-plaintext highlighter-rouge">1UL << 32</code> for 32-bit int)</li>
</ul>
<p>Moreover, <strong>shifting value into or past the sign bit</strong> is also undefined
behavior. More precisely, it happens the resulting value is not
<a href="https://stackoverflow.com/questions/3784996/why-does-left-shift-operation-invoke-undefined-behaviour-when-the-left-side-oper">representable in result type</a> (because signed integer overflow is
undefined). This is the case when shifting signed positive value by 31 bits on
an architecture with 32-bit int (and two’s complement representation of signed
integers):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int foo = (1 << 31); /* Undefined behavior */
</code></pre></div></div>
<p>To avoid undefined behavior when left-shifting, use unsigned literals and do not
exceed result type size:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>uint32_t bar = (1UL << 31);
</code></pre></div></div>
<h3 id="implementation-defined-behavior">Implementation-defined behavior</h3>
<ul>
<li>The number of bits in <code class="language-plaintext highlighter-rouge">char</code>, defined in <code class="language-plaintext highlighter-rouge">CHAR_BITS</code> (§3.6)</li>
<li>Whether <code class="language-plaintext highlighter-rouge">char</code> is treated as <code class="language-plaintext highlighter-rouge">signed char</code> or <code class="language-plaintext highlighter-rouge">unsigned char</code> (§6.2.5)</li>
<li>Expansion of the <code class="language-plaintext highlighter-rouge">NULL</code> macro (§3.6) – it doesn’t have to be <code class="language-plaintext highlighter-rouge">((void*)0)</code>.
However:
<ul>
<li>Using <code class="language-plaintext highlighter-rouge">if (!ptr)</code> to check for a null pointer is correct because an
expression with value 0 cast to <code class="language-plaintext highlighter-rouge">void *</code> is a <em>null pointer constant</em>
(§6.3.2.3)</li>
<li>It is safe to assume that <code class="language-plaintext highlighter-rouge">static char *str</code> will be initialized
to a null pointer (§6.7.8)</li>
</ul>
</li>
<li>Representation of signed integer types (§6.2.6.2)
<ul>
<li>Can be either sign and magnitude, one’s complement or two’s complement</li>
<li>However, <code class="language-plaintext highlighter-rouge">intN_t</code> types from <code class="language-plaintext highlighter-rouge"><stdint.h></code> have to represent integers with
two’s complement representation (§7.18.11.1).</li>
</ul>
</li>
<li>Right-shifting negative values (§6.5.7)</li>
<li>Endianness</li>
</ul>
<h3 id="unspecified-behavior">Unspecified behavior</h3>
<ul>
<li>Evaluation order of operands except for <code class="language-plaintext highlighter-rouge">&&</code>, <code class="language-plaintext highlighter-rouge">||</code>, <code class="language-plaintext highlighter-rouge">?:</code> and <code class="language-plaintext highlighter-rouge">,</code> (§6.5)
<ul>
<li>The exception is handy for constructions like
<code class="language-plaintext highlighter-rouge">if (str != NULL && *str != '\0')</code>.</li>
</ul>
</li>
<li>Evaluation order of function arguments (§6.5.2.2)</li>
</ul>
<h2 id="function-prototypes">Function prototypes</h2>
<p>Prototype <code class="language-plaintext highlighter-rouge">void foo(void)</code> declares a function which takes no arguments, whereas
prototype <code class="language-plaintext highlighter-rouge">void foo()</code> declares a function accepting any number of arguments.</p>
<p>Prototype for the main function can be either <code class="language-plaintext highlighter-rouge">int main(int argc, char *argv[])</code>
or <code class="language-plaintext highlighter-rouge">int main(void)</code> (§5.1.2.2.1). Moreover, it is not necessary to explicitly
return a value from <code class="language-plaintext highlighter-rouge">main</code> (§5.1.2.2.3):</p>
<blockquote>
<p>If the return type of the <code class="language-plaintext highlighter-rouge">main</code> function is a type compatible with <code class="language-plaintext highlighter-rouge">in</code>t, a
return from the initial call to the <code class="language-plaintext highlighter-rouge">main</code> function is equivalent to calling
the <code class="language-plaintext highlighter-rouge">exit</code> function with the value returned by the <code class="language-plaintext highlighter-rouge">main</code> function as its
argument; reaching the <code class="language-plaintext highlighter-rouge">}</code> that terminates the <code class="language-plaintext highlighter-rouge">main</code> function returns a value
of 0.</p>
</blockquote>
<p>Therefore, the following construction is perfectly legal:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int main(void)
{
printf("Hi there\n");
}
</code></pre></div></div>
<h2 id="useful-gcc-flags">Useful GCC flags</h2>
<p>Apart from <code class="language-plaintext highlighter-rouge">-Wall -pedantic</code>:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">-Wextra</code></li>
<li><code class="language-plaintext highlighter-rouge">-Wconversion</code></li>
<li><code class="language-plaintext highlighter-rouge">-Wcast-align</code></li>
<li><code class="language-plaintext highlighter-rouge">-Wdouble-promotion</code></li>
<li><code class="language-plaintext highlighter-rouge">-Wfloat-conversion</code></li>
<li><a href="https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-ftrapv"><code class="language-plaintext highlighter-rouge">-ftrapv</code></a> traps signed integer overflow by calling <code class="language-plaintext highlighter-rouge">abort()</code></li>
</ul>
<h2 id="references">References</h2>
<p>All references in the text refer to the <a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf">N1256 draft</a> of the C99 standard
(ISO/IEC 9899:1999).</p>
<p>Useful links:</p>
<ul>
<li><a href="https://gist.github.com/lisovy/b2e8633a53915d7e95c6">gist.github.com/lisovy/b2e8633a53915d7e95c6</a></li>
<li><a href="http://www.catb.org/esr/structure-packing/">The Lost Art of Structure Packing</a></li>
<li><a href="https://spin.atomicobject.com/2014/05/19/c-undefined-behaviors/">Undefined Behaviors in C that You’ve Probably Run into without Knowing It</a></li>
<li><a href="http://david.tribble.com/text/cdiffs.htm">Incompatibilities Between ISO C and ISO C++</a></li>
<li><a href="http://www.pvv.org/~oma/DeepC_slides_oct2011.pdf">Deep C (and C++)</a></li>
</ul>
Context Switch on the ARM Cortex-M02016-07-28T00:00:00+02:00https://www.adamh.cz/blog/2016/07/context-switch-on-the-arm-cortex-m0<p>I have written a simple round-robin scheduler (<a href="https://github.com/adamheinrich/os.h/tree/blog_2016_07">available on GitHub</a>) for the
ARM Cortex-M0 (ARMv6-M) CPU to understand the context switch mechanism. This
article is a short summary of its principle.</p>
<p>The same approach is used by many RTOSes and is well described in
<a href="http://store.elsevier.com/The-Definitive-Guide-to-ARM%C2%AE-Cortex%C2%AE-M0-and-Cortex-M0+-Processors/Joseph-Yiu/isbn-9780128032770/">The Definitive Guide to ARM® Cortex®-M0 and Cortex-M0+ Processors</a> by
Joseph Yiu.</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_intro.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_intro.png" alt="Round-robin scheduler" /></a></p>
<!--more-->
<h2 id="background">Background</h2>
<p>The processor has two separate stack pointer which can be accessed through banked
<code class="language-plaintext highlighter-rouge">SP</code> register: <strong>Main Stack Pointer</strong> (MSP) which is the default one after
startup and <strong>Process Stack Pointer</strong> (PSP) which can be optionally used.</p>
<p>The processor supports multiple modes:</p>
<ul>
<li>The default mode is the <strong>Privileged Thread Mode</strong>. It is possible to switch
stack to PSP in this mode</li>
<li>The Privileged mode can be switched to <strong>Unprivileged Thread Mode</strong> which has
certain register and memory access restrictions</li>
<li>Exceptions and interrupts are handled in the <strong>Handler Mode</strong> which uses the
MSP stack.</li>
</ul>
<p>In this application, tasks run in the Unprivileged Thread Mode with PSP and
kernel runs in the Handler Mode with MSP. This allows stack separation
between the kernel and tasks (which simplifies the context switch procedure) and
prevents tasks from accessing important registers and affecting the kernel.</p>
<h2 id="context-switch">Context Switch</h2>
<p>The context switch happens in an interrupt handler. Once an interrupt occurs,
the NVIC hardware automatically stacks an <strong>exception frame</strong> (registers
<code class="language-plaintext highlighter-rouge">xPSR</code>, <code class="language-plaintext highlighter-rouge">PC</code>, <code class="language-plaintext highlighter-rouge">LR</code>, <code class="language-plaintext highlighter-rouge">r12</code> and <code class="language-plaintext highlighter-rouge">r3-r0</code>) onto the Process Stack (PSP)
and branches to the interrupt handler routine in Handler Mode (which uses the
Main Stack).</p>
<p>The context switch routine has to:</p>
<ul>
<li>Manually stack remaining registers <code class="language-plaintext highlighter-rouge">r4-r11</code> on the Process Stack</li>
<li>Save current task’s PSP to memory</li>
<li>Load next task’s stack pointer and assign it to PSP</li>
<li>Manually unstack registers <code class="language-plaintext highlighter-rouge">r4-r11</code></li>
<li>Call <code class="language-plaintext highlighter-rouge">bx 0xfffffffD</code> which makes the processor switch to Unprivileged
Handler Mode, unstack next task’s exception frame and continue on its <code class="language-plaintext highlighter-rouge">PC</code>.</li>
</ul>
<p>Exception frame saved by the NVIC hardware onto stack:</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_stack_nvic.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_stack_nvic.png" alt="Exception frame saved by NVIC" /></a></p>
<p>Registers saved by the software:</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_stack_sw.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_stack_sw.png" alt="Registers saved by SW" /></a></p>
<h2 id="performing-the-context-switch">Performing the Context Switch</h2>
<p>The context switch could be performed by the <code class="language-plaintext highlighter-rouge">SysTick_Handler</code> with a SysTick
timer configured to fire interrupts periodically:</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_onyly.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_onyly.png" alt="Context switch performed by SysTick" /></a></p>
<p>This approach would however not work with other interrupts (peripheral interrupt
for example). The <code class="language-plaintext highlighter-rouge">SysTick_Handler</code> would stack registers affected by the
peripheral IRQ handler and unstack task’s registers, resulting in undefined
behavior of both tasks and peripheral interrupt handler:</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_irq_problem.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_irq_problem.png" alt="Context switch performed by SysTick - problem with IRQ" /></a></p>
<p>The solution is simple - the <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L104">SysTick_Handler</a> with the highest priority
only selects the next task to be run and triggers PendSV interrupt.
The <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os_pendsv_handler.s">PendSV_Handler</a> with the lowest priority performs the actual
context switch once all interrupt requests with higher priority have been
handled:</p>
<p><a href="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_pendsv.png"><img src="/public/img/context-switch-on-the-arm-cortex-m0/os_systick_pendsv.png" alt="Context switch scheduled by SysTick and performed by PendSV" /></a></p>
<p>The <code class="language-plaintext highlighter-rouge">PendSV_Handler</code> is written in pure assembly. The code relies on a fact
that the task’s stack pointer is the first element of the <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L8">os_task_t</a>
structure - the structure’s address corresponds to the address of its first
element according to C language specification.</p>
<h2 id="task-initialization">Task Initialization</h2>
<p>Each task is defined by its handler function and stack. The initialization phase
of task’s stack must ensure that the first 64 bytes (16 words) form a valid
exception frame. It is neccessary to store at least the default value of three
registers:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">xPSR</code> to <code class="language-plaintext highlighter-rouge">0x01000000</code> (the defaul value)</li>
<li><code class="language-plaintext highlighter-rouge">PC</code> to the handler function</li>
<li><code class="language-plaintext highlighter-rouge">LR</code> to a <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L27">function</a> to be called when the handler function finishes
(otherwise the CPU would jump to invalid location, causing HardFault or
undefined behavior)</li>
</ul>
<p>The actual function <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L40">os_task_init</a> stores values for registers <code class="language-plaintext highlighter-rouge">r0-r12</code> as
well for debugging purposes.</p>
<h2 id="startup">Startup</h2>
<p>The startup phase has to configure SysTick and PendSV interrupt levels,
initialize the SysTick timer to fire interrupts periodically and start the
first task.</p>
<p>As the microcontroller starts in Privileged Thread Mode with Main Stack it is
neccessary to switch to Unprivileged mode with Process Stack. This is done by
writing to the <code class="language-plaintext highlighter-rouge">CONTROL</code> register followed by <code class="language-plaintext highlighter-rouge">ISB</code> instruction.</p>
<p>The <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L82">os_start</a> function is written without inline assembly thanks to
functions and intrinsics provided by the CMSIS library.</p>
<h2 id="example">Example</h2>
<p>An <a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/examples/stm32f030x8/main.c">example</a> runs three tasks (which are switched every second). All tasks
blink the onboard LED with different frequency.</p>
<p>The example can be run on STM32F030R8 Nucleo board and requires <a href="http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software.html?querycriteria=productId=LN1897">STM32Cube</a>
software pack which has to be present in the <a href="https://github.com/adamheinrich/os.h/tree/blog_2016_07/examples/lib">lib</a> directory.</p>
<p>The provided Makefile requires GCC compiler and OpenOCD. See
<a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/examples/stm32f030x8">README</a> for more information about compilation and flashing.</p>
<h2 id="compatibility">Compatibility</h2>
<p>The SysTick timer and Privileged mode are optional features of the ARMv6-M
architecture. They are however supported by vast majority of microcontrollers.</p>
<p>The code relies on standard <a href="http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php">CMSIS library</a> by ARM which is usually
distributed by microcontroller vendors. The library provides functions and
intrinsics for accessing features of the ARM Cortex-M core.</p>
Acrylic Case for the Intel Edison Breakout Board2016-05-31T00:00:00+02:00https://www.adamh.cz/blog/2016/05/acrylic-case-for-the-intel-edison-breakout-board<p>Last year I’ve bought the Intel Edison Breakout Board for some experiments.
I was affraid to carry it around in my bag so I have decided to design
a simple laser-cut acrylic case.</p>
<p><a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/edison_case.jpg">
<img alt="Edison Case - Photo" src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/edison_case_thumb.jpg" />
</a></p>
<!--more-->
<p>The case is made of five parts cut from 3 mm thick acrylic. Blue are inner
spacers (one of them does have a hole for both micro USB connectors), black is
the top plate (with holes for jumpers and reset button) and red is the bottom
plate:</p>
<p><img alt="Edison Case - Drawing" src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/parts_color.png" /></p>
<p>You can choose from two options for the bottom plate - with or without a cutout
exposing breakout board’s GPIO pins.</p>
<p>Recommended material:</p>
<ul>
<li>3 mm thick acrylic</li>
<li>4× 6 mm M3 nylon screws (top)</li>
<li>4× 8 mm M3 nylon screws (bottom)</li>
<li>4× 6 mm M3 nylon standoffs (between top plate and PCB)</li>
<li>4× M3 nylon washers (between bottom plate and PCB)</li>
<li>(optional) 2× M1.6 standoffs with shorter screw (see below)</li>
<li>(optional) 2× M1.6 nuts to replace the original ones</li>
</ul>
<p>The original M1.6 standoff’s screw is too long so it does not fit the case.
As you can see on photos, I’ve just cut the original screws and left the module
without any nuts.</p>
<p>It’s generally a good idea to buy more nylon screws - mine crack easily.
The case does also look god with metal inbus (hex) screws.</p>
<p><strong>You can download the drawing <a href="/public/etc/acrylic-case-for-the-intel-edison-breakout-board/edison_case.zip">here</a></strong> (Inkscape SVG and PDF).
It is licensed under <a href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</p>
<p>Many thanks to <a href="mailto:michalneoral@email.cz">Michal Neoral</a> for beautiful photos!</p>
<p><a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/front_black.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/front_black_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/back_black.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/back_black_thumb.jpg" class="half" /><a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/side_black.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/side_black_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/both_black.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/both_black_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/top_white.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/top_white_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/bottom_white.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/bottom_white_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/standing_black.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/standing_black_thumb.jpg" class="half" /></a>
<a href="/public/img/acrylic-case-for-the-intel-edison-breakout-board/standing_white.jpg">
<img src="/public/img/acrylic-case-for-the-intel-edison-breakout-board/standing_white_thumb.jpg" class="half" /></a></a></a></p>
Converting Excel (Calc) Spreadsheet to Code2013-10-20T00:00:00+02:00https://www.adamh.cz/blog/2013/10/converting-excel-calc-spreadsheet-to-code<p>A few months ago I needed to convert quite complex (kind of black box) computation from Excel spreadsheet to C code. At first I manually copied expressions from all cells but this was not simple as I had to check cells mentioned in expressions (formulas). It took long time and energy and the code didn’t work properly - results calculated by the spreadsheet differed from my program’s result.</p>
<p>Therefore I decided to save the Excel spreadsheet in ODF format (OpenOffice.org Calc) and write a little helper called <strong>SpreadsheetToCode</strong>. It’s a command-line utility that does the following steps:</p>
<ol>
<li><strong>Load OpenDocument Spreadsheet file</strong> (.ods) because it's basically XML file wrapped in ZIP and therefore super easy to handle</li>
<li><strong>Pick all cells</strong> which contain numerical values (inputs, parameters) or expressions with them (formulas)</li>
<li><strong>Print C-like source code</strong> with the spreadsheet's functionality expressed in lines of code</li>
</ol>
<!--more-->
<p>As this is just helper and not fully equipped Excel-to-EXE converter, so that your task is to rename variables to something meaningful (instead of G42), maybe merge some lines of code or remove redundant steps and incorporate the resulting code in your own program. Converter’s greatest help is that it checks for dependencies which means that code</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="kt">double</span> <span class="n">C2</span> <span class="o">=</span> <span class="n">C10</span><span class="o">/</span><span class="mi">3</span><span class="p">;</span></code></pre></figure>
<p>will be placed after</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="kt">double</span> <span class="n">C10</span> <span class="o">=</span> <span class="n">C5</span><span class="o">+</span><span class="n">C1</span><span class="p">;</span></code></pre></figure>
<p>because C10 mustn’t be used before its declaration/definition.</p>
<h2>Usage</h2>
<p>To illustrate the usage I created <a href="http://dir.adamheinrich.com/spreadsheet_to_code/resistive_divider.ods">simple spreadsheet</a> (the original complex spreadsheet can not be disclosed) for calculating resistive voltage divider. It calculates <em>V2</em> from <em>V1</em>, <em>R1</em> and <em>R2</em>:</p>
<p><a href="/public/img/converting-excel-calc-spreadsheet-to-code/resistive_divider_spreadsheet.png">
<img class="alignnone" alt="Resistive divider spreadsheet" src="/public/img/converting-excel-calc-spreadsheet-to-code/resistive_divider_spreadsheet_thumb.png" width="500" />
</a></p>
<p>Of course this is a trivial task but I couldn’t think up anything better that wouldn’t take long minutes to create (Excel calculations aren’t my style, I use it just for painting tables). Many thanks to Wikipedia for the <a href="http://en.wikipedia.org/wiki/File:Resistive_divider.png">File:Resistive_divider.png</a>.</p>
<p>For the conversion just download <a href="http://dir.adamheinrich.com/spreadsheet_to_code/SpreadsheetToCode.jar">SpreadsheetToCode.jar</a> and run it:</p>
<figure class="highlight"><pre><code class="language-sh" data-lang="sh">java <span class="nt">-jar</span> SpreadsheetToCode.jar resistive_divider.ods</code></pre></figure>
<p>The resulting code will be:</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="cm">/* Constants: */</span>
<span class="kt">double</span> <span class="n">C5</span> <span class="o">=</span> <span class="mi">22</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span>
<span class="kt">double</span> <span class="n">C6</span> <span class="o">=</span> <span class="mi">20</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span>
<span class="kt">double</span> <span class="n">C7</span> <span class="o">=</span> <span class="mi">3</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span>
<span class="cm">/* Formulas: */</span>
<span class="kt">double</span> <span class="n">C10</span> <span class="o">=</span> <span class="n">C6</span><span class="o">+</span><span class="n">C7</span><span class="p">;</span>
<span class="kt">double</span> <span class="n">C11</span> <span class="o">=</span> <span class="n">C7</span><span class="o">/</span><span class="n">C10</span><span class="p">;</span>
<span class="kt">double</span> <span class="n">E5</span> <span class="o">=</span> <span class="n">C5</span><span class="o">*</span><span class="n">C11</span><span class="p">;</span></code></pre></figure>
<p>The only thing you need is to wrap it into some function and look into spreadsheet what cells are inputs (<em>C5</em>, <em>C6</em>, <em>C7</em>: they are listed in <em>Constants</em> section) and which are results (<em>E5</em> in our case). Problem solved!</p>
<h2>Source code and limitations</h2>
<p>Full Java source code is at <a href="http://dir.adamheinrich.com/spreadsheet_to_code/">dir.adamheinrich.com/spreadsheet_to_code/</a> (source.zip). As this was an one-hour project the code is not ideal (but I tried to keep it understandable) and there are some known limitations (or features :-) ) I didn’t want to solve:</p>
<ul>
<li>The resulting code is always C-like (with statements ended by a semicolon). You can change this in <strong>Cell.getGeneratedCode()</strong> method.</li>
<li>Your task is to decide what cells are inputs and which are outputs. The easiest way is to wrap generated code in function and fill input cells with function arguments.</li>
<li>Formulas containing IF or mathematical functions are not converted to valid C code (but from the generated code it's quite obvious is it supposed to do). Your task is to change function names to something meaningful and replace IF with ternary operator.</li>
<li>Cells with column indexes higher than Z (AA1, AB1, ...) are not correctly represented. You can modify the cell name assignment in <strong>Cell.setName(int row, int column)</strong> method if you need this. It should be a few lines of code.</li>
</ul>
<h2>Result</h2>
<p>This utility saved me hours of boring copy&paste in Excel and ordering lines of code because of dependencies. Instead I spend the time coding this utility which was more fun:-) It is placed online so that it can help someone else. Enjoy!</p>
How to Load Native JNI Library from JAR2012-12-09T00:00:00+01:00https://www.adamh.cz/blog/2012/12/how-to-load-native-jni-library-from-jar<p>The <strong>JNI</strong> (Java Native Interface) is a framework that provides a bridge between Java and native applications. Java applications can define <strong>native methods</strong> which are implemented in dynamic library written in other languages such as C or C++. The dynamic library is able to call both static and dynamic methods. More information about JNI can be found on <a href="http://en.wikipedia.org/wiki/Java_Native_Interface">Wiki</a> or in tutorial <a href="http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html">Beginning JNI with NetBeans</a> (for Linux).</p>
<p>The problem is that for loading such a dynamic library you have to call method <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load%28java.lang.String%29">System.load(String filename)</a> which requires an absolute filename. This approach is just fine if you have dynamic library outside the application’s JAR archive, but when bundling dynamic library within the JAR it is necessary to extract the library into filesystem before loading it. And that’s exactly what my code does.</p>
<!--more-->
<p>Our simple JNI class could look like this:</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloJNI</span> <span class="o">{</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="s">"/path/to/my/library.so"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">native</span> <span class="kt">void</span> <span class="nf">hello</span><span class="o">();</span>
<span class="o">}</span></code></pre></figure>
<p>To extract the library before loading it it’s necessary to add some code into the <strong>static</strong> section. I wrapped it into a static method inside simple class called <strong><a href="https://github.com/adamheinrich/native-utils">NativeUtils</a></strong>. I decided to put it into separate class in order to have space for adding more features (like choosing the right dynamic library for host OS and architecture). The class can be found on my <a href="https://github.com/adamheinrich/native-utils">Github</a>.</p>
<p>The code is commented and self-explaining, so I don’t have to write too much about it. Just three notes:</p>
<ul>
<li><strong>The file path is passed as string, not as instance of File.</strong> It is because File transforms the abstract path to system-specific (absolute path decision, directory delimiters) one, which could cause problems. It must be an absolute path (starting with '/') and the filename has to be at least three characters long (due to restrictions of <a href="http://docs.oracle.com/javase/6/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String%29">File.createTempFile(String prefix, String suffix)</a>.</li>
<li>The temporary file is stored into temp directory specified by <i>java.io.tmpdir</i> (by default it's the operating system's temporary directory). It should be automatically deleted when the application exits.</li>
<li>Although the code has some try-finally section (to be sure that streams are closed properly in case an exception is thrown), it does not catch exceptions. <strong>The exception has to be handled by the application.</strong> I belive this approach is cleaner and has some benefits.</li>
</ul>
<p>Final usage is pretty simple. :-) Just call method <strong>loadLibraryFromJar</strong> and handle exception somehow:</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">cz.adamh.NativeUtils</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloJNI</span> <span class="o">{</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="nc">NativeUtils</span><span class="o">.</span><span class="na">loadLibraryFromJar</span><span class="o">(</span><span class="s">"/resources/libHelloJNI.so"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span> <span class="c1">// This is probably not the best way to handle exception :-) </span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">native</span> <span class="kt">void</span> <span class="nf">hello</span><span class="o">();</span>
<span class="o">}</span></code></pre></figure>
<p><strong>Edited 2013-04-02:</strong> Lofi <a href="#comment-1760518031">came with a workaround</a> to release and delete our DLL from temporary directory on Widnows.</p>
<p>Get the whole code from my <a href="https://github.com/adamheinrich/native-utils">Github</a>!</p>
Decimal Comma in Matlab Plots2012-12-01T00:00:00+01:00https://www.adamh.cz/blog/2012/12/decimal-comma-in-matlab-plots<p>A lot of languages (not only the European ones) use <strong>decimal comma</strong> instead decimal point to separate the whole and the fractional part of a number in decimal form. <strong>Matlab</strong> (like other programs) uses decimal point for this purpose. This is okay when using it for computations but it is better to use decimal comma in graphs embedded in documents written in some European language.</p>
<p><a href="/public/img/decimal-comma-in-matlab-plots/decimal_comma_with.png">
<img class="alignnone" title="A Matlab graph with decimal commas" alt="" src="/public/img/decimal-comma-in-matlab-plots/decimal_comma_with.png" width="318" height="239" />
</a></p>
<p>This approach also works in <strong>GNU Octave</strong> which is available for free. ;-)</p>
<!--more-->
<p>Basic plotting in Matlab is done by the <a href="http://www.mathworks.com/help/matlab/ref/plot.html">plot</a> function:</p>
<figure class="highlight"><pre><code class="language-matlab" data-lang="matlab"><span class="c1">% Define function</span>
<span class="n">x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">1</span><span class="p">;</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.^</span><span class="mi">3</span><span class="p">;</span>
<span class="c1">% Open graph window and plot the function</span>
<span class="nb">figure</span><span class="p">;</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
<span class="c1">% Label axes, show grid and title</span>
<span class="nb">grid</span> <span class="n">on</span><span class="p">;</span>
<span class="nb">title</span><span class="p">(</span><span class="s1">'Simple plot'</span><span class="p">);</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'y=x^3'</span><span class="p">);</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'x'</span><span class="p">);</span></code></pre></figure>
<p>To change decimal point to decimal comma I used simple approach: Function <em>get(gca, ‘XTick’)</em> returns the tick of X asix. In our case it is a vector -1:0.01:1. Another function <em>set(gca, ‘XTickLabel’)</em> sets a label for X axis. This label contains cell array of strings. The only necessary step between calling these functions is to change decimal points to commas using the <a href="http://www.mathworks.com/help/matlab/ref/strrep.html">strrep</a> function.</p>
<p>I wrapped this code into a simple function <a href="https://github.com/adamheinrich/decimal-comma"><strong>decimal_comma</strong></a> with a few arguments:</p>
<ul>
<li><strong>axes_handle</strong> is handle of axes to be changed. Just enter gca for current plot.</li>
<li><strong>axis_name</strong> is the name of axis to be changed: <em>'X'</em>, <em>'Y'</em> or <em>'XY'</em> for both axes.</li>
<li><strong>formatstr</strong> (optional) is simple <a href="http://www.mathworks.com/help/matlab/ref/sprintf.html#inputarg_formatSpec">sprintf</a>-like format string, e.g. <em>'%2.2f'</em></li>
</ul>
<p>Just copy the code below and save it into new script file named decimal_comma.m or clone the whole <strong><a href="https://github.com/adamheinrich/decimal-comma">Github repository</a></strong>:</p>
<figure class="highlight"><pre><code class="language-matlab" data-lang="matlab"><span class="k">function</span> <span class="n">decimal_comma</span><span class="p">(</span><span class="n">axis_handle</span><span class="p">,</span> <span class="n">axis_name</span><span class="p">,</span> <span class="nb">varargin</span><span class="p">)</span>
<span class="c1">%DECIMAL_COMMA - decimal comma in 2-D plot</span>
<span class="c1">%</span>
<span class="c1">% A simple function to replace decimal points with decimal commas (which</span>
<span class="c1">% are usual in Europe) in Matlab or Octave plots.</span>
<span class="c1">%</span>
<span class="c1">% DECIMAL_COMMA(axis_handle, axis_name) changes decimal point to decimal</span>
<span class="c1">% comma in a plot. Use gca for current axes handle and one of 'X', 'Y' or</span>
<span class="c1">% 'XY' for axis_name.</span>
<span class="c1">%</span>
<span class="c1">% DECIMAL_COMMA(axis_handle, axis_name, formatstr) changes decimal point </span>
<span class="c1">% to decimal comma in a plot. Number format is specified by formatstr </span>
<span class="c1">% (see SPRINTF for details). </span>
<span class="c1">% (c) 2012 Adam Heinrich <adam@adamh.cz>. Published under the MIT license.</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">nargin</span> <span class="o"><</span> <span class="mi">2</span> <span class="o">||</span> <span class="nb">nargin</span> <span class="o">></span> <span class="mi">3</span><span class="p">)</span>
<span class="nb">error</span><span class="p">(</span><span class="s1">'Wrong number of input parameters.'</span><span class="p">);</span>
<span class="k">end</span>
<span class="k">switch</span> <span class="n">axis_name</span>
<span class="k">case</span> <span class="s1">'XY'</span>
<span class="n">decimal_comma</span><span class="p">(</span><span class="n">axis_handle</span><span class="p">,</span> <span class="s1">'X'</span><span class="p">,</span> <span class="nb">varargin</span><span class="p">{:});</span>
<span class="n">decimal_comma</span><span class="p">(</span><span class="n">axis_handle</span><span class="p">,</span> <span class="s1">'Y'</span><span class="p">,</span> <span class="nb">varargin</span><span class="p">{:});</span>
<span class="k">case</span> <span class="p">{</span><span class="s1">'X'</span><span class="p">,</span> <span class="s1">'Y'</span><span class="p">}</span>
<span class="n">tick</span> <span class="o">=</span> <span class="nb">get</span><span class="p">(</span><span class="n">axis_handle</span><span class="p">,</span> <span class="nb">strcat</span><span class="p">(</span><span class="n">axis_name</span><span class="p">,</span> <span class="s1">'Tick'</span><span class="p">));</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">length</span><span class="p">(</span><span class="n">tick</span><span class="p">);</span>
<span class="n">labels</span> <span class="o">=</span> <span class="nb">cell</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="n">n</span>
<span class="n">label</span> <span class="o">=</span> <span class="nb">num2str</span><span class="p">(</span><span class="n">tick</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="nb">varargin</span><span class="p">{:});</span>
<span class="n">labels</span><span class="p">{</span><span class="n">i</span><span class="p">}</span> <span class="o">=</span> <span class="nb">strrep</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="s1">'.'</span><span class="p">,</span> <span class="s1">','</span><span class="p">);</span>
<span class="k">end</span>
<span class="n">labels</span><span class="p">{</span><span class="mi">1</span><span class="p">}</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="n">labels</span><span class="p">{</span><span class="n">n</span><span class="p">}</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="nb">set</span><span class="p">(</span><span class="n">axis_handle</span><span class="p">,</span> <span class="nb">strcat</span><span class="p">(</span><span class="n">axis_name</span><span class="p">,</span> <span class="s1">'TickLabel'</span><span class="p">),</span> <span class="n">labels</span><span class="p">);</span>
<span class="k">otherwise</span>
<span class="nb">error</span><span class="p">(</span><span class="s1">'Wrong axis name! Use one of X, Y or XY.'</span><span class="p">);</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<p>Usage for our example function <em>y</em>=<em>x</em><sup>3</sup>:</p>
<figure class="highlight"><pre><code class="language-matlab" data-lang="matlab"><span class="n">x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">1</span><span class="p">;</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.^</span><span class="mi">3</span><span class="p">;</span>
<span class="nb">figure</span><span class="p">;</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
<span class="nb">grid</span> <span class="n">on</span><span class="p">;</span>
<span class="nb">title</span><span class="p">(</span><span class="s1">'Simple plot'</span><span class="p">);</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'y=x^3'</span><span class="p">);</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'x'</span><span class="p">);</span>
<span class="n">decimal_comma</span><span class="p">(</span><span class="nb">gca</span><span class="p">,</span> <span class="s1">'XY'</span><span class="p">);</span></code></pre></figure>
<p>If you like to hide numbers on the left and right, just add this line before <em>set(axis_handle, …)</em>:</p>
<figure class="highlight"><pre><code class="language-matlab" data-lang="matlab"><span class="n">labels</span><span class="p">{</span><span class="mi">1</span><span class="p">}</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="n">labels</span><span class="p">{</span><span class="n">n</span><span class="p">}</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span></code></pre></figure>
<p>The plot will then change to:</p>
<p><a href="/public/img/decimal-comma-in-matlab-plots/decimal_comma_without.png">
<img class="alignnone" title="A Matlab graph with decimal commas" alt="" src="/public/img/decimal-comma-in-matlab-plots/decimal_comma_without.png" width="318" height="239" />
</a></p>
<p>Get the whole code from my <a href="https://github.com/adamheinrich/decimal-comma">Github</a>!</p>