Hardware

TI-1200 Opdracht 8

I imagine a lot of people don’t know how to do this Java exercise, so here is some code :) Please do NOT copy it!!!

(I cannot redistribute the original assignment as this would be a breach of copyright. Please see blackboard here)

Adres.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Scanner;

public class Adres {

	//post: creert een nieuw adres, met de opgegeven waarden.

	private String straat;
	private String huisNummer;
	private String postcode;
	private String plaats;

	/** Create a new adres
	 * @param straat
	 * @param huisNummer
	 * @param postcode
	 * @param plaats
	 */
	public Adres(String straat, String huisNummer, String postcode, String plaats)
	{
		this.straat = straat;
		this.huisNummer = huisNummer;
		this.postcode = postcode;
		this.plaats = plaats;
	}

	//post: retourneert een String-representatie van dit adres
	public String toString()
	{
		return "<Adres<straat=" + this.straat + ", huisNummer=" + this.huisNummer + ", postcode=" + this.postcode + ", plaats=" + this.plaats + ">>";
	}

	//post: retourneert true als other hetzelfde adres is ( zelfde postcode en huisnummer) als dit.
	public boolean equals(Object other)
	{
		if(other instanceof Adres)
		{
			Adres other2 = (Adres)other;

			if(other2.huisNummer == this.huisNummer
				&& other2.straat == this.straat
				&& other2.plaats == this.plaats
				&& other2.postcode == this.postcode
			)
				return true;
		}
		return false;
	}

	//
	/** Create an address from a scanner string
	 * pre: sc bevat een rij tokens die een adres voorstellen
	 * post: retourneert een Adres, opgebouwd uit de waarden die sc teruggeeft
	 * @param scanner text containing the address
	 */
	public static Adres read(Scanner scanner)
	{
		// scanner text ==
		// Emmalaan 23
		// 3051JC Rotterdam
		String straat = scanner.next();
		String huisNummer = scanner.next();
		String postcode = scanner.next();
		String plaats = scanner.next();

        return new Adres(straat, huisNummer, postcode, plaats);
	}

}

Adres_test.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Adres_test {

	public static void main(String [] args)
	{
		FileReader fileString;
		Adres test;

		System.out.println("test = Adres.read(sc);");
		try {
			fileString = new FileReader("src/Adres_test_text.txt");
			Scanner sc = new Scanner(fileString);

			 test = Adres.read(sc);

			System.out.println(test);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("---");

		Adres test1 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");
		Adres test2 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");

		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));

		System.out.println("---");

		test1 = new Adres("Buitenhof", "20", "2513AG", "Den Haag");
		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));
	}
}

Woning.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Scanner;

public class Woning {
	private Adres adres;
	private int kamers;
	private int vraagPrijs;

	/** Create a Woning object
	 * post: creeert een Woning met de opgegeven waarden
	 *
	 * @param adres
	 * @param kamers
	 * @param vraagPrijs
	 */
	public Woning(Adres adres, int kamers, int vraagPrijs)
	{
		this.adres = adres;
		this.kamers = kamers;
		this.vraagPrijs = vraagPrijs;
	}

	//post: retourneert een String-representatie van deze woning
	public String toString()
	{
		return  "<Woning<adres=" + this.adres + ", kamers=" + this.kamers + ", vraagPrijs=" + this.vraagPrijs + ">>";
	}

	/** Check if the price is max
	 * post: retourneert true als vraagprijs = maxprijs
	 *
	 * @param maxprijs
	 * @return
	 */
	public boolean kostHooguit(int maxprijs)
	{
		return (this.vraagPrijs <= maxprijs);
	}

	//post: retourneert true als other dezelfde woning is (dwz hetzelfde adres) als deze.
	public boolean equals(Object other)
	{
		if(other instanceof Woning)
		{
			Woning otherWoning = (Woning) other;
			if(otherWoning.kamers == this.kamers
				&& otherWoning.vraagPrijs == this.vraagPrijs
				&& otherWoning.adres.equals(this.adres)
				)
				return true;

		}
		return false;
	}

	/** Read Woning from file
	 * pre: sc bevat een rij tokens die een woning voorstellen
	 * post: retourneert een Woning, opgebouwd uit de waarden die sc teruggeeft
	 *
	 * @param sc Scanner text input
	 * @return
	 */
	public static Woning read(Scanner sc)
	{
		/*
		 * Text ==
		 * Emmalaan 23
		 * 3051JC Rotterdam
		 * 7 kamers
		 * prijs 300000
		 */
		Adres adres = Adres.read(sc);
		int kamers = sc.nextInt();
		sc.next();
		sc.next();
		int price = sc.nextInt();

		return new Woning(adres,kamers,price);
	}

}

Woning_test_text.txt

Emmalaan 23
3051JC Rotterdam
7 kamers
prijs 300000

Woning_test.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Woning_test {

	public static void main(String [] args)
	{
		FileReader fileString;
		Woning test0;

		System.out.println("test0 = Woning.read(sc);");
		try {
			fileString = new FileReader("src/Woning_test_text.txt");
			Scanner sc = new Scanner(fileString);

			test0 = Woning.read(sc);

			System.out.println("test0:" + test0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("---");

		Adres adres1 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");
		Adres adres2 = new Adres("Buitenhof", "20", "2513AG", "Den Haag");

		Woning test1 = new Woning(adres1,10,99);
		Woning test2 = new Woning(adres2,10,99);

		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));

		System.out.println("---");

		Woning test3 = new Woning(adres2,11,99);
		System.out.println("test3:" + test3);
		System.out.println("test2:" + test2);
		System.out.println("test3.equals(test2):" + test3.equals(test2));

		System.out.println("---");

		Woning test4 = new Woning(adres2,10,99);
		System.out.println("test4:" + test4);
		System.out.println("test2:" + test2);
		System.out.println("test4.equals(test2):" + test4.equals(test2));
	}
}

Portefeuille.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Portefeuille {

	private ArrayList<Woning> woningen;

	/** Initialize new Portefeuille
	 *
	 */
	public Portefeuille()
	{
		this.woningen = new ArrayList<Woning>();
	}

	/** Add a woning
	 * post: voegt de woning toe - tenminste als hij er nog niet in zat
	 * @param woning
	 */
	public void voegToe(Woning woning)
	{
		this.woningen.add(woning);
	}

	/** Get a list of woning'n with a max price
	 * post: retourneert een lijst met alle woningen die maxprijs of minder als vraagprijs	hebben
	 * @param maxprijs
	 * @return
	 */
	public ArrayList<Woning> woningenTot(int maxprijs)
	{
		Iterator<Woning> iter = this.woningen.iterator();
		ArrayList<Woning> result = new ArrayList<Woning>();

		Woning temp;

		while(iter.hasNext())
		{
			temp = iter.next();
			if(temp.kostHooguit(maxprijs))
				result.add(temp);
		}

		return result;
	}

	/** Get a Protefeuille from file
	 * pre : infile is de naam van een tekstbestand waarin op bovenbeschreven wijze een aantal woningen staan
	 * post: retourneert een Portefeuille, die de woningen, gelezen uit dit tekstbestand bevat
	 *
	 * @param infile filename
	 * @return
	 */
	public static Portefeuille read(String infile)
	{
		/*
		 * SAMPLE TEXT==
		 * 3
		 * Emmalaan 23
		 * 3051JC Rotterdam
		 * 7 kamers
		 * prijs 300000
		 * Javastraat 88
		 * 4078KB Eindhoven
		 * 3 kamers
		 * prijs 50000
		 * Javastraat 93
		 * 4078KB Eindhoven
		 * 4 kamers
		 * prijs 55000
		 */

		Portefeuille result = new Portefeuille();
		FileReader fileString;

		try {
			fileString = new FileReader(infile);
			Scanner sc = new Scanner(fileString);

			int amount = sc.nextInt();
			for(int i = 0; i < amount; i++)
				result.voegToe(Woning.read(sc));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return result;
	}

	public String toString()
	{
		String result;

		result = "<Portefeuille<woningen:";
		Iterator<Woning> iter = this.woningen.iterator();
		while(iter.hasNext())
		{
			result = result + iter.next() + ",";
		}
		result  = result + ">>";

		return result;
	}
}

Portefeuille_test_text.txt

3
Emmalaan 23
3051JC Rotterdam
7 kamers
prijs 300000
Javastraat 88
4078KB Eindhoven
3 kamers
prijs 50000
Javastraat 93
4078KB Eindhoven
4 kamers
prijs 55000

WoningDriver.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Iterator;
import java.util.Scanner;

public class Woningdriver {

	public static void main(String [] args)
	{
		System.out.println("Wat is de maximale prijs?");

		Scanner sc = new Scanner(System.in);
		int maxPrijs = sc.nextInt();

		Portefeuille port = Portefeuille.read("src/Portefeuille_test_text.txt");

		System.out.println("-----");

		Iterator<Woning> iter = port.woningenTot(maxPrijs).iterator();
		while(iter.hasNext())
			System.out.println(iter.next());
	}

}

Tags:

Friday, December 3rd, 2010 Hardware, TU-Delft No Comments

Debian Lenny Rotating Screen without Drivers

Had to write this down somewhere..

/boot/grub/menu.lst
Kernel options

video=vesa:ywrap,mtrr vga=794 fbcon=rotate:3

VGA mode is described in

http://wiki.antlinux.com/pmwiki.php?n=HowTos.VgaModes

Then for Xorg server
/etc/X11/xorg.conf

Section "Device"
 Driver      "fbdev"
Option "Rotate" "CCW"

Tags: , ,

Monday, November 22nd, 2010 Hardware No Comments

Using PHP to read out a EMP800 Coin Collector

A friend at netcompany called me up a long time ago and said “Nick, I have this coin collector, I need to be able to read out its values in PHP!” so I naturally said “ok!” :)

This script works on CC-Talk enabled EMP800 coin collectors. Theoretically it should work on any CC-Talk enabled coin collector, but never been tested.

CoinCollector.php

<?php

/*********************************
* EMP800 USB
* Written/hacked-up by Nick ten Cate / nicktencate.com
*********************************/

class CoinCollector
{
	private $_handle;						 // Open Connection
	private $_device;
	private $_maxCoinType = 16;  // Maximum different types of coins to accept
	private $_eventCounter = 0;  // Event Counter

	function CoinCollector($device = "/dev/ttyUSB0", $deviceAddress = "2", $myAddress = "1")
	{
		$this->_device = $device;
		$this->_deviceAddress = $deviceAddress;
		$this->_myAddress = $myAddress;

		$this->setPortMode();
		$this->connect();
	}

	/********************************
	| Set Port Mode Correctly
	********************************/
	function setPortMode()
	{
		exec("chown root:root ".$this->_device);
		exec("stty -F ".$this->_device." -brkint -icrnl -imaxbel -opost -isig -icanon -echo");
	}

	/********************************
	| Initiate Connection
	| returns true/false
	********************************/
	function connect()
	{
		if($this->_handle = fopen($this->_device,"w+"))
		{
			return true;
		}
		else
		{
			throw new Exception('Could not open device'. $this->_device);
		}
		return false;
	}

	/********************************
	| Calculate Package CheckSum
	| $commands = array of integers
	********************************/
	function calcCheckSum($commands)
	{
		return 256-(array_sum($commands)%256);
	}

	/********************************
	| Send commands to coin collector
	| $commands = array of integers
	| returns # of commands done
	********************************/
	function sendCommands($commands)
	{
		// Add commands to send
		$c = array();
		$c[] = $this->_deviceAddress;    // Target Device Address
		$c[] = count($commands)-1;		   // Amount of DATA bits we are sending (excluding header bit (first one))
		$c[] = $this->_myAddress;		     // My Address

		foreach($commands as $x)		     // Add header bit + commands
		{
			$c[] = $x;
		}

		$c[] = $this->calcCheckSum($c);  // Add the checksum

		foreach($c as $command)			     // Send each command to device
		{
			if(!fputs($this->_handle,chr($command)))			// Convert to byte
				throw new Exception('Could not execute command: '. $command);
		}

		return count($c);
	}

	/********************************
	| Return results from coin collector
	| $count = amount of commands sent
	| return array of integers
	********************************/
	function getResults($count)
	{
		$results = array();
		$resultCounter = 1;
		$command = true;
		// Fetch first result
		// & fetch all others after it, until check sum completes.
		// This should be re-written, the 2nd byte is # of data being returned.
		do
		{
			$char = fgetc($this->_handle);	// Each Byte represents something. 1=Source, 2=Amount of Data, 3=Target, 4=Data1, 5=Data2, #=checksum

			if($resultCounter > $count)
			{
				$command = false;
				$results[] = ord($char);
			}

			$resultCounter++;
		} while($command 	|| $this->calcCheckSum($results) != 256);

		array_pop($results);	// Remove last byte, the checksum 

		unset($results[0]);	// Remove 'source address'
		unset($results[1]); // Remove 'amount of data'
		unset($results[2]);	// Remove 'target address'
		return $results;
	}

	/********************************
	| Run the commands and return the results
	| Return array of results
	********************************/
	function execute($commands)
	{
		$count = $this->sendCommands($commands);
		$results = $this->getResults($count);
		return $results;
	}

	/********************************
	| Get array of coin types and ids
	| Return array KEY=id, VALUE=coin
	********************************/
	function getCoinTypes()
	{
		$results = array();
		for($x = 1; $x < $this->_maxCoinType+1; $x++)
		{
			$result = $this->execute(array(184,$x));
			$string = "";
			foreach($result as $xx)
			{
				$string .= chr($xx);
			}
			$string = str_replace(" ","",$string);

			unset($t);
			$t = array();
			$t["currency"] = substr($string,1,2);
			$t["value"] = round(substr($string,3,3)/100,2)*1.00;
			$t["denomination"] = "1";
			$t["coinId"] = $x;

			$results[$x] = $t;
		}
		return $results;
	}

	/********************************
	| Set coinIds open
	| (send command + binary bytes->int for each id)
	********************************/
	function allowCoinEntry($array = 1)
	{
		$binary = array();

		if($array == 1)
		{
			$binary[] = "255";
			$binary[] = "255";
		}
		else
		{
			// Make empty array
			$settings = array();
			for($x = 1; $x < $this->_maxCoinType; $x++)
			{
				$settings[$x] = "0";
			}
			// Transform into correct array
			foreach($array as $key => $x)
			{
				$settings[$key] = $x;
			}

			// Create binary
			$tempBinary = "";
			foreach($array as $key => $x)
			{
				if($key%8 == 0)
				{
					$binary[] = bindec($tempBinary);
					$tempBinary = "";
				}

				if($x == 1)
					$binary .= "1";
				else
					$binary .= "0";
			}
		}

		$commands = array_merge(array('231'),$binary);

		$this->execute($commands);
	}

	/********************************
	| Poll for coins
	| Return array KEY=coinId VALUE=# of coins
	********************************/
	function poll()
	{
		$results = $this->execute(array(229));
		//Results Array Definitions
		// 4  = event counter
		// 5  = Event A Key ==> 0 (ERROR) || 1 - $_maxCoinType (COIN ID)
		// 6  = Event A Value ==> Error Id  || 0
		// 7  = Event B Key
		// 8  = Event B Value
		// 9  = Event C Key
		// 10  = Event C Value
		// 11  = Event D Key
		// 12  = Event D Value
		// 13  = Event E Key
		// 14  = Event E Value
		if($this->_eventCounter > 0)
			$diff = $results[4] - $this->_eventCounter;
		else
			$diff = 0;

		$coins = array();
		for($x = 1; $x < $this->_maxCoinType; $x++)
		{
			$coins[$x] = 0;
		}

		for($x = 0; $x < $diff; $x++)
		{
			$coinId = $results[(5+(2*$x))];

			// Add to coin amount
			$coins[$coinId] = $cois[$coinId] + 1;
		}

		// Set Event Counter
		$this->_eventCounter = $results[4];
		return $coins;
	}

	function debug($text)
	{
		echo "DEBUG:: $text \r\n";
	}

	/********************************
	| Called when coin is put
	| $coins = array of key=coinId, value=# of coins
	********************************/
	function onCoinEvent($coins)
	{
		// Do something
		echo "Got Coin(s)! ";
		foreach($coins as $k => $v)
		{
			if($v > 0)
			{
				echo $v."x".$k."-";
			}
		}
		echo "\r\n";
	}

	/********************************
	| Calculate Total Value
	| Return total value
	********************************/
	function calculateValue($coins, $coinTypes = false)
	{
		if($coinTypes == false)
		{
			$coinTypes = $this->getCoinTypes();
		}

		$total = 0;

		foreach($coins as $coinId => $amount)
		{
			$total += $coinTypes[$coinId]["value"] * $amount;
		}
		return $total;
	}

	/********************************
	| Poll for coins
	| Return array of coins
	********************************/
	function pollTimeOut($time = 5)
	{
		$originalTime = $time;
		$totalCoins = array();

		for($x = 1; $x < $this->_maxCoinType; $x++)
		{
			$totalCoins[$x] = 0;
		}

		while($time > 0)
		{
			$runAmount = 3;
			for($x = 0; $x < $runAmount; $x++)
			{
				$coins = $this->poll();		// Poll for coins
				if(array_sum($coins) > 0)	// Accepted Coin
				{
					$this->onCoinEvent($coins);

					$time = $originalTime;	// Reset Timeout
					foreach($coins as $k => $v)	// Add-up coins
					{
						$totalCoins[$k] += $coins[$k];
					}
				}

				usleep((1000000/$runAmount)); // Wait for 500ms
			}

			$this->debug("Time is: $time");
			$time--;
		}
		return $totalCoins;

	}

}

?>

Heres a quick implementation

<?php

include("CoinCollector.php");

$collector = new CoinCollector();

$a = $collector->getCoinTypes();

print_r($a);

$coins = $collector->pollTimeOut(10);

print_r($coins);

echo "Total Value \r\n";
echo $collector->calculateValue($coins);
echo " EURO";

?>

Tags: , , , , ,

Wednesday, October 13th, 2010 Hardware No Comments

Installing Computer-Modules’ VidPort SD/HD™ on Debian Lenny

So last friday I received 2 VidPort SD/HD cards in the mail. They come at a very reasonable price compared to some other HD-SDI input/output cards out there!. Heres a quick ‘howto’ to get this card working on linux (debian 5/lenny).

Get your linux headers and build tools

apt-get install build-essential linux-headers-$(uname -r)

Download, Compile, and load card modules

wget http://www.computermodules.com/drivers/master-2.7.1.tar.gz
tar -xvvf master-2.7.1.tar.gz
cd master-2.7.1
make
make install
modprobe hdsdim

If you look at your dmesg, you should see the card loaded.

dmesg
[  393.979657] PCI: Setting latency timer of device 0000:02:00.0 to 64
[  393.979657] hdsdim: VidPort SD/HD I detected, firmware version 1.0 (0x0100)
[  393.979657] hdsdim: registered board 0
[  393.979657] sdivideo: registered receiver 0
[  393.979657] sdiaudio: registered receiver 0

Now, if you wish to use it, load a transcoding program like ffmpeg

./ffmpeg \
 -f rawvideo -s 1280x720 -pix_fmt uyvy422 -r 60 -i /dev/sdivideorx0 \
 -f s16le -ar 48000 -ac 2 -i /dev/sdiaudiorx0 \

(ofcourse you still have to specify output..)

Tags: , , , ,

Tuesday, October 12th, 2010 Hardware No Comments