Thursday 20 March 2014

Create a Simple Kernel Module on Ubuntu

Create a file, hello.c, that prints "Hello, world" when the module is loaded and prints "Goodbye, cruel world" when the module is unloaded
#include 
#include 
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void) {
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}

static void hello_exit(void) {
        printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Create a Makefile
ifneq ($(KERNELRELEASE),)
        obj-m := hello.o

else
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)

default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

Running make will create the kernel module named hello.ko
make

load the module by using the following command
sudo insmod hello.ko
View the messages by entering the dmesg command.
dmesg

unload the module by using the following command
sudo rmmod hello

Sunday 16 March 2014

Playing Internet Radio Stations on Raspberry Pi

Install mpd and mpc. MPD is the music player and MPC is a client to control the music player
sudo apt-get install mpc mpd

Once these are installed, you just need to have a stream to play from. I found this site useful for getting streams: http://www.listenlive.eu/index.html
Add the stream to the playlist
mpc add http://stream.hoerradar.de:80/deltaradio-alternative128
Play the station using the following command.
mpc play
Use the following command to stop the player.
mpc stop
Once the station is added, you will only need to use mpc play or mpc stop to control the player.

Add Syntax Highlighting in Vim

Syntax highlighting is extremely helpful when editing code. Vim has syntax highlighting built in to it but it is not enabled by default. To enable syntax highlighting, edit the Vim configuration file, /etc/vim/vimrc, and add "syntax on" to the file and then restart Vim.

Show line numbers in Vim

Adding line numbers in Vim is very simple. To add line numbers in Vim, edit the vim configuration file, /etc/vim/vimrc and add "set number" to the file and then restart Vim.

Connect to Wifi network with Raspberry Pi

Edit the /etc/network/interfaces file. You will need to use sudo when editing the file.

sudo vim /etc/network/interfaces


The contents of the file should have the following, where you have to substitute your_network and network_password are the ssid and the password of your network:
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp
        wpa-ssid "your_network"
        wpa-psk "network_password"


Restart networking to pick up the changes
sudo /etc/init.d/networking restart

Setting up VNC on Raspberry Pi

Setting up a VNC server on Raspberry Pi will allow you to access the desktop remotely, similar to Windows Remote Desktop Connection (RPC). Unlike RPC, multiple VNC sessions can be run concurrently, allowing multiple users to use the same machine at once.

To set up a VNC server on Raspberry Pi, install tightvncserver

sudo apt-get install tightvncserver

Get the resolution of your screen in Windows
Right click on the desktop and click on "Screen Resolution"

Then see what the resolution of your screen is. Mine is 1600 x 900


Start a VNC session on the Raspberry Pi with the following command
vncserver -geometry 1600x900

This will ask for a password, enter the password.

Once this is done, the session will be displayed
New 'X' desktop is raspberrypi:1

The session number is 1. You can now view the desktop remotely.

To do this you will need to download a VNC viewer. I use RealVNC http://www.realvnc.com/download/viewer/

Now you will need the IP Address of your Raspberry Pi. Use the following command to get the IP Address
ifconfig | grep inet

pi@raspberrypi ~ $ ifconfig | grep inet
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet addr:192.168.1.37  Bcast:192.168.1.255  Mask:255.255.255.0

You will need to the inet addr. There will be two addresses. 127.0.0.1 and some other address. In my case, it is 192.168.1.37

Open the VNC viewer and type in the IP Address, colon, VNC session in the VNC Server field. <ipaddr>:<sessionnum>
Select the "Let VNC Server choose" option for Encryption and then click "Connect".



A warning message will be shown saying the connection is unencrypted.  Click the "Do not warn me about this for <vnc session> again" and click the continue button.


You will then be prompted for your password. Enter the password that you used when you installed tightnvc and click OK.

You will now be able to use the desktop


Thursday 6 March 2014

Link Apache Web Server Root Directory to Home Directory on Raspberry Pi

When Apache2 is installed on Raspberry Pi, the www directory (the root directory of the web server), is configured as /var/www

You need to use sudo to write to files in this directory. You can configure another directory to be the root directory of the web server by modifying the Apache configuration files but I used a quick and dirty way to link it to my home directory

first install apache2
sudo apt-get install apache2

stop apache
sudo /etc/init.d/apache2 stop

delete the apache www directory
sudo rm -rf /var/www

create the www directory in /home/pi
mkdir /home/pi/www

create the symbolic link to /home/pi
sudo ln -s /home/pi/www/ /var/www

Create an index.html file in /home/pi/www
vim /home/pi/www/index.html

<html>
</body>
test
</body>
</html>

Start Apache
sudo /etc/init.d/apache2 start

Check that the webserver works in the browser

Tuesday 4 March 2014

Windows API C++ Hello World Application

#define WIN32_LEAN_AND_MEAN
#include 

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hinst;

const char CLASS_NAME[] = "WinMain";
const char APP_TITLE[] = "Hello World";
const int WINDOW_WIDTH = 400;
const int WINDOW_HEIGHT = 400;

int WINAPI WinMain(HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPSTR lpCmdLine,
       int nCmdShow) {
  
  MSG msg;

  if(!CreateMainWindow(hInstance, nCmdShow)) {
   return false;
  }

  int done = 0;
  while(!done) {
   if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    if(msg.message == WM_QUIT) {
     done = 1;
    }

    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
  }

  return msg.wParam;
}

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
 switch(msg) {
  case WM_DESTROY:
   PostQuitMessage(0);
   return 0;
 }
 return DefWindowProc(hWnd, msg, wParam, lParam);
}

bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow) {
 WNDCLASSEX wcx;
 HWND hwnd;

 wcx.cbSize = sizeof(wcx);
 wcx.style = CS_HREDRAW | CS_VREDRAW;
 wcx.lpfnWndProc = WinProc;
 wcx.cbClsExtra = 0;
 wcx.cbWndExtra = 0;
 wcx.hInstance = hInstance;
 wcx.hIcon = NULL;
 wcx.hCursor = LoadCursor(NULL, IDC_ARROW);

 wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
 wcx.lpszMenuName = NULL;
 wcx.lpszClassName = CLASS_NAME;
 wcx.hIconSm = NULL;

 if(RegisterClassEx(&wcx) == 0) {
  return false;
 }

 hwnd = CreateWindow(CLASS_NAME,
       APP_TITLE,
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       WINDOW_WIDTH,
       WINDOW_HEIGHT,
       (HWND) NULL,
       (HMENU) NULL,
       hInstance,
       (LPVOID) NULL);
 if(!hwnd) {
  return false;
 }

 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);
 return true;
}