Archive for the 'Code' Category

PIC10F Assembly "Hello World"

So for a project, I’m using a PIC10F due to their very small size (in a SOT23-6, wow) and low cost.  They don’t have interrupt support, but they do have a timer, watchdog timer and a comparator.  You can do some fairly funky things with them.  However, I’m opting to use assembly for this project since it’s probably going to be simpler this way, as the stuff I need to do is very timing critical (reading sensors without interrupts = lots of polling).  Also I think I can squeeze the size of the program down better than a C compiler might do.

But I ran into a roadblock.  There are no documentations of assembly code listings for the PIC10F, and very few resources online.  So here’s a little snippet for you, which blinks an LED on pin GP2:

	list      p=10F222            ; list directive to define processor

#include
        ; processor specific variable definitions

    #define LightOn bsf GPIO,2  ; turn drive output on
    #define LightOff bcf GPIO,2 ; turn drive output off

;**********************************************************************
	ORG     0x1FF             ; processor reset vector
	ORG     0x000             ; coding begins here

    cblock 0x18 ; this is where we declare what to call user memory locations
      DelayTime:3       ; reserve 3 locations for delay counters
     endc

	goto Init
Start:
	clrwdt
	LightOn
	call Delay
	LightOff
	goto Start

Init:
    movwf   OSCCAL      ; load factory osccal value at start-up
    bcf     OSCCAL,0    ; do NOT output osc/4 on GP2 (IR sensor input)
    movlw   b'00000000' ; set all GPIO low at startup
    movwf   GPIO        ; load port latches
    movlw   b'00000000' ; All GPIO pins are outputs
    tris    GPIO        ; set I/O directions
    movlw   b'11011000'	; wake-up on pin change disabled, weak pull-ups off
                        ; Timer0 clock select on GP2 internal
    option              ; write to OPTION_REG

    ; clear all user RAM/variables on boot

    movlw  0x10         ; initialize pointer
    movwf  FSR          ; to RAM start
	goto Start

Delay					  ;
    movlw   D'2'          ; 2 = a delay period of approximately 500mS

Delay2                    ;
    movwf   DelayTime     ; pre-load W & enter at Delay2 for different delays
    movlw   D'232'        ;
    movwf   DelayTime+1   ;
    movlw   D'255'        ;
    movwf   DelayTime+2   ;
Dloop
    clrwdt                ; 1
    decfsz  DelayTime+2,f ; 1
    goto    $-2           ; 2/1
    decfsz  DelayTime+1,f ; 1
    goto    $-4           ; 2/1
    decfsz  DelayTime,f   ; 1
    goto    $-6           ; 2/1
    retlw   0             ; 2

	end

Delay routine and general structure is shamelessly stolen from Bruce on PICbasic.co.uk.

Hopefully, someone will find this useful :)

*twitch*

I’ve been working on my second year project for the last …. 12 hours now.  Most of the backend is done.  It looks like this:

As I snapped this screenshot, I noticed that my BlueJ window was exactly 1337 pixels wide.  That’s surely got to be a good sign, right?

Now, before someone goes off on a rant “Oh, NMEA parsing isn’t hard, I did it in a Python script!”.  I’ve got to do a bit more than just updating Location information.  I have to generically parse all NMEA sentences and check if they’re valid or not, and depending on whether they’re a Proprietary sentence or not I need to switch send the data to the right sort of object.  I’ve got measurements coming in on the stream which gives me Temperature, Humidity, Pressure and vertical acceleration.  I need to be able to dispatch these Measurements to the right places so they can be updated in the GUI. Also, each one of these Measurements needs to be associated with a Location.  I also need to be able to throw Exceptions which can be sensibly handled by the clients. Also everything needs to be completely documented.

It’s not particularly hard, just big and long. The class to turn the GGA sentences into Locations was the easy bit.

Now: I’m going to watch some BSG and go to sleep before my shift in 10 hours.

Bleeeding!

Upcoming.org PHP5 API Wrapper

Anyone who uses Upcoming.org and would like to integrate it with an application made PHP5, I have a class for you!

Upcoming.org API Wrapper

It’s the product of a couple of days work, thinking that there has to be a better way of using the Upcoming.org API through PHP5. It uses magic methods for the API calls – meaning that implementing a new API method is as simple as inserting a single line into the class. It has caching support.

The only part that’s missing is the whole authentication token/frobbing thing … since I didn’t need to use that for the project I was working on I concentrated on getting the main API calling framework of it working. If anyone fancies hacking it to work in a sensible way, feel free. I’d appreciate if you sent me your work, too :)

The Coolest thing ever AKA Spambot Trap

The coolest thing in the world!!111

Actually it’s just a huge list of randomly generated email addresses. I’m trying to fill up an email box with as much spam as I can, so that I can dissect it and better design filters for my own mail server and such. With any luck the spambots will crawl my site (as they do already) and pick up on it and gorge on it for a little while. It should never ever contain any email addresses that are real.

The code is pretty neat if I say so myself. I might submit the snippets to Code Snippets… I made a couple of functions. The thing was loosely based on this script Random String Generator, but I made some much needed improvements.

The first “long but simple” function was way too long for my liking. I made a much neater solution:

function map_char($num) {

// range is 61... no validation in the function itself!

$int = $num;

$int+=48;

($int > 57) ? $int += 7 : null;

($int > 90) ? $int += 6 :null;

return  chr($int);

}

True, 3 logic operations have to occur for any charachter, BUT that’s a lot better than a mean of 13 comparisons per charachter (as in the giant select case in the previous function). Since the script is so small and takes no use input I didn’t see the point in putting any validation into it.

Next snippet is to make a string of length $length…

for($i=1; $i< =$length; $i++)

{

mt_srand(make_seed());

$rand_string .= map_char(mt_rand(0,61));

}

make_seed() was nicked from the php documentation on mt_srand(). Easy enough. Then you just echo out your random strings with @spamdomain.co.uk on the end. You might also want to add failsafes so that you don’t ever echo out a real email address.

Then simply make an array with all your real email addresses in it, and run the returned random string against it using in_array();

The whole code to create the random string looks like this:

function generate_string($length)

{

global $valid_emails;

if($length>0)

{

$rand_id="";

for($i=1; $i< =$length; $i++)

{

mt_srand(make_seed());

$rand_id .= map_char(mt_rand(0,61));

}

}

if (in_array($rand_id,$valid_emails)) {

return generate_string($length);

} else {

return $rand_id;

}

}

The $valid_emails variable is an array containing all of your valid email addresses, for example:

$valid_email = Array ( "goldfish","arnold","grandma","auntyhetty" );

And so forth… I made it a global because it might make things easier in the future :p

So there we go… a spambot trap. Fantastical.