Tuesday, February 19, 2013

Webcam on Linux

Camera commandline tools on Linux
  • fswebcam
  • streamer
  • uvccapture
  • motion

Example to use fswebcam:


fswebcam -d /dev/video0 -r 320x240 mypic.jpg
 

This will use webcam video0 as a camera device with a resolution of 320x240 pixels and outputs it to the file mypic.jpg.

Another nice feature is to use fbi (on Ubuntu) to display the picture in a terminal.
If you don't have fbi, install it with:
sudo apt-get install fbi
Than show an image as follows:
fbi mypic.jpg
or all available pictures:
fbi *.jpg
With  PgDn or PdUp you can go to the next or previous picture.
If it doesn't work for you, you should google for it, it has something to do with vga setting.

Another very useful tool is ImageMagick for processing images. I'll cover that next time.

Thursday, February 14, 2013

Crosscompiling for Raspberry Pi


Linux crosscompile for the Raspberry Pi

I was looking for a crosscompiler to compile executables for the RPi.
On google I found links about how to build your own toolchain, but I rather have a ready made toolchain instead of cooking my own. So I found the following link:
https://github.com/raspberrypi/tools

According to http://qt-project.org/wiki/RaspberryPi_Beginners_guide, it's based on 32 bits machines, so you also need
to install ia32-libs if you work on a x64 based machine.

...and I assume you use a Linux system like Ubuntu or some other flavor.

If you want to cook your own toolchain, this might be a good link for you:
http://www.kitware.com/blog/home/post/426

Wednesday, January 23, 2013

Pathogen, the Vim plugins manager

Pathogen
After installing a few plugins I started to get a little bit annoyed by copying the plugin files to the .vim folder after downloading. It's not a big problem though, but after some plugins, it starts to get messy. You can't see directly what plugins you've installed or what could cause problems and so on...

So to manage the plugins in a proper way, I use Pathogen to do the job. What you have to do is download the plugin from:
https://github.com/tpope/vim-pathogen

Copy the 'autoload' folder to your .vim folder. Actually, the first time you use Vim you should  install pathogen. Create another folder in .vim and call it 'bundle'. The bundle folder is where  you put all your future Vim plugins:
/.vim
   |___ autoload/
   |___ bundle/
              |___  snipmate/
              |___  taglist/
              |___  .../

So every time you download a plugin, you just copy the complete plugin folder in .vim/bundle/
You could also go in the .vim/bundle/ folder and download a plugin using Git, like this:
Assuming you're in the .vim/bundle/ folder.
# git clone https://link.to.vimplugin.git

You just need a small piece of script added in your .vimrc:
execute pathogen#infect()
syntax on
filetype plugin indent on

That's it! Try this out with snipmate plugin, as this plugin doesn't need any other configuration in your .vimrc file. If it works for you, than you can download all your favorite plugins and all are hierarchically structured.

Tuesday, January 22, 2013

Vim plugins for C/C++ development

To use Vim as an editor for C/C++ (and other programming languages as well), we need to extend Vim with some handy plugins. Vim is a powerful editor, it really is, but you need some plugins to make it more powerful to your needs. Vim is not a code editor out of the box, so you can not expect to have cool stuff like Visual Studio's intellisense, or jumping to definition of a function and so on. So to make our coding life easier, we can install Vim plugins to have more fun while coding.

First, here's a list of Vim plugins below I use it for writing code:
  • Surround
  • Matchit
  • Snipmate
  • Clang_complete
  • NERDTree
  • TagList

Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.

Put cursor on head tag and enable V mode


Hit % to jump to the matching tag


Link: http://www.vim.org/scripts/script.php?script_id=39

 
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!

Typing 'for' before hitting TAB




Result after hitting TAB


Link to download snipmate plugin: https://github.com/garbas/vim-snipmate

Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"

Before typing yss" in normal mode

After typing yss" in normal mode

After typing yss} in normal mode

After typing cs}) in normal mode

When using visual selection (v or V) than use capital S for surrounding, i.e. veS' is the same as ysw'.
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m

For more detailed information go to link: https://github.com/tpope/vim-surround


NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.

I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)

By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap   :NERDTreeToggle

Example of NERDTree

Link: https://github.com/scrooloose/nerdtree


TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.

I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.


TagList example on the right side





Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you  need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev

Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete

Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:



Tuesday, October 18, 2011

Voice Interface

Since Apple brilliantly introduced the touch interface, I thought now it's time to introduce Voice interface. It's especially useful when a car driver has to focus on the road. You just speak some commands and a smartphone responses back to you. I mean, you need some kind of feedback. The difficulty seems to me the intelligence of signal processing. Such a device needs to know the context when it hears someone talking. Is it talking to the device? Is it someone just talking to someone else etc...

From what I know, there are ear devices, like ear aids, which can hear from which directions the sound comes from. We can also filter the sound for speech and we have overcome two problems: Direction and speech filtering (with noise reduction). Next is to distinguish real speech commands from normal speech conversation. But that's up to the intelligence of the software to interpret. Another helpful thing is to recognize the right voice. If someone else is talking, the device can ignore it. If this is designed very well, it should be possible to create a good device that listens to you and help you performing some tasks, while focusing on other more important things (like watching the road for obstacles, though today smart cars can warn you for obstacles too).

My colleagues at work don't believe this as a good solution to safely drive a car while commanding a navigation device to set a new location. But I believe this will be a great solution, just like Apple did with the touch interface. Apple was always ahead with design and usability, while others stick to the old fashion technology. Well, I'm not that kind of people, I'm more like Steve Jobs :-)

To be continued...

Monday, October 10, 2011

Git tips!

When you work on a project managed by Git, you have to type a lot to see status and results. For example, to seen which branch you're working on, you have to type:
#git branch
You'll see a list of branches and the one with an asterisk is the current branch you are on at the moment.

If you want to switch to another branch you have to do like:
#git chekout nameofbranch

There are some nice stuff we can do, like showing the current branch in the prompt!
Put the following lines in your .bashrc file:
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\]\w\[\033[01;33m\]$(__git_ps1)\[\033[01;34m\] \$\[\033[00m\] '


Furthermore, adding .gitconfig file in your home directory will automatically change the behavior of Git. My .gitconfig looks like following:


[user]
name = My Name
email = My.Email@gmail.com


[alias]
ci = commit
co = checkout
st = status
di = diff
br = branch


[pager]
color = true


[color]
ui = true


[core]
editor = vim


[format]
pretty = oneline

Especially those alliases are cool. If I want to checkout to a branch, I can do this:
#git co nameofbranch

Thursday, October 6, 2011

Vim - Omnicompletion - ctags!

How to get intellisense like (omnicompletion) feature on Vim for standard C libraries!
If you're a Vim user and also code C sources in Vim, than you quickly find yourself switching between terminals or browser to look for the type of arguments you need for standard C functions like strcmp() or strtok() or whatever. Well, not anymore! I found a lot of examples how to use omnicompletion whitin Vim, but almost all of them assume you need omnicompletion for C++, php, python or whatever. I googled my ass off to find an example of omnicompletion for standard C libraries.
First, what I did that didn't work. I looked at the folders where those standard c include files reside and found those in /usr/include/ directory. So I was analyzing the files for C only headers like stdio.h and such. I was also looking for Linux's systemcall files, like sockets etc...
Since version Vim 7.0 Vim supports omnicompletion and to work with that feature Vim needs a ctagged file (a file that is generated by ctags). This tag files contains a list of functionnames, prototypes, defines and other C related stuff, formatted in an indexed way so it can be looked up in a fast and efficient way (I guess, I'd rather use an SQLite engine as a solution). So to put it all together, we need to make a tag file using ctags. On Linux distros like Ubuntu, it's installed by default, but if you don't have one, google for exuberant ctags, download it and install it.
To generate a ctags file, you need to do the following:
# ctags /usr/include/*.h
This will generate a ctags file named tags.
If you open a c file and try out omnicompletion on the word "prin" by hitting Ctrl+x Ctrl+o, Vim won't give you a printf in the list of possible functions. Well, that sucks :(
One of the solutions is figure out how ctags work! Well, I'm sorry, I just don't have the time to do that just because I happen to want omnicompletion for standard C libraries in Vim.
The other solution is this: Be sure you downloaded and installed apt-file. If you never used apt-file than run "apt-file update". After that do the following:
Create file "libcs-ignore" with the following content:
__attribute__
__attribute_deprecated__
__attribute_format_arg__
__attribute_format_strfmon__
__attribute_malloc__
__attribute_noinline__
__attribute_pure__
__attribute_used__
__attribute_warn_unused_result__
__wur
__THROW
__nonnull+


Than execute:
# apt-file list libc6-dev | grep -o '/usr/include/.*\.h'> ~/.vim/tags/libc6-filelist
Finally do this:
# ctags --sort=foldcase --c++-kinds=+p --fields=+iaS --extra=+q -I./libc6-ignore -f ~/.vim/tags/libc6 -L ~/.vim/tags/libc6-filelist

And voila! You're done, you're finished! Try it out, but don't forget to set the tags in Vim:
# :set tags+=path/to/tag

Somehow, when ctags process the files, it gets fooled by those attributes listed in libc6-ignore file. -I option tells ctags to ignore thow words, so functionname will be parsed with the right functionname instead of __attribute__ etc... 



.


I found this solution from the following link:
http://stackoverflow.com/questions/1632633/ctags-does-not-parse-stdio-h-properly

NSNotification example