Strange idea: JS library for interfacing with any circuit via webcam

I’ve been pondering the odd way that our use of a webcam in the PLOTS spectrometer allows connecting directly from HTML5/JavaScript to a spectrometer over USB. What if we used a standardized array of LEDs — say, an Arduino shield of ’em — in a dark box facing a webcam. And wrote a standardized library for reading analog & digital pins from the video output… You could then plug many different circuits directly into a webpage.

Object.hasOwnProperty() in JavaScript

On StackOverflow, user Pablo Cabrera points out:

As slashnick pointed out, you can use the “for in” construct to iterate over an object for its attribute names. However you’ll be iterating over all attribute names in the object’s prototype chain. If you want to iterate only over the object’s own attributes, you can make use of the Object#hasOwnProperty() method. Thus having the following.

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
/* useful code here */
}
}

Wish I’d known of that a year ago.

Need for a scene graph in Canvas

Samuli Kaipiainen and Matti Paksula from the University of Helsinki Department of Computer Science wrote a great overview of SVG and Canvas performance issues. The above summarizes some of their thoughts specifically about the need for a scene graph, something I’ve wished for (esp. in regard to WebGL).

Read more

And I’m not just putting this up because they referenced my post on SVG vs. Canvas. Really.

Instiki ‘Permission denied’ problem

I run a few Instiki sites on Phusion Passenger, and they occasionally break – namely, they give a ‘Permission denied’ message which is hard to track.

The problem is that there are temp files like /tmp/passenger.1234 which have some kind of permissions problem.

To resolve this, you can simply delete those files, like with:

sudo rm -r /tmp/passenger*

and reboot Instiki (Apache, really):

sudo apachectl graceful

Instiki: add an auto redirect to the default web

I have a few Instiki instances out there: wiki.grassrootsmapping.org and wiki.cartagen.org – and the Grassroots Mapping wiki now has two ‘webs’. Initially when I added a second web, the front page at wiki.grassrootsmapping.org began showing a list of webs instead of the HomePage of the main web… if you’re an Instiki user you’ll know what I’m talking about. You can see the page I saw here: wiki.grassrootsmapping.org/web_list

Instiki allows you to define a DEFAULT_WEB in your /config/environment.rb, but it won’t redirect the web root to that web’s HomePage, which would seem to be the point. Darn. So I forked and patched Instiki on Github, adding the following code to replace line 26 of /app/controllers/wiki_controller.rb:

      if defined? DEFAULT_WEB
        @web_name = DEFAULT_WEB
        redirect_home
      else
        redirect_to :action => 'web_list'
      end

Now if I go to wiki.grassrootsmapping.org, I see the HomePage of my default web. Hooray!

Update: OK, my bad. This is unnecessary:

As distler pointed out on Github, defining DEFAULT_WEB should work out of the box. Why didn’t it work for me, I wondered?

Well, I put the line:

DEFAULT_WEB = 'wiki'

in environment.rb, but I put it OUTSIDE the Rails::Initializer.run do |config| block. Putting that line inside that code block solved my problem.

I’m leaving this all up for future generations to learn from my mistakes. (Most likely me in a year or so.)

Tidying up Cartagen a bit with tips from dev.opera.com

dev.opera.com has a great series of tips for improving JavaScript performance. Example:

Use strings accumulator-style

String concatenation can be an expensive process. Using the “+” operator does not wait for the result to be assigned to a variable. Instead, it creates a new string in memory, assigns its result to that string, and it is that new string that may be assigned to a variable. The following code shows a common assignment of a concatenated string:

“a += ‘x’ + ‘y’;”

That code would be evaluated by firstly creating a temporary string in memory, assigning the concatenated value of ‘xy’, then concatenating that with the current value of “a”, and finally assigning the resulting value of that to “a”. The following code uses two separate commands, but because it assigns directly to “a” each time, the temporary string is not used. The resulting code is around 20% faster in many current browsers, and potentially requires less memory, as it does not need to temporarily store the concatenated string:

a += ‘x’;
a += ‘y’;

Fixing Cache Images plugin in WordPress on MediaTemple

Matt Mullenweg’s great Cache Images plugin for WordPress (Just go to the Plugins > Add New page in your WordPress install and search for it) lets you copy any hotlinked images on your blog into your own /uploads/ folder for safekeeping (and to be polite to others’ servers!), and I love it. But when you first install it, it often chokes, especially on MediaTemple. I’ve had problems with this more than once, so I thought I’d jot this down for future reference and to help anyone else struggling with the same issues. The two problems are a) permissions for the /uploads/ folder, and b) the file_get_contents() function. Here’s a fix:

First, you have to change the uploads directory from a long absolute path to just a relative one, so in Settings > Miscellaneous, instead of:

/bla/bla/mediatemples/long/url/domainname/html/wp-content/uploads/

it should say:

wp-content/uploads

Second, you have to change the file_get_contents() call on line 118 of the plugin file “plugins/cache-images/cache-images.php”. MediaTemple and other hosts don’t like that. But you can usually use the curl() library, so if you add the following code somewhere near the top of the file, you can use this fancy new ‘file_get_contents_curl()‘ function instead of the native one:

function file_get_contents_curl($url) {
	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
	curl_setopt($ch, CURLOPT_URL, $url);
	
	$data = curl_exec($ch);
	curl_close($ch);
	
	return $data;
}

Then all you have to do is change the call (on what USED TO BE line 118) to:

$img      = file_get_contents_curl( $b['scheme'] . '://' . $b['host'] . str_replace(' ', '%20', $b['path']) . $b['query'] );

That is, just change “file_get_contents” to “file_get_contents_curl()”.

I’d do it myself for you and host the file, but it’s not my code. Hmm, can I commit this change anywhere? Matt?

RAGI on Rails 2.3+

RAGI, a Ruby interface for Asterisk, allows you to handle phone calls with a Ruby on Rails application. The config/environment.rb entry has to be modified a bit for a modern Rails, since many online tutorials/resources were written in the Rails ~1.2 era, and assume WEBrick as the server.

The offending line is this:

class SimpleThreadServer < WEBrick::SimpleServer

Which can be modified to:

class SimpleThreadServer < Mongrel::HttpHandler

The complete configuration you'll need to add inside your Rails::Initializer.run do |config| block is:

  # The following code tells Rails to start a Ragi server
  # as a separate thread.
  ActiveSupport::Dependencies.mechanism = :require
  # Include your application configuration below
  # Simple server that spawns a new thread for the server
  class SimpleThreadServer < Mongrel::HttpHandler
    def SimpleThreadServer.start(&block)
      Thread.new do block.call
      end
    end
  end
  require 'ragi/call_server'
  RAGI::CallServer.new( :ServerType => SimpleThreadServer )