Tuesday, July 18, 2017

Tiny script for connecting to wireless AP

# Connect to a Wireless Router
# $1 = interface (use ip link)

#!/bin/bash

# Enable the interface
ip link set $1 up
# Find access point names
iw dev $1 scan | grep SSID

# Select SSID and enter password
printf "SSID: "
read SSID
printf "pass: "
read -s PASS

# Connect
TEMPFILE=`mktemp`
wpa_passphrase $SSID $PASS > $TEMPFILE
wpa_supplicant -i $1 -c $TEMPFILE -B
rm $TEMPFILE
dhcpcd $1

Wednesday, May 17, 2017

RTAGS Quick Setup

RTAGS is a tool for C/C++ code navigation and completion (primarily for Emacs). Here's how to get it working quickly on Arch:
  1. Get the tool: git clone https://aur.archlinux.org/packages/rtags-git/ ; cd rtags-git/ ; makepkg -si
  2. Alternatively run `rtags-install` within emacs. 
  3. Start the server: rdm &
  4. Index your project: cd project-code-dir/ ; make -nk | rc -c -
There are a couple of ways to index depending on your build tool (make, cmake etc). 
 Add to .emacs :

(require 'rtags)
(require 'company-rtags)
(setq rtags-completions-enabled t)
(eval-after-load 'company
  '(add-to-list
    'company-backends 'company-rtags))
(setq rtags-autostart-diagnostics t)
(rtags-enable-standard-keybindings)

(define-key c-mode-base-map (kbd "M-.") 'rtags-find-symbol-at-point)
(define-key c-mode-base-map (kbd "M-,") 'rtags-location-stack-back) 



You may want to enable automatic starting of rdm:;; autostart rdm when entering c modes
(add-hook 'c-mode-hook 'rtags-start-process-unless-running)
(add-hook 'c++-mode-hook 'rtags-start-process-unless-running)
 


See also:
https://github.com/Andersbakken/rtags
http://syamajala.github.io/c-ide.html

Saturday, March 18, 2017

Arch installation checklist (i3 desktop)

During basic setup remember to install base-devel as well as base. After basic setup and first reboot:
  1. Check network (ping 8.8.8.8). If down, find adapter name using ip link and enable dhcp using systemctl enable dhcpcd@<adapter name>.service
  2. Add a user:  useradd -m -G groupname -s /bin/bash username. Enable sudo for that user by editing /etc/sudoers.
  3. Install:
    xorg-server  xorg-apps  xorg-xinit  [graphics-driver] #xorg stuff
    i3 dmenu
    lxappearance compton # Desktop environment
    terminator/urxvt firefox git emacs  feh bash-completion #Apps
    dialog wpa_supplicant wpa_actiond #wireless
  4. Setup xorg to autostart i3. Add exec i3 to ~/.xinitrc (cp /etc/X11/xinit/xinitrc ~/.xinitrc).
  5. Setup dot-files for i3, i3status, bash (+aliases), emacs and more (via dotfile repo).
  6. Update X fonts by running lxappearance.
  7. Setup AUR applications (e.g. Dropbox) 
  8. For wireless, generate configurations by running wifi-menu and enable automatic connection management: sudo systemctl enablenetctl-auto@<interface>.service where interface is the name the wireless card (use ip link to see it) .

Monday, September 29, 2014

Imagemagick annotate and animate

Anotate an image:

$convert in.jpg -fill white -pointsize 20  -annotate +20+30 'Blahblah' out.jpg

Make a looping gif of all jpg images in the current folder with 50 ms delay between frames:

$convert   -delay 50   -loop 0  *.jpg  my.gif


Tuesday, June 24, 2014

Makefile template

A makefile to be used as template. It automatically creates header dependencies using the -MMD flag of gcc so the user only needs to specify the source files. Libraries and compiler flags are generated using pkg-config.

CXXFLAGS =    -g  -I/usr/include/blah $(shell pkg-config --cflags thelib)
LIBS     = $(shell pkg-config --libs thelib) -lotherlib 
CXX      =  g++
SOURCES = source.cpp
OBJS = $(SOURCES:.cpp=.o)
DEPS = $(SOURCES:.cpp=.d)

TARGET =    a.out

$(TARGET):    $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all:    $(TARGET)

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET) $(DEPS)

-include $(DEPS)

Friday, September 20, 2013

Seeting up transmission web interface

Transmission is a popular bittorrent client that has a very useful web interface. Its ideal when run on an always-on machine such as a home server or an HTPC.

1. Install transmission: sudo apt-get install transmission-daemon

2. Make a start_transmision.sh file and add the following:

sudo service transmission-daemon stop
transmission-daemon -a 192.168.*.* -c ~/torrents-to-download/ --incomplete-dir /tmp/

-a is the list of IPs that are allowed to connect to the web interface
-c is a folder for placing torrents. Torrents placed here are automatically downloaded

3. chmod u+x start_transmision.sh and run

4. Check your IP by running ifconfig

5. Now any machine on the allowed list can connect to the web interface from a browser using the address: yourIP:9091

Setup static ip on Ubuntu Server

Edit /etc/network/interfaces

Change  iface eth0 inet dhcp to
iface eth0 inet static
address 192.168.1.200
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1 
dns-nameservers 8.8.8.8 8.8.4.4


Since most other systems on your network will use dhcp, pick an address that doesn't cause conflict. Most routers will assign small numbers 1st (192.168.1.[1,2,3..]) so pick a big one. Obviously, the rest of the settings need to be change to match your network.

Thursday, September 19, 2013

Setting up a home file server

1. Download Ubuntu Server and copy to a usb stick using UNetbootin.

2. Boot your server from the usb stick.

3. The installation is pretty much straightforward. Just select "guided partitioning" and the disk/partition that you want to install to and the installer will do the rest.

4. When prompted for packages to install, select ssh-server and samba.

5. After booting into the new system we need to setup a few things.The 1st is to  set up a static IP so that you can connect to the server from elsewhere using ssh.

6. Next we need to setup samba so that we can share our files.

$ nano /etc/samba/smb.conf


Here we can change the workgroup name to match the workgroup name on the machines that will be connecting to the server (WORKGROUP is the Windows 7 default). We also need to specify the folders on the server that will be shared on the network. We do this by adding the following entry at the end of smb.conf:

[public]
comment = Just a comment
path = /path/to/share/point
read only = no
guest only = yes
guest ok = yes

Add one entry per share point and change the path accordingly.

7. Additionally we can install transmission so that our file server can grab torrents.

Tuesday, July 9, 2013

Remote Camera Control

Gphoto2 is a library/command line tool that can be used to remotely control a digital camera (DSLR) connected to via usb. Under Ubuntu gphoto can be installed from the repository:
sudo apt-get install gphoto2

The following are a few of the available commands to get started:

1. Shoot camera and download image as test.jpg
gphoto2  --capture-image-and-download --filename=test.jpg

2. Download all files on the camera to PC
gphoto2 --get-all-files

3. Show the camera configuration options
gphoto2 --list-config

Some examples from running the above on a Canon EOS 10D:
/main/imgsettings/iso
/main/imgsettings/whitebalance
/main/capturesettings/aperture
/main/capturesettings/shutterspeed


4. Change camera configuration
gphoto2 --set-config /main/capturesettings/shutterspeed=1/8

By combining the above commands in simple scripts we can do stuff like HDR imaging and time lapse:

Shoot three exposures using gphoto and convert to HDR using luminance:
for i in 0.5 1 2; do  gphoto2 --set-config /main/capturesettings/shutterspeed=$i --capture-image-and-download --filename=$i.jpg; done;
 luminance-hdr-cli  -s hdr.exr *jpg

Take a picture every 3 days:
for i in {1..10}; do  gphoto2--capture-image-and-download --filename=$(date -I).jpg; sleep 3d; done;

Wednesday, June 5, 2013

ImageMagick Cheat Sheet

# negate
$ convert black-white.png -negate white-black.png

# rotate
$ convert upside-down.png -rotate 180 rightside-up.png

# resize
$ convert img.gif -resize 64x64 resized_img.gif

#convert to grayscale 
$ convert -type Grayscale color.jpg gray.jpg

# find the differences between two images
$ composite img1.jpg img2.jpg -compose difference diff.jpg

# compare images using a metric (can be mse, psnr and more)
$ compare -verbose -metric mse 1.jpg 2.jpg difference.png

# compose two images together using a mask (stencil)
$ convert -size 512x256 tile:img.jpg  tile:img2.jpg mask.jpg -composite result.jpg



Thursday, May 9, 2013

Keep command running after logging out

Use nohup to keep a command running after you exit the shell. Useful for running stuff on remote machines without having to be logged in.
  1. Login to a machine
  2. nohup command &
  3. Logout, command keeps running

Tuesday, April 23, 2013

Multiple binaries in a single Eclipse CDT project

Sometimes it is convenient to have a single Eclipse project that produces multiple executables. An example is a set of tools that share some base code like a set of image manipulation tools that share some common image loading / saving code.  In a situation like this, the source code has multiple source files that contain a main() function. The standard way to do this is to create a makefile eclipse project and edit the makefile so that you have a target for each executable:

binary_1: main_1.cpp shared_code.cpp
       gcc -o  $@  $^

binary_2: main_2.cpp shared_code.cpp
       gcc -o  $@  $^

If for some reason you want to use a managed eclipse project (where the makefile is automatically generated) do the following:
  1. Create a managed project (File > New  C++ Project > Executable)
  2. Add the source code containing multiple main() functions
  3. Go to Project > Properties > C/C++ Build > Manage Configurations...
  4. Make a build configuration for each executable and name it appropriately (you can clone existing configurations like Debug and Release).
  5. From the project explorer, right click on each source file that contains a main() function > Resource Configurations > Exclude from Build and exclude all build configurations except the one that builds the executable with this main() function
  6. All other code is included in all build configurations by default. You may need to change this depending on your application.
  7. You can now build an executable for each main function by going to Project > Build Configurations > Set Active , Project > Build Project
To debug each executable:
  1. Build all binaries (as explained above) 
  2. Go to Run > Debug Configurations.
  3. Create a configuration for each binary
  4. For each configuration set the correct Build Configuration and C/C++ Application (this is the binary). 
  5. Use Search Project... to locate the binaries easily 

Thursday, April 11, 2013

Pipe to clipboard


Copy the output of a command into clipboard:
echo adafasdfa | xclip

Useful for pasting command line output into text editors, spreadsheets etc.
xclip -o | grep name

Useful for pasting large selections (etc from a browser). Equivalent to middle clicking the terminal (but less messy).

Wednesday, April 10, 2013

Compile using pkg-config

pkg-config makes it easier to compile your source when you need to link libraries. For example, building a Qt application that has OpenGL support requires the following build command:

g++ source.cpp -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4/QtGui  -lQtOpenGL -lQtGui -lQtCore  

With pkg-config that becomes:

g++ source.cpp `pkg-config --libs --cflags QtCore QtOpenGL `

Note the use of back-quotes  ``  to do in-line substitution. 

pkg-config can be integrated into eclipse through a pluggin and is also supported by qmake. Just add the following to your .pro file:

CONFIG += link_pkgconfig
PKGCONFIG +=  name_of_library

Monday, April 8, 2013

Make an image montage using ImageMagick

The following makes a 1x10 vertical montage of all .png images in the current folder. Before adding to the montage, images are cropped.

$ montage -geometry +0+0 -tile 1x10  -crop 170x70+115+125 *png montage.jpg

-geometry controls the border arround each sub-image in the montage. +0+0 means no border.

How to decode the crop string "170x70+115+125" :

170x70: new size of the cropped image
+115+125: Pixel (0,0) of the cropped image will be pixel (115,125) of the old one. Pixel (170,70) of the cropped image is pixel (170+115, 70+125) of the old one.

Convert HDR files to EXR and tonemap using pfstools

Convert all .hdr files to .exr using pfstools. This will replace the hdr extension to exr.

$ for img in *.hdr; do pfsin ${img} | pfsout ${img%%.hdr}.exr; done


Similarly, tonemap multiple HDR files using a variety of tonemapping operations:

$ for img in *.exr; do pfsin ${img} | pfstmo_mantiuk08|pfsout ${img%%.exr}.ppm; done

pfstmo_mantiuk08 is one of many available tonemap operations available in pfstools.

Mounting remote folders using ssh and SAMBA


Two ways to mount your remote folders. Use sshfs over unreliable network. Get uid, gid by running id on linux.

mount -t cifs //samba_server/folder ~/yoho/ -o user=username,domain=COMPANY,uid=xxxx'

This will mount remote /folder to local folder ~/yoho.

sshfs -o workaround=rename -o reconnect -o idmap=user -o uid=xxxx -o gid=xxxx -o allow_other username@computer_with_ssh_access.company.com: ~/yuhu/

This will mount the home folder of user username at local folder ~/yuhu.


Record webcam video

vlc v4l2:// :v4l2-dev=/dev/video0 :v4l2-width=320 :v4l2-height=240  --sout '#transcode{vcodec=mp4v,vb=3072,scale=1,acodec=mpga,ab=192,channels=2}:duplicate{dst=display,dst=std{access=file,mux=mp4,dst="/tmp/test.mpg"}}'

Webcam is typically /dev/video0 in linux

Grab video frames using VLC

The following grabs every fifth frame starting from second 0:20 and ending on 0:30 (10 seconds). Frames are saved in ./out/ . The number of frames captured depends on the number of frames per second of the video.

vlc video.avi --video-filter=scene --vout=dummy --start-time=20 --stop-time=30 --scene-ratio=5 --scene-prefix=frame --scene-path=./out vlc://quit

How to install pfstools Matlab support (HDR, EXR)

Pfstools is a set of command line programs for reading, writing and manipulating high-dynamic range (HDR) images and video frames. This is how to install for Matlab:
  1. Get pfstools http://pfstools.sourceforge.net/
  2. ../configure --prefix=$HOME/local --with-mex=/path/to/matlab/bin/mex (this installs in home folder)
  3. make
  4. make install
  5. From Matlab, File, Set Path, set it to $HOME/local//share/pfstools/pfstools_matlab/
Use --prefix=/path/ to istall to custom path. By default it installs to /usr/local/
Note: Most linux distros have a pfstools package. if you don't need Matlab support install that instead.