onsdag den 9. november 2011

Adding an USB hub to keyboard

So long time a go, I saw this keyboard with an usb hub build in, I think it was apple that had made it, I really can't remember it. But anyway I wanted a keyboard with an usb hub buildin, not that it would make my life easier but because then I would have one less thing on my table.

I have two usb hub, one with an external power supply and one with out the external power supply, but this one has tree port in front, and one in the back, which suites the purpurs of this mod, just fine.
So the plan is use the one in the back for the keyboard, an the tree others for other usages.

    Keyboard:    Logitech Internet 350 USB Keyboard

    USB Hub:    Targus USB Hub ACH93EU

The Plan
I have put my plans in to some steps:
  1. Open the usb hub and the keyboard
  2. Disconnect the usb cable from the keyboard
  3. Disconnect the usb cable from the hub and disconnect one of the usb ports.
  4. See where the usb hub pcb can fit in the keyboard.
  5. Drill/cut 3 ports into the keyboard's plastic.
  6. Wire the keyboard directly to the disconnected port on the usb hub
  7. Wire the keyboards old usb cable to the usb hub.
  8. Check if it works
  9. Close it up
So this is the plan, and now it is the time for implementing it.
First I open the usb hub and found this:

Take note of where the wires are connected (taking an pictures, is a good way to do this), as we need to make sure that they connect. This is done because some of the connections can be altere as of the PCB design. Now to solder one of the usb header, power connector and the wires off, like this:

Now to take the keyboard apart. As you properly can see from the pictures above I took both the usb hub and the keyboard apart at the same time. In this keyboard there is a lot of screws, it is almost silly that there is so many screws. There is two screws under the keys "Caps-lock" / "Tab" (you need to remove those two to get to one screw) and one screw under the "+" on the numberpad. On the bottom there is, about 18 screws, I told you that is silly how many screws there is. Remove those screws.
Now you should be able to open the keyboard, there you can see the workings of the keyboard, the one thing we are interested in is the PCB where the usb connection are connected to.
Again take note of where the wires are connected, are going to remove the wires:

After removing the wires, remove the silicone buttons. Now it is time to lay out where the usb hub, is going to be:
When you have found the place where, you'll want to have the usb switch, then mark where the usb ports are, and start cutting about half of the usb ports. Here I will suggest that you use an dremel cutting tool or something like it. I used an knife, and it took a long time and I got some cuts on my fingers. 
After being finished with the bottom half, then proceed to the top half, and mark an cut it.

Then it is time, to add the new wires. My first thought was to use the usb cable from the usb switch, but I was'nt able to re solder them on, not with out some of the wires, pulling out when soldering a wire. So I did use some scrap wires, that I had lying around, for the usb connection for the usb keyboard.

As there can be seen on the last picture above, I have already solder the usb cable from the usb keyboard on the usb switch, that cable was a better quality than the one from the usb switch.
Some pictures of the finished product in use:

It works, quite well, but I have noticed that it gets hot, where the usb switch is placed, so perhaps I'll have to do something about it at some point.

  [1] USB Pinout or USB Cable Pinout

tirsdag den 8. november 2011

python and download an file

So when you are programming/scripting in python, there comes a time when you want do get python to download a file.
This is more or less really good documented on the web, not really from the official python documentation.
So there is two libraries to designed to handle web request (and there is at least one to handle socket request). So the two libraries handle is called urllib and urllib2, the one I would suggest at least for web downloads of and file or a web page.
So to make an simple start:
import urllib2
url = "http://www.python.com"
request = urllib2.Request(url)
urlobj = urllib2.urlopen(request)
readpage = urlobj.read()
print readpage
So first we import the urllib2 library into python... yeah I know that you properly know this, but humor me.
The next I have defined url that I would like to download this is http://www.python.com, why I wanted to download this web page is to show it is possible.
So make an request for this url (urllib2.Request), and after that we going to make an url object with urllib2.urlopen. When it is done, we can read the web page with urllib2.read and we are going to print it out in the terminal.
So this is fine if we wanted to just print it out to the terminal, but what if we want to write this to a file. So we are going to create an new script, which have almost the same:
import urllib2
import shutil
url = "http://www.python.com"
fname = "python.com.html"
request = urllib2.Request(url)
urlobj = urllib2.urlopen(request)
with open(fname, 'wb') as f:
shutil.copyfileobj(urlobj, f)

And when we open this file in an editor, like gedit we see something like:
So, we open our local file "python.com.html" using the python open function using the 'wb' statement, so the 'w' is writeable, and the 'b' is binary mode. To read more on the built-in open function see reference no.1, and we are using the with statement to open it our local file, I'm not going into depth on the with statement, but effbot.org has already done this, see reference no.2 to read up on it.

Creating a class to handle our download
okay, now our code works to download a file, but if we are going to download more than one file, it can be useful to put it in an function, or make an class that can handle all the downloads.
If you are writing on an bigger script, where you need to download more than one file. So if you need to read up on python classes see reference no. 3. Create a file called "DownloadFile.py" and here is the code:
from urllib2 import Request, urlopen, URLError, HTTPError
from shutil import copyfileobj
from os.path import isfile, exists

class DownloadFile:
def __init__(self, url, tofile):
self.link = url
self.filename = tofile
self.urlobj = None

def __connect(self):
returncode = True
request = Request(self.link)
self.urlobj = urlopen(request)
except URLError, e:
 # There probably need some code, to
 # handle  error code
returncode = False
except HTTPError, e:
# There probably need some code, to
# handle  error code
returncode = False
return returncode

def __write(self):
with open(self.filename, 'wb') as f:
copyfileobj(self.urlobj, f)
def run(self):
if not exists(self.filename) and not isfile(self.filename):
gets = self.__connect()
if gets:
return "Downloaded"
return "Failed"
return "Exists"

def info(self):
if self.fileobj is None:
return self.urlobj.info()

if __name__ == "__main__":
url = "http://meganfoxfans.net/wp-content/uploads/2011/10/Megan_Fox_Picture-coffee.jpg"
lname = "Megan_Fox_Walking_with_Coffee.jpg"
print "Will try to download a picture of Megan Fox:",
print DownloadFile(url,lname).run()
To run it just do like this:
python /path_to_file/DownloadFile.py
It will return something like
"Will try to download a picture of Mega Fox: Downloaded"

That is it for now, hope it can be useful for somebody :D And if you can use it, and you meet me someday, just remember to buy me a beer ;)

[1] Python open function
[2] Python with statement
[3] Python classes

mandag den 7. november 2011

XBMC and packaging

So I've got this media centre, which is in reality just an "old" Intel motherboard, with an Intel Atom processor and 2 GB ram.
About two weeks ago, I reinstalled XBMC  on it, at first I was running an gentoo with XBMC installed on top. I really liked it, because it was an minimal installation, but with me an gentoo which I always fuck it up. But that's life, always updating the system when I don't have the time to take care of it.

So this time I installed the live cd provided  from the xbmc-live team, nice and easy to run and install. But this is not the neweste version of the xbmc media centre, but again xbmc, has a packaging team, I must say that I think that they have done a fine job of making the scripts user friendly.
But there is not any instructions on how getting started with these scripts or how to setting an build environment up.

So there is many ways to setting the build environment up, but as far I can see there is two methods to do this. The first method is to create an new user, which handles all the building of the xbmc packaging, the other way is to use your xbmc user to build and package the files.

I always ssh into my media centre, so I will assume, if you want to follow this, that you also do this.

So here is the first way to do this:

This way we create an new user, you can call it what you want, but I have called it "buildd". So when you have login in to your xbmc media centre.
xbmc@XBMCLive ~: sudo adduser buildd
Just enter all the informations that it will ask about. This will create an user home directory in "/home", this will become our build environment.
The next we want to do is give our new user superuser access, so as I just assumes that we are on an debian system (like the xbmc live cd is), so now we are going to modify sudoers.
xbmc@XBMCLive ~: sudo visudo
In that file, you have to be careful on what you are doing, because it can remove your user from the sudo list, which is not good. But just add this at the end of the file:
## lines to be added in order to build XBMC
(be aware that this command is not secure, it will grant root access for anybody that is login as buildd)
After this we are going login as the new user:
xbmc@XBMCLive ~: su buildd && cd
and do the rest from the new user. The next thing we need to do is setup an GPG key, for signing the packages that we compiles and build.
buildd@XBMCLive ~: gpg --cert-digest-algo=SHA256 --default-preference-list="h10 h8 h9 h11 s9 s8 s7 s3 z2 z3 z1 z0" --gen-key
And this is how I done it, you can self take an look at Ubuntu community GnuPrivacyGuardHowto
  • Select it the one that is "RSA (sign only)"
  • The default key size should be 2048 if not the choose 2048
  • Then enter your name, e-mail, comments according to the buildd user
  • And I have not entered an password
So right about now it could be useful to install git and pbuilder, as of we are going to use them in the next section.
buildd@XBMCLive ~: sudo apt-get update && sudo apt-get install git-core pbuilder
When done installing, we want setup some directories for our build environment
buildd@XBMCLive ~: mkdir -vp {tmp,pbuilder/{aptcache,build,result}}
buildd@XBMCLive ~: sudo rm -rf /var/cache/pbuilder
buildd@XBMCLive ~: sudo ln -s /home/buildd/pbuilder /var/cache/pbuilder
buildd@XBMCLive ~: sudo ln -s /home/buildd/tmp /tmp/buildd

An now, the build environment is done, then we want to clone the xbmc/xbmc-packaging repository
buildd@XBMCLive ~: git clone git://github.com/xbmc/xbmc-packaging.git
buildd@XBMCLive ~: cd xbmc-packaging
Now before we can compile xbmc, we need the source for it. You can setup your own clone repository for xbmc, but there is an script that can handle that for us, so that is we want to do. This script can get the stable source or the development (unstable) source code, by setting the variable "--stable" after the script. To get the development version:
buildd@XBMCLive ~/xbmc-packaging: ./xbmc-get-orig-source
stable version:
buildd@XBMCLive ~/xbmc-packaging: ./xbmc-get-orig-source --stable
pbuilder calls its compressed build environment for images, so lets us create an image. As of writing the newest version of ubuntu is not supported (oneiric), but the it is fortune that XBMC live is lucid, and that is what we are going to make packages for. The xbmc-packaging repository provides us with an script that handle all the pbuilder commands and etc. So to first we need to create and build image for pbuilder.
buildd@XBMCLive ~/xbmc-packaging: ./pbuilder-dist lucid create
This creates the "build environment" for us, and it is going take some time to do it. It will create an chroot environment, which is an nice way to say that is going to download a lot of packages. If you want to know more about chroot environments, you can search google, or take a look at the ubuntu documentations.
When it is done with downloading the packages an compressed the image, we now can begin to build our first package (first make sure the there is an *.dsc file in the directory)
buildd@XBMCLive ~/xbmc-packaging: ./pbuilder-dist lucid build xbmc-11.0.dsc
Now this is also going to take some time, so watch some tv, drink some coffee, the, beer or cola pick your poison. Now when the script is done, the resulting package should be in the "/home/buildd/pbuilder/results/" directory.
This package you should be able to install using the dpkg, with the install switch.
buildd@XBMCLive ~/pbuilder/results: sudo dpkg -i *.deb
And it now you should have the newest compiled package installed. But now you are thinking about setting up your own ubuntu repository, and this will not be coveret at this time.
The next time, I will show you how do the same but without create an new user.