Makefiles

 

A tutorial by example

 
Compiling your source code files can be tedious, specially when you want to include several source files and have to type the compiling command everytime you want to do it.
Well, I have news for you… Your days of command line compiling are (mostly) over, because YOU will learn how to write Makefiles.
Makefiles are special format files that together with the make utility will help you to automagically build and manage your projects.For this session you will need these files:

I recommend creating a new directory and placing all the files in there.

note: I use g++ for compiling. You are free to change it to a compiler of your choice

The make utility

If you run

make

this program will look for a file named makefile in your directory, and then execute it.

If you have several makefiles, then you can execute them with the command:

make -f MyMakefile

There are several other switches to the make utility. For more info, man make.

Build Process

  1. Compiler takes the source files and outputs object files
  2. Linker takes the object files and creates an executable

Compiling by hand

The trivial way to compile the files and obtain an executable, is by running the command:

g++ main.cpp hello.cpp factorial.cpp -o hello

The basic Makefile

The basic makefile is composed of:


target: dependencies
[tab] system command

This syntax applied to our example would look like:

all:
    g++ main.cpp hello.cpp factorial.cpp -o hello

[Download here]

To run this makefile on your files, type:

make -f Makefile-1

On this first example we see that our target is called all. This is the default target for makefiles. The make utility will execute this target if no other one is specified.
We also see that there are no dependencies for target all, so make safely executes the system commands specified.

Finally, make compiles the program according to the command line we gave it.

Using dependencies

Sometimes is useful to use different targets. This is because if you modify a single file in your project, you don’t have to recompile everything, only what you modified.
Here is an example:

all: hello

hello: main.o factorial.o hello.o
    g++ main.o factorial.o hello.o -o hello

main.o: main.cpp
    g++ -c main.cpp

factorial.o: factorial.cpp
    g++ -c factorial.cpp

hello.o: hello.cpp
    g++ -c hello.cpp

clean:
    rm *o hello

[Download here]

Now we see that the target all has only dependencies, but no system commands. In order for make to execute correctly, it has to meet all the dependencies of the called target (in this case all).

Each of the dependencies are searched through all the targets available and executed if found.

In this example we see a target called clean. It is useful to have such target if you want to have a fast way to get rid of all the object files and executables.

Using variables and comments

You can also use variables when writing Makefiles. It comes in handy in situations where you want to change the compiler, or the compiler options.

# I am a comment, and I want to say that the variable CC will be
# the compiler to use.
CC=g++
# Hey!, I am comment number 2. I want to say that CFLAGS will be the
# options I'll pass to the compiler.
CFLAGS=-c -Wall

all: hello

hello: main.o factorial.o hello.o
    $(CC) main.o factorial.o hello.o -o hello

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp

factorial.o: factorial.cpp
    $(CC) $(CFLAGS) factorial.cpp

hello.o: hello.cpp
    $(CC) $(CFLAGS) hello.cpp

clean:
    rm *o hello

[Download here]

As you can see, variables can be very useful sometimes. To use them, just assign a value to a variable before you start to write your targets. After that, you can just use them with the dereference operator $(VAR).

Where to go from here

With this brief introduction to Makefiles, you can create some very sophisticated mechanisms for compiling your projects. However, this is just the tip of the iceberg. I don’t expect anyone to fully understand the example presented below without having consulted some Make documentation (which I had to do myself).

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp hello.cpp factorial.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

all: $(SOURCES) $(EXECUTABLE)
    
$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@


[Download here]

If you understand this last example, you could adapt it to your own personal projects changing only 2 lines, no matter how many additional files you have !!!.

782 Words

Using an Atmel ATTiny2313 to blink a LED on the cheap.

After using Arduino for a while, I decided to give actual microcontrollers a try. Arduino makes it really easy for you to have everything on a single package, with a really simple and well documented API. But, if you want to save money or want to do things from scratch, a bare microcontroller is the way to go.

After doing some research, I chose to use the ATTiny2313, because it is inexpensive, it has an internal oscillator and has enough IO pins and features to do some cool stuff. But first, I went and implemented the “hello world” of microcontrollers on it: blink a LED.

Continue reading

Pen and Paper Crypto: A basic exploration of substitution ciphers

Cryptography is an essential part of today’s online information security. Without cryptography there would be no way to safely store your private data on remote servers (it is stored encrypted right?), to safely complete online transactions or to authenticate with online services (you make sure SSL is enabled on the websites you visit, correct?).

Cryptography and information security, however, is not new. It has served as a tool for secure information exchange throughout history, playing a very important role on events that have marked humanity. With time, it has also been rapidly evolving and as new cryptographic techniques are created, more sophisticated attacks are devised, becoming a never-ending game of catch-up.

The first known real cryptographic techniques were very basic, but are both fun to play with and provide a good foundation to understand modern crypto practices and their weaknesses. In this article I’ll be exploring some of these techniques, how they work and provide interactive “playgrounds” to experiment with.

Rot13

The first one is Rot13. This encryption method is more of an obfuscation technique, because it doesn’t provide real security since the key to encrypt is public. However it has been used effectively on online forums to hide messages that are not meant to be read right away. Movie spoilers or puzzle solutions are some good examples.

This algorithm works by assigning a number to each letter of the alphabet. For example, A=1, B=2, C=3 … Y=25, Z=26. Then for each letter on the message, we add 13 to it. If it goes over Z, then wrap around and start on A. This is visualized better on the playground below.

One of the most interesting properties of this encryption technique is that the encrypted text is all that’s necessary to get the original message. You can try this by copying the Cipher Text into the Plain Text field on the playground above.

Caesar Cipher

The Caesar Cipher is a variation on Rot13 in which the rotation by 13 is no longer fixed. This means that our key space and therefore the security of this encryption algorithm is increased 26-fold (yep, mindblown)

If we designate X to be the shifting value when encrypting, then the key to decrypt an encrypted message is (26 – X). You can try it below.

Vigenere Cipher

The Vigenere Cipher is like having multiple Caesar Ciphers chained together, each with a different shift value. The number available key combinations now multiplies by 26 for each instance of a caesar cipher. So, if you have a 3 character key (a character represents a shift value), our key space is 26 * 26 * 26 = 17576.

While the key space for the Vigenere cipher is much larger, all these encryption techniques have some flaws that make it easy to break. I’ll explore the basic technique for this attack in part 2 of this series.

The code for the encryption playgrounds is available here: https://github.com/urtubia/substitution-ciphers-playground


At Olark, we constantly spend time teaching and learning from each other. We have a weekly Show n’ Tell session, where we spend time learning from each others’ skills and interests. This post is an adaptation of one of our Show N’ Tell sessions I did.

 

SX-150 synth mod instructions, schematics and code

Many people have asked about the instructions and code for the SX-150 mod I did to add MIDI and Audio out, therefore I’m posting the plans for everyone to try. By using the Arduino as the brains of the operation, a lot of other options are available for the mod, really the sky is the limit. If you do any cool hacks to it, by all means, please let me know.

Parts List

Connections from the synth

Back of SX-150

The contact spots shown on the picture refer to the connections on the schematic. Just make sure to get these ones right and that the soldering joints you make are pretty solid.

Casing

IMG_2762

This part gives you a chance to be creative, so get your tools out and make something cool. For my mod, I used a cigar box, and made holes for all the cables that will get connected with the main circuit. I used epoxy to glue the gakken to the box and it is doing pretty well so far.

Just make sure that you will also be able to change the batteries when planning your holes.

Circuit Schematics

Schematics for the gakken sx-150 mod

Schematics for the gakken sx-150 mod


NOTE: It was brought to my attention that the connections P1 and P2 on the Gakken SX-150 are reversed on the schematic above.

Arduino Code

HakkenGakken.pde

Note that there is support on the code for a LED on digital pin 3 that flashes as notes are played. This has not been added on the schematics and it is optional.

If you improve the code, make some other cool mods or have questions, let me know!.

I’m now working on a standalone Arduino based MIDI sequencer on the cheap that can drive this little toy. Updates coming hopefully soon.

Gakken sx-150 arduino hack number two: Adding MIDI and Audio out

IMG_2764

I finally got around to finishing the hack to my Gakken SX-150 mini synth. On the first part I added the ability to be controlled by Arduino through a virtual software midi port. On this iteration, I added a real midi in port and I also added a 1/4′ plug audio out, to make it more usable with standard audio equipment. For more picture you can take a look at the flickr set.

IMG_2769 IMG_2758IMG_2761

The MIDI part consisted in combining the Arduino MIDI IN circuit and the Voltage Control Circuit described on the first part of this hack.
And, this is how it sounds!

Is your dream drum teacher on the other side of the country? No problem. Now, you can meet your teacher online on your smartphone or using a webcam on your computer.

No matter where you live, africa casinos chances are we can introduce you to an amazing drum teacher in your neighborhood, including children’s drum lessons. You can take lessons in the privacy of your own home or at your teacher’s location.

Controlling a Gakken SX-150 synth with Arduino

The Gakken SX-150 is a really inexpensive analog synth (about $50, now available at the make store). This synth is included on a Japanese magazine (I wouldn’t expect less from them) so that makes it 2*2 times as cool.

Since its release, someone already has created a module to add midi support to it using the atmel attiny2313, but I’ve wanted to use an Arduino board to do it, to keep things easy to prototype and hack.

My implementation uses a single DAC IC chip, the MCP4921, which sells for about 2 bucks from Mouser. The current version uses the Arduino, only to read one byte, as a midi note, from the serial connection and sends that to the SX-150. I could have created the full midi circuit to make it a standalone solution, but that increases the cost and complexity of the casinoluck.ca physical connections. I’ll probably do that on a later revision.

So, in order to receive midi data, I wrote a small Processing sketch that receives midi, and sends the note information through the serial port to the Arduino.

The Arduino connection diagram looks like this:
arduinosx150diagram

And the final connections, like this:
arduinosx150-detail

arduinosx150

And.. the whole thing sounds like this:

On the video, I’m using Ableton Live to send midi data to the processing sketch using a Network Midi connection on the Mac. On windows, you should be able to use Midi Yolke to accomplish the same thing. Then, I make sure that the processing sketch receives midi on that same virtual midi connection and sends the data through serial to the arduino. So, depending on your system configuration, you might have to tweak the Processing sketch a little bit to make sure it is reading and writing to the right midi and serial ports.

The flow of data from the sequencer to the synth looks like this:
Ableton Live => Virtual Midi Connection => Processing Sketch => Serial Port =≶ Arduino => DAC => SX-150

And finally, the source code:

Now, hook it up and make some noise!

The importance of having an open relationship with your IDE.

IDE’s: you either love them or you hate them, but there is no question about it: they do make a developer’s life easier. Developers that are really good with such a tool can seriously count on them being an extension of their body, an invisible limb, a partner. They physically hurt when the IDE crashes, they clean it, take care of it. But is this good or bad? Are developers leaving part of their brain on software? I think so. The worst part if they leave it there for too long they might not get it back.
Technology changes way too rapidly for programmers to develop such a strong relationship with a tool like this. Is this relationship beneficial? Absolutely, but you have to be ready to dump your software partner at a time when you least expect it. I know of stories where broken software hearts means having to change jobs.
The beauty of this relationship is that cheating on your IDE does actually benefit you as a developer. Besides, the IDE won’t get mad at you, even if it finds out. So my fellow developers, free yourselves. Play with other IDE’s, languages, techonologies and don’t be afraid. Times change rapidly, embrace change.