Monday, June 11, 2012

Control leds on mini2440 by a Qt Creator's program

Mini2440 board has four leds that is used to indicate the status of the board. Normally, the leds will run when led service is loaded. However, we can use command to stop or restart that led service, they are:
$/etc/rc.d/init.d/leds stop
(after this command, leds will stop blinking)

$/etc/rc.d/init.d/leds start
(after this command, leds will start blinking)
 I wrote a QtCreator's program that passes those two commands by pressing button, the leds will stop or start running then. The source code of that program can be found at link here.

In Qt, there are two things you should know, they are "slot" and "signal". For example, when you press on button, it means you give a "signal"; then, that "signal" will trigger (all thing in) a "slot".
In the source code at above link, you will find those line:
void MainWindow::on_pushButton_2_clicked()
{
    system("/etc/rc.d/init.d/leds stop");    //stop led-player
    system("/etc/rc.d/init.d/leds start");    //start led-player
}

So, when you press (click) the pushButton_2 (giving a "signal"), then the sub-program ("slot") named MainWindow::on_pushButton_2_clicked() will run.
I just briefly introduce about programming in Qt, for more deeply in this issue, please take more day in book. My book at the first day programming Qt is "C++ GUI programming with qt4" (author: Jasmin Blanchette and Mark Summerfield), it is easy for newcomer, hope it will help you too.

Leds on mini2440 is controlled by device driver located at /dev/leds. So, we just open that file, pass it some data, that's it, the leds will light or not as in received data.
For example, in the source code, open the file and write data in order to control leds:
                int fd;
                if((fd=open("/dev/leds", 0))<0){
                        ::close(fd);
                        return;
                }
                ioctl(fd,0,0);    //led0 off
                ioctl(fd,0,1);    //led1 off

Once again, to understand more, we have to work with book again. There is a excellent book that i found, it is "Unix System Programming" by Keith Haviland, Ben Salama. This book will teach us how to open, write, delete, ... a file, and more.
And here, here is my program running in mini2440 board


5 comments:

  1. You can teach me ? I have Project like this ,Please this is my E-mail petergirl1991@gmail.com

    ReplyDelete
  2. really sorry you, you should manage it by yourself.

    ReplyDelete
  3. will you help me..
    I have erased my nand ,and started off with new u-boot so as to boot it from SD-card ,I got my kernel compiled and everything's set .Here I am using another root filesystem ( built form busybox)..but after booting up I don't find /dev/leds .The file in the /etc/rc.d/init.d/leds is for blinking the leds... .Since my rfs is different one,I don't have it. What could be the problem behind the absence of /dev/leds .. ?

    ReplyDelete
  4. I have followed your previous tutorial about how to install and configure Qt and set tool chain in Qt creator.
    Now, when i run this program, i can see the window on the tft with 4 push buttons.
    But the touch screen does not work and i get this error:-
    QWSTslibMouseHandlerPrivate: ts_conf()failed withe error "No such file or directory. Please chack your tslib installation."

    How should i solve this error ??

    ReplyDelete