Posts

Showing posts from 2009

Vim: Replace tabs with spaces

When working in other peoples code I almost always notice mixed tabs and spaces. Like many programmers, this makes my skin crawl since it usually causes formatting to look horrible or incomprehensible. First I'll double check that there are mixed tabs and spaces by highlighting the tabs. /\t If I see some whitespace highlighted and some not highlighted I'll look at the indentation and figure out how many spaces each tab should be, then do a replace. This will perform a replace on the entire file ( %s ) for all tabs ( \t ) into four spaces ( \ \ \ \ ), the g at the end will allow the match to be done multiple times per line: :%s/\t/\ \ \ \ /g If your tab expand matches the file, you can also use the vim built-in retab: :%retab

Taking Stuff Apart: Data-Doc Data Switch

Image
Not much to this one. The case is a nice size for a project box, which is why it was picked up in the first place. The mess of wires inside is impressive. As usual click the pictures for more (and in higher resolution).

strcpy or fun with pointers, a while loop and cstrings

ps1 and ps2 are pointers to a character array, what does this do? while (*ps2++=*ps1++); It is my favorite example of bizarre looking C code which utilizes pointers, pointer increments, unary operation precedence, C-string null termination and the evaluation of an assignment. Breaking it down a little "*ps2++=*ps1++" means the same thing as this: *ps2 = *ps1; ps2++; ps1++; Since ps1 and ps2 are character pointers the first line dereferences each and sets the value of ps1 to the memory pointed to by ps2. The second and third lines just increment each pointer. Both are pointing to character arrays, so they now point to the next character in the array. The final important detail is that a cstring ends with a null terminator. So eventually the loop will get to the strings end and assign "'\0' = '\0'", which evaluates to '\0', which is false. Thus ending the loop. Here's a full example: Here it is in the form of strcpy (from

Linux Automated Backup

I've been wanting to setup some sort of automatic backup on my personal data for quite some time, but have always settled on manually copying important documents to a second hard drive. Well thanks to THIS nice guide to setting up rsync and cron I now have it setup. Short guide shorter: 1) Create a bash script with the following (note there are only two lines below, the command is long): #!/bin/bash sudo rsync -av --progress --delete --log-file=<location to put log file>/$(date +%Y%m%d)_rsync.log <directory to backup> <directory to put backed up directory in> 2) Run "sudo crontab -e" and enter the following line to have your backup done every morning at 4am: 0 4 * * * /root/rsync-shell.sh

Showing Gnome Workspace Names

Image
One of my favourite things in Linux is having multiple workspaces. Something I didn't know until recently is that you could name your workspaces, and with a simple trick have the name displayed on your top or bottom panel.

New Project Smell

Image
Took the first steps on a new project, an automatic cat feeder. The plan is to have it completed by Thanksgiving so that the cats have something to eat while I'm out of town. That being said, I cut the pieces for the box out of some 1/4" MDF and have a lot of work to do.

Taking Stuff Apart: 8mm Projector

Image
More treasures from the landfill, this is something I took apart a year or two ago. The only reason its still kicking around is because of the motor, which will be re-purposed in another project someday. There's nothing left to identify what model it is, anyone know? There isn't much left for the front. Everything interesting has been ripped out including an old bulb, lens and a switch. The bulb's reflector was actually used in another project. In the top corners are a couple spokes which were probably used to attach the reels, film would go through the slot in the bottom right where the bulb and lens used to be.

OpenGL in SDL on Ubuntu

Supposedly SDL is better supported than GLUT for cross-platform OpenGL. There's even a Nintendo DS and Wii port for it! SDL is right in the Ubuntu repository, so it is just as easy to install as GLUT was: sudo aptitude install libsdl-dev To test it out, this time I went to NeHe Lesson 2 . There is a port for Linux/SDL . The same GL header files are needed, but this time SDL is needed as well: #include <GL/gl.h> #include <GL/glu.h> #include "SDL.h" It looks like there are a couple more flags needed, the part at the end is SDL, I'm not sure why it looks that way. gcc lesson02.c -lGL -lGLU `sdl-config --cflags --libs`

GLUT on Ubuntu: Basics

To install GLUT: sudo aptitude install freeglut3 freeglut3-dev This will install the OpenGL header files to /usr/include/GL/ And the library objects to /usr/lib/libglut.a With all that taken care of now you just need a quick sample app to see if it worked, I googled "glut hello world" and this was the second result . I needed to add "#include ". Also notice that GLUT is included like "#include ". I then renamed it to helloworld.cpp and was able to compile as follows: g++ helloworld.cpp -lglut That's it, now you're ready to figure out something cool to make.

Crappy Volume Control

Image
My dad was kind enough to provide me with a nice db systems amplifier, but the thing didn't have any volume control. I found my source (a PS3) to be lacking in range settings so I needed to come up with some sort of pre-amp. What you see below is the result. Nothing more than a dual potentiometer stuck inside an MDF box, well almost a box, I never built the top or rear of the thing.

DVD Rip for PS3 through Mediatomb

Image
For episodic content I like to rip my DVD's so that I can play them off my mediacenter (MediaTomb enabled PS3) one after another without worrying about disks. For Linux I use an application called "DVD::Rip" which makes this process fairly simple:

Job control on UNIX systems

I've been using control-z and fg to stop / resume programs from the UNIX terminal for as long as I've been using that environment. The problem is that I've only used those two commands so my usual workflow has always been "stack based". Meaning I have always used fg to resume the most recent program that I stopped. Long story short, there is another command called jobs which enumerates all the programs which have been stopped. At this point you can use fg # to resume whichever job you want. What a concept. I found this web page had a nice description of all these commands: http://acs.ucsd.edu/info/jobctrl.shtml

Sim Ant

Image
I found a copy of Sim Ant at the local landfill today. Complete with the original box, 4x 5.25" floppy disks, 2x 3.5" floppy disks and the manual, with a whopping 178 pages. One of the requirements on the box actually says "Requires hard drive". Click the photo for some more.

Taking Stuff Apart: Realistic Modulette-8

Image
When I saw this fine piece of electronics at the landfill I knew it was special. Not only did it have a plethora of nice looking knobs and various shiny areas, but it also had a wooden enclosure. Although stamped steel cases are nice, its a little impersonal, not like this finely crafted device. I had already thrown it in the trunk before I even noticed - the thing was an 8-track player. Click below for more pictures.

MediaTomb + PS3

I like to stream audio and video content from my Ubuntu box to my tv and I have a PS3. The PS3 has Universal Plug and Play support. I tried a handful of UPNP servers including Vuze, GeexBox, uShare and MediaTomb. I stopped at MediaTomb because it worked for all my needs and had a snazzy web ui for the server. There is a problem however. MediaTomb's development version has better PS3 support and some extra features which are nice (video thumbnails, dvd iso navigation, watched markers, probably others). Building MediaTomb from source was a huge hassle the first time I did it due to ffmpeg and its various dependencies. When I built it yesterday however I found THIS guide. Which was almost perfect, I needed to modify the "nut" Makefile to compile with -fPIC, and ffmpeg has moved from libamr to libopencore-amr, so I needed to install the opencore libraries from source as well. Other than that its a GREAT guide and now I have a MediaTomb 0.12.0 build.

Ants

I haven't had a lot of time to work on my Ant application thanks to a new job, but I have high hopes that I will start working on it again soon. I've been running the last iteration of the program a couple times a day, cursing the ants for not following the trails the way I expect them to. To get back up to speed with DS / Devkitpro development I started working on a simple Logging program that I've wanted to make for a while. Essentially it will take down point of sale information for things like Gas Station, Restaurant and Shopping, then allow display / calculations based on the information. In other news, HERE (youtube video) is a Nintendo DS project my brother has started in the past couple months. He has a rom file available here: ROM FILE

Vim Window Size

I have a couple entries in my vimrc configuration that I need: map - <C-W>- map = <C-W>+ When you have multiple windows open you can change their size with "Control-w" and '+' or '-' (note that you need to be holding SHIFT to get a '+' sign). This will change the window size one line at a time. If your extremely suave and can count line numbers in your head you can prepend the number of lines you would like to move and the keyboard shortcut isn't all that bad. I can't count the lines in my head, so I use the above macro. Since I work with multiple windows constantly I have the increase and decrease size bound to '-' and '=', I use equal so that I don't have to press SHIFT for one but not the other.

goto in PHP 5.3

It appears that the goto statement is being added to PHP 5.3. Considering its bad reputation the move seems like a strange feature to be adding. Especially since one of the main benefit of a goto, exception handling, appears to have been introduced in PHP 5. Hopefully all the web programmers out there will tread cautiously with their new power.

Taking Stuff Apart: Nikko NA-550

Image
From time to time I come across an old piece of electronics that I want to harvest some parts from. Stepping motors from floppy disk drives, Solenoids from zip drives, small DC motors from CD drives to name a few of my favorites. Audio Amplifiers in particular are my very favorit. They have lots of interesting and hard to find pieces. Without further adieu, here's a before shot of this vintage 80's "Nikko NA-550 Integrated Amplifier" that I picked up at a landfill a few weeks ago. For anyone curious, the power button was jammed in the off position, which is probably what earned it a spot at the dump. Got a bunch of nice metal knobs, some dB meters, some very nice Alps selector switches, some Alps potentiometers, a 2.5A breaker and some other misc parts.

Ant AI

I've been toiling away trying to come up with an algorithm which will make ants move in a believable manner while only using a very limited view (the 4 adjacent tiles which they can move to). This was much more difficult than I was expecting, it sounds too easy to say "the ants wander around randomly, then once they found food they could head home following the chemicals they left behind". Implementing it like that not only doesn't work, but the ants end up congregating around the ant hill and don't accomplish much of anything. The final algorithm is a set of rules that look like this: 1. If carrying food, call go home AI. 2. If carrying food and in base, drop food (or wander until there is a spot to drop the food), set chemical output to low. 3. If not on surface, wander until on surface. 4. If on surface, check for food, pickup, change chemical output to high. 5. If on surface and no food, try to follow a chemical trail. 6. If no trail to follow search f

First Demo

My DS application has come a long way in the past few weeks, I think it is finally time to present the fruits of my labor. I made a video to show off the player movement, world movement, camera scrolling and some of the simple AI / world effects I have going. This is only the beginning though! Everything is now in place for me to build more cool things. There are a couple more things I would like to show off in the near term which I'll post here and on the YouTube channel so keep an eye out! If you would like the demo shoot me an e-mail. Its not at the point where I'd like to publicly distribute it however.

Video Conversion - mencoder + ffmpeg

This is just a quick post. I am working on creating a video for YouTube to show off the current state of my project (see my prior post). Several challenges came up during this process, this is how I handled them: 1) I needed a desktop recording program to record the emulator. This was easy, I installed a program called "gtk-recordmydesktop" and was able to figure everything out with no instructions. On Linux (Ubuntu) this is in the package manager so it was as easy as " sudo apt-get install gtk-recordmydesktop " 2) The resulting video is a non-standard resolution and format, in this case my video file is out.ogv with 272 x 432 resolution. My video editor wants a dv file with 4:3 aspect ratio and YouTube wants 640 x 480 resolution. Getting this to the necessary aspect ratio / resultion / file type required re-encoding / padding / scaling with mencoder, and a transcode with ffmpeg. I found this command to do a straight conversion: mencoder out.ogv -ovc xv

Side Tracked

I have a new addiction which has put all my other projects on hold. Nintendo DS Homebrew . A couple years ago I took a look at the homebrew development tools and libraries available for the DS and Wii, but didn't get much further than building and tweaking the example programs. Recently I took another look at the Nintendo DS homebrew devkit, devkitPRO . After a few hours I had myself a nice PONG clone, complete with touchpad control. Well, it was so easy to put together that I started working on something bigger. That was all two weeks ago, and my bigger project is coming along nicely (5000 lines strong). At the core my "game" is an ant simulator, complete with randomly generated underground tunnels, a surface world with food spawns and multiple possible underground <-> surface links and a queen that lays eggs and soon breeders to move the eggs away from the queen, soldiers to fight spiders, spiders, etc. I'm nearing the point where I can stop with the a

Automated Checksums

I'm creating some checksums today for a folder full of large files. This is the method, which I found on the ubuntu forums. Essentially md5sum is called on each file with 'find -exec' : find -type f -exec md5sum "{}" \; > md5sum.txt ' find ' returns the contents of the directory, ' -type f ' ensures only regular files are returned, I'm not sure what the ' "{}" \;' part is for, finally I added '> md5sum.txt' to send the output to a file. This call can take a very long time depending on the size of files it is being called on, so I checked up on it by counting the lines in md5sum.txt with the following command: cat md5sum.txt | nl | tail -1 | cut -f 1 What this does is list the entire md5sum.txt file, count each line with 'nl' , output only the last line (aka first from the end) with 'tail - 1' then return only the first field of that line with 'cut -f 1' Finally