Tuesday, February 24, 2009

OpenMoko: First Upgrade 2008.12

OpenMoko was "just" arrived, and is already time to Upgrade. As Open Moko Getting Started says "According to the FAQ and Distributions pages, all phones ship with Om 2007.2. "

If you want take a look at the /etc/version file you will find something like this (in my case the code is this) 200804240820. Ok, it says 2008.. but is 2007.2.

Preparing the PC
Download the uImage and RootFS from the OpenMoko respository.

Shutdown your device using the Power Button (bottom right) and click on "Shutdown" menu item.

Connect PC and Phone with the USB cable (in the pack).

Press and keep pressed the AUX button (top left) on the Phone and, at the same time, press the Power button (bottom right).

Ok, Now you're in the U-Boot Menu, is something like GRUB.

Press AUX, Three times until you're on the "Set console to USB" item and then Press Power. (AUX is used to move between items, Power is the "Enter" button).

Then replace the Kernel and RootFS:
dfu-util -a kernel -R -D <kernel path>.bin
dfu-util -a rootfs -R -D <rootFS path>.jffs2


That's all! Reboot your Moko and try the new Software.

Setup PC Network:
ifconfig usb0 192.168.0.200 netmask 255.255.255.192
iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.0.0/24


Connect to Moko using ssh:
ssh root@192.168.0.202

Thursday, February 19, 2009

QtContest NEO FreeRunner is Here!

The NEO FreeRunner of Nokia/TrollTech QtContest, is Here! with a great Qt T-Shirt!


Thanks Nokia/TrollTech!

Sunday, February 1, 2009

Data Structures: B*Tree

Hierarchy and Relational Databases Doesn't Scale well For Humans, they add Structure. They only scales well for an amount of structure that is feasible to learn.

I'm Writing a Library that contains some data structure useful for data indexing, this library will be the base for a simple FileSystem and it can be simply used by Applications to store and retrieve data efficently. Today I've finished the B*Tree Implementation, Now I've to implement something for full-text search like Suffix Tree or similar data structure.

Balanced trees are used in databases, and more generally, wherever a programmer needs to search and store to non-random memory by a key, and has the time to code it this way.

Internal Nodes


Internal nodes consist of pointers to sub-trees separated by their delimiting keys. The key that precedes a pointer to a sub-tree is a duplicate of the first key in the first formatted node of that sub-tree. Internal nodes exist solely to allow determining which formatted node contains the item corresponding to a key.

Leaf Nodes





Leaf nodes contains information, Information are stored in Items and each of which is identified by a key used to identify data and to keep sorted the tree. An item is a data container that is contained entirely within a single node, and it allows us to manage space within nodes. Every item has a key, an offset to where in the node the item body starts and a length of the item body.

If you want more information about this Project feel free to contact me, else you've to wait the first release of the project :)

Friday, December 26, 2008

C# Parallel Foreach using Thread Pool



public void Foreach(T[] array, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;

using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;

for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(array[j]);
}

if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}

signal.WaitOne();
}
}





public void ForeachWithResults(T[] array, T1[] results, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;

using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;

for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(j, array[j], results);
}

if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}

signal.WaitOne();
}
}

Saturday, December 20, 2008

C#: Evaluating Infix Expression

This morning I've written a simple Infix Notation Evaluator in C, and this afternoon I've rewritten it in C#. I Think that C# is more easy to understand than C, so I've posted the C# Example. (If you want the C example, ask me).
In few words, I take the Input in "Infix Notation" and I convert it in Reverse Polish Notation (RPN), and then the RPN string is evaluated.

First Example: Evaluating a simple Math Expression.

string expr = "10 + 5 - (2 + (3 * 4) + (5 * 3)) - 4 ^ 2 << 8";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());


Second Example: Evaluating a simple Math Expression with variables.

Dictionary vars = new Dictionary();
vars.Add("A", "1"); vars.Add("B", "2");
vars.Add("C", "3"); vars.Add("D", "4");
vars.Add("K", "5"); vars.Add("L", "6");

string expr = "(A + B) + C + (D * (K + (L - 1)))";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());


Third example: Evaluating simple String expression.

string expr = "'Hello' + ' ' * 3 + \"World\"";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());


After the Example you can find the Source Code Here (You can compile it under Linux with gmcs).

That's all folks! I'm really busy with the Real Work, I'm waiting the 25th for taking a break.

Saturday, December 6, 2008

WebKit: Dynamic Content

Today I've played a bit with WebKit. I Want do something like Cocoa VBox View and NSScrollView but with more flexibility. Below you can see the example result.
The interesting part of this example is How to manage WebKit content, and how to add something dynamically.

I use webView:decidePolicyForNavigationAction:request:frame:decisionListener: of WebPolicyDelegate Protocol to handle my custom requests, in this way...

- (void)webView:(WebView *)sender 
         decidePolicyForNavigationAction:(NSDictionary *)actionInformation
         request:(NSURLRequest *)request frame:(WebFrame *)frame
         decisionListener:(id)listener
{
  if ([[[request URL] path] isEqualToString:@"PATH to Handle"]) {
    [listener ignore];
    // Handle Request
  } else {
    [listener use];
  }
}

...Then with a little bit of JavaScript I'll set my dynamic content loaded from Database or somewhere else.

NSArray *args = [NSArray arrayWithObjects:@"Item1Content", 
@"Hi, I'm Test 1 Content", nil];
[[webKit windowScriptObject] callWebScriptMethod:@"fillElement" 
withArguments:args];

The fillElement method is a simple JS function like this document.getElementById(elem).innerHTML = data;

Here you can find the Source Code.

Friday, November 28, 2008

Objective-C: SQLite Wrapper

I've written a simple SQLite Wrapper with two Examples one for Mac and one for the iPhone. The Wrapper class has the same source code for both platforms.

Here you can find the Mac Example Source Code and the iPhone Example Source Code.


This is a simple usage example:

Sqlite *sqlite = [[Sqlite alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
stringByAppendingPathComponent:@"SQLiteTest.db"];
if (![sqlite open:writableDBPath])
  return;

[sqlite executeNonQuery:@"CREATE TABLE test (key TEXT NOT NULL, value TEXT);"];
[sqlite executeNonQuery:@"DELETE FROM test;"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
[Sqlite createUuid], @"PROVA"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
[Sqlite createUuid], @"PROVA 2"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
[Sqlite createUuid], @"PROVA 3"];

NSArray *results = [sqlite executeQuery:@"SELECT * FROM test;"];
for (NSDictionary *dictionary in results) {
  NSLog(@"Row");
  for (NSString *key in [dictionary keyEnumerator])
    NSLog(@" - %@ %@", key, [dictionary objectForKey:key]);
}

[results release];
[sqlite release];