Easy mbed Programming from Linux

If you use mbed you know it is cool not to have to install compilers or build libraries and toolchains. You just enter some code, grab some up to date libraries and press compile. Everything runs in your browser and you download a binary file with your program in it. However, you still have to get it to the board. Most of the Arm boards look like a USB drive and you can simply copy or move the file over to do a program. However, I get tired of manually doing that step. This is especially a problem if you use a GUI. You have to remember to move and not copy (so your browser doesn’t keep renaming the file) and you also need to keep confirming the overwrite on the phantom file still on the “fake” USB drive.
So instead, I write a little command line “script” to do it all for me. You could use inotify, but this is easy and I just do it when I’m developing and stop it when I’m done. Suppose your object file is XXX.bin and you are in the /tmp/mbed directory.

Open a shell and go to the /tmp/mbed directory. In my case, the fake USB drive is at /media/USER/FRDM-KL25Z. Then enter this:

while true
 do if [ - f XXX.bin ]
 then echo Programming
 sleep 1
 mv -f XXX.bin /media/USER/FRDM-KL25Z 
 echo Done
 fi
 sleep 2
done

Now when you save your binary file to /tmp/mbed, it will automatically program the device. Simple!

Quick Qt5 Example for GP3

I noticed Qt5 has a QtSerialPort class. Here’s a simple example of talking to the GP3  using this library:

 

    static int state=0;
    QSerialPort port("ttyUSB8",this);  // works with /dev/ttyUSB8 or ttyUSB8
    qDebug()<<port.setBaudRate(QSerialPort::Baud57600);
    qDebug()<<port.open(QIODevice::ReadWrite);
    qDebug()<<port.write(state?"\x0C":"\x0D",1);  // Turns on/off light
    state^=1;
    port.waitForBytesWritten(1000);
    port.close();

Pretty simple. You can read data back either blocking or using slots. I’ll cook up an example of that later.

Pithos (Pandora) Quicklist for Unity or Icon-Only Task Manager

I use KDE but recently installed the excellent Icon-Only Task Manager (http://kde-look.org/content/show.php?content=144808) that lets you set up a Unity-like desktop. One cool thing is that some programs have QuickLists. So if you right click on a Chrome icon, for example, it gives you options to open a new window or tab, etc.

Making a new Quicklist is pretty easy and I use Pithos (http://kevinmehall.net/p/pithos/) quite a bit to listen to Pandora. So here’s my Quicklist for Pithos. Save it as pithos.desktop and put it in /usr/share/applications/pithos.desktop.

[Desktop Entry]
Name=Pithos
Comment=Play music from Pandora Radio
Categories=GNOME;AudioVideo;Player;
Exec=pithos
Icon=pithos
Terminal=false
Type=Application
Actions=Like;Ban;Skip;Pause-Play;

[Desktop Action Like]
Name=Like Song
Exec=qdbus org.mpris.MediaPlayer2.pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.LoveCurrentSong

[Desktop Action Ban]
Name=Ban Song
Exec=qdbus org.mpris.MediaPlayer2.pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.BanCurrentSong

[Desktop Action Skip]
Name=Skip Song
Exec=qdbus org.mpris.MediaPlayer2.pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.SkipSong

[Desktop Action Pause-Play]
Name=Play/Pause
Exec=qdbus org.mpris.MediaPlayer2.pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.PlayPause

Byobu (Advanced GNU Screen) under Centos 5/Python 2.4

I recently discovered Byobu, which is a set of scripts that lets you use GNU Screen more easily. If you spend a lot of time ssh’d into a server, you should definitely check it out. It worked fine on my desktop machine, but on my Centos 5 server, not so much. I did a little research and I found there were two problems.

The easy problem was keeping F2-F4 from working. The trick is that /etc/screenrc has the following line in it:

termcapinfo xterm 'k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~'

Commenting out the line make the function keys work! That was easy.

The 2nd problem was that F9 was not bringing up the menu. Well, most of the time. Sometimes you could get a peek of a Python traceback and sometimes — not often — it would work. The real puzzle was that if you started byobu-config from the command line it worked fine.

It turns out Centos has Python 2.4 which is pretty old. I intercepted stderr from the script and found out that a command that was reading from a pipe was returning “System Call Interrupted.” This isn’t really an error, but this version of Python apparently thinks it is. So sometimes, that signal wouldn’t interrupt the pipe and it would work.

I tried installing Python 2.6 but there are a lot of dependencies in Centos on Python and although I found someone with a “parallel” RPM it didn’t have the necessary libraries and I finally gave up. But then it hit me. If I could just pause a bit before the offending pipe, the signal would come in — not interrupt the pipe — and then all would be well. It seems to work!

Here’s the fix:

if os.path.exists("%s/.%s/disable-autolaunch" % (HOME, PKG)):
return 0
time.sleep(3)

The script already imports time, so that wasn’t a problem. Problem solved. I like Byobu!

Speeding up Ubuntu Linux with SSDD

A lot of people are switching their Windows machines to boot of solid state disk drives (SSDD) to get faster boot times. The price of these drives has been dropping, but since I use Linux (Kuuntu, actually, which is Ubuntu with KDE) it didn’t make much sense to me. I maybe reboot my machine once a month. What do I care if my reboot time is 30 seconds faster?

Fry’s had a 64GB SSDD on sale this week. After rebate it was around $85 after rebate (you can get roughly the same deal at Newegg). They had a 32GB drive (which had slower specs, too) for even less. So I couldn’t resist. The drive was for a laptop, but the SATA connectors fit. I bought a $4 pair of brackets to make it fit, but in the end since the thing doesn’t spin, I just let it sit on top of a drive tray — I’ll probably secure it later, but for now it is just sitting on the inside. I formatted the whole device with btrfs (no partitions at all) and sure enough I had about 59GB of useful space in a new volume.

I edited fstab to have the line:

UUID=f9f6588e-dc7f-4675-b132-8b008486d243      /ssdd1    btrfs  defaults,ssd,noatime,discard

Of course, your UUID will vary. I had to create /ssdd1 as root and then I did a mount -a to put the drive in place. Now… how to make things go faster?

My basic idea was to copy stuff that KDE, X Windows, etc. uses a lot to the SSDD. I didn’t care so much about user files and I didn’t want the constant writing of /tmp or things from /var (in general) since SSDDs have some limit on write endurance (although modern drives do pretty well if you look into that). And I didn’t want to get messed up with things in /boot or things that I would need if the drive ever failed to mount (/bin, /sbin, and /lib, for example).

What I did was make exact copies of /usr/share, /usr/lib, /usr/bin and put them on /ssdd1/usr/share, and so forth. You have to make exact copies since some things in /usr/bin are suid — that is, they run as their owner and a normal cp won’t do the job (not without a lot of options; look at -p, for example). I used cpio:

find . -print | cpio -admp /ssdd1/usr/bin

Obviously, you need to be root to do that and you need to be in the /usr/bin directory. Once I had the directories in place, I had to move the old directories (I was too chicken to delete them) and link to the new one. Before you do that, though, I’d check that /usr/bin/sudo was in place on the SSDD and that its sticky bit was set. If you lose that and you have no root account set…. well… let’s not do that, ok?

Moving /usr/bin is a little tricky because you need to be root, but sudo is in /usr/bin! The easiest thing to do is just become root even though that angers the Ubuntu faithful:

sudo -i

Then you can do commands like the following:

mv /usr/bin /usr/bin-old ; ln -s /ssdd1/usr/bin /usr/bin

If things go wrong you can still get to things via /usr/bin-old.  So if you do mess up you could do something like:

/usr/bin-old/sudo mv /usr/bin-old /usr/bin

That actually sped things up quite a bit. Booting is notably faster and the response time of X in general is greatly improved.

I then repeated the steps but this time I made shadow directories for ~/.config and ~/.kde. I redirected these to /ssdd1/home/alw/.config and /ssdd1/home/alw/.kde, respectively. This also made the system much snappier.

There are other ways you could have done this. I considered loop mounting over the existing directories, for example. This would be very safe since if the drive didn’t mount you’d just lose performance. The issue is you’d need to occasionally sync the real directory and the shadow directory to be sure any updates were applied. I had bad luck with a scheme like that for putting Firefox’s files into a ram drive — if it does get out of sync, look out. Of course, my scheme has a similar issue — if you lose the SSDD your old files are out of date. But with my setup there is no way for updates to occur to the old files (for example, if the SSDD fails to mount and you don’t notice) making them  mismatched in both directions. You can rsync them from time to time, either way, but with my method if the SSDD fails to mount you will know it immediately. Also, the old files stay visible making syncing them easier if you care to do that.

Meanwhile, I’m happy with how fast this has made my system. Granted, it was fast to start with (8G of RAM, 3.6GHz, and 4 cores), but the system does boot much faster, Gimp loads almost immediately, and switching between tabs in Chrome is notably faster. Since the system files are just moved and not removed, this “update” is fairly safe — you can still get to the original files if you have to. Just remember that updates will not change the old copies.

UPDATE: Turns out Apparmor realizes that things aren’t in /usr/bin anymore and starts blocking things. The only thing I noticed was my printers (the CUPS daemon won’t start). You need to go to /etc/apparmor.d/tunables and edit the alias file. Then reload apparmor and reload affected services (or reboot, which is easier). Here’s mine:

alias /usr/bin/ -> /ssdd1/usr/bin/,
alias /usr/lib/ -> /ssdd1/usr/lib/,
alias /usr/share/ -> /ssdd1/usr/share/,

UPDATE #2: VirtualBox does not like /usr/lib as a symbolic link, I’m not sure why but the code specifically checks for this case and throws an error. I had thought about using bind mounts instead of symlinks before and this pushed me over the edge. So I still use the symlinks for stuff in my home directory but for /usr instead of creating bin, share, and lib links I created empty directories to use as mount points. Then in /etc/fstab I put:

/ssdd1/usr/bin   /usr/bin   bind   defaults,bind   0 0

And similar lines for /usr/share and /usr/lib. Now Apparmor is happy and so is VirtualBox. I assume fstab mounts in order, so be sure those lines come AFTER the mount of /ssdd1, of course.

And one more update: Nepomuk and Strigi — which always give me grief anyway — write huge files to ~/.kde/share/apps/nepomuk/blah blah blah. This will quickly fill up a small SSDD. So one more link. I made ~/.nepomuk and then linked it in the ~/.kde tree. UNFORTUNATELY, filling up a btrfs volume causes it (at least in my case) to become totally unusable and unrepairable. So I would up switching everything to ext4. No compression, but better than having a total crash of the drive when it gets full.

After I had the crash I decided it would be nice if the regular hard drive had a pretty recent copy of what it was supposed to have. Since I had the space full crash about a week after I installed, it wasn’t so bad, but what if it had been a year or more later? Luckily, I had followed a simple pattern to replace the directories. Given some directory I wanted to replace called /x/y/z, I copy it to /ssdd1/x/y/z and then rename the original to /x/y/z-nossdd. Finally a bind mount or symlink points the original location at the SSDD.

Given that pattern, it was easy to create a daily cron job (/etc/cron.daily) named ssdd-backup:

#!/bin/bash
for dir in /home/alw/.kde /opt/google /usr/bin /usr/share /usr/lib /usr/include /usr/lib32 /home/alw/.config /home/alw/.local
do
rsync --archive --update --quiet /ssdd1$dir/ $dir-nossdd
done
This runs every day early in the morning and ensure that the old directories get any updates. The update is NOT two way. If you change something in xxx-nossdd, that does not propagate to the SSDD (but why would you?). Of course, you’ll want to modify the first line to name the directories you actually shadowed.

Viewing EFX Fax Files (EFAX) with Linux (or anything else probably)

I get about 2 faxes a year, so I hate to stand up a dedicated fax machine and phone line. There is, of course, a free fax service available. But the problem is the free version only delivers proprietary efx files. To open these you need to run the eFax software. Sure its free, but its Windows only! Might run under Wine with some coaxing and, of course, you can run Windows in VirtualBox, but what a pain just to open a fax or two a year.

EFax will let you get the fax as PDF if you upgrade. I just don’t get enough faxes to justify that. But there is a way.

If you log into your account online you will see several options including “View Faxes.” Click that. You get a web page that looks like an e-mail inbox. You’ll see the faxes you have received. Click the fax you want to view or print. This will load a preview pane at the bottom. But you can only look at the first page. Double clicking the fax brings up a  new tab that shows the same preview image. But there is a link in the header that says “View Fax: xxxxxxxx.efx” (where the x’s are the file name). You’d think that would just download the efx file, right? Nope.

Click the link. Go on. Now you have a viewer that lets you pick a page of the FAX, zoom it, and rotate it. If you just need to look at the fax you are done. But I wanted to print mine. Am I out of luck? Here’s the trick: Right click on the displayed fax (it doesn’t matter if you can see it all or not). Depending on your browser you’ll get an option like “Copy Image URL” (I guess you could also “Save Image” and then go open the image in your favorite picture editor and go from there.  With the Image URL, go to a Web browser and paste it into the URL bar. Woo hoo! You now have a full page image that you can print from the browser. Different browsers handle images differently, so you are on your own at this point. On Chrome I’d zoom the image up to 100% and print and I got nice full page faxes. If you were not at 100% the print would be smaller than the page.

Of course, if you have a 100 page fax its a pain to have to do this for every page. But seriously, if you are getting 100s of pages of fax, subscribe or install the free app under VirtualBox or Wine! But for those of us who left most of our faxes back in the 1990’s this is an easy way to get faxes printed without Windows.

Emacs, Chrome, and Daemons

I  really liked the Chrome extension that lets you edit multiline text edits in Chrome with emacs (or, I guess any other editor — as though there are any other editors). However, it has to have a server running. You can run a python server or one in e-lisp. But the lisp one requires emacs to be running already.

Here’s how I set mine up.

1) Autostart emacs –daemon using your choice of methods (autostart, Xsession, whatever)

2) In your .emacs you need this:

(if (and (daemonp) (locate-library "edit-server"))

 (progn
(require 'edit-server)
; can add options here like (setq edit-server-new-frame-mode-line t) (setq edit-server-new-frame-minibuffer t)
(edit-server-start)))

 3) Then in your .bashrc put this:
if [ -z "$DISPLAY" ]
then
  alias emacs='emacsclient -t'

   export EDITOR='emacsclient -t'
else
   alias emacs='emacsclient -c -n'
   export EDITOR='emacsclient -c -n'

 fi

 Works great. Of course, you need to start the daemon manually THIS TIME (and execute the script lines) or log out and log back on for this to work.

By the way, once you set up the alias, you can’t easily restart the emacs daemon. Remember you can override an alias with quotes: ’emacs’ –daemon

Update: you can set ALTERNATE_EDITOR in your environment to the empty string and emacsclient will start a daemon if one is not running. So that means you could skip step 1 if you add:


 export ALTERNATE_EDITOR=""

to step 3. This is nice because if you kill the server (to reload your .emacs, for example) or it dies (unlikely) then things till work without the user having to reload the daemon manually. Of course, you can still keep step 1 as well which makes your initial load a little faster, and still protects you against a dead daemon.

Analog Chart Recorder in Shell Script

GNU Plot streams data
GNU Plot streaming GP3 data "live"

Linux has a long history of stringing together data and processing to get new features. With the interface board analog and digital “real world” data just become another piece of data to process using the Linux tools.

This article shows you how to string together a GP-3, some fancy shell script, a Perl script found on the Internet, and GNU Plot to make a data recorder that is flexible without writing any actual code.

GP3 Videos and a New Board On its Way

If you’ve been reading my posts about the Rigol scope you’ve probably noticed I use the to generate odd test signals. We have made these for a long time and they are in the most amazing number of things all over the world. But over the years we’ve heard customers ask for new or different options and we’ve thought of a few things ourselves. So within the next month there will be a new PCB option for the GP-3. The new board will have the option to be easily wired for RS232, USB, or both. It also has provisions for “regular” LEDs, screw terminals that can double as feet, and all signals brought out to the terminals. A dedicated jumper lets you set the GP3EZ program status, an improved reset switch, and provisions for a resonator or a crystal are just some of the new features you can expect.

These new boards will be even better for tough industrial applications and have a quieter A/D layout than the old boards. But the older boards will still be available. They cost less, are easier to work with on a solderless breadboard, and they are much smaller so they squeeze into tight spots better. But choice is good, right?

If you want to watch some videos about the GP3:

Oh and by the way. We’ve always supported Linux with a variety of libraries. But the GP3EZ software now runs under Linux too. If you haven’t seen GP3EZ, check out the videos.