<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Coding Stuffs</title>
    <description>Program received signal SIGSEGV, Segmentation fault. Powered by Jekyll &amp; GitHub Pages</description>
    <link>http://blog.wizche.ch/</link>
    <atom:link href="http://blog.wizche.ch/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 23 May 2022 07:50:32 +0000</pubDate>
    <lastBuildDate>Mon, 23 May 2022 07:50:32 +0000</lastBuildDate>
    <generator>Jekyll v3.9.2</generator>
    
      <item>
        <title>Fobber Code Decryption</title>
        <description>&lt;p&gt;After reading the &lt;a href=&quot;https://blog.malwarebytes.org/intelligence/2015/06/elusive-hanjuan-ek-caught-in-new-malvertising-campaign/&quot;&gt;Malwarebytes blog post&lt;/a&gt; describing Fobber, a new variant of Tinba, I wanted to have a look at it myself (MD5 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;691ce6807925fed03cb61f4add0e5ffd&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Instead of unpacking all the code at once in memory, Fobber uses a cleverer way to make the analysis much more difficult. Before any code can be executed, Fobber performs the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Decrypt the function to be executed&lt;/li&gt;
  &lt;li&gt;Execute it&lt;/li&gt;
  &lt;li&gt;Re-encrypt the function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, let’s have a look on how the decryption routine works.
On the screenshot below we see OllyDbg stopped before the decryption function at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x009E2592&lt;/code&gt; is getting called.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/imgs/ollydbg_decrypt_view.png&quot; alt=&quot;OllyDbg call to decrypt function&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The decryption function will use specific bytes preceding this call to perform the decryption. Let’s describe them:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Position&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0xB&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mutex: either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x21&lt;/code&gt; (free) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xC1&lt;/code&gt; (occupied)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0xA..0x9&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Code length&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0x8&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;XOR key&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0x7&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Either 1 (decrypted) or 0 (encrypted)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0x6&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Not used&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- 0x5..0x1&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Call to decrypt function&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;First byte of encrypted code&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;If we want to match these field to the above code we obtain:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Mutex: 0x21
Key: 0x37
Size 0x9c bytes
Encrypted: 0x0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The size is not directly used but it is first XORed with the constant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0E489&lt;/code&gt;, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xE415 ^ 0x0E489 = 0x9C&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The mutex is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x21&lt;/code&gt; meaning that no other thread is decrypting it.&lt;/p&gt;

&lt;p&gt;Now let’s have a look at the decryption function in IDA:
&lt;img src=&quot;/imgs/ida_decrypt.png&quot; alt=&quot;OllyDbg call to decrypt function&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loc_2597&lt;/code&gt; the decrypt function waits until the mutex is free (value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x21&lt;/code&gt;), then it adds 1 to the -7th byte and checks that the the previous value was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0&lt;/code&gt; (encrypted), if so, it continues by pushing the XOR key and the computed size to the stack.&lt;/p&gt;

&lt;p&gt;Function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xor_code&lt;/code&gt; performs the actual decryption.
This function loops from 0 to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt; and for each byte code performs the following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;XOR the current code byte with the XOR key&lt;/li&gt;
  &lt;li&gt;Compute a new XOR key for the next step by rotating the bits of the XOR key 3 position to the right (ROR)
Once finished it foes back&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To simplify this process I wrote a small python script performing the steps described above. You can either use it directly on a dump of the injected memory or adapt it to be loaded in IDA debugger. The script uses &lt;a href=&quot;https://github.com/Gallopsled/pwntools&quot;&gt;pwntools library&lt;/a&gt; to dump assembly code to the console.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;struct&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwnlib.asm&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disasm&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwnlib.context&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'i386'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'windows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endian&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'big'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Injected memory dump file
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./data/dump_009E0000.mem&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytearray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decrypt_section&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ror&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r_bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; \
        &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r_bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; \
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r_bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x53&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tmpkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFF&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Write ASM code to the console
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disasm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Total length %s bytes&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Match where encrypted functions begins using this regex
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;finditer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x21&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xFF&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;]{5}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xe8&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xFF&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;]{4}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Encrypted code section found at %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;H'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0e489&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Wrong match, size bigger then binary data&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;encrypted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encrypted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Wrong match, encrypted flag must be zero&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Key: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Size: %s bytes&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Encrypted: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encrypted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;decrypt_section&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Found %d references!&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Save the decrypted code to a file
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./data/decrypted.bin&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;wf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Written %d bytes to %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Mon, 10 Aug 2015 09:00:12 +0000</pubDate>
        <link>http://blog.wizche.ch/fobber/malware/analysis/2015/08/10/fobber-encryption.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/fobber/malware/analysis/2015/08/10/fobber-encryption.html</guid>
        
        
        <category>fobber</category>
        
        <category>malware</category>
        
        <category>analysis</category>
        
      </item>
    
      <item>
        <title>Blog migrated to Github Pages</title>
        <description>&lt;p&gt;This blog continues the one I started a long time ago on Blogger.
Thanks to &lt;a href=&quot;https://github.com/jekyll/jekyll-import&quot;&gt;jekyll-import&lt;/a&gt; I could migrate all posts from Blogger with ease.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;
</description>
        <pubDate>Sat, 08 Aug 2015 19:01:52 +0000</pubDate>
        <link>http://blog.wizche.ch/misc/2015/08/08/blog-migrated.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/misc/2015/08/08/blog-migrated.html</guid>
        
        
        <category>misc</category>
        
      </item>
    
      <item>
        <title>Modify intercepted Windows API functions parameters with WinAppDbg</title>
        <description>If you are a malware analyst, you probably already came across &lt;a href=&quot;http://winappdbg.sourceforge.net/index.html#&quot;&gt;WinAppDbg&lt;/a&gt;.&lt;br /&gt;This module allows you to quickly script instrumentation code in python under Windows.&lt;br /&gt;&lt;br /&gt;One of its (many) powerful features is the ability to quickly &lt;a href=&quot;http://winappdbg.sourceforge.net/Debugging.html#example-9-intercepting-api-calls&quot;&gt;intercept Windows API calls&lt;/a&gt;. &lt;br/&gt;Let's have a look at a small example showing how to intercept &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx&quot;&gt;Sleep&lt;/a&gt; calls.&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;&lt;br /&gt;class MyEventHandler(EventHandler):&lt;br /&gt;    apiHooks = {&lt;br /&gt;        'kernel32.dll': [&lt;br /&gt;            (&quot;Sleep&quot;, 1)&lt;br /&gt;        ]&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;# Intercept the API before the actual call is being made to Sleep ('pre' callback)&lt;br /&gt;def pre_Sleep(self, event, retval, dwMilliseconds):&lt;br /&gt;    thread = event.get_thread()&lt;br /&gt;    process = event.get_process()&lt;br /&gt;&lt;br /&gt;    print &quot;Intercepted Sleep call of %d milliseconds&quot;&lt;br /&gt;&lt;/pre&gt; Many malware use the &lt;tt&gt;Sleep&lt;/tt&gt; function to delay an operation on the infected machine (e.g. to timeout dynamic analysis or to interval home-beaconing).&lt;br/&gt;With WinAppDbg we can modify the function parameter (&lt;tt&gt;dwMilliseconds&lt;/tt&gt;) before the actual call is being made to Sleep().&lt;br/&gt; To do so we simply access the stack frame and write a new &lt;tt&gt;DWORD&lt;/tt&gt; with value &lt;tt&gt;0x00&lt;/tt&gt; to &lt;tt&gt;EBP+4&lt;/tt&gt;&lt;br/&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;&lt;br /&gt;# Access first parameter on the stack: EBP + 4&lt;br /&gt;stack_offset = thread.get_sp() + 4&lt;br /&gt;# Write the new value at that address (e.g. 0 milliseconds)&lt;br /&gt;process.write_dword(stack_offset, 0x00)&lt;br /&gt;&lt;/pre&gt;
</description>
        <pubDate>Mon, 06 Jul 2015 10:58:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2015/07/06/modify-intercepted-windows-api.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2015/07/06/modify-intercepted-windows-api.html</guid>
        
        <category>winappdbg</category>
        
        <category>reverse-engineering</category>
        
        <category>python</category>
        
        <category>malware</category>
        
        
      </item>
    
      <item>
        <title>Weekend project: Xamarin.Forms app in 20 hours</title>
        <description>&lt;div dir=&quot;ltr&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-O3klRxqxvEE/VMZ-eRXaTQI/AAAAAAAABwI/idZ9mh0ytWQ/s1600/ic_home512.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;http://4.bp.blogspot.com/-O3klRxqxvEE/VMZ-eRXaTQI/AAAAAAAABwI/idZ9mh0ytWQ/s1600/ic_home512.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;Some weekends ago I wanted to test how fast can you develop and deploy an application with &lt;a href=&quot;http://xamarin.com/forms&quot;&gt;Xamarin.Forms&lt;/a&gt;.&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;After around 20 hours of work the Android version landed on the Google Play store. I was very impressed on how fast and clean I had developed the UI in XAML and how all the bindings just worked.&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;The more interesting part was the development of the platform-dependent services for the app to access native audio API and the Ads rendering (using &lt;a href=&quot;https://www.google.com/admob/&quot;&gt;AdMob &lt;/a&gt;component).&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;Fortunately some good guy already deployed a NuGet library called &lt;a href=&quot;https://github.com/kphillpotts/Xamarin.Plugins&quot;&gt;Lamp&lt;/a&gt; to access camera flash across multiple platforms. I just had to pull the library source code to &lt;a href=&quot;https://github.com/kphillpotts/Xamarin.Plugins/issues/3&quot;&gt;fix a bug&lt;/a&gt; within the Android implementation that prevented the flash to turn off.&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;The Audio service is used to play different sound effects simultaneously which were synchronize against the thunder effects generated by the camera flash. I simply used &lt;a href=&quot;http://developer.xamarin.com/guides/android/application_fundamentals/working_with_audio/&quot;&gt;MediaPlayer&lt;/a&gt; for Android and &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/index.html&quot;&gt;AvAudioPlayer&lt;/a&gt; for iOS.&amp;nbsp;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;The challenging part was showing the interstitial Ad to the user before a sequence is started.&amp;nbsp;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;I could not find ready to use library which supported Interstitial Ad from AdMob, nevertheless the &lt;a href=&quot;https://github.com/sharker86/AdsPCLXamarin&quot;&gt;AdsPCLXamarin&lt;/a&gt; put me on the right track on how to develop a custom renderer for each platform for this purpose. It took sometime to get everything working in iOS once it was okay for Android, mostly because I had to migrate the project to unified API and fix some library compatibility issue.&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;But, summing up, &lt;u&gt;20 hours later&lt;/u&gt; I have a ad-supported app on both Google with a sleek UI and which, hopefully, is also useful!&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-p6BJbJ_v9P0/VMZ_-6R9t9I/AAAAAAAABwU/KCmUe9s1d80/s1600/thunderlight.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;180&quot; src=&quot;http://2.bp.blogspot.com/-p6BJbJ_v9P0/VMZ_-6R9t9I/AAAAAAAABwU/KCmUe9s1d80/s1600/thunderlight.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;Now, enough about the pro, here some cons I experienced:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Don't expect that working code on Android behave exactly the same on iOS&lt;/li&gt;&lt;li&gt;Label color on iOS was not working (TextColor property)&lt;/li&gt;&lt;li&gt;Debugging via a separate Mac with the Xamarin.iOS Build Host is a pain in the ass, Visual Studio was laggy and crashed &lt;b&gt;many&lt;/b&gt;&amp;nbsp;times (FYI: I have a dedicated LAN cable to the Mac)&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;I would like to thank all the community which is bringing day after day more and more ready to use libraries to &lt;a href=&quot;http://xamarin.com/forms&quot;&gt;Xamarin.Forms&lt;/a&gt;.&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;b&gt;Google Play URL:&lt;/b&gt; &lt;a href=&quot;https://play.google.com/store/apps/details?id=com.geeksonsoftware.android.thunderlight&quot;&gt;https://play.google.com/store/apps/details?id=com.geeksonsoftware.android.thunderlight&lt;/a&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot;&gt;&lt;br /&gt;&lt;b&gt;AppStore URL:&lt;/b&gt; &lt;i&gt;coming at somepoint&lt;/i&gt;&lt;/div&gt;</description>
        <pubDate>Sun, 05 Jul 2015 17:38:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2015/07/05/weekend-project-xamarinforms-app-in-20.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2015/07/05/weekend-project-xamarinforms-app-in-20.html</guid>
        
        <category>mobile</category>
        
        <category>programming</category>
        
        <category>xamarin</category>
        
        <category>xamarin.forms</category>
        
        <category>apps</category>
        
        
      </item>
    
      <item>
        <title>Xamarin Stability - Graphical overview of Bugzilla Issues</title>
        <description>Hi all,&lt;br /&gt;&lt;br /&gt;I'm a fellow subscriber of Bugzilla and I have reported some issues for Xamarin.Forms myself.&amp;nbsp; &lt;br /&gt;What I wanted is a better way to understand bugs reporting vs fixing, so I came up with a graphical solution:&amp;nbsp;&lt;a href=&quot;http://xamarinstability.azurewebsites.net/&quot;&gt;&lt;b&gt;http://xamarinstability.azurewebsites.net/&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-8ALSrn3M_IU/VMoKQhmYIeI/AAAAAAAABwo/XKHqSgGdwIk/s1600/Screen%2BShot%2B2015-01-29%2Bat%2B11.23.05.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-8ALSrn3M_IU/VMoKQhmYIeI/AAAAAAAABwo/XKHqSgGdwIk/s1600/Screen%2BShot%2B2015-01-29%2Bat%2B11.23.05.png&quot; height=&quot;393&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;The chart above shows an example of the bugs count in &lt;a href=&quot;http://xamarin.com/forms&quot;&gt;Xamarin.Forms&lt;/a&gt; by status. Data are taken from the &lt;a href=&quot;https://bugzilla.xamarin.com/&quot;&gt;Xamarin Bugzilla&lt;/a&gt; website via the CSV feed.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Its interesting to see how Xamarin.Forms &quot;hype&quot; is growing and more time and resources are invested to make it &lt;b&gt;stable&lt;/b&gt;.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;The charts are updated once a day with the latest information. The webapp is deployed on Azure Websites and uses node.js &amp;amp; express, Google Charts and bootstrap for its purpose.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;The charts are generated for Xamarin.Forms, Xamarin.Android and Xamarin.iOS products.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;If you are interested in more products/platforms, just PM me.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;</description>
        <pubDate>Fri, 30 Jan 2015 07:29:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2015/01/30/xamarin-stability-graphical-overview-of.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2015/01/30/xamarin-stability-graphical-overview-of.html</guid>
        
        
      </item>
    
      <item>
        <title>Xamarin Studio - Dark color scheme</title>
        <description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;I'm a big fan of Visual Studio and I use it almost everyday, lately also for Xamarin development. I recently came across this &lt;a href=&quot;http://blog.xamarin.com/contest-favorite-feature-of-xamarin-studio/&quot;&gt;Xamarin Contest&lt;/a&gt;&amp;nbsp;and decided to give Xamarin Studio a try. Xamarin Studio loaded the solution&amp;nbsp;&lt;b&gt;blazing fast&lt;/b&gt;&amp;nbsp;compared to Visual Studio (maybe because of the absence of &lt;a href=&quot;http://www.jetbrains.com/resharper/&quot;&gt;R#&lt;/a&gt; and &lt;a href=&quot;http://www.wholetomato.com/&quot;&gt;Visual Assist&lt;/a&gt;?).&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;My first customization was to change the default color scheme for the editor windows of the Xamarin Studio. I'm more a night-scheme guy, so I checked just the dark theme for this purpose.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;Currently there are 4 dark color scheme supported:&lt;/div&gt;&lt;div class=&quot;&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;Monokai&lt;/li&gt;&lt;li&gt;Nightshade&lt;/li&gt;&lt;li&gt;Oblivion&lt;/li&gt;&lt;li&gt;Solarized Dark&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-hgXF46F_1eY/U-i9MwQdELI/AAAAAAAABqE/Bf9VCbdi9vM/s1600/darkscheme_xs.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-hgXF46F_1eY/U-i9MwQdELI/AAAAAAAABqE/Bf9VCbdi9vM/s1600/darkscheme_xs.png&quot; height=&quot;452&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I decided to go with &lt;b&gt;Monokai &lt;/b&gt;as it seemed to me the easiest to perceive (clear, good contrast, not too dark). This, together with the &lt;a href=&quot;http://blog.xamarin.com/be-more-productive-with-these-new-xamarin-studio-features/&quot;&gt;Floating Window feature&lt;/a&gt; and the amazing &lt;a href=&quot;https://justgetflux.com/&quot;&gt;f.lux&lt;/a&gt; create a perfect multiple-monitors development environment.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;You can change the color scheme in the &lt;i&gt;Options &lt;/i&gt;dialog under the &lt;i&gt;Text Editor - Syntax Highlighting&lt;/i&gt; section.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-DAWVGJt4C3k/U-jBeCu7pnI/AAAAAAAABqQ/jV-7rimQiU0/s1600/syntax_highlight.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://4.bp.blogspot.com/-DAWVGJt4C3k/U-jBeCu7pnI/AAAAAAAABqQ/jV-7rimQiU0/s1600/syntax_highlight.png&quot; height=&quot;292&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;In the future, I would like to see a &lt;b&gt;complete &lt;/b&gt;dark user-interface theme, like the one provided by Visual Studio.&lt;/div&gt;&lt;br /&gt;</description>
        <pubDate>Mon, 11 Aug 2014 13:27:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2014/08/11/xamarin-studio-dark-color-scheme.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2014/08/11/xamarin-studio-dark-color-scheme.html</guid>
        
        
      </item>
    
      <item>
        <title>Relax Cube - Arduino ADK Mega</title>
        <description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://i.imgur.com/fv00qdO.jpg?1&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;287&quot; src=&quot;http://i.imgur.com/fv00qdO.jpg?1&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;&quot;&gt;Relax Cube is a weekend project spent trying to port&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://play.google.com/store/apps/details?id=com.geeksonsoftware.android.relaxlamp.lite&quot; style=&quot;background-color: white; color: #3b73af; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; text-decoration: none;&quot;&gt;Relax Lamp&lt;/a&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;&quot;&gt;&amp;nbsp;to the physical world using an Arduino Mega ADK and some leds.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px;&quot;&gt;Check it out on &lt;a href=&quot;https://bitbucket.org/wizche/relax-cube&quot;&gt;Bitbucket&lt;/a&gt;!&lt;/span&gt;&lt;/div&gt;</description>
        <pubDate>Sat, 04 May 2013 09:57:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2013/05/04/relax-cube-arduino-adk-mega.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2013/05/04/relax-cube-arduino-adk-mega.html</guid>
        
        <category>programming</category>
        
        <category>arduino</category>
        
        <category>embedded</category>
        
        
      </item>
    
      <item>
        <title>Wicket 1.5–Custom Exception Page</title>
        <description>&lt;p&gt;I’m building a web application using Wicket for the first time (which is &lt;strong&gt;really&lt;/strong&gt; cool by the way). I’d like that all uncaught RuntimeExceptions are displayed in a custom exception page which permit the user to submit the error report to myself.&lt;br&gt;&lt;br&gt;I had no luck searching for a simple example that explains how to redirect a user to a custom page when an exception happens. (at least not for Wicket v1.5).&lt;br&gt;&lt;br&gt;Following the description in the &lt;a href=&quot;https://cwiki.apache.org/WICKET/migration-to-wicket-15.html#MigrationtoWicket1.5-Exceptionhandling&quot;&gt;Wicket Migration Guide&lt;/a&gt; I came out with my own own code.&lt;br&gt;&lt;br&gt;1) You have to add an &lt;a href=&quot;http://wicket.apache.org/apidocs/1.5/org/apache/wicket/request/cycle/AbstractRequestCycleListener.html&quot;&gt;AbstractRequestCycleListener&lt;/a&gt; to your WebApplication constructor and override the onException method.&lt;/p&gt; &lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f6960020-1bfb-46e4-a281-f0b32439807d&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;public class TestApplication extends WebApplication {&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Constructor&lt;br /&gt;   */&lt;br /&gt;  public TestApplication() {&lt;br /&gt;	  &lt;br /&gt;	  // In case of unhandled exception redirect it to a custom page&lt;br /&gt;	  this.getRequestCycleListeners().add(new AbstractRequestCycleListener() {&lt;br /&gt;		  @Override&lt;br /&gt;	      public IRequestHandler onException(RequestCycle cycle, Exception e) {&lt;br /&gt;			  return new RenderPageRequestHandler(new PageProvider(new ExceptionPage(e)));&lt;br /&gt;		  }&lt;br /&gt;	  });&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public Class&amp;lt;HomePage&amp;gt; getHomePage() {&lt;br /&gt;    return HomePage.class;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;2) Prepare the exception page, in my case I would like to fill out a form automatically with all exception information's (like full stacktrace) and let the user submit it.&lt;br&gt;&lt;br&gt;My ExceptionPage.java looks like this:&lt;br&gt;&lt;br /&gt;&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:812469c5-0cb0-4c63-8c15-c81123a09de7:b8f1f803-784f-4269-9e92-8c927505bd75&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public class ExceptionPage extends WebPage {&lt;br /&gt;&lt;br /&gt;	private static final long serialVersionUID = 593717625018028083L;&lt;br /&gt;&lt;br /&gt;	// My model for the form data&lt;br /&gt;	FormExceptionProvider formProvider;&lt;br /&gt;	&lt;br /&gt;	// Mapped wicket:id &lt;br /&gt;	private static final String wicketIdformSendException=&quot;formSendException&quot;;&lt;br /&gt;	private static final String wicketIdformTarget=&quot;formTarget&quot;;&lt;br /&gt;	private static final String wicketIdformComments=&quot;formComments&quot;;&lt;br /&gt;	private static final String wicketIdmessageDetails=&quot;messageDetails&quot;;&lt;br /&gt;	private static final String wicketIdsubmitForm=&quot;submitForm&quot;;&lt;br /&gt;&lt;br /&gt;	public ExceptionPage(Exception e) {&lt;br /&gt;		formProvider = new FormExceptionProvider();&lt;br /&gt;		formProvider.setTarget(&quot;Administrator&quot;);&lt;br /&gt;		formProvider.setComments(&quot;Please provide here any useful information...&quot;);&lt;br /&gt;		StringBuilder sb = new StringBuilder();&lt;br /&gt;		sb.append(e.getClass().toString()+&quot;\n&quot;);&lt;br /&gt;		// Get the full stacktrace from exception and append to the string&lt;br /&gt;		// Thanks to ripper234 (ref: http://stackoverflow.com/questions/1292858/getting-full-string-stack-trace-including-inner-exception)&lt;br /&gt;		sb.append(joinStackTrace(e));&lt;br /&gt;		// Add fullstacktrace as a form's field (readonly)&lt;br /&gt;		formProvider.setAdditional(sb.toString());&lt;br /&gt;		add(makeExceptionForm());&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	private Form&amp;lt;?&amp;gt; makeExceptionForm() {&lt;br /&gt;&lt;br /&gt;		Form&amp;lt;?&amp;gt; form = new Form&amp;lt;Void&amp;gt;(wicketIdformSendException);&lt;br /&gt;&lt;br /&gt;		FormComponent&amp;lt;?&amp;gt; tfTarget = new TextField&amp;lt;String&amp;gt;(&lt;br /&gt;				wicketIdformTarget, new PropertyModel&amp;lt;String&amp;gt;(&lt;br /&gt;						formProvider, &quot;target&quot;));&lt;br /&gt;		form.add(tfTarget);&lt;br /&gt;	&lt;br /&gt;		FormComponent&amp;lt;?&amp;gt; taComments = new TextArea&amp;lt;String&amp;gt;(wicketIdformComments, new PropertyModel&amp;lt;String&amp;gt;(formProvider, &quot;comments&quot;));&lt;br /&gt;		form.add(taComments);&lt;br /&gt;&lt;br /&gt;		FormComponent&amp;lt;?&amp;gt; taAdditional = new TextArea&amp;lt;String&amp;gt;(wicketIdmessageDetails, new PropertyModel&amp;lt;String&amp;gt;(formProvider, &quot;additional&quot;));&lt;br /&gt;		form.add(taAdditional);&lt;br /&gt;&lt;br /&gt;		form.add(new Button(wicketIdsubmitForm) {&lt;br /&gt;			private static final long serialVersionUID = -3553700128326728526L;&lt;br /&gt;&lt;br /&gt;			@Override&lt;br /&gt;			public void onSubmit() {&lt;br /&gt;				/* &lt;br /&gt;				 * Do your stuff here (i.e. Send Email)&lt;br /&gt;				 */&lt;br /&gt;			}&lt;br /&gt;		});&lt;br /&gt;		return form;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	public static String joinStackTrace(Throwable e) {&lt;br /&gt;	    StringWriter writer = null;&lt;br /&gt;	    try {&lt;br /&gt;	        writer = new StringWriter();&lt;br /&gt;	        joinStackTrace(e, writer);&lt;br /&gt;	        return writer.toString();&lt;br /&gt;	    }&lt;br /&gt;	    finally {&lt;br /&gt;	        if (writer != null)&lt;br /&gt;	            try {&lt;br /&gt;	                writer.close();&lt;br /&gt;	            } catch (IOException e1) {&lt;br /&gt;	                // ignore&lt;br /&gt;	            }&lt;br /&gt;	    }&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public static void joinStackTrace(Throwable e, StringWriter writer) {&lt;br /&gt;	    PrintWriter printer = null;&lt;br /&gt;	    try {&lt;br /&gt;	        printer = new PrintWriter(writer);&lt;br /&gt;&lt;br /&gt;	        while (e != null) {&lt;br /&gt;&lt;br /&gt;	            printer.println(e);&lt;br /&gt;	            StackTraceElement[] trace = e.getStackTrace();&lt;br /&gt;	            for (int i = 0; i &amp;lt; trace.length; i++)&lt;br /&gt;	                printer.println(&quot;\tat &quot; + trace[i]);&lt;br /&gt;&lt;br /&gt;	            e = e.getCause();&lt;br /&gt;	            if (e != null)&lt;br /&gt;	                printer.println(&quot;Caused by:\r\n&quot;);&lt;br /&gt;	        }&lt;br /&gt;	    }&lt;br /&gt;	    finally {&lt;br /&gt;	        if (printer != null)&lt;br /&gt;	            printer.close();&lt;br /&gt;	    }&lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;And the related HTML code:&lt;br&gt;&lt;br /&gt;&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:812469c5-0cb0-4c63-8c15-c81123a09de7:fcd8cad9-c155-47d9-99be-286115ef9113&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;pre name=&quot;code&quot; class=&quot;xml&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Exception Page&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;form wicket:id=&quot;formSendException&quot;&amp;gt;&lt;br /&gt;	&amp;lt;table&amp;gt;&lt;br /&gt;	  &amp;lt;tr&amp;gt;&lt;br /&gt;	    &amp;lt;td width=&quot;100&quot; align=&quot;right&quot; &amp;gt;Target&amp;lt;/td&amp;gt;&lt;br /&gt;	    &amp;lt;td&amp;gt;&amp;lt;input wicket:id=&quot;formTarget&quot; type=&quot;text&quot; size=&quot;20&quot; value=&quot;admin@unizh.ch&quot;  readonly=&quot;readonly&quot;/&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;	  &amp;lt;/tr&amp;gt;&lt;br /&gt;	  &amp;lt;tr&amp;gt;&lt;br /&gt;	   &amp;lt;td width=&quot;100&quot; align=&quot;right&quot; &amp;gt;Comments:&amp;lt;/td&amp;gt;&lt;br /&gt;	    &amp;lt;td&amp;gt;&amp;lt;textarea wicket:id=&quot;formComments&quot; rows=&quot;10&quot; cols=&quot;50&quot;&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;	  &amp;lt;/tr&amp;gt;&lt;br /&gt;	  &amp;lt;tr&amp;gt;&lt;br /&gt;	   &amp;lt;td width=&quot;100&quot; align=&quot;right&quot; &amp;gt;Additional:&amp;lt;/td&amp;gt;&lt;br /&gt;	   &amp;lt;td&amp;gt;&amp;lt;textarea wicket:id=&quot;messageDetails&quot; rows=&quot;20&quot; cols=&quot;100&quot; readonly=&quot;readonly&quot;&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;	   &amp;lt;/tr&amp;gt;&lt;br /&gt;	  &amp;lt;tr&amp;gt;&lt;br /&gt;	    &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt;&lt;br /&gt;	    &amp;lt;td align=&quot;left&quot;&amp;gt;&amp;lt;button wicket:id=&quot;submitForm&quot; type=&quot;submit&quot;&amp;gt;Submit Report&amp;lt;/button&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;	    &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt;&lt;br /&gt;	  &amp;lt;/tr&amp;gt;&lt;br /&gt;	&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;I didn’t cared much (yet) about the UI, I was just looking to build something quick and useful for submitting error reports. An example of the resulting page is shown here:&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://lh4.ggpht.com/-RZyfpT4m5UE/TvJlA5U2rjI/AAAAAAAAA-E/wY81yEAviZA/s1600-h/image%25255B4%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://lh3.ggpht.com/-aZhklHv9U2c/TvJlBoPG9wI/AAAAAAAAA-M/MR4WqRcgUHk/image_thumb%25255B2%25255D.png?imgmax=800&quot; width=&quot;603&quot; height=&quot;380&quot;&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;Hope it helps! &lt;/p&gt;  </description>
        <pubDate>Wed, 21 Dec 2011 23:02:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2011/12/21/wicket-15custom-exception-page.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2011/12/21/wicket-15custom-exception-page.html</guid>
        
        <category>java wicket web tomcat programming coding jee</category>
        
        
      </item>
    
      <item>
        <title>Having Fun with IR Consumer Remotes</title>
        <description>I took a couple of evening to look how TV remotes works and how can we simply interface them with our PC (with minimal hw requirements) and I’d like to share the results with you.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;First of all we will need a way to capture those infrared signal coming from our remotes, the easiest way (and cheapest) is to build a small receiver with a photo-diode. (on the sides you see the electric diagram and my test bench)&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://lh5.ggpht.com/-jo1WQiEvx3k/TfY9IVoV3hI/AAAAAAAAApI/Z42bf7gpv-M/s1600-h/clip_image002%25255B18%25255D.gif&quot;&gt;&lt;img align=&quot;left&quot; alt=&quot;receiver_diagram&quot; border=&quot;0&quot; height=&quot;137&quot; src=&quot;http://lh6.ggpht.com/-Rcfqhve9qKk/TfYzYV1W5LI/AAAAAAAAApM/0LnGoDKMrs0/clip_image002_thumb%25255B18%25255D.gif?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin: 0px 25px 0px 0px;&quot; title=&quot;receiver_diagram&quot; width=&quot;425&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://lh3.ggpht.com/-mx5jRqzD_Jc/TfYz2WMzOdI/AAAAAAAAApQ/fDQ-sITey2k/s1600-h/image%25255B33%25255D.png&quot;&gt;&lt;img align=&quot;left&quot; alt=&quot;image&quot; border=&quot;0&quot; height=&quot;137&quot; src=&quot;http://lh5.ggpht.com/-9BoHYBmDacM/TfYz9_s3JBI/AAAAAAAAApU/oEUwfB1MC90/image_thumb%25255B27%25255D.png?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin: 0px;&quot; title=&quot;image&quot; width=&quot;185&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now that we built our receiver we can test if it’s working like it should. I used &lt;b&gt;Audacity &lt;/b&gt;(a Linux audio multifunctional tool) to analyze the signal coming from the photodiode. The results:&lt;br /&gt;&lt;a href=&quot;http://lh5.ggpht.com/-BK33eCSMsVk/TfY0AiM4_aI/AAAAAAAAApY/zefwq8oqxfE/s1600-h/clip_image0026%25255B8%25255D.gif&quot;&gt;&lt;img align=&quot;left&quot; alt=&quot;clip_image002[6]&quot; border=&quot;0&quot; height=&quot;274&quot; src=&quot;http://lh6.ggpht.com/-qUxYRSulrfk/TfY0E1Gs4CI/AAAAAAAAApc/7coS3X1qTME/clip_image0026_thumb%25255B8%25255D.gif?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin-left: 0px; margin-right: 0px;&quot; title=&quot;clip_image002[6]&quot; width=&quot;626&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now we have a signal composed by some waves, great, let’s try to explain how &lt;b&gt;CIR &lt;/b&gt;(Consumer infrared) works.&lt;br /&gt;First of all, quite each brand has his own solution, so we can’t really generalize the discussion. Anyway the most used is the &lt;b&gt;RC5&lt;/b&gt; from Philips. As you can remember, the lesser the frequency the the fair signal can get. The modulation (how you encode your data on the wave) used by fast every brand is called Amplitude-Shift Keying (ASK).&lt;br /&gt;&lt;a href=&quot;http://lh4.ggpht.com/-MsVSHLUwZKA/TfY0IYvwtsI/AAAAAAAAAos/b38eS9k4bns/s1600-h/clip_image00276.jpg&quot;&gt;&lt;img align=&quot;right&quot; alt=&quot;clip_image002[7]&quot; border=&quot;0&quot; height=&quot;195&quot; src=&quot;http://lh5.ggpht.com/-wpqwgsYzruI/TfY0NBoCxWI/AAAAAAAAAow/shP7lMvJHkA/clip_image0027_thumb4.jpg?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; margin: 0px 5px 0px 0px;&quot; title=&quot;clip_image002[7]&quot; width=&quot;471&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;RC5 Structure:&lt;/b&gt;&lt;br /&gt;Each frame is composed by:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;2 start bits which are used by the AGC (Automatic Gain Controller) from the receiver (TV)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;1 toggle bit which varies every time a button is pressed&lt;br /&gt;&lt;/li&gt;&lt;li&gt;5 bits which represents the device address&lt;br /&gt;&lt;/li&gt;&lt;li&gt;6 bits with the command &lt;/li&gt;&lt;/ol&gt;Bits are encoded with Manchester format, for this reason a logical zero is in practice a 1-0 and a logical one (1) is a 0-1, you can probably better understand this by the following schema:&lt;br /&gt;&lt;img alt=&quot;clip_image002[9]&quot; border=&quot;0&quot; height=&quot;133&quot; src=&quot;http://lh3.ggpht.com/--BTAGZKEfHY/TfY0QKn0sFI/AAAAAAAAAo0/0ZkfvsWkbi0/clip_image0029_thumb4.jpg?imgmax=800&quot; style=&quot;border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;&quot; title=&quot;clip_image002[9]&quot; width=&quot;602&quot; /&gt;&lt;br /&gt;Now that we have a minimal understanding of the ground let’s try code something with this. First of all we have to access our audio mic line (where we connected our fancy IR receiver) and prepare a buffer that our system will fill up with sampled data. Fortunately Linux provide a simple way to capture data from your soundcard.&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot; style=&quot;background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;&quot;&gt;&lt;div id=&quot;codeSnippet&quot; style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum1&quot; style=&quot;color: #606060;&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; init_audio(&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum2&quot; style=&quot;color: #606060;&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum3&quot; style=&quot;color: #606060;&quot;&gt;   3:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; rc;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum4&quot; style=&quot;color: #606060;&quot;&gt;   4:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; val, dir;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum5&quot; style=&quot;color: #606060;&quot;&gt;   5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum6&quot; style=&quot;color: #606060;&quot;&gt;   6:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Open PCM device for capture &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum7&quot; style=&quot;color: #606060;&quot;&gt;   7:&lt;/span&gt;     rc = snd_pcm_open(&amp;amp;handle, &lt;span style=&quot;color: #006080;&quot;&gt;&quot;default&quot;&lt;/span&gt;,  SND_PCM_STREAM_CAPTURE, 0);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum8&quot; style=&quot;color: #606060;&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (rc &amp;lt; 0) {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum9&quot; style=&quot;color: #606060;&quot;&gt;   9:&lt;/span&gt;         fprintf(stderr,&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum10&quot; style=&quot;color: #606060;&quot;&gt;  10:&lt;/span&gt;                 &lt;span style=&quot;color: #006080;&quot;&gt;&quot;Unable to open pcm device: %s\n&quot;&lt;/span&gt;,&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum11&quot; style=&quot;color: #606060;&quot;&gt;  11:&lt;/span&gt;                 snd_strerror(rc));&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum12&quot; style=&quot;color: #606060;&quot;&gt;  12:&lt;/span&gt;         exit(1);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum13&quot; style=&quot;color: #606060;&quot;&gt;  13:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum14&quot; style=&quot;color: #606060;&quot;&gt;  14:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum15&quot; style=&quot;color: #606060;&quot;&gt;  15:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Setting default samples &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum16&quot; style=&quot;color: #606060;&quot;&gt;  16:&lt;/span&gt;     snd_pcm_hw_params_alloca(&amp;amp;params);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum17&quot; style=&quot;color: #606060;&quot;&gt;  17:&lt;/span&gt;     snd_pcm_hw_params_any(handle, params);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum18&quot; style=&quot;color: #606060;&quot;&gt;  18:&lt;/span&gt;     snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum19&quot; style=&quot;color: #606060;&quot;&gt;  19:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum20&quot; style=&quot;color: #606060;&quot;&gt;  20:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// 8bits Signed samples &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum21&quot; style=&quot;color: #606060;&quot;&gt;  21:&lt;/span&gt;     snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S8);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum22&quot; style=&quot;color: #606060;&quot;&gt;  22:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum23&quot; style=&quot;color: #606060;&quot;&gt;  23:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Single channel (mono) &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum24&quot; style=&quot;color: #606060;&quot;&gt;  24:&lt;/span&gt;     snd_pcm_hw_params_set_channels(handle, params, 1);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum25&quot; style=&quot;color: #606060;&quot;&gt;  25:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum26&quot; style=&quot;color: #606060;&quot;&gt;  26:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Setting sampling rate &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum27&quot; style=&quot;color: #606060;&quot;&gt;  27:&lt;/span&gt;     val = SAMPLERATE;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum28&quot; style=&quot;color: #606060;&quot;&gt;  28:&lt;/span&gt;     snd_pcm_hw_params_set_rate_near(handle, params,&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum29&quot; style=&quot;color: #606060;&quot;&gt;  29:&lt;/span&gt;                                     &amp;amp;val, &amp;amp;dir);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum30&quot; style=&quot;color: #606060;&quot;&gt;  30:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum31&quot; style=&quot;color: #606060;&quot;&gt;  31:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Set period size to 300 frames. &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum32&quot; style=&quot;color: #606060;&quot;&gt;  32:&lt;/span&gt;     frames = 300;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum33&quot; style=&quot;color: #606060;&quot;&gt;  33:&lt;/span&gt;     snd_pcm_hw_params_set_period_size_near(handle,   params, &amp;amp;frames, &amp;amp;dir);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum34&quot; style=&quot;color: #606060;&quot;&gt;  34:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum35&quot; style=&quot;color: #606060;&quot;&gt;  35:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Write the parameters to the driver &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum36&quot; style=&quot;color: #606060;&quot;&gt;  36:&lt;/span&gt;     rc = snd_pcm_hw_params(handle, params);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum37&quot; style=&quot;color: #606060;&quot;&gt;  37:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum38&quot; style=&quot;color: #606060;&quot;&gt;  38:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (rc &amp;lt; 0) {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum39&quot; style=&quot;color: #606060;&quot;&gt;  39:&lt;/span&gt;         fprintf(stderr,&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum40&quot; style=&quot;color: #606060;&quot;&gt;  40:&lt;/span&gt;                 &lt;span style=&quot;color: #006080;&quot;&gt;&quot;Unable to set hw parameters: %s\n&quot;&lt;/span&gt;,&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum41&quot; style=&quot;color: #606060;&quot;&gt;  41:&lt;/span&gt;                 snd_strerror(rc));&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum42&quot; style=&quot;color: #606060;&quot;&gt;  42:&lt;/span&gt;         exit(1);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum43&quot; style=&quot;color: #606060;&quot;&gt;  43:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum44&quot; style=&quot;color: #606060;&quot;&gt;  44:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum45&quot; style=&quot;color: #606060;&quot;&gt;  45:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Use a buffer large enough to hold one period &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum46&quot; style=&quot;color: #606060;&quot;&gt;  46:&lt;/span&gt;     snd_pcm_hw_params_get_period_size(params,  &amp;amp;frames, &amp;amp;dir);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum47&quot; style=&quot;color: #606060;&quot;&gt;  47:&lt;/span&gt;     size = frames * 1; &lt;span style=&quot;color: green;&quot;&gt;// 1 bytes/sample, 1 channels &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum48&quot; style=&quot;color: #606060;&quot;&gt;  48:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum49&quot; style=&quot;color: #606060;&quot;&gt;  49:&lt;/span&gt;     buffer = (&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; *) malloc(size);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum50&quot; style=&quot;color: #606060;&quot;&gt;  50:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum51&quot; style=&quot;color: #606060;&quot;&gt;  51:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// We want to loop for 2 seconds &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum52&quot; style=&quot;color: #606060;&quot;&gt;  52:&lt;/span&gt;     snd_pcm_hw_params_get_period_time(params,&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum53&quot; style=&quot;color: #606060;&quot;&gt;  53:&lt;/span&gt;                                       &amp;amp;val, &amp;amp;dir);    &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum54&quot; style=&quot;color: #606060;&quot;&gt;  54:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum55&quot; style=&quot;color: #606060;&quot;&gt;  55:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Audio initialized!\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum56&quot; style=&quot;color: #606060;&quot;&gt;  56:&lt;/span&gt; }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum57&quot; style=&quot;color: #606060;&quot;&gt;  57:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum58&quot; style=&quot;color: #606060;&quot;&gt;  58:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;// listen function used as a dedicated thread (pthread_create())&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum59&quot; style=&quot;color: #606060;&quot;&gt;  59:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; *listen_function(&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum60&quot; style=&quot;color: #606060;&quot;&gt;  60:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum61&quot; style=&quot;color: #606060;&quot;&gt;  61:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; rc;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum62&quot; style=&quot;color: #606060;&quot;&gt;  62:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; j;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum63&quot; style=&quot;color: #606060;&quot;&gt;  63:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum64&quot; style=&quot;color: #606060;&quot;&gt;  64:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt; (!QUIT) {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum65&quot; style=&quot;color: #606060;&quot;&gt;  65:&lt;/span&gt;         &lt;span style=&quot;color: green;&quot;&gt;// variable has been initialized in init_audio function&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum66&quot; style=&quot;color: #606060;&quot;&gt;  66:&lt;/span&gt;         rc = snd_pcm_readi(handle, buffer, frames); &lt;span style=&quot;color: green;&quot;&gt;// read &quot;frames&quot; from soundcard&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum67&quot; style=&quot;color: #606060;&quot;&gt;  67:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum68&quot; style=&quot;color: #606060;&quot;&gt;  68:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (rc == -EPIPE) {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum69&quot; style=&quot;color: #606060;&quot;&gt;  69:&lt;/span&gt;             &lt;span style=&quot;color: green;&quot;&gt;// EPIPE means overrun &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum70&quot; style=&quot;color: #606060;&quot;&gt;  70:&lt;/span&gt;             fprintf(stderr, &lt;span style=&quot;color: #006080;&quot;&gt;&quot;overrun occurred\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum71&quot; style=&quot;color: #606060;&quot;&gt;  71:&lt;/span&gt;             snd_pcm_prepare(handle);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum72&quot; style=&quot;color: #606060;&quot;&gt;  72:&lt;/span&gt;             &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum73&quot; style=&quot;color: #606060;&quot;&gt;  73:&lt;/span&gt;         } &lt;span style=&quot;color: blue;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (rc &amp;lt; 0) {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum74&quot; style=&quot;color: #606060;&quot;&gt;  74:&lt;/span&gt;             fprintf(stderr,    &lt;span style=&quot;color: #006080;&quot;&gt;&quot;error from read: %s\n&quot;&lt;/span&gt;, snd_strerror(rc));&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum75&quot; style=&quot;color: #606060;&quot;&gt;  75:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum76&quot; style=&quot;color: #606060;&quot;&gt;  76:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum77&quot; style=&quot;color: #606060;&quot;&gt;  77:&lt;/span&gt;         &lt;span style=&quot;color: green;&quot;&gt;// draw signal to stdout&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum78&quot; style=&quot;color: #606060;&quot;&gt;  78:&lt;/span&gt;         &lt;span style=&quot;color: green;&quot;&gt;//graph(buffer, rc);&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum79&quot; style=&quot;color: #606060;&quot;&gt;  79:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum80&quot; style=&quot;color: #606060;&quot;&gt;  80:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(DEBUG)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum81&quot; style=&quot;color: #606060;&quot;&gt;  81:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum82&quot; style=&quot;color: #606060;&quot;&gt;  82:&lt;/span&gt;             &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(j=0; j&amp;lt;rc;j++){&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum83&quot; style=&quot;color: #606060;&quot;&gt;  83:&lt;/span&gt;                 fprintf(fp, &lt;span style=&quot;color: #006080;&quot;&gt;&quot;%.1f\t%d\n&quot;&lt;/span&gt;, (&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)(delta*1000.0*(&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)counter), buffer[j]);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum84&quot; style=&quot;color: #606060;&quot;&gt;  84:&lt;/span&gt;                 counter++;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum85&quot; style=&quot;color: #606060;&quot;&gt;  85:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum86&quot; style=&quot;color: #606060;&quot;&gt;  86:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum87&quot; style=&quot;color: #606060;&quot;&gt;  87:&lt;/span&gt;         &lt;span style=&quot;color: green;&quot;&gt;// elaborate buffer&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum88&quot; style=&quot;color: #606060;&quot;&gt;  88:&lt;/span&gt;         computeDelta(buffer,rc);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum89&quot; style=&quot;color: #606060;&quot;&gt;  89:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum90&quot; style=&quot;color: #606060;&quot;&gt;  90:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;ListenThread terminated!\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum91&quot; style=&quot;color: #606060;&quot;&gt;  91:&lt;/span&gt;     closeAudio();&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum92&quot; style=&quot;color: #606060;&quot;&gt;  92:&lt;/span&gt;     pthread_exit(NULL);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum93&quot; style=&quot;color: #606060;&quot;&gt;  93:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;Next we need a simple algorithm to discriminate the length of the peaks we read from the photodiode:&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot; style=&quot;background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;&quot;&gt;&lt;div id=&quot;codeSnippet&quot; style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum1&quot; style=&quot;color: #606060;&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; SAMPLERATE 44100&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum2&quot; style=&quot;color: #606060;&quot;&gt;   2:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; DELTATHRESHOLD 0.5&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum3&quot; style=&quot;color: #606060;&quot;&gt;   3:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt; 1&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum4&quot; style=&quot;color: #606060;&quot;&gt;   4:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt; 0&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum5&quot; style=&quot;color: #606060;&quot;&gt;   5:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; THRESOLDLIMIT 6 &lt;span style=&quot;color: green;&quot;&gt;// Under this no calculation is made [range 1-127]&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum6&quot; style=&quot;color: #606060;&quot;&gt;   6:&lt;/span&gt; &lt;span style=&quot;color: #cc6633;&quot;&gt;#define&lt;/span&gt; DEBUG 0  &lt;span style=&quot;color: green;&quot;&gt;// If true: verbose output + sampled data written to logfile.txt&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum7&quot; style=&quot;color: #606060;&quot;&gt;   7:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum8&quot; style=&quot;color: #606060;&quot;&gt;   8:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; OVERTH=&lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt;; &lt;span style=&quot;color: green;&quot;&gt;// FLAG used to know if signal is below or above threshold&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum9&quot; style=&quot;color: #606060;&quot;&gt;   9:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; PEAK = &lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt;; &lt;span style=&quot;color: green;&quot;&gt;// FLAG used to let know when a peak occurred&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum10&quot; style=&quot;color: #606060;&quot;&gt;  10:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum11&quot; style=&quot;color: #606060;&quot;&gt;  11:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;/* delta is time (in milliseconds) between each sample */&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum12&quot; style=&quot;color: #606060;&quot;&gt;  12:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt; delta=(1.0/(&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)SAMPLERATE*1000);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum13&quot; style=&quot;color: #606060;&quot;&gt;  13:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum14&quot; style=&quot;color: #606060;&quot;&gt;  14:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; computeDelta(&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; *buffer, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; length)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum15&quot; style=&quot;color: #606060;&quot;&gt;  15:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum16&quot; style=&quot;color: #606060;&quot;&gt;  16:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; k,j;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum17&quot; style=&quot;color: #606060;&quot;&gt;  17:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; maxgraph= -100;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum18&quot; style=&quot;color: #606060;&quot;&gt;  18:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; threshold;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum19&quot; style=&quot;color: #606060;&quot;&gt;  19:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; temp;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum20&quot; style=&quot;color: #606060;&quot;&gt;  20:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum21&quot; style=&quot;color: #606060;&quot;&gt;  21:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; countstep=0;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum22&quot; style=&quot;color: #606060;&quot;&gt;  22:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt; dpeak; &lt;span style=&quot;color: green;&quot;&gt;// delta peak&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum23&quot; style=&quot;color: #606060;&quot;&gt;  23:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum24&quot; style=&quot;color: #606060;&quot;&gt;  24:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Find max value in sampled buffer&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum25&quot; style=&quot;color: #606060;&quot;&gt;  25:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(j=0; j&amp;lt;length;j++)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum26&quot; style=&quot;color: #606060;&quot;&gt;  26:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum27&quot; style=&quot;color: #606060;&quot;&gt;  27:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(buffer[j]&amp;gt;maxgraph)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum28&quot; style=&quot;color: #606060;&quot;&gt;  28:&lt;/span&gt;             maxgraph=buffer[j];&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum29&quot; style=&quot;color: #606060;&quot;&gt;  29:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum30&quot; style=&quot;color: #606060;&quot;&gt;  30:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum31&quot; style=&quot;color: #606060;&quot;&gt;  31:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Compute threshold against max value&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum32&quot; style=&quot;color: #606060;&quot;&gt;  32:&lt;/span&gt;     threshold = (&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;)((&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)maxgraph*(&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)DELTATHRESHOLD);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum33&quot; style=&quot;color: #606060;&quot;&gt;  33:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum34&quot; style=&quot;color: #606060;&quot;&gt;  34:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Check if signal rise above threshold&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum35&quot; style=&quot;color: #606060;&quot;&gt;  35:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(threshold &amp;gt; THRESOLDLIMIT)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum36&quot; style=&quot;color: #606060;&quot;&gt;  36:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum37&quot; style=&quot;color: #606060;&quot;&gt;  37:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(DEBUG) printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Threshold: %d [MAX: %d]\n&quot;&lt;/span&gt;, threshold, maxgraph);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum38&quot; style=&quot;color: #606060;&quot;&gt;  38:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(k=0;k&amp;lt;length;k++)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum39&quot; style=&quot;color: #606060;&quot;&gt;  39:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum40&quot; style=&quot;color: #606060;&quot;&gt;  40:&lt;/span&gt;             &lt;span style=&quot;color: green;&quot;&gt;// peak length&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum41&quot; style=&quot;color: #606060;&quot;&gt;  41:&lt;/span&gt;             dpeak = ((&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)(delta)*(&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;)countstep);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum42&quot; style=&quot;color: #606060;&quot;&gt;  42:&lt;/span&gt;             &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum43&quot; style=&quot;color: #606060;&quot;&gt;  43:&lt;/span&gt;             &lt;span style=&quot;color: green;&quot;&gt;// signal low (below 0) &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum44&quot; style=&quot;color: #606060;&quot;&gt;  44:&lt;/span&gt;             &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(buffer[k]&amp;gt;threshold &amp;amp;&amp;amp; OVERTH == &lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt;){&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum45&quot; style=&quot;color: #606060;&quot;&gt;  45:&lt;/span&gt;                     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(DEBUG) printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Low Peak length: %8.3f ms [Steps: %d][Value: %4d]\n&quot;&lt;/span&gt;, dpeak,countstep, buffer[k]);    &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum46&quot; style=&quot;color: #606060;&quot;&gt;  46:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// peak triggered when delta peak is longer than 0.4 ms                    &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum47&quot; style=&quot;color: #606060;&quot;&gt;  47:&lt;/span&gt;                     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(dpeak&amp;gt;0.4)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum48&quot; style=&quot;color: #606060;&quot;&gt;  48:&lt;/span&gt;                         PEAK=&lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt;;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum49&quot; style=&quot;color: #606060;&quot;&gt;  49:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// reset counterstep for next peak&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum50&quot; style=&quot;color: #606060;&quot;&gt;  50:&lt;/span&gt;                     countstep=0;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum51&quot; style=&quot;color: #606060;&quot;&gt;  51:&lt;/span&gt;                     countstep++;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum52&quot; style=&quot;color: #606060;&quot;&gt;  52:&lt;/span&gt;                     OVERTH = &lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt;;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum53&quot; style=&quot;color: #606060;&quot;&gt;  53:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum54&quot; style=&quot;color: #606060;&quot;&gt;  54:&lt;/span&gt;             &lt;span style=&quot;color: green;&quot;&gt;// signal high (above 0)&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum55&quot; style=&quot;color: #606060;&quot;&gt;  55:&lt;/span&gt;             &lt;span style=&quot;color: blue;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(buffer[k]&amp;lt;threshold &amp;amp;&amp;amp; OVERTH==&lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt;){&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum56&quot; style=&quot;color: #606060;&quot;&gt;  56:&lt;/span&gt;                     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(DEBUG) printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;High Peak length: %8.3lf ms [Steps: %d][Value: %4d]\n&quot;&lt;/span&gt;, dpeak,countstep, buffer[k]);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum57&quot; style=&quot;color: #606060;&quot;&gt;  57:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// peak triggered when delta peak is longer than 0.4 ms&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum58&quot; style=&quot;color: #606060;&quot;&gt;  58:&lt;/span&gt;                     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(dpeak&amp;gt;0.4)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum59&quot; style=&quot;color: #606060;&quot;&gt;  59:&lt;/span&gt;                         PEAK=&lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt;;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum60&quot; style=&quot;color: #606060;&quot;&gt;  60:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// reset counterstep for next peak&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum61&quot; style=&quot;color: #606060;&quot;&gt;  61:&lt;/span&gt;                     countstep=0;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum62&quot; style=&quot;color: #606060;&quot;&gt;  62:&lt;/span&gt;                     countstep++;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum63&quot; style=&quot;color: #606060;&quot;&gt;  63:&lt;/span&gt;                     OVERTH=&lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt;;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum64&quot; style=&quot;color: #606060;&quot;&gt;  64:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum65&quot; style=&quot;color: #606060;&quot;&gt;  65:&lt;/span&gt;             &lt;span style=&quot;color: green;&quot;&gt;// Peak detected&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum66&quot; style=&quot;color: #606060;&quot;&gt;  66:&lt;/span&gt;             &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(PEAK==&lt;span style=&quot;color: blue;&quot;&gt;TRUE&lt;/span&gt;)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum67&quot; style=&quot;color: #606060;&quot;&gt;  67:&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum68&quot; style=&quot;color: #606060;&quot;&gt;  68:&lt;/span&gt;                 &lt;span style=&quot;color: green;&quot;&gt;// I know the peak length should be within 800 us - 1.8 ms&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum69&quot; style=&quot;color: #606060;&quot;&gt;  69:&lt;/span&gt;                 &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(dpeak&amp;lt;2.0)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum70&quot; style=&quot;color: #606060;&quot;&gt;  70:&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum71&quot; style=&quot;color: #606060;&quot;&gt;  71:&lt;/span&gt;                     temp=(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;)dpeak;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum72&quot; style=&quot;color: #606060;&quot;&gt;  72:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// temp is one if peak is longer than 1 ms or zero if shorter&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum73&quot; style=&quot;color: #606060;&quot;&gt;  73:&lt;/span&gt;                     cmd = (cmd&amp;lt;&amp;lt;1) | temp;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum74&quot; style=&quot;color: #606060;&quot;&gt;  74:&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum75&quot; style=&quot;color: #606060;&quot;&gt;  75:&lt;/span&gt;                 &lt;span style=&quot;color: blue;&quot;&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum76&quot; style=&quot;color: #606060;&quot;&gt;  76:&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum77&quot; style=&quot;color: #606060;&quot;&gt;  77:&lt;/span&gt;                     &lt;span style=&quot;color: green;&quot;&gt;// command ready&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum78&quot; style=&quot;color: #606060;&quot;&gt;  78:&lt;/span&gt;                     &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(cmd &amp;gt; 0)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum79&quot; style=&quot;color: #606060;&quot;&gt;  79:&lt;/span&gt;                     {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum80&quot; style=&quot;color: #606060;&quot;&gt;  80:&lt;/span&gt;                         printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;CMD: %3d\n&quot;&lt;/span&gt;, cmd);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum81&quot; style=&quot;color: #606060;&quot;&gt;  81:&lt;/span&gt;                         cmd=0;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum82&quot; style=&quot;color: #606060;&quot;&gt;  82:&lt;/span&gt;                     }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum83&quot; style=&quot;color: #606060;&quot;&gt;  83:&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum84&quot; style=&quot;color: #606060;&quot;&gt;  84:&lt;/span&gt;                 PEAK = &lt;span style=&quot;color: blue;&quot;&gt;FALSE&lt;/span&gt;;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum85&quot; style=&quot;color: #606060;&quot;&gt;  85:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum86&quot; style=&quot;color: #606060;&quot;&gt;  86:&lt;/span&gt;             countstep++;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum87&quot; style=&quot;color: #606060;&quot;&gt;  87:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum88&quot; style=&quot;color: #606060;&quot;&gt;  88:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum89&quot; style=&quot;color: #606060;&quot;&gt;  89:&lt;/span&gt; }    &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;a href=&quot;http://lh3.ggpht.com/-pt9_GHnig0U/TfZStcj3nmI/AAAAAAAAApo/RkR7_nYKXkc/s1600-h/image%25255B40%25255D.png&quot;&gt;&lt;img alt=&quot;image&quot; border=&quot;0&quot; height=&quot;450&quot; src=&quot;http://lh3.ggpht.com/-GiKXK6GnmAY/TfZSt8s9aUI/AAAAAAAAAps/Ko_I4kC8FkY/image_thumb%25255B34%25255D.png?imgmax=800&quot; style=&quot;border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: block; float: none; margin: 0px auto;&quot; title=&quot;image&quot; width=&quot;571&quot; /&gt;&lt;/a&gt; The results are shown in the picture. Each peak is measured to see how long is it and&amp;nbsp; was that the shortest peaks (shrinked) have a length of around &lt;b&gt;900 us&lt;/b&gt; and the long peak a length of about &lt;b&gt;1.8 ms&lt;/b&gt;.&lt;br /&gt;A simple proof of this values is done by comparing the graph (gnuplot) generated on the raw sampled data and the program result. If we compare the peak type (long/short) and the peak length we will see a “perfect” match.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://lh3.ggpht.com/-AQ43axVglMg/TfY0c_1BYXI/AAAAAAAAApw/efemEWyzTWA/s1600-h/image%25255B37%25255D.png&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img align=&quot;right&quot; alt=&quot;image&quot; border=&quot;0&quot; height=&quot;518&quot; src=&quot;http://lh3.ggpht.com/-gfBH79iunn0/TfY0nCUwURI/AAAAAAAAAp0/d31hRQSadIw/image_thumb%25255B31%25255D.png?imgmax=800&quot; style=&quot;border-color: -moz-use-text-color; border-style: none; border-width: 0px; display: inline; margin: 0px 0px 0px 5px;&quot; title=&quot;image&quot; width=&quot;393&quot; /&gt;&lt;/a&gt;Now we have do identify and differentiate the remote’s command (which button pressed). &lt;br /&gt;A simple way to do this is using some bitwise operator. Let’s say that if the peak is longer than 1 ms we insert a one (bit) into our command (8 bit uchar), if shorter we insert a zero (bit).&lt;br /&gt;&lt;br /&gt;If you take a look in the code above you will see that there is no length check when shifting and inserting new bit into command. therefore the algorithm will take only the last 8 peaks into the command (the other will be “overwritten”).&lt;br /&gt;&lt;br /&gt;Trying the program will results in a series of command number that are associated to remote buttons. In the screenshot is shown the result of pressing two different buttons: channel – (CMD: 194), and volume + (CMD: 57) from a Philips remote. &lt;br /&gt;The algorithm needs of course some more improvements since as you see the commands some time are wrong (0, 98, 25).&lt;br /&gt;&lt;br /&gt;Now that we have a list of commands associated with buttons we could move the mouse respectfully with the remote you’re using:&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot; style=&quot;background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; height: 210px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.54%;&quot;&gt;&lt;div id=&quot;codeSnippet&quot; style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum1&quot; style=&quot;color: #606060;&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(cmd==62 || cmd==254)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum2&quot; style=&quot;color: #606060;&quot;&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum3&quot; style=&quot;color: #606060;&quot;&gt;   3:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Move 10 px left\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum4&quot; style=&quot;color: #606060;&quot;&gt;   4:&lt;/span&gt;     mouse_move(-10,0);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum5&quot; style=&quot;color: #606060;&quot;&gt;   5:&lt;/span&gt; }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum6&quot; style=&quot;color: #606060;&quot;&gt;   6:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(cmd==57 || cmd==249)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum7&quot; style=&quot;color: #606060;&quot;&gt;   7:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum8&quot; style=&quot;color: #606060;&quot;&gt;   8:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Move 10 px right\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum9&quot; style=&quot;color: #606060;&quot;&gt;   9:&lt;/span&gt;     mouse_move(10,0);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum10&quot; style=&quot;color: #606060;&quot;&gt;  10:&lt;/span&gt; }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum11&quot; style=&quot;color: #606060;&quot;&gt;  11:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(cmd==194)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum12&quot; style=&quot;color: #606060;&quot;&gt;  12:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum13&quot; style=&quot;color: #606060;&quot;&gt;  13:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Move 10 px down\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum14&quot; style=&quot;color: #606060;&quot;&gt;  14:&lt;/span&gt;     mouse_move(0,10);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum15&quot; style=&quot;color: #606060;&quot;&gt;  15:&lt;/span&gt; }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum16&quot; style=&quot;color: #606060;&quot;&gt;  16:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(cmd==192)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum17&quot; style=&quot;color: #606060;&quot;&gt;  17:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum18&quot; style=&quot;color: #606060;&quot;&gt;  18:&lt;/span&gt;     printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;Move 10 px up\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum19&quot; style=&quot;color: #606060;&quot;&gt;  19:&lt;/span&gt;     mouse_move(0,-10);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum20&quot; style=&quot;color: #606060;&quot;&gt;  20:&lt;/span&gt; }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum21&quot; style=&quot;color: #606060;&quot;&gt;  21:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum22&quot; style=&quot;color: #606060;&quot;&gt;  22:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;// Move mouse &lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum23&quot; style=&quot;color: #606060;&quot;&gt;  23:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;// Current Position + x / y&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum24&quot; style=&quot;color: #606060;&quot;&gt;  24:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; mouse_move(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; x, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; y)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum25&quot; style=&quot;color: #606060;&quot;&gt;  25:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum26&quot; style=&quot;color: #606060;&quot;&gt;  26:&lt;/span&gt;         Display *displayMain = XOpenDisplay(NULL);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum27&quot; style=&quot;color: #606060;&quot;&gt;  27:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum28&quot; style=&quot;color: #606060;&quot;&gt;  28:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(displayMain == NULL)&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum29&quot; style=&quot;color: #606060;&quot;&gt;  29:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum30&quot; style=&quot;color: #606060;&quot;&gt;  30:&lt;/span&gt;                 fprintf(stderr, &lt;span style=&quot;color: #006080;&quot;&gt;&quot;Error Opening Display!\n&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum31&quot; style=&quot;color: #606060;&quot;&gt;  31:&lt;/span&gt;                 exit(EXIT_FAILURE);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum32&quot; style=&quot;color: #606060;&quot;&gt;  32:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum33&quot; style=&quot;color: #606060;&quot;&gt;  33:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum34&quot; style=&quot;color: #606060;&quot;&gt;  34:&lt;/span&gt;         XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum35&quot; style=&quot;color: #606060;&quot;&gt;  35:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum36&quot; style=&quot;color: #606060;&quot;&gt;  36:&lt;/span&gt;         XCloseDisplay(displayMain);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum37&quot; style=&quot;color: #606060;&quot;&gt;  37:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;Maybe it comes handy sometime to draw the signal direct into the console output, for this reason I’ve created a simple ascii graph function to display my buffer (sampled data) content.&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot; style=&quot;background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; height: 210px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.71%;&quot;&gt;&lt;div id=&quot;codeSnippet&quot; style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum1&quot; style=&quot;color: #606060;&quot;&gt;   1:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;// Simple ascii graph function&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum2&quot; style=&quot;color: #606060;&quot;&gt;   2:&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;// Draw char buffer to stdout using printf&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum3&quot; style=&quot;color: #606060;&quot;&gt;   3:&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; graph(&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; *buffer, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; length)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum4&quot; style=&quot;color: #606060;&quot;&gt;   4:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum5&quot; style=&quot;color: #606060;&quot;&gt;   5:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum6&quot; style=&quot;color: #606060;&quot;&gt;   6:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; iy;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum7&quot; style=&quot;color: #606060;&quot;&gt;   7:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; mingraph=126, maxgraph=-126;    &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum8&quot; style=&quot;color: #606060;&quot;&gt;   8:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; temp;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum9&quot; style=&quot;color: #606060;&quot;&gt;   9:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; val;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum10&quot; style=&quot;color: #606060;&quot;&gt;  10:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; i,j,k;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum11&quot; style=&quot;color: #606060;&quot;&gt;  11:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum12&quot; style=&quot;color: #606060;&quot;&gt;  12:&lt;/span&gt;     &lt;span style=&quot;color: green;&quot;&gt;// Find max / min&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum13&quot; style=&quot;color: #606060;&quot;&gt;  13:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(j=0; j&amp;lt;length;j++)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum14&quot; style=&quot;color: #606060;&quot;&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum15&quot; style=&quot;color: #606060;&quot;&gt;  15:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(buffer[j]&amp;gt;maxgraph)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum16&quot; style=&quot;color: #606060;&quot;&gt;  16:&lt;/span&gt;             maxgraph=buffer[j];&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum17&quot; style=&quot;color: #606060;&quot;&gt;  17:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(buffer[j]&amp;lt;mingraph)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum18&quot; style=&quot;color: #606060;&quot;&gt;  18:&lt;/span&gt;             mingraph=buffer[j];&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum19&quot; style=&quot;color: #606060;&quot;&gt;  19:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum20&quot; style=&quot;color: #606060;&quot;&gt;  20:&lt;/span&gt;     &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum21&quot; style=&quot;color: #606060;&quot;&gt;  21:&lt;/span&gt;     &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(k=0;k&amp;lt;length;k++)&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum22&quot; style=&quot;color: #606060;&quot;&gt;  22:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum23&quot; style=&quot;color: #606060;&quot;&gt;  23:&lt;/span&gt;         val=buffer[k];&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum24&quot; style=&quot;color: #606060;&quot;&gt;  24:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(mingraph&amp;lt;0) &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum25&quot; style=&quot;color: #606060;&quot;&gt;  25:&lt;/span&gt;             val-=mingraph;&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum26&quot; style=&quot;color: #606060;&quot;&gt;  26:&lt;/span&gt;         &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum27&quot; style=&quot;color: #606060;&quot;&gt;  27:&lt;/span&gt;         iy=(val*maxgraph)/(maxgraph-mingraph);&lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum28&quot; style=&quot;color: #606060;&quot;&gt;  28:&lt;/span&gt;         printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;%4d &quot;&lt;/span&gt;, val);&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum29&quot; style=&quot;color: #606060;&quot;&gt;  29:&lt;/span&gt;         &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt;(i=0;i&amp;lt;iy;i++) &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum30&quot; style=&quot;color: #606060;&quot;&gt;  30:&lt;/span&gt;             printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot; &quot;&lt;/span&gt;);         &lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum31&quot; style=&quot;color: #606060;&quot;&gt;  31:&lt;/span&gt;         printf(&lt;span style=&quot;color: #006080;&quot;&gt;&quot;*\n&quot;&lt;/span&gt;); &lt;/pre&gt;&lt;pre style=&quot;background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum32&quot; style=&quot;color: #606060;&quot;&gt;  32:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style=&quot;background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;&quot;&gt;&lt;span id=&quot;lnum33&quot; style=&quot;color: #606060;&quot;&gt;  33:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;That’s it.</description>
        <pubDate>Mon, 13 Jun 2011 16:02:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2011/06/13/having-fun-with-ir-consumer-remotes.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2011/06/13/having-fun-with-ir-consumer-remotes.html</guid>
        
        <category>cir</category>
        
        <category>programming</category>
        
        <category>linux</category>
        
        <category>infrared</category>
        
        
      </item>
    
      <item>
        <title>Automating an Astronomical Observatory</title>
        <description>&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: right; margin-left: 1em; text-align: right;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/_cXYmdFluLpw/TP6iwVpfzvI/AAAAAAAAAmA/-lCdTAgkRa4/s1600/test_benchmark.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;240&quot; src=&quot;http://4.bp.blogspot.com/_cXYmdFluLpw/TP6iwVpfzvI/AAAAAAAAAmA/-lCdTAgkRa4/s320/test_benchmark.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;First tryouts with the Siemens S7-200 (and Lego) &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;My Summer, this year, was kind of an hard one. I was indeed occupied by my bachelor thesis which consisted in the automation of an astronomical observatory in the Southern Switzerland. The observatory is owned by an association of amateur astronomers called &quot;LePleiadi&quot;. The biggest challenge of this project is tied to the observatory location (at 1600 meters, quite unreachable during winter) therefore the observatory has to take care of himself alone, especially in the winter season.&lt;br /&gt;&lt;br /&gt;You can learn more about the observatory &lt;a href=&quot;http://www.lepleiadi.ch/default.asp?inc=./lema.asp&quot;&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: right;&quot;&gt;&lt;/div&gt;&lt;a href=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6uK06E_iI/AAAAAAAAAmQ/EnvncLowH98/s1600/osservatorio.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;133&quot; src=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6uK06E_iI/AAAAAAAAAmQ/EnvncLowH98/s200/osservatorio.jpg&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;I've spent about 30 nights on the top of a mountain (can't count the ones in 'lowland')&amp;nbsp; working on this project, it was really fun and exciting. My work mostly consisted in taking care of all security aspects (to avoid damages) with a (limited) Siemens &lt;a href=&quot;http://www.automation.siemens.com/mcms/programmable-logic-controller/en/simatic-s7-controller/s7-200/Pages/Default.aspx&quot;&gt;PLC S7-200 &lt;/a&gt;which I'm now sure that was a too lower series for this kind of purpose. (unfortunately discovered too late)&lt;br /&gt;&lt;br /&gt;One important feature of this solution is that every  possible alarms that happens are signaled by the PLC via email (and  maybe later by SMS). For most common alarms (like sky becoming cloudy)  the PLC take care of it by himself. There is anyway some situation where  the human action (remote or in extreme case in place) is needed. &lt;br /&gt;&lt;br /&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; text-align: left;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6hPYqg97I/AAAAAAAAAls/Zm64MAMiL4A/s1600/indoor_vga.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;240&quot; src=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6hPYqg97I/AAAAAAAAAls/Zm64MAMiL4A/s320/indoor_vga.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Inside view of the observatory&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;The electronics of the observatory has been completely reviewed and a lot of sensors were added.&lt;br /&gt;To stay safe the observatory has to know every possible threats to him. (weather, snow, sky clouds, telescope dangerous positions, ....).&lt;br /&gt;A radio bridge grants the Internet connectivity to the observatory. All the astronomical software has been deployed on a brand new x3550 IBM server and a 8kVA UPS has been installed to permit emergency&amp;nbsp; operations (PLC operations) in case of power failure.&lt;br /&gt;&lt;br /&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: right; margin-left: 1em; text-align: right;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6wU-L1CCI/AAAAAAAAAmU/e1UGEvS5EG0/s1600/acp_web_anon.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;302&quot; src=&quot;http://1.bp.blogspot.com/_cXYmdFluLpw/TP6wU-L1CCI/AAAAAAAAAmU/e1UGEvS5EG0/s320/acp_web_anon.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Observatory Web Interface (ACP)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;All astronomical components (like CCD, Focuser, Telescope, Observatory roof...) are centrally managed (or better 'orchestrated')&amp;nbsp; by a commercial software called &lt;a href=&quot;http://acp.dc3.com/index2.html&quot;&gt;ACP&lt;/a&gt;. This software use &lt;a href=&quot;http://ascom-standards.org/&quot;&gt;ASCOM &lt;/a&gt;specifically written (in C#) driver to open/close the roof (via the PLC) or to move the Telescope (via LX200 protocol aka Vertigo).&lt;br /&gt;&lt;br /&gt;A better (but probably confusing for this post purpose) overview of the various components (software and hardware) is shown in the diagram below.&lt;br /&gt;&lt;br /&gt;Any comments would be appreciated&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/_cXYmdFluLpw/TP6pnmkWixI/AAAAAAAAAmM/jSDxyjyGrlE/s1600/Interconnections2.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;424&quot; src=&quot;http://4.bp.blogspot.com/_cXYmdFluLpw/TP6pnmkWixI/AAAAAAAAAmM/jSDxyjyGrlE/s640/Interconnections2.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Interactions diagram&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description>
        <pubDate>Tue, 07 Dec 2010 22:04:00 +0000</pubDate>
        <link>http://blog.wizche.ch/2010/12/07/automating-astronomical-observatory.html</link>
        <guid isPermaLink="true">http://blog.wizche.ch/2010/12/07/automating-astronomical-observatory.html</guid>
        
        <category>observatory astronomy plc s7 automation</category>
        
        
      </item>
    
  </channel>
</rss>
