Friday, August 27, 2010

Nothing really technical

In three week days I'll be working for a month in Espeo Software. So I guess some summary would be in place.

I sure have enjoyed working there and I think I have spent it well. I did learn a lot in that time, probably more than I usually learn in a month of class.

Android robot logo.Image via Wikipedia

I have been working with my collegue Kris on an app for Android. I intent to continue my adventure with Android development (waiting for new Android phone ATM). >]
I have also had a chance of diving into Groovy(augmented, dynamically typed Java-like language that runs on JVM) and Grails code (web framework on Groovy) and I intend to swim there a bit more as well.


By the way, I have been hearing some stuff about new Java version. If it's going to be even just a little more like Groovy - me likey !
By the way2, if you feel awkward with dynamically typed languages, as I am a little bit, but want the power of Groovy for yourself you might want to check out Groovy++.

There has been a poll posted on my blog for a while. An outstanding number of 10 people has voted in it (yes, including me). I'd like to thank everyone for visiting my blog and voting. =)
The poll was about programming languages you are 'mostly into' and the results are :


I have voted for Haskell, but at the moment I do not work on any piece of code in Haskell. One of the reasons is that the integration of Haskell code with Java code is a pretty tedious thing to do. My boss has recommended me Scala as a functional language that integrates with Java very well. Who knows, it might be just the thing to check out in some less eventfull times.
Currently I'm mostly into Java !
I expect 'other...' to be some .Net language (or not a programming language at all ;] ) which is quite popular with people I'm studying with.
Quite a suprise is that more votes ( 40% vs 20% ) are on C/C++ than Java.
Ofcourse that big sample is not really a sample and any reasoning based on it is pretty... lame. ;)

Monday, August 16, 2010

Android Activity.managedQuery selection and selectionArgs arguments

Most of the examples of managedQuery don't use the selection and selectionArgs arguments, others are pretty simple like :

Cursor simple(String id) {
return managedQuery(getIntent().getData(), PROJECTION,
 "id = " + id, null, DEFAULT_SORT_ORDER);
}

or

Cursor simple(String id) {
return managedQuery(getIntent().getData(), PROJECTION,
 "id = ?", new String [] ={id}, DEFAULT_SORT_ORDER);
}
Both of the above do exacly the same thing.
The '?'s are the elements in the array in order they are placed. [Resource]

I was working on a piece of code that stored IDs in SharedPreferences and then loaded the elements with those IDs into a ListView from a ContentProvider.

Basically I was trying to get that :
WHERE id IN ( id1, id2, id3, id4,...)

So the selection argument looked like that :
"id IN ( ? " +  [",? "] + " )"
and the selection args stored the ID of elements I wanted to load.

SharedPreferences data = getSharedPreferences("elementList",0);
Map <String, Integer> map = (Map<String, Integer>)data.getAll();
if (map.size() == 0) {
    return;
}
int i = 0;
Iterator it = map.entrySet().iterator();
String str [] = new String [map.size()];
while ( it.hasNext() ) {
    Map.Entry<String,Integer> pair
        = (Map.Entry<String,Integer>)it.next()
    str[i++] = pair.getKey();
    Log.i(this.getLocalClassName(), "pair key " + str[i-1]);
}
String whereClause = Prod._ID + " IN ( ?";
if (map.size()>1) {
    for (int j = 1 ; j < map.size(); ++j) {
        whereClause+=", ?";
    }
}
whereClause+=")";
Log.i(this.getLocalClassName(), whereClause);
Cursor cursor = managedQuery(getIntent().getData(), PROJECTION,
    whereClause , str, DEFAULT_SORT_ORDER);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    this, R.layout.element_list_item,
    cursor, new String[] { NAME, DETAILS},
    new int[] { android.R.id.text1, android.R.id.text2 }
);
elemList.setAdapter(adapter);

Hope that not-that-trivial example make this topic more clear. If you would like to know more about ContentProviders or managedQuery I suggest you try this or the Notepad Tutorial.

Monday, August 9, 2010

Android Virtual Machine and non-ASCII characters the path

I have found a job ! =)

For a month (and hopefully more) I'll be working at Espeo Software. I've started last Wednesday and I've really enjoyed it so far. Currently I'm working on an android app. During its developement I've encountered a bug when trying to launch the AVM on Windows with a profile containing the letter 'Ł'.

This is the message I got :
ERROR: no search paths found in this AVD's configuration. Weird, the AVD's config.ini file is malformed. Try re-creating it.

It appears that while the sdk can be in a folder with such a path that contains non-ASCII character the emulator does not like it. Specifically to .android/avd which on default is stored in your windows user folder.

The solution is to add a local user/system variable ANDROID_SDK_HOME with the path to a place you want .android to be. For example ANDROID_SDK_HOME=C:\AndroidDev.



You might have to restart your Eclipse or other IDE after doing that. But that fixed it for me.


I have still got to resolve some problems with SpringSource Tool Suite and they way it sometimes breaks my xml files or considers proper xml files as buggy. Maybe switching to a standard Eclipse would be the solution.

Monday, July 26, 2010

Mockups - Free tools

NetBeans E-commerce Tutorial shows how to use mock-ups but not how to make them.

Obviously you could use a piece of paper and a pen but there are also some software solutions, both commerce and free.

I have used a free one - Pencil to mimic one of the NetBeans E-commerce Tutorial's mockup. Here's the result of my roughly 15 minute work :


Looks a lot better than if I'd draw it by hand, but is the extra effort worth the result ?

My friend says no. He says that there is software for prototyping webpages, even browser-apps, and some people say that for prototyping the best thing is powerpoint.

Me, I'm not sure. If its for me, I'd stick with a paper and pen. For showing other people I'd probably use one of the mentioned tools.
Not because I draw so badly but because some of aspects of the mockup are complemented by things in noted your mind, like how the colors will be. It's easier to show them with a software tool to other person than with a piece of paper.

What do you think ?

The NetBeans E-commerce Tutorial

For some time now I've been looking into Java EE. Again, first step into the unknown is the hardest.

After getting the new Netbeans (6.9) I have stumbled upon The NetBeans E-commerce Tutorial.
I think it is a great way to learn some aspects of Java EE and how to develop commercial software/webpages.

The tutorial shows how to setup a simple website for a grocery shop, that allows people to order their products. It shows how to eficiently use the IDE (NetBeans) and MySQL workbench to connect the database (MySQL) to the business logic (Java) and the webpage (JSP, HTML, CSS).

It also shows the whole process of developing a commerce application starting with a scenario, gathering customer's requirements, use-cases, mockups and so on. The code part is based on a design pattern called Model-View-Controller, which is explained in the tutorial but if you'd like to know more here's a good resource.


Over the next few posts I'd like to show how to do the above but using a PostgreSQL and pgAdmin III.

You can download PostgreSQL as well as the admin tool here. I have used a standard setup but included jdbc4 for installing.

You start by doing exacly what the tutorial says until "Communicating with the Database Server".
At this point you copy the postgresql-x.x.-xxx.jdbc4.jar file to the {glassfish}/lib directory.
Now you should restart glassfish server by :
  • going to services [tab]->servers->glassfish 3  right-clicking and selecting restart (or start if its offline),
  • or asadmin stop-domain domain1
    asadmin start-domain domain1
After that you right-click on the services [tab]->databases. Choose New Connection. Select PostgreSQL as a driver and fill out the rest of your form with necessary data. For me it looked like that :

 Sorry about the language on the right side but I was unable to change it to english. I think that the icons are explanatory though.
After you accept you will be prompted to choose the schema. Note that in my case I was unable to change the default schema after creating the connection (despite the option) so you might want to create one beforehand in pgAdmin.

After connecting you should be able to create tables, views and procedures in you database by Netbeans.

If it does not you might find some answers on this site which I used to figure it out in the first place or just post a comment here/mail me.

Soon I'll post how to create the necessary tables from Designing the Data Model.

Thursday, July 1, 2010

New things !

As I am past all my exams and very glad of it I'm tending to my much neglected blog.

The summer has finally started for me but I'm looking for work. The goal is a position of a programmer in one of the companies in my city. Today I've finished my CV and sent it to a friend for checking. If he approves then I'll send it to every interesting company I'll find. I recon a month should be enough to find something.


As you can see I've changed the design of my blog and adjusted the code displaying script. It's a simple template from Blogger, but still I like it very much.


Lastly our little robo-project has been compleated. The final stage was to remove the parts responsible for communication with the PC, add a microcontroller and program it.

We have put in a Atmega 88 20pu microcontroller and wrote a simple active-polling program, compiled it with avr-gcc and then used  USB AVR ISP I and avrdude to write it to the microcontroller.

The code was written in c using avr libc.

Avr dude gui, a program providing gui for the avr programmer, proved to be buggy - didn't write data to the microcontroller.

You can watch the video of the whole operation here :




A little note : the beginning is shot on the junkyard; one of the objectives was to make the robot cheap. To be honest, in the end we didn't actually use any of the parts salvaged from there.

Tuesday, May 4, 2010

Devil lies in details

I've just spent an hour looking for an error not letting me compile some c++ + openGL code on Visual Studio. The problem occurred after adding GLM library code(glm.h) from glut's demo programs. After triple-checking my usage of the library, googling my error ("unresolved external symbol "), trying on different project I've finally found out the source of the problem. Pretty silly to be honest.

Everything compiled until I used a function from the library - it couldn't be resolved.

The problem was that the library was in C, while my code was C++. Even though the glm.h + glm.c compiled, trying to call a function defined in glm.h was not possible because the compiler tried to find it in glm.cpp.

Changing the name to glm.cpp and minor c->c++ changes fixed the problem.

If you're interested, you can find glut source and demo code here. The library for loading .OBJ model files is in progs\demos\smooth.

Another thing you might find interesting is this Pearl script converting OBJ files to arrays hard-coded in a header file. I haven't tried it out in practice because I use very big models.

Wednesday, April 21, 2010

Blog changes and haskell exercise

Hey there ! Just as I've promised I've came up with some Haskell exercises (with answers). To do that I had to post some code, which is not really convenient on Blogger. Fortunately there is a solution ! What I've found on the web is easy syntax highlighting for blogger.
Enjoy !


So now with the exercises :

1. Pretty standard problem - find all prime numbers up to a number given in the argument.
2. Function that takes a function (two numbers as arguments) and a list of numbers and returns a list of those two argument functions but already applied to numbers in the list given. That way we gain a list of functions taking one number.
3. Find all automorphic numbers up to a given number.
4. Rewrite the following functions so they use (.) operator :
one = sum ( takeWhile (<10000) (filter isPrime  [1..]) )
two = map (\x -> cos (sqrt x))

Solutions:
1.
allPrime :: (Integral a) => a -> [a]
allPrime range = filter isPrime [2..range]

isPrime :: (Integral a) => a -> Bool
isPrime x = not (isNotPrime 1 x)

isNotPrime :: (Integral a) => a -> a -> Bool
isNotPrime dv nr
    | dv == nr = False
    | dv == 1 = False || isNotPrime 2  nr
    | otherwise = nr `mod` dv==0 || isNotPrime (dv+1) nr
2.
partialL ::( Num a ) => (a-> a -> b) -> [a] -> [ a->b ]
partialL _ [] = []
partialL f (x:xs) = f x : partialL f xs
Usage :
let bla = partialL (\x -> \y -> x+y) [1,2,3,4]
map ($ 2) bla
[3,4,5,6]

3.
automorphic :: Int -> [Int]
automorphic y = [ x | x <- (take y [1..]), show x == (splitter x) ]

splitter :: (Num a) =>  a -> [Char]
splitter x = snd (  splitAt ( length (show sq) - length (show x) ) (show sq)  )
 where sq = x^2

4.
one = sum . takeWhile (<10000) . filter isPrime  [1..]
two = map (cos . sqrt)

Sunday, April 18, 2010

Ferrari F40 robot

Hey, I haven't been writing for a while - that's because I've been caught up with a project we (my group) have been assigned on Computer Systems Architecture class. The assignment was to build a robot that was supposed to drive through an 8-shaped track marked with white paper boarders marked black. To do that we had to steer him through LPT port using a computer and 8259 for interrupt handling. The interrupts occurred when the sensors, positioned in the front left and right of the robot, had changed state. After that the software was supposed to read their state from STATUS(input) pins of the LPT and set proper signals on DATA(output) pins so the engines steer our robot in the right way.

The whole thing took us combined of one week work. The most problems we had with electronics, especially with engine circuitry. We have melted a lot transistors. I was responsible mostly for software (written in C and partially assembler).

That's the (almost) final state of the robot. As you can see it's build on  Ferrari F40, red plastic.



 In case you ever wanted to send something to 8259, 8253 or your LPT port here's a code snippet :

void aoutPort(short port,short data)
{
    asm cli
    asm mov dx, port
    asm mov ax, data
    asm out dx, ax
    asm sti
}

short ainPort(short port)
{
    short tmp;
    asm cli
    asm mov dx, port
    asm in ax, dx
    asm mov tmp, ax
    asm sti
    return tmp;
} 

It does work on a computer with DOS or FDOS operating system. Ofcourse if your using FDOS/DOS you could also use dos.h and outport and inport functions already implemented. Some groups last year had a similar task and used Linux.

And here is an exemplary code we used to update the system remembering the previous state of the sensors and detecting a change.

void refreshDiode()
{
    lastAck=ACK_ON;
    aoutPort(DATA, lastEng+lastAck+lastLight);
    lastAck=ACK_OFF;
    aoutPort(DATA, lastEng+lastAck+lastLight);
} 

ACK_ON is defined as 32 because we use DATA5 pin on the LPT, DATA is defined as 0x378 which is the address of data line in LPT port. We don't want to change the state of the other DATA pins so we add the state of the engine and light pins (D0-D4).

The next assignment is to use a micro-controller instead of a computer - the robot will be autonomous then. The down side is we'll have to use only assembler to program it.

Monday, April 5, 2010

Happy Easter and some more Haskell

Happy Easter everyone ! :)

Last time I've written about good ways to learn Haskell, now I'd like to present why should you tackle this language.
  1. Its purely functional. If you have no idea what that means you can visit wiki or this page. Some most notable characteristics are : 
    • recursion is used instead of loops,
    • everything you can do with some data can also be done with functions (you can say that functions are 'first class') - this is pretty awesome,
    • 'side-effects' are very visible (something I'd like when working with OpenGL),
    • based on Lambda Calculus (explanations of the last two you could find here),
    • (...).
  2. The first argument may not be convincing very much - there are lots of stuff you won't ever understand, why get into lambda ? For a standard Programmer, the bread and butter are languages of imperative or object orientation. It turns out that some of these languages, like C#, Python, possibly Java, are coming with functional-like ways of programming. It may be tricky to understand at first and some say its the best to learn it in a pure form.
  3. Haskell can be and is used in real projects.
  4. It is very efficient. Maybe not as fast as C, but the development is faster. Usually the speed up C would give you is not that big compared to efficiency you would gain by implementing a better algorithm. I've seen (rather small in number of code lines) some tests showing that Haskell beats languages like Python and Ruby by a lot.
  5. Some people, for example Simon Peyton, call it the most beautiful language.
  6. A new look at the concurrent computing with Software Transactional Memory (STM). Simon Peyton Jones in his chapter of Beautiful Code shows the faulty of achieving multi-threading by using locks and gives a solution to the 'Santa' problem using Haskell. I'll surely write about this, when I find out more. Some (small) tests I've seen prove its efficiency and elegance. This is the aspect of Haskell that I'm personally most excited about.
I suppose there are a lot more reasons to learn Haskell. I've name only these 6 due to lack of my knowledge. Please post your own reasons for learning this language.

I've promised some Haskell-exercises and I shall post some in the near future.

Sunday, April 4, 2010

Learning Haskell

I've had a try with Haskell, at the begining of my studies with little success. Now I'm getting back to it.

I've started with some reading. Mostly Gentle Introduction to Haskell 98 and Learn you a Haskell , these two I find complementing themselves - the first one being more theoretical, the latter more practical.
Unfortunately that has not given as much as I hoped. I realised that if I was given a certain task to do in Haskell I'd have to begin with a long scratch on my head and a long Internet search.
I realised that what I need is some exercise.

This is what I've found so far to help me with that : 6 problems and 99 problems.
I've done a bit of both and now I feel more like a Haskell Programmer. More like 0.0002 per mill more. ;)
99 problems are really problems for Prolog (a declarative programming language) solved in Haskell. I've had to learn some Prolog 2 semesters back thus I'm not that excited to be doing them.

I will definitely look for more and post some problems of my own (with solutions) soon.

First Post

I'm finding myself hitting backspace and deleting what I've written, looking for better thoughts. That's because its my First Post and its kind of awkward. The fact that I'm not a native speaker is definitely not helping. Using English instead of Polish I'm hoping for a broader audience. Also a bit of practice never hurts, right ?

This is something I've been wanting to do for quite some time. Create my own blog that is. The reasons ? Quite a few so I'm gonna dot out some of  them :

  • Blogs are cool,
  • I'd like to express myself some more and drawing is not an option.
The next ones touch the title of my blog, which is "Programmer wannabe" :
  • Programming is cool,
  • I'd like my path to becoming a Programmer to be 'saved' somewhere, what place better that The Internet ?
  • I'd like others to know they are facing similar problems and maybe even find some answers here.
What will my blog be about ? A bit about me and a bit more about programming. The languages I'm interested in at the moment are (in priority) : Java, Haskell, C/C++ and Python. If I had to order them by the level I am proficient in them, it'd be something like this : C/C++, Java, Python, Haskell.

Other things which I'm interested in and you are probably not are games, books, anime and some other stuff.
I might mention some of them on occasion.

Enough for now. Can't wait for my next, more concrete post, so I'm gonna finish now. Probably get back to some explanations later.

One last thing : please don't be too outraged by the mistakes I'll (or already did) make. Although I'd love you to correct me if I'm wrong in any (linguistic/syntactic/conceptional/other) way.