<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>peterbudai</title><link href="https://peterbudai.com/" rel="alternate"/><link href="https://peterbudai.com/feeds/all.atom.xml" rel="self"/><id>https://peterbudai.com/</id><updated>2019-07-11T10:58:03+02:00</updated><subtitle>return true;</subtitle><entry><title>Collecting broadcast UDP packets using async networking in Rust</title><link href="https://peterbudai.com/blog/2019/07/11/collecting-broadcast-udp-packets-using-async-networking-in-rust/" rel="alternate"/><published>2019-07-11T10:58:03+02:00</published><updated>2019-07-11T10:58:03+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2019-07-11:/blog/2019/07/11/collecting-broadcast-udp-packets-using-async-networking-in-rust/</id><summary type="html">&lt;p&gt;Creating a simple console application in Rust for fun and profit, including some nice tricks to push async programming to the limits.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;I’m a big fan of &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt;, although I usually have little free time to fiddle with it. But recently when I had a task at my job to create a small, greenfield networking &lt;a href="https://crates.io/crates/tresorit-dropbox-discovery"&gt;application&lt;/a&gt; I decided to take the chance and experiment with asynchronous programming and the &lt;a href="https://tokio.rs/"&gt;Tokio&lt;/a&gt; framework in Rust. I didn’t just succeed but actually had so much fun, that I even managed to push it to the other extreme: the whole console application essentially became a single async event stream.&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;ORIGINALLY PUBLISHED ON &lt;a href="https://medium.com/tresorit-engineering/collecting-broadcast-udp-packets-using-async-networking-in-rust-7fd93a631eac"&gt;MEDIUM&lt;/a&gt; ON JULY 8, 2019&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="" class="alignleft" height="239" src="https://cdn-images-1.medium.com/max/540/1*G-p9lxpwjTTzkSek0KVLKw.png" width="359"/&gt;&lt;/p&gt;
&lt;p&gt;The application’s task is to discover devices on the local network that broadcast over UDP: it listens on a given port for a minute and collects packets that adhere to a specific format, along with their originator IP addresses. In this article, I’ll only cover the interesting parts, but you can find the complete source on Tresorit’s &lt;a href="https://github.com/tresorit/tresorit-dropbox-discovery"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Asynchronous programming enables you to continue working on tasks on a single thread while you’re application waits for some long-running operation to complete, resulting in a more efficient use of CPU time. When you’re doing async programming, you have an event loop at the core of your application, which dispatches execution to the processing functions once an external event arises. In Rust terms, you define a long-running, asynchronous task, called a &lt;code&gt;Future&lt;/code&gt;&lt;em&gt;,&lt;/em&gt; that will be repeatedly polled by the &lt;code&gt;Executor&lt;/code&gt;until the external condition it’s waiting for is fulfilled and it will finally return its actual result. Rust’s nice functional-style operators also appear here that allows you to chain and combine your futures to implement a more complex, yet fully asynchronous tasks without sacrificing efficiency.&lt;/p&gt;
&lt;h2&gt;From datagram bytes to a packet stream&lt;/h2&gt;
&lt;p&gt;The UDP packets we’re looking for have a simple JSON payload, so we’re starting with declaring that as a regular Rust struct, along with annotations for the powerful &lt;a href="https://serde.rs/"&gt;Serde&lt;/a&gt; library that will do the heavy lifting of JSON deserialization for us.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/1668d614165637110e2a3682dcc65bce.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/1668d614165637110e2a3682dcc65bce"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;JSON payload object as a Rust structure, to be deserialized by &lt;a href="https://docs.serde.rs/serde_json/"&gt;Serde JSON&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The Tokio framework gives you all building blocks to start doing async networking, starting with &lt;code&gt;UdpSocket&lt;/code&gt; and &lt;code&gt;UdpFramed&lt;/code&gt;. The latter converts incoming UDP datagrams into an async &lt;code&gt;Stream&lt;/code&gt;: a long-running async operation which, unlike to a simple &lt;code&gt;Future&lt;/code&gt;, can yield several results of the same type before being terminated — an ideal async model for a network connection.&lt;/p&gt;
&lt;p&gt;An &lt;code&gt;UdpFramed&lt;/code&gt; instance can even assemble higher level protocol data structures for us out of the network byte stream, regardless of how they are aligned within the stream; they can even span over multiple datagrams. All we need to do is to create an implementation of the &lt;code&gt;Decoder&lt;/code&gt; trait.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/e7977a6c7b6e2db6aa3ef1e7582f3e43.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/e7977a6c7b6e2db6aa3ef1e7582f3e43"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;Continuously decoding JSON payload from UDP datagram byte stream&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Here we’re doing two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Declaring the return type of the async stream by specifying the associated type &lt;code&gt;Item&lt;/code&gt;. There’s a small trick here. We’re more interested in collecting as much valid packets from multiple sources as possible rather than strictly adhering to the protocol format, so we even accept ones that cannot be decoded and simply drop them.
   For this we specify the return type to be &lt;code&gt;Option&lt;beaconpacket&gt;&lt;/beaconpacket&gt;&lt;/code&gt;, where &lt;code&gt;None&lt;/code&gt; will signal a malformed packet.&lt;/li&gt;
&lt;li&gt;Implementing the actual decode routine in the &lt;code&gt;decode&lt;/code&gt; function. Note that the return type of the function is actually &lt;code&gt;Result&lt;option&lt;option&lt;...&gt;&amp;gt;&amp;gt;&lt;/option&lt;option&lt;...&gt;&lt;/code&gt;. The outermost &lt;code&gt;Result&lt;/code&gt; is for signaling network or protocol errors — cases we would like to avoid if possible to keep the packets flowing, so we always return &lt;code&gt;Ok&lt;/code&gt;. The middle &lt;code&gt;Option&lt;/code&gt; is for signaling &lt;code&gt;UdpFramed&lt;/code&gt; to give more bytes for decoding, we use that when an empty buffer is encountered. The innermost &lt;code&gt;Option&lt;/code&gt; is the actual return type carrying a (in)valid protocol data structure — the result of a (un)successful JSON deserialization of the datagram payload bytes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now we have everything at our hands to build a nice asynchronous &lt;code&gt;Stream&lt;/code&gt; of incoming beacon packets out of a plain UDP socket.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/d410e32385c2da24517624daa67ef17b.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/d410e32385c2da24517624daa67ef17b"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;Building an async stream that returns &lt;code&gt;(host_id, ip_address)&lt;/code&gt; pairs for the received UDP packets&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;We open the UDP socket, wrap it in a &lt;code&gt;UDPFramed&lt;/code&gt; instance and also set up some transformations on the stream before returning it. These transformations include dropping the invalid packets (note the inner &lt;code&gt;map&lt;/code&gt;that will effectively translate inner &lt;code&gt;None&lt;/code&gt;s to outer &lt;code&gt;None&lt;/code&gt;s); extracting the source IP addresses and the &lt;code&gt;host_int&lt;/code&gt; field from the decoded packets, because only these two pieces of information we actually need to identify the originator devices; and finally converting the result type to &lt;code&gt;Event&lt;/code&gt; — more on this last step later.&lt;/p&gt;
&lt;h2&gt;Adding a countdown timer&lt;/h2&gt;
&lt;p&gt;The devices on the local network emit UDP broadcast packets at regular intervals of 30 seconds. Therefore the discovery needs to run for at least this long, but I doubled it to account for lost packets: our application waits and actively listens for 60 seconds and then prints the results.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt="" src="https://cdn-images-1.medium.com/max/720/0*_F2TaPVEu186j-Fn"/&gt;
&lt;figcaption&gt;Photo by &lt;a href="https://unsplash.com/@agebarros?utm_source=medium&amp;amp;utm_medium=referral"&gt;Agê Barros&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;There is a &lt;code&gt;Timeout&lt;/code&gt; future in the Tokio library that will fire and terminate another future or stream after a given period, however we cannot use that here because of two reasons:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If a valid item is returned from the stream before the timeout period elapses, it will effectively cancel the timeout: it is designed for cancelling unsuccessful futures not for general termination use-cases.&lt;/li&gt;
&lt;li&gt;Having a console application doing seemingly nothing for a minute is not quite user friendly. It would be much nicer to have visual feedback of the remaining time.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So instead of &lt;code&gt;Timeout&lt;/code&gt;, we’re going to use &lt;code&gt;Interval&lt;/code&gt; that is a built-in stream that yields a result periodically — in this case once every second.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/0725a75b35c612d32af15b28f67de53d.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/0725a75b35c612d32af15b28f67de53d"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;Building an async stream producing countdown timer events&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;This is a quite straightforward implementation, but note two peculiarities:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We include the remaining seconds in the result. However, the stream is not terminated automatically when reaching zero, so we must take care that the number can never go below zero.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Event&lt;/code&gt; type appears again as the result of this stream as well. I’ll cover the reasons in the next section.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Putting it all together&lt;/h2&gt;
&lt;p&gt;Let’s start with the &lt;code&gt;Event&lt;/code&gt; type right away. It’s a great example of using Rust’s feature-rich enum type to create type safe unions: it either can be a Host ID — IP Address pair or a number of remaining seconds.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/083cbdfed4759ad3a4c6712cf2d6771b.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/083cbdfed4759ad3a4c6712cf2d6771b"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;A simple enum to carry possible stream events&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;As you saw earlier, we use this enum as the result type of both the UDP and the timer streams. This enables us to do the most interesting part: combining async streams together to implement application logic.&lt;/p&gt;
&lt;h3&gt;Supporting optional streams&lt;/h3&gt;
&lt;p&gt;Before we move on to that, there’s one last thing that needs to be done. As you remember, our UDP stream can handle both IP protocol versions, but only one at a time. We would like to benefit from this and build IPv4/6 support into our application: it can listen for incoming packets on either an IPv4 or IPv6 interface, or even on both of them simultaneously.&lt;/p&gt;
&lt;p&gt;From the source code’s perspective it means that we will have one or two UDP active streams concurrently. The latter could easily be done by applying the &lt;code&gt;select&lt;/code&gt; combinator on the stream: it will yield the results from both streams, in order they arrive. There’s a caveat however. The underlying type of a &lt;code&gt;select&lt;/code&gt;ed stream is different from a single stream: the first one is &lt;code&gt;Select&lt;impl stream&lt;event=""&gt;, …&amp;gt;&lt;/impl&gt;&lt;/code&gt; while the second one is some opaque &lt;code&gt;impl Stream&lt;event&gt;&lt;/event&gt;&lt;/code&gt;, more precisely &lt;code&gt;FilterMap&lt;udpframed&lt;…&gt;, …&amp;gt;&lt;/udpframed&lt;…&gt;&lt;/code&gt; in our case.&lt;/p&gt;
&lt;p&gt;In the Tokio library, the &lt;code&gt;Either&lt;/code&gt; type serves for combining two futures of separate types where only one of them can be “active” at a given time — unlike to &lt;code&gt;Select&lt;/code&gt; where both child streams are active. Unfortunately &lt;code&gt;Either&lt;/code&gt; is not implemented for streams in the framework, only for futures. Thus, we ended up rolling our own implementation, which is actually quite straightforward.&lt;/p&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/a0bc6617c138b84c7647008e3f311d1e.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/a0bc6617c138b84c7647008e3f311d1e"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;Combinator type for selecting one of two separate underlying streams&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h3&gt;The main function&lt;/h3&gt;
&lt;p&gt;Now that we have all building blocks available, we can finally put it together in the &lt;code&gt;main&lt;/code&gt; function of our application. Let’s go through the steps we’d like to implement before we jump to the code.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt="" height="225" src="https://cdn-images-1.medium.com/max/1080/1*Y_R-WlEsAOL4bqBuMju7oA.png" width="575"/&gt;
&lt;figcaption&gt;Visualization of the event pipeline by &lt;a href="https://medium.com/@siptarabel"&gt;Ábel Siptár&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;ol&gt;
&lt;li&gt;First, we create the UDP streams that will emit &lt;code&gt;HostFound&lt;/code&gt; events for valid incoming packets on either the IPv4 or IPv6 interfaces, or both. We’re using our &lt;code&gt;EitherStream&lt;/code&gt; implementation to allow any combination of the two protocol versions.&lt;/li&gt;
&lt;li&gt;We create the other source stream that will emit &lt;code&gt;Countdown&lt;/code&gt; events once every second and combine it to the UDP streams resulting in a stream of any &lt;code&gt;Event&lt;/code&gt;s. Due to the common result type, we could do that as easy as a simple &lt;code&gt;select&lt;/code&gt; call.&lt;/li&gt;
&lt;li&gt;The next transformation will print out the remaining time to the console each time a &lt;code&gt;Countdown&lt;/code&gt; event is encountered. More importantly, the &lt;code&gt;take_while&lt;/code&gt; operator will filter out all events with the remaining time counter being zero, effectively terminating the whole stream — along with the UDP sockets as well. A pretty elegant way to do so.&lt;/li&gt;
&lt;li&gt;If we got this far in the stream, the countdown events are no longer used for anything, they just noise in the code, so we simply filter them out, keeping only &lt;code&gt;(host_id, ip_address)&lt;/code&gt; pairs in the stream.&lt;/li&gt;
&lt;li&gt;These pairs will then be fed to a &lt;code&gt;HashMap&lt;/code&gt; to filter multiple packets from the same host device. Remember, our discovery process runs longer than the beacon packet interval, and a host may also broadcast over both IP protocol versions.&lt;/li&gt;
&lt;li&gt;When the stream is eventually terminated, the above map will contain the actual result of our application — the list of IP addresses we were looking for — so the only thing left is to display it to the user.&lt;/li&gt;
&lt;/ol&gt;
&lt;figure class="gist"&gt;
&lt;script src="https://gist.github.com/peterbudai/87b57659880ac1b4256844c12b6a9881.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="https://gist.github.com/peterbudai/87b57659880ac1b4256844c12b6a9881"&gt;View this gist on GitHub&lt;/a&gt;&lt;/noscript&gt;
&lt;figcaption&gt;The complete event processor pipeline in the main function of the application&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;And basically that’s it. I’ve omitted some utility functions for clarity, but otherwise our application is now complete. 🎉&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I learned a lot while creating this application. Asynchronous programming in Rust is still quite new (so is Rust itself), and requires a somewhat different mindset than you are used to, but creating production-ready applications with it is definitely possible.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt="" src="https://cdn-images-1.medium.com/max/720/0*s7qGVicfN2gBeTkr"/&gt;
&lt;figcaption&gt;Photo by &lt;a href="https://unsplash.com/@wizwow?utm_source=medium&amp;amp;utm_medium=referral"&gt;Donald Giannatti&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The example application here is a bit extreme in the way how async streams and functional style combinators are used exclusively over the source code, making it harder to follow. Async style is not suitable for every use-case, I think that in a professional environment you should end up with a more healthier combination of async and imperative code. On the other hand, I wanted to show that once you get accustomed to this style, it can easily drag you in — and you’ll still be having fun in the meantime. 😉&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;ORIGINALLY PUBLISHED ON &lt;a href="https://medium.com/tresorit-engineering/collecting-broadcast-udp-packets-using-async-networking-in-rust-7fd93a631eac"&gt;MEDIUM&lt;/a&gt; ON JULY 8, 2019&lt;/p&gt;
&lt;/blockquote&gt;</content><category term="Blog"/><category term="async"/><category term="crosspost"/><category term="network"/><category term="open source"/><category term="programming"/><category term="rust"/><category term="stream"/><category term="tokio"/><category term="tresorit"/><category term="udp"/></entry><entry><title>I got interviewed on SE-Radio</title><link href="https://peterbudai.com/blog/2018/05/09/i-got-interviewed-on-se-radio/" rel="alternate"/><published>2018-05-09T15:03:19+02:00</published><updated>2018-05-09T15:03:19+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2018-05-09:/blog/2018/05/09/i-got-interviewed-on-se-radio/</id><summary type="html">&lt;p&gt;Thanks to Kim Carter I had the honor to appear in one of SE-Radio’s regular podcasts, where we talk about the current issues and upcoming trends of cloud security and creating secure, encrypted applications.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;Thanks to Kim Carter I had the honor to appear in one of SE-Radio’s regular podcasts, where we talk about the current issues and upcoming trends of cloud security and creating secure, encrypted applications. You can &lt;a href="http://www.se-radio.net/2018/04/se-radio-episode-321-peter-budai-on-end-to-end-encryption/"&gt;listen to it here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Kim Carter of &lt;a href="https://binarymist.io/"&gt;BinaryMist&lt;/a&gt; turned out to be a fellow &lt;a href="https://tresorit.com/"&gt;Tresorit&lt;/a&gt; user and a SE-Radio podcast host in one person. So he decided to put together a show on cloud security for which he thought about me as a good guest speaker based on the role and experiences as a developer and cryptography engineer at Tresorit.&lt;/p&gt;
&lt;p&gt;In this episode, Kim and I are talking about end-to-end encryption, backdoors, the scenarios where E2EE can be and should be used. IM, VoIP, email scenarios, as well as interservice communication scenarios such as securing data in use with full memory encryption, CPU-based key storage, enclaves, cryptographic protocols. We discuss encrypting communications between microservices in the cloud. Which ciphers to avoid and which to use along with how to find the right crypto library for your next software project.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.se-radio.net/"&gt;Software Engineering Radio&lt;/a&gt; is a podcast targeted at the professional software developer. The goal is to be a lasting educational resource, not a newscast. Three to four times per month, we publish a new episode talking to experts from the software engineering world about the full range of topics that matter to professional developers.&lt;/p&gt;</content><category term="Blog"/><category term="cloud"/><category term="cryptography"/><category term="encryption"/><category term="interview"/><category term="podcast"/><category term="security"/><category term="tresorit"/></entry><entry><title>Using #pragma once with conditional compilation</title><link href="https://peterbudai.com/blog/2014/10/12/using-pragma-once-with-conditional-compilation/" rel="alternate"/><published>2014-10-12T14:10:17+02:00</published><updated>2014-10-12T14:10:17+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2014-10-12:/blog/2014/10/12/using-pragma-once-with-conditional-compilation/</id><summary type="html">&lt;p&gt;&lt;em&gt;The #pragma once directive offers some advantages over the traditional header guard idiom, but as it is not part of the C/C++ standard we have to rely on the – not too verbose – compiler documentations. In this post, I examine what happens if you mix this directive with other preprocessor directives and conditional compilation.&lt;/em&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;The #pragma once directive offers some advantages over the traditional header guard idiom, but as it is not part of the C/C++ standard we have to rely on the – not too verbose – compiler documentations. In this post, I examine what happens if you mix this directive with other preprocessor directives and conditional compilation.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;C/C++ programs are separated to header (&lt;em&gt;.h&lt;/em&gt;, &lt;em&gt;.hpp&lt;/em&gt;) and source (&lt;em&gt;.c&lt;/em&gt;, &lt;em&gt;.cc&lt;/em&gt;, &lt;em&gt;.cpp&lt;/em&gt;) files and header files are usually included from one or more header and source files. Care must be taken to avoid redefinitions of types and preprocessor directives. Every programmer knows how to use header guards as the only standard way of doing this.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;#pragma once&lt;/em&gt; directive is a newer method for this purpose that offers superior compiler performance because the compiler won’t have to read and preprocess the header file again. Other benefits include improved code readibility, less errors introduced by moving or renaming header files, and no cluttering of preprocessor namespace. Although it’s not included in the standard, all major C/C++ compilers &lt;a href="http://en.wikipedia.org/wiki/Pragma_once#Portability"&gt;already support it&lt;/a&gt;. GCC and Clang claim that they are able to detect the header guard idiom and offer the same performance as &lt;em&gt;#pragma once&lt;/em&gt; making it unnecessary. On the other hand, Visual Studio encourages the usage of this directive over the header guards.&lt;/p&gt;
&lt;p&gt;However, one aspect is not covered well in any of the compilers’ documentation, namely what happens if we not follow the traditional header guard idiom and mix &lt;em&gt;#pragma once&lt;/em&gt; with conditional compilation. So I did a little test to find out how do they work if I put the directive in the middle of a header file instead of the beginning.&lt;/p&gt;
&lt;p&gt;I’ve created the following header file that contains alternating code depending on a value of a preprocessor define. Only one path contains the &lt;em&gt;#pragma once&lt;/em&gt; directive. The &lt;em&gt;#pragma message&lt;/em&gt; lines are pleaceholders for the actual source code, se will be able to clearly see which lines are processed from the compiler output.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;// a.h
#ifdef HEAD
# pragma message ("Head included")
#else
# pragma once
# pragma message ("Body included")
#endif
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To test it, I have prepared a source file that includes the header file multiple times with different values for the preprocessor define controlling the header file. Similarly to the other file, I’ve put &lt;em&gt;#pragma message&lt;/em&gt; directives to have more sensible output from the compiler.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;// a.c
#define HEAD
# pragma message ("Including head")
# include "a.h"
# pragma message ("Including head")
# include "a.h"
#undef HEAD

#pragma message ("Including body")
#include "a.h"
#pragma message ("Including body")
#include "a.h"

#define HEAD
# pragma message ("Including head")
# include "a.h"
#undef HEAD
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I’ve compiled these files with various compilers (Clang 3.4, GCC 4.8.3 and MSVC 18) and all worked as expected. I include only the MSVC output as it’s the most readable, but all compilers behaved the same way:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;a.c
Including head
Head included
Including head
Head included
Including body
Body included
Including body
Including head
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we include only the first part of the header file (&lt;em&gt;HEAD&lt;/em&gt; defined) it is possible to include it many times, but as soon as the second part is included (and &lt;em&gt;#param once&lt;/em&gt; is read by the preprocessor) it will never be included again (regardless of whether the &lt;em&gt;HEAD&lt;/em&gt; macro is defined or not).&lt;/p&gt;</content><category term="Blog"/><category term="c"/><category term="clang"/><category term="cplusplus"/><category term="gcc"/><category term="msvc"/><category term="pragma"/><category term="preprocessor"/><category term="programming"/></entry><entry><title>Designing the cube – part 2</title><link href="https://peterbudai.com/blog/2014/05/11/designing-the-cube-part-2/" rel="alternate"/><published>2014-05-11T19:12:42+02:00</published><updated>2014-05-11T19:12:42+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2014-05-11:/blog/2014/05/11/designing-the-cube-part-2/</id><summary type="html">&lt;p&gt;In my &lt;a href="/blog/2014/04/18/designing-the-cube-part-1/" title="Designing the cube – part 1"&gt;last article&lt;/a&gt; I presented an overview of the LED cube, separating the design into three parts: the &lt;em&gt;cube&lt;/em&gt; itself and the &lt;em&gt;driver&lt;/em&gt; and &lt;em&gt;controller&lt;/em&gt; circuits. This time I&amp;rsquo;m going to discuss the second one and emphasize the differences between the &lt;a href="http://www.instructables.com/id/Led-Cube-8x8x8/" title="LED cube build instructions"&gt;original building instructions&lt;/a&gt; and my solution.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In my &lt;a href="/blog/2014/04/18/designing-the-cube-part-1/" title="Designing the cube – part 1"&gt;last article&lt;/a&gt; I presented an overview of the LED cube, separating the design into three parts: the &lt;em&gt;cube&lt;/em&gt; itself and the &lt;em&gt;driver&lt;/em&gt; and &lt;em&gt;controller&lt;/em&gt; circuits. This time I’m going to discuss the second one and emphasize the differences between the &lt;a href="http://www.instructables.com/id/Led-Cube-8x8x8/" title="LED cube build instructions"&gt;original building instructions&lt;/a&gt; and my solution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Doing the math&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before we dive into the details of the driver circuit, we need to do some math regarding the timings and currents of our LED cube configuration.&lt;/p&gt;
&lt;p&gt;I’m dreaming of playing fancy 3D animations on the cube once I’m finished. A visually smooth animation has 25-30 &lt;em&gt;frames per second&lt;/em&gt; (FPS). To let each &lt;em&gt;frame&lt;/em&gt; (a complete, yet still 3D image) stabilize before advancing to the next one, the whole cube should have a &lt;em&gt;refresh rate&lt;/em&gt; around 100 Hz, which means this many frames will be displayed per second. Because in our case, the 3D image is created by quickly &lt;em&gt;multiplexing&lt;/em&gt; eight 2D &lt;em&gt;layers&lt;/em&gt; onto each other, the layer refresh rate is eight times higher than the frame refresh rate, resulting in 800 Hz. Calculating with the latter value further, we get 1 s / 800 Hz = 1.25 ms active time for a single layer. Assuming the 12.5% duty cycle we have calculated in the previous article, this means that each LED will be turned on and off for 1.25 ms and 8.75 ms, respectively. The original design had roughly 15 times faster refresh rates, but I find it to be an overkill, however if I experience some flickering I can easily boost the refresh rate from software.&lt;/p&gt;
&lt;p&gt;The datasheet of the LEDs I have ordered from Ebay says that they can withstand a current of 30 mA for continuous operation or 70 mA peak current for momentary operation. Because the brightness of a LED is proportional to the current flowing through it, I’m planning to use the highest possible current. Although in theory the 1.25 ms active time and 12.5% duty cycle would qualify as momentary, I don’t want to overstress the LEDs during building and testing when they will be switched manually, so I decided to go with 30 mA.&lt;/p&gt;
&lt;p&gt;The datasheet also states that the forward voltage of the LEDs are around 2.2 V. From this, we can calculate the size of the current limiting resistors required to achieve 30 mA through the LEDs.d Assuming 5 V supply voltage, the voltage on the resistor is 5 – 2.2 = 2.8 V and the required resistance is 2.8 V / 0.03 A = 93 Ω. However, after trying out one of the LEDs with a 100 Ω resistor, my measurements showed that the forward voltage is a little higher, so using a 82 Ω resistor would result in exactly 30 mA current flowing through the LED.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Driving columns&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We need to control all 64 column terminals simultaneously. No microcontroller has this many I/O lines, so somehow we have to reduce the number of required outputs. I choose the &lt;a href="http://www.electronics-tutorials.ws/sequential/seq_5.html" title="Shift register tutorial"&gt;shift register&lt;/a&gt; solution suggested in the original build instructions. This version uses &lt;em&gt;8-bit serial in parallel out shift register&lt;/em&gt;s. A shift register has 1 data input and 1 clock input terminal and 8 data output terminals. The data is shifted in serially one bit at a time in eight shift register clock cycles and then it can be read parallel on the eight data outputs. In this configuration the data outputs directly control 8 out of the 64 LED cube columns (I like to call this a &lt;em&gt;row&lt;/em&gt;) via current-limiting resistors, so 8 shift registers cover all 64 columns organized in 8 rows.&lt;/p&gt;
&lt;p&gt;The clock inputs of the shift registers are connected together, thus every shift register shifts their bits synchronously. Regarding the data inputs, we have two choices: we can either &lt;em&gt;daisy chain&lt;/em&gt; the shift registers (the bit 7 of the previous shift register goes to the data input of the next one, so setting up a layer requires 64 shift register clock cycles), or we can fed the data to the shift registers &lt;em&gt;parallel&lt;/em&gt; (each shift registers have an independent data input, thus only 8 shift register clock cycles is required to set up a layer’s state).&lt;/p&gt;
&lt;p&gt;In the microcontroller, several instructions are required to manipulate the shift registers: fetching the next byte to show from the memory (2-3 instructions), shifting the bits (1-2 instruction), setting the data output pin (1-2 instructions), setting the clock output pin high and back to low (2 instructions), plus branching for loops (2-3 instructions). With the daisy chain configuration, this has to be repeated 64 times, resulting in 64 × 12 = 768  instructions. Assuming 1 MHz microcontroller clock frequency and RISC architecture (1 clock cycle per instruction), setting up a layer’s state requires 768 μs, so around 0.8 ms. Now recall the 1.25 ms layer active time we have calculated in the previous section. This means that in this configuration the microcontroller would spend most of it’s CPU time controlling the LEDs, leaving no time for anything else (computing animation frames or communicating with the computer).&lt;/p&gt;
&lt;p&gt;Opposed to this, the parallel data feeding configuration (the one that is presented in the original build instruction as well) requires 8 times less instructions (0.1 ms estimated) at the cost of using 7 more I/O lines on the microcontroller. This seemed a reasonable compromise, so I ended up using the parallel data feeding configuration. The following schematic fragment shows the two different shift register chaining configuration (I show only 3 shift registers, but the principle remains the same).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Daisy chain and parallel feeding of shift registers" class="aligncenter image-process-responsive" height="661" sizes="(max-width: 754px) 100vw, 754px" src="/images/2014/responsive/1024w/shift_register_chaining.png" srcset="/images/2014/responsive/300w/shift_register_chaining.png 300w, /images/2014/responsive/1024w/shift_register_chaining.png 1024w, /images/2014/responsive/2048w/shift_register_chaining.png 2048w" width="754"/&gt;&lt;/p&gt;
&lt;p&gt;The original design suggests the &lt;a href="http://www.futurlec.com/74HC/74HC164.shtml" title="74HC164 Datasheet"&gt;74HC164&lt;/a&gt; shift register. However, the &lt;a href="http://www.futurlec.com/74HC/74HC595.shtml" title="74HC595 Datasheet"&gt;74HC595&lt;/a&gt; would be a better choice, because it has a built-in latch, so it retains its last output state until bit shifting the next state is finished, which results in reduced flickering of the outputs. &lt;a href="http://www.youtube.com/watch?v=h84cDS7_9Pg" title="74HC164 and 74HC595 comparison video"&gt;This&lt;/a&gt; is a great explanatory video tutorial demonstrating the differences between the two components. Because of the latching functionality, the 74HC595 has an additional input pin for the latch control. With this in mind, my design requires 8+1+1 = 10 microcontroller I/O pins for controlling the columns.&lt;/p&gt;
&lt;p&gt;So far so good, but if you read the 74HC595 datasheet carefully you can notice that although the shift register can handle 35 mA per output pin (showed as I&lt;sub&gt;OUT&lt;/sub&gt; in the datasheet) – remember, I need 30 mA for my LEDs – that may be enough, but the whole IC cannot handle more than 70 mA (I&lt;sub&gt;CC&lt;/sub&gt;) in total, so if more than 2 LEDs are active in a row at the same time, the shift register IC will most likely end in up smoke. This is unacceptable as there may be lot of 3D animations that require more than 2 LEDs per row.&lt;/p&gt;
&lt;p&gt;To mitigate this problem one may use additional components to deal with larger currents like external transistors or the &lt;a href="http://www.ti.com/lit/ds/symlink/uln2803a.pdf" title="ULN2803 Datasheet"&gt;ULN2803&lt;/a&gt; driver IC. While these are both valid solutions, I tried to minimalize the number of components (thus potential error sources both during calculations and assembly) in my design, so I choose the &lt;a href="http://www.ti.com/lit/ds/slis061c/slis061c.pdf" title="TPIC6C595 Datasheet"&gt;TPIC6C595&lt;/a&gt; shift register instead. As its part number suggests, it has exactly the same functionality as the 74HC595, but it has &lt;em&gt;open drain&lt;/em&gt; outputs which can handle up to 100 mA per pin even if all pins are turned on (I&lt;sub&gt;D&lt;/sub&gt; in the datasheet).&lt;/p&gt;
&lt;p&gt;If you aren’t familiar with the terms open drain or open collector (I weren’t also before starting this project), &lt;a href="http://www.evilmadscientist.com/2012/basics-open-collector-outputs/" title="Open drain basics"&gt;here is a great tutorial&lt;/a&gt; on how they work and how they can be used. There is a caveat you have to pay attention to when using open drain outputs. As this kind of output can only sink current (the current must flow into the pin towards the ground), all LEDs in the cube must have their negative (cathode) terminal connected to the shift register (they “point” towards the shift register on the schematic). This means the LEDs has to be wired in the direction “opposite” to the one presented in the original build instructions. As I wrote in my previous article all LEDs in a layer have their anode terminal connected and all LEDs in a column have their cathode terminal soldered together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Driving layers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After figuring out how to control the 64 columns, let’s focus on controlling the 8 layers. Because the column driver logic uses 10 microcontroller I/O lines, I decided to save on layer controller lines by using a &lt;em&gt;3 to 8 line decoder&lt;/em&gt; IC which was not present in the original build instructions (or at least not for this purpose). A decoder has 3 inputs representing 3 address bits and 8 output lines of which one and only one is active at a given time selected by the address on the input – if the input is 000, 010 or 110 the output line 0, 2 or 6 is active, respectively. There are two decoder ICs in the 74x family: the &lt;a href="https://www.futurlec.com/74HC/74HC138.shtml" title="74HC138 Datasheet"&gt;74HC138&lt;/a&gt; and &lt;a href="https://www.futurlec.com/74HC/74HC238.shtml" title="74HC238 Datasheet"&gt;74HC238&lt;/a&gt; with the only difference that the first one has &lt;em&gt;active-low&lt;/em&gt; while the second one has &lt;em&gt;active-high&lt;/em&gt; outputs.&lt;/p&gt;
&lt;p&gt;Since it is possible that all 64 LEDs are active at the same time in a given layer, we have to switch 64 × 30mA = 1.8 A, which is quite a lot. There isn’t any decoder ICs that can handle this much current, so we will need an external transistor. Since the LEDs that form a layer in the cube are connected together by their anode (positive) terminals (this is called the &lt;em&gt;common-anode&lt;/em&gt; configuration) we have to switch the anodes (the positive power source terminal) to turn a layer on or off. This is called &lt;em&gt;high-side switching&lt;/em&gt; (as opposed to &lt;em&gt;low-side switching&lt;/em&gt; when we switch the ground terminal). The difference between the two is shown on the following schematic.&lt;/p&gt;
&lt;p&gt;&lt;img alt="LED controlling schematics" class="aligncenter image-process-responsive" height="471" sizes="(max-width: 754px) 100vw, 754px" src="/images/2014/responsive/1024w/led_control_with_transistor.png" srcset="/images/2014/responsive/300w/led_control_with_transistor.png 300w, /images/2014/responsive/1024w/led_control_with_transistor.png 1024w, /images/2014/responsive/2048w/led_control_with_transistor.png 2048w" width="754"/&gt;&lt;/p&gt;
&lt;p&gt;The next step is to decide whether to use traditional bipolar (BJT) or field-effect (MOSFET) transistors. BJTs are much cheaper so this was the first option I looked at. Bipolar transistors are current-controlled, the amount of current flowing through between the &lt;em&gt;collector&lt;/em&gt; and the &lt;em&gt;emitter&lt;/em&gt; is the function of the current flowing into or out from the &lt;em&gt;base&lt;/em&gt; terminal. To switch 1.8 A on the high side with a PNP transistor, the base current exceeds 100 mA which is too much for the decoder IC. So we would need another transistor to control the first one, plus a few additional resistors (&lt;a href="http://www.w9xt.com/page_microdesign_pt8_pnp_switching.html" title="High side switching with transistor"&gt;here&lt;/a&gt; and &lt;a href="http://wiki.netduino.com/LED-how-to-drive-it.ashx" title="LED driving with transistors"&gt;here&lt;/a&gt; are a few example circuits, though the latter uses the terms common anode and cathode incorrectly) that overly complicates the design.&lt;/p&gt;
&lt;p&gt;In order to keep the circuit as simple as possible I choose MOSFETs over bipolar transistors despite their somewhat higher pricetag. For high-side switching only one P-channel MOSFET is required per layer without any additional components (the exact circuit is shown on the right side of the schematic above). Because MOSFETs are voltage-controlled there are no gate current limitations to deal with. However, we still need to carefully select an appropriate variant that is able to handle at least 2 A of source-drain current. I’ve chosen the &lt;a href="https://www.futurlec.com/Transistors/IRF9Z34N.shtml" title="IRF9Z34N Datasheet"&gt;IRF9Z34N&lt;/a&gt; that supports I&lt;sub&gt;D&lt;/sub&gt; = 14 A maximum current as specified in the datasheet. The P-MOSFET is turned on (current flows between the &lt;em&gt;source&lt;/em&gt; and &lt;em&gt;drain&lt;/em&gt;terminals&lt;em&gt;)&lt;/em&gt;if the &lt;em&gt;gate&lt;/em&gt; terminal has negative (ground) voltage and turned off if the gate has high voltage attached to it. This implies that the MOSFET can easily be controlled with the active-low version (74HC138) of the decoder.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting it all together&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now that we have both parts – the column and layer driver – designed we can assemble the complete LED cube driver circuit that you can study on the following schematics.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/2014/responsive/2048w/ledcube_driver_schematic.png"&gt;&lt;img alt="LED cube driver circuit schematic" class="aligncenter image-process-responsive" height="531" sizes="(max-width: 754px) 100vw, 754px" src="/images/2014/responsive/1024w/ledcube_driver_schematic.png" srcset="/images/2014/responsive/300w/ledcube_driver_schematic.png 300w, /images/2014/responsive/1024w/ledcube_driver_schematic.png 1024w, /images/2014/responsive/2048w/ledcube_driver_schematic.png 2048w" width="754"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can see the shift registers, current limiting resistors, the decoder and the layer controller MOSFETs and the connections to other main parts of the LED cube. JROW0-JROW7 and JLAYER are the connectors going to the LED cube column and layer terminals. J0-J9 are the connectors going to the controller circuit panel. J0 to J7 are the eight shift register data inputs, J8 is the 5 V power supply input and J9 is the connector for the IC control lines: shift register clock inputs and decoder address inputs. I’ve added two input lines I haven’t discussed so far. The first is the global &lt;em&gt;reset&lt;/em&gt; line for clearing all shift register (and resetting the microcontroller itself on the controller panel), the second is an &lt;em&gt;enable&lt;/em&gt; line that makes it possible to completely turn off the cube from the microcontroller by disabling the outputs of the shift register and the decoder.&lt;/p&gt;
&lt;p&gt;There are also few additional components as well. There is a 10 kΩ pull-down resistor to aid properly turning off the layers. And there are some capacitors scattered around the schematic. These are required to deal with the sudden voltage drop running through the complete circuit when a layer with many active columns (thus large current need) is turned on. The capacitors are placed near every IC and MOSFET to sustain proper supply voltage to the digital circuitry even during a short temporary voltage drop.&lt;/p&gt;
&lt;p&gt;Now that both the cube and the driver circuit is properly designed (I hope I haven’t made any miscalculations), I can finally start building the cube.&lt;/p&gt;</content><category term="Blog"/><category term="current"/><category term="high side switch"/><category term="led"/><category term="led cube"/><category term="mosfet"/><category term="open drain"/><category term="resistor"/><category term="shift register"/><category term="transistor"/></entry><entry><title>Designing the cube – part 1</title><link href="https://peterbudai.com/blog/2014/04/18/designing-the-cube-part-1/" rel="alternate"/><published>2014-04-18T23:54:08+02:00</published><updated>2014-04-18T23:54:08+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2014-04-18:/blog/2014/04/18/designing-the-cube-part-1/</id><summary type="html">&lt;p&gt;I have promised LEDs, and lots of interesting topic, but all you got is just some physics stuff, you could say. And you were right, so this time I introduce the design of my LED cube, explaining the decisions that lead to this specific design, which may help others if they are doing a similar project.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In this series of articles, I present the overall design of my LED cube. I try to emphasize the key points I had to take into consideration and the decisions that I had to make before settling with the final version that in turn will be assembled in reality.&lt;/p&gt;
&lt;p&gt;Since I have no prior experience with similar projects, so I based my design on &lt;a href="http://www.instructables.com/id/Led-Cube-8x8x8/" title="LED cube build instructions"&gt;these instructions&lt;/a&gt; that are pretty thorough and cover almost every details. However, I needed to slightly change the design at some point. I recommend studying that article, because I will only highlight the differences. Some parts were not available and I tried to keep the costs and part number at minimum to have a clean design. But don’t rush too far ahead, let’s start at the beginning.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main parts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;According to my plans, my working LED cube will consist of three distinctive parts. The first one is the &lt;em&gt;cube&lt;/em&gt; itself without any electronics that I’m going to discuss in the next two sections. The second part in the &lt;em&gt;driver&lt;/em&gt; electronics that is responsible to supply appropriate currents and voltages to show up the desired image on the cube. The third part is the &lt;em&gt;controller&lt;/em&gt; which is the programmable “brain” of the cube. Its main part is a microcontroller IC that controls what image or animation to play on the cube.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cube layout&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I employ the same layout as in the original design. My cube consists of 8 &lt;em&gt;layers&lt;/em&gt;, each layer contains 64 LEDs in 8 rows and 8 columns. All LEDs in a layer are connected together by their &lt;em&gt;anode&lt;/em&gt; leads. The layers are stacked on each other and the LEDs directly above each other are connected together by their &lt;em&gt;cathode&lt;/em&gt; leads forming 64 &lt;em&gt;columns&lt;/em&gt;. This is called &lt;em&gt;common-anode&lt;/em&gt; configuration and it differs from the original design where the cathodes were connected in a layer (common-cathode). The cause of this change will be discussed later. To light up a specific LED, we have to connect the ground to the selected column’s terminal and some positive voltage to the selected layer’s terminal. This layout results in altogether 72 terminals: 64 for the &lt;em&gt;columns&lt;/em&gt; and 8 for the &lt;em&gt;layers&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;As shown in &lt;a href="https://www.youtube.com/watch?v=ECl0XgiVDrI" title="LED cube multiplexing demo"&gt;this video&lt;/a&gt;, at a given time only one layer is active by selecting some of the columns, then the next layer is turned on with possibly different columns selected, and so on. This is called &lt;em&gt;multiplexing&lt;/em&gt;, and if we switch layers fast enough, the human eye can be fooled to see all layers active at the same time resulting in a 3-dimensional image. This effect is called &lt;em&gt;persistence of vision&lt;/em&gt; or &lt;em&gt;POV&lt;/em&gt; for short and utilized in almost every display device from LED panels to televisions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LEDs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since only one layer is active at a time, each LED is lit only 1/8 of the time (which gives us 1 / 8 = 0.125 = 12.5 % &lt;em&gt;duty cycle&lt;/em&gt;). This also means that the visible brightness of the LEDs are 1/8 of their maximum brightness. Standard LEDs have brightness around 40 mcd (&lt;em&gt;millicandela&lt;/em&gt;, thousandth of the brightness of a candle), way too dim to use in the cube. That’s why I was looking for ultra bright LEDs that start from 1000 mcd and above.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt="LEDs in different sizes and colors" class="image-process-responsive" height="555" sizes="(max-width: 754px) 100vw, 754px" src="/images/2014/responsive/1024w/WP_20140407_09_41_02_Pro-e1397925511863.jpg" srcset="/images/2014/responsive/300w/WP_20140407_09_41_02_Pro-e1397925511863.jpg 300w, /images/2014/responsive/1024w/WP_20140407_09_41_02_Pro-e1397925511863.jpg 1024w, /images/2014/responsive/2048w/WP_20140407_09_41_02_Pro-e1397925511863.jpg 2048w" width="754"/&gt;
&lt;figcaption&gt;These are LEDs in various sizes and colors. The two on the left are 5 mm diameter, the others are 3 mm. The ones to the right have clear housing while the other three are diffused ones with colored housing. You can see the two leads: the long one is the &lt;em&gt;anode&lt;/em&gt; (positive), the short one is the &lt;em&gt;cathode&lt;/em&gt; (negative) terminal. The cathode is also marked with a flat edge on the LED casing.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;However, ultra bright LEDs are slightly more expensive and since the cube contains altogether 512 LEDs (8×8×8), this little price difference begins to matter. So I looked around on Ebay, by ordering directly from China I could save up to 70% compared to buying from local suppliers here in Hungary. The original design recommended diffused LEDs for better 3D light distribution, but unfortunately diffused LEDs with this brightness levels are quite rare to find at a reasonable price. So I ended up ordering 5000mcd clear LEDs in orange colour. Almost every cubes around the internet use clear LEDs, so I guess they will be alright.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Driver circuit&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The driver circuit does nothing else but selecting exactly one layer and – at the same time – selecting the appropriate columns (0-64) to light up in that layer. Besides that there are two main reasons why we need dedicated driver electronics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;According to the layout, there are 64 column wires and 8 layer wires to control which LEDs are lit. No microcontroller has this many I/O lines, so the driver circuit employs some kind of &lt;em&gt;multiplexing&lt;/em&gt; electronics that maps the 72 cube terminals to much fewer I/O lines.&lt;/li&gt;
&lt;li&gt;Depending on the actual 3D image shown a layer may have all its 64 LEDs turned on at the same time, which needs quite a lot of power. Microcontrollers contain fine circuitry that can’t handle high currents, so we need some electronics between the cube and the controller that is designed to deal with such high currents.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the upcoming article I will discuss the driver circuit in more details.&lt;/p&gt;</content><category term="Blog"/><category term="architecture"/><category term="common anode"/><category term="design"/><category term="led"/><category term="led cube"/><category term="multiplexing"/></entry><entry><title>Some basic enlightement</title><link href="https://peterbudai.com/blog/2014/03/09/some-basic-enlightement/" rel="alternate"/><published>2014-03-09T18:36:21+01:00</published><updated>2014-03-09T18:36:21+01:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2014-03-09:/blog/2014/03/09/some-basic-enlightement/</id><summary type="html">&lt;p&gt;Before dealing with complex electronic circuits, we need to get familiar with some basic concepts like voltage, current, resistance and their relation. In this article I provide a realy quick summary of these terms.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Let’s start with some basic theory, I promise it won’t hurt. First we need to get familiar with some basic concepts like &lt;em&gt;voltage&lt;/em&gt;, &lt;em&gt;current&lt;/em&gt;, &lt;em&gt;resistance&lt;/em&gt; and their relation. Please note that here and throughout this blog, I’m not seeking to provide physically perfect explanations, just a minimal description necessary for me to understand the various phenomenons.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Current&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Current is the flow of electricity, which makes everything work, it makes lamps glow, motors revolve and radios play music. Current flows through the wires and electrical components that can conduct electricity, hence they called &lt;em&gt;conductors&lt;/em&gt;. In the vast majority of cases, these conductors have subatomic particles in them called &lt;em&gt;electrons&lt;/em&gt;, and the flow of current manifests in the movement of these particles. More electrons moving through in a specific timeframe will result in larger electric &lt;em&gt;current&lt;/em&gt;. But don’t worry, we don’t have to deal with such microscopic details. Current (I) is measured in &lt;em&gt;amperes&lt;/em&gt; (A). Generally we will deal with milliamperes (mA), the thousandth of an ampere (&lt;code&gt;1 A = 1000 mA&lt;/code&gt;), which is considered small current, while ranges above 10 A are considered large currents.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Voltage&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;But what makes current begin to flow? I’ll jump right to the answer: &lt;em&gt;voltage&lt;/em&gt; does. Voltage has another fancy name, it’s called potential difference. While you wouldn’t call it that way, it helps understanding how it works. Imagine a basketball, this will represent our electron. If you put it on the floor (&lt;em&gt;ground potential&lt;/em&gt;), it doesn’t go anywhere. Now if you pick it up, hold it high, then let it go, it doesn’t keep floating int the air but falls down instead. That’s because it was placed to a &lt;em&gt;higher potential&lt;/em&gt;, so we have created a potential difference that made the ball start moving.&lt;/p&gt;
&lt;p&gt;The way gravitational field affects the basketball is very similar to how electric field affects the electrons. If you create protential difference between two points and connect them with some kind of conductor, current will flow from the point with higher potential to the point with lower one. But how can I create potential difference, you would ask. You have to use a battery or some kind of external power supply like an AC adapter (wall-wart). In circuit diagrams, these power sources are shown as in the following image.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Battery and external power symbols" class="aligncenter image-process-responsive" height="156" sizes="(max-width: 300px) 100vw, 300px" src="/images/2014/responsive/1024w/power_symbol.png" srcset="/images/2014/responsive/300w/power_symbol.png 300w, /images/2014/responsive/1024w/power_symbol.png 1024w, /images/2014/responsive/2048w/power_symbol.png 2048w" width="300"/&gt;&lt;/p&gt;
&lt;p&gt;Voltage (U) is measured in &lt;em&gt;volts&lt;/em&gt; (V). Values below 50 V is considered safe low voltages, flashlight batteries supply 1.5 volts, car batteries supply 12 volts, typical wall adapters supply from 3 to 19 volts. Values above this are &lt;strong&gt;dangerous&lt;/strong&gt; to deal with. Wall sockets (mains) have 110 or 230 volts, while long-distance power lines uses many thousand volts. Generally, higher voltage causes higher current to flow, that’s what makes it dangerous for humans if they touch it with hand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Power and energy&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Power is the product of voltage and current. It describes how much electric energy a device consumes (or to say it a fancy way: dissipates), and how much work can be done with that device. Power (P) is measured in watts (W), the higher the value is the higher power a device has.&lt;/p&gt;
&lt;p&gt;The actual amount of electric energy consumed is the product of power and time. Generally, energy (E) is measured in joules (J), but standard household electricity meters show kilowatt-hours (kWh) as units.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Resistance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As it’s name suggests, &lt;em&gt;resistance&lt;/em&gt; (R) is a property of electronic devices, it show how much a device obstructs (resists) the flow of current. It is based on the physical characteristics of the device and it is measured in ohms (Ω). A piece of wire for example has a very low resistance (nearly 0 Ω), allowing very large current to flow through it, while a standard lightbulb for example has a resistance around 660 Ω. There are specific electric circuit parts called &lt;em&gt;resistors&lt;/em&gt;, which come in various resistance values from less than 1 ohm up to several megaohms, and they are used to limit the amount of current flowing through. The circuit diagram symbol of a resistor is shown on the following image.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Resistor symbol" class="aligncenter image-process-responsive" height="156" sizes="(max-width: 300px) 100vw, 300px" src="/images/2014/responsive/1024w/res_symbol.png" srcset="/images/2014/responsive/300w/res_symbol.png 300w, /images/2014/responsive/1024w/res_symbol.png 1024w, /images/2014/responsive/2048w/res_symbol.png 2048w" width="300"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting it all together&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There is a correlation between all these properties. Let’s consider the aforementioned lightbulb as an example. It has a tungsten filament that has &lt;code&gt;R = 660 Ω&lt;/code&gt; resistance because of it’s material and shape. It’s nominal voltage is &lt;code&gt;U = 230 V&lt;/code&gt;, which will cause &lt;code&gt;I = U / R = 230 / 660 = 0.348 A&lt;/code&gt; of current to flow through it. The current will make the tungsten filament to heat up and glow. Lower voltage would result in lower current that would cause the lighbulb to glow much more dimmed. This correlation between current, voltage and resistance is called Ohm’s Law (yes, it’s the same Ohm like the resistance measurement unit, named after Georg Ohm).&lt;/p&gt;
&lt;p&gt;The power of the lightbulb can be calculated from the voltage and current values by multiplicating them: &lt;code&gt;P = U × I = 230 × 0.348 = 80 W&lt;/code&gt;. This means that it consumes 80 J of energy every second and if we turn it on for a whole hour, it will consume &lt;code&gt;E = P × t = 80 W × 1 h = 80 Wh = 0.08 kWh&lt;/code&gt; or &lt;code&gt;E = P × t = 80 J/s × 3600 s = 288 kJ&lt;/code&gt; of electric energy. Diet enthusiasts can further convert it to 69 calories, but it’s not advisable to eat electricity for breakfast.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/2014/responsive/2048w/lightbulb_circuit.png"&gt;&lt;img alt="Circuit with a lightbulb" class="aligncenter image-process-responsive" height="225" sizes="(max-width: 300px) 100vw, 300px" src="/images/2014/responsive/1024w/lightbulb_circuit.png" srcset="/images/2014/responsive/300w/lightbulb_circuit.png 300w, /images/2014/responsive/1024w/lightbulb_circuit.png 1024w, /images/2014/responsive/2048w/lightbulb_circuit.png 2048w" width="300"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I created a circuit involving our lightbulb in an electronic circuit design software and ran the simulation. The picture above shows the results (you can click on it to enlarge). In the schematic there is an external power source of 220 V, a 80 W lightbulb, two &lt;em&gt;voltmeters&lt;/em&gt; and two &lt;em&gt;ammeters&lt;/em&gt; (short for ampere meter). Voltage is always measured between two points, the first voltmeter (&lt;code&gt;U1&lt;/code&gt;) is placed between the power terminals, the other one (&lt;code&gt;U2&lt;/code&gt;) measures the voltage between the two terminals of the lightbulb. On the contrary, to measure current, it must flow through the metering device the same way it flows through the lightbulb. Both ammeters (&lt;code&gt;I1&lt;/code&gt; and &lt;code&gt;I2&lt;/code&gt;) measure the current flowing through the lightbulb, and thus the entire circuit but they are placed to the different sides of the lightbulb.&lt;/p&gt;
&lt;p&gt;If we start the simulation, the lighbulb begins to glow and we can read the values on the meters. It’s no surprise that both voltmeters and both ammeters show the same values, and the values match our calculations above.&lt;/p&gt;
&lt;p&gt;&lt;a href="/images/2014/responsive/2048w/lightbulb_equiv_circuit.png"&gt;&lt;img alt="Circuit with a resistor" class="aligncenter image-process-responsive" height="223" sizes="(max-width: 300px) 100vw, 300px" src="/images/2014/responsive/1024w/lightbulb_equiv_circuit.png" srcset="/images/2014/responsive/300w/lightbulb_equiv_circuit.png 300w, /images/2014/responsive/1024w/lightbulb_equiv_circuit.png 1024w, /images/2014/responsive/2048w/lightbulb_equiv_circuit.png 2048w" width="300"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I also created an equivalent circuit, replacing the lightbulb with a resistor of 660 ohms. I also reversed the polarity (the &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; ends) of the &lt;code&gt;U2&lt;/code&gt; voltmeter and the &lt;code&gt;I2&lt;/code&gt; ammeter, which now show the same values but with the opposite sign. In this circuit, current flows from the higher potential (&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;Mains&lt;/code&gt;) towards the lower one (&lt;code&gt;-&lt;/code&gt;, &lt;code&gt;GND&lt;/code&gt;), but from the ammeter’s perspective the current flows from its &lt;code&gt;-&lt;/code&gt; terminal towards its &lt;code&gt;+&lt;/code&gt; terminal, so it sees reverse current flow which is denoted by the negative sign of the value. Very similarly, the &lt;code&gt;U2&lt;/code&gt; voltmeter sees that it’s &lt;code&gt;-&lt;/code&gt; terminal has higher potential that it’s &lt;code&gt;+&lt;/code&gt; terminal, so the difference between the potentials (aka. voltage) is negative: &lt;code&gt;0 - 220 V = -220 V&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Simulation software is very useful for testing an idea of a circuit before building it. Doing so you can avoid dangerous situations which could harm to you or damage your devices or equipment. Nevertheless, having such a tool, you can bypass lots of math calculations with a trial-and-error method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I hope I could shed a light on the basic principles of electronics. I’ve created a small cheat sheet with the aforementioned physics terms and their relations:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Current (I)          I = U / R        I = P / U
    1 A
Voltage (U)          U = I × R        U = P / I
    1 V
Resistance (R)       R = U / I
    1 Ω
Power (P)            P = U × I
    1 W = 1 J/s
Energy (E)           E = P × t
    1 J = 1 Ws
    1 kWh = 3600 kWs = 3600000 Ws = 3600000 J
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I have discovered a pretty good &lt;a href="http://www.electronics-tutorials.ws/" title="ElectronicsTutorials"&gt;website&lt;/a&gt;, with plenty of well-written electronics tutorials covering all topics that me or you will ever need if doing a similar project. I’ve read all the relevant topics that helped me understand more complex electronics devices like transistors, LEDs and integrated circuits.&lt;/p&gt;</content><category term="Blog"/><category term="circuit"/><category term="current"/><category term="electronics"/><category term="power"/><category term="resistance"/><category term="resistor"/><category term="theory"/><category term="voltage"/></entry><entry><title>My adventures with a LED cube</title><link href="https://peterbudai.com/blog/2014/02/25/my-adventures-with-a-led-cube/" rel="alternate"/><published>2014-02-25T19:54:25+01:00</published><updated>2014-02-25T19:54:25+01:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2014-02-25:/blog/2014/02/25/my-adventures-with-a-led-cube/</id><summary type="html">&lt;p&gt;It all started a few weeks ago when I decided to build my own LED cube. The decision was easy, but the realization won&amp;rsquo;t be that trivial. After some research I had to find out that there is more that I must learn than that I already know.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;It all started a few weeks ago when I decided to build my own LED cube. The decision was easy, but the realization won’t be that trivial. After some research I had to find out that there is more that I must learn than that I already know. I plan this post to be the beginning of a series of articles in which I’m going to present the steps towards a working LED cube and the small pieces of knowledge I gained throughout the process.&lt;/em&gt;
&lt;img alt="Blue LED cube" class="aligncenter image-process-responsive" height="300" sizes="(max-width: 300px) 100vw, 300px" src="/images/2014/responsive/1024w/27-thickbox_default.jpg" srcset="/images/2014/responsive/300w/27-thickbox_default.jpg 300w, /images/2014/responsive/1024w/27-thickbox_default.jpg 1024w, /images/2014/responsive/2048w/27-thickbox_default.jpg 2048w" width="300"/&gt;&lt;/p&gt;
&lt;p&gt;What is a LED cube? Like you can see on the picture above, an 8x8x8 LED cube consists of 512 small lamps (LEDs) arranged in a 3-dimensional structure with 8 layers, 8 columns and 8 rows. There are many good things about a LED cube:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It looks cool, you can program it to show any kind of 3-dimensional animations, which is even cooler.&lt;/li&gt;
&lt;li&gt;You cannot just buy it at any corner (however, you can buy DIY kits which contains everything needed, you just have to assemble it), which makes it a special stuff in your room.&lt;/li&gt;
&lt;li&gt;It’s far from impossible to build by hand, so it’s perfect as a hobby project, even for a beginner like me.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you Google it, you can find many &lt;a href="http://www.youtube.com/watch?v=6mXM-oGggrM" title="LED cube video"&gt;videos&lt;/a&gt;of working LED cubes and some pretty good &lt;a href="http://www.instructables.com/id/Led-Cube-8x8x8/" title="LED cube build instruction"&gt;build instructions&lt;/a&gt;. I always wanted to build one, all I was lacking is determination, but a few weeks ago I took a deep breath and ordered a bunch of LEDs and a microcontroller from China on Ebay.&lt;/p&gt;
&lt;p&gt;As a beginner, even if you follow the build instructions very closely, you may find yourself in trouble at some point without some basic electronics and programming knowledge. If you’re going to diverge from the blueprints in the build instruction, because either you got slightly different parts, or you think you can come up with a better solution, or you just want to educate yourself, you will need some understanding of what’s going on. All the previous statements apply to me.&lt;/p&gt;
&lt;p&gt;Since I’m a software developer, I’m not afraid of the microcontroller programming part, but the electronics stuff. The last time I was dealing with electronics problems was in a forgettable academic course back in my BSc studies accompanied with a 10-hour seminar course where we toyed with a microcontroller and an oscilloscope. Besides that I faintly remember some guy named Ohm and his law from high school physics course. Not much.&lt;/p&gt;
&lt;p&gt;So I started educating myself on the topic, I spent my last couple of weekends with Googling around and browsing Wikipedia. I decided that I’m not going to just let this knowledge fade away, but keep posting some articles with my findings and progress reports of my LED cube here in my blog. So stay tuned!&lt;/p&gt;</content><category term="Blog"/><category term="announcement"/><category term="beginner"/><category term="led cube"/></entry><entry><title>BitLocker on non-system partition</title><link href="https://peterbudai.com/blog/2013/01/19/bitlocker-on-non-system-partition/" rel="alternate"/><published>2013-01-19T21:36:08+01:00</published><updated>2013-01-19T21:36:08+01:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2013-01-19:/blog/2013/01/19/bitlocker-on-non-system-partition/</id><summary type="html">&lt;p&gt;Microsoft&amp;rsquo;s full disk encryption software can leverage the functionality of the computers built-in TPM chip. Bacause of this you won&amp;rsquo;t have to type in yet another password every time you start the computer while the data on your hard disk still remains protected. But it is not so simple when you only want to encrypt your secondary partition.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;I know that nowadays passwords are indispensable for keeping other people away from my precious data, but sometimes it gets really frustrating typing them again and again, so I seize every option that offers me the same level of security without having to remember a new password. My laptop has a TPM chip built into it so when I was looking for an encryption software to keep my valuable private data safe in that unfortunate case when I lose my laptop or it gets stolen, I choose a solution that can leverage the TPM’s functionality.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;BitLocker is a full disk encryption software by Microsoft that is bundled with every higher-grade versions of the Windows operating systems since Vista. In encrypts a whole disk partition which then can be decrypted typing a password (called PIN in BitLocker terminology), inserting an external USB drive, using the key stored in the &lt;a href="http://en.wikipedia.org/wiki/Trusted_Platform_Module"&gt;TPM&lt;/a&gt; hardware or the combinations of two or all three of them. I choose the TPM-only authentication mode, that works transparently without any user interaction &lt;em&gt;as long as the hard disk remains in my laptop and it boots from it&lt;/em&gt;. If any part of the pre-boot environment changes BitLocker will refuse to decrypt the drive. Please note that &lt;em&gt;TPM-only authentication mode is not recommended&lt;/em&gt; because it is vulnerable to some kind of cold-boot attacks. These attacks all have in common that they require that my laptop is still running when the attacker acquires it. I decided to take this risk for the convenience.&lt;/p&gt;
&lt;p&gt;My laptop has two hard drives: an SSD (primary volume) on which only the operating system and program files reside for faster system and software startup times, and a standard HDD (secondary volume) for all the data, my documents, source codes, photos, etc. My original intention was to enable BitLocker only on this secondary partition, because the system partition does not contain any sensitive information and the encryption would have some minimal performance overhead.&lt;/p&gt;
&lt;p&gt;While setting up the BitLocker drive encryption using the built-in wizard of Windows 8 Pro that is installed on my notebook, I noticed with a great surprise that no TPM-only option was present on the authentication-mode selection screen. I double checked that the TPM hardware is functioning properly, so I started digging up the internet for a solution or an explanation.&lt;/p&gt;
&lt;p&gt;As it turned out, this is because the way TPM and BitLocker operate together by forming a chain of protection. TPM protects (detects any change to) the pre-boot environment which consist of the computer hardware, firmware (including BIOS) and operating system boot loader. The boot loader protects the essential operating system files and the system partition if BitLocker is enabled. And finally, the system partition protects any other volumes. The key for the secondary partition is not stored in the TPM chip itself but somewhere on the system partition, so for the secondary volume to be decrypted automatically the system volume must be encrypted as well.&lt;/p&gt;
&lt;p&gt;At the end I decided to encrypt both volumes, this time the TPM-only option was present for the secondary partition, too. Now my private data is safe on my hard drive without requiring to enter another password (besides Windows logon password of course, never have your operating system without any logon password, really, don’t). It’s worth noting that Windows 8 has a new BitLocker feature (at least I don’t remember seeing this on the Windows 7 computer at my workplace) that allows the user to encrypt only the currently allocated parts of the hard disk for the first time BitLocker is enabled. This is useful for newly purchased disk drives (like my new SSD) as it greatly reduces the time needed to enable BitLocker drive encryption.&lt;/p&gt;
&lt;p&gt;Finally I got so much into data encryption, that I also BitLocked my USB flash drive. To be honest, there is much more chance to have that tiny flash drive lost or stolen than the laptop. Flash drives can only be decrypted with a password, however, there is a convenience option that they can be automatically unlocked on computers with BitLocker-enabled system volumes. With this option it is possible that my flash drive can be used on my laptop without any further action, but it requires a password on every other machines.&lt;/p&gt;</content><category term="Blog"/><category term="bitlocker"/><category term="hdd"/><category term="partition"/><category term="password"/><category term="ssd"/><category term="tpm"/><category term="windows"/><category term="windows 8"/></entry><entry><title>Windows API at its best</title><link href="https://peterbudai.com/blog/2012/07/18/windows-api-at-its-best/" rel="alternate"/><published>2012-07-18T18:54:58+02:00</published><updated>2012-07-18T18:54:58+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2012-07-18:/blog/2012/07/18/windows-api-at-its-best/</id><summary type="html">&lt;p&gt;Whether you are a Windows programmer or not, you probably have heard about Windows API. The opinions on the API and Hungarian notation are controversial, but one thing is certain: Hungarian notation gave rise to some very interesting type names.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;With the advent of object-oriented programming frameworks for Windows like MFC or, most recently, .NET Framework, the average programmer doesn’t have to deal with low-level Windows API programming. Nevertheless, you can still do everything with C/C++ and pure Windows API, but with a little more work, of course.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I’m not willing to start a debate whether Windows API or Hungarian notation is good or bad. Personally, I prefer the descriptive, camelcased function names (&lt;code&gt;CreateFile&lt;/code&gt;) in Windows API to the cryptic, as short as possible UNIX/Linux standard library functions (&lt;code&gt;fopen&lt;/code&gt;), but that’s just a matter of taste.
Every long-lived software project has its legacy, and this is exceptionally true for operating systems where backward compatibility is essential. The Windows API carries a lot of old stuff, one of it is the Hungarian notation, it’s there, you can’t avoid it, but you get used to it pretty soon. But there are also the API functions from the 16-bit era, long pointer types (like &lt;code&gt;LPSTR&lt;/code&gt;), or the infamous &lt;code&gt;TCHAR&lt;/code&gt; type and related function macros.&lt;/p&gt;
&lt;p&gt;The reason why all this got into my mind is that a few weeks ago I stumbled upon a &lt;a href="http://stackoverflow.com/q/11009997/509303"&gt;question on StackOverflow&lt;/a&gt; which mentioned an interesting Windows API typename, &lt;code&gt;PCZZTSTR&lt;/code&gt;. The original poster even called it scary, and to be honest, he was right to some extent. Nevertheless, with a little WinAPI knowledge and some googling around (it’s not a widely used type), one is able to “decrypt” this typename to a more digestible description:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;P&lt;/code&gt; is for pointer,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CZ&lt;/code&gt; means C-style string which is zero-terminated,&lt;/li&gt;
&lt;li&gt;the second &lt;code&gt;Z&lt;/code&gt; signs that the string must be terminated with another zero (&lt;code&gt;'\0'&lt;/code&gt; character), like it would be some kind of string of strings,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;T&lt;/code&gt; means that the string consists of &lt;code&gt;TCHAR&lt;/code&gt;s, which are &lt;code&gt;char&lt;/code&gt;s on pre Windows NT systems and &lt;code&gt;wchar_t&lt;/code&gt;s on newer Windowses,&lt;/li&gt;
&lt;li&gt;finally, &lt;code&gt;STR&lt;/code&gt; simply stands for string, which may be redundant after all the previous characters, but it’s there for consistency with other string types in the API.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So &lt;code&gt;PCZZTSTR&lt;/code&gt; essentially means just a simple C-style string of &lt;code&gt;TCHAR&lt;/code&gt;s, which is terminated with two ‘\0’ characters. It’s not so scary anymore, is it?&lt;/p&gt;</content><category term="Blog"/><category term="api"/><category term="c"/><category term="hungarian notation"/><category term="windows"/></entry><entry><title>Base class as method argument</title><link href="https://peterbudai.com/blog/2012/06/09/base-class-as-method-argument/" rel="alternate"/><published>2012-06-09T14:00:49+02:00</published><updated>2012-06-09T14:00:49+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2012-06-09:/blog/2012/06/09/base-class-as-method-argument/</id><summary type="html">&lt;p&gt;Modern object-oriented programming languages won&amp;rsquo;t allow you to access protected members of base classes when they are used as parameters in the derived class. At first you may find this strange, but I&amp;rsquo;ll try to explain the reasoning behind this behaviour.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;In modern object-oriented programming languages, protected members had always been treated as a kind of necessary evil, as they have more disadvantage than advantage. Almost all coding style guides will discourage you from using them. In this post, I’ll show another issue related to protected members which at first may seem unexpected behaviour, but on the other hand it is perfectly reasonable.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let’s start with a short example, consider the following C++ code excerpt:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;class Base {
protected:
    int prot;

public:
    int getProt() {
        return prot;
    }
};

class Derived : public Base {
private:
    int priv;

public:
    void setThat(Derived* derived) {
        derived-&amp;gt;prot = this-&amp;gt;prot;
    }

    void setThis(Base* base) {
        this-&amp;gt;prot = base-&amp;gt;prot;
    }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What you see here is two classes, &lt;code&gt;Base&lt;/code&gt; with a protected attribute &lt;code&gt;prot&lt;/code&gt;, and &lt;code&gt;Derived&lt;/code&gt; which inherits this protected attribute from its base class. The two methods, &lt;code&gt;setThis&lt;/code&gt; and &lt;code&gt;setThat&lt;/code&gt; both access the protected member &lt;code&gt;prot&lt;/code&gt; of their argument, the only difference is the static type of the argument (&lt;code&gt;Base*&lt;/code&gt; and &lt;code&gt;Derived*&lt;/code&gt;, respectively). At first sight, there is no problem with this code. However, when you try to compile it, the compiler will issue an error (I used &lt;em&gt;gcc&lt;/em&gt; in this example, but I also tried &lt;em&gt;MSVC&lt;/em&gt;, the result was the same but the error message was not so helpful):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;test.cpp: In member function ‘void Derived::setThis(Base*)’:
test.cpp:4: error: ‘int Base::prot’ is protected
test.cpp:22: error: within this context
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Why is that error message, you could ask, what’s wrong? &lt;code&gt;Base::prot&lt;/code&gt; is protected, &lt;code&gt;Derived&lt;/code&gt; inherits from &lt;code&gt;Base&lt;/code&gt;, so methods in &lt;code&gt;Derived&lt;/code&gt; should be able to access protected attributes from &lt;code&gt;Base&lt;/code&gt;. That’s right, but there is a catch. If you look at the error message, you will see that the error is caused by the &lt;code&gt;setThis&lt;/code&gt; method, the &lt;code&gt;setThat&lt;/code&gt; method compiles just fine.&lt;/p&gt;
&lt;p&gt;Deriving from a base class does not mean that the derived class will be able to access the protected members of the base class. It means that the derived class will be able to access &lt;strong&gt;its own members&lt;/strong&gt; that were &lt;strong&gt;inherited from&lt;/strong&gt; the base class and have at least protected visibility.&lt;/p&gt;
&lt;p&gt;This is why the &lt;code&gt;setThat&lt;/code&gt; method compiles without any issues. It accesses the protected member from the same class (&lt;code&gt;Derived&lt;/code&gt;) where the method resides, this would also work with private members (&lt;code&gt;priv&lt;/code&gt;, for example). On the other hand, &lt;code&gt;setThis&lt;/code&gt; tries to access a protected member in &lt;code&gt;Base&lt;/code&gt;, which is not allowed by the otherwise obvious visibility rules.&lt;/p&gt;
&lt;p&gt;I was curious and also tried the same code in C# and Java (after trimming it according to the language specifics, of course). In C# the result was the same, as I expected. But surprisingly, the Java compiler happily compiled the source code without any error. At first I thought that maybe Java treated protected members differently than the other two languages, but then I realized that the classes were in the same package. And because in Java the default (package-level) visibility is stronger than the protected visibility, &lt;code&gt;Derived&lt;/code&gt; was able to access the &lt;code&gt;prot&lt;/code&gt; attribute regardless whether it is protected or not. This is yet another reason to use protected members with caution: in Java, they are visible from outside of the package, and this is usually not desired.&lt;/p&gt;
&lt;p&gt;Back to the example, all I had to do is to separate the two classes to different packages. This is the &lt;code&gt;Base&lt;/code&gt; class:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;package base;

public class Base {
    protected int prot;

    public int getProt() {
        return prot;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here comes the source code of the &lt;code&gt;Derived&lt;/code&gt; class:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;package derived;

import base.Base;

public class Derived extends Base {
    private int priv;

    public void setThat(Derived derived) {
        derived.prot = this.prot;
    }

    public void setThis(Base base) {
        this.prot = base.prot;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that package visibility was out of scope, I was able to reproduce the expected behaviour with the Java compiler as well:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;derived\Derived.java:13: prot has protected access in base.Base
            this.prot = base.prot;
                            ^
1 error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I don’t agree that protected members are evil, but there are inevitably some issues around them that have to be considered when designing classes and object-oriented programs.&lt;/p&gt;</content><category term="Blog"/><category term="concept"/><category term="cplusplus"/><category term="csharp"/><category term="java"/><category term="oop"/></entry><entry><title>Website launch</title><link href="https://peterbudai.com/blog/2012/05/08/website-launch/" rel="alternate"/><published>2012-05-08T07:00:37+02:00</published><updated>2012-05-08T07:00:37+02:00</updated><author><name>Peter Budai</name></author><id>tag:peterbudai.com,2012-05-08:/blog/2012/05/08/website-launch/</id><summary type="html">&lt;p&gt;I&amp;rsquo;m glad to announce that the peterbudai.eu website has been launched. Contents are already on the way, however, some minor outages and changes may be expected for a few days.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;I’m glad to announce that the &lt;a href="/" title="peterbudai.eu"&gt;peterbudai.eu&lt;/a&gt; website has been launched.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By this time, most of the hardware and software environment had been established and considered stable. There are still some work to do, mostly on improving and unifying the design all across the website, so short temporary outages and continous changes may still be expected for a couple of days.&lt;/p&gt;
&lt;p&gt;I’m continuously filling up the blog and the website home page as well with contents, so it’s worth visiting the site regularly from now on. An even more convenient way to subscribe to blog updates using the RSS feed on the right.&lt;/p&gt;</content><category term="Blog"/><category term="announcement"/><category term="meta"/></entry></feed>