Oh frabjous day!

Made with Octopress!

How I Set Up My Pi

| Comments

I have recently reinstalled Raspbian on my Pi. The following is a record of the steps I followed to get everything how I like it. (I’m doing this mainly as a reminder for my future self, but you might find it interesting.)

  1. Install Raspbian using the NOOBS Setup Guide.
  2. Install Shairport using these instructions. (You have to scroll down a long way to get to the good bit! Also note that this site uses an abandoned fork, rather than the original repo. The fork still works, but you might consider using the original.)
  3. Install nginx using Ars Technica’s Web Served tutorial series.
  4. Set up the permissions for the website directory using a combination of the top answers from here and here.
  5. Clone personal website (this website) from Bitbucket into the directory prepared above.
  6. Harden server using these instructions.
  7. Install BTSync using these instructions.

New YouTube Series

| Comments

I have recently started a YouTube series comparing how easy it is to do various tasks on Windows, Mac and Linux. Because this involves screen recording, I have started the series by comparing screen recording on each OS.

The results are embedded below:

Mac

Windows

Ubuntu Linux

Also, here are some torrent links, if you are into that sort of thing. You can use them under the Creative Commons Non-Commercial license

These are magnet links, which will work if I am currently seeding.

If those don’t work, you can download torrent files here: (I am using web seeds, so these will work even if there is no-one in the world seeding.)

TTY Launcher for pbLua

| Comments

I’ve been experimenting recently with pbLua. PbLua is custom firmware for Lego Mindstorms NXT robots which allows you to program them with Lua, rather than the incredibly annoying graphical program they ship with the kits.

However, pbLua is not the most user friendly of firmwares. For instance, once you’ve flashed your NXT brick, you connect to it by running the following command:

screen /dev/tty.usbmodem* 38400.

This gives you a Lua prompt on your computer which is executed by the NXT. Essentially, you are turning your computer into an old fashioned dumb terminal, and the NXT brick is the mainframe… (Ironic right?)

Also, you can’t run the above command if it is already running, because the NXT won’t let you connect multiple sessions simultaneously.

This all seemed too confusing to explain to my 8 year old sister, so I decided to wrap it all up in a nice script. It’s not quite foolproof yet, but it’s a lot easier than doing everything manually.

You can get it from Bitbucket. Feel free to send me pull requests if you think of any improvements.

It’s very easy to use. Just download it and run

./nxt

You can stick it in your $PATH if you like, but it’s not necessary.

Tty Launch for Pblua

| Comments

I have recently created a site proposal on StackExchange. It is called “Code Recommendations”. It’s a site for asking questions like “What is the best library for face recognition?”. These questions are usually closed on Stack Overflow as off topic.

Help make it a reality by going and asking questions here!

Code Recommendations

Broken Disqus Comments

| Comments

I have had this Octopress blog for some time now, but I’ve never been able to get Disqus comments working. Up until now, that is.

The Octopress instructions suggest that Disqus comments can be easily added by simply creating a Disqus account, and adding the relevant details. However, after adding all my details, all I got was an exciting message which said

We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

This problem persisted, despite multiple attempts to fix the problem. I went through all the Disqus settings I could find to no avail. I tried removing the trailing slash from my site name in the Disqus settings. Still nothing. Finally, I tried isolating the problem by installing Disqus on a non-Octopress page. Suddenly, everything worked. Now I had something to work with. I found the Octopress Disqus code inside source/_includes/disqus.html, and after some experimentation removed (commented) the following lines:

disqus.html
1
2
var disqus_identifier = 'daviewales.tk/blog/2014/03/04/broken-disqus-comments/';
var disqus_url = 'daviewales.tk/blog/2014/03/04/broken-disqus-comments/';

After running rake generate, suddenly Disqus comments started working. I will leave a comment at the end of this post, just to prove it.

I have no idea why this works, or why it didn’t work before…

Quick Python Tutorial for C People

| Comments

Python for users of C-like languages:

I’m going to assume you are familiar with at least one C-like language, and describe the differences and similarities between Python and C-like languages. Note that when I say ‘C’ in the examples below, I probably mean C++, or C-like. I know that C doesn’t contain bool or cout. =P

  • Python is dynamically typed. Instead of specifying the type of a variable in the code, it is determined dynamically by the Python interpreter when the script is run. The following example shows how a variable would be created to store the integer ‘7’ in C and Python respectively:
1
2
// C
int my_cool_variable = 7;
1
2
# Python
my_cool_variable = 7
  • Python uses line breaks to specify the end of a line. The following examples show how multiple lines of code would be written in C and Python respectively:
1
2
3
// C
int first = 1;
int second = 2;
1
2
3
# Python
first = 1
second = 2
  • Python functions are a little different. They do not specify type, because Python is dynamically typed. Also, a Python function has a colon (:) after the parentheses, and the rest of the function is indented. The following examples show how functions are defined in C and Python respectively:
1
2
3
4
5
6
7
8
9
10
11
12
13
// C
int function1()
{
    int a = 1 + 2;
    return a - 1;
}

bool function2()
{
    int a = 3;
    int b = 2;
    return a == b;
}
1
2
3
4
5
6
7
8
9
# Python
def function1():
    a = 1 + 2
    return a - 1

def function2():
    a = 3
    b = 2
    return a == b
  • Python uses indentation to specify loop boundaries and nesting. Rather than specifying the start and end of a loop or function with curly braces, Python uses a colon, followed by indentation.
1
2
3
4
5
6
7
8
9
10
11
// C-like
int a = 0;
while (a < 7)
{
    cout << a;
    if (a == 3)
    {
        cout << "That's my favourite number.";
    }
    a++
}
1
2
3
4
5
6
7
# Python
a = 0
while a < 7:
    print(a)
    if a == 3:
        print("That's my favourite number.")
    a += 1
  • Python uses ‘#’ for one line comments, and lots of #’s for multi-line comments. (Some people will try to tell you that Python uses “””triple-quoted strings””” as multi-line comments. You can do this if you like, but it is not technically correct. “””Triple-quoted strings””” will be instantly garbage collected on run, whereas #comments will be completely ignored, so #comments are generally better. One legitimate use for “””triple-quoted strings””” is docstrings. Docstrings are strings directly after the start of a module, or a function or a class, and are used for interactively accessible documentation among other things.
1
2
3
4
5
6
7
// One line C comment.
/*
  Multi-
  line
  C 
  comment.
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# One line Python comment

#  Multi-
#  line
#  Python
#  comment.

"""
Something that acts like a multi-line
comment, but is usually frowned upon.
"""

def new_function():
    """Docstring that says what this
    function does"""

# Accessing a docstring from within a program
docstring = new_function.__doc__
  • Google is your friend. Have fun.

Python, PyObjC, ScriptingBridge and AppleScript

| Comments

I’ve recently been investigating the best way to control Keynote from another application. I know it’s possible with AppleScript, but AppleScript confuses me, so I thought I would try to avoid it.

After looking around, I finally found ScriptingBridge, which is a framework developed by Apple. It can be accessed from ObjC, Ruby and Python. However, I found there was very little documentation explaining how to use it. In this post, I plan to quickly show the key points, which I have found out through Google, and trial and error.

In Python, (assuming you have PyObjC installed) you can get up and running like this:
Note: I am using Keynote 6.1.
Also note: Keynote.startSlideshow() will not work with previous versions of Keynote. (You can find out what functions they support as described below, using the help() function.)

Talking to Keynote
1
2
3
4
5
6
7
#!/usr/bin/env python3

from Foundation import *
from ScriptingBridge import *
Keynote = SBApplication.applicationWithBundleIdentifier_("com.apple.iWork.Keynote")
if Keynote.isRunning():
    Keynote.startSlideshow()

To find out what functions you have access to, you can use Python’s built in help() function. Supposing you have typed the above code into an interactive prompt, you can use help(Keynote) obtain a list of all available functions.

One of the trickiest things for me was working out the Bundle identifier. I assumed it would be com.apple.Keynote, but it wasn’t. To find it, try looking at the names of the files in /Library/Preferences or ~/Library/Preferences. For instance, I found that Keynote was com.apple.iWork.Keynote, and that iTunes was com.apple.iTunes.

Git Forks

| Comments

I was recently trying to contribute to a Homebrew formula, and I realised I knew absolutely nothing about working with other people’s git repos. Fortunately, I found this blog post, which basically covered all the things I needed to know. I’m putting this post here, so I can find it next time I forget how to do it, but it might be useful for you too…

Mumbling at Mavericks Dictation

| Comments

The new Mac operating system has pretty good dictation. I used it to write this post. However, when I was first learning to use it, it said some pretty crazy things. Here is a sample:

Would be really really interesting to find out why our pages has watching issues with dictation change word watching to twitching you really can’t say glitching Tony you just don’t like the way I are speaking mumble that’s really missing I said are because I need to say and I’m on our work anyway that wrote our St ARA medication thing I think I’ve really need someone to speak more clearly without saying are and am if I want start dictating stuff stop period. Dictating is really different to typing. Enter return carriage return

End of line graph delete word delete backspace I’m do back delete delete delete you know this dictation actually works really well and not in pages moderately well anyway. You have to remember to say. Effectively if you say you have to remember to say. They’re just right. I wonder if I can type the work full stop can you put emphasis between the words fall and stop. you just won’t say it doesn’t delete words and another one and it’ll B is it yesterday dictation works a lot better if you’ve enough of the memories up an updated sister does not mind and a line graph delete won’t delete backspace idea back delete legally in this dictation actually works really well not and not in a dress well it morally well anyway have to remember to say.effectively if you say we have to remember to say this is just right order if I can type the word. Can you exit print words: stop just won’t say it doesn’t delete words and another one it’ll be it yesterday dictation works a lot better if you’ve another memories and updated sister you mind late again went isolated again with isolated yes I’m really appreciate accurate this thing is I think you need to get my speech and annunciation lessons are things I need to get speech annunciation lessons to make my speech precisely easy to disown are you gonna was not”Mood got rich are not wrote a lot going for a 5 mile marker over driver brought on by while we didn’t write why are are all awful and I left it was delightfully hello Scott Morrison…
New Testament running late testament running late

You Stephanie Meyer I’m I’m I’m RRR lovely darling I’m gonna love you:-:
Hello Carlisle one) at book about that this might not gonna alumni are still on the laptop or another partner would’ve wanted vampires are vampires them a lot
I always à la cycles sparkles styles sparkles à la glass bottles bottles bottles and another follower of the fighters are you still at the fifth of the father will immediately do believe that is David reading about the guy was in our money multiple A row of David on your holiday went well more Normans only really know what I will be funny to know what I sing a song of sixpence is a long time about you can’t add you as a matter of environmental listening and Maximo
NZ meeting with the warehouse tonight with the handouts for tomorrow night was a fairly to the harbour appear was your mom about you the most promising
Lala Lala Lala are you lessons from the Internet so shall did you know that this actually works moderately well if you annunciate your words correctly.

I would appreciate if you could accurately and precisely listen and decide what I am saying so close I would appreciate if you could accurately precisely listen and decide what I am saying this are SRM SRM hello are you ridiculous although you do you write your ridiculous yes I’m ridiculously messy you’re at Scott’s panel up the real activist script I will I will I will do: I am writing you down hi

Mavericks Dictation

Finding Parts Per Million (Ppm)

| Comments

Everyone gets very confused when they try to learn how to convert from g/l or mg / l, or mg / g, or something similar, to ppm. However, the process is almost identical to finding a percentage. To find a percentage (parts per hundred), you take the part, and the total and do the following:

percentage of part in total %

To find ppm (parts per million), you take the part, and the total and do the following:

parts per million ppm

Notice any similarities there?

For example, suppose after a fire we have 1 g of carbon in every litre of water in some pond. (I don’t know if this is realistic. Don’t criticise me… =P) We first convert everything to the same units. 1 litre of water is approximately 1000 g. Now, we can find the percentage of carbon in the water:

percentage % %

And the ppm of carbon in the water:

ppm ppm

Simple right?

comments powered by Disqus