Posts tagged ‘Code’

ISO Date/Time in java

took me a while to figure this one out, you’d think getting a standarized date/time would be a bit simpler…

put the following in a class

private final static SimpleDateFormat dateFormat;
static {
    //as per ISO 8601
    dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    //make sure the formatter requires an exact string
    dateFormat.setLenient(false);
    //set "timezone" to Universal Time Coordinates (GMT adjusted by leap seconds)
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}

and the following elsewhere in the class:

//to create a string
String nowString = dateFormat.format(new Date());
//to parse a proper string to a date
Date nowDate = dateFormat.parse(nowString);

Java ActionEvents and ActionListeners

an ActionListener is an interface for receiving action events, a class that should process action events should implement this interface, then be registered with a component.

an ActionListener can be registered with any object capable of it, typically this is an instance of an AbstactButton class like a JButton or JMenuItem.
Continue reading ‘Java ActionEvents and ActionListeners’ »

Eagle3D Component - Simple Jumpers

some very simple jumpers for Eagle3D, I tend to use a lot of them due to single-sided board restrictions.

the include file, to be placed in “[Eagle3D Root]/povray” and added to “[Eagle3D Root]/povray/user.inc”. see this post for details.
user_jumper.inc

 

and the associated images (to be placed in “[Eagle3D Root]/ulp/img” to aid in identification, optional):

Zip archive of images

and as requested, a rendered image and an example board:
Wire Jumpers

Example Wire Jumper Board(jumper.brd)

additionally, if the correct user functions can not be found (or you would rather not look for them),
the following lines can be manually added to “EAGLE_ROOT/ulp/3dusrpac.dat”:

05:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:USER_WIREJUMPER_05MM(:USER_WIREJUMPER_05MM:
07:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:USER_WIREJUMPER_07MM(:USER_WIREJUMPER_07MM:
10:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:USER_WIREJUMPER_10MM(:USER_WIREJUMPER_10MM:
12:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:USER_WIREJUMPER_12MM(:USER_WIREJUMPER_12MM:
15:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:USER_WIREJUMPER_15MM(:USER_WIREJUMPER_15MM:

Update:
The above file is incompatible with newer versions of eagle. If you get errors with user_jumper.inc, try the following file:
user_jumper-54.inc
and adding #include “user_jumper-54.inc” rather than #include “user_jumper.inc” to /povray/user.inc

Designing a keypad for an AVR Microcontroller

by using the upper four bits as outputs and the lower four bits as inputs, a simple 4×4, 16 button keypad can be connected to a microcontroller.

the first picture shows the state after the “0″ key has been pressed.
the 0×24 corresponds to the “scancode”, the first digit being the column and the second being the row.
to be exact, in binary 0×24 equals 0b00101000, this shows that during the scan, the key in the second column and the fourth row was pressed.

the bracketed values under that are the contents of the key buffer.

HPIM0056-e.JPG mainboard-top.png mainboard-bot.png
mainboard-sch.png mainboard-brd-e.png mainboard-brd-silk.png

 
 
a simple code example for this:

//high bits are columns, low bits are rows
volatile uint8_t scancode = 0x00;
uint8_t i, j;

// set data direction on PORTD
//   pins 8,7,6,5 as output, pins 4,3,2,1 as input
outb(DDRD,  0b11110000);

//loop over pins
for( i=4; i<8; i++ ) {

	// sets outputs low one pin at a time
	//   also enables internal pull-ups on inputs
	outb( PORTD, ~(1<<i) );

	//debounce delay and settling time
	timerPause(5);

	//loop over port positions
	for( j=0; j<4; j++ ) {
		if( bit_is_clear(PIND,j) ) {
			//set scancode
			scancode = (1<<i)|(1<<j);
		}
	}

}

 
 
the “~(1<<n)” is an example of bitshifting, in this case it means “bitshift 1 left “n” times, then invert the result” or if we take n as 3, it would be:
~(1<<3)
after 0 shifts:
~(0b00000001)
after 1 shift:
~(0b00000010)
after 2 shifts:
~(0b00000100)
after 3 shifts:
~(0b00001000)
then inverted:
0b11110111

a slightly more complicated looking example of this is used in the inner loop:
(1<<n)|(1<<m)
or if we take n as 6 and m as 2 it would be:
(0b01000000)|(0b00000100)
and that would resolve to:
0b01000100

GhostScript and Margins

I had a postscript document that simply did not want to come out correctly, the offsets were such that the page content was going well off the visible area.

the following vile hack is one solution:

cat <<EOF>offsetfix.ps
%!PS-Adobe-2.0
<<
	/PageOffset [ 16 -52]
	/Margins [0 0]
	/.HWMargins [0 0 0 0]
>>
setpagedevice
EOF

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite \
	-dOptimize=true \
	-sOutputFile=output.pdf \
	offsetfix.ps input.ps

granted, one could cat the snippit in offsetfix.ps with the original document, but as I’m not sure what else may be using it, I’d rather not.

this also has the advantage of being a bit more portable, albeit rather ugly when compared to the original:

ps2pdf input.ps output.pdf
  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives