Code formatting using shell script

This is a little script to automatically format source code of different languages, just copy it into a directory that is in your PATH.

You can also add an alias to your ~/.bashrc

alias cstyle='cs style'
alias cclean='cs clean'
#!/bin/bash
#
#   codingstyle.sh (or short cs)
#   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   A little script to automatically apply my preferred coding style
#   to all .c, .cpp, .h, .java and .py files in and below the current directory
#   Just copy it into a directory that is in your PATH and rename the file to cs.
#
#   Author: Christian Jann <christian 0x2e jann 0x40 ymail 0x2e com>
#   URL: http://www.jann.cc/2013/02/20/code_formatting_using_shell_script.html
#
#   Requirements:
#
#     * astyle (http://astyle.sourceforge.net/astyle.html)
#     * autopep8 (http://pypi.python.org/pypi/autopep8/)
#       (only if it finds Python files)
#

case "$1" in
    style)
        echo "Formating source files..."
        # Modified kdelibs coding style as defined in
        #   http://techbase.kde.org/Policies/Kdelibs_Coding_Style

        find -regex ".*\.\(c\|cpp\|h\|java\)" -exec \
            astyle --indent=spaces=4 --brackets=break \
              --indent-labels --pad-oper --unpad-paren --pad-header \
              --keep-one-line-statements --convert-tabs \
              --indent-preprocessor "{}" \;

        # Other variants:
          # Maybe use --mode=java for java files, --mode=c

          # find . -perm -200 -regex ".*[.][CHch]p*" -exec astyle \
          #    --suffix=none --style=ansi --convert-tabs "{}" \;

          # find -regex ".*[.][CHch]p*" -exec astyle  --style=attach "{}" \;

          # astyle --indent=spaces=4 --brackets=break \
          #     --indent-labels --pad-oper --unpad-paren --pad-header \
          #     --keep-one-line-statements --convert-tabs \
          #     --indent-preprocessor \
          #     `find -type f -name '*.c'` \
          #     `find -type f -name '*.cpp'` \
          #     `find -type f -name '*.h'` \
          #     `find -type f -name '*.java'`

        # Apply coding conventions for Python code
        # http://www.python.org/dev/peps/pep-0008/
        # https://pypi.python.org/pypi/autopep8/
        # https://github.com/jcrocholl/pep8
        # sudo pip-python install --upgrade autopep8
        for file in $(find . -name "*.py")
        do
          #echo "Creating backup: $file.orig"
          #cp -v $file{,.orig}
          cp $file{,.orig}

          #echo "Formating file: $file"
          autopep8 -i "$file"

          diff "$file" "$file.orig" >> /dev/null  \
            && echo "Unchanged  $file" || echo "Formatted  $file"
        done
    ;;
    clean)
        echo "Deleting temporary files and backup copies..."
        #find . \( -name "*.orig" -or -name "*~" \) -exec rm -v "{}" \;
        #find -regex ".*\(orig\|~\|pyc\|bak\)" -delete
        find -regex ".*\(orig\|~\|pyc\|bak\)" -exec rm -v "{}" \;
    ;;
    *)
        echo "run 'cs style' to format all"\
            ".c .cpp .h .java and .py files recursively"

        echo "run 'cs clean' to delete temporary"\
            "files and backup copies (*.orig)"
    ;;
esac

Read more...

Laustracker: Using computer vision to track little robots moving through a labyrinth

Laustracker3D using OpenSceneGraph

Laustracker3D using OpenSceneGraph

Read more...

The “cu” command

From a given occasion a short introduction to the cu command.

The cu command is used to call up another system and act as a dial in terminal. It can also do simple file transfers with no error checking.

minicom is another popular serial communication program. Unfortunately, many users have reported problems using it with U-Boot and Linux, especially when trying to use it for serial image download. It’s use is therefore discouraged.

http://www.denx.de/wiki/view/DULG/SystemSetup#Section_4.4.

Other alternatives are screen, picocom and kermit.

Install the “cu” command

[chris@thinkpad ~]$ sudo yum install uucp

Configure the “cu” command

[chris@thinkpad ~]$ sudo vim /etc/uucp/sys
[chris@thinkpad ~]$ cat /etc/uucp/sys
# This is an example of a sys file. This file is syntax compatible
# with Taylor UUCP (not HDB, not anything else). Please check uucp
# documentation if you are not sure how to configure Taylor UUCP config files.
# Edit the file as appropriate for your system, there are sample files
# in /usr/share/doc/uucp-1.07/samples.

# Everything after a '#' character is a comment.


# /dev/ttyUSB0 at 115200 bps:
#
system          USB0@115200
port            serial0_115200
time            any

[chris@thinkpad ~]$ sudo vim /etc/uucp/port
[chris@thinkpad ~]$ cat /etc/uucp/port
# This is an example of a port file. This file is syntax compatible
# with Taylor UUCP (not HDB, not anything else). Please check uucp
# documentation if you are not sure how to configure Taylor UUCP config files.
# Edit the file as appropriate for your system, there are sample files
# in /usr/share/doc/uucp-1.07/samples.

# Everything after a '#' character is a comment.

#
# /dev/ttyUSB0 at 115200 bps:
#

port            serial0_115200
type            direct
device          /dev/ttyUSB0
speed           115200
hardflow        false

[chris@thinkpad ~]$

Connect to the serial line

[chris@thinkpad ~]$ cu USB0@115200
Connected.
HTLLCLC

U-Boot 2013.01-00033-g852e10a-dirty (Feb 07 2013 - 16:21:11)

CPU:   Freescale i.MX23 rev1.4 at 454 MHz
BOOT:  SSP SD/MMC #0
DRAM:  64 MiB
MMC:   MXS MMC: 0
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   Net Initialization Skipped
No ethernet found.
Hit any key to stop autoboot:  0
mmc0 is current device
462 bytes read in 0 ms
Loaded environment from uEnv.txt
Importing environment from mmc ...
3684752 bytes read in 0 ms
8450 bytes read in 0 ms
Booting from mmc ...
## Booting kernel from Legacy Image at 42000000 ...
  Image Name:   Linux-3.7.6-dirty
  Image Type:   ARM Linux Kernel Image (uncompressed)
  Data Size:    3684688 Bytes = 3.5 MiB
  Load Address: 40008000
  Entry Point:  40008000
  Verifying Checksum ... OK
## Flattened Device Tree blob at 41000000
  Booting using the fdt blob at 0x41000000
  Loading Kernel Image ... OK
OK
  Using Device Tree in place at 41000000, end 41005101

Starting kernel ...

Uncompressing Linux... done, booting the kernel.
[    0.000000] Booting Linux on physical CPU 0
[    0.000000] Linux version 3.7.6-dirty (chris@thinkpad) (gcc version 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305] (GNU Tools for ARM Embedded Processors) ) #1 Tue Feb 5 11:02:16 CET 2013
...

Copy a file over the serial line

Create a test file on your Linux PC:

[chris@thinkpad ~]$ echo "This is a test file" >> testfile.txt
[chris@thinkpad ~]$ cat testfile.txt
This is a test file
[chris@thinkpad ~]$

To copy a file from the local system to the remote system, enter:

[root@olinuxino ~]# ~p testfile.txt
Remote file name [testfile.txt]:
1
[file transfer complete]
[connected]
[root@olinuxino ~]# ls
fixterm  housenka.sh  led_blink  testfile.txt  wconfig
[root@olinuxino ~]# cat testfile.txt
This is a test file
[root@olinuxino ~]#

To copy a file from the remote system to the local system, enter:

[root@olinuxino ~]# ~t /var/log/messages.log
Local file name [messages.log]: olinuxino_messages.log
[file transfer complete]
[connected]
[root@olinuxino ~]#
[chris@thinkpad ~]$ du -h olinuxino_messages.log
512K    olinuxino_messages.log
[chris@thinkpad ~]$ tail olinuxino_messages.log
Dec 31 18:34:32 olinuxino kernel: [ 2065.160000] wlan0: Selected IBSS BSSID ee:73:d9:ca:2e:92 based on configured SSID
Dec 31 18:34:32 olinuxino dhcpcd[141]: version 5.6.4 starting
Dec 31 18:34:32 olinuxino dhcpcd[141]: all: configured as a router, not a host
Dec 31 18:34:33 olinuxino dhcpcd[141]: wlan0: broadcasting for a lease
Dec 31 18:34:36 olinuxino dhcpcd[141]: wlan0: offered 10.42.0.48 from 10.42.0.1
Dec 31 18:34:36 olinuxino dhcpcd[141]: wlan0: acknowledged 10.42.0.48 from 10.42.0.1
Dec 31 18:34:36 olinuxino dhcpcd[141]: wlan0: checking for 10.42.0.48
Dec 31 18:34:41 olinuxino dhcpcd[141]: wlan0: leased 10.42.0.48 for 3600 seconds
Dec 31 18:34:52 olinuxino dhcpcd[141]: forked to background, child pid 169
Dec 31 18:54:53 olinuxino -- MARK --
[chris@thinkpad ~]$

Disconnect

[root@olinuxino ~]# ~.

Disconnected.
[chris@thinkpad ~]$

U-Boot for the iMX233-OLinuXino

Get U-Boot

[chris@thinkpad OLinuXino]$ git clone git://git.denx.de/u-boot.git u-boot.git
Cloning into 'u-boot.git'...
remote: Counting objects: 200191, done.
remote: Compressing objects: 100% (37411/37411), done.
remote: Total 200191 (delta 161097), reused 198842 (delta 159910)
Receiving objects: 100% (200191/200191), 50.43 MiB | 2.17 MiB/s, done.
Resolving deltas: 100% (161097/161097), done.
[chris@thinkpad OLinuXino]$ cd u-boot.git/
[chris@thinkpad u-boot.git]$ git checkout v2013.07 -b tmp
Switched to a new branch 'tmp'
[chris@thinkpad u-boot.git]$

Read more...

Lets play: CLI snake on the OLinuXino

CLI snake over 115200 baud serial line

CLI snake over 115200 baud serial line

Read more...

A new SD card image for the iMX233-OLinuXino

Download the latest Arch Linux ARM SD card image

First download the latest image for the OLinuXino from http://archlinuxarm.org/platforms/armv5/olinuxino:

[chris@thinkpad Downloads]$ wget http://archlinuxarm.org/os/ArchLinuxARM-olinuxino-latest.img.gz
[chris@thinkpad Downloads]$

Copy the image to your SD card

Now uncompress the image and add a timestamp to make it easier to distinguish different images:

[chris@thinkpad Downloads]$ ls -l Arch*
-rw-rw-r--. 1 chris chris 188339953 Jun  4 21:45 ArchLinuxARM-olinuxino-latest.img.gz
[chris@thinkpad Downloads]$ mv ArchLinuxARM-olinuxino-latest.img.gz  ArchLinuxARM-olinuxino-$(date +%Y-%m-%d -r ArchLinuxARM-olinuxino-latest.img.gz).img.gz
[chris@thinkpad Downloads]$ ls -l Arch*
-rw-rw-r--. 1 chris chris 188339953 Jun  4 21:45 ArchLinuxARM-olinuxino-2013-06-04.img.gz
[chris@thinkpad Downloads]$ gzip -d ArchLinuxARM-olinuxino-2013-06-04.img.gz
[chris@thinkpad Downloads]$ ls -l Arch*
-rw-rw-r--. 1 chris chris 2001731584 Jun  4 21:45 ArchLinuxARM-olinuxino-2013-06-04.img
[chris@thinkpad Downloads]$

The directory listing under http://os.archlinuxarm.org/os/ will also tell you the modification time of the image. Our image has now the name ArchLinuxARM-olinuxino-2013-06-04.img.

The default behaviour of gzip is to delete the .gz file after it decompresses, if you want to keep it use the -c option to uncompress the file to stdout:

[chris@thinkpad Downloads]$ gzip -dc ArchLinuxARM-olinuxino-2013-06-04.img.gz > ArchLinuxARM-olinuxino-$(date +%Y-%m-%d -r ArchLinuxARM-olinuxino-latest.img.gz).img

Read more...

Using the Watchdog Timer in Linux

The Software Watchdog

First: build the Linux kernel with watchdog support, the full guide is located here:

Device Drivers  --->
  [*] Watchdog Timer Support  --->
    -*-   WatchDog Timer Driver Core
    <*>   Software watchdog

After a reboot with the new kernel there should be a /dev/watchdog file:

[root@alarm ~]# ls -l /dev/watchdog
crw------- 1 root root 10, 130 Jan  1 01:00 /dev/watchdog
[root@alarm ~]#

Next: you will need to install a watchdog daemon:

[root@alarm ~]# pacman -Sy watchdog
:: Synchronizing package databases...
core is up to date
extra                    450.8 KiB  7.72K/s 00:58 [######################] 100%
community                483.5 KiB  6.38K/s 01:16 [######################] 100%
alarm is up to date
aur is up to date
resolving dependencies...
looking for inter-conflicts...

Targets (1): watchdog-5.12-2

Total Download Size:    0.04 MiB
Total Installed Size:   0.18 MiB

Proceed with installation? [Y/n] y
:: Retrieving packages from extra...
watchdog-5.12-2-arm       41.9 KiB  76.1K/s 00:01 [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
(1/1) installing watchdog                          [######################] 100%
[root@alarm ~]#

Read more...

How to check the memory usage of your embedded Linux system

After reading a forum post here I had to check the the memory usage off my OLinuXino too.

I have a bit more free men but also not that mush:

[root@alarm ~]# cat /proc/version
Linux version 3.7.4-dirty (chris@thinkpad) (gcc version 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305] (GNU Tools for ARM Embedded3
[root@alarm ~]# cat /proc/meminfo |head -n 10
MemTotal:          52188 kB
MemFree:           17636 kB
Buffers:            2380 kB
Cached:            19232 kB
SwapCached:            0 kB
Active:             9036 kB
Inactive:          17256 kB
Active(anon):       4708 kB
Inactive(anon):      168 kB
Active(file):       4328 kB
[root@alarm ~]# ps -aux |head -n 10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  2.1  4.8   4768  2516 ?        Ss   01:00   0:03 /usr/lib/systemd/systemd
root         2  0.0  0.0      0     0 ?        S    01:00   0:00 [kthreadd]
root         3  0.1  0.0      0     0 ?        S    01:00   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    01:00   0:00 [kworker/0:0]
root         5  0.0  0.0      0     0 ?        S<   01:00   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        S    01:00   0:00 [kworker/u:0]
root         7  0.0  0.0      0     0 ?        S<   01:00   0:00 [kworker/u:0H]
root         8  0.0  0.0      0     0 ?        S    01:00   0:00 [watchdog/0]
root         9  0.0  0.0      0     0 ?        S<   01:00   0:00 [khelper]
[root@alarm ~]#

Disable systemd-journald storage

It seems systemd-journald is using most memory on my system:

[root@alarm ~]# ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5
10.5  0.4  69648    58 /usr/lib/systemd/systemd-journald
5.6  0.1   7040    96 /usr/sbin/syslog-ng -F
4.8  0.5   4768     1 /usr/lib/systemd/systemd
4.2  0.0   6272    94 /usr/sbin/sshd -D
3.3  0.1   3268   140 -bash
[root@alarm ~]#

Read more...

Control your light at home with a Raspberry Pi and Olimex’s MOD-IO2

Since I’ve won the RPI-UEXT + MOD-IO2 I had to test it and this is the result.

Controlling your light is just an example, you can control nearly everything from everywhere in the world.

Read more...

Control the MOD-IO2 via i2c on Raspbian

This is just a short summary, the full guide is located here.

Edit /etc/modprobe.d/raspi-blacklist.conf and comment out the line that says blacklist i2c-bcm2708, then reboot.

pi@raspberrypi ~ $ sudo vim /etc/modprobe.d/raspi-blacklist.conf
pi@raspberrypi ~ $ cat /etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't need them)

blacklist spi-bcm2708
#blacklist i2c-bcm2708
pi@raspberrypi ~ $ sudo reboot
Broadcast message from root@raspberrypi (pts/1) (Mon Jan 21 11:28:05 2013):
The system is going down for reboot NOW!
pi@raspberrypi ~ $

After the reboot:

Read more...