Node.js + raspberry pi + aquarium = awesome!

Melissa and I recently bought a new aquarium. The tank is set up now and is currently cycling. We plan to get about 3 or 4 fish, with a German Blue Ram as the centerpiece fish. One of the things I really like about our tank is that it has a separate day/night LED lighting system in it. Power is reduced and night-time lighting is a little more realistic, but there is a major downside: there is no automation on the lights. There is a physical switch that you throw to switch between the lights and off, meaning we can’t throw it on a standard electrical timer.

Being the enterprising yet lazy programmer that I am, I couldn’t just allow this situation to stand. I already have a Raspberry Pi that was just lying around (I originally intended to use for a sleep project but I got sidetracked with other projects). So I decided to get to work.

Using a Raspberry Pi just to turn lights on and off at a preset schedule isn’t a very good use of the hardware though, so I started brainstorming other cool things I could do with it, and I’ve settled on the following features for v1:

  • Lights will be switched automatically each day by querying an internet service for sunrise sunset times.
  • Delays/offsets from sunrise/sunset can be configured so that lights can mimic daylight breaking, which tends to be a little off from actual sunrise times
  • Switch to manual control
  • Have a web server running on the Raspberry Pi that clients can connect to to set the configuration options

That’s a much better use of the hardware :)

So far I have implemented a few odds and ends in node.js and have designed the interface circuit. On the software side, I implemented a new logging library for node.js called transport-logger that is inspired by winston (you can install it via “npm install transport-logger”). The schematic for the interface circuit is shown below

Aquarium Schamtic

Aquarium Schamtic

The circuit is pretty simple. Starting at the connector for the Raspberry Pi, we go into a Texas Instruments CD40109B level shifter that converts the 3.3v signals from the Raspberry Pi to 5v levels (and provides a little protection in the process). The level shifter is powered by an ON-semi LM2574N 5v switching regulator. The 5v output signal from the level shifter controls the coils on two relays that are connected to the day and night LED setups. The relays are given power from the switch, which is in turn powered from the main aquarium supply. The switch allows me to either turn off the sytem entirely, or bypass the raspberry pi in case I need to remove it but don’t want the lights to turn off. Below is a photo of some testing I was doing on a breadboard where I tested powering the relays from the switching regulator.

Breadboard Testing

Breadboard Testing

All of the project is being hosted on GitHub and all software, schematics, layouts, etc are released under the GPLv3 license.

Hiking and photographing Upper Stevens Creek County Park

It’s been a few months since I’ve gone hiking, but I finally managed to get out and go hiking this last weekend. I hiked in Upper Stevens Creek County Park south of the bay area. To date, it is the longest hike I’ve done at 11.2 miles with a lot of elevation change during the first half. It was supposed to be only a 9.5 mile hiking, but I kinda missed one of my trail splits and had to backtrack…oops :) . That aside it was a great hike. The terrain was highly varied, going from dense, tall forests to shrubs to meadows to almost swamp-like. There were also a lot of deer out that day; I probably saw two dozen.

Anyways, I managed to get a few shots that show the wide variety of terrain on this hike.

Upper Stevens Creek 1

IMG_3268

IMG_3283

IMG_3295

IMG_3300

Ambiguous Blocks: when unknown values meet conditionals

I last talked about unknown values and how they work in expressions, but what happens when an unknown value is passed to a conditional statement? Let’s say you try to do:

var x = new Date();
if (x < new Date(2013, 3, 12)) {
    console.log('It is before April 12th, 2013');
} else {
    console.log('It is on or after April 12th, 2013');
}

What should happen here? From an analysis perspective, both branches will be run at some point in time by someone, so it makes sense to analyze both branches and this is exactly what the code processor does. In general, anytime a decision needs to be made on whether or not a block of code needs to be executed and that decision is based on an unknown value, that code is executed.

This raises the question of course, what happens when statements inside of this code block affect code outside of the block? For example:

var x = 10;
if (new Date()) {
    x += 10;
}
console.log(x);

What should the value of x be when it is logged to the console? Since it depends on how the actual code will be executed on device, the answer is that we don’t know and so I set x to unknown. Since code processor has to treat assignments (and related operations) differently inside of the block than it does outside of the block, the code processor flags the block as ambiguous.

There are further rules and restrictions when dealing with these ambiguous blocks. What happens when a return statement is encountered inside of an ambiguous block? In this case, the code processor marks the function as having an unknown return value, stops evaluating the block, and continues outside the block. Semantically, the return behaves as if it were a break statement inside a loop block. The rest of the function is evaluated, and then unknown is returned. Similar, but a lot more tricky, is throw statements; honestly I haven’t figured this one out yet.

My examples so far have all been if statements, but what about loops? The same rules apply, with special handling of the iteration constructs. For for loops, while loops, and do-while loops, if the loop conditional is unknown, the body of the loop is evaluated exactly once as an ambiguous block. If the iteration condition is known for the first few iterations, but later becomes unknown, then the block is evaluated one more time in ambiguous mode as a sort of “clean up.” Note that a for loop conditional is the middle argument, i.e. the “i < Date.now()" bit below:

for (var i = 0, i < Date.now(); i++) {}

For-in loops are different than the other loops, since they don't have an iteration condition. Instead, a for-in loop is evaluated in ambiguous mode if the value to be iterated over is unknown, as shown below:

for (var i in Date.now()) {}

There is one more important case that the code processor handles: what happens when a function is called inside of an ambiguous block? That will be discussed in my next post dealing with ambiguous contexts, so stay tuned.

Tomb Raider in a post The Walking Dead and Spec Ops: The Line world

Tomb Raider

Tomb Raider

I’ve recently finished playing the new Tomb Raider. It’s certainly a good game, but I can’t help but feel that my enjoyment of the game is being tarnished by The Walking Dead and, more importantly, Spec Ops: The Line. I recently wrote about how much The Walking Dead affected me, but I played Spec Ops before I started my blog, so I have no previous words of my own to share on the topic. Sufficed to say, the narrative in Tomb Raider comes of as amateurish compared to The Walking Dead, and the cognitive dissonance between the narrative of Lara the victim and the game play of Lara the superhuman serial killer is made all that more obvious by Spec Ops: The Line.

I was really looking forward to this game because the trailers and press coverage leading up to the release seemed to indicate that there was going to be a serious look at what happens when someone inexperienced like Lara is thrown into an extreme situation. In practice there is a passing mention of the idea in a few cutscenes early on in the game, but that’s about it. In the past though, I would have thought that this basic treatment of the material was a brave step for gaming, but The Walking Dead and Spec Ops have really shifted my perspective on how good narrative can be in a game.

Ultimately this is a good problem to have. It’s a sign that the industry is maturing. There has long been a stereotype that gaming is only for kids and teenagers hunkering down in their parent’s basements. Obviously this isn’t true, but stereotypes usually don’t appear out of nowhere. I think there are historical reasons that this stereotype exists, but I also think there may be another reason: gaming hasn’t done a good job at targeting adults specifically. Whenever a game tries to be “adult” or “mature”, it usually just ends up being juvenile in the same vein as Duke Nukem (not that I didn’t love DN3D when I was in high school).

If we look at film, there is a huge variety of genres. Sure, action movies and romantic comedies make up the bulk of films made, but there are a lot of other genres, with plenty of intellectually challenging films to choose from. Unfortunately the diversity of games doesn’t match the diversity of films (as an aside: game “genres” aren’t really genres…they are gameplay mechanic classification systems. We don’t call film genres “3D”, “70mm”, “digital”, “IMAX”, etc, so why do we call game genres “FPS”, “RPG”, “Driving”, etc?).

That’s not to say no one has ever tried to make gaming’s equivalent of “Citizen Kane,” but for one reason or another, no one had really succeeded before The Walking Dead (just my opinion, of course). I think that 2011/2012 was something of a tipping point though. I think that everything traces back primarily to Braid. Braid was an indie game with a thematically complicated plot/message aimed squarely at adults that also managed to garner a sizable audience. The game was released in 2008, roughly four and a half years ago.

Of course one popular indie game isn’t going to sway the direction of AAA game development. But a sea storm of amazing indie games can. After Braid, we have seen a multitude of truly amazing indie games come out. Off the top of my head, we saw Amnesia: The Dark Descent, Bastion, and The Binding of Isaac in 2011, and 2012 was a veritable gold mine of amazing indie games. In fact, I would say 2012 is quite possibly the best year in gaming I can remember. We saw Journey on the PS3 (I really want to play this, but seeing as I don’t have a PS3…), Dear Esther, Thomas Was Alone, Kentucky Route Zero, and the list goes on. Most importantly though, was Spec Ops: The Line and The Walking Dead. The timing of these releases is important. Braid paved the way, and several follow-ups established the indie scene, and now we are reaping the benefits. I am truly excited to see where gaming goes from here.

Which brings me back to Tomb Raider. I fully believe that the developers wanted to craft an amazing story, but the AAA world takes longer to respond to changing market dynamics. If nothing else, the development time for a game ensures that any given game won’t show it’s influences for a few years. In fact, according to the Tomb Raider Wikipedia page, development began in 2008, the same year Braid was first released. I think that this quote by lead writer Rhianna Pratchett shows the limitations of the mindset prior to the indie revolution:

There’s always a balancing act. I think the narrative team would have liked to see that as a slower ramp up. That time between the first kill and lots of kills, I think we would have liked to see that slower paced. But, this isn’t always about narrative.

This simply is no longer the case, and Spec Ops: The Line and The Walking Dead proved that. Both games take place in very violent worlds, but the former used lots of kills specifically to advance the narrative, and the latter simply didn’t have lots of kills. It leads me to wonder what Tomb Raider would have been like if it had been released two years later, and it makes me really excited to see what the next Tomb Raider will be like. The times they are a-changin.

The Walking Dead won’t get out of my head, and that’s a good thing

The Walking Dead

Last night, I finished Season 1 of The Walking Dead by Telltale Games and I can’t stop thinking about it. That’s why I’m sitting here writing this at 9am on a Saturday morning instead of still being asleep in bed. To say that it completely deserved all of the game awards it earned doesn’t even begin to do the game justice, even with 2012 being the best year in gaming in a long time, possibly ever.

This is a game that is all about story and characters. Telltale made a smart decision in making this a traditional point and click adventure game because it forces the game to really focus on the characters. Two characters in specific: Lee and Clementine. This game is their story, the zombies are just the backdrop.

And what a game it is! For a long time, people have been comparing the evolution of games with the evolution of film, claiming that games are now where film was in the early twentieth century, still trying to figure out just what can be done. Along with this, there is the inevitable question: “when will gaming have it’s Citizen Cane?” I wonder if The Walking Dead is it.

Clementine

I think this quote from a review of the game says it better than I ever could:

The Walking Dead reflects a resounding cry from the gaming community for an adult gaming experience to serve as a platform for exploring the art of feeling. It has set a new standard for storytelling and player immersion few games will be able to meet. If you have not played The Walking Dead you will be poorer for it, both as a gamer and as a human being. It is an experience transcending just entertainment.

Gaming won’t be the same again. I won’t be the same again for having played it. By the time the credits rolled, I was emotionally wrecked and, I’m not ashamed to admit, crying my eyes out. Never before has a game touched me like this one has.

Each episode starts with the saying “This game series adapts to the choices you make. The story is tailored by how you play.” At first, it seems like they are saying “if you make choice A, then plot B happens,” but the game does something much more subtle and brilliant than that. In The Walking Dead, the choices you make affects characters, not plot. By having your choices affect the relationships with everyone else, it makes the story unbelievably personal.

Spoilers behind the fold, you have been warned!

Continue reading

Version 0.3.1 of the Titanium Code Processor is out

Version 0.3.1 of the Titanium Code Processor has been released!

New Features

  • Added support for parsing values from Ti.UI.createXXX constructors
  • Added support for Ti.UI.Window.url

These new features are all under the hood improvements that increase the intelligence of the code processor and make the results more accurate.

Bug Fixes

  • Fixed a bug where deferred analysis of functions wasn’t setting up the proper context
  • Prevented duplicate errors and warnings from being reported

The first bug occurred when evaluation of a function was deferred. Functions are deferred either because they are a callback (addEventListener/setTimeout/etc) or because they weren’t analyzed and the processUnvisitedCode option was set to true. When a deferred function was evaluated, the context stack wasn’t being set up properly, and all of the closure variables (variables defined outside the function itself) were missing.

The second bug was really just an aesthetic thing that came about as a side effect of the code processors’ error recovery mechanisms. In a normal JavaScript engine, execution stops completely when an exception occurs and the error is only reported once (by definition). In the code processor with exception recovery enabled, the same “bad” line of code could be visited multiple times, thus throwing the exception multiple times. I now take all of these exceptions and roll them into a single exception with a count message.

How Unknown Values Work in the Titanium Code Processor

This post has been a long time coming, sorry for the delay (work has been really busy lately)! In a previous post I discussed the basic concepts behind how I do dynamic offline analysis of JavaScript in the Titanium Code Processor. Today I want to follow up on this post and talk a little more about unknown values.

An unknown value is its own data type. Technically, it has no type and represents all types at the same time. An unknown value is generated whenever it is not possible to evaluate an expression. There are many ways that these values can be generated, but we’ll start with a simple example:

var d = Date.now();

Obviously we don’t know what the time will be when the app is run, so the code processor simply returns unknown in this instance. Date is significant because it is the only part of the ECMAScript spec that generates an unknown value from all known values. There are other ways to generate unknown values though; most references to the Titanium API generate unknown values for instance. Once we have our unknown value, the next question is then “how does this affect everything else?”

The rules are really quite simple: any expression involving an unknown value produces an unknown value. For example, all of these expressions produce unknown values:

var d = Date.now();
d++;
d + 10;
d * 10;
/[a-z]/.test(d);
typeof d;
!!d;

The last two are significant in that the expected behavior does not apply. typeof is supposed to always return a string, but in this case it returns the value unknown. Similarly, !!d is often used to cast a value to a boolean, but in this case it also returns the value unknown.

This behavior makes unknown values “viral” in the sense that once something becomes unknown, it tends to make everything it touches unknown as well. This can have especially profound effects on conditionals, which I’ll talk about in my next post.

Version 0.3.0 of the Titanium Code Processor is out

Version 0.3.0 of the Titanium Code Processor has been released! The big new feature that warranted the minor version bump is a re-worked CLI with lots of new features

New Features

  • Reworked the CLI interface to be much more robust
    • The code processor now takes a sub-command, one of ‘options’, ‘plugins’, ‘analyze’, and ‘subprocess’
    • The ‘subprocess’ sub-command provides an interactive, structured mechanism for calling the code processor from other programs
  • Plugins can now take options
    • Options must be specified using a configuration file
  • Plugins can now reside anywhere
    • To specify plugins in another location, they must be specified in a configuration file
  • The ti-api-processor plugin now allows you to specify values for any primitive type in the API
    • This can be used to see how an app will react to, say, a specific screen size
  • Plugins are now checked to make sure their dependencies are also loaded
  • Added –all-plugins and –non-ti-plugins flags to the CLI to load all plugins or all non-Titanium-specific plugins, respectively
    • Now, if no plugins are specified, no plugins are loaded

Bug Fixes

  • Fixed a bug where the code processor crashes when encountering a reference to an undeclared variable that is named the same as an Object prototype property
  • Added a lot of configuration validation to prevent unexpected behavior/crashes when supplied with bad configuration data

The primary impetus with these new features was to add a new interface for studio to interact with. As part of this process, probably the biggest thing I added was support for reading options from a config file. This config file, detailed in the project README, allows for greater control over options than is possible from the command line. Another advantage to using a config file is that the “titanium” CLI command is no longer used, which shaves about 2 seconds off the startup time and allows people without the Titanium CLI installed to use the code processor.

Titanium Code Processor version 0.2.2 released

New Features

  • Added a new option “processUnvisitedCode”
  • Removed support for event tags because they were not being used

Bug Fixes

  • Fixed property references on Ti objects being ignored
  • Improved maximum recursion detection/handling to avoid maximum call stack exceeded exceptions
  • Lots of small bug fixes dealing with unknown values

The new “processUnvisistedCode” option tells the code processor to analyze any unvisited code after it has finished it’s normal run of code. This makes the code processor work like a hybrid of static and dynamic analysis. Fast runs of the code processor can be achieved by setting “processUnvisistedCode” to true and “invokeMethods” to false. At this point the code processor is a mostly static analyzer, so it analyzes quickly and has great coverage, but at the cost of accuracy.

Home Raspberry Pi Project

I just received a shiny, brand-new Raspberry Pi in the mail today! I’ve only been playing around with it for an hour or so but I already love this little device! It only took me about 30 minutes to go from unwrapping to running, which is very impressive for what is essentially an embedded development board. I remember back in college how it took me a week to get ucLinux up and running on a Freescale Coldfire 5271. My how times have changed. The power of this device is amazing. I’m even writing part of this post on it! Below is a pic of the new device up and running.

Raspberry Pi

Raspberry Pi Up and Running

I didn’t just buy the board to play with though; I have an idea for a project. My wife recently discovered an iPhone app that tracks sleep quality using the accelerometer in the phone. The app is pretty slick, but it suffers from a fatal weakness: it can only track the movements of a single person since there is only one sensor. To track the sleep quality of two people in the same bed requires an array of sensors to determine where movement originated, something a smartphone can’t do. Enter the Raspberry Pi. I’m going to use it as a controller for some custom sensor boards I’m going to make. It will be running a server on it that a custom iPhone and Windows Phone 8 app I’m going to write will connect to. This way, I still get the easy to access smart phone interface, but with the necessary sensor network. The end result of what I’m analyzing may not be all that interesting, but the tech behind it will be a lot of fun to play with.

Below is a draft block diagram of the system.
System Block Diagram
Most of the data processing will happen on the Raspberry Pi. It’s going to have the main signal processing algorithms, database storage, server interface for the apps, etc. The accelerometer boards are going to be custom made with the following components below. I could buy some pre-made accelerometer dev kits, but that’s no fun :) . The mobile apps are basically going to serve as a remote interface to start the system, stop it, and visualize data from the server. It will be a healthy mix of dev work I’ve done before and learning new things. Ironically enough, the embedded work will be the old, and the mobile apps will be the new, despite the fact that I work for a mobile company. Fun times ahead!