Hardware Programming! The picKit2 from Microchip

So I’ve wanted to learn some hardware programming for quite a while, and have now taken the first step!

A popular choice of learning resources is the company microchip.com , they have a product called the picKit 2, which is a ‘demo board’ . After much reading i understand that this is a circuit designed to help you get up and running quickly.

In the case of the pickit2, this means it has a processor on board ( the pic16f917 ) , a bunch of led’s already wired in , a potentiometer (turny switch) and a button.. as well as a bunch of open outputs for you to do whatever you like with.

This is not something for the faint of heart it would appear, so after stumbling around the documentation and sample code for a while, i bought this book The Pic Microcontroller, your personal introductory course, a promising name.

The book is very well written, and after the first 2 chapters i felt much more able to approach my board.

The demo board comes with a usb device that attaches it to the computer, i’ve learned that this is a ‘programmer’ , which is in charge of delivering the compiled code to the chip itself, the integration of this is one of the things that makes this a ‘demo’ board.

After some more reading, i tried out my first program, borrowing some sample code from one of the existing projects, I had success!

See here my first program running, in all it’s glorious 7 lights on, 1 light off glory!

pic!

Functionally, every 8 outputs get assigned a file register (memory location), and by assigning an 8 bit binary value to this location, you can set the pins as either on, or off. So to set the first pin on, you use a value of 00000001 , the first and last as 10000001, etc… The default numbering system in assembly is hexidecimal, so you represent these as hex, so, 00000001 = 1, 10000001 = 81.

Here is the functional element to my program, which turned all pins on, except for the 2nd pin.
================

MainLoop
movlw 0xfd
movwf PORTD

goto MainLoop

================

so, as you can see here I am using the hex value fd, and assigning it to the file page that controls my led’s. fd of course equals 253 in decimal, or 11111101 in binary, so you can see the led that’s off. The other commands are pretty simple as well
mov = mov
l = literal
w = working register (you only get one of these, everything moves through it)
f = file page

so
movlw 0xfd
means, move a literal into the working register, and then i give the literal 0xfd , 0x just means its a hex number. I could aslo have used movlw b’11111101 , or for that matter movlw d’253

and then
movwf PORTD
means, move the working register into the filepage PORTD (my output register).

Really pretty simple at the end of the day, once your environment is running.

IE Mysteriously not allowing cookies

So,

All of a sudden an application I had written started being buggy in that if you were using IE, you wouldn’t stay logged in after the initial login.

A bit of investigation showed that the session cookie wasn’t being set, odd.. it had been working. A quick workaround in IIS was to set the session management to be url based, but this caused horrors to happen to the url (it prepending all links with a session key hash).

The answer at the end of the day, was that ie will reject cookies from domains that have an underscore ‘_’ in them, and indeed in this case mine did (some_sub_domain.mydomain.com) .

So changing that fixed the problem, caveat emptor! you may be able to set any crazy subdomain you want, but be careful. It would seem that _ is not valid as far as the official RFC is concerned (can’t find that myself) , and so some apps care, some don’t.

Another interesting discovery here was the IIS Session State Management control panel, where you can set it to be cookie, url, autodetect, a session server (that’s interesting) or custom (guess you’d roll your own) . This explains part of how to do web app load balancing and such with shared sessions.

Click this image to see the menu in context
Small iis

PHP MagickWand GIF Cropping issues.

So, I have some magickwand code which crops images, all seemed well.

Then it’s pointed out that GIF files are not cropping properly, an investigation shows serious wierdness with them.

Eventually I discover that the are in fact being cropped, but some metadata about their size is not being updated..

The symptoms here were that the image looked fine in my imageviewer (irfanview) , but in a browser (including the windows explorer thumbnail view) they were showing as their orriginal pre-cropped size as transparent, and the newly cropped image visible..

see below (click for larger versions)

Here’s the original Image

And here is the perplexing ‘cropped’ image, you can see that the crop worked, but for some reason it is layered on the original image resolution..

The windows file properties show the file as 486*563, the original size… but opening in irfanview shows the image as 275*160, the cropped size. Clearly there is some metadata in a GIF that defines the size, which some applications (browsers) care about and others (irfanview) do not.. so on to find that.

A trip to imagemagicks identify command was equally confusing, here it shows us the ‘correct’ cropped resolution
identify -format “%@” orig.gif
223×438+0+0

But after some experimenting , I found
identify -format “%g” orig.gif
486×563+0+0

This was the original and now ‘incorrect’ size, so I had at least located the data. Apparently GIF stores ‘page geometry’ data, surprise !

So I found the method in magickwand to deal with this

MagickBooleanType MagickSetPage(MagickWand *wand,
const unsigned long width,const unsigned long height,const long x,
const long y)

Putting that in place unfortunately solved nothing, and the identify %g still showed the wrong data.. my love affair with magickwand ended a long time ago, so this was not well received.

This is where we enter the great, and terrible things about open source:
1. Documentation is often wrong, outdated, or totally absent
2. You can find the source code and just look at that

So as has been the case many times with MagickWand, I found the code and had a look, turns out there is a method called MagickSetImagePage in ImageMagick (not MagickWand) , so I figure I’d try it out. Sure enough that method works fine, and does exactly what I want see here:

MagickBooleanType MagickSetImagePage(MagickWand *wand,
const unsigned long width,const unsigned long height,const long x,
const long y)

Issue resolved, and here see the cropped image in all it’s proper glory.

Here’s the code that generates these files


<?php

$current_file = 'orig.gif';
$sourceWand = NewMagickWand();
MagickReadImage($sourceWand,$current_file);

$cropwand = CloneMagickWand($sourceWand);
MagickCropImage( $cropwand, 275,160,90,200);

magickWriteImage($cropwand,"bad.gif");
MagickSetImagePage( $cropwand,275,160,0,0);
magickWriteImage($cropwand,"good.gif");

DestroyMagickWand($sourceWand);

?>

Wordpress and the dreaded “Your PHP installation appears to be missing the MySQL”

So, if you’ve setup wordpress on your own server you’ve probably seen this

“Your PHP installation appears to be missing the MySQL”

Terrible. really terrible.

So, this is what I’ve done several times to make it work, and it does appear to work.

Environment
Server 2003, IIS, PHP 5+, wordpress some version (1.5 + i think)

This assumes your php is up and running , do a phpinfo to ensure it is.
steps:

1. add php to your path
2. ensure that “extension=mysql.so” and “extension=mysqli.so” are uncommented in your php.ini
3. ensure both of those files actually are in your php /ext directory
4. ensure the ‘ext_dir‘ path is set to the full directory path to php/ext in your php.ini eg:"c:\php\ext"
5. ensure the iis user (internet guest account and iis_wpg) have read priveleges to your php directory
6. add an environment variable “PHPRC” with the value of your php directory , this is key
—update—
EVeryone feels a reboot is required, I expect restarting the www service would do the trick as well
6.5 restart the www service
————-
7. shazaam.

You can read through the mess of inane oppinions here
http://wordpress.org/support/topic/37800

some of them are good, most are nonsense (see ‘just use php4′)

Thread Safe custom logging in PHP

I read a lot of rediculous and convoluted suggestions as to how one would create a custom log in php, the problem of course making in thread safe in a web server environment.

Well it turns out to be this simple

error_log($LOGG_MESSAGE.”\n”,3,$YOUR_LOG_FILE);

the 3 is important, see here

Mozilla, javascript and xml parsing.

So I did an ajax application, which used ajax to pass around potentially large amounts of xml (something i may choose not to repeat). Some very strange errors were appearing, which i traced to some nodes being truncated.

After much testing, i saw that the xml was intact, the node was intact, but when I used my GetNode function, it was truncated. Turns out that for very long (larger than 4096 characters) , the engine creates multiple text nodes, so the trusty node.firstChild.nodeValue only returned the first 4096 characters of the text!

The fix appears to be to iterate through the childnodes, building a string as you go, as done here.

———-

getNodeValueDebug = function(node,key,debug){

if (!node || node.getElementsByTagName(key).length == 0||  node.getElementsByTagName(key) == null){
return null;
}
if (node.getElementsByTagName(key)[0].firstChild){

var val=”";
var obtext = node.getElementsByTagName(key)[0];

//have to build it up to avoid truncation
for(var cnt=0; cnt < obtext.childNodes.length; cnt++)
{

if (debug){
alert(val.length +”:”+obtext.childNodes[cnt].nodeValue.length);
}
val += obtext.childNodes[cnt].nodeValue;
}

return val;

}else{
return null;
}
}

.net mysql , and the unreliable connection state.

I posted this as a bug on mysql… here it is again

Well, it seems to me that Connection.State is not reliably correct. If I stop
and start the mysql server, the state still reads as open, and throws
'connection reset' exceptions if i act on it.

So, I made a wrapper class, because the MysqlConnection class is sealed and I
couldn't extend. Here it is for your benefit.
---------
/*
* User: bill@bigmojo.net
* Date: 9/12/2005
* Time: 11:16 AM
*
*/

using System;
using System.Data;

using MySql.Data.MySqlClient;

namespace My.MySqlConnection
{

///
/// Description of MySqlConnection.
///
public class xmmsMySqlConnection:IDbConnection
{
private MySqlConnection _mysql = null;

public xmmsMySqlConnection()
{
_mysql = new MySqlConnection();
}

#region IDbConnection Members

public void ChangeDatabase(string databaseName)

{

_mysql.ChangeDatabase(Database);

}

public IDbTransaction BeginTransaction(System.Data.IsolationLevel il)

{

return _mysql.BeginTransaction(il);

}

IDbTransaction System.Data.IDbConnection.BeginTransaction()

{

return _mysql.BeginTransaction();

}

///
/// This is why we have this class, return results of ping, more reliable.
///
public System.Data.ConnectionState State

{

get

{

if ( _mysql != null && _mysql.State != ConnectionState.Closed &&
_mysql.Ping()){
return _mysql.State;
}else{
return ConnectionState.Closed;
}

}

}

public string ConnectionString

{

get

{

return _mysql.ConnectionString;

}

set

{

_mysql.ConnectionString = value;

}

}

public IDbCommand CreateCommand()

{

return _mysql.CreateCommand();

}

public void Open()

{

_mysql.Open();

}

public void Close()

{

_mysql.Close();

}

public string Database

{

get

{

return _mysql.Database;

}

}

public int ConnectionTimeout

{

get

{

return _mysql.ConnectionTimeout;

}

}

#endregion

#region IDisposable Members

public void Dispose()

{

_mysql.Dispose();

}

#endregion
}
}
----------------

The Auto suggest typeahead conundrum

UPDATE AGAIN: apparently this IS working on safari, so there you go..
UPDATE : apparently this isn’t working on safari, any mac devs out there that are interested, I don’t have a mac, so testing is a problem….

So I wanted to do an autosuggest type box, along the lines of google suggest, or the flickr tags.

I found this example at codeproject , which seemed to do the right thing, but as the comments suggest, its crazy heavyweight.. To run the example you need a database, need to compile the project, etc.. etc… Seemed unnecessary. Also, the javascript was > 80k , huge.

It turned out almost all of this javascript was the Google XSLT libraries, which were being used to simply draw 1 div. Also, this example had no keyboard handling, or multi-word support.

So, I’ve created the ’skinny typeahead demo’ , this is dramatically smaller (javascript file is 7k) and runs without a database or server side script (for the demo, your implementation will need something to provide the word suggestions).

This demo supports keyboard control (up/down arrow, enter key to select) , as well as comma separated words, so if you use a comma, it will start providing suggestions based on the word after the comma.

Here is a functional demo and here is a zip of the project

AJAX caching xml problems.

So,

I’ve been using ajax for a while now, and a real problem is that the browser caches the xml like crazy, so if your not using parameters, then getting the most up to date xml can be a problem.

I looked around and found some good suggestions

1. use a server side script to output the xml + a no-cache header

2. add a custom header to the xml type on the web server (usually not an option)

3. add a ‘get if newer than’ header to your request (set newer than = jan 1 1970)

4. add an arbitrary random parameter to your request

Solution 4 is brilliant and simple, so just add a random guid to your request! eg :

url += “?sid=”+Math.Random;

nice.

ajax synchronous vs. NON-synchronous

So,

sometimes you dont want your call to be asyncchronous, when there is an order of operations to be followed, for example.

Here is a modification on the xml.com ‘loadXmlDoc’ method that takes asynch as a paramater, it also takes an xml bundle to post back to the server if you need to do that, leave it null if not.

// spacing gets mangled by wordpress

//XML utilities
function loadXMLDoc(url,method,asynch,xmlPost)
{
_method = method;
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
XmlHttpreq = new XMLHttpRequest();

if (asynch == true){
XmlHttpreq.onreadystatechange = processReqChange;
}

XmlHttpreq.open(”POST”, url, asynch);
XmlHttpreq.setRequestHeader(”Content-Type”, “application/x-www-form-urlencoded; Charset=utf-8″);
XmlHttpreq.send(xmlPost);

if (asynch == false){
processReqChange() ;
}

// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
XmlHttpreq = new ActiveXObject(”Microsoft.XMLHTTP”);
if (XmlHttpreq) {
if (asynch == true){
XmlHttpreq.onreadystatechange = processReqChange;
}

XmlHttpreq.open(”POST”, url, asynch);
XmlHttpreq.setRequestHeader(”Content-Type”, “application/x-www-form-urlencoded; Charset=utf-8″);
XmlHttpreq.send(xmlPost);

if (asynch == false){
processReqChange() ;
}
}
}
}