Wipe your DerivedData folder with this one simple trick

When you do any form of iOS development involving Xcode, chances are you’ve been in this situation where nothing works anymore and everybody tells you to wipe your DerivedData folder and try again. During the beginning of Swift this was happening a lot to me and the easier way to do that was to use a zsh alias available in “Oh-My-Zsh”. I’ve always wanted to play with Arduino. I had this idea in the back of my head when I discovered the LikeLight video on Vimeo. I recently had the opportunity to start learning about all this when I met Angelo, a great guy who happens to build robots for a living (or something like that). This two things really don’t have anything in common unless your decide to get a little creative.

Building the Arduino thingy

Schema of the Arduino board

The Arduino part is actually the easy one. I ordered a starter kit on DigiKey (which has he fastest delivery time I’ve ever seen: less than 24h). I needed an Arduino board (of course), a 500Ω resistance, a switch, a LED and a couple of wires. (Note that I say that I needed when it’s actually Angelo who guided me through the whole thing).

Arduino board

Once the “device” was assembled according to the schematic displayed above, we needed a small program to upload to the board. This program would declare a specific pin as an input (meaning you won’t be able to send data to it, trust me I tried) and listen for input on that pin. If it’s HIGH (5V), current is flowing thought this pin. If it’s LOW (0V) well, we don’t care about it.

Debugging with Arduino is pretty hard, you can’t add breakpoints on your board. We went for a little DEL that would light when the button was pressed, an easy enough way to make sure something was happening. We also used the Serial package to log content on the serial port and display it in the Arduino IDE Serial monitor.

void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT);
}

bool pressed = false;

void loop() {
  if (!pressed && digitalRead(8) == HIGH) {
    pressed = true;
    Serial.println("NUKE");
    delay(500);
  }

  if (digitalRead(8) == LOW) {
    pressed = false;
  }
}

The OSX application

You’ll notice in the code sample above that we are sending “NUKE” on the serial port because the Arduino IDE can listen on that port and display any message we want to print. It also means we can too! That was the idea behind the Mac application that you can find on Github here.

Listening to a specific serial port can be done using different methods. For example, Apple provide an IOKit framework capable of doing that. I may have considered giving up when I saw how much I had to learn to make this stuff work. Fortunately, there was a pod for that, providing a very simple API to listen to my Arduino board. I’ll let you dive into the code and go straight to the interesting part: the implementation of the serialPort(serialPort:didReceiveData:) method.

func serialPort(serialPort: ORSSerialPort, didReceiveData data: NSData) {
    if let payload = NSString(data: data, encoding: NSUTF8StringEncoding) where payload.rangeOfString("\n").location != NSNotFound {
        nuke()
    }
}

Results and conclusion

Instead of asking you to trust me I made a very quick video showing you the result. It shows the board, the DerivedData being emptied and an alert displayed in the notification center.

Nuking your DerivedData folder with Arduino from Palleas on Vimeo.

Overall this turned out to be a great pet project. I learned a lot about the basics of electronics and Arduino and some of the things I learned at school - like Ohm’s law - slowly started to make sense again. There is of course room for improvement:

  • Right now the whole thing isn’t standalone. I need to assemble it again to be able to play with it a little more. We’ve already started to discuss about how we could package it into something I wouldn’t be ashamed to put on my desk.
  • I hardcoded a lot of things, like the path to the serial port we care about. Also, we only react when the line break character is detected on this port. There is probably some kind of ping/pong exchange we could use to make sure we are talking to the proper serial port. We also considered Wifi or Bluetooth. We’ll see how it goes.
  • Unfortunately, SourceKit has became a lot more stable in the past releases so we don’t need to nuke the DerivedData folder quite as often as we used to. There is probably other folders we can nuke though.

I’m reallt looking forward to going back to it and making it better. Again, many thanks to Angelo for helping me!