Posts tagged ‘Examples’

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’ »

concatenate (join together) several *.pdf files

pdf is a document format. there are occasions where I find it easier to work with one large document rather than several single documents (most recently, while working with several spec sheets for electronic components), the following command will join two or more *.pdfs into a single document using ghostscript.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT01.pdf INPUT02.pdf ...

-dBATCH - exit after last file
-dNOPAUSE - no pause after page
-q - “quiet”, fewer messages
-sDEVICE=<devname> - select device, in this case the pdf writer
-sOutputFile=<file> - select output file

the following were not, but could be, included:
-g<width>x<height> - page size in pixels
-r<res> - pixels/inch resolution

Animated Flag

an example of the cloth and wind effects available in Blender 2.45

Blender just keeps getting better…

converting *.pdf to an image series

pdf is a document format. on the occasion where it is easier to reference an image rather than the full document, the following command will convert the pdf.

convert -density 96x96 -trim INPUTFILE.pdf OUTPUTFILE.jpg

-density is the image density in pixels per square inch
-trim will trim the image edges

the output will be in the form of OUTPUTFILE-#.jpg, where # is an increasing “page” number.
this command will also work for OUTPUTFILE.png, OUTPUTFILE.gif, et. al..

also, imagemagick uses ghostscript to convert, so the quality of the final images will only be as good as ghostscript can provide.

Converting *.flac to *.mp3

flac is an audio format (Free Lossless Audio Codec) that is excellent for archiving, but few if any portable players will be able to play it, so a method to convert to mp3 is shown below.

convert .flac to .mp3

flac -cd "FILENAME.flac" | lame -h - "FILENAME.mp3"

flac: -c is output to console, -d is decode
lame: -h is shorthand for “-q 2″ and -q is quality (in this case a high quality), “-” is input from console

same but for all .flac files in a directory

for FILE in *.flac; do $(flac -cd "$FILE" | lame -h - "${FILE%.flac}.mp3"); done

same options in a bash for loop, the ${FILE%.flac} strips “.flac” from the name

Notes:
id3 options for lame:
–tt <title> –ta <artist> –tl <album> –ty <year> –tc <comment> –tn <track> –tg <genre>
will automatically add an id3v1 or id3v2 as needed (choice is based on the size of the fields),
“–add-id3v2″ “–id3v2-only” “–id3v1-only” can be used to modify choice.

more complicated operations are beyond the scope of this article, but I’d suggest using K3B.

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

Soccerball

A rendering of a soccerball.

soccerball.jpg

soccerball.gif

Random Content in Wordpress

this example is for WordPress, but the concepts should work fine anywhere.

a quick note, though they are not in the code examples, it is important that the code go between <? and ?> bits. any syntax highlighting editor should make it plain where they are needed.

 

the simplest version of this would be to add the following to your template where you want the random content to show up:

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"just some text",
	"some more text",
	"yet more text",
);

$key = array_rand($content);
echo( "<p>" . $content[$key] . "</p>" );

 

to turn this into a function available throughout the theme, add the following to the end of the “functions.php” (it is in your theme folder).
if this file does not exist, you can create it.

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"just some text",
	"some more text",
	"yet more text",
);

function get_random_content() {
	global $content;
	if( is_array($content) ) {
		$key = array_rand($content);
		return $content[$key];
	}
	else {
		return "";
	}
}

function echo_random_content() {
	echo( "<p>" . get_random_content() . "</p>" );
}

this will allow you to add the following to any spot in your templates:

<? echo_random_content(); ?>

or better yet, wrap the call in a check:

<? if( function_exists("echo_random_content") ) { echo_random_content(); } ?>

 
 

if you want to extend this idea further, for example into different “databases” of content:

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"text" => array(
		"just some text",
		"some more text",
		"yet more text",
	),
	"other" => array(
		"just some other text",
		"some more other text",
		"yet more other text",
	),
);

function get_random_content($from) {
	//this is the default if no "$from" is set
	return get_random_content("text");
}

function get_random_content($from) {
	global $content;
	if( is_array($content) ) {
		if( isset($quote[$from]) ) {
			$key = array_rand($content[$from]);
			return $content[$key][$from];
		else {
			//this is the default if "$from" does not exist
			$key = array_rand($content["text"]);
			return $content[$key]["text"];
		}
	}
	else {
		return "";
	}
}

function echo_random_content() {
	echo( "<p>" . get_random_content() . "</p>" );
}

all that has been added is another dimention to the array, some error checking, and the ability to select the array to draw from.

this version will allow you to add the following to any spot in your templates:

<? echo_random_content(); ?>
<? echo_random_content("text"); ?>
<? echo_random_content("other"); ?>

or even better:

<? if( function_exists("echo_random_content") ) { echo_random_content(); } ?>
<? if( function_exists("echo_random_content") ) { echo_random_content("text"); } ?>
<? if( function_exists("echo_random_content") ) { echo_random_content("other"); } ?>

 

the code still does not check for the possibility of empty or erroneous content, but if you create a variable in echo_random_content() and use it to hold the results of get_random_content(), you can check if it is empty, contains bad characters, swap newlines out for break tags, or whatever else you would like.

Extracting RPM Package Contents

firstly, there is a package available called “alien” that will convert to/from packages for nearly any distro, I use this on machines that it can be installed on.

however, there are times that using standard tools is faster and simpler,

piping the output of “rpm2cpio” (extract cpio archive from RPM Package Manager (RPM) package.) into “cpio” (copy files to and from archives) will extract the RPM package into the current directory.

rpm2cpio package.rpm | cpio -dimv

where:
-d = make directories, -i = “in” mode, -m = keep date/time stamps, -v = verbose.

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

Eagle3D Component - LCD Module

this is a rendering of an Eagle3D component in progress, an LCD module that is part of a set of several text-based modules.

LCD_16×2-top

as requested, the required files:
HD44780 Based LCD Modules Library (lcdmodules.lbr)
user.inc
user_colors.inc
user_lcd.inc
alphalcd.ttf

the lib file should go in your eagle “lbr” directory, and the inc and ttf files should go in your eagle3D “povray” directory.

a word of warning, thes are in pretty rough shape, but usable.
the include files follow the proposed rules in this post, so if you have made any changes to your user.inc, be sure to merge the files.

alternately, you can simply add the following lines to your user.inc:

#include "user_colors.inc"
#include "user_lcd.inc"

happy designing, and any comments or suggestions are welcome.

An Animated Yin-Yang

Rendering of a printed circuit board

Rendering of a printed circuit board

First decent version


with the full version here.

Renderings of the Puzzle-Box From the Hellraiser Series

an initial rendering to check for lighting and placement


 
 
after rebuilding and cleaning the meshes (and a few tweeks on lighting)

 
 
more streamlining of the meshes and animation, and if it were constructed of aluminum.

 
 
the textures are tedious, excessive time must be spent either setting them up within Blender, or designing them outside of it.
given that it is far too easy (for me at least) to do something horribly wrong while editing the maps within Blender, I chose a method using the “excessive design” approach. the map specifics are exported to SVG, then edited, then converted to PNG via a script for Blender to import.
I’ve also divided the meshes a bit more, and added more animation.

a Flash dice roller

a Flash dice roller using custom-rendered virtual dice.
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

of course, this could be customized to match a particular sort of roll and style of game.
the next version contains the ability to set all of the attributes from the HTML page, and to send the results back to it’s containing HTML page.

for example, in firewater productions’s game “Chaos University”, the standard is 5 dice:
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives