Using CoffeeScript To Write Altium Scripts

Altium is a powerful program which can be scripted. There is a built in scripting engine and API which is mostly exposed using an Object Model. You can read more about Altium Scripting here, but it turns out the main languages you can use are DelphiScript, VisualBasic and Javascript (called JScript in the Altium documentation). This version of Javascript implements what appears to be an ECMAScript 5 compliant version of javascript, and as a result you can apply javascript design techniques to your Altium Scripts.

I have not seen many articles which reference this, and I find it interesting, because it means you can use Javascript transpilers to write your scripts in languages such as CoffeeScript. These languages compile to valid Javascript, and can result in more maintainable scripts (assuming you can maintain the compiler version).

The Tools

For this project I used a pretty simple set of tools. However, the versions of the tools may be useful to know, as they may dictate how effective this method is.

  • Altium Designer 2013
  • CoffeeScript, version 1.9.0

Testing It Out

To test this out I decided to write a simple script that would show a message in Altium when run. This script uses the ShowMessage(str) function, which is the Altium version of alert(str). Below is a script which defines a javascript object, a function to format that object and then shows the formatted alert. It also includes a list comprehension, a feature not natively present in javascript, to count from 1 to 6 and show a message for each count.

Which compiles to the following after running following command:

.

Believe it or not, this is a valid Altium script!

Why?

To be honest I am not quite sure why you would do this; however, it does open the door of doing more complex Altium script development for self contained libraries, since CoffeeScript can actually join multiple ".coffee" files into one at compile time. You also have the power of an expressive language at your fingertips. However, the largest value of this experiment is that it show that you can recycle work done by the general javascript community to writing Altium scripts. I will be inspecting other such opportunities as time goes on.

Secret Santa List Generator And Mailer

The Problem

This Christmas season I found myself running a secret santa, completely over the internet. I want to keep everyones secret santa information secret, without even me knowing it. I also wanted to automate the emailing process, which was required to keep the information for who had who, away from me. A log file also needed to be produced (and promised not to be looked at... or encrypted), to help fix disputes. So I ended up with the following requirements:

  • Takes a list of participants and generates secret santa pairs.
  • Create a message from a template
  • Send the email
  • Create a log file

The Solution

I am not going to lie, this piece of kit was not particularly difficult to write, but I believe is a good argument for why everyone should know how to write scripts; and why public API's and libraries are awesome.

To write the script I chose Python, which has a built in JSON parser library, and support for SendGrid's API. I chose SendGrid to send the emails because it required the least setup, I just needed to set up an account and I could send emails. Their API was simple and all of the code I used for interacting with it was ripped directly out of their examples. Quick and easy, just what I wanted.

I then came up with a quick JSON structure for containing all of the information I needed, an example of which can be found below:

The python script parses the JSON file, shuffles the participants, links them together into partners and then sends each of the participants an email populating the template sotred in "message". I will admit this script has a lot of short comings, but it successfully helped me send out my secret santa emails and everyone is happy!

You can access the code on Github.

Building A Basic Dynamo

Overview

This is just a basic little dynamo which uses a tiny 6V DC motor I scavenged from an old CD drive. I have grander plans for the motor in the future, but I thought it would be cool to use it to turn on an LED with it!

The basic idea is pretty straight forward. If you take a motor, which has a permanent magnet in it, and turn the magnet on your own you will change the magnetic flux in the coil and as such start generating some current and, you will start to get a potential difference building up across the terminals of the motor. If I knew the RPM rating of the motor then I could figure out how fast to turn it to get a full 6 Volts. At the moment all I know is that I need to spin it pretty fast and the peak value my multimeter reads is 2.5 V, which is enough to power an LED.

Continue reading Building A Basic Dynamo