Mount SD Card on Fedora

I attended Raspberry Pie meetup this weekend. We had lot of fun there playing with Pie. After coming home, i wanted to try all those things on my Pie. But first step is to prepare a image for it.

I decided to use pidora, the fedora remix for Raspberry Pie. Fedora comes with fedora-arm-installer package. After installing it on F19 machine and starting it, i inserted SD card and it didn’t get detected.

Without SD card, i couldn’t do anything. After googling, i found out that you have to install kmod-staging package. It is available as part of rpmfusion repo.

After that, next part was to find out your driver for SD card reader

lsusb | grep Reader
Bus 001 Device 005: ID 0bda:0139 Realtek Semiconductor Corp. RTS5139
Card Reader Controller

So it told me that my SD card driver is RTS5139. Most of the google results also had same driver so i tried next step.

modprobe rts5139

And i got error that rts5139 does not exist. The issue here is the kernel version for which you install kmod-staging matters. So i needed to install the version of kmod-version that matches with my kernel version. Otherwise it won’t work.

uname -r

gave me 3.9.8-300.fc19.x86_64

Then

yum whatprovides \*/rts5139.ko

provided list of all packages having rts5139.

I found the appropriate version with my kernel

yum install kmod-staging-3.9.8-300.fc19.x86_64-3.9.2-2.fc19.7.x86_64

Then

modprobe rst5139

And it works and SD card gets mounted automatically. :)

Right not pidora installation is going on :) I will post more about my Raspberry Pie experiments soon.

Some links -

Arduino/Raspberry Pie meetup from Pune

IOT tutorial for the meetup

Using `rails-api` with Rails 3

rails-api is an awesome gem to design API for API-only applications. One more interesting thing about rails-api is, it doesn’t have separate versions for Rails 3 and Rails 4.

rails-api picks up latest Rails release which is available. So with the release of Rails 4, it will use Rails 4.

What if you are not yet ready for Rails 4? What if you want to use rails-api with Rails 3?

Thanks to @steveklanik for answering my doubt.

You have to just specify in the Gemfile that you want 3.2.13 versions of gems that rails-api depends on.

For example -

  gem 'rails-api'               # for Rails API
  gem 'actionpack', '3.2.13'    # Specifically say you want '3.2.13'
  # rails-api depends on actionpack.
  # only specifying actionpack dependency is enough.

And now rails-api will use Rails 3.

Installing racket on fedora

As root user

    cat >/etc/yum.repos.d/rpm-sphere.repo <<EOF
    [rpm-sphere]
    name=RPM Sphere
    baseurl=http://download.opensuse.org/repositories/home:/zhonghuaren/Fedora_18/
    gpgkey=http://download.opensuse.org/repositories/home:/zhonghuaren/Fedora_18/repodata/repomd.xml.key
    enabled=1
    gpgcheck=1
    EOF

Then

    yum update
    yum install racket

To run racket from Emacs, enable marmalade repo

    (require 'package)
       (add-to-list 'package-archives
       '("marmalade" . "http://marmalade-repo.org/packages/"))
       (package-initialize)

Then install geiser package

   M-x package-install-RET geiser
   M-x run-geiser

It will ask for choice of language guile or racket. Selecting racket will open the REPL to play with it.

jenkins on fedora with rails

Installing jenkins on Fedora is quite easy.

$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
$ sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
$ sudo yum install jenkins
$ sudo service jenkins start/stop/restart

Start jenkins by

$ sudo service jenkins start

You will get a success message.

Starting Jenkins                                           [  OK  ]

By default jenkins starts on port 8080. Visit http://localhost:8080. You will be greeted by jenkins index page.

Setting up System

Set shell for jenkins user

$ sudo usermod -s /bin/bash jenkins

Set home for jenkins user

$ sudo usermod -m /var/lib/jenkins jenkins

Now we have to perform next steps as jenkins user. You can give superuser permissions to jenkins also.

Install RVM

$ sudo su - jenkins
$ curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enabled
Complete script is here

Load RVM

$ vi /var/lib/jenkins/.bashrc
$ [ -s "/var/lib/jenkins/.rvm/scripts/rvm" ] && source "/var/lib/jenkins/.rvm/scripts/rvm" # This loads RVM into a shell session.

#### Configuring path

Make sure that you have configured your bashrc properly so that PATH has everything that is needed to run a Rails project. I spent a lot of time with javascript runtime not found error even if i had nodejs installed. Later i realized that my bashrc was not containing path to node executable.

Gitlab hooks

I am using Gitlab as the repository server.

Create new key with ssh-keygen.

Gitlab plugin

Install

From the jenkins UI, you can install gitlab plugin. More info can be found here.

Build now hook

Add this web hook on your Gitlab project:

http://yourserver/gitlab/build_now

Now whenever there will be commit, gitlab will send a request to jenkins to trigger the build.

Build Configuration

We can run rake tasks, shell scripts once the build is triggered. This configuration can be done on the project page on jenkins server.

I prefer running a shell script which will do all the steps required to run specs. This script can be stored into version control system like git so that it will be available to everyone in your project.

I have created a sample ci script for a Rails 3.2+ project with PostgreSQL database here.

Post Build configuration

It allows to add email ids of project members who will get email after every build. It generates report for last build using various plugins.

Same steps can be used with CentOS to setup jenkins.

Happy hacking!

SICP Exercise 1-11

Problem -

A function f is defined by the rule that

f(n) = n if n < 3 and

f(n) = f(n-1) + 2 * f(n-2) + 3 * f(n-3) if n >= 3.

Write a procedure that computes f by means of a recursive process.

Write a procedure that computes f by means of an iterative process.

Solution -

Recursive solutions is very simple.

scheme Recursive solution (define (f n) (if (< n 3) n (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))

Iterative solution is a bit different.

Base condition is same for iterative solution also. If n < 3, n is the answer.

For n >= 3, we have to define a procedure which will evolve as a iterative process.

For n >= 3, f(n) = f(n-1) + 2 * f(n-2) + 3 * f(n-3)

For n = 3, we have to evaluate above rule once. For n = 4, we have to evaluate it twice. First to find out f(3) which then will be used to find f(4).

So, it is clear that for n >= 3, we have to evaluate this rule n - 2 times. Lets name diff to (n - 2).

Now lets consider state variables which will hold all the information to find solution at a particular instance in this process.

For n = 3, we need f(0), f(1) and f(2) to find out f(3). They are respectively 0, 1 and 2.

Lets name them a = 0, b = 1 and c = 2.

Now answer in each iteration is 3 * a + 2 * b + c

This answer needs to be passed to recursive call till diff is 0.

So after each iteration, state variables are changed as follows:

b becomes a

c becomes b

3 * a + 2 * b + c becomes c

diff becomes diff - 1

For n = 3, initially, a = 0, b = 1, c = 2 and diff = 3 - 2 = 1,

After one iteration, a = 1, b = 2, c = 4 and diff = 0

Now, base condition is when diff = 0, c is the answer. So here for f(3), 4 is the answer.

Lets repeat this for f(4)

n > 3

diff = 4 - 2 = 2

a = 0, b = 1, c = 2

After first iteration,

diff = 2 - 1 = 1

a = 1, b = 2, c = (3 * 0 + 2 * 1 + 2) = 4

diff > 0 So second iteration will be called

After second iteration,

diff = 1 - 1 = 0

a = 2, b = 4, c = (3 * 1 + 2 * 2 + 4) = 11

Now diff = 0, so c is the answer.

f(4) = 11

The implementation of this process is recursive as shown below. But the process which is evolved is iterative.

``` scheme iterative-solution

(define (f n)
    (if (< n 3)
        n
        (f-iter 0 1 2 (- n 2))))

(define (f-iter a b c diff)
    (if (= diff 0)
        c
        (f-iter b c (+ (* 3 a) (* 2 b) c) (- diff 1)))) ```

Iterative process evloved for f(4)


    ;; (f 4)
    ;; (f-iter 0 1 2 2)
    ;; (f-iter 1 2 4 1)
    ;; (f-iter 2 4 11 0)
    ;; 11

SICP Exercise 1-6 : Understanding order of evaluation

SICP Exercise 1.6 is very interesting. I found it difficult to understand the problem and then the solution. It was fun to actually understand and reason about the solution.

In section 1.1.7 we define a function to calculate square root of a number by Newton’s method of successive approximation.

``` scheme Newton’s method of successive approximation for finding square root of a number

(define (square x) (* x x))

(define (average x y) (/ (+ x y) 2))

(define (improve guess x) (average guess (/ x guess)))

(define (good-enough? guess x) (< (abs (- x (square guess))) 0.0000000001))

(define (square-root-iter guess x) (if (good-enough? guess x) guess (square-root-iter (improve guess x) x))) ```

Exercise 1.6 defines a replacement for special form if as follows

``` scheme ‘if’ as a ordinary procedure

(define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) ```

Now square-root-iter function changed as follows

``` scheme New version of ‘square-root-iter’

(define (square-root-iter guess x) (new-if (good-enough? guess x) guess (square-root-iter (improve guess x) x))) ```

The problem asks us, what is the result of this version of square-root-iter?

Definition of new-if is correct and it can be tested with some test cases.


(new-if (= 5 0) 0 5)
; 5
(new-if (= 0 0) 0 0)
; 0

But still evaluation of square-root-iter with new-if results in infinite loop. Why?

The answer is order of evaluation.

In scheme, there is a general rule of evaluation. First value inside parenthesis is considered as function and all other values are passed as arguments to that function. Only Special forms are evaluated differently.

if is a special form which is evaluated in following way.

(if <predicate> <consequent> <alternative>)

First, <predicate> is evaluated. If it is true, then <consequent> is evaluated. Otherwise <alternative> is evaluated. But both <consequent> and <alternative> are never evaluated at the same time.

Our new-if is not a special form. So it will be evaluated as any other function following applicative order of evaluation.

Applicative order of evaluation first evaluates all operands completely and then passed them to operator. There is also normal order evaluation in which operands are evaluated when they are actually needed.

Scheme interpreter uses applicative order. So in new version of square-root-iter, while evaluating new-if it will try to evaluate it’s operands first. But the second operand of new-if is recursive call to square-root-iter. So this evaluation will never finish.

Problem is not in implementation of new-if but in it’s evaluation.

SICP Exercise 1-3

Define a procedure that takes three numbers as arguments and returns the sum of squares of the two larger numbers

I broke this problem into 3 steps

Define a square function which will take one argument and return its square.

(define (square x) (* x x))
; (square 6) 36

Define a sum-of-squares function which will take two arguments and calculate sum of their squares.

(define (sum-of-squares x y) (+ (square x) (square y)))
; (sum-of-squares 3 4) 25

Define a max function which will take two numbers and return maximum of both numbers.

(define (max x y) (if (> x y) x y))
; (max 3 4) 4

Now the solution to the problem is just combine all the 3 subproblems into one.

EDIT: In my earlier logic, i had a flaw. I was just passing (max x y) (max y z) to the sum-of-squares function. Thanks to Ankur for pointing out the mistake. The second argument actually depends on the result of (max x y).
(define (sum-of-squares-of-2-larger x y z)
    (sum-of-squares (max x y)
                    (if (= (max x y) x)
                        (max y z)
                        (max x z)
                        )))
; (sum-of-squares-of-2-larger 1 2 3) 13
; (sum-of-squares-of-2-larger 5 4 3) 41
; (sum-of-squares-of-2-larger 1 3 2) 13

SICP chapter 1 tells about how problems can be solved by breaking them into smaller subproblems and then building from them and solving bigger problems.

starting again

I am starting writing this blog again. Last year, i started this blog thinking that i will write about my coding adventures but soon sloth caught me. I just wrote two posts last year(one of them being just an image).

This year i aim to write more in quantity and in quality.

My new year resolution is to complete the SICP book. I have the hard copy of the book and also have the videos from MITOCW

I am doing exercises in MIT Scheme as the book also uses it. I have started with chapter 1 and completed upto Exercise 1-3. I am keeping my solutions here. I am planning to write my next post about the chapter 1 of SICP.

i-want-2-blog

I was introduced to Octopress by Vedang Manerikar in his talk on emacs. I wanted to have a blog for long time but wanted something new, fresh and Octopress is just that.

If you want to learn about basic rake tasks, markdown, git etc; Octopress is just for you :)

Octopress : Blogging using Git, Github, Ruby

Octopress is a framework designed for Jekyll, the blog aware static site generator powering Github Pages by Brandon Mathis. Detail features are available @ here

Getting Started

This tutorial is only for *nix like systems :D #### Before You Begin You will need to install git and setup your ruby enviornment Octopress requires Ruby 1.9.2 which you can easily install with RVM. #### Get source and install initial dependancies

git clone git://github.com/imathis/octopress.git octopress
cd octopress    # If you use RVM, You'll be asked if you trust the .rvmrc file (say yes).
ruby --version  # Should report Ruby 1.9.2

Now install dependancies

gem install bundler
bundle install

Install the default Octopress theme.

bundle exec rake install

A Detailed explanation about bundler is found here

Configure your blog

You have to change following fields from _config.yml

 url:                # For rewriting urls for RSS, etc
 title:              # Used in the header and title tags
 subtitle:           # A description used in the header
 author:             # Your name, for RSS, Copyright, Metadata
 simple_search:      # Search engine for simple site search
 description:        # A default meta description for your site
 subscribe_rss:      # Url for your blog's feed, defauts to /atom.xml
 subscribe_email:    # Url to subscribe by email (service required)
 email:              # Email address for the RSS feed if you want it.

Spice it up with twitter and other 3rd party plugins

Simply fill in the configurations which are already generated like your twitter id , github handle etc;

The Octopress layouts read these configurations and only include the javascript and html necessary for the enabled services.

Get started with blogging

Octopress provides some rake tasks for creating new posts

bundle exec rake new_post["title"]

The default file extension for new posts is markdown.

This is an example from my first post

---
layout: post
title: "RubyConfIndia2012"
date: 2012-03-21 03:07
comments: true
tags:
---

Generate and Deploy

bundle exec rake generate   # Generates posts and pages into the public directory
bundle exec rake watch      # Watches source/ and sass/ for changes and regenerates
bundle exec rake preview    # Watches, and mounts a webserver at http://localhost:4000
bundle exec rake deploy

Further Reading

I haven’t covered all points because i don’t know them :) .

You can get better help @ octopress home page

Octopress is great asset with good documentation available so it’s good tool to blog.

RubyConfIndia2012

I am attending RubyConf India 2012


I am attending RubyConfIndia from 24th to 25th March 2012 in Pune.

Are you coming ?