<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrew Buntine</title>
    <description>The latest articles on DEV Community by Andrew Buntine (@buntine).</description>
    <link>https://dev.to/buntine</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F526%2F14033.png</url>
      <title>DEV Community: Andrew Buntine</title>
      <link>https://dev.to/buntine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/buntine"/>
    <language>en</language>
    <item>
      <title>The historical obscurity of the SEGA SC-3000 Personal Computer</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Mon, 08 Jan 2018 08:55:49 +0000</pubDate>
      <link>https://dev.to/buntine/the-historical-obscurity-of-the-sega-sc-3000-personal-computer-1hac</link>
      <guid>https://dev.to/buntine/the-historical-obscurity-of-the-sega-sc-3000-personal-computer-1hac</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is part of a series on the retro SEGA SC-3000 personal computer. In the next part I will give an introduction to writing BASIC on this machine. In the last part we will make a simple shoot-em-up video game and persist it to audio cassette.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It was an otherwise average hot summers evening in Melbourne, Australia when I heard a "knock, knock" at the door.&lt;/p&gt;

&lt;p&gt;I saw my neighbour standing in front of me with a large plastic bag in his hand. Upon closer inspection, I could see the plastic bag seemed to contain a stack of old video games and assorted peripherals. My eyes lit up a bit but I tried to hold my excitement.&lt;/p&gt;

&lt;p&gt;He went on to tell me that he was doing some demolition work in an abandoned house in the outer suburbs when he'd stumbled upon an "old computer or something". He knew I was into that type of thing so he asked if I wanted it.&lt;/p&gt;

&lt;p&gt;Did I want it?! Hell yes, I did. I've always been a huge fan of retro computer gear. Finding something lke that in an abandoned house is like treasure to me!&lt;/p&gt;

&lt;p&gt;"OK, compose yourself Andrew", I thought to myself.&lt;/p&gt;

&lt;p&gt;I ran inside and grabbed a ~$300 bottle of &lt;a href="https://www.domperignon.com/ww-en/champagne/vintage/dom-perignon-vintage/2006" rel="noopener noreferrer"&gt;Dom Pérignon&lt;/a&gt; that I'd been given when I started my last job. After some convincing, he took the bottle and we called it a fair trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better than expected
&lt;/h2&gt;

&lt;p&gt;When I got inside and emptied the contents of the plastic bag on my loungeroom floor, I saw it was a lot more than I'd initially realised. In the bag there was a retro minicomputer, some RF cables, an AC adapter, five software cartridges, a joystick and a game pad. All boxed and in near mint condition. It was like a time capsule from simpler times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz944v863j5yq55nckpc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz944v863j5yq55nckpc.jpg" alt="The SEGA SC-3000H haul"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait, a SEGA computer?
&lt;/h2&gt;

&lt;p&gt;One thing that really stood out to me was the logo on each of the games and the computer itself: &lt;strong&gt;SEGA&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since when did SEGA make home computers?&lt;/p&gt;

&lt;p&gt;When I was a little kid, the first video game console I'd owned was a &lt;a href="https://en.wikipedia.org/wiki/Master_System" rel="noopener noreferrer"&gt;SEGA Master System&lt;/a&gt; (or Mark III in Japan). It was a great little system with a good catalogue of games developed for it. What I did not know, however, was that before the &lt;a href="https://en.wikipedia.org/wiki/Sega_Genesis" rel="noopener noreferrer"&gt;Genesis&lt;/a&gt;, before the &lt;a href="https://en.wikipedia.org/wiki/Sega_Saturn" rel="noopener noreferrer"&gt;Saturn&lt;/a&gt; and even before the Master System - SEGA released a home computer called the &lt;a href="https://segaretro.org/SC-3000" rel="noopener noreferrer"&gt;SC-3000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Amazing... But there was also something else that stood out. The machines full name was the &lt;strong&gt;"John Sands SEGA SC-3000H Personal Computer"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For those who are not aware, John Sands is an Australian supplier of greeting cards and gift wrap. Just &lt;a href="http://johnsands.com/" rel="noopener noreferrer"&gt;take a look at their website&lt;/a&gt; - it doesn't look much like a computer distributor, does it? Well, sure enough, in the early 80's John Sands really did &lt;a href="https://www.youtube.com/watch?v=4HbYyCj3gLE" rel="noopener noreferrer"&gt;enter the fledgling home computer market&lt;/a&gt; when they helped SEGA distribute this machine and a video game equivalent called the &lt;a href="https://segaretro.org/SG-1000" rel="noopener noreferrer"&gt;SG-1000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3slzkntoqrgfjf74d8nr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3slzkntoqrgfjf74d8nr.jpg" alt="Alien - Dotsoft / Softgold 1984"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mind blown...&lt;/p&gt;

&lt;h2&gt;
  
  
  OK, gimme' the nitty gritty details
&lt;/h2&gt;

&lt;p&gt;The reason you've probably never heard of this machine is because the SEGA SC-3000 was released in just a few markets: Japan, Australia, New Zealand, France, Italy and Finland.&lt;/p&gt;

&lt;p&gt;It did not fare particularly well in Europe but saw some minor success in Japan and Australia/NZ. Infact, the SC-3000 represents SEGAs &lt;em&gt;first and last&lt;/em&gt; foray into the personal computer market. And although the SC-3000 was marketed as a tool primarily for home business and educational use, it was seemingly only ever used to play video games. What a surprise...&lt;/p&gt;

&lt;p&gt;The machine was released in two models: The SC-3000 (with rubberized keys) and the SC-3000H which boasts a fully mechanical keyboard (and a pretty nice one, I must say!). Each market seems to have got it's own colour variant, also. All of the John Sands Australian models are black. Supposedly the white models are the rarest.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh8fien16dcuvcpiqkrs8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh8fien16dcuvcpiqkrs8.jpg" alt="Back of the SEGA SC-3000"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchcgkuf5j1k5y1o2irdu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchcgkuf5j1k5y1o2irdu.jpg" alt="Left side of the SEGA SC-3000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the back there is a power switch, a DC-in, composite video port, printer port, audio cassette in/out ports (I'll discuss those a bit more later) and an RF connector port for plugging into your television.&lt;/p&gt;

&lt;p&gt;On one side there is a large space to put software cartridges. On the other there is two standard joystick ports.&lt;/p&gt;

&lt;p&gt;On the inside there is an 8-bit NEC D780C-1 CPU running at 4 MHz. I believe it's identical to the more well known Z80 CPU that saw massive use during the 80s in scientific calculators and many arcade machines.&lt;/p&gt;

&lt;p&gt;Embedded in the machine is just a measly 2Kb of RAM. Along with 16Kb of VRAM provided for the video display processor (a Texas Instruments TMS9929A). Perhaps a surprise to some people, but the software cartridges actually provided their own RAM (anywhere from 1Kb up to 32Kb). While a game was running, it would read and write via the RAM on it's own cartridge. So, unfortunately that means saving your game progress and coming back later was simply out of the question!&lt;/p&gt;
&lt;h2&gt;
  
  
  And how about the software?
&lt;/h2&gt;

&lt;p&gt;The SEGA SC-3000 has no operating system. In fact, if you turn it on without a cartridge stuffed in the side of it you will get just a blank screen of nothingness.&lt;/p&gt;

&lt;p&gt;As I mentioned previously, the main way of distributing software was via cartridge. The SC-3000 cartridge slot was also compatible with games for the earlier &lt;a href="https://en.wikipedia.org/wiki/SG-1000%20video%20game%20system" rel="noopener noreferrer"&gt;SG-1000&lt;/a&gt; and the &lt;a href="https://en.wikipedia.org/wiki/Sega_Card#My_Card_Mark_III_/_Sega_Card" rel="noopener noreferrer"&gt;SEGA "my card"&lt;/a&gt; format.&lt;/p&gt;

&lt;p&gt;I found out the hard way that these cartridges tend to get dirty (even when left in their box) and so I spent quite some time cleaning them with cotton buds before I was able to get each to work. It was very nostalgic to sit in my living room blowing into cartridges and wiggling them around. It definitely brought back memories of my childhood!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk6tiilip6ft6ksmluyx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk6tiilip6ft6ksmluyx.jpg" alt="SEGA SC-3000 BASIC cartridge"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn15aris6sradk574av9l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn15aris6sradk574av9l.jpg" alt="SEGA SC-3000 BASIC level 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I believe there was less than 100 officially released games for the SC-3000/SG-1000 systems, which I can only assume means they are probably pretty rare nowadays. Although, somewhat amazingly, there is a group of fans who have provided many of the tapes dumps as uncompressed audio files on over at the &lt;a href="http://sc3000-multicart.com/sc3000-tape-restoration-project.htm" rel="noopener noreferrer"&gt;SC-3000 Survivors website&lt;/a&gt;. This means you can actually play many lost SEGA games on this retro machine via the Internet. Wow!&lt;/p&gt;
&lt;h2&gt;
  
  
  How do I hack on this thing?
&lt;/h2&gt;

&lt;p&gt;Like many home computers of the time, the SC-3000 was intended to be programmed via the &lt;a href="https://en.wikipedia.org/wiki/BASIC" rel="noopener noreferrer"&gt;BASIC&lt;/a&gt; programming language. From my research, it seems to me that all models were sold with a BASIC cartridge. There were a few separate BASIC cartridges available, the difference lying in the amount of RAM available. The most "basic" release had just 1Kb (of which 512 bytes were taken by the BASIC runtime itself). The other releases has 16Kb and 32Kb, respectively. Many adverts of the SC-3000 at the time boasted of 48Kb of RAM - but this would only be the case if you also bought the most expensive BASIC cartridge!&lt;/p&gt;

&lt;p&gt;The SC-3000 also allowed for one relatively interesting means of I/O in conjunction with the BASIC cartridge - via standard audio cassettes. The software was written to the tape bit-by-bit, each taking just under 1ms (833.3μs) to record. This resulted in roughly 6.8 seconds of audio per kilobyte of data. So reading a 16Kb game from cassette and onto the memory on the BASIC cartridge would take nearly 2 minutes...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsad83ji5gbb6f0a4ycc7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsad83ji5gbb6f0a4ycc7.jpg" alt="SEGA data recorder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once your cassette player had been plugged into the SC-3000 via 3.5mm audio cable, it could be read into memory and executed very simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOAD some-game
RUN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;Hacking, of course. But for that we will have to wait until the next post.&lt;/p&gt;

</description>
      <category>history</category>
      <category>computers</category>
      <category>basic</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Laser Drift: Reverse engineering slot cars for fun and (maybe) profit!</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Mon, 28 Aug 2017 12:29:49 +0000</pubDate>
      <link>https://dev.to/buntine/laser-drift-reverse-engineering-slot-cars-for-fun-and-no-profit</link>
      <guid>https://dev.to/buntine/laser-drift-reverse-engineering-slot-cars-for-fun-and-no-profit</guid>
      <description>&lt;p&gt;&lt;strong&gt;In this blog series I discuss the creation of &lt;a href="https://github.com/buntine/laser-drift" rel="noopener noreferrer"&gt;Laser Drift&lt;/a&gt;, a system that allows you to remotely control any Carrera Digital slot cars by emulating the infrared wireless controllers for between one and four players in realtime.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Feel free to take a look at my promo video or the &lt;a href="https://github.com/buntine/laser-drift" rel="noopener noreferrer"&gt;Github repo&lt;/a&gt; before you read.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VmsQ4c20vLg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;A couple of months ago I was in a brainstorming meeting with the Creative Director, the Head of Strategy and an assortment of other tech people. This is a volatile melting pot of skills that in the past has produced many amazing things (and the odd Frankenstein). Our client had tasked us with "doing something awesome with one of &lt;a href="https://en.wikipedia.org/wiki/Brain%E2%80%93computer_interface" rel="noopener noreferrer"&gt;those BCI devices&lt;/a&gt;". For those unaware, &lt;strong&gt;B&lt;/strong&gt;rain-&lt;strong&gt;C&lt;/strong&gt;omputer &lt;strong&gt;I&lt;/strong&gt;nterface devices measure electrical activity in the brain and provide an SDK for performing computations based on simple thoughts, emotions and physical gestures.&lt;/p&gt;

&lt;p&gt;These open briefs are generally great, but it can be difficult to come up with a viable idea that satisfies everyones concept of what constitutes awesomeness and (God forbid) fits within the proposed budget.&lt;/p&gt;

&lt;p&gt;We eventually settled upon the idea of &lt;strong&gt;brain powered &lt;a href="https://en.wikipedia.org/wiki/Slot_car" rel="noopener noreferrer"&gt;slot car&lt;/a&gt; racing&lt;/strong&gt;, in which a user would think certain thoughts like "push" in order to make the cars move. So, in a nutshell, two to four players would race against each other and the first player to &lt;em&gt;n&lt;/em&gt; laps would win. We felt this would work well because (as those of us old enough to remember already know) turning is automatic in slot car systems. Users had only to move forward and the rest would be be taken care of.&lt;/p&gt;

&lt;p&gt;This idea also satisfied a few other prerequisites - most importantly it would allow users to interact with the physical world. The client, perhaps correctly, felt that it would be more impressive if the BCI device was able to move physical objects opposed to digital ones.&lt;/p&gt;

&lt;p&gt;To me, this idea actually involved two separate projects. First: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PC -&amp;gt; Slot Cars&lt;/strong&gt;: We needed to create software to accurately control up to four players on a slot car set using simple, language-agnostic commands&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And second:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BCI -&amp;gt; PC&lt;/strong&gt;: We needed to create software to listen for mental commands like "push" and "pull" and then translate them into those simple commands I mentioned above&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this series I will discuss the former as it is by far the most interesting and challenging of the two - even though "mind control" may sound cooler in a press release.&lt;/p&gt;

&lt;p&gt;The project manifested itself in three phases. In this post I will discuss the first two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Research and Experimentation&lt;/li&gt;
&lt;li&gt;Reverse Engineering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a separate post I will discuss the creation of the solution itself: &lt;a href="https://github.com/buntine/laser-drift" rel="noopener noreferrer"&gt;Laser Drift&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Research and Experimentation
&lt;/h2&gt;

&lt;p&gt;The only experience my colleagues and I had with slot cars were distant childhood memories of analogue cars zipping around single-lane tracks. And so the first task at hand was to find out what a state of the art slot car set looked like in 2017.&lt;/p&gt;

&lt;p&gt;Wow! They've really &lt;a href="https://www.youtube.com/watch?v=jbQQT5sxGus" rel="noopener noreferrer"&gt;come a long way&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the old days, each car needed to occupy it's own lane. And if any two cars got in the same lane, they would always travel at exactly the same speed. They were fun, but relatively monotonous toys.&lt;/p&gt;

&lt;p&gt;But that was the past! For in about 2004, the major manufacturers started introducing digital systems in which each car could be uniquely identified (even if they occupied the same lane). So now players can freely change lanes, simulate oil spills, have pit stops, configure braking performance and even represent real-world concepts like fuel levels and tyre blowouts. All of this was going to allow for a whole new dimension of &lt;em&gt;badassery&lt;/em&gt; in the end result.&lt;/p&gt;

&lt;p&gt;The next point of call was to decide was which slot car manufacturer we would go with. As it turns out, there are two main players in the slot car world: &lt;a href="https://www.scalextric.com/uk-en/" rel="noopener noreferrer"&gt;Scalextric&lt;/a&gt; and &lt;a href="http://www.carrera-toys.com/en/" rel="noopener noreferrer"&gt;Carrera&lt;/a&gt;. The general sentiment among enthusiasts is that Scalextric is for the older, hobbyist demographic whilst Carrera is for the younger crowd who just want to have some fun. But both options are of a very high quality.&lt;/p&gt;

&lt;p&gt;After some looking around, I found that Carrera offered a bluetooth interface called the &lt;a href="http://www.carrera-toys.com/en/products/digital-132/accessories/appconnect-538/" rel="noopener noreferrer"&gt;AppConnect&lt;/a&gt; that would allow one to program car speeds, set fuel levels and perform various other tasks. One Thomas Kemmer had also released &lt;a href="https://github.com/tkem/carreralib" rel="noopener noreferrer"&gt;Python bindings&lt;/a&gt; into the underlying protocol, whch meant I could easily whip up a python program to control the cars. I was starting to think this whole thing may be much easier than I initially thought...&lt;/p&gt;

&lt;p&gt;Oh, ignorance is bliss.&lt;/p&gt;

&lt;p&gt;My elation was quickly extinguished when I realised that by "programming car speeds", Carrera had meant that the bluetooth interface can be used to &lt;strong&gt;pre-program&lt;/strong&gt; cars with individual top speeds before a race. It's of no use &lt;em&gt;during&lt;/em&gt; a race! Alas, we still ordered a new (and pretty damned expensive!) &lt;a href="http://www.carrera-toys.com/en/products/digital-132/sets/gt-championship-1300/years-2016/" rel="noopener noreferrer"&gt;Carrera Digital 132 GT Championship Wireless+&lt;/a&gt; set and the bluebooth adapter regardless. This particular set came with wireless controllers, which communicated with the control unit on the track via infrared (atleast, that is what I had read on the official forums). &lt;/p&gt;

&lt;p&gt;At this point I realised that there was no official way of using a PC to control each car in realtime. And so we hit the drawing board.&lt;/p&gt;

&lt;p&gt;The first idea was to physically press the accelerator on the &lt;a href="http://www.carrera-toys.com/fileadmin/user_upload/products/acc/zubehoehr_42012_.jpg" rel="noopener noreferrer"&gt;controllers&lt;/a&gt; using something like a &lt;a href="https://www.motiondynamics.com.au/images/magictoolbox_cache/80543c918ed05ba09145ba56f4b3651f/3/0/30444/original/3335263249/50_light_industrial_jqjh-k3.jpg" rel="noopener noreferrer"&gt;linear actuator&lt;/a&gt;. The controllers themselves would be housed in a chassis of some kind and we'd use a hobbyist microcontroller like an &lt;a href="https://www.arduino.cc/" rel="noopener noreferrer"&gt;Arduino&lt;/a&gt; to press down on each controller when desired. If you watch the first 20 seconds of &lt;a href="https://www.youtube.com/watch?v=gnLSE44leyU" rel="noopener noreferrer"&gt;this video&lt;/a&gt; it should be obvious how this may work.&lt;/p&gt;

&lt;p&gt;This would have the benefit of not requiring us to open the hood of the Carrera set but simply to emulate the pressing of a thumb. Although it did require a whole bunch of additional hardware for each controller and some specialised skills that no one on the project professed to have. It also didn't give us the accuracy we were really after as the actuators we found tended to &lt;em&gt;actuate&lt;/em&gt; very slowly.&lt;/p&gt;

&lt;p&gt;The next idea was to reverse engineer the controllers infrared signals. We could emulate the entire array of controllers as long as we could send the right data packets at the right time. I mean, the controllers are basically like really simple TV remotes, right? This would require some hardware aswell, but atleast there would be no soldering involved.&lt;/p&gt;

&lt;p&gt;I did some additional research online and I was able to find &lt;a href="http://www.slotbaer.de/index.php/carrera-digital-124-132/13-d132-d124-infrarot-regler-protokoll" rel="noopener noreferrer"&gt;some brialliant work&lt;/a&gt; by Stephan HeÃŸ in which he showed the basic contents of the infrared packets sent by the controllers. This meant that we already had some of the reverse engineering completed for us. And I already knew that cheap USB-based IR transceivers like &lt;a href="http://www.irdroid.com/wp-content/uploads/2013/11/IMG_0586-Small.jpg" rel="noopener noreferrer"&gt;this one from Irdroid&lt;/a&gt; were readily available.&lt;/p&gt;

&lt;p&gt;So it was decided: &lt;strong&gt;We would use an infrared transceiver to emulate the data sent by the controllers to the control unit. One simple device would act as all of the controllers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One additional curve ball was thrown my way when I took a closer look at the controllers we had. They were not the 38khz IR controllers I had read about but actually 2.4ghz RF! This was going to make communication a whole lot more difficult. As it turns out, the IR controllers were decommissioned in 2013 and replaced with the superior RF models. Luckily, it seemed that the new control units still supported the older controllers. So I got en ebay and bought the &lt;a href="http://www.professormotor.com/v/vspfiles/photos/CAR10103-2.jpg" rel="noopener noreferrer"&gt;older gear&lt;/a&gt; that I wanted.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reverse Engineering
&lt;/h2&gt;

&lt;p&gt;The IR transceiver I purchased was the &lt;a href="http://www.irdroid.com/" rel="noopener noreferrer"&gt;Irdroid&lt;/a&gt;. Interestingly, it's handmade in Bulgaria in repurposed SD card readers. The reason I chose this device was because it's supported out of the box by the predominant infrared tool suite for Linux environments, &lt;a href="http://lirc.org" rel="noopener noreferrer"&gt;Lirc&lt;/a&gt;. LIRC is a package that allows you to decode and send infrared signals of many (but not all) commonly used remote controls. A full introduction to it is beyond the scope of this article, but it's sufficient to just know that lirc gives you the ability to send and receive IR blasts in a Linux environment.&lt;/p&gt;

&lt;p&gt;An important part of reverse engineering is to take small steps. Try to get a general idea of how the device works before trying to reach the end goal. For me, the first port of call was to see what the information coming from the controllers looked like. To do this, I used a low-level tool that lirc provides called &lt;a href="https://linux.die.net/man/1/mode2" rel="noopener noreferrer"&gt;mode2&lt;/a&gt;. Mode2 simply listens for raw IR data and prints it to &lt;code&gt;STDOUT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let me explain what IR data looks like.&lt;/p&gt;

&lt;p&gt;Infrared devices typically encode their message to the receiver via a method called Pulse Code Modulation (PCM) in which an IR-emitting LED is switched on and off rapidly. When the transmitter is "off", we call it a space. And when the transmitter is "on", we call it a pulse. There are a few common methods for encoding binary into pulses and spaces, but all involve transitioning between said pulses and spaces.&lt;/p&gt;

&lt;p&gt;In my case, I expected to see a random looking series of pulses and spaces being printed to &lt;code&gt;STDOUT&lt;/code&gt;. But I got nothing. No matter what I did, I could not get any output from the controller. I spent the next hour troubleshooting why the Carrera control unit could pick up on the IR data but I couldn't. It wasn't until I pointed my IR transceiver at the &lt;em&gt;control unit itself&lt;/em&gt; that I realised there was more to the story.&lt;/p&gt;

&lt;p&gt;The control unit (well, technically the IR device on the unit) was actually sending out the following signals every ~70 milliseconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 800
space 700
pulse 550
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The numbers are microseconds. What we are seeing here is a pulse of 800Î¼s, a space of 700Î¼s and then a pulse of 550Î¼s. This was repeated ad infinitum.&lt;/p&gt;

&lt;p&gt;As it turns out, the controllers only respond when they are instructed to by the control unit. Pressing the button does not actually cause an IR transmission. It just updates some state on the microcontroller in the controller itself. This, I assume, is important for synchronising the controllers so that their transmissions do not get tangled on the receiving end.&lt;/p&gt;

&lt;p&gt;Cool. So the next thing I did was transmit the syncing blast from my transceiver to the controller and see what it returned. This is what I got back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 1000
pulse 250
space 250
pulse 250
space 250
pulse 500
space 250
pulse 250
space 500
pulse 250
space 250
pulse 250
space 250
pulse 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the suggestion of &lt;a href="https://dtcristo.com/" rel="noopener noreferrer"&gt;a friend&lt;/a&gt;, I wrote a little script to visualise this with a step graph where a pulse is high and a space is low.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw549bw9wphzjrzmcv99r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw549bw9wphzjrzmcv99r.png" alt="Packet 1 visualised"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I performed this test a bunch of times. I could see that the transmission repeated every ~60 ms. And it always started with a pulse of 300 followed by a space of either 750 or 1000. This was definitely the start bit, which told the control unit that the blast was coming from a controller and that the rest of the transmission was the payload.&lt;/p&gt;

&lt;p&gt;Following the start bit was a series of pulses and spaces that were always either &lt;code&gt;250&lt;/code&gt;Î¼s or &lt;code&gt;500&lt;/code&gt;Î¼s.&lt;/p&gt;

&lt;p&gt;After some additional experimentation and research I presumed Carrera were using &lt;a href="https://en.wikipedia.org/wiki/Manchester_code" rel="noopener noreferrer"&gt;Manchester Coding&lt;/a&gt; to encode their data packets. In Manchester Coding, a bit is represented as a transition from either low-to-high or high-to-low. Specifically, &lt;code&gt;1&lt;/code&gt; is encoded as a pulse of &lt;strong&gt;N&lt;/strong&gt;Î¼s followed by a space of &lt;strong&gt;N&lt;/strong&gt;Î¼s. And &lt;code&gt;0&lt;/code&gt; is encoded as a space of &lt;strong&gt;N&lt;/strong&gt;Î¼s followed by a pulse of &lt;strong&gt;N&lt;/strong&gt;Î¼s.&lt;/p&gt;

&lt;p&gt;As we can see, in our case &lt;code&gt;N = 250&lt;/code&gt;. So each bit required exactly 500Î¼s. Summing the numbers together (minus the start bit) we get &lt;code&gt;4000&lt;/code&gt;. And &lt;code&gt;4000 / 500 = 8&lt;/code&gt;. So the data packet consisted of a start bit followed by eight bits (one byte) of data.&lt;/p&gt;

&lt;p&gt;Consulting the work of Stephan HeÃŸ, which by this point had saved me many hours, I was able to confirm the structure of the data packet sent by each controller. Each controller sent the start bit, followed by &lt;strong&gt;two bits&lt;/strong&gt; representing the player, &lt;strong&gt;four bits&lt;/strong&gt; for the speed and &lt;strong&gt;one bit&lt;/strong&gt; to indicate whether the lane-change button was pressed. Wait - that's only seven bits!?&lt;/p&gt;

&lt;p&gt;As it turns out, there was also a trailing bit that seemed to be used to represent additional players. I assume it was added to the end for backwards compatibility with older models. This also explained why there was no end bit on the data word - it allowed for future expansion of the payload whilst maintaining backwards compatibility.&lt;/p&gt;

&lt;p&gt;I've drawn the payload contents below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flefie0qz6m5e74yn5jbp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flefie0qz6m5e74yn5jbp.jpg" alt="Carrera controller protocol"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As an example, let's suppose we were player #3 travelling at speed 7 with the lane change button pressed. Our 8 bits would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10011110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To encode these bits into pulses and spaces using Manchested Coding we can first add the &lt;em&gt;start bit&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 750
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we start adding in each bit one by one. Remember that bits in Manchested Coding are represented as a transition. So let's add the 1 (a pulse followed by space):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 750
pulse 250
space 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far so good. Let's add the next bit, a 0 (a space followed by a pulse):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 750
pulse 250
space 250
space 250
pulse 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uh-oh! Now we have two spaces in a row. We can fix this by simply merging them into one, longer space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 750
pulse 250
space 500
pulse 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool. Every time we see a &lt;code&gt;0-&amp;gt;1&lt;/code&gt; or a &lt;code&gt;1-&amp;gt;0&lt;/code&gt;, we are going to merge one end with the other to produce a pulse or a space of 500Î¼s. Now let's add in the rest of the bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pulse 300
space 750
pulse 250 # 1
space 500 # 0
pulse 250
space 250 # 0
pulse 500 # 1
space 250
pulse 250 # 1
space 250
pulse 250 # 1
space 250
pulse 250 # 1
space 500 # 0
pulse 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we're done!&lt;/p&gt;

&lt;p&gt;Here is another representation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntxg8bzpt2uhi33uraht.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntxg8bzpt2uhi33uraht.jpg" alt="Carrera controller protocol"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when we visualise it as a step graph, the data packet is very similar to the one I showed above. You should be able to see the difference between the 250Î¼s and 500Î¼s bursts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbhc8xzssb3m2sdcdwid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbhc8xzssb3m2sdcdwid.png" alt="Packet 2 visualised"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with this, the reverse engineering project was complete. From here I was able to use lirc to listen for the syncing blast from the Carrera control unit and respond with some hardcoded data packet.&lt;/p&gt;

&lt;p&gt;After I solved some minor timing issues I was elated when &lt;strong&gt;I saw the car move&lt;/strong&gt;! It was proof enough for me, at the very least, that the idea was not totally bonkers.&lt;/p&gt;

&lt;p&gt;Below is a video of the first time I got the car to move without a physical controller. In this video I am ignoring all syncing efforts from the IR tower on the Carrera set. I am just blasting the same message over and over and hoping the timing is correct. This explains why the car only moves in little bursts.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WsgaB_ByTto"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Laser Drift
&lt;/h2&gt;

&lt;p&gt;Wow, this post got long...&lt;/p&gt;

&lt;p&gt;I am going to leave the details of the end-result to another blog post. For now, you can check out &lt;a href="https://github.com/buntine/laser-drift" rel="noopener noreferrer"&gt;Laser Drift on Github&lt;/a&gt; or watch the promo video I embedded at the top of this article.&lt;/p&gt;

</description>
      <category>reverseengineering</category>
      <category>hacking</category>
      <category>python</category>
      <category>fun</category>
    </item>
    <item>
      <title>Hulkamania! Or; How I made our office play personalized entrance theme music</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Sun, 02 Apr 2017 12:47:38 +0000</pubDate>
      <link>https://dev.to/buntine/hulkamania-or-how-i-made-our-office-play-personalized-entrance-theme-music</link>
      <guid>https://dev.to/buntine/hulkamania-or-how-i-made-our-office-play-personalized-entrance-theme-music</guid>
      <description>&lt;p&gt;&lt;em&gt;This blog post details how I hacked the office WiFi to play entrance theme music for my colleagues when they entered the building each morning. If you only want to see the finished product and are not interested in the details, here is a little video demonstration (including some seriously underreceived overacting):&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/o2S1Mmf6gnc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I was a pretty normal 90's kid. My brother and I spent more than our fair share of time watching and emulating our favourite WWF (ahem, WWE) superstars.&lt;/p&gt;

&lt;p&gt;We didn't get wrestling on television in Australia and so we were limited to watching the VHS releases on &lt;a href="https://en.wikipedia.org/wiki/WWE_Home_Video#Coliseum_Video" rel="noopener noreferrer"&gt;Coliseum Video&lt;/a&gt;. And watch them we did! Every Friday night we would take advantage of the "5-for-5" deal at our local video shop, which allowed us to hire five weekly videos for just $5. Typically, at least three of these would have been classics like &lt;a href="https://en.wikipedia.org/wiki/WrestleMania_X" rel="noopener noreferrer"&gt;Wrestlemania X&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/SummerSlam_(1995)" rel="noopener noreferrer"&gt;SummerSlam '95&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Royal_Rumble_(1994)" rel="noopener noreferrer"&gt;Royal Rumble '94&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One of the most satisfying parts of the wrestling events was the theme music that each wrestler would get when they were first entering the ring (or squared circle, as I should say). For those uninitiated into the world of very-much-not-fake-I'm-sure-of-it wrestling, the average entrance looked something like this:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/V3cBiEhSRd0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I'm sure you noticed the uncanny similarities between myself and "Stone Cold" Steve Austin.&lt;/p&gt;

&lt;p&gt;Growing up, I thought it was only fair that I got my own theme music. And I figured that if I got theme music, then my colleagues also deserved their own (even if they disagreed).&lt;/p&gt;

&lt;p&gt;Luckily, we already had a bunch of Internet connected &lt;a href="http://sonos.com" rel="noopener noreferrer"&gt;Sonos speakers&lt;/a&gt; in the office that I could communicate with over the LAN. So I knew the actual playing of music would be easy enough. And so the main problems were now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Detect when someone entered the office&lt;/li&gt;
&lt;li&gt;Determine &lt;em&gt;who&lt;/em&gt; it was&lt;/li&gt;
&lt;li&gt;Determine if this is their first visit today&lt;/li&gt;
&lt;li&gt;Perform 1, 2 and 3 without any input from the user (aka magically)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My first thought was to use a proximity sensor on the main door &lt;sup&gt;1&lt;/sup&gt;    . This was short-lived as I determined     that users would need to buy-in to the idea by installing an app on their phone and agreeing to some terms and conditions. Not to mention the fact that I'm way too tight to actually buy those sensors.&lt;/p&gt;

&lt;p&gt;After what was probably an embarrassing amount of time pondering the problems, I eventually realized that the WiFi was a perfectly suitable way of determining when people entered and left the office area. And because our office WiFi signal is strong enough to reach the footpath out the front, it would have the added benefit of playing a users theme song slightly &lt;em&gt;before&lt;/em&gt; they entered the building (you know, for added realism).&lt;/p&gt;

&lt;p&gt;I started digging around in the office router control panel hoping I'd find some way of easily detecting WiFi events like &lt;em&gt;connect&lt;/em&gt; and &lt;em&gt;disconnect&lt;/em&gt;. After some time I came to the conclusion that it wasn't going to be that easy! Whilst I didn't find a way of detecting events &lt;sup&gt;2&lt;/sup&gt;, I did see that this particular router had pretty comprehensive logs. I thought that perhaps a quick and dirty approach would be to continuously poll the logs looking for useful events. It wasn't great, but it was &lt;em&gt;something&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After some further investigation I found that I was able to forward the router logs over TCP to a remote &lt;a href="https://en.wikipedia.org/wiki/Syslog" rel="noopener noreferrer"&gt;syslog server&lt;/a&gt;. This was important because it meant that the router could push the events to me instead of me having to continuously tail a log file. I started to see how this thing was going to come together. So I found an old computer in a closet, installed a flavour of Linux on it, connected it to the LAN and had syslog listen on TCP port 514 (the syslog standard port).&lt;/p&gt;

&lt;p&gt;It worked! I tailed &lt;code&gt;/var/log/syslog&lt;/code&gt; and started seeing logs like this almost instantly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATA 127.0.0.1: &amp;lt;14&amp;gt;Dec 12 15:15:20 10.our.host.com.au (\"YO,v3.7.21.5389 libubnt[1441]: w event.cust(): EVENT_STA_JOIN ath0: 00:34:da:58:8d:a6 / 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time a device connect and disconnected from the network, a log including the devices unique mac address was appended &lt;sup&gt;3&lt;/sup&gt;. Awesome! But there was a lot of noise. There were logs for connections, disconnections, roamings, handshakes, failed connections, rate limiting, etc. I eventually decided that there were only two events that I was actually interested in: &lt;code&gt;EVENT_STA_JOIN&lt;/code&gt; and &lt;code&gt;EVENT_STA_LEAVE&lt;/code&gt;. So I added some syslog configuration that would&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find logs that matched my desired events&lt;/li&gt;
&lt;li&gt;Forward them onto local TCP port &lt;code&gt;10514&lt;/code&gt; (Where I'd have a simple TCP server running to handle the next step)&lt;/li&gt;
&lt;li&gt;Ignore everything else&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The syslog configuration ended up looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if $msg contains "EVENT_STA_JOIN" or $msg contains "EVENT_STA_LEAVE" then
   action(type="omfwd" Target="localhost" Port="10514" Protocol="tcp")
&amp;amp; ~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now the problem was much simpler. Every time someone connected or disconnected from the WiFi, my little computer in the corner of the room was sent the devices mac address and an indication of the event type (&lt;code&gt;JOIN&lt;/code&gt; or &lt;code&gt;LEAVE&lt;/code&gt;). All I needed to do now was to read those incoming messages, map the mac address to a Human and then play the appropriate song.&lt;/p&gt;

&lt;p&gt;I wrote a TCP server in the &lt;a href="http://rustlang.org" rel="noopener noreferrer"&gt;Rust programming language&lt;/a&gt;, which you can &lt;a href="https://github.com/buntine/wrestlers-adapter" rel="noopener noreferrer"&gt;check out on Github&lt;/a&gt; &lt;sup&gt;4&lt;/sup&gt;. It simply reads the byte stream into a string, parses out the useful information and then takes the appropriate action on the Sonos.&lt;/p&gt;

&lt;p&gt;In terms of knowing &lt;em&gt;who&lt;/em&gt; was triggering the WiFi event, I kept it simple and just created a JSON file that mapped mac addresses to names, songs, greetings, etc. It looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3d:5b:22:b7:dc:a9&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Andrew&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;// In case I want it later (and for logging)&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;state&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UNSEEN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// IN | OUT | UNSEEN&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;song&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;owenhart.mp3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;join&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to the office, Bunts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;leave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;See you later, Bunts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastEvent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1490436790&lt;/span&gt;  &lt;span class="c1"&gt;// Unix Epoch timestamp&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4d:aa:58:b6:9a:61&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;state&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;song&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stonecold.mp3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;join&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome back, Mel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;leave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I hope to see you soon, Mel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastEvent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1490436790&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each night at midnight I have a little cron job that resets each users &lt;code&gt;state&lt;/code&gt; to &lt;code&gt;UNSEEN&lt;/code&gt;. I use this to ensure that a particular theme song is only played the first time the given user connects to the WiFi. Any subsequent events trigger a greeting or exit message to be vocalized over the Sonos. This was achieved using the great &lt;a href="https://en.wikipedia.org/wiki/Google_Text-to-Speech" rel="noopener noreferrer"&gt;Google Text-to-Speech&lt;/a&gt; service.&lt;/p&gt;

&lt;p&gt;The only thing left to do was to add in some minor smarts to prevent the office from being spammed with "hellos" and "goodbyes". I noticed that certain devices would often lose, and then quickly reestablish, their connection, which would cause their greetings to be fired in succession even though the user didn't actually leave and re-enter the office. I tackled this by ensuring that certain events had to match criteria in order to be considered valid. For example, in order for a &lt;code&gt;JOIN&lt;/code&gt; event to be considered valid, the user in question must:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Be in the &lt;code&gt;OUT&lt;/code&gt; or &lt;code&gt;UNSEEN&lt;/code&gt; states&lt;/li&gt;
&lt;li&gt;Not have fired any events in the past 15 seconds&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I also allowed for the &lt;code&gt;join&lt;/code&gt; and &lt;code&gt;leave&lt;/code&gt; keys to have an optional array of strings, in which a random greeting would be chosen and vocalised. This gave the whole thing a bit of variation and prevented people from tiring of the same greetings every day.&lt;/p&gt;

&lt;p&gt;The final product was now ready to rock and roll! After adding in some logging, I ended up with something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fi.imgur.com%2FiAUC0l6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fi.imgur.com%2FiAUC0l6.png" title="Tailing logs" alt="Tailing logs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As for next steps, creating a simple GUI to wrap the JSON file (or perhaps I should do the right thing and move it to a database) would be pretty simple. This would provide users with a simple way to actually write their own greetings and upload their own favourite theme music. And, at least theoretically, this system could be used for more practical tasks such as a slack bot that could tell an employee if a certain colleague was in the office.&lt;/p&gt;

&lt;p&gt;I believe this type of passion project is a great way to get out my creative energy whilst also giving the office a refreshing spin. Thanks for reading!    &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Footnotes&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In case you were wondering - no, unfortunately we don't have any type of swipes or user identification processes in place when people enter the office. &lt;/li&gt;
&lt;li&gt;I later found out that the router actually shipped with a minimal HTTP API that may have been sufficient - but finding documentation and examples proved difficult.&lt;/li&gt;
&lt;li&gt;Yes, I know that some mobile operating systems spoof mac addresses. But this is not a problem in the short term as once a device connects to a WiFi network, it's reported mac address tends to stay the same. Spoofed mac addresses are more of a concern for WiFi networks that are trying to track &lt;em&gt;unconnected&lt;/em&gt; devices across nodes.&lt;/li&gt;
&lt;li&gt;I've since moved some of the higher-level details to a Node-based web service for reasons totally uninteresting to the reader. I've decided not to mention that explicitly in the article as it derails the technical details unnecessarily.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>comedy</category>
      <category>rust</category>
      <category>creativity</category>
      <category>fun</category>
    </item>
    <item>
      <title>DOM elements with ID's are global variables</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Thu, 12 Jan 2017 13:07:23 +0000</pubDate>
      <link>https://dev.to/buntine/dom-elements-with-ids-are-global-variables</link>
      <guid>https://dev.to/buntine/dom-elements-with-ids-are-global-variables</guid>
      <description>&lt;p&gt;So, here is something weird.&lt;/p&gt;

&lt;p&gt;Yesterday I was refactoring some code and came across something akin to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;brand&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At no point in the codebase was a variable named &lt;code&gt;brand&lt;/code&gt; defined. And so any sane observer would expect that the preceding snippet would result in a runtime error -- assuming there was not something like a linter available that would have picked up on it (and the fact that &lt;code&gt;logo&lt;/code&gt; was being defined but never used) first.&lt;/p&gt;

&lt;p&gt;I started having one of those moments that we all dread: I was looking at some code that could not possibly have ever worked. Yet, there it was. Working. I sank into my chair, not sure to make of what I was seeing.&lt;/p&gt;

&lt;p&gt;In fact, the only other place I could see the word "brand" in the entire application was in the HTML document upon which this Javascript was being executed (yes, some websites still send HTML over the wire).&lt;/p&gt;

&lt;p&gt;So I changed it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"brand_foo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; ... &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, lo and behold, the code instantly started causing a runtime error. Wait. No way... &lt;strong&gt;The browser is surely not pre-defining a global variable for each DOM element with an ID attribute. Right?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Wrong! It does. I've looked at this code in most major browsers and mobile devices and not one of them complained.&lt;/p&gt;

&lt;p&gt;Don't believe me? You can try it yourself right now. Press F12 (or otherwise Right Click -&amp;gt; Inspect), find an element with an ID, and then evaluate that ID at the console REPL.&lt;/p&gt;

&lt;p&gt;What about IDs such as "brand-name" that don't conform to Javascript's grammar? Surely they are ignored? Nope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;brand-name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;// --&amp;gt; &amp;lt;div id="brand-name"&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have no idea how well known this is. I certainly did not know about it but, admittedly, I haven't spent a lot of time in the browser in many years. I've reached out to all of the frontend developers I know and none of them knew about this either.&lt;/p&gt;

&lt;p&gt;As it turns out, the HTML specification actually &lt;a href="https://html.spec.whatwg.org/#named-access-on-the-window-object"&gt;documents this functionality&lt;/a&gt;. Although it seems major browsers are not implementing it uniformly at all. If you define more than one element with the same ID (you evil person), then Chrome will actually return an array of &lt;code&gt;Element&lt;/code&gt; objects whilst Firefox returns the first element it saw. And neither browsers define global variables based on the &lt;code&gt;name&lt;/code&gt; attribute on elements like &lt;code&gt;img&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should I make use of this?
&lt;/h2&gt;

&lt;p&gt;No. Please, no. Imagine how confusing this could get if you were dealing with many DOM elements in a non-trivial codebase and none of them were being defined anywhere visible to the programmer. Or imagine if you go all-in and then one day a major browser decides this is insanity and disables it!&lt;/p&gt;

&lt;p&gt;The only place I can see this being of use would be in the REPL whilst debugging. But, still, it's kind of cool in a never-do-this kind of way.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Header image originally from &lt;a href="http://www.classichorrorcampaign.com"&gt;http://www.classichorrorcampaign.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dom</category>
      <category>browsers</category>
    </item>
    <item>
      <title>Pâ€³ - The original Brainf*ck and mother of the Turing tarpits</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Mon, 05 Dec 2016 06:37:33 +0000</pubDate>
      <link>https://dev.to/buntine/p---the-original-brainfck-and-mother-of-the-turing-tarpits</link>
      <guid>https://dev.to/buntine/p---the-original-brainfck-and-mother-of-the-turing-tarpits</guid>
      <description>&lt;p&gt;When we think of esoteric programming languages, most of us quickly gravitate towards one in particular: the infamous &lt;a href="https://en.wikipedia.org/wiki/Brainfuck"&gt;Brainf*ck&lt;/a&gt; - a super minimal programming language that achieves &lt;a href="https://en.wikipedia.org/wiki/Turing_completeness"&gt;Turing Completeness&lt;/a&gt; in just six simple instructions (not including the two for I/O).&lt;/p&gt;

&lt;p&gt;The incessantly lovely Brainf*ck was created in 1993 by Urban MÃ¼ller in an attempt to construct a usable - albeit barely - programming language with a compiler under 1024 bytes. Why 1024 bytes? Well, he had been inspired by Wouter van Oortmerssen's &lt;a href="https://esolangs.org/wiki/FALSE"&gt;FALSE&lt;/a&gt;, a stack-based language with a compiler of exactly 1024 bytes. So I guess you could say it was a competition of sorts.&lt;/p&gt;

&lt;p&gt;In this article, purely for the joy of it, we will see that &lt;strong&gt;Brainf*ck is actually an informal dialect of a programming language invented in Italy more than 50 years ago: Corrado BÃ¶hm's Pâ€³ (pronounced "P Prime Prime" or "P Double-Prime)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I do expect that the reader has basic familiarity with Brainf*ck - although, if you've never heard of it, I definitely recommend you take a look. Oh, and don't worry; despite the scary name, it's really very easy to learn (note, I didn't say "easy to use"!).&lt;/p&gt;

&lt;p&gt;Let's talk a bit about Corrado BÃ¶hm's place in the history of Computer Science before we get into the nuts and bolts of his decidedly strange creation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Corrado BÃ¶hm and the Structured Program Theorem
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;You can skip this section if you're only interested in the details and not the motivation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we talk about "programming" today, we are almost universally referring to what is known as "&lt;a href="https://en.wikipedia.org/wiki/Structured_programming"&gt;structured programming&lt;/a&gt;". That is, writing programs that employ the use of block structures, functions and loops. Pretty simple, huh? In fact, this all seems so obvious nowadays that we don't feel the need to qualify so specifically what we mean - so we use more general terms like "imperative" and "declarative" to describe our favorite paradigms of programming. The whole "structured" part is kind of a given.&lt;/p&gt;

&lt;p&gt;But it wasn't always this way...&lt;/p&gt;

&lt;p&gt;Many of the earliest programming languages, including heavy-hitters like BASIC, FORTRAN and COBOL&lt;sup&gt;1&lt;/sup&gt;, were &lt;a href="https://en.wikipedia.org/wiki/Non-structured_programming"&gt;unstructured&lt;/a&gt;: Statements were sequentially ordered, generally one per-line. And those lines were given labels or numbered in such a way that a program could perform an &lt;em&gt;unconditional jump&lt;/em&gt; from one part of the program to the other.&lt;/p&gt;

&lt;p&gt;And when I say "jump", I really mean jump! Execution was not returned back to the calling context as is the case with a function call (unless, of course, it was physically asked to with another jump). One could, at least theoretically, jump straight into the middle of a &lt;code&gt;SUBROUTINE&lt;/code&gt; or an &lt;code&gt;IF&lt;/code&gt;&lt;sup&gt;2&lt;/sup&gt;. Following the execution of a program meant tracing all of the jumps. Such a messy form of execution eventually gave rise to a phrase we still hear a lot of today (but never about our own code, of course): "Spaghetti code"&lt;sup&gt;3&lt;/sup&gt;!&lt;/p&gt;

&lt;p&gt;In case you haven't worked it out already; yes, I am talking about the notorious &lt;code&gt;GO TO&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The early '60s were dominated by the debate over structured vs. non-structured programming. This was further ignited when, in 1966, a landmark paper titled "Flow diagrams, Turing Machines and Languages with only Two Formation Rules" was published by Corrado BÃ¶hm and Giuseppe Jacopini. This paper&lt;sup&gt;4&lt;/sup&gt; provided the &lt;a href="https://en.wikipedia.org/wiki/Structured_program_theorem"&gt;theoretical foundation for structured programming&lt;/a&gt; (and therefore the abolishment of &lt;code&gt;GO TO&lt;/code&gt;) by proving that one can compute any computable function with only three simple control structures:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Execute one subprogram, and then another subprogram (sequence)&lt;/li&gt;
&lt;li&gt;Execute one of two subprograms according to the value of a boolean expression (selection/branching)&lt;/li&gt;
&lt;li&gt;Execute a subprogram until a boolean expression is true (iteration)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To demonstrate this in relation to Turing machines, they, of course, used a programming language: &lt;strong&gt;Pâ€³&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pâ€³, the mother of the Turing tarpits
&lt;/h2&gt;

&lt;p&gt;Pâ€³ is a very simple. In fact, it's so simple that the whole thing can get a bit confusing. Kinda' like those movies that are so bad they loop back around to being good again. The language consists of only four characters: &lt;code&gt;R&lt;/code&gt;, &lt;code&gt;Î»&lt;/code&gt;, &lt;code&gt;(&lt;/code&gt; and &lt;code&gt;)&lt;/code&gt; that act upon a &lt;a href="https://en.wikipedia.org/wiki/Turing_machine"&gt;Turing machine&lt;/a&gt; with infinite tape (main memory) and a finite alphabet (the set of things that can be written in a memory cell - such as the digits &lt;code&gt;0..9&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;BÃ¶hm defined the syntax rules&lt;sup&gt;5&lt;/sup&gt; as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Î», R âˆˆ Pâ€³&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;qâ‚, qâ‚‚ âˆˆ Pâ€³&lt;/code&gt; implies &lt;code&gt;qâ‚qâ‚‚ âˆˆ Pâ€³&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;q âˆˆ Pâ€³&lt;/code&gt; implies &lt;code&gt;(q) âˆˆ Pâ€³&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Only the expressions that can be derived from rules 1, 2 and 3 belong to Pâ€³.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Right. Let's try that without the set notation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The characters &lt;code&gt;Î»&lt;/code&gt; and &lt;code&gt;R&lt;/code&gt; are syntactically valid programs in Pâ€³.&lt;/li&gt;
&lt;li&gt;Programs can be composed. So, if &lt;code&gt;qâ‚&lt;/code&gt; and &lt;code&gt;qâ‚‚&lt;/code&gt; are programs in Pâ€³, then so is their composition: &lt;code&gt;qâ‚qâ‚‚&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any valid program can be iterated over. So, if &lt;code&gt;q&lt;/code&gt; is a program in Pâ€³, then so is &lt;code&gt;(q)&lt;/code&gt;. This will make sense when we get to the semantics.&lt;/li&gt;
&lt;li&gt;That's it!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;BÃ¶hm then goes on to explain the language semantics, which I've summarized and simplified here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The machine has a finite alphabet of length &lt;code&gt;N&lt;/code&gt;, where &lt;code&gt;N &amp;gt; 1&lt;/code&gt;. &lt;code&gt;0&lt;/code&gt; is considered a special, blank symbol. So each memory cell can contain any value from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;N&lt;/code&gt;. Because we are dabbling in the theoretical world of Turing machines, we can say that the exact value of &lt;code&gt;N&lt;/code&gt; is precisely what it needs to be for the computation at hand.&lt;/li&gt;
&lt;li&gt;Execution starts at the leftmost symbol and proceeds to the right until there is nothing left to compute.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;R&lt;/code&gt; is the operation of shifting the tapehead (a.k.a &lt;em&gt;data pointer&lt;/em&gt;) forward to the right one cell (if there is one).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Î»&lt;/code&gt; is the operation of replacing the symbol, &lt;code&gt;c&lt;/code&gt;, at the tapehead with &lt;code&gt;(c+1) mod (N+1)&lt;/code&gt; and then shifting the tapehead to the left one cell. Note the modulus operation - so if &lt;code&gt;N = 5&lt;/code&gt; then trying to increment &lt;code&gt;5&lt;/code&gt; will result in &lt;code&gt;0&lt;/code&gt; (the blank symbol) because &lt;code&gt;6 mod 6 = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(q)&lt;/code&gt;, where &lt;code&gt;q&lt;/code&gt; is any valid program, is interpreted as a &lt;em&gt;while loop&lt;/em&gt; that iterates while the current cell does not contain the blank symbol (&lt;code&gt;0&lt;/code&gt;). Endless loops are possible, of course.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, we can think of Pâ€³ as being a near-literal implementation of a Turing machine in a similar sense that we can see the original Lisp as being deeply related to Church' Lambda Calculus. The former is just far less useful to the practicing programmer.&lt;/p&gt;

&lt;p&gt;In a paper published in 1964 for the International Computation Center in Rome&lt;sup&gt;6&lt;/sup&gt;, BÃ¶hm proved that Pâ€³ was Turing-complete, which makes it the first &lt;em&gt;structured&lt;/em&gt; programming language that did not contain a &lt;code&gt;GO TO&lt;/code&gt; instruction but instead relied upon iteration. Djikstra would go on to cite BÃ¶hm and Jacopini and in his now-famous paper, &lt;a href="http://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf"&gt;Go To Statement Considered Harmful&lt;/a&gt;, which would help to solidify their place in computer science folklore.&lt;/p&gt;

&lt;p&gt;Here is a simple program to add two numbers together for a Turing machine where &lt;code&gt;N = 3&lt;/code&gt; and main memory looks something like this &lt;code&gt;[2, 1]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(
 Î»RÎ»RÎ»R    # Decrement current cell (remember that N+1 wraps back around to 0, so decrementing is equivalent to N increments).
 R         # Move tapehead right.
 Î»R        # Increment current cell.
 Î»RÎ»RÎ»RÎ»   # Move tapehead left by decrementing the current cell and then executing one final Î».
)          # Iterate if the current cell is not 0. This means we iterate twice in this example.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At the end of execution main memory will look like this: &lt;code&gt;[0, 3]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ok, cool, I guess. Maybe you can see some minor similarities to Brainf*ck, but we are definitely missing some things. And even in esoteric programming language terms, it's all very clunky!&lt;/p&gt;

&lt;p&gt;But, luckily for us, BÃ¶hm provided several small abstractions that allow us to write much simpler programs. And in doing so we will see that Brainf*ck and Pâ€³ are equivalent.&lt;/p&gt;

&lt;p&gt;Let's take a look.&lt;/p&gt;

&lt;h2&gt;
  
  
  When two become one
&lt;/h2&gt;

&lt;p&gt;There are a few instructions that Brainf*ck gives us that we don't get out-of-the-box with Pâ€³. Specifically, we still need to fill in the following gaps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increment the current cell (without moving the tapehead!).&lt;/li&gt;
&lt;li&gt;Decrement the current cell.&lt;/li&gt;
&lt;li&gt;Move the tapehead leftward.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's define those as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increment&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;I = Î»R&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Increment the current cell and move left, then move right.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decrement&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;D = I^N&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;This is defined as &lt;code&gt;N&lt;/code&gt; &lt;code&gt;I&lt;/code&gt;'s in a row. So for a Turing machine where &lt;code&gt;N = 5&lt;/code&gt; it's literally &lt;code&gt;IIIII&lt;/code&gt;. The best way to think of &lt;code&gt;D&lt;/code&gt; is as a macro that expands to &lt;code&gt;N&lt;/code&gt; &lt;code&gt;I&lt;/code&gt;'s rather than a runtime construct like a function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Move left&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;L = DÎ»&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Decrement the current cell and then increment it (to restore the original value) and move left.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To recap, we have now now defined the following instructions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Move right&lt;/strong&gt;: &lt;code&gt;R&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Move left&lt;/strong&gt;: &lt;code&gt;L&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Increment&lt;/strong&gt;: &lt;code&gt;I&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decrement&lt;/strong&gt;: &lt;code&gt;D&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open loop&lt;/strong&gt;: &lt;code&gt;(&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close loop&lt;/strong&gt;: &lt;code&gt;)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now it's starting to look familiar! And so, finally, let's write the previous program again with our new abstractions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(D R I L)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And how does the same program look in Brainf*ck?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[- &amp;gt; + &amp;lt;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Interesting! Although we've decided upon slightly different syntax, these two programs are now semantically identical.&lt;/p&gt;

&lt;p&gt;I might just take a moment here to mention that there is something very fulfilling about defining a programming language in which &lt;code&gt;DRILL&lt;/code&gt; is a valid program (although, I must admit, BÃ¶hm used different names). I think I'll call my dialect &lt;strong&gt;"Braindriller"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's try another program&lt;sup&gt;7&lt;/sup&gt; that moves a value from cell &lt;code&gt;0&lt;/code&gt; two places to the right (cell &lt;code&gt;2&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R R ( D ) L L ( D R R I L L )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And Brainf*ck:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; &amp;gt; [ - ] &amp;lt; &amp;lt; [ - &amp;gt; &amp;gt; + &amp;lt; &amp;lt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, I don't know about you, but I'm sold. I implore you to keep trying other Brainf*ck programs but, considering the two models of computation are now the same, it's my hypothesis that they will all be instruction-for-instruction identical.&lt;/p&gt;

&lt;p&gt;One final note - you'll notice that we've skipped on the I/O instructions &lt;code&gt;,&lt;/code&gt; and &lt;code&gt;.&lt;/code&gt; that exist in most dialects of Brainf*ck. This is because the theoretical nature of Pâ€³ means that practical things like input and output would serve no purpose - we are only interested in the effect of the instructions upon the machine state (memory cells). But if you were to build a Pâ€³ interpreter - and I encourage you to do so - then you could implement I/O exactly as Brainf*ck does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;If you've made it this far - well done! And I'm sure the mental energy you've just exerted will all pay off in the long run. Think about it: Next time you're talking to someone who brings up Brainf*ck, you can tell them all about the wonders of Pâ€³ and the genius of Corrado BÃ¶hm.&lt;/p&gt;

&lt;p&gt;As always, corrections and feedback are always more than welcome. :)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;BASIC, FORTRAN and COBOL all added support for structured programming in later versions.&lt;/li&gt;
&lt;li&gt;I guess this would make coroutines somewhat simple to implement.&lt;/li&gt;
&lt;li&gt;It's not known exactly who coined this term, but according to Wikipedia, an early usage comes from the Guy L. Steele's 1977 paper: &lt;a href="http://dl.acm.org/citation.cfm?id=806933"&gt;Macaroni is better than spaghetti&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;This paper is known to be cited more than it's read. OK, I admit, I &lt;em&gt;tried&lt;/em&gt; to get through it all. I really did. But before you judge me - &lt;a href="http://www.cs.unibo.it/~martini/PP/bohm-jac.pdf"&gt;give it a shot yourself&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Had Backus-Naur Form been popular in '64, it would have looked something like this: &lt;code&gt;&amp;lt;program&amp;gt; ::= R|Î»|&amp;lt;program&amp;gt;&amp;lt;program&amp;gt;|(&amp;lt;program&amp;gt;)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I have searched high-and-low for a copy of this paper. The ICC was decommissioned in the late-60s and most of their publications are very difficult to find now. I've tracked a physical copy down to the British Library, but am yet to get my hands on it.&lt;/li&gt;
&lt;li&gt;Courtesy of &lt;a href="https://esolangs.org/wiki/brainfuck"&gt;esolangs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Cover image, originally from &lt;a href="http://www.corradobohm.it/"&gt;corradobohm.it&lt;/a&gt;, shows the attendees of the first Lambda Calculus Conference (1979). Corrado has the white hair in the second row. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>computerscience</category>
      <category>brainfck</category>
      <category>turingmachines</category>
    </item>
    <item>
      <title>The Balanced Ternary Machines of Soviet Russia</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Mon, 21 Nov 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/the-balanced-ternary-machines-of-soviet-russia</link>
      <guid>https://dev.to/buntine/the-balanced-ternary-machines-of-soviet-russia</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"Perhaps the prettiest number system of all is the balanced ternary notation" - Donald E. Knuth, The Art of Computer Programming, Vol. 2.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's pretty common knowledge that computers store and operate on data using the binary number system. One of the main reasons for this can be found in the circuitry of modern computers, which are made up of billions of easily mass-producible transistors and capacitors that can together represent two states: &lt;strong&gt;high voltage&lt;/strong&gt; (&lt;code&gt;1&lt;/code&gt;) and &lt;strong&gt;low voltage&lt;/strong&gt; (&lt;code&gt;0&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Such a design is so ubiquitous nowadays that it's hard to imagine that computers could operate in any other way. But, in Soviet Russia during the 1950s, they did. Enter &lt;strong&gt;Setun&lt;/strong&gt;, a balanced ternary computer developed in 1958 by a small team led by Nikolay Brusentsov at the Moscow State University.&lt;/p&gt;

&lt;p&gt;OK, so before we continue talking any more about Brusentsov and Setun, let me explain a little bit about balanced ternary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Balanced Ternary
&lt;/h2&gt;

&lt;p&gt;Ternary, or base-3, is a number system in which there are &lt;em&gt;three&lt;/em&gt; possible values: &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;. In balanced ternary, these three possibilities are &lt;code&gt;-1&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;+1&lt;/code&gt;; often simplified to &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt;, respectively. &lt;/p&gt;

&lt;p&gt;So, in its balanced form, we can think of the ternary values as being "balanced" around the mid-point of &lt;code&gt;0&lt;/code&gt;. The same rules apply to ternary as to any other numeral system: The right-most symbol, &lt;code&gt;R&lt;/code&gt;, has it's own value and each successive symbol has it's value multiplied by the base, &lt;code&gt;B&lt;/code&gt;, raised to the power of it's distance, &lt;code&gt;D&lt;/code&gt; from &lt;code&gt;R&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Uhh, right, maybe I'll just give you an example. Let's encode &lt;code&gt;114&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+++-0 = (1 * 3^4) + (1 * 3^3) + (1 * 3^2) + (-1 * 3^1) + 0
      = 81 + 27 + 9 + -3
      = 114
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And in binary (base-2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1110010 = (1 * 2^6) + (1 * 2^5) + (1 * 2^4) + 0 + 0 + (1 * 2^1) + 0
        = 64 + 32 + 16 + 2
        = 114
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And, just to be sure, here are the same rules applied in decimal (base-10):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;114 = (1 * 10^2) + (1 * 10^1) + (4 * 10^0)
    = 100 + 10 + 4
    = 114
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Cool?&lt;/p&gt;

&lt;p&gt;Now, what if we wanted to represent &lt;code&gt;-114&lt;/code&gt;? Well, in binary and decimal, we need to introduce a new symbol: the sign. In the main memory of a binary computer, this would either translate to storing a leading bit to specify the sign or significantly reduce the amount of numbers we can represent &lt;sup&gt;1&lt;/sup&gt;. This is the reason we speak about &lt;code&gt;signed&lt;/code&gt; and &lt;code&gt;unsigned&lt;/code&gt; integers in programming languages.&lt;/p&gt;

&lt;p&gt;But in balanced ternary, as we're about to discover, to represent the inverse of a number we simply have to swap all &lt;code&gt;+&lt;/code&gt;'s with &lt;code&gt;-&lt;/code&gt;'s and vice versa. We don't need any extra information to represent the sign!&lt;/p&gt;

&lt;p&gt;Check it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---+0 = (-1 * 3^4) + (-1 * 3^3) + (-1 * 3^2) + (1 * 3^1) + 0
      = -81 + -27 + -9 + 3
      = -114
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In a moment we will see that this property (and a couple others) of balanced ternary give us some very interesting computational efficiencies. But,                 for now, let's go back to talking about the Setun computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Birth of Setun
&lt;/h2&gt;

&lt;p&gt;The late 50's were an exciting time in computers: Nathaniel Rochester and his team at IBM had recently developed the first mass-produced, stored-program (a.k.a "modern") computer - the &lt;a href="https://en.wikipedia.org/wiki/IBM_701"&gt;IBM 701&lt;/a&gt;. John Backus and his team had invented &lt;a href="https://en.wikipedia.org/wiki/Fortran"&gt;FORTRAN&lt;/a&gt;, the first high-level programming language that was seeing widespread use. And, perhaps most important of all, the first fully-transistorized computers, such as     the &lt;a href="https://en.wikipedia.org/wiki/TX-0"&gt;TX-0&lt;/a&gt; and the &lt;a href="https://en.wikipedia.org/wiki/Philco"&gt;Philco Transac S-2000&lt;/a&gt;, were starting to be developed. The path was set for the development of the binary computing machines that have dominated the market ever since.&lt;/p&gt;

&lt;p&gt;But that was North America.&lt;/p&gt;

&lt;p&gt;At the same time in Russia, a group of mathematicians and engineers under the guidance of &lt;a href="http://www.computer-museum.ru/english/galglory_en/Brusentsov.htm"&gt;Nikolay Brusentsov&lt;/a&gt; and his colleague &lt;a href="http://www.math.nsc.ru/conference/sobolev/english/About_Sobolev_SL.htm"&gt;Sergei Sobolev&lt;/a&gt; were devising different ways in which computers could operate &lt;sup&gt;2&lt;/sup&gt;. Brusentsov and co. surveyed the array of computers and technological advancements emerging from the West and     comprehended their use of transistors to represent binary data. But, let's remember, this was Russia - transistors were not readily available behind the Iron Curtain. And &lt;a href="https://en.wikipedia.org/wiki/Vacuum_tube"&gt;vacuum tubes&lt;/a&gt; sucked as much in Russia as they did in the West!&lt;/p&gt;

&lt;p&gt;So Brusentsov devised an element from miniature &lt;a href="https://en.wikipedia.org/wiki/Ferrite_core"&gt;ferrite cores&lt;/a&gt; and semiconductor diodes that was able to act as a controlled current transformer, which proved to be an effective base for implementing ternary logic &lt;sup&gt;3&lt;/sup&gt;. It was found that these elements, compared with their binary counterparts, provided more speed and reliability and required less power to operate.&lt;/p&gt;

&lt;p&gt;The team of ten literally built Setun from scratch, working in a small room filled with laboratory tables (which they also built!). Every morning, the team members began their day by assembling five basic machine elements. They started with a ferrite core and, using an ordinary sewing needle, wound 52 coils of wire onto it. The cores were then handed off to technicians who completed the assembly process and mounted them onto blocks.&lt;/p&gt;

&lt;p&gt;Ternary logic was implemented by combining two of these ferrite elements and wiring them in such a way that they could represent three stable states. This approach was successful but did nothing to reduce the number of elements required as, in reality, those two ferrite cores could potentially represent two binary bits, which represents more information (&lt;code&gt;2^2&lt;/code&gt;) than one ternary "trit" (&lt;code&gt;3^1&lt;/code&gt;). Alas, at least their power consumption was reduced!&lt;/p&gt;

&lt;p&gt;Setun operated on numbers of up to &lt;code&gt;18&lt;/code&gt; trits, meaning one could represent anything between &lt;code&gt;-387,420,489&lt;/code&gt; and &lt;code&gt;387,420,489&lt;/code&gt;. A binary computer, on the other hand, would need at least &lt;code&gt;29&lt;/code&gt; bits to reach this capacity.&lt;/p&gt;

&lt;p&gt;Overall, development of Setun lasted two years - although it was operational after just ten days of testing, which was unheard of at the time. In total, only about 50 machines were produced. And even though Setun machines proved capable of operating successfully for years-on-end in Russia's extreme climate, the entire project was racked with controversy.&lt;/p&gt;

&lt;p&gt;This was primarily due to the fact the manufacturing plant was unable to justify the mass-production of what they deemed as a cheap intellectual endeavor and the "fruit of university fantasy". I think it's safe to assume that Russia was simply not ready to comprehend the eventual importance of computing machines. Ultimately, the Setun machines were replaced with binary counterparts that were able to calculate with similar efficiency but at more than twice the cost of operation!&lt;/p&gt;

&lt;h2&gt;
  
  
  But why Ternary?
&lt;/h2&gt;

&lt;p&gt;As I demonstrated briefly above, in Ternary, there is no need to store a leading bit, ahem trit, to indicate sign. Therefore, there is no concept of &lt;code&gt;signed&lt;/code&gt; or &lt;code&gt;unsigned&lt;/code&gt; integers - everything is just an integer. So subtraction is achieved by simply inverting the operand and applying addition (which is implemented similarly to binary machines). This plus-minus consistency is also able to cut down on the number of carries required in multiplication operations.&lt;/p&gt;

&lt;p&gt;Another useful trait of balanced ternary (or any balanced numeral system for that matter!) is the fact that one can implement rounding on floating-point numbers by blatant truncation, which allows for a simplified implementation of division. This is because of the way balanced ternary represents the fractional portion of real numbers.&lt;/p&gt;

&lt;p&gt;Let me give you a simple example. Encoding &lt;code&gt;0.2&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.+--+ = 0 + (1 * (3^-1)) + (-1 * (3^-2)) + (-1 * (3^-3)) + (1 * (3^-4))
       = 0.33 + -0.11 + -0.03 + 0.01
       = 0.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to encode &lt;code&gt;0.8&lt;/code&gt; we must start with a &lt;code&gt;+&lt;/code&gt; in the most significant digit and then simply inverse the fractional portion (e.g &lt;code&gt;1 + -0.2&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+.-++- = 1 + (-1 * (3^-1)) + (1 * (3^-2)) + (1 * (3^-3)) + (-1 * (3^-4))
       = 1 + -0.33 + 0.11 + 0.03 + -0.01
       = 0.8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above we can see that truncating the trits to the right of the radix point is equivalent to rounding: &lt;code&gt;0.2&lt;/code&gt; becomes &lt;code&gt;0&lt;/code&gt; whereas &lt;code&gt;0.8&lt;/code&gt; becomes &lt;code&gt;1&lt;/code&gt;. Cool!&lt;/p&gt;

&lt;h2&gt;
  
  
  Programming with trits and trytes!
&lt;/h2&gt;

&lt;p&gt;OK, back to Setun one last time. In the late 60's, Brusentsov developed a more modern machine called "Setun-70" that embodied it's &lt;em&gt;ternarity&lt;/em&gt; more directly. The "Tryte" was introduced, which accounted for &lt;code&gt;6&lt;/code&gt; trits (roughly &lt;code&gt;9.5&lt;/code&gt; bits). The Setun-70 computer was a &lt;a href="https://en.wikipedia.org/wiki/Stack_machine"&gt;stack machine&lt;/a&gt;, and so rather than having machine instructions that specifically named registers for input and output, all operations worked on two stacks - one for operands (input) and one for return values (output). In order to accommodate this design, machine instructions were written in &lt;a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation"&gt;reverse Polish notation&lt;/a&gt; (a.k.a postfix).&lt;/p&gt;

&lt;p&gt;In the late 70's, Brusentsov and some of his students developed a programming language for the Setun-70 computer called the Dialog System for Structured Programming (DSSP). In my research &lt;sup&gt;4&lt;/sup&gt;, I have been able to discover that it is a stack-based language (no surprise there) similar to &lt;a href="https://en.wikipedia.org/wiki/Forth_(programming_language)"&gt;Forth&lt;/a&gt; that uses reverse Polish notation. This allows one to write programs in a relatively high-level language yet still feel "close to the metal". So close, in fact, that the authors had the following to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DSSP was not invented. It was found. That is why DSSP has not versions, but only extensions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take the following DSSP program that sums a bunch of numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 3 4  DEEP 1- DO +
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's try to break it down. In the first column we have the instruction, in the second we have the machine state after execution (the operand stack), and in the third I've provided an explanation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1    [1]            Push 1 to the stack.
2    [2 1]          Push 2 to the stack.
3    [3 2 1]        Push 3 to the stack.
4    [4 3 2 1]      Push 4 to the stack.
DEEP [4 4 3 2 1]    Push "the depth of the stack" (4) to the stack.
1-   [-1 4 4 3 2 1] Push -1 to the stack.
DO   [4 3 2 1]      Initiate LOOP, pop top two items from stack. To control 
                    the loop, the first item is applied to the second item until
                    it reaches 0.
+    []             Apply the "+" operation repeatedly until the LOOP terminates,
                    each time popping the top item from the operand stack stack,
                    applying + and then pushing the output to the return stack.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At the end of execution, the operand stack will be empty and the return stack will contain &lt;code&gt;[10]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can read a bit more about DSSP over at &lt;a href="http://brokestream.com/daf.txt"&gt;the website of Ivan Tikhonov&lt;/a&gt; (original authors Sidorov S.A. and Shumakov M.N.).&lt;/p&gt;

&lt;h2&gt;
  
  
  The future
&lt;/h2&gt;

&lt;p&gt;The development of balanced ternary machines has all but faded into a small footnote in the annals of computer history. And whilst research into memory cells able to efficiently represent three distinct states has been relatively minimal, there have been some efforts in this area.&lt;/p&gt;

&lt;p&gt;Particularly, researchers in Japan in the late 90's &lt;a href="http://ieeexplore.ieee.org/document/679270/"&gt;described the possibility&lt;/a&gt; of using a Josephson Junction to implement ternary logic. This could be achieved by circulating superconducting currents, either &lt;em&gt;clockwise&lt;/em&gt;, &lt;em&gt;counterclockwise&lt;/em&gt;, or &lt;em&gt;off&lt;/em&gt;. They found that this gave the memory cells a "capability of high speed computation, low power consumption and very simple construction with less number of elements due to the ternary operation".&lt;/p&gt;

&lt;p&gt;But, for the moment at least, I don't think that you'll be hearing much more about balanced ternary computers in the near future. Nor do I think DSSP is going to be the next big thing in programming language fanboyism. But I do believe that much wisdom can be sought by looking into the past &lt;sup&gt;5&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for making it this far. Corrections and feedback is very welcome. You can also read more &lt;a href="https://web.archive.org/web/20080808111712/http://sovietcomputing.com/node/47"&gt;here&lt;/a&gt;, &lt;a href="http://www.computer-museum.ru/english/setun.htm"&gt;here&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Balanced_ternary"&gt;here&lt;/a&gt;. :)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This depends on how the machine in question represents numbers. &lt;a href="https://en.wikipedia.org/wiki/Two's_complement"&gt;Two's complement&lt;/a&gt; is a common notation that would allow one to represent &lt;code&gt;-((2^n) / 2)&lt;/code&gt; to &lt;code&gt;((2^n) / 2) - 1&lt;/code&gt; in &lt;code&gt;n&lt;/code&gt; bits. &lt;/li&gt;
&lt;li&gt;Although Setun was the first electronic device to operate on balanced ternary, it's worth mentioning that the idea of using balanced ternary in computing devices was first popularized over 100 years before. In 1840, Thomas Fowler &lt;a href="http://www.mortati.com/glusker/fowler/fowlerbio.htm"&gt;built a calculating machine&lt;/a&gt; entirely from wood that operated on data in the balanced ternary notation.&lt;/li&gt;
&lt;li&gt;A far more accurate description can be found on the &lt;a href="http://www.computer-museum.ru/english/setun.htm"&gt;Russian Computer Museum website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Reference material for DSSP in English is not widely available so I'd like to provide a warning here that my knowledge is very limited and &lt;em&gt;just may&lt;/em&gt; contain some guesswork.&lt;/li&gt;
&lt;li&gt;My own contribution can be seen at &lt;a href="http://computerpionee.rs/"&gt;computerpionee.rs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Cover image, originally from the &lt;a href="http://hpc.msu.ru/"&gt;Moscow University Supercomputing Center&lt;/a&gt;, depicts one of the Setun machines in operation.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>ternary</category>
      <category>history</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>ComputerPionee.rs or: How six hours turned into six months</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Thu, 18 Aug 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/computerpioneers-or-how-six-hours-turned-into-six-months</link>
      <guid>https://dev.to/buntine/computerpioneers-or-how-six-hours-turned-into-six-months</guid>
      <description>&lt;p&gt;Several months ago I made an off-the-cuff comment to my girlfriend along the lines of:    &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I want to be able to see a "cloud" of computer pioneers sized relative to their impact in a given sub-field of computer science&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the time it felt like a simple enough project. I'd just grab some pre-packaged data and whip up a little circle-packing algorithm. Or maybe I'd just utilise some existing framework for data visualisation like D3. It'll probably take a few hours... Right?&lt;/p&gt;

&lt;p&gt;The first hurdle was the data. I could find many sources of data for achievements in the field, but nothing that was both consumable and sufficiently complete. Of the sources I found, all simply exclaimed that an achievement was exactly that: an achievement. None attempted to purvey the relative impact of each achievement - probably because of the inherent risk in doing so. And by risk I am referring to the fact that achievements can be difficult (or impossible) to truly compare to one another. Was Grace Hopper's &lt;a href="https://en.wikipedia.org/wiki/FLOW-MATIC"&gt;FLOW-MATIC&lt;/a&gt; compiler more impactful than Robin Milner's &lt;a href="https://en.wikipedia.org/wiki/ML_(programming_language)"&gt;ML&lt;/a&gt;? Would one have even happened without the other? Oh no, it's all getting too hard! In either case, I decided that I'd need to collect my own data in my own format and hope that the end result was both sufficiently fair and sufficiently accurate.&lt;/p&gt;

&lt;p&gt;Three hours in and I was up to pioneer #4. Great, I though, this is going to take a while... Although, I must admit, part of me was silently kind of excited as I knew doing it manually would force me into actually learning a tonne of interesting things about the past achievements of the giants in my field. And as an amateur history buff, such a challenge is not entirely uninteresting to me!&lt;/p&gt;

&lt;p&gt;But moving onto the actual circle-packing. And, in case you are unsure, when I say "circle packing" I mean (to quote wikipedia):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The arrangement of circles (of equal or varying sizes) on a given surface such that no overlapping occurs and so that all circles touch one another&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think &lt;a href="https://raw.githubusercontent.com/buntine/pioneers/master/data/screenshots/impact.png"&gt;something like this&lt;/a&gt; (spoiler alert!) or &lt;a href="http://www.adammarcus.com/wp/wp-content/uploads/2014/01/circle-packing-574x347.jpg"&gt;this&lt;/a&gt;. It turned out to be far more interesting (and challenging) than I had first imagined. I quickly decided against using an off-the-shelf data viz library as I wanted deep control over the eventual effect, didn't want to burden the user with unnecessary dependencies and, heck, I wanted to learn something interesting! &lt;/p&gt;

&lt;p&gt;As it turns out, certain forms of circle packing are considered NP-hard. But considering we are dealing with a visualisation we are not trying to find the "best" pack possible, but simply a pack that is visually "good enough" to the Human observer. Lucky us! The eventual algorithm I came up with was one that performs successive refinements by applying an attractive force to each circle pulling them to the center point of the canvas, which is then slightly over-powered by a detractive force when two circles collide. The exact amount of refinement passes we perform is a function of the number of circles we are packing. And to add padding of &lt;code&gt;N&lt;/code&gt; pixels between each circle we simply pretend each circles radius is &lt;code&gt;N/2&lt;/code&gt; larger than it is in reality. An upside to this approach is that it makes animating the whole thing trivial; it's just a matter of repositioning every circle on each iteration of the algorithm.&lt;/p&gt;

&lt;p&gt;As for tools I decided to keep everything simple. The website is implemented in &lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt; and Python. In place of a potentially gratuitous frontend framework I have a simple dispatch pattern (who I am sure the Gang Of Four has a nice name for which I've forgotten). State is handled by a simple dictionary of three items: &lt;code&gt;tab&lt;/code&gt;, &lt;code&gt;tags&lt;/code&gt; and &lt;code&gt;operation&lt;/code&gt; . I did make use of the (very handy) &lt;a href="http://snapsvg.io/"&gt;Snap SVG&lt;/a&gt; library in order to wrap the physical generation of SVG elements at runtime. I'll leave my analysis of TypeScript as a viable language to another post.&lt;/p&gt;

&lt;p&gt;During this project I realised just how much effort goes into the collection and curation of data. I must have spent at least 50 hours scouring the web for birth dates, suitable pictures and details of achievements. And then, of course, I was struggling with trying to "rank" achievements that I quite simply couldn't truly appreciate. For example, in 1971, Stephen Cook published a landmark paper that now-famously proposed the &lt;a href="https://en.wikipedia.org/wiki/P_versus_NP_problem"&gt;P vs. NP&lt;/a&gt; problem. This is &lt;em&gt;still&lt;/em&gt; an unsolved problem in computer science. I know what this problem states as far as the practicing programmer needs to and I also know it is incredibly important. But without recent education in theoretical computer science it's somewhat difficult for me to feel confident in exclaiming just &lt;em&gt;how&lt;/em&gt; important it is to the field. Hopefully over time others will help me correct some of my mistakes in this area.&lt;/p&gt;

&lt;p&gt;Because of the amount of time it took me to collate the data, I felt it was important that it was presented back to the community in a parsable format. And so I've packaged up the current data set in CSV format and hosted it on the &lt;a href="https://github.com/buntine/pioneers"&gt;official repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Anyway... Without further ado, I present: &lt;a href="http://computerpionee.rs/"&gt;The Pioneers of Computer Science&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>pioneers</category>
    </item>
    <item>
      <title>Simple technique for car engine sounds in video games</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Sat, 28 May 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/simple-technique-for-car-engine-sounds-in-video-games</link>
      <guid>https://dev.to/buntine/simple-technique-for-car-engine-sounds-in-video-games</guid>
      <description>&lt;p&gt;When I was working on &lt;a href="https://github.com/buntine/SwervinMervin"&gt;Swervin' Mervin&lt;/a&gt;, there was one thing that constantly sat on my TODO list. It seemed simple enough, but getting rid of it proved a lot harder than I'd initially thought.&lt;/p&gt;

&lt;p&gt;All I wanted to do was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generate engine sounds based on speed&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I didn't know exactly how I was going to do it, but I knew a couple of things. For example, I knew that the effect didn't need to be perfect. It just needed to get the general idea into the players head and their imagination would do the rest. I also knew that I didn't want to fall too far into the rabbit hole of &lt;a href="http://en.wikipedia.org/wiki/Audio_signal_processing"&gt;audio signal processing&lt;/a&gt; because I knew that once I climbed in, I'd either never come out or I'd come crawling out a defeated mess of a Human.&lt;/p&gt;

&lt;p&gt;My first thought was to generate a &lt;a href="http://en.wikipedia.org/wiki/Sawtooth_wave"&gt;sawtooth wave&lt;/a&gt; and set the frequency based on the players speed. I knew it could theoretically work because the sound programmer(s) on the classic arcade game &lt;a href="https://www.youtube.com/watch?v=_Y-oMdBaMFk"&gt;Pole Position&lt;/a&gt; used a similar (but more advanced) technique. I setup a little prototype and it worked fine, but unfortunately, sounded almost nothing like a car engine. I've &lt;a href="http://bunts.io/experiments/engine1"&gt;recreated the effect&lt;/a&gt; using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API"&gt;WebAudio API&lt;/a&gt;. I started dabbling with more advanced waveforms, but quickly realised I was slipping into the aforementioned rabbit hole of doom.&lt;/p&gt;

&lt;p&gt;If I was releasing Swervin' Mervin in 1979, my half-baked attempt at waveform generation may have been sufficient. Alas...&lt;/p&gt;

&lt;p&gt;My next idea was to find a soundbite of a running engine that I could seamlessly loop and simply shift the pitch and/or playback rate in response to the increase of the players speed. This idea fell to pieces when I realised that the sound libraries in &lt;a href="http://www.pygame.org/"&gt;Pygame&lt;/a&gt; simply didn't support seamless looping. There was a few milliseconds gap between each loop, which totally ruined the effect. But then I noticed in the documentation that &lt;a href="https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.queue"&gt;I could queue sounds&lt;/a&gt;. Awesome! But, to my dismay, it simply didn't work. I tried everything I could, until I eventually found out that &lt;a href="http://stackoverflow.com/questions/25277724/pygame-music-queue-not-functioning"&gt;it kinda' just doesn't work&lt;/a&gt;. Damn...&lt;/p&gt;

&lt;p&gt;I have &lt;a href="http://bunts.io/experiments/engine2"&gt;recreated the car engine soundbite effect&lt;/a&gt; again using the (quite obviously) far more powerful WebAudio API. As for a simple engine effect, this one actually works pretty well for me...&lt;/p&gt;

&lt;p&gt;I had one final idea that involved playing a soundbite of a car engine from idle to full acceleration and keeping track of the position we were at in the stream. If the player stopped accelerating, another soundbite that was a reverse of the first would start playing from the same position. Pretty simple, huh? Well, it turns out that Pygame doesn't provide us with an easy way of tracking the current position in a sound file! The WebAudio API does, of course...&lt;/p&gt;

&lt;p&gt;These little experiments taught me an important lesson:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be extra careful in the dependencies you choose!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I always knew that car engine sounds was something I wanted to add into Swervin' Mervin, but I was so confident in Pygame that I never really bothered to look into exactly how "powerful" it was. As it turns out, Pygame is all but dead and the last stable release was from over 6 years ago! Like all things in life - don't make decisions lightly... Happy coding.&lt;/p&gt;

&lt;p&gt;The WebAudio API examples I mentioned here can also be found in &lt;a href="https://github.com/buntine/CarEngines"&gt;my repository&lt;/a&gt; on Github.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>games</category>
      <category>sound</category>
    </item>
    <item>
      <title>Aha! Understanding lifetimes in Rust</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Sat, 02 Jan 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/aha-understanding-lifetimes-in-rust</link>
      <guid>https://dev.to/buntine/aha-understanding-lifetimes-in-rust</guid>
      <description>&lt;p&gt;When I'm learning something complex, I often find myself waiting around for the &lt;a href="https://en.wikipedia.org/wiki/Eureka_effect"&gt;Eureka effect&lt;/a&gt; (also known as the "Aha! moment"). Wikipedia defines the Eureka Effect as the "common human experience of suddenly understanding a previously incomprehensible problem or concept". This effect has been understood for thousands of years and was most-famously popularised by the great Greek mathematician Archimedes, who discovered how to measure the volume of an object whilst sitting in his bath.&lt;/p&gt;

&lt;p&gt;In this post I'd like to share my own "Aha!" moments whilst grappling with &lt;a href="https://doc.rust-lang.org/book/lifetimes.html"&gt;lifetimes&lt;/a&gt; in Rust. I hope this helps some of you in your journey to the summit of Rust mountain.&lt;/p&gt;

&lt;p&gt;I will assume that the readers already know some basic Rust and have read the documentation on &lt;a href="https://doc.rust-lang.org/book/ownership.html"&gt;ownership&lt;/a&gt;, &lt;a href="https://doc.rust-lang.org/book/lifetimes.html"&gt;lifetimes&lt;/a&gt; and &lt;a href="https://doc.rust-lang.org/book/references-and-borrowing.html"&gt;borrowing&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple scenario
&lt;/h2&gt;

&lt;p&gt;First, I want to propose a simple, suitably contrived snippet that requires us to stipulate a lifetime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// Our Person struct consists of a name and an optional reference to another Person.&lt;/span&gt;
&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;Eq,&lt;/span&gt; &lt;span class="nd"&gt;PartialEq)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Rust lets us elide the lifetime of &amp;amp;self here.&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;parents_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.parent&lt;/span&gt;&lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And some code that implements it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;jane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;tom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tom"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;jane&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tom&lt;/span&gt;&lt;span class="py"&gt;.parent&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tom&lt;/span&gt;&lt;span class="nf"&gt;.parents_name&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jane&lt;/span&gt;&lt;span class="py"&gt;.parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jane&lt;/span&gt;&lt;span class="nf"&gt;.parents_name&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  It's just a contract!
&lt;/h2&gt;

&lt;p&gt;If we omit the lifetime specifiers in the previous example, we run into problems:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;cargo &lt;span class="nb"&gt;test
  &lt;/span&gt;Compiling lifetimes v0.1.0 &lt;span class="o"&gt;(&lt;/span&gt;file:///home/andrew/dev/rust/lifetimes&lt;span class="o"&gt;)&lt;/span&gt;
  src/lib.rs:4:20: 4:27 error: missing lifetime specifier &lt;span class="o"&gt;[&lt;/span&gt;E0106]
  src/lib.rs:4     parent: Option&amp;lt;&amp;amp;Person&amp;gt;,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why is this? Well, it's because Rust needs a way to ensure that any borrowed Person does not outlive any other Person that is borrowing it. And therein lay my first "Aha!". When we specify a lifetime explicitly, &lt;strong&gt;we are simply entering a contract with the compiler guaranteeing that the given resource will be available for a certain scope&lt;/strong&gt;. We are, in a sense, making a promise to the compiler. But we all know that promises can be broken and so Rust still makes all of the necessary checks for us.&lt;/p&gt;

&lt;p&gt;We are &lt;em&gt;not&lt;/em&gt; "creating" lifetimes. Nor are we telling Rust to allow a reference to exist somewhere that it shouldn't. We are just telling the compiler to complain if the calling code breaks the lifetime rules we've dictated.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's in a name?
&lt;/h2&gt;

&lt;p&gt;So exactly which scope is &lt;strong&gt;'a&lt;/strong&gt;? Is it referring to something in our Person type? No, &lt;strong&gt;'a&lt;/strong&gt; is defined by the calling code. Our type just dictates how long the value we are borrowing must live. Think of it like â€œfor any lifetime â€˜aâ€ rather than â€œthe particular lifetime called â€˜aâ€. The constructor demonstrates this pretty well. Look at the function signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are saying &lt;strong&gt;"new is a function that accepts a reference to a Person that must live for a lifetime we are going to call 'a' and it returns a new Person that contains a reference that must live for the same lifetime"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;See that? We've stated at the syntactic level that the borrowed value (parent) will have the same lifetime as the thing that's borrowing it (the return value). In reality, the borrowed value may outlive its container. Our lifetime annotations just refer to the smallest lifetime for which the value must exist.&lt;/p&gt;

&lt;h2&gt;
  
  
  But why?
&lt;/h2&gt;

&lt;p&gt;Why canâ€™t Rust just deal with this for us under the hood? And why canâ€™t Rust just ensure that an object lasts for exactly long as itâ€™s required by the program? Like, for example, an object that escapes its scope in &lt;a href="https://golang.org/"&gt;Go&lt;/a&gt; will be heap-allocated and dealt with by the garbage collector at a later time. Well, thatâ€™s just it: &lt;strong&gt;such conveniences require a garbage collector&lt;/strong&gt;, which would give our programs a performance hit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; As told by Reddit user andytoshi in &lt;a href="https://www.reddit.com/r/rust/comments/3z4fna/ive_documented_some_of_my_aha_moments_when/cyjelri"&gt;this comment&lt;/a&gt;, Rust's ownership and borrowing systems do a lot more than simply "avoid garbage collection for performance reasons". They also, potentially more importantly, guarantee that any borrowed value you have access to &lt;em&gt;will not change out from underneath you&lt;/em&gt;, even if you have a mutable reference to something. This gives our code a higher degree of reasonability and reduces overall complexity. &lt;/p&gt;

&lt;p&gt;Rustâ€™s ownership and lifetime systems allow the compiler to validate the memory safety of our programs at &lt;em&gt;compile-time&lt;/em&gt;. We donâ€™t have to delegate this task to a garbage collector at runtime such as in Ruby, Python, Go, etc nor do we have to manually allocate/deallocate memory ourselves such as in C. Infact, it's probably worth mentioning at this point that all this mumbo-jumbo about lifetimes is only of concern at compile-time. We are only referring to the lexical scopes that our borrows must exist in.&lt;/p&gt;

&lt;p&gt;Rusts memory model is one of a few things that Rust refers to as a â€œzero-cost abstractionâ€, which brings me to my next Aha!&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero-cost to our programs, not to us!
&lt;/h2&gt;

&lt;p&gt;When I first started reading about some of these zero-cost abstractions, I wondered how it really applied here as the learning curve involved certainly didnâ€™t seem like a â€œzero-costâ€. But then I realised that &lt;strong&gt;when Rustaceans talk about zero-cost, they are talking solely about runtime performance of programs&lt;/strong&gt;. Aha! Infact, in the case of lifetimes, Rust is actually removing the necessity of a garbage collector entirely by pushing additional rules onto the programmer. If your program compiles - itâ€™s almost certainly memory safe. These rules force us to think about the memory safety of the way we are programming upfront rather than in bug fixes months down the track. Yes, it's potentially more work, but it will undoubtedly save us headaches in the future!&lt;/p&gt;

&lt;h2&gt;
  
  
  One last point
&lt;/h2&gt;

&lt;p&gt;Often when Iâ€™m dealing with simple structs I find myself wondering why Rust canâ€™t elide here like it does in certain function declarations. For example, when we say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Canâ€™t Rust just translate it to this under the hood:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This point has caused some discussion in the Rust community. Some &lt;a href="https://news.ycombinator.com/item?id=10484568"&gt;core contributors&lt;/a&gt; even believe that modern Rust should elide in this scenario.&lt;/p&gt;

&lt;p&gt;Itâ€™s important to remember here that Rust often favours explicitness over succinctness. I think the example Yehuda Katz gives in his reply in the thread above is a good one. If we were able to omit the lifetime annotations then implementing code may end up looking something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;â€¦&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have suddenly lost the fact that Person contains a borrowed value. In complex, real-life software, this could lead to confusing bugs. But with that said, I imagine that there will be some movement in this area in the coming versions of Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  The End
&lt;/h2&gt;

&lt;p&gt;That's all I have for the moment. I hope that something in here helped you on your journey. And if you've noticed that I've said something that's incorrect, please feel free to correct me. Thanks in advance!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>lifetimes</category>
      <category>programming</category>
    </item>
    <item>
      <title>Building a DIY arcade machine, part 1: Baby Steps</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Fri, 09 Oct 2015 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/building-a-diy-arcade-machine-part-1-baby-steps</link>
      <guid>https://dev.to/buntine/building-a-diy-arcade-machine-part-1-baby-steps</guid>
      <description>&lt;p&gt;In &lt;a href="https://dev.to/buntine/building-a-diy-arcade-machine-part-0-motivation"&gt;the previous post&lt;/a&gt;, I spoke about the motivation to build my own arcade machine. This time I'm going to start showing you how I actually did it.&lt;/p&gt;

&lt;p&gt;My years of computer programming have taught me that the best way to start a large and daunting project is to take the overall problem and sub-divide it into lots of little problems. The idea is that you can abstract away the scariness of the overall goal and just focus on small tasks, whose sum will (well, may) reveal something awesome.&lt;/p&gt;

&lt;p&gt;So I sat down and started jotting down all of the things I could think of. I ended up with a list that contained things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find old monitor&lt;/li&gt;
&lt;li&gt;Get old computer or Arduino&lt;/li&gt;
&lt;li&gt;Work out how to get component speakers working&lt;/li&gt;
&lt;li&gt;Buy thing to cut wood good (It's called a Jigsaw)&lt;/li&gt;
&lt;li&gt;Buy lots of wood&lt;/li&gt;
&lt;li&gt;Learn how to use jigsaw&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And so on...&lt;/p&gt;

&lt;p&gt;One of the tasks that seemed the scariest to me was "&lt;strong&gt;Work out how to wire Joysticks and buttons into computer&lt;/strong&gt;". I had no idea how to do this, or even if I'd phrased the task correctly. And so I did what every good developer does: I started Googling. Pretty quickly I came across a company called &lt;a href="https://www.ultimarc.com"&gt;Ultimarc&lt;/a&gt; who produce a product called the &lt;a href="https://www.ultimarc.com/ipac1.html"&gt;I-PAC&lt;/a&gt; (no relation to any Apple product).&lt;/p&gt;

&lt;p&gt;The I-PAC is a PC interface to industry-standard arcade controls: A keyboard encoder. In other words, it translates button presses and joystick movements into key strokes on a normal PS/2 keyboard. And it's re-programmable so you can assign new keystroke outputs to each pin.&lt;/p&gt;

&lt;p&gt;I excitedly bought the i-PAC along with 8 arcade pushbuttons and a joystick called the &lt;a href="http://www.ultimarc.com/controls.html"&gt;Mag-Stik Plus&lt;/a&gt;. While I waited for my package to arrive, I went to my local electronics shop and picked up some hook-up wire, crimp connectors, a small vice and a beginners electronics kit that had some wire cutters, a soldering iron, some solder and a bunch of heat shrinks in different sizes and colours. I was all set!&lt;/p&gt;

&lt;p&gt;After some searching I'd found an old monitor (in my wardrobe), found an old computer (the old office file server, a Dell setup from the early naughties) and purchased a shitload of wood (or as I now knew to call it: MDF). I reformatted the old Dell computer and chucked Linux on there so I'd feel more comfortable. I also noticed that the machine had an OK old graphics card in there! I thought that I may be able to use it to get some more advanced arcade games working. So I decided to inspect it a bit more closely and swiftly snapped off a few diodes... Oh well, there goes that idea.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nQGKLy8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/uPeNHQd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nQGKLy8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/uPeNHQd.jpg" alt="She works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;My apartment is small.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When my package from Ultimarc finally arrived, the first task I set myself was to wire up the joystick so that when I jiggled it my computer would receive the appropriate arrow key strokes. This, luckily, turned out to be pretty simple. The joystick had four &lt;a href="https://en.wikipedia.org/wiki/Miniature_snap-action_switch"&gt;microswitches&lt;/a&gt; on it; one for each direction. I needed to run a wire from each microswitch into the appropriate pin on the I-PAC. All four microswitches also needed to be grounded, which I accomplished by &lt;a href="https://en.wikipedia.org/wiki/Daisy_chain_(electrical_engineering)"&gt;daisy-chaining&lt;/a&gt; them using a bunch of wires all connected together in the right way. The hardest part was getting the length of the wires correct. I must have gone through 20 meters of the stuff...&lt;/p&gt;

&lt;p&gt;When I felt like I'd got everything hooked up correctly I plugged the I-PAC into my computer via USB and started gently moving the joystick head down. I expected that it would either do nothing or just simply blow up. But, no. &lt;strong&gt;It worked! It fucking worked!&lt;/strong&gt;. I was so excited that I made my girlfriend watch me move around a text document using a Joystick that was flimsily attached via some probably-too-short hookup wire.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vj9jdkK1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/OKUcnFK.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vj9jdkK1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/OKUcnFK.jpg" alt="She still works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The joystick connected to the I-PAC. Dangerously close to the sink.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'd hit my first milestone. In software, it's often essential to create what's known as a "proof-of-concept" before taking on a larger project. It's essentially a super scaled down version of the final product that  demonstrates the essence of a potential solution. This probably-poorly wired joystick was my proof-of-concept. And so at this point I knew that I was going to succeed to some degree. I still had no real idea what success would look like, but I knew that, for better of worse, I'd produce something!&lt;/p&gt;

&lt;p&gt;My next goal was to fully wire the player controls and mount them into their final resting place. But I'll talk about this more in the next part. Stay tuned.&lt;/p&gt;

</description>
      <category>diy</category>
      <category>arcade</category>
    </item>
    <item>
      <title>Building a DIY arcade machine, part 0: Motivation</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Sat, 15 Aug 2015 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/building-a-diy-arcade-machine-part-0-motivation</link>
      <guid>https://dev.to/buntine/building-a-diy-arcade-machine-part-0-motivation</guid>
      <description>&lt;p&gt;I've always been a fan of retro video games. It takes me back to my childhood years when my brother, my sister and I would huddle on the floor in front of our tube television and play games on our Sega Master System console. It was one of the special edition versions that had &lt;a href="https://en.wikipedia.org/wiki/Alex_Kidd_in_Miracle_World"&gt;Alex Kidd in Miracle World&lt;/a&gt; built-in. I remember that game in particular because it was both very difficult and also didn't ship with any notion of a "save game". So, naturally, we would just pause the game and leave the console running for weeks at a time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1fe_bRhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/dCBECJ0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1fe_bRhY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/dCBECJ0.jpg" alt="Me as a kid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Artists impression of my childhood.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some time in the early nineties we got an upgrade when our Dad surprised us with a brand new Super Nintendo. I have fond memories of beating games like &lt;a href="https://en.wikipedia.org/wiki/Donkey_Kong_Country_2:_Diddy%27s_Kong_Quest"&gt;Donkey Kong Country II&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Mega_Man_X_(video_game)"&gt;Mega Man X&lt;/a&gt;. Each weekend, my brother and I would hire a mix of SNES games, Schwarzenegger films and WWF wrestling videos from the local video store. These were the pre-WWW days when the only indication of whether or not a game was any good was by looking for the "Highly Recommended!" sticker on the cover.&lt;/p&gt;

&lt;p&gt;Sporadically, my siblings and I were also known to lose a dollar or two (hundred) at our local arcade that, funnily enough, had been fashioned out of an old Horse Stables building. We were particularly good on the 4-player X-Men game (a classic of the Beat 'em up genre) and a relatively unknown Japanese platformer named &lt;a href="https://en.wikipedia.org/wiki/Toki_(video_game)"&gt;Toki&lt;/a&gt;. One of my good friends actually pissed his pants while playing Toki because he was up to a huge score and didn't trust me to take over for 30 seconds. Mum drove us home in shame that day...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QqRYKcTW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/OOf8GVE.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QqRYKcTW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/OOf8GVE.jpg" alt="The local arcade"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The local arcade where Adam pissed his pants.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fast-forward 20 years. I'm sitting at the dinner table with my girlfriend talking about an idea for a video game I want to make. It will be a racing game like the one I used to play in the arcade: &lt;a href="https://www.youtube.com/watch?v=WiWiTXq4yYY"&gt;Out Run&lt;/a&gt;. I knew  that for full effect, the game would need to run in an actual arcade machine. My initial thoughts were that perhaps I could buy an existing arcade machine and replace some of the internal hardware. It turns out that this &lt;em&gt;is&lt;/em&gt; a possibility because most arcade machines manufactured after the mid-eighties were standardised by &lt;a href="https://en.wikipedia.org/wiki/Japan_Amusement_Machinery_Manufacturers_Association#Connector_standards"&gt;JAMMA&lt;/a&gt;, which means that you can swap out an old circuit board for a modern motherboard and connect the two with a JAMMA-2-USB connector.&lt;/p&gt;

&lt;p&gt;But, alas, finding an original arcade machine in good condition is relatively difficult. This may have something to do with the fact that the poor machines typically spent their first twenty years being beaten to shit by everyone that played them and then laid rotting due to exposure to the elements in some old guys shed. On top of this, an active collectors market has also pushed prices up steadily...&lt;/p&gt;

&lt;p&gt;I was left with only one option: Build the damn thing myself!&lt;/p&gt;

&lt;p&gt;In this blog series I will detail the trials and tribulations faced by me, Andrew Buntine, a man with no experience in cabinetry or electronics, in building my very own arcade machine. And, just to prove that I really did build it and it really does exist, I'd like to give you a sneak preview of the finished product:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--THCUkgb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/JC4TikH.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--THCUkgb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/JC4TikH.jpg" alt="Arcade machine"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rounded corners are hard.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stay tuned for the next part. In the mean time, maybe you could try to beat the hardest video game ever made, &lt;a href="https://en.wikipedia.org/wiki/Battletoads_(video_game)"&gt;Battletoads&lt;/a&gt;, on a NES emulator? Good luck!!&lt;/p&gt;

</description>
      <category>diy</category>
      <category>arcade</category>
    </item>
    <item>
      <title>ClojureScript saved me 100 hours</title>
      <dc:creator>Andrew Buntine</dc:creator>
      <pubDate>Tue, 28 Jul 2015 00:00:00 +0000</pubDate>
      <link>https://dev.to/buntine/clojurescript-saved-me-100-hours</link>
      <guid>https://dev.to/buntine/clojurescript-saved-me-100-hours</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/buntine/Medieval-Alien-Massacre/commit/101082f3d99317a10a27da9e544bcc650b91b565"&gt;Five years ago&lt;/a&gt;, I started tinkering with Clojure, a Lisp that runs atop the JVM. The language was still somewhat esoteric at the time. It &lt;em&gt;was&lt;/em&gt; a Lisp, afterall!&lt;/p&gt;

&lt;p&gt;Picking up Clojure wasn't too difficult because I'd already spent a few years playing around with Scheme. Generating fractals, Fibonacci sequences (of  course) and working my way through the &lt;a href="https://mitpress.mit.edu/sicp/"&gt;SICP&lt;/a&gt; and &lt;a href="https://www.cs.berkeley.edu/~bh/ss-toc2.html"&gt;Simply Scheme&lt;/a&gt; textbooks. Infact, I think I may still be helping some first-year Comp Sci students at Berkeley University through my &lt;a href="https://github.com/buntine/Simply-Scheme-Exercises"&gt;answers repository&lt;/a&gt; to the latter. Hey Berkeley, how about an honorary degree? Oh c'mon...&lt;/p&gt;

&lt;p&gt;It was obvious from the get-go that Clojure was going to be a big deal. Although the language designer, Rich Hickey, decided to stick with S-Expressions, Clojure came with a much richer set of data structures than the humble linked list. And they were immutable and persistent, which gave them both the thread safety of immutability and the performance characteristics of mutability. The whole Java interop thing was also very important... It meant that Clojure programmers had access to the plethora of existing Java libraries.&lt;/p&gt;

&lt;p&gt;The first non-trivial software I wrote in Clojure was a text-based game called &lt;a href="https://github.com/buntine/Medieval-Alien-Massacre"&gt;Medieval Alien Massacre&lt;/a&gt;. It was inspired by an old game called Dunnet that, funnily enough, still ships as a default package with the Emacs editor today. The game was great fun to write, although I made the mistake of choosing a title and publishing the game &lt;em&gt;before&lt;/em&gt; I'd written the story. The finished product did not involve any massacres and did not occur in the middle ages. It did, however, contain aliens so I wasn't completely wrong!&lt;/p&gt;

&lt;p&gt;But, more importantly, sharing the game with my non-techy friends was kind of difficult as it required them to have Java installed and run it in a shell. I decided that I would (one day) rewrite the game in Javascript so that I could utilise the browser. As far as ease-of-distribution goes, it's difficult to beat Javascript.&lt;/p&gt;

&lt;p&gt;And so sat my game. Incomplete and unknown. Alas, such is the blight of the the computer programmer... Until, that is, a friend mentioned to me that a project called &lt;a href="https://github.com/clojure/clojurescript"&gt;ClojureScript&lt;/a&gt; was gaining some popularity. ClojureScript is a Clojure to Javascript compiler, which meant, atleast theoretically, I could directly compile Medieval Alien Massacre to Javascript and save myself the hassle of doing it manually!&lt;/p&gt;

&lt;p&gt;I knew it wouldn't "just work". I was using Java's threading library to suspend execution, doing user IO from the terminal, etc. And so I started through the code, line by line, flagging everything that I thought might not compile. The process turned out to be relatively simple. ClojureScript swaps out Java interop for Javascript interop and so, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Thread&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;was simply replaced with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.setTimeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;js/window&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I replaced all IO with a simple HTML &lt;code&gt;input&lt;/code&gt; wrapped in a &lt;code&gt;form&lt;/code&gt; with a submit event attached to it. I'd say about 85% of the core codebase stayed exactly the same. The language parsing didn't change at all!&lt;/p&gt;

&lt;p&gt;In total, the process took me three quiet evenings after work. I also took the chance to give the game a more appropriate name: &lt;a href="https://github.com/buntine/MoonDweller"&gt;Moon Dweller&lt;/a&gt;. At a guess, I reckon I would have spent atleast 100 hours writing my own version in Javascript.&lt;/p&gt;

&lt;p&gt;Oh, and guess what?! You can play it right now directly in your browser!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://moondweller.io/"&gt;PLAY MOON DWELLER NOW&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy hacking, people.&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>clojurescript</category>
      <category>games</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
