Thursday, May 16, 2013

Flashing bios using a usb drive

If you are like me where most PC run's linux and has no optical drive.  I saw the following steps around the net to be helpful.
  1. Get the bios update iso
  2. Get 'geteltorito' and extract the boot image from the iso
    $ wget 'http://www.uni-koblenz.de/~krienke/ftp/noarch/geteltorito/geteltorito.pl'
    $ perl geteltorito.pl biosupdate.iso > biosupdate.img
    
  3. Copy the image to the usb thumdrive
  4. $ sudo dd if=biosupdate.img of=/dev/usbthumdrive bs=512K

Saturday, April 13, 2013

Small Android stick (mk808b) mini review

Me and my mates at the office just bought one of those Android mk808b.  Its dirt cheap, so we gave it a try.

So far its been pretty good.  I plugged to an existing TV with hdmi and I can now use the TV for browsing, watching streaming videos and skype.  Since I am a Linux user I got a UVC compliant webcam, which works on this device as Android uses a Linux kernel.  So with little a fraction of a cost I was able to turn my old TV into what they call a smart TV (didn't like the idea of smart tv, never bought one).  Normally before this TV had a laptop plugged into it, now Android stick pc replaced it.

Pros (would not elaborate on it too much, lots of people have put heaps of comments around the web)
  • Low cost.  These cost about $50-90 around the net.  Also as part of the low cost, is that you can use your existing TV as long as it has a hdmi input.
  • Small and low power.  This comes with a USB adapter.  However I had mine just plugged into my xbox 360 usb port, even way better.
  • Newer firmware comes pre-rooted.  Mine is already rooted.
Cons
  • Clearly Android is for touch.  I bought the bundled air mouse, you can also use normal usb mouse and keyboard.  However the experience is not as nice, same experience as running the Android developer emulator on your PC.  You can only use apps that will run with single touch operations or single touch swipes.  Youtube, web browsers, skype, angry birds will be ok.  However google maps (no pinch zoom, so I don't know how to zoom out.. zoom in can done via double tap).  My idea was to get my tablet back from my kids, however this clearly an inferior experience for them.  So it kinda did not work, and now they can tell me to use my stick Android and they will use the Android tablet.  Plan back-fired.
  • Its not well polished.  This can leave a poor impression on Android for users having this as their first Android device.   Though this is Jelly Bean, its really far away from the experience you have from a Nexus 4, even from a 2 year old Asus Transformer.
  • It seems the cpu stays at 1 ghz no matter what the load is.  Now sure why I haven't played with the governor yet.  So it doesn't go 1.6ghz or drops down to 200-400mhz.
  • The out display seems to be 16 bit (not sure but it looks like it), also always resets to 720p after every boots up.  I read that you will need to reflash so it renders the color in 24/32 bit.  Again doesn't look that good, but not too bad if don't look for image issues (gradients and dithers).
  • The air mouse is sensitive to interference.  I am still running on 2.4 wifi, I have not much other devices and neighbors are far away.  However you can see during real big burst of traffic the air mouse is getting cut off as seen by the lag.
So the android stick PC is not really dirt cheap as it seems to be.  This is probably not a good first Android device.  However is a great bargain if.  You have a TV that you want to run some basic computing needs (web browsing, video calls, streaming video, music playing).  You can upgrade the TV and replace an aging laptop that used to perform those computing needs.

In order for the stick PC to have a great experience you need to buy a good input device.  Which would now increase the cost.  It also doesn't have a screen, no battery, etc.  So a Nexus 7 would probably be cheaper as a first good Android device.

Sunday, August 26, 2012

Angular.js for ajax CRUD

Its been a busy weekend, not in terms of coding though.  Just did some quick coding now.  This is now a book-markable ajax CRUD talking to conventional REST/JSON backend.

//app.js

angular.module('ratewatcher', ['ngResource']).
 config(function($routeProvider) {
  $routeProvider.
   when('/lenders', {controller:LenderListController, templateUrl:'lender-list.html'}).
   when('/lenders/new', {controller:LenderCreateController, templateUrl:'lender-detail.html'}).
   when('/lenders/:id', {controller:LenderEditController, templateUrl:'lender-detail.html'}).
   otherwise({redirectTo:'/lenders'});
 }).
 factory('Lender', function($resource) {
  return $resource('/lenders/:id', {id: '@id'}, {});
 });

//controllers.js

function LenderListController($scope, Lender) {
  $scope.lenders = Lender.query();
  
  $scope.remove = function(lender) {
   lender.$remove();
  }
}
  
function LenderCreateController($scope, Lender) {
  $scope.save = function() {
    var lender = new Lender({id: 0,
     name: $scope.lender.name, 
     category: $scope.lender.category, 
     url: $scope.lender.url
    });
    
    lender.$save();
  };
}

function LenderEditController($scope, $location, $routeParams, Lender) {
 $scope.lender = Lender.get({id: $routeParams.id});
 
  $scope.save = function() {
   $scope.lender.$save();
   $location.path('/');
  };
}

I thought the scala backend was really concise, but it turns you can also make the front end javascript just as concise.  Coupled with bootstrap css and basic html, this seems pretty good.

Suggested readings:
http://www.angularjs.org/ - Wire Up a Backend example
http://docs.angularjs.org/api/ngResource.$resource
http://jsfiddle.net/johnlindquist/qmNvq/
http://docs.angularjs.org/api/ng.$route

Thursday, August 23, 2012

Angular.js my first few mins impression

I have been looking at Javascript frameworks.  I have experience in GWT and lately trying to learn ember.js.  So far ember.js is really powerful, but the docs is not that great.  Ember.js has made a lot of changes and the only way to learn it seems to be to read the code/api.  Since my javascript is not that good, it was taking me a bit of time to learn the magic of ember.js.  This lead to me look again a bit, I found angular.js because it was in google's CDN api.

Having read the watch and read the front page of angular.js it seems to be just as powerful as ember.js.  Its got bi-directional binding, a template engine, name spaces.  The terminology they have is FP (ala Scala).  They have apply, future promise.  So gave a try.

function LenderController($scope, $http) {
 $scope.lenders = [];
 
  $scope.all = function() {
    $http.get('/lenders').success(function(data) {
      $scope.lenders = data;
    });
  };
}

<div class="span10" ng-controller="LenderController">
    <table class="table striped">
        <tr> 
            <th>Name</th><th>Category</th><th>Url</th> 
        </tr>
        <tr ng-repeat="lender in lenders"> 
            <td>{{lender.name}}</td> <td>{{lender.category}}</td> <td>{{lender.url}}</td> 
        </tr>
    </table>
    <a href="" ng-click="all()">fetch all</a>
</div>
Pretty short and direct code for fetching from json/rest backend and displaying it on the view.  I think Angular.js is powerful and yet easy for a newbie to pickup.  That is my first few mins impression of it.

Thursday, July 12, 2012

Ruby was shiny before compared to Java, Tcl and Perl. Isn't as shiny when its beside Scala now.

Just looking at Ruby again.  My first encounter with Ruby like most people is through ROR, it was a few years back (era 1.x or maybe before that).  It was a very nice language and at that time, it wasn't that popular.  I did however convince a colleague of mine to push ROR with the help of Bruce Tate's From Java To Ruby book.  He was successful in getting ROR into a Java EE environment.  I had a quick glance of Ruby again today and in tutorial there was this code:
class MegaGreeter
  attr_accessor :names

  # Create the object
  def initialize(names = "World")
    @names = names
  end

  # Say hi to everybody
  def say_hi
    if @names.nil?
      puts "..."
    elsif @names.respond_to?("each")

      # @names is a list of some kind, iterate!
      @names.each do |name|
        puts "Hello #{name}!"
      end
    else
      puts "Hello #{@names}!"
    end
  end

  # Say bye to everybody
  def say_bye
    if @names.nil?
      puts "..."
    elsif @names.respond_to?("join")
      # Join the list elements with commas
      puts "Goodbye #{@names.join(", ")}.  Come back soon!"
    else
      puts "Goodbye #{@names}.  Come back soon!"
    end
  end

end


if __FILE__ == $0
  mg = MegaGreeter.new
  mg.say_hi
  mg.say_bye

  # Change name to be "Zeke"
  mg.names = "Zeke"
  mg.say_hi
  mg.say_bye

  # Change the name to an array of names
  mg.names = ["Albert", "Brenda", "Charles",
    "Dave", "Englebert"]
  mg.say_hi
  mg.say_bye

  # Change to nil
  mg.names = nil
  mg.say_hi
  mg.say_bye
end
It's nice and concise, however it doesn't stand out as much as before.   My comparison was Java, Tcl and Perl a few years ago.  I then did a quick copy and paste, translated it to Scala.

class MegaGreeter(var names: Any = List("World")) {
 
  def say_hi {
    names match {
      case null => println("...")
      case listOfNames: List[String] => listOfNames foreach (n => println("Hello " + n)) 
      case _ => println("Hello " + names)
    }
  }

  def say_bye {
    names match {
      case null => println("...")
      case listOfNames: List[String] => println("Goodbye " + listOfNames mkString(",") + ".  Come back soon!") 
      case _ => println("Goodbye " + names + ".  Come back soon!")
    }
  }
}

object MegaGreeter {
  def main(args: Array[String]) {
    val mg = new MegaGreeter
    mg.say_hi
    mg.say_bye

    // Change name to be "Zeke"
    mg.names = "Zeke"
    mg.say_hi
    mg.say_bye

    // Change the name to an array of names
    mg.names = List("Albert", "Brenda", "Charles",
            "Dave", "Englebert")
    mg.say_hi
    mg.say_bye

    // Change to nil
    mg.names = null
    mg.say_hi
    mg.say_bye
  }
}
Above is not really Scala idiomatic, but close to the original Ruby code especially how the class is created and used.  Ruby still looks nice, but not as shiny as a few years ago.  I am not here to say Scala is better than Ruby, but more of what is shiny before may not be as shiny today.  Just like Java was shiny when I first time used it about 1996, it seems these days people see Java as the wart language.  Scala will loose its shiny appeal in the next few years too.

I also been using the term "shiny", as its really about appeal.  I still use Java and occasionally Perl, even if the are not shiny anymore.  These older languages are still great in their own right.  C is still cool these days.. I think.

Thursday, June 28, 2012

Coin problem, how do you solve it?

My son has this typical homework problem.  If you have 10c, 20c and 1 dollar.  How many ways can you make $2?

I am pretty sure there should be a combination formula for it, but I forgot.  So I tried google but seems there is no formula

http://mathforum.org/library/drmath/view/57913.html

Is there really no formula?

Anyway so he started to make a table of combination.  I told him I better start to see if I can code it, maybe it would help him to verify if the table of combination is correct.  I came up w/ this.  I hope its right otherwise his table of combination is wrong and my code is wrong.

val twoDollars = 200

val coinsCombo = for ( oneDollar <- 0 to (twoDollars/100);
 twentyCents <- 0 to (twoDollars/20);
 tenCents <- 0 to (twoDollars/10);
 if (oneDollar * 100 + twentyCents * 20 + tenCents * 10 == twoDollars) 
) yield (oneDollar, twentyCents, tenCents)
 
coinsCombo foreach println

println(coinsCombo.size)

Explaining C++, Java and Scala to my son

Last night my son was asking about computers again.  I think the conversation came about, what language is on my screen.  I told him its Java and Scala.  I talked about what is assembly, C, C++ and Objective-C and how it relates to Scratch and Kojo.

He asked about why it was named as C++, then I showed him a Java typical loop which is based C where we increment a counter.

int c = 0;
while (c <= 10) {
 System.out.println(c);
 c++;
}



He asked how the loop will be done in Scala.  I started w/ a for comprehension, as he is familiar w/ loops on Scratch and Kojo.

for (c <- 0 to 10) {
 println(c)
}
Then he asked me about the for, <-, etc.

I then changed the code into

0 to 10 foreach println


His reaction "that is so awesome it's like english!"