<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Adam Heinrich</title><link href="https://www.adamh.cz/" rel="alternate"/><link href="https://www.adamh.cz/atom.xml" rel="self"/><id>https://www.adamh.cz/</id><updated>2020-11-28T00:00:00+01:00</updated><entry><title>Parametric stand for synthesizers in OpenSCAD</title><link href="https://www.adamh.cz/blog/2020/11/parametric-stand-for-synthesizers-in-openscad/" rel="alternate"/><published>2020-11-28T00:00:00+01:00</published><updated>2020-11-28T00:00:00+01:00</updated><author><name/></author><id>tag:www.adamh.cz,2020-11-28:/blog/2020/11/parametric-stand-for-synthesizers-in-openscad/</id><summary type="html">&lt;p&gt;When looking for a suitable stand for my beloved &lt;a href="https://www.vectorsynth.com"&gt;Vector synthesizer&lt;/a&gt;
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 &amp;ndash; the stand had to be able to
fit …&lt;/p&gt;</summary><content type="html">&lt;p&gt;When looking for a suitable stand for my beloved &lt;a href="https://www.vectorsynth.com"&gt;Vector synthesizer&lt;/a&gt;
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 &amp;ndash; the stand had to be able to
fit the limited space of my drawer when not being used.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/parametric-stand-for-synthesizers-in-openscad/vector.jpg"&gt;
&lt;img src="/images/parametric-stand-for-synthesizers-in-openscad/vector_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/parametric-stand-for-synthesizers-in-openscad/stand.jpg"&gt;
&lt;img src="/images/parametric-stand-for-synthesizers-in-openscad/stand_thumb.jpg" class="half"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Although the stand is designed to be printed on a 3D printer (for this
particular print I used the awesome transparent red PETG by &lt;a href="https://www.filament-pm.cz"&gt;Plasty Mladeč&lt;/a&gt;
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.&lt;/p&gt;
&lt;p&gt;The GPL-licensed OpenSCAD source code and detailed instructions can be
found on &lt;a href="https://github.com/adamheinrich/gear_stand"&gt;GitHub&lt;/a&gt;. The model is parametric so it is possible to change the
dimensions, angle or the geometry of the rubber feet used.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Drawing with main parameters" src="/images/parametric-stand-for-synthesizers-in-openscad/drawing.png"&gt;&lt;/p&gt;</content><category term="posts"/></entry><entry><title>C cheatsheet</title><link href="https://www.adamh.cz/blog/2018/02/c-cheatsheet/" rel="alternate"/><published>2018-02-25T00:00:00+01:00</published><updated>2018-02-25T00:00:00+01:00</updated><author><name/></author><id>tag:www.adamh.cz,2018-02-25:/blog/2018/02/c-cheatsheet/</id><summary type="html">&lt;p&gt;This is a short collection of notes on the C language which are important
especially when dealing with hardware.&lt;/p&gt;
&lt;h2&gt;Data types&lt;/h2&gt;
&lt;h3&gt;Type sizes&lt;/h3&gt;
&lt;p&gt;The exact size of integer data types is implementation-specific. Only minimum
&lt;em&gt;ranges&lt;/em&gt; are defined (§5.2.4.2.1):&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Signed range&lt;/th&gt;
&lt;th&gt;Unsigned range&lt;/th&gt;
&lt;th&gt;Size*&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;±127 …&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</summary><content type="html">&lt;p&gt;This is a short collection of notes on the C language which are important
especially when dealing with hardware.&lt;/p&gt;
&lt;h2&gt;Data types&lt;/h2&gt;
&lt;h3&gt;Type sizes&lt;/h3&gt;
&lt;p&gt;The exact size of integer data types is implementation-specific. Only minimum
&lt;em&gt;ranges&lt;/em&gt; are defined (§5.2.4.2.1):&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Signed range&lt;/th&gt;
&lt;th&gt;Unsigned range&lt;/th&gt;
&lt;th&gt;Size*&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;±127&lt;/td&gt;
&lt;td&gt;0 to 255&lt;/td&gt;
&lt;td&gt;8 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;±32767&lt;/td&gt;
&lt;td&gt;0 to 65535&lt;/td&gt;
&lt;td&gt;16 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;±2147483647&lt;/td&gt;
&lt;td&gt;0 to (2&lt;sup&gt;32&lt;/sup&gt;-1)&lt;/td&gt;
&lt;td&gt;32 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;long long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;±(2&lt;sup&gt;63&lt;/sup&gt;-1)&lt;/td&gt;
&lt;td&gt;0 to (2&lt;sup&gt;64&lt;/sup&gt;-1)&lt;/td&gt;
&lt;td&gt;64 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Important notes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Whether a &lt;code&gt;char&lt;/code&gt; is treated as &lt;code&gt;signed char&lt;/code&gt; or &lt;code&gt;unsigned char&lt;/code&gt; is
  up to the implementation (see &amp;ldquo;Implementation-defined behavior&amp;rdquo;).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;size in bits&lt;/strong&gt; is not defined by the standard (ditto).&lt;/li&gt;
&lt;li&gt;There is no implicit assumption that negative numbers are represented by
  &lt;strong&gt;two&amp;rsquo;s complement&lt;/strong&gt; (which is the most common case). Consequences:&lt;ul&gt;
&lt;li&gt;Range is &lt;code&gt;±(2^(N-1) - 1)&lt;/code&gt; rather than &lt;code&gt;-2^(N-1)&lt;/code&gt; to &lt;code&gt;2^(N-1) - 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Undefined behavior for signed overflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;int&lt;/code&gt; type usually represents the &lt;em&gt;natural&lt;/em&gt; processor word. However, this
is not the case for 8-bit architectures or for some 64-bit systems.&lt;/p&gt;
&lt;p&gt;To write portable code, use types defined in &lt;code&gt;&amp;lt;stdint.h&amp;gt;&lt;/code&gt; (e.g. &lt;code&gt;uint32_t&lt;/code&gt;)
where size matters.&lt;/p&gt;
&lt;h3&gt;The sizeof operator&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;sizeof&lt;/code&gt; operator (not a function) gives object size in &amp;ldquo;bytes&amp;rdquo; (§6.5.3.4).
However, because the standard mandates that &lt;code&gt;sizeof(char)&lt;/code&gt; equals to 1, the
term &amp;ldquo;byte&amp;rdquo; is not understood as an octet (8 bits) &amp;ndash; simply because a &lt;code&gt;char&lt;/code&gt;
can be represented by more than 8 bits. To get size in bits, multiply the result
of the &lt;code&gt;sizeof&lt;/code&gt; operator by &lt;code&gt;CHAR_BITS&lt;/code&gt; (defined in &lt;code&gt;&amp;lt;limits.h&amp;gt;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Its result is an unsigned integer of type &lt;code&gt;size_t&lt;/code&gt; (defined in &lt;code&gt;&amp;lt;stddef.h&amp;gt;&lt;/code&gt;).
Therefore, the &lt;code&gt;size_t&lt;/code&gt; type is guaranteed to hold size of any possible object
including arrays. This makes it useful for portable array indexing.&lt;/p&gt;
&lt;h3&gt;Integer promotion&lt;/h3&gt;
&lt;p&gt;The following code prints &lt;code&gt;c != 0xff&lt;/code&gt; given that it is compiled on a platform
where &lt;code&gt;char&lt;/code&gt; is treated as signed:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nv"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;xff&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;xff&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;printf&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;c == 0xff\n&amp;quot;&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;printf&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;c != 0xff\n&amp;quot;&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the effect of &lt;em&gt;integer promotion&lt;/em&gt; (§6.3.1.1):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If an &lt;code&gt;int&lt;/code&gt; can represent all values of the original type, the value is
converted to an &lt;code&gt;int&lt;/code&gt;; otherwise, it is converted to an &lt;code&gt;unsigned int&lt;/code&gt;.
These are called the &lt;em&gt;integer promotions&lt;/em&gt;. All other types are unchanged by
the integer promotions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The value &lt;code&gt;0xff&lt;/code&gt; (255 in decimal) assigned to &lt;code&gt;c&lt;/code&gt; is outside the range ±127
defined for &lt;code&gt;signed char&lt;/code&gt;; given that the machine uses two&amp;rsquo;s complement
representation, the value of &lt;code&gt;c&lt;/code&gt; is interpreted as -1.&lt;/p&gt;
&lt;p&gt;Because all values of &lt;code&gt;signed char&lt;/code&gt; are representable in &lt;code&gt;int&lt;/code&gt;, both operands
of the comparison &lt;code&gt;(c == 0xff)&lt;/code&gt; are promoted to &lt;code&gt;int&lt;/code&gt;. While the &lt;code&gt;0xff&lt;/code&gt; literal
is represented by &lt;code&gt;0x000000ff&lt;/code&gt; on a machine with 32-bit &lt;code&gt;int&lt;/code&gt;, the negative
value of &lt;code&gt;c&lt;/code&gt; will be represented as &lt;code&gt;0xffffffff&lt;/code&gt; after the promotion.
And obviously, &lt;code&gt;0x000000ff&lt;/code&gt; and &lt;code&gt;0xffffffff&lt;/code&gt; do not match.&lt;/p&gt;
&lt;p&gt;Possible fixes to print &lt;code&gt;c == 0xff&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Declare &lt;code&gt;c&lt;/code&gt; as &lt;code&gt;unsigned char&lt;/code&gt; or &lt;code&gt;uint8_t&lt;/code&gt;. In this case, both operands are
  interpreted as 255 and &lt;code&gt;0x000000ff&lt;/code&gt; will be compared to &lt;code&gt;0x000000ff&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Cast &lt;code&gt;0xff&lt;/code&gt; to char in the comparison: &lt;code&gt;(chat)0xff&lt;/code&gt;. In this case, both
  operands are interpreted as -1 and &lt;code&gt;0xffffffff&lt;/code&gt; is be compared to
  &lt;code&gt;0xffffffff&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Structures&lt;/h3&gt;
&lt;p&gt;General rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The address of the structure is the address of its first member, i.e. the
   first member&amp;rsquo;s offset is always 0.&lt;/li&gt;
&lt;li&gt;Ordering of members is preserved in memory, i.e. a member&amp;rsquo;s offset is greater
   than offset of previously declared members.&lt;/li&gt;
&lt;li&gt;The compiler might add &lt;em&gt;padding&lt;/em&gt; between two consecutive members or at the
   end of the structure.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Designated initializers&lt;/h4&gt;
&lt;p&gt;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).&lt;/p&gt;
&lt;p&gt;A common approach is to use &lt;code&gt;memset()&lt;/code&gt; from &lt;code&gt;&amp;lt;string.h&amp;gt;&lt;/code&gt; to clear the structure
before assigning individual members:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;struct foo {
    int f1;
    int f2;
} s;

memset(&amp;amp;s, 0, sizeof(s));
s.f1 = 1;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, a better option is to use a &lt;em&gt;designated initializer&lt;/em&gt;. In this case,
members not assigned by the initializer will be automatically initialized to
zero:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;struct foo s = { .f1 = 1 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or, when assigning a new value:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;s = (struct foo) { .f1 = 1 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Behavior&lt;/h2&gt;
&lt;p&gt;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 &lt;a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf"&gt;C standard&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Undefined behavior&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Using uninitialized variables with automatic storage which
  &lt;a href="https://stackoverflow.com/a/11965368"&gt;never had its address taken&lt;/a&gt; (§6.3.2.1). If the address has been
  taken, the value is &amp;ldquo;just&amp;rdquo; indeterminate&lt;/li&gt;
&lt;li&gt;Using object outside its lifetime (§6.2.4)&lt;/li&gt;
&lt;li&gt;Signed integer overflow&lt;/li&gt;
&lt;li&gt;Buffer overflow (accessing array elements outside bounds)&lt;/li&gt;
&lt;li&gt;Dereferencing &lt;code&gt;NULL&lt;/code&gt; pointer (§6.3.2)&lt;/li&gt;
&lt;li&gt;Modification of string literals (§6.4.5) and &lt;code&gt;const&lt;/code&gt; objects (§6.7.3)&lt;/li&gt;
&lt;li&gt;Left shifting past bit-width (e.g. &lt;code&gt;1UL &amp;lt;&amp;lt; 32&lt;/code&gt; for 32-bit int)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moreover, &lt;strong&gt;shifting value into or past the sign bit&lt;/strong&gt; is also undefined
behavior. More precisely, it happens the resulting value is not
&lt;a href="https://stackoverflow.com/questions/3784996/why-does-left-shift-operation-invoke-undefined-behaviour-when-the-left-side-oper"&gt;representable in result type&lt;/a&gt; (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&amp;rsquo;s complement representation of signed
integers):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;int foo = (1 &amp;lt;&amp;lt; 31); /* Undefined behavior */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To avoid undefined behavior when left-shifting, use unsigned literals and do not
exceed result type size:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;uint32_t bar = (1UL &amp;lt;&amp;lt; 31);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Implementation-defined behavior&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The number of bits in &lt;code&gt;char&lt;/code&gt;, defined in &lt;code&gt;CHAR_BITS&lt;/code&gt; (§3.6)&lt;/li&gt;
&lt;li&gt;Whether &lt;code&gt;char&lt;/code&gt; is treated as &lt;code&gt;signed char&lt;/code&gt; or &lt;code&gt;unsigned char&lt;/code&gt; (§6.2.5)&lt;/li&gt;
&lt;li&gt;Expansion of the &lt;code&gt;NULL&lt;/code&gt; macro (§3.6) &amp;ndash; it doesn&amp;rsquo;t have to be &lt;code&gt;((void*)0)&lt;/code&gt;.
  However:&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;if (!ptr)&lt;/code&gt; to check for a null pointer is correct because an
  expression with value 0 cast to &lt;code&gt;void *&lt;/code&gt; is a &lt;em&gt;null pointer constant&lt;/em&gt;
  (§6.3.2.3)&lt;/li&gt;
&lt;li&gt;It is safe to assume that &lt;code&gt;static char *str&lt;/code&gt; will be initialized
  to a null pointer (§6.7.8)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Representation of signed integer types (§6.2.6.2)&lt;/li&gt;
&lt;li&gt;Can be either sign and magnitude, one&amp;rsquo;s complement or two&amp;rsquo;s complement&lt;/li&gt;
&lt;li&gt;However, &lt;code&gt;intN_t&lt;/code&gt; types from &lt;code&gt;&amp;lt;stdint.h&amp;gt;&lt;/code&gt; have to represent integers with
     two&amp;rsquo;s complement representation (§7.18.11.1).&lt;/li&gt;
&lt;li&gt;Right-shifting negative values (§6.5.7)&lt;/li&gt;
&lt;li&gt;Endianness&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Unspecified behavior&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Evaluation order of operands except for &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt;, &lt;code&gt;?:&lt;/code&gt; and &lt;code&gt;,&lt;/code&gt; (§6.5)&lt;ul&gt;
&lt;li&gt;The exception is handy for constructions like
  &lt;code&gt;if (str != NULL &amp;amp;&amp;amp; *str != '\0')&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Evaluation order of function arguments (§6.5.2.2)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Function prototypes&lt;/h2&gt;
&lt;p&gt;Prototype &lt;code&gt;void foo(void)&lt;/code&gt; declares a function which takes no arguments, whereas
prototype &lt;code&gt;void foo()&lt;/code&gt; declares a function accepting any number of arguments.&lt;/p&gt;
&lt;p&gt;Prototype for the main function can be either &lt;code&gt;int main(int argc, char *argv[])&lt;/code&gt;
or &lt;code&gt;int main(void)&lt;/code&gt; (§5.1.2.2.1). Moreover, it is not necessary to explicitly
return a value from &lt;code&gt;main&lt;/code&gt; (§5.1.2.2.3):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If the return type of the &lt;code&gt;main&lt;/code&gt; function is a type compatible with &lt;code&gt;int&lt;/code&gt;, a
return from the initial call to the &lt;code&gt;main&lt;/code&gt; function is equivalent to calling
the &lt;code&gt;exit&lt;/code&gt; function with the value returned by the &lt;code&gt;main&lt;/code&gt; function as its
argument; reaching the &lt;code&gt;}&lt;/code&gt; that terminates the &lt;code&gt;main&lt;/code&gt; function returns a value
of 0.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Therefore, the following construction is perfectly legal:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;int main(void)
{
    printf(&amp;quot;Hi there\n&amp;quot;);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Useful GCC flags&lt;/h2&gt;
&lt;p&gt;Apart from &lt;code&gt;-Wall -pedantic&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-Wextra&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wconversion&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wcast-align&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wdouble-promotion&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wfloat-conversion&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-ftrapv"&gt;&lt;code&gt;-ftrapv&lt;/code&gt;&lt;/a&gt; traps signed integer overflow by calling &lt;code&gt;abort()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;p&gt;All references in the text refer to the &lt;a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf"&gt;N1256 draft&lt;/a&gt; of the C99 standard
(ISO/IEC 9899:1999).&lt;/p&gt;
&lt;p&gt;Useful links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://gist.github.com/lisovy/b2e8633a53915d7e95c6"&gt;gist.github.com/lisovy/b2e8633a53915d7e95c6&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.catb.org/esr/structure-packing/"&gt;The Lost Art of Structure Packing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://spin.atomicobject.com/2014/05/19/c-undefined-behaviors/"&gt;Undefined Behaviors in C that You’ve Probably Run into without Knowing It&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://david.tribble.com/text/cdiffs.htm"&gt;Incompatibilities Between ISO C and ISO C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.pvv.org/~oma/DeepC_slides_oct2011.pdf"&gt;Deep C (and C++)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><category term="posts"/></entry><entry><title>Context Switch on the ARM Cortex-M0</title><link href="https://www.adamh.cz/blog/2016/07/context-switch-on-the-arm-cortex-m0/" rel="alternate"/><published>2016-07-28T00:00:00+02:00</published><updated>2016-07-28T00:00:00+02:00</updated><author><name/></author><id>tag:www.adamh.cz,2016-07-28:/blog/2016/07/context-switch-on-the-arm-cortex-m0/</id><summary type="html">&lt;p&gt;I have written a simple round-robin scheduler (&lt;a href="https://github.com/adamheinrich/os.h/tree/blog_2016_07"&gt;available on GitHub&lt;/a&gt;) for the
ARM Cortex-M0 (ARMv6-M) CPU to understand the context switch mechanism. This
article is a short summary of its principle.&lt;/p&gt;
&lt;p&gt;The same approach is used by many RTOSes and is well described in
&lt;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/"&gt;The Definitive Guide to ARM® Cortex …&lt;/a&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;I have written a simple round-robin scheduler (&lt;a href="https://github.com/adamheinrich/os.h/tree/blog_2016_07"&gt;available on GitHub&lt;/a&gt;) for the
ARM Cortex-M0 (ARMv6-M) CPU to understand the context switch mechanism. This
article is a short summary of its principle.&lt;/p&gt;
&lt;p&gt;The same approach is used by many RTOSes and is well described in
&lt;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/"&gt;The Definitive Guide to ARM® Cortex®-M0 and Cortex-M0+ Processors&lt;/a&gt; by
Joseph Yiu.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_intro.png"&gt;&lt;img alt="Round-robin scheduler" src="/images/context-switch-on-the-arm-cortex-m0/os_intro.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Background&lt;/h2&gt;
&lt;p&gt;The processor has two separate stack pointer which can be accessed through banked
&lt;code&gt;SP&lt;/code&gt; register: &lt;strong&gt;Main Stack Pointer&lt;/strong&gt; (MSP) which is the default one after
startup and &lt;strong&gt;Process Stack Pointer&lt;/strong&gt; (PSP) which can be optionally used.&lt;/p&gt;
&lt;p&gt;The processor supports multiple modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The default mode is the &lt;strong&gt;Privileged Thread Mode&lt;/strong&gt;. It is possible to switch
  stack to PSP in this mode&lt;/li&gt;
&lt;li&gt;The Privileged mode can be switched to &lt;strong&gt;Unprivileged Thread Mode&lt;/strong&gt; which has
  certain register and memory access restrictions&lt;/li&gt;
&lt;li&gt;Exceptions and interrupts are handled in the &lt;strong&gt;Handler Mode&lt;/strong&gt; which uses the
  MSP stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;Context Switch&lt;/h2&gt;
&lt;p&gt;The context switch happens in an interrupt handler. Once an interrupt occurs,
the NVIC hardware automatically stacks an &lt;strong&gt;exception frame&lt;/strong&gt; (registers
&lt;code&gt;xPSR&lt;/code&gt;, &lt;code&gt;PC&lt;/code&gt;, &lt;code&gt;LR&lt;/code&gt;, &lt;code&gt;r12&lt;/code&gt; and &lt;code&gt;r3-r0&lt;/code&gt;) onto the Process Stack (PSP)
and branches to the interrupt handler routine in Handler Mode (which uses the
Main Stack).&lt;/p&gt;
&lt;p&gt;The context switch routine has to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Manually stack remaining registers &lt;code&gt;r4-r11&lt;/code&gt; on the Process Stack&lt;/li&gt;
&lt;li&gt;Save current task&amp;rsquo;s PSP to memory&lt;/li&gt;
&lt;li&gt;Load next task&amp;rsquo;s stack pointer and assign it to PSP&lt;/li&gt;
&lt;li&gt;Manually unstack registers &lt;code&gt;r4-r11&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;bx 0xfffffffD&lt;/code&gt; which makes the processor switch to Unprivileged
  Handler Mode, unstack next task&amp;rsquo;s exception frame and continue on its &lt;code&gt;PC&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Exception frame saved by the NVIC hardware onto stack:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_stack_nvic.png"&gt;&lt;img alt="Exception frame saved by NVIC" src="/images/context-switch-on-the-arm-cortex-m0/os_stack_nvic.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Registers saved by the software:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_stack_sw.png"&gt;&lt;img alt="Registers saved by SW" src="/images/context-switch-on-the-arm-cortex-m0/os_stack_sw.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Performing the Context Switch&lt;/h2&gt;
&lt;p&gt;The context switch could be performed by the &lt;code&gt;SysTick_Handler&lt;/code&gt; with a SysTick
timer configured to fire interrupts periodically:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_systick_onyly.png"&gt;&lt;img alt="Context switch performed by SysTick" src="/images/context-switch-on-the-arm-cortex-m0/os_systick_onyly.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This approach would however not work with other interrupts (peripheral interrupt
for example). The &lt;code&gt;SysTick_Handler&lt;/code&gt; would stack registers affected by the
peripheral IRQ handler and unstack task&amp;rsquo;s registers, resulting in undefined
behavior of both tasks and peripheral interrupt handler:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_systick_irq_problem.png"&gt;&lt;img alt="Context switch performed by SysTick - problem with IRQ" src="/images/context-switch-on-the-arm-cortex-m0/os_systick_irq_problem.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The solution is simple - the &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L104"&gt;SysTick_Handler&lt;/a&gt; with the highest priority
only selects the next task to be run and triggers PendSV interrupt.
The &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os_pendsv_handler.s"&gt;PendSV_Handler&lt;/a&gt; with the lowest priority performs the actual
context switch once all interrupt requests with higher priority have been
handled:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/context-switch-on-the-arm-cortex-m0/os_systick_pendsv.png"&gt;&lt;img alt="Context switch scheduled by SysTick and performed by PendSV" src="/images/context-switch-on-the-arm-cortex-m0/os_systick_pendsv.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;PendSV_Handler&lt;/code&gt; is written in pure assembly. The code relies on a fact
that the task&amp;rsquo;s stack pointer is the first element of the &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L8"&gt;os_task_t&lt;/a&gt;
structure - the structure&amp;rsquo;s address corresponds to the address of its first
element according to C language specification.&lt;/p&gt;
&lt;h2&gt;Task Initialization&lt;/h2&gt;
&lt;p&gt;Each task is defined by its handler function and stack. The initialization phase
of task&amp;rsquo;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:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;xPSR&lt;/code&gt; to &lt;code&gt;0x01000000&lt;/code&gt; (the defaul value)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PC&lt;/code&gt; to the handler function&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LR&lt;/code&gt; to a &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L27"&gt;function&lt;/a&gt; to be called when the handler function finishes
  (otherwise the CPU would jump to invalid location, causing HardFault or
  undefined behavior)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The actual function &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L40"&gt;os_task_init&lt;/a&gt; stores values for registers &lt;code&gt;r0-r12&lt;/code&gt; as
well for debugging purposes.&lt;/p&gt;
&lt;h2&gt;Startup&lt;/h2&gt;
&lt;p&gt;The startup phase has to configure SysTick and PendSV interrupt levels,
initialize the SysTick timer to fire interrupts periodically and start the
first task.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;CONTROL&lt;/code&gt; register followed by &lt;code&gt;ISB&lt;/code&gt; instruction.&lt;/p&gt;
&lt;p&gt;The &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/src/os.c#L82"&gt;os_start&lt;/a&gt; function is written without inline assembly thanks to
functions and intrinsics provided by the CMSIS library.&lt;/p&gt;
&lt;h2&gt;Example&lt;/h2&gt;
&lt;p&gt;An &lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/examples/stm32f030x8/main.c"&gt;example&lt;/a&gt; runs three tasks (which are switched every second). All tasks
blink the onboard LED with different frequency.&lt;/p&gt;
&lt;p&gt;The example can be run on STM32F030R8 Nucleo board and requires &lt;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"&gt;STM32Cube&lt;/a&gt;
software pack which has to be present in the &lt;a href="https://github.com/adamheinrich/os.h/tree/blog_2016_07/examples/lib"&gt;lib&lt;/a&gt; directory.&lt;/p&gt;
&lt;p&gt;The provided Makefile requires GCC compiler and OpenOCD. See
&lt;a href="https://github.com/adamheinrich/os.h/blob/blog_2016_07/examples/stm32f030x8"&gt;README&lt;/a&gt; for more information about compilation and flashing.&lt;/p&gt;
&lt;h2&gt;Compatibility&lt;/h2&gt;
&lt;p&gt;The SysTick timer and Privileged mode are optional features of the ARMv6-M
architecture. They are however supported by vast majority of microcontrollers.&lt;/p&gt;
&lt;p&gt;The code relies on standard &lt;a href="http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php"&gt;CMSIS library&lt;/a&gt; by ARM which is usually
distributed by microcontroller vendors. The library provides functions and
intrinsics for accessing features of the ARM Cortex-M core.&lt;/p&gt;
&lt;details class="comments"&gt;
  &lt;summary&gt;Comments&lt;/summary&gt;
  &lt;p&gt;&lt;i&gt;The comments have been archived from Disqus.&lt;/i&gt;&lt;/p&gt;

  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;23ars&lt;/b&gt; on 2017-04-02:&lt;/div&gt;
    &lt;p&gt;I took a look through the code and saw that you handle the PendSv interrupt in assembly. Couldn't be used a naked interrupt and only the context switch to be made in assembly? I mean, some inline functions  for context save/load.&lt;/p&gt;
    &lt;p&gt;Interesting article!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam Heinrich&lt;/b&gt; on 2017-04-04:&lt;/div&gt;
    &lt;p&gt;Hi, thank you and sorry for the delay. Yes, it could be also written as a naked interrupt (GCC: &lt;code&gt;__attribute__((naked))&lt;/code&gt;) with inline assembly and the &lt;code&gt;__get_PSP()&lt;/code&gt;/&lt;code&gt;__set_PSP()&lt;/code&gt; intrinsic functions provided by CMSIS.&lt;/p&gt;
    &lt;p&gt;I usually tend to split the code into "pure C" and "pure assembly" functions as it is quite complicated to write a compiler-agnostic code with all the required function attributes and inline assembly.&lt;/p&gt;
    &lt;p&gt;Also, one has no control over the produced code and the compiler could in theory insert some instructions modifying registers before the inline assembly section (although this is not very likely).&lt;/p&gt;
    &lt;p&gt;The downside of my solution is that the assembly code has to access variables &lt;code&gt;os_curr_task&lt;/code&gt;/&lt;code&gt;os_next_task&lt;/code&gt; from os.c and that the structure &lt;code&gt;os_task_t&lt;/code&gt; must keep field &lt;code&gt;sp&lt;/code&gt; as its first element.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;grapherd&lt;/b&gt; on 2017-08-28:&lt;/div&gt;
    &lt;p&gt;Thank for your post, the picture were great!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Ken&lt;/b&gt; on 2019-06-21:&lt;/div&gt;
    &lt;p&gt;Got a question about  the final example with PendSV, where the execution flow looks like:&lt;/p&gt;
    &lt;p&gt;Task 1 -&amp;gt; Peripheral interrupt -&amp;gt; Systick -&amp;gt; IRQ -&amp;gt; PendSV -&amp;gt; Task2.&lt;/p&gt;
    &lt;p&gt;When Task 1 goes to the Peripheral interrupt, the exception frame gets pushed onto PSP. But what about R4-R11? Wouldn't this information get lost by the time PendSV tries to store Task 1's R4-R11 onto  PSP? Is the Peripheral interrupt supposed to store R4-R11 onto the PSP?&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam Heinrich&lt;/b&gt; on 2019-06-21:&lt;/div&gt;
    &lt;p&gt;Hi, good question!&lt;/p&gt;
    &lt;p&gt;If any interrupt service routine uses r4-r11 itself, it pushes them on stack "manually" on entry (the ISR code is responsible for that) and pops them on exit.&lt;/p&gt;
    &lt;p&gt;If no ISR uses r4-r11, the registers simply keep their content.&lt;/p&gt;
    &lt;p&gt;In both cases, r4-r11 contain Task 1's values when PendSV fires so the values stored are valid.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Ken&lt;/b&gt; on 2019-06-21:&lt;/div&gt;
    &lt;p&gt;I see! Does the ISR push the registers onto MSP or PSP? I wanna say it's MSP since it's safer (e.g. what if the PSP was able to overflow).&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam Heinrich&lt;/b&gt; on 2019-06-21:&lt;/div&gt;
    &lt;p&gt;The NVIC stacks exception frame to MSP (ISR is the user code which optionally stacks the registers which are not part of the exception frame already stacked by HW).&lt;/p&gt;
    &lt;p&gt;The only time when NVIC stacks to PSP is when an exception (e.g. interrupt) happens + the CPU is in a Thread mode (i.e. not in ISR) + the CPU has been configured to use PSP in the thread mode (this is not the default behavior but the code presented here does it as part of its initialization).&lt;/p&gt;
  &lt;/div&gt;
&lt;/details&gt;</content><category term="posts"/></entry><entry><title>Acrylic Case for the Intel Edison Breakout Board</title><link href="https://www.adamh.cz/blog/2016/05/acrylic-case-for-the-intel-edison-breakout-board/" rel="alternate"/><published>2016-05-31T00:00:00+02:00</published><updated>2016-05-31T00:00:00+02:00</updated><author><name/></author><id>tag:www.adamh.cz,2016-05-31:/blog/2016/05/acrylic-case-for-the-intel-edison-breakout-board/</id><summary type="html">&lt;p&gt;Last year I bought the Intel Edison Breakout Board for some experiments.
I&amp;nbsp;was afraid to carry it around in my bag so I&amp;nbsp;have decided to design
a simple laser-cut acrylic case.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/edison_case.jpg"&gt;
&lt;img alt="Edison Case - Photo" src="/images/acrylic-case-for-the-intel-edison-breakout-board/edison_case_thumb.jpg"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The case is made of five parts cut from 3 mm thick acrylic. Blue are inner
spacers …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Last year I bought the Intel Edison Breakout Board for some experiments.
I&amp;nbsp;was afraid to carry it around in my bag so I&amp;nbsp;have decided to design
a simple laser-cut acrylic case.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/edison_case.jpg"&gt;
&lt;img alt="Edison Case - Photo" src="/images/acrylic-case-for-the-intel-edison-breakout-board/edison_case_thumb.jpg"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Edison Case - Drawing" src="/images/acrylic-case-for-the-intel-edison-breakout-board/parts_color.png"&gt;&lt;/p&gt;
&lt;p&gt;You can choose from two options for the bottom plate - with or without a cutout
exposing breakout board&amp;rsquo;s GPIO pins.&lt;/p&gt;
&lt;p&gt;Recommended material:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;3 mm thick acrylic&lt;/li&gt;
&lt;li&gt;4&amp;times; 6 mm M3 nylon screws (top)&lt;/li&gt;
&lt;li&gt;4&amp;times; 8 mm M3 nylon screws (bottom)&lt;/li&gt;
&lt;li&gt;4&amp;times; 6 mm M3 nylon standoffs (between top plate and PCB)&lt;/li&gt;
&lt;li&gt;4&amp;times; M3 nylon washers (between bottom plate and PCB)&lt;/li&gt;
&lt;li&gt;(optional) 2&amp;times; M1.6 standoffs with shorter screw (see below)&lt;/li&gt;
&lt;li&gt;(optional) 2&amp;times; M1.6 nuts to replace the original ones&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The original M1.6 standoff&amp;rsquo;s screw is too long so it does not fit the case.
As you can see on photos, I&amp;rsquo;ve just cut the original screws and left the module
without any nuts.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s generally a good idea to buy more nylon screws - mine crack easily.
The case does also look god with metal inbus (hex) screws.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;You can download the drawing &lt;a href="/files/acrylic-case-for-the-intel-edison-breakout-board/edison_case.zip"&gt;here&lt;/a&gt;&lt;/strong&gt; (Inkscape SVG and PDF).
It is licensed under &lt;a href="http://creativecommons.org/licenses/by-sa/4.0/"&gt;CC BY-SA 4.0&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Many thanks to &lt;a href="mailto:michalneoral@email.cz"&gt;Michal Neoral&lt;/a&gt; for beautiful photos!&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/front_black.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/front_black_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/back_black.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/back_black_thumb.jpg" class="half"&gt;&lt;a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/side_black.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/side_black_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/both_black.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/both_black_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/top_white.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/top_white_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/bottom_white.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/bottom_white_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/standing_black.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/standing_black_thumb.jpg" class="half"&gt;&lt;/a&gt;
&lt;a href="/images/acrylic-case-for-the-intel-edison-breakout-board/standing_white.jpg"&gt;
&lt;img src="/images/acrylic-case-for-the-intel-edison-breakout-board/standing_white_thumb.jpg" class="half"&gt;&lt;/a&gt;&lt;/p&gt;</content><category term="posts"/></entry><entry><title>Converting Excel (Calc) Spreadsheet to Code</title><link href="https://www.adamh.cz/blog/2013/10/converting-excel-calc-spreadsheet-to-code/" rel="alternate"/><published>2013-10-20T00:00:00+02:00</published><updated>2013-10-20T00:00:00+02:00</updated><author><name/></author><id>tag:www.adamh.cz,2013-10-20:/blog/2013/10/converting-excel-calc-spreadsheet-to-code/</id><summary type="html">&lt;p&gt;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 …&lt;/p&gt;</summary><content type="html">&lt;p&gt;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&amp;rsquo;t work properly - results calculated by the spreadsheet differed from my program&amp;rsquo;s result.&lt;/p&gt;
&lt;p&gt;Therefore I decided to save the Excel spreadsheet in ODF format (OpenOffice.org Calc) and write a little helper called &lt;strong&gt;SpreadsheetToCode&lt;/strong&gt;. It&amp;rsquo;s a command-line utility that does the following steps:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;strong&gt;Load OpenDocument Spreadsheet file&lt;/strong&gt; (.ods) because it's basically XML file wrapped in ZIP and therefore super easy to handle&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Pick all cells&lt;/strong&gt; which contain numerical values (inputs, parameters) or expressions with them (formulas)&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Print C-like source code&lt;/strong&gt; with the spreadsheet's functionality expressed in lines of code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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&amp;rsquo;s greatest help is that it checks for dependencies which means that code&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;double C2 = C10/3;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;will be placed after&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;double C10 = C5+C1;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;because C10 mustn&amp;rsquo;t be used before its declaration/definition.&lt;/p&gt;
&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;To illustrate the usage I created &lt;a href="/files/spreadsheet_to_code/resistive_divider.ods"&gt;simple spreadsheet&lt;/a&gt; (the original complex spreadsheet can not be disclosed) for calculating resistive voltage divider. It calculates &lt;em&gt;V2&lt;/em&gt; from &lt;em&gt;V1&lt;/em&gt;, &lt;em&gt;R1&lt;/em&gt; and &lt;em&gt;R2&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/converting-excel-calc-spreadsheet-to-code/resistive_divider_spreadsheet.png"&gt;
&lt;img class="alignnone" alt="Resistive divider spreadsheet" src="/images/converting-excel-calc-spreadsheet-to-code/resistive_divider_spreadsheet_thumb.png" width="500"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Of course this is a trivial task but I couldn&amp;rsquo;t think up anything better that wouldn&amp;rsquo;t take long minutes to create (Excel calculations aren&amp;rsquo;t my style, I use it just for painting tables). Many thanks to Wikipedia for the &lt;a href="http://en.wikipedia.org/wiki/File:Resistive_divider.png"&gt;File:Resistive_divider.png&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For the conversion just download &lt;a href="/files/spreadsheet_to_code/SpreadsheetToCode.jar"&gt;SpreadsheetToCode.jar&lt;/a&gt; and run it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;java -jar SpreadsheetToCode.jar resistive_divider.ods
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The resulting code will be:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;/*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Constants&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*/&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;22.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C6&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;20.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C7&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;/*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Formulas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*/&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C10&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C6&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;C7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C11&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C7&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;C10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;double&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;E5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;C5&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;C11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The only thing you need is to wrap it into some function and look into spreadsheet what cells are inputs (&lt;em&gt;C5&lt;/em&gt;, &lt;em&gt;C6&lt;/em&gt;, &lt;em&gt;C7&lt;/em&gt;: they are listed in &lt;em&gt;Constants&lt;/em&gt; section) and which are results (&lt;em&gt;E5&lt;/em&gt; in our case). Problem solved!&lt;/p&gt;
&lt;h2&gt;Source code and limitations&lt;/h2&gt;
&lt;p&gt;Full Java source code is available in &lt;a href="/files/spreadsheet_to_code/source.zip"&gt;source.zip&lt;/a&gt;. 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&amp;rsquo;t want to solve:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;The resulting code is always C-like (with statements ended by a semicolon). You can change this in &lt;strong&gt;Cell.getGeneratedCode()&lt;/strong&gt; method.&lt;/li&gt;
    &lt;li&gt;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.&lt;/li&gt;
    &lt;li&gt;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.&lt;/li&gt;
    &lt;li&gt;Cells with column indexes higher than Z (AA1, AB1, ...) are not correctly represented. You can modify the cell name assignment in &lt;strong&gt;Cell.setName(int row, int column)&lt;/strong&gt; method if you need this. It should be a few lines of code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Result&lt;/h2&gt;
&lt;p&gt;This utility saved me hours of boring copy&amp;amp;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!&lt;/p&gt;</content><category term="posts"/></entry><entry><title>How to Load Native JNI Library from JAR</title><link href="https://www.adamh.cz/blog/2012/12/how-to-load-native-jni-library-from-jar/" rel="alternate"/><published>2012-12-09T00:00:00+01:00</published><updated>2012-12-09T00:00:00+01:00</updated><author><name/></author><id>tag:www.adamh.cz,2012-12-09:/blog/2012/12/how-to-load-native-jni-library-from-jar/</id><summary type="html">&lt;p&gt;The &lt;strong&gt;JNI&lt;/strong&gt; (Java Native Interface) is a framework that provides a bridge between Java and native applications. Java applications can define &lt;strong&gt;native methods&lt;/strong&gt; 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 …&lt;/p&gt;</summary><content type="html">&lt;p&gt;The &lt;strong&gt;JNI&lt;/strong&gt; (Java Native Interface) is a framework that provides a bridge between Java and native applications. Java applications can define &lt;strong&gt;native methods&lt;/strong&gt; 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 &lt;a href="http://en.wikipedia.org/wiki/Java_Native_Interface"&gt;Wiki&lt;/a&gt; or in tutorial &lt;a href="http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html"&gt;Beginning JNI with NetBeans&lt;/a&gt; (for Linux).&lt;/p&gt;
&lt;p&gt;The problem is that for loading such a dynamic library you have to call method &lt;a href="http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load%28java.lang.String%29"&gt;System.load(String filename)&lt;/a&gt; which requires an absolute filename. This approach is just fine if you have dynamic library outside the application&amp;rsquo;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&amp;rsquo;s exactly what my code does.&lt;/p&gt;
&lt;p&gt;Our simple JNI class could look like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;HelloJNI&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/path/to/my/library.so&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;native&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb nb-Type"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To extract the library before loading it it&amp;rsquo;s necessary to add some code into the &lt;strong&gt;static&lt;/strong&gt; section. I wrapped it into a static method inside simple class called &lt;strong&gt;&lt;a href="https://github.com/adamheinrich/native-utils"&gt;NativeUtils&lt;/a&gt;&lt;/strong&gt;. 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 &lt;a href="https://github.com/adamheinrich/native-utils"&gt;Github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The code is commented and self-explaining, so I don&amp;rsquo;t have to write too much about it. Just three notes:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;The file path is passed as string, not as instance of File.&lt;/strong&gt; 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 &lt;a href="http://docs.oracle.com/javase/6/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String%29"&gt;File.createTempFile(String prefix, String suffix)&lt;/a&gt;.&lt;/li&gt;
    &lt;li&gt;The temporary file is stored into temp directory specified by &lt;i&gt;java.io.tmpdir&lt;/i&gt; (by default it's the operating system's temporary directory). It should be automatically deleted when the application exits.&lt;/li&gt;
    &lt;li&gt;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. &lt;strong&gt;The exception has to be handled by the application.&lt;/strong&gt; I belive this approach is cleaner and has some benefits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Final usage is pretty simple. :-) Just call method &lt;strong&gt;loadLibraryFromJar&lt;/strong&gt; and handle exception somehow:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;cz.adamh.NativeUtils&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;HelloJNI&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="n"&gt;NativeUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadLibraryFromJar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/resources/libHelloJNI.so&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;catch&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IOException&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;printStackTrace&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;This&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;probably&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;way&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;native&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Edited 2013-04-02:&lt;/strong&gt; Lofi &lt;a href="#comment-1760518031"&gt;came with a workaround&lt;/a&gt; to release and delete our DLL from temporary directory on Windows.&lt;/p&gt;
&lt;p&gt;Get the whole code from my &lt;a href="https://github.com/adamheinrich/native-utils"&gt;Github&lt;/a&gt;!&lt;/p&gt;
&lt;details class="comments"&gt;
  &lt;summary&gt;Comments&lt;/summary&gt;
  &lt;p&gt;&lt;i&gt;The comments have been archived from Disqus.&lt;/i&gt;&lt;/p&gt;

  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;davs&lt;/b&gt; on 2012-12-27:&lt;/div&gt;
    &lt;p&gt;The article is great. &lt;br&gt;One little remark ..&lt;/p&gt;
    &lt;p&gt;Lines 60-61:&lt;br&gt;// Prepare temporary file&lt;br&gt;        File temp = File.createTempFile(prefix, suffix);&lt;/p&gt;
    &lt;p&gt;in your case suffix contain only extension (without '.' before) .. And I was getting the error of FileNotFound in line 91.&lt;/p&gt;
    &lt;p&gt;Changing Line 61 to &lt;br&gt;File temp = File.createTempFile(prefix, "." + suffix);&lt;br&gt;fixed my issue .. maybe not only mine :)&lt;/p&gt;
    &lt;p&gt;Cheers,&lt;br&gt;Davs&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam&lt;/b&gt; on 2012-12-27:&lt;/div&gt;
    &lt;p&gt;Thank you, davs. Sorry for my fault, the temporary file was really "libraryso" instead of "&lt;a href="http://library.so" rel="nofollow noopener" target="_blank" title="library.so"&gt;library.so&lt;/a&gt;". But I didn't have any problems caused by the FileNotFound with my code, maybe it was some OS-specific issue (I run application using this code on Linux).&lt;/p&gt;
    &lt;p&gt;I modified line 52 from &lt;code&gt;suffix = (parts.length &amp;gt; 1) ? parts[parts.length - 1] : null;&lt;/code&gt; to &lt;code&gt;suffix = (parts.length &amp;gt; 1) ? "."+parts[parts.length - 1] : null;&lt;/code&gt;, because the &lt;i&gt;File.createTempFile(prefix, suffix)&lt;/i&gt; method uses ".tmp" as default extension if suffix is null.&lt;/p&gt;
    &lt;p&gt;I also changed &lt;i&gt;split()&lt;/i&gt; on line 50, it is now limited to 2 items.&lt;/p&gt;
    &lt;p&gt;Eveything should be OK now :-)&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam&lt;/b&gt; on 2013-02-07:&lt;/div&gt;
    &lt;p&gt;Hello, this is interesting. Look at nice explanation at &lt;a href="http://kalblogs.blogspot.cz/2009/01/java.html" rel="nofollow noopener" target="_blank" title="http://kalblogs.blogspot.cz/2009/01/java.html"&gt;http://kalblogs.blogspot.cz...&lt;/a&gt;, maybe the &lt;code&gt;$LD_LIBRARY_PATH&lt;/code&gt; variable could be the way to go.&lt;/p&gt;
    &lt;p&gt;Some people wanted to do the same thing (embed JNI library that depends on some shared libs into JAR), after some searching I found only this crazy solution: &lt;a href="http://stackoverflow.com/a/2979861" rel="nofollow noopener" target="_blank" title="http://stackoverflow.com/a/2979861"&gt;http://stackoverflow.com/a/...&lt;/a&gt;...&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Lars Nielsen&lt;/b&gt; on 2014-08-05:&lt;/div&gt;
    &lt;p&gt;Hey I am trying this with a folder tructure&lt;br&gt;/demo&lt;br&gt;--/src&lt;br&gt;  -----  &lt;a href="http://App.java" rel="nofollow noopener" target="_blank" title="App.java"&gt;App.java&lt;/a&gt;&lt;br&gt;  ----- /com.core&lt;br&gt;         ----- &lt;a href="http://NativeUtils.java" rel="nofollow noopener" target="_blank" title="NativeUtils.java"&gt;NativeUtils.java&lt;/a&gt;&lt;br&gt;--/Libs&lt;br&gt;  ----- libs.jar&lt;/p&gt;
    &lt;p&gt;with the &lt;a href="http://App.java" rel="nofollow noopener" target="_blank" title="App.java"&gt;App.java&lt;/a&gt; looking like this:&lt;br&gt;&lt;code&gt;&lt;br&gt;import java.io.IOException;&lt;br&gt;&lt;br&gt;import com.core.NativeUtils;&lt;br&gt;&lt;br&gt;&lt;br&gt;public class App {&lt;br&gt;&lt;br&gt;&lt;br&gt;public static void main(String[] args) {&lt;br&gt;try {&lt;br&gt;NativeUtils.loadLibraryFromJar("/lib/lib_fifi.so");&lt;br&gt;System.out.println("Library Loaded - Loaded from jar");&lt;br&gt;} catch (IOException e) {&lt;br&gt;// TODO Auto-generated catch block&lt;br&gt;e.printStackTrace();&lt;br&gt;}&lt;br&gt;}&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;br&gt;But it does not work any ideas I get: File /lib/&lt;a href="http://lib_fifi.so" rel="nofollow noopener" target="_blank" title="lib_fifi.so"&gt;lib_fifi.so&lt;/a&gt; was not found inside JAR&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam&lt;/b&gt; on 2014-08-09:&lt;/div&gt;
    &lt;p&gt;Hello, it looks like you didn't include the file (I don't see any /lib/&lt;a href="http://lib_fifi.so" rel="nofollow noopener" target="_blank" title="lib_fifi.so"&gt;lib_fifi.so&lt;/a&gt; in your folder structure).&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Aurélien&lt;/b&gt; on 2014-01-29:&lt;/div&gt;
    &lt;p&gt;Thanks, useful trick&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Baruch Youssin&lt;/b&gt; on 2014-06-24:&lt;/div&gt;
    &lt;p&gt;A great solution, very simple, much simpler than all the competing solutions posted on the net.&lt;br&gt;There is one thing in this article that I do not understand: the issue of "absolute path (starting with ‘/’)".&lt;br&gt;This refers to the &lt;br&gt;path&lt;br&gt;parameter in the code, which indicates the native library as a resource inside the jar.&lt;br&gt;This far, I have worked only with audio files as resources, and their paths should not start with a slash / .&lt;br&gt;In fact, I have tried to specify audio files by a path starting with a slash, as /com/...; here com was the name of the top-level directory in the jar, and the rest of the path pointed to the file-resource.  This did not work out (java could not find the resource), neither on Windows XP nor on Linux Ubuntu 12.04, both running a most recent java from Oracle.&lt;br&gt;Removing the initial slash resolves the problem: java finds the audio file.&lt;br&gt;I am going to try both ways with the native library; but what is the rule here?&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Baruch Youssin&lt;/b&gt; on 2014-06-26:&lt;/div&gt;
    &lt;p&gt;I have found the answer:&lt;br&gt;loadLibraryFromJar calls java.lang.Class.getResourceAsStream(String) method while for audio files I use a different method, javal.lang.ClassLoader.getResource(String).&lt;br&gt;These two methods have different rules for path name resolution, and for this reason in getResource(..) I must use the path without the leading / while in getResourceAsStream(..) I must use the leading /.&lt;br&gt;Thanks for the nice code, it works!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment" id="comment-1760518031"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Lofi&lt;/b&gt; on 2013-03-28:&lt;/div&gt;
    &lt;p&gt;Thank you very much for sharing this! However, there's a slight hickup in the solution: The temporary library won't get deleted on windows because the JVM still has a lock on it when it shuts down and invokes deleteOnExit(). The library should be "unloaded". Unfortunately that's currently not possible in Java.&lt;/p&gt;
    &lt;p&gt;In case anyone else runs into this issue of having their temporary folder flooded with DLLs, I came up with this workaround:&lt;/p&gt;
    &lt;p&gt;* in additon to the temporary DLL, also create a .lock file on which you also issue a deleteOnExit(). This file will get deleted when the JVM shuts down&lt;/p&gt;
    &lt;p&gt;* during startup search for all .dll files and delete those which don't have an accompanying ".lock" file&lt;/p&gt;
    &lt;p&gt;Not quite the proper solution, but unless this is fixed internally in Java it may be some workaround :-)&lt;/p&gt;
    &lt;p&gt;Here's some code in case someone wants to use it, just put it at the bottom of the loadLibraryFromJar method:&lt;/p&gt;
    &lt;p&gt;&lt;/p&gt;
    &lt;pre&gt;        final String libraryPrefix = prefix;&lt;br&gt;        final String lockSuffix = ".lock";&lt;br&gt;        &lt;br&gt;        // create lock file&lt;br&gt;        final File lock = new File( temp.getAbsolutePath() + lockSuffix);&lt;br&gt;        lock.createNewFile();&lt;br&gt;        lock.deleteOnExit();&lt;br&gt;     &lt;br&gt;        // file filter for library file (without .lock files)&lt;br&gt;        FileFilter tmpDirFilter =&lt;br&gt;          new FileFilter()&lt;br&gt;          {&lt;br&gt;            public boolean accept(File pathname)&lt;br&gt;            {&lt;br&gt;              return pathname.getName().startsWith( libraryPrefix) &amp;amp;&amp;amp; !pathname.getName().endsWith( lockSuffix);&lt;br&gt;            }&lt;br&gt;          };&lt;br&gt;          &lt;br&gt;        // get all library files from temp folder  &lt;br&gt;        String tmpDirName = System.getProperty("java.io.tmpdir");&lt;br&gt;        File tmpDir = new File(tmpDirName);&lt;br&gt;        File[] tmpFiles = tmpDir.listFiles(tmpDirFilter);&lt;br&gt;        &lt;br&gt;        // delete all files which don't have n accompanying lock file&lt;br&gt;        for (int i = 0; i &amp;lt; tmpFiles.length; i++)&lt;br&gt;        {&lt;br&gt;          // Create a file to represent the lock and test.&lt;br&gt;          File lockFile = new File( tmpFiles[i].getAbsolutePath() + lockSuffix);&lt;br&gt;          if (!lockFile.exists())&lt;br&gt;          {&lt;br&gt;            System.out.println( "deleting: " + tmpFiles[i].getAbsolutePath());&lt;br&gt;            tmpFiles[i].delete();&lt;br&gt;          }&lt;br&gt;        } &lt;br&gt;&lt;/pre&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam&lt;/b&gt; on 2013-04-02:&lt;/div&gt;
    &lt;p&gt;Lofi, thank you for this workaround, it will be useful!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Blackshues&lt;/b&gt; on 2015-01-25:&lt;/div&gt;
    &lt;p&gt;This workaround is very clean, and worked for me right out of the box. Thank you sir.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Harbour Porpoise&lt;/b&gt; on 2018-04-26:&lt;/div&gt;
    &lt;p&gt;Thanks so much, this saved the day!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Víctor Vanaclocha Cebrián&lt;/b&gt; on 2019-09-18:&lt;/div&gt;
    &lt;p&gt;I'm trying use this class in a Maven project.&lt;/p&gt;
    &lt;p&gt;-src/main/java&lt;br&gt;----&lt;a href="http://app.java" rel="nofollow noopener" target="_blank" title="app.java"&gt;app.java&lt;/a&gt;&lt;br&gt;..src/main/resources&lt;br&gt;----libraries&lt;br&gt;--------win32-x86-64&lt;br&gt;------------jni.dll&lt;/p&gt;
    &lt;p&gt;I'm calling the "loadLibraryFromJar()" method and I try this:&lt;/p&gt;
    &lt;p&gt;NativeUtils.loadLibraryFromJar("/resources/libraries/win32-x86-64/jni.dll");&lt;br&gt;NativeUtils.loadLibraryFromJar("/libraries/win32-x86-64/jni.dll");&lt;/p&gt;
    &lt;p&gt;The exception is as follows:&lt;br&gt;java.io.FileNotFoundException: File /resources/libraries/win32-x86-64/jni.dll was not found inside JAR.&lt;/p&gt;
    &lt;p&gt;I am generating a jar with the libraries inside the maven jar-project so that I only have one jar application. the libraries go inside the resources directory of the maven structure.&lt;/p&gt;
    &lt;p&gt;Any idea what is happening?&lt;br&gt;In my project there are the dll inside the jar and in my eclipse project.&lt;/p&gt;
  &lt;/div&gt;
&lt;/details&gt;</content><category term="posts"/></entry><entry><title>Decimal Comma in Matlab Plots</title><link href="https://www.adamh.cz/blog/2012/12/decimal-comma-in-matlab-plots/" rel="alternate"/><published>2012-12-01T00:00:00+01:00</published><updated>2012-12-01T00:00:00+01:00</updated><author><name/></author><id>tag:www.adamh.cz,2012-12-01:/blog/2012/12/decimal-comma-in-matlab-plots/</id><summary type="html">&lt;p&gt;A lot of languages (not only the European ones) use &lt;strong&gt;decimal comma&lt;/strong&gt; instead decimal point to separate the whole and the fractional part of a number in decimal form. &lt;strong&gt;Matlab&lt;/strong&gt; (like other programs) uses decimal point for this purpose. This is okay when using it for computations but it is …&lt;/p&gt;</summary><content type="html">&lt;p&gt;A lot of languages (not only the European ones) use &lt;strong&gt;decimal comma&lt;/strong&gt; instead decimal point to separate the whole and the fractional part of a number in decimal form. &lt;strong&gt;Matlab&lt;/strong&gt; (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.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/decimal-comma-in-matlab-plots/decimal_comma_with.png"&gt;
&lt;img class="alignnone" alt="A Matlab graph with decimal commas" src="/images/decimal-comma-in-matlab-plots/decimal_comma_with.png"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This approach also works in &lt;strong&gt;GNU Octave&lt;/strong&gt; which is available for free. ;-)&lt;/p&gt;
&lt;p&gt;Basic plotting in Matlab is done by the &lt;a href="http://www.mathworks.com/help/matlab/ref/plot.html"&gt;plot&lt;/a&gt; function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;% Define function&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.^&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;% Open graph window and plot the function&lt;/span&gt;
&lt;span class="nb"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c"&gt;% Label axes, show grid and title&lt;/span&gt;
&lt;span class="nb"&gt;grid&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Simple plot&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;y=x^3&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;x&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To change decimal point to decimal comma I used simple approach: Function &lt;em&gt;get(gca, &amp;lsquo;XTick&amp;rsquo;)&lt;/em&gt; returns the tick of X asix. In our case it is a vector -1:0.01:1. Another function &lt;em&gt;set(gca, &amp;lsquo;XTickLabel&amp;rsquo;)&lt;/em&gt; 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 &lt;a href="http://www.mathworks.com/help/matlab/ref/strrep.html"&gt;strrep&lt;/a&gt; function.&lt;/p&gt;
&lt;p&gt;I wrapped this code into a simple function &lt;a href="https://github.com/adamheinrich/decimal-comma"&gt;&lt;strong&gt;decimal_comma&lt;/strong&gt;&lt;/a&gt; with a few arguments:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;axes_handle&lt;/strong&gt; is handle of axes to be changed. Just enter gca for current plot.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;axis_name&lt;/strong&gt; is the name of axis to be changed: &lt;em&gt;'X'&lt;/em&gt;, &lt;em&gt;'Y'&lt;/em&gt; or &lt;em&gt;'XY'&lt;/em&gt; for both axes.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;formatstr&lt;/strong&gt; (optional) is simple &lt;a href="http://www.mathworks.com/help/matlab/ref/sprintf.html#inputarg_formatSpec"&gt;sprintf&lt;/a&gt;-like format string, e.g. &lt;em&gt;'%2.2f'&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Just copy the code below and save it into new script file named decimal_comma.m or clone the whole &lt;strong&gt;&lt;a href="https://github.com/adamheinrich/decimal-comma"&gt;Github repository&lt;/a&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;decimal_comma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;axis_handle, axis_name, varargin&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;%DECIMAL_COMMA - decimal comma in 2-D plot&lt;/span&gt;
&lt;span class="c"&gt;%&lt;/span&gt;
&lt;span class="c"&gt;%   A simple function to replace decimal points with decimal commas (which&lt;/span&gt;
&lt;span class="c"&gt;%   are usual in Europe) in Matlab or Octave plots.&lt;/span&gt;
&lt;span class="c"&gt;%&lt;/span&gt;
&lt;span class="c"&gt;%   DECIMAL_COMMA(axis_handle, axis_name) changes decimal point to decimal&lt;/span&gt;
&lt;span class="c"&gt;%   comma in a plot. Use gca for current axes handle and one of &amp;#39;X&amp;#39;, &amp;#39;Y&amp;#39; or&lt;/span&gt;
&lt;span class="c"&gt;%   &amp;#39;XY&amp;#39; for axis_name.&lt;/span&gt;
&lt;span class="c"&gt;%&lt;/span&gt;
&lt;span class="c"&gt;%   DECIMAL_COMMA(axis_handle, axis_name, formatstr) changes decimal point&lt;/span&gt;
&lt;span class="c"&gt;%   to decimal comma in a plot. Number format is specified by formatstr&lt;/span&gt;
&lt;span class="c"&gt;%   (see SPRINTF for details).&lt;/span&gt;

&lt;span class="c"&gt;% (c) 2012 Adam Heinrich &amp;lt;adam@adamh.cz&amp;gt;. Published under the MIT license.&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nargin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;nargin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nb"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Wrong number of input parameters.&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;axis_name&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;XY&amp;#39;&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;decimal_comma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_handle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;X&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;varargin&lt;/span&gt;&lt;span class="p"&gt;{:});&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;decimal_comma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_handle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;varargin&lt;/span&gt;&lt;span class="p"&gt;{:});&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;X&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_handle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;strcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Tick&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;labels&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;num2str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;varargin&lt;/span&gt;&lt;span class="p"&gt;{:});&lt;/span&gt;
&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="n"&gt;labels&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;strrep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;.&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;labels&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;labels&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_handle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;strcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;TickLabel&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;labels&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;otherwise&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="nb"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Wrong axis name! Use one of X, Y or XY.&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Usage for our example function &lt;em&gt;y&lt;/em&gt;=&lt;em&gt;x&lt;/em&gt;&lt;sup&gt;3&lt;/sup&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m m-Double"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;grid&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="nx"&gt;Simple&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;plot&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;x&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;decimal_comma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gca&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="nx"&gt;XY&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you like to hide numbers on the left and right, just add this line before &lt;em&gt;set(axis_handle, &amp;hellip;)&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;labels{1} = &amp;#39;&amp;#39;;
labels{n} = &amp;#39;&amp;#39;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The plot will then change to:&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/decimal-comma-in-matlab-plots/decimal_comma_without.png"&gt;
&lt;img class="alignnone" alt="A Matlab graph with decimal commas" src="/images/decimal-comma-in-matlab-plots/decimal_comma_without.png"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Get the whole code from my &lt;a href="https://github.com/adamheinrich/decimal-comma"&gt;Github&lt;/a&gt;!&lt;/p&gt;
&lt;details class="comments"&gt;
  &lt;summary&gt;Comments&lt;/summary&gt;
  &lt;p&gt;&lt;i&gt;The comments have been archived from Disqus.&lt;/i&gt;&lt;/p&gt;

  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Janek&lt;/b&gt; on 2013-08-30:&lt;/div&gt;
    &lt;p&gt;Again, nice work, but I would modify 14nth line of the code from:&lt;/p&gt;
    &lt;p&gt;label = sprintf(strcat(formatstr, '|'), tick);&lt;/p&gt;
    &lt;p&gt;to:&lt;/p&gt;
    &lt;p&gt;label='';&lt;br&gt;for i=1:length(tick)&lt;br&gt;   label=[label num2str(tick(i)) '|'];&lt;br&gt;end&lt;/p&gt;
    &lt;p&gt;With the above loop we avoid additional zeros (for example I had on my plot 0,10000).&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam&lt;/b&gt; on 2013-10-04:&lt;/div&gt;
    &lt;p&gt;Thank  you for the comment, the function works much better now. I'm sorry for not mentioning it in the article earlier.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Don-Umbro&lt;/b&gt; on 2013-09-19:&lt;/div&gt;
    &lt;p&gt;This hint from Janek is very useful!&lt;br&gt;Maybe you can add this to your explanations - I didn't read the comments at first.&lt;br&gt;By the way: With Janek's solution, you don't need line 15 anymore. My alternative version would be:&lt;/p&gt;
    &lt;p&gt;&lt;code&gt;function [  ] = plotUseDecimalComma( axis_handle, axis_name)&lt;br&gt;% PLOTUSEDECIMALCOMMA changes decimal point to decimal comma in a plot.&lt;br&gt;% Parameters are:&lt;br&gt;% axis_handle: Axes handle to work with (use gca for current axes handle)&lt;br&gt;% axis_name: Name of axis to be changed ('X', 'Y' or 'XY' for both axes)&lt;br&gt;    if(nargin~=2)&lt;br&gt;        error('Wrong number of input parameters.');&lt;br&gt;    end;&lt;br&gt;    switch axis_name&lt;br&gt;        case 'XY'&lt;br&gt;            plotUseDecimalComma(axis_handle, 'X');&lt;br&gt;            plotUseDecimalComma(axis_handle, 'Y');&lt;br&gt;        case {'X', 'Y'}&lt;br&gt;            tick = get(axis_handle, strcat(axis_name, 'Tick'));&lt;br&gt;            label='';&lt;br&gt;            for i=1:length(tick)&lt;br&gt;                label=[label num2str(tick(i)) '|']; %#ok&lt;br&gt;            end&lt;br&gt;            label = strrep(label, '.', ',');&lt;br&gt;            set(axis_handle,  strcat(axis_name, 'TickLabel'), label);&lt;br&gt;        otherwise&lt;br&gt;            error('Wrong axis name! Use one of X, Y or XY.');&lt;br&gt;    end;&lt;br&gt;end &lt;/code&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;janek&lt;/b&gt; on 2013-08-11:&lt;/div&gt;
    &lt;p&gt;You saved my day!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Antonio&lt;/b&gt; on 2013-10-28:&lt;/div&gt;
    &lt;p&gt;Thank you very much!&lt;/p&gt;
    &lt;p&gt;I had to translate an english technical report to spanish, I had assumed to leave the points in the figures but finding your function it has been a present from heaven&lt;/p&gt;
    &lt;p&gt;thanks&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;alexandre&lt;/b&gt; on 2013-04-11:&lt;/div&gt;
    &lt;p&gt;Good work!&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Lukasz&lt;/b&gt; on 2015-02-24:&lt;/div&gt;
    &lt;p&gt;Thank you, it is very good code, but it doesn't work on Matlab 2014b. Graphic engine was changed. Everything works fine when you run it on Matlab 2014a or lower version.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Adam Heinrich&lt;/b&gt; on 2015-06-13:&lt;/div&gt;
    &lt;p&gt;Hi Lukasz, thank you for finding the bug and sorry for my late response.&lt;/p&gt;
    &lt;p&gt;I've just changed ( &lt;a href="https://github.com/adamheinrich/decimal-comma/commit/082e3a242a9586a8058650ee8bd994bc0eb5dec0" rel="nofollow noopener" target="_blank" title="https://github.com/adamheinrich/decimal-comma/commit/082e3a242a9586a8058650ee8bd994bc0eb5dec0"&gt;https://github.com/adamhein...&lt;/a&gt; ) the code to use the newer engine. It seems to work in older versions as well (tested on R2012).&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="comment"&gt;
    &lt;div class="comment-author"&gt;&lt;b&gt;Dmitry 🔴&lt;/b&gt; on 2019-07-19:&lt;/div&gt;
    &lt;p&gt;Thank you so very much!  A most helpful function that works just awesome!&lt;/p&gt;
  &lt;/div&gt;
&lt;/details&gt;</content><category term="posts"/></entry></feed>