Category Archives: Tech

How to give permissions to devices

Many a time you face with this challenge that as a user you do not have permission to read write a device file in Linux without becoming sudoer.

Example: Let us say you have a USB device say a camera or any other device file let us say /dev/uinput the file for multi touch.
You want to read write these files in your program.

You can only run this program with sudo as the device files will not be accessible otherwise.

There is a workaround to make these files read/writable once and for all by doing something like:

sudo chmod 777 /dev/uinput

But You need to do above on each restart and also not a great way to handle this problem.

There is an easy way by creating rules file inside /etc/udev/rules.d.
For example:


# DVP sensor driver rule

KERNEL=="video0", SUBSYSTEM=="video4linux", ATTRS{name}=="vfe_0", GROUP="video", MODE:="766"

All ip assigned within a network

Many a time you wanted to know the assigned ip addresses in your LAN. My use cases:

I was working on nano pi neo cheapest things can be!!

You don’t want to move from your ass but this is more of a reason when you already know how to do this 🙂

Okay so on windows you have Advanced ip scanner. You can even log into your router and get to see the client list this is always the easiest way but (ohh but again)..   what if you have reset your router password. Most of the time you will forgot it!! It is not often that you need to login into your router so reset password means forgot. Do you want to reset the router password now just for this with router’s reset button. Or you are in office or you don’t own the router.

Anyway whatever your cause is here is a very easy command to get the list of all assigned ip using linux system:


sudo apt-get install nmap


:~$ sudo nmap 192.168.100.5/24 -sP

Starting Nmap 6.40 ( http://nmap.org ) at 2017-01-04 18:23 IST
Nmap scan report for domain.name.dlink.com (192.168.100.1)
Host is up (0.00024s latency).
MAC Address: 1C:5F:2B:4D:05:71 (Unknown)
Nmap scan report for 192.168.100.26
Host is up (0.00028s latency).
MAC Address: FA:7B:DE:ED:46:55 (Unknown)
Nmap scan report for 192.168.100.4
Host is up.
Nmap done: 256 IP addresses (3 hosts up) scanned in 3.27 seconds

If there are not much of machines in your LAN you will figure out which machine has what ip or you can ssh to each to verify. Without nmpa also you can ping all the ip and get the list but nmpa also tells you the friendly name of the machine.
 

 

Clone an SD-card

You will find many ways to clone an SD-card there are softwares like win32diskimager (A great tool for all practical purposes related to flashing cloning). You have the famous Linux dd command.

The problem arise when you are working on a bigger sd-card say 64 Gb. You have done something really cool and you want this to be in production or just share this with your friend so they will not spend 4-5 hours following your script to recreate the same image. You can just clone your card and zip it that will make the footprint small because anyway most of the 64 Gb part was empty to zip has done it’s trick but what if your friend has a 8 Gb card. You shouldn’t have started with a big card? Anyway cloning the whole card look very ugly to me (time factor is also there).

How to cleanly clone a SD card?

This is very simple I will explain it first then we will walk through a sample use case with all the commands. All the exercise is on Linux machine.

Steps:

  1. Insert the SD-card on Linux machine and get the device name e.g. /dev/sdb sdb1 sdb2
  2. Start Gparted and select your device from top. sudo apt-get install gparted If you do not have this. I like using the GUI interface but if you are the bearded Unix man you can just use the parted command.
  3. Shrink your partition with the graphical interface. You need to unmount the partitions if it is auto mounted. You can do this from the gparted UI itself by right clicking on the partition and selecting unmount. After the device is unmounted(all its partitions) you can select a partition and see the options of deleting the partition shrinking/resizing it.
  4. gparted will automatically recognize the unused space with some margin and give you option to shrink it to that much space. shrink your partition and click save to apply it. This will take some time.
  5. Now you have just sufficient amount of space for each partition unused area is mark unallocated now. We can now clone the SD-card as usual.
  6. Just check the memory used via your favorite software/command then copy using dd   sudo dd if=/dev/sdb of=~/shrinked.img bs=1M count=2470

The way to it

Now let us walk through an example with all the commands:

:~$ sudo gparted

:~$ sudo dd if=/dev/sdb of=~/myRaspberryPi.img bs=1M count=2470

:~$ sudo fdisk -l

Partition table entries are not in disk order

Disk /dev/sdb: 64.0 GB, 64021856256 bytes
64 heads, 32 sectors/track, 61056 cylinders, total 125042688 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00042380

Device Boot Start End Blocks Id System
/dev/sdb1 49152 131071 40960 83 Linux
/dev/sdb2 131072 5160959 2514944 83 Linux

You need this information to know the size you have to copy with dd command.

:~$ sudo dd if=/dev/sdb of=~/eyerisDvpCam.img bs=1M count=2470

2470+0 records in
2470+0 records out
2589982720 bytes (2.6 GB) copied, 82.9483 s, 31.2 MB/s

Do you know SD stands for Secure Digital?

There are some open points like:

  • What if your partitions are not continuous?
  • What if there is a big unallocated space in the beginning of the sd-card?

You can use skip with dd to reject the initial space I tried once didn’t worked for me got image was corrupted. May be I have done it incorrectly. Then you can also copy your partitions one by one and then create an image separately by joining them.

command with skip

:~$ sudo dd if=/dev/sdb of=~/myCoolTvSkip.img bs=1M skip=23552 count=2444

2444+0 records in
2444+0 records out
2562719744 bytes (2.6 GB) copied, 107.389 s, 23.9 MB/s

Kodi with tvaddons

This really is Future of television Open source entertainment

I am not a fan of news and television programs broadcasted in India so my tv was just lying there never being used. I had a spare CPU connected with tv but it wasn’t used extensively. You can watch movies tv-series on your laptop so why bother.

I was thinking about buying chromecast but wasn’t sure how helpful it is another option was Teewe. There are so many from chinese vendors (Allcast, MiraScreen, Anycast etc). I was weighing on which one to buy then I realize that I have a spare board which I ordered sometime back on aliexpress when I was experimenting with raspberry pi/cubieboard/beaglebone/odroid.

The board name is not important as you can use any of the boards you will find someway to run kodi. I realized there is a android lollipop image available for that board. Just took the micro-sdcard from my camera and flashed the image. It was okay fast. Downloaded some apps like netflix, hotstar, gaana, wynk music, google drive, one drive, youtube, tvf, Kodi etc. The option would have been to use Open-elec. That will be faster but android suited my purpose it had a familiar interface for everyone. Open-elec may scare some new friend using the television.

I was thinking of signing into netflix then I realized let me first try Kodi. I saw some youtube videos on how to add addons on kodi and boom. It was so simple to setup everything. with tvaddons my simple plain board which was useless has made my tv a smart useful piece of entertainment system. I can watch any movie, tv -series in no time. You name it and it is there. I tried some recently released movies and it is there.

No torrent required no wait for download etc. anyway now torrent sites are getting banned in India this will also get blocked someday but for now enjoy streaming from putlocker. primewire, alluc etc.

There is one more service which I liked most it is called Trakt.tv. This is really cool service for movies and tv-series watchlist. It is better than IMDB for this particular service. Since you can directly configure it with Kodi it will keep tracking what you are watching just like last.fm for movies/tv-series.
I like the web interface and the data they show. Example:

 

Screen Shot 2017-01-05 at 10.48.02 PM.png

 

Even better:

Screen Shot 2017-01-05 at 10.49.28 PM.png

Trakt.tv may be a cancer for privacy and for pirate users as the data is synced on their server and can be used/shared. Technically you can make your profile private by checking the box in your profile settings page.

Screen Shot 2017-01-05 at 10.51.46 PM.png

If you open a movie or tv-series page you will get lot more user insight:

Screen Shot 2017-01-05 at 10.59.08 PM.png

I generally used Exodus and for most of the time I am just satisfied with the service. I have other addons but I never feel any need to use them.

 

 

Qt and it’s bug

Sometime you spend hours just to realize it is not you who is using the platform incorrectly but the platform itself is released with some limitation. Sometime the limitations doesn’t make any sense.

So I wanted to have menu like options in my application with qt. The options you see in almost all applications on top => File, Edit, Settings, Help etc when you click on it you see a list of sub-options.

As expected it is very simple to do it in Qt. It is widely used feature isn’t it?

menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem { text: qsTr("Hide"); onTriggered: windowClosing() }
MenuItem { text: qsTr("Exit"); onTriggered: mainwindow.exitApp() }

.. ..

I was using Qt5.5.1 with qml I made the menus add listeners everything seemed fine. Then I realized that I can’t click and select the sub-options by using touch screen(means touch) on windows with mouse it is okay but not with touch. It was like 3-4 hours of effort to do everything to realize that it is useless. I even updated my qt version to latest.

I searched and searched may be I was doing something wrong, no it is a known problem.

Menu bar doesn’t work with touch qt

I wasn’t going to stop using the menu feature and make buttons and windows for all these options now. I have added extra options just because I have it all so simple. I have added all these features in my application.

I was searching for workaround and there was one with QMenuBar it isn’t a workaround per say a different way of doing same thing with Qt and QMenuBar doesn’t have this bug. Now I just have to remove all my qml code and do things with Qwidgets etc which is again pain lots of code changes.

There is another way to handle all this using ‘createWindowContainer’  a sweet useful feature to create a parent widget with your existing qml window. You will get handle of the parent window widget which has your qml window contained inside it. It was such a saver for me.

QWidget *container = QWidget::createWindowContainer(qmlWindow, myWidget);

I created a widget class by inheriting Widget to add some extra features like I do not wanted to close the application when window close is clicked.

Some part of the code just to make things clear:

QWindow *qmlWindow = qobject_cast(this->rootObjects().first());

MyWidget* myWidget = new MyWidget();
QWidget *container = QWidget::createWindowContainer(qmlWindow, myWidget);
container->setMinimumSize(qmlWindow->size());
widget = new MyWidget();
QGridLayout *grid = new QGridLayout(widget);
grid->addWidget(container,0,0);
widget->show();

createActions();

menuBar = new QMenuBar();
createMenus();
widget->layout()->setMenuBar(menuBar);
showWindow();
connect(widget, SIGNAL(closeWindow(QCloseEvent*)), this, SLOT(onClosing()));
connect(myWidget, SIGNAL(closeWindow(QCloseEvent*)), this, SLOT(onClosing()));

First you get the qmlwindow handle using which you created the container window widget

actually there was one problem where I spent some extra time just to figure out that I can pass parent widget in createWindowContainer(qmlWindow); function and get my inherited widget work with it. createWindowContainer is a static function but luckily or intelligently they have given this option to pass a parent widget in it to make inheritance work.

createMenu and creatActions are functions to create all my menu and listener stuff.

Here is my special widget class inherited from QWidget

#include "mywidget.h"

MyWidget::MyWidget():QWidget()
{

}

void MyWidget::closeEvent(QCloseEvent *event)
{
emit closeWindow(event);
event->ignore();
}

Alexa on Raspberry pi

Amazon has done a good job for hobbyist by opening their Alexa API. They have even given the boilerplate code base to start.

You an create almost same experience as Amazon echo with Raspberry-pi.

After first sexting up raspberry pi by raspbian and then installing Debian. You need to configure raspberry. One mistake I did was installing raspbian and then installing debian from that while I should have directly written debian on my sd card. Installing debian via rasbian path is so much time consuming.

Anyway configuring raspberry-pi is very simple

after first debian boot do

sudo apt-get update

sudo apt-get upgrade

then

sudo rasps-config

Now enable ssh (in In advanced optionsI don’t know why they have disabled this to begin with (security reason I know but why?)

You can overclock your raspberry pi for better performance.
Alexa RPi

Follow the steps from:

https://github.com/alexa/alexa-avs-sample-app

Very simple to setup just need time.

At last open three tabs in command window T1, T2, T3

T1

cd ~/Desktop/alexa-avs-sample-app/samples
cd companionService && npm start

T2

cd ~/Desktop/alexa-avs-sample-app/samples
cd javaclient && mvn exec:exec
authorization
T3
To use the Sensory wake word engine, type –

cd ~/Desktop/alexa-avs-sample-app/samples
cd wakeWordAgent/src && ./wakeWordAgent -e kitt_ai

My experience with RPi-Alexa was not so great because I used old RPi which was not supported/mentioned in the blog. And frankly speaking this was damm slow not very usable. Overclock help a little bit. It was a good experience/fun and good toy 🙂 that is all I can say for echo. I don’t think it is really usable to be more than a toy right now. You can enjoy it for a month or two max.

Alexa has (great I am not so sure of) many addons which you can add to your raspberry pi via web interface. This can make it more interesting for may be more than a month. You can say like “Whats my briefing” You an play guessing games. Adding items to shopping list and emailing them setting reminder for gym, grocery shopping etc.

Some Commands in not any particular order:

  • Tell me a joke
  • Add X to my shopping list
  • Add 34 + 67
  • Who is X?
  • How tall is X?
  • Whats on my shopping list?
  • Create a new to-do.?
  • What time is it?
  • What time is it in Bengaluru?
  • What is the weather like in Bengaluru?
  • Set a timer for X seconds/minutes
  • What’s on my to-do list?
  • How old is X (Obama)
  • How many grams in an ounce?
  • News highlights
  • knock knock
  • Do you have any new features?
  • How do you say tomato in hindi /french
  • Define X
  • How many calories in a tomato ?
  • How do you spell entrepreneur ?
  • Flip a coin
  • Roll a dice
  • Rock paper scissor
  • When is holi?
  • Wikipedia X
  • One fish two fish
  • Do you like green eggs and  ham ?
  • Where are you from ?
  • I want the truth
  • Surely you can’t be serious.
  • I need to buy deodorant.
  • Good afternoon
  • What’s the meaning of life
  • Sing happy birthday
  • What day of the week christmas fall
  • Can you rap?
  • What’s my flash briefing?
  • Whats in the news ?
  • Alexa inspire me
  • How to make bloody marry ?
  • Start workout
  • Start Akinator
  • play jeopardy

For some you may need to enable addons like for news, for workout you need 7 -minutes workout.

Add Alexa in front of all command which makes it really annoying citing her name each time. They should have made it more smart and keep it triggered for at least 5 extra minutes. Could have add this to user agreement. I know people are worried about security, privacy. Amazon/Google listening our conversation always. But I guess main problem for amazon would have been understanding a whole para to make sense out of questions directed to Echo  when listening for five full minutes.

Some recording:

Reply for Alexa, When is holy?

Reply for Alexa, One fish two fish 

Some more:

 

ffmpeg command yuv toolkit for raw image/video

I was involved in driver development for a sensor there I faced this problem of viewing raw data:

ffmpeg -t 30 -f v4l2 -channel 0 -video_size 752×480 -i /dev/video0 -pix_fmt nv12 -r 30 -b:v 64k -c:v copy Desktop/raw.nut

save the raw file to play the raw file with vlc use below command

“C:\Program Files (x86)\VideoLAN\VLC\vlc.exe” –colorthres-saturationthres=0 “C:\Users\nandan\Desktop\raw.nut”

OR
run raw2_video.nut with vlc and change the saturation to zero.
in vlc Goto:
tools>effects and filter>video effects>
tick image adjust
set saturaion to 0.

For raw image viewing yuv toolkit is really good tried both on Ubuntu and windows
http://www.yuvtoolkit.com/

set resolution to your image size. If the image name has resolution in it yuv toolkit can pick that. Set Image format 8bit grayscale or whatever.

 

Python Notes – 1

I think python is best for any personal use (small scale development) prototyping ideas etc.

Some examples:
Like say I want to parse a document for some specific keywords and format the extracted data.
I want to get update when something on some website changes.
I want to teach a lesson to a spammer by sending him lots and lots of email.
For investing you want to make sense out of some stocks historic data. Using numpy and pandas. Using pandas reading data in meaningful way from a web resource is as easy as reading it from local file.
You want to download all the news paper resources (having a particular date format) from an internet location

All above are my actual use cases to give you an idea.Apart from this I think Python is made for interviews it is very simple to write algorithms or create a data structure in Python. (I guess)

That said now let us get to work and learn some python you will find many online resources really good resources or you can join open/free courses from coursera or similar sites. If you don’t want all this you can right away jump into coding and google(basically stack overflow) your requirements as it arises. (given that you have prior experience with programming and at least one language). This may be problamatic as it will not teach you to code in the Pythonic way. But you can cover that later by browsing some existing good resources/libraries.

Lots of talking now let us get back to work the really boring stuff in language learning syntax.

Note: The notes are all personal it is not meant to cover everything in the language. I am biased towards whatever interested me more or whatever was new for me.

** exponent
Example: 2**3 = 8 = 2 ^ 3

// Integer division/floored quotient

5//2 = 2 % gives you remainder as usual

List in python are object same as string although string are immutable similar to java string.

I don’t know why I assumed that python list should be like C++ STL vector. This was a shock to me when I got to know that python list are object just like any array in Java or C++. So if you pass it in a function which is modifying it it will be modified in caller also.
I was thinking may be in Python as long as possible things will be copied and passed around.

To be continued…

ffmpeg: libavformat, libavcodec and x264

ffmpeg is great tool for almost all your video processing playing need. For general purpose it has everything given as command line. Most of the time you only need to find the correct command for your specific requirement.
Rarely you need more control on your video like encoding in different library and then muxing using ffmpeg. Or say you are streaming on network and it is not of any particular format. You don’t want the overhead of coding to make it of some format and then stream and decode via ffmpeg. In those scenario you have to get away with ffmpeg wrapper and just want to do it in your own way by using core API’s.

ffmpeg is easy in that way also once you get the feel of it. There are just couple of functionality which you need to achieve all this. Sometime you don’t even need all ffmpeg and just getaway with libavcodec swscale etc.

If you have the input file for processing you can just check out the sample code provided. You just have to set the input file for reading and you are done. If your requirement is providing your own input and also getting output in code and not in a file or stream.
I wrote my own wrapper on ffmpeg for windows to get acess to core API I needed.

Once you have the wrapper say ffmpeg_wrapper.c just compile it like

gcc -I. -c -o ffmpeg_wrapper.o ffmpeg_wrapper.c
gcc -shared -o ffmpeg_wrapper.dll ffmpeg_wrapper.o -L. -lavformat -lavcodec -lavutil -lWs2_32 -liconv

any other library you need.

Include are done like this:

#include "libavformat/avformat.h"
#include "libavutil/opt.h"
#include "libavutil/mathematics.h"
#include "libavutil/timestamp.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"

#define DLLEXPORT __declspec(dllexport)
#define CDECL __cdecl

I faced a problem while I was doing encoding via separate h264 dll and then passing the input for muxing via ffmpeg. The problem was with timing I know there are some option in h264 to set the timing info but it was not working out. So I generated the timing using ffmpeg itself.

static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
/* rescale output packet timestamp values from codec to stream timebase */
pkt->pts = av_rescale_q_rnd(pkt->pts, *time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt->dts = av_rescale_q_rnd(pkt->dts, *time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt->duration = av_rescale_q(pkt->duration, *time_base, st->time_base);
pkt->stream_index = st->index;</code>

/* Write the compressed frame to the media file. */
log_packet(fmt_ctx, pkt);
return av_interleaved_write_frame(fmt_ctx, pkt);
}

 

Git: Server setup

git best guide
http://rogerdudler.github.io/git-guide/

 

How to setup a git server

git clone –bare my_project my_project.git
Cloning into bare repository ‘my_project.git’… done.
The output for this command is a little confusing. Since clone is basically a git init then a git fetch, we see some output from the git init part, which creates an empty directory. The actual object transfer gives no output, but it does happen. You should now have a copy of the Git directory data in your my_project.git directory.
cp -Rf my_project/.git my_project.git

Putting the Bare Repository on a Server

Now that you have a bare copy of your repository, all you need to do is put it on a server and set up your protocols. Let’s say you’ve set up a server called git.example.com that you have SSH access to, and you want to store all your Git repositories under the /opt/git directory. You can set up your new repository by copying your bare repository over:

$ scp -r my_project.git user@git.example.com:/opt/git

At this point, other users who have SSH access to the same server which has read-access to the /opt/git  directory can clone your repository by running

$ git clone user@git.example.com:/opt/git/my_project.git

Examples

Clone from upstream:

$ git clone git://git.kernel.org/pub/scm/.../linux.git my-linux
$ cd my-linux

Make a local clone that borrows from the current directory, without checking things out:

$ git clone -l -s -n . ../copy
$ cd ../copy
$ git show-branch

Clone from upstream while borrowing from an existing local directory:


$ git clone --reference /git/linux.git \
git://git.kernel.org/pub/scm/.../linux.git \
my-linux
$ cd my-linux

Create a bare repository to publish your changes to the public:

$ git clone --bare -l /home/proj/.git /pub/scm/proj.git

created a user for linux called git
everyone just install git on their machine
configure git with below command:


git config --global user.email "nandan.dubey@abcde.com"
git config --global user.name "Nandan Dubey"
git status
git add .
git commit -m "first commit"
$ git remote -v
origin ssh://git@10.0.0.31:/home/git/hello_world.git (fetch)
origin ssh://git@10.0.0.31:/home/git/hello_world.git (push)

now there are two ways for any project. either it is a brand new project. Correct for all project right now as git server repository is brand new so no projects exists right now.
So for brand new project first you need to create

git init hello_world

git remote remove origin

Instead of removing and re-adding, you can do this:

git remote set-url origin git://new.url.here

git clone –bare hello_world hello_world.git
scp -r hello_world.git git@10.0.0.31:/home/git
To copy an exisiting project use
git clone ssh://git@10.0.0.31:/home/git/hello_world.git
make changes do git add and git commit
finally do git push
git push origin master
folder files:
git init
git add .
git commit -m “first commit”
git remote add origin ssh://git@10.0.0.31:/home/git/test.git

Now, you can set up an empty repository for them by running git init with the –bare option, which initializes the repository without a working directory:

$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init

Then, John, Josie, or Jessica can push the first version of their project into that repository by adding it as a remote and pushing up a branch. Note that someone must shell onto the machine and create a bare repository every time you want to add a project. Let’s use git server as the hostname of the server on which you’ve set up your ‘git’ user and repository. If you’re running it internally, and you set up DNS for git server to point to that server, then you can use the commands pretty much as is:

# on Johns computer
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin ssh://git@10.0.0.31:/home/git/test.git
$ git push origin master

At this point, the others can clone it down and push changes back up just as easily:


$ git clone git@gitserver:/opt/git/project.git
$ cd project
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master

cd ~/.sshls -al# Lists the files in your .ssh directory

ssh-keygen -t rsa -C “your_email@example.com”# Creates a new ssh key, using the provided email as a label# Generating public/private rsa key pair.# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]

Enter passphrase (empty for no passphrase): [Type a passphrase]# Enter same passphrase again: [Type passphrase again]

Which should give you something like this:

# Your identification has been saved in /c/Users/you/.ssh/id_rsa.# Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.# The key fingerprint is:# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

Then add your new key to the ssh-agent:

ssh-add ~/.ssh/id_rsa

Run the following code to copy the key to your clipboard.

clip < ~/.ssh/id_rsa.pub# Copies the contents of the id_rsa.pub file to your clipboard

ssh -T git@github.com# Attempts to ssh to github

ref: https://git-scm.com/