Archive for the ‘General’ Category

glib.idle_add for tkinter in python

Saturday, November 10th, 2012

After doing a lot of python GTK+ work on Exaile, I’ve found that it uses glib.idle_add() extensively — and usually with good reason. idle_add is great if you want to ensure that whatever you’re calling is being called on the GUI thread, so that way you don’t have to worry too much about thread interactions as long as you keep things separate.

Another mentor and I are developing a GUI video game along with a ‘fake wpilib’ for our FIRST Robotics programming students to help them learn how to program, and as such we’ve decided to use TKinter for the GUI toolkit (since it supports Python 3, and usually doesn’t require the kids to install anything special to make it work). However, as I started making things I couldn’t find the equivalent of idle_add() for TKinter, and I guess there isn’t one — after_idle() apparently blocks until the event loop is idle, and so that isn’t what I wanted.

A number of posts I found online advocated to poll a queue for input… but I *really* dislike polling, and try to avoid it when I can. So I wrote up this set of routines that is roughly equivalent to idle_add() in tkinter, and uses a queue while avoiding polling.

        
import Tkinter as tk
from queue import Queue, Empty

def idle_add(callable, *args):
    '''Call this with a function and optional arguments, and that function
       will be called on the GUI thread via an event.
       
       This function returns immediately.
    '''
    queue.put((callable, args))
    root.event_generate('<<Idle>>', when='tail')
    
def _on_idle(event):
    '''This should never be called directly, it is called via an 
       event, and should always be on the GUI thread
    '''
    while True:
        try:
            callable, args = queue.get(block=False)
        except queue.Empty:
            break
        callable(*args)
        
queue = Queue()
root = tk.Tk()
root.bind('<<Idle>>', _on_idle)

Comments inadvertently disabled

Wednesday, October 10th, 2012

Apparently at some point my anti-spam plugin decided to stop working. No wonder I haven’t had any comments in a really long time…. should be working now!

cRio Netconsole implemented in Python

Friday, November 12th, 2010

The FIRST Robotics Competition uses the NI-cRio platform for the controller for the robots in the competition. There is a bit of functionality called ‘NetConsole’ which sends the stdout from vxWorks out to a waiting client (refer to the WPI FIRST website for instructions to enable this). It turns out that the protocol used to implement the NetConsole for the cRio is incredibly simple… it just sends the raw output data as a bunch of UDP packets out to the broadcast address on port 6666 (which I suppose is slightly amusing). Here’s a dirt-simple python script that catches the output (TODO: need to send it input… haven’t gotten around to looking at that yet).

#!/usr/bin/env python

import socket
import select

UDP_PORT=6666

sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP )

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind( ('',UDP_PORT) )
sock.setblocking(0)

while True:
    result = select.select( [sock], [], [] )
    msg = result[0][0].recv( 8196 )
    print msg

Obsidian Theme for Visual Studio 2008

Wednesday, March 24th, 2010

I’ve become a fan of the obsidian theme for notepad++ (even though I know it comes from other locations as well), because I feel like the theme is a lot easier on my eyes. So I’ve had to start using Visual Studio a bit more lately, and I adjusted the fonts/colors to match the notepad++ obsidian theme (with some variation, of course). I’ve exported the settings, and you can use them as you wish. I haven’t fully explored the settings yet, but this seems to do it for much of the stuff I run into. If you have adjustments for this that makes it better, let me know!

Download here

WebDMA 0.2 Released

Saturday, January 23rd, 2010

WebDMA 0.2 is now available for download at the WebDMA project site. Using C++ operator overloading, WebDMA provides proxy objects that your application can use as normal variables which can be manipulated or displayed by your application via a configurable jQuery/javascript powered AJAX-enabled Web 2.0 interface hosted by an lightweight embedded web server. Its really neat, and really useful for tuning/debugging our FRC robot. Of course, it works on Windows/Linux too.

This is a better packaged version of WebDMA, with a few feature improvements. Of particular interest to FRC teams, I have released an installer that installs the object files for WebDMA on your robot, and copies the header files to the needed locations for Wind River. There is also a sample program installed for Wind River as well that allows you to control two motors on your robot with the web interface as a demo. All you need to do to use WebDMA on your robot is install the install package to your development computer, and then run the install program to copy the necessary object files to your robot.

While this is very easy and painfree for beginners to install and use, of course I must give you this disclaimer since a program installed by the installer writes directly to the robot to install the object file:

WARNING: The provided installer will WRITE DIRECTLY TO YOUR ROBOT and modify files on it via FTP.

While I have tested this and this works just fine for our team on our cRio with no ill effects, I cannot be held responsible for your robot. This installer is not sanctioned or associated with National Instruments, Wind River, or FIRST Robotics. In particular, this may void your warranty, render your cRio useless, and COST YOUR TEAM A LOT OF MONEY.

If you are a student: DO NOT DOWNLOAD AND INSTALL THIS ON YOUR ROBOT WITHOUT ASKING THE ADULT LEADER OF YOUR TEAM.

But seriously, it works just fine for me though. And if you have problems, let me know. :)

Boost.Asio Tech Talk Slides

Tuesday, November 24th, 2009

I gave a presentation at work about Boost.Asio, and it came out reasonably well, so I figured I would post the slides here. Its a brief introduction to Asio and what one might use it for. Also has some basic concepts and patterns one might use while programming with Asio. Aimed for a technical audience, but nothing too detailed though.

Boost.Asio Tech Talk Slides (PDF)

Free disk/sector editor from Microsoft

Sunday, October 11th, 2009

This is just a random note that I’ve actually known for quite awhile, but I figured I would post it online somewhere where others could find it. Apparently Microsoft made a disk editor thing called ‘Disk Probe’, which came in the NT Resource Kit. I had found it available to download standalone a really long time ago, but it doesn’t appear to be available anymore. However, you can find it in the Windows XP Service Pack 2 Support Tools available from Microsoft’s download website.

Its a bit weird (at least, the NT version was), but its pretty useful if you ever find yourself playing with raw disks and you need to access them from inside windows in a pinch.

XSMELL: C++ XML creation library

Wednesday, September 30th, 2009

I’m taking a workshop at work taught by David Abrahams, and he briefly mentioned this thing called XSMELL, which apparently allows you to write XML-like things in pure C++ code. From the README:

Congratulations! You have in your hands the MOST BRILLIANTEST C++ XML CREATION
LIBRARY EVER CREATED.

...

And you no longer have to worry about generating malformed XML! After spending
hours fighting obscure C++ compiler errors, you’ll be 100% certain that your
XML is correct.

Definitely amusing. :)

Middle button in X

Thursday, August 20th, 2009

I’ve always been a fan of PuTTY, the Windows terminal emulator. Its extremely simple to use and is one of the main reasons I like using windows to maintain linux systems. One of the biggest things I like about PuTTY is that it allows you to copy on highlight, and paste into the window by right clicking. It saves so much time and is really easy to use.

So when I moved my desktop to Ubuntu this summer and got rid of XP, this has really been one thing that I really miss. However, I just found a blog post that explains copy/paste in X, and that a similar system actually exists system-wide, except it uses the ‘middle’ mouse button instead.

Seriously, this is an awesome feature that I did not know about, and definitely am going to take advantage of. Now I just need to remap one of the extra buttons on my mouse to be the middle one so its more convenient to use…

WebDMA demo video

Saturday, April 25th, 2009

If you read my previous post, I was talking about this new open source library I’ve created that allows you to embed a web server in your C++ application so that you could modify variables inside of it. I had created it for our FIRST Robotics team so that we could use it to tune things on our robot (and its been extremely useful for that) and use it for simple data acquisition. Of course, driving a robot is nothing more than changing variables… so I thought it would be a neat demo to set it up so that it could be driven by the web interface. :)

Here’s a video of our robot being controlled via the WebDMA interface:

And theres a (non-working) HTML version of the interface (though, its no longer the exact one that is on the robot, but its close) at http://www.virtualroadside.com/botface/index.html