Wednesday, April 6, 2016

Framebuffer screen dumps to PNG and back

I've got a nice card with an Atmel AVR32 processor and a nice big 320x240 (monochrome!) screen. :)
I can put data on the screen from buildroot linux by writing directly to the framebuffer, /dev/fb0. However the data there is in raw format. Here is how to dump from screen. Then convert, using ffmpeg, the raw data to a PNG and then back to raw data after editing:

  1. Get a screenshot of the framebuffer using cat /dev/fb0 > screendump.bin.
  2. Convert to PNG using ffmpeg: ffmpeg   -vcodec rawvideo   -f rawvideo   -pix_fmt monow   -s 320x240   -i screendump.bin     -f image2   -vcodec png screendump.png
    This will read raw video from the screendump.bin, handle it as pixel monochrome data from a 320x240 screen and save as sreendump PNG file.
  3. Next, open in Gimp or similar to edit. Save as PNG afterwards.
  4. Convert to rawvideo binary again using ffmpeg: ffmpeg -vcodec png -i savedimage.png -vcodec rawvideo -f rawvideo -pix_fmt monow savedimage.bin
  5. Put the data on the screen: cat savedimage.bin > /dev/fb0
Use ffmpeg -pix_fmts to see other raw video types to convert from/to.

Sources: Check out these great sites for more:
http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs
http://forum.videohelp.com/threads/334333-Help-with-lossless-ffmpeg-command-%5Bvideo-png-back-to-video%5D
http://stackoverflow.com/questions/3781549/how-to-convert-16-bit-rgb-frame-buffer-to-a-viewable-format
https://community.freescale.com/docs/DOC-100347

Remote Desktop over the Internet using UltraVNC on Windows

Here is a HowTo for setting up a UltraVNC server on your computer, automatically open needed ports, and creating a configured UltraVNC single-click client to let you see someones desktop where all they have to do is start the client you send to them. No installations!
(A great alternative is TeamViewer which is free for personal use, but expensive for commercial use)

Here is how:

- Download UltraVNC and install:
See bottom of: http://www.uvnc.com/downloads/ultravnc.html

- Download UltraVNC SingleClick custom.zip package from:
http://www.uvnc.com/pchelpware/sc/create.html

- Extract helpdesk.txt file.

- Edit to look like you want to.
Example helpdesk.txt:

[TITLE]
UltraVnc SC

[HOST]
My RDC connection
-connect 158.158.158.158:5900 -noregistry

[TEXTTOP]
Double click to open connection

[TEXTMIDDLE]

[TEXTBOTTOM]


[TEXTRBOTTOM]


[TEXTRMIDDLE]


[TEXTRTOP]


[TEXTBUTTON]
UltraVNC web

[WEBPAGE]
http://www.ultravnc.net

[TEXTCLOSEBUTTON]
Cancel


[BALLOON1TITLE]
Establishing connection ...

[BALLOON1A]
5 min connection attempt

[BALLOON1B]
If it fails, the software will close.

[BALLOON1C]


[BALLOON2TITLE]
Connection active.

[BALLOON2A]
Warning, your desktop is visible remotely

[BALLOON2B]
You can break the connection any time

[BALLOON2C]
by using the close button



(remember to replace 158.158.158.158 with your external IP, e.g. from https://www.whatismyip.com/)

- Create a ZIP file with only helpdesk.txt in it.

- Go to:
http://support1.uvnc.com/cgi-bin/upload4.pl

- Enter user: foo
- Enter password: foobar
- Select Zip file containing helpdesk.txt
- Click Upload
- Wait until you a link to download exe file appears. Then download the exe file.
- Start it to see what it looks like.


- Download UPnP PortMapper to open the needed port on your computer (if you havent done so manually on your router).
https://sourceforge.net/projects/upnp-portmapper/files/latest/download
https://github.com/kaklakariada/portmapper

- Open PortMapper jar file (should need only double-click if you have Java installed).
- Under the section "Port Mapping presets", select Create button.
- Enter a description, e.g. UltraVNC.
- Then click Add button.
- Change port 1 to 5900 in both External Port and Internal Port columns.
- Not sure if you also need UDP port open, but might be useful so click Add again and select UDP with port 5900 internal and external.
- Click save.

- Click connect to connect to your router.
When successfully connected (you might get an error, but stuff seems to work regardless).
- Select your UltraVNC entry in the list and press "Use".
- Wait for ports to show up in the list above.

Now you have ports open. You need to start VNC listener:
- Open command line prompt (cmd in windows start menu) and navigate to where you installed UltraVNC and run it with flag -listen 5900 to tell it to listen on port 5900:

cd "C:\Program Files\uvnc bvba\UltraVnc"
vncviewer.exe -listen 5900


- Finally, send the exe file you generated from helpdesk.txt (not the zip file) to the person you want to connect to. Get the person to open the file and double click on the top option and you should get a message on your computer asking you to accept the connection.


Source:
http://www.uvnc.com/docs/uvnc-sc/76-how-to-setup-and-configure-a-custom-Ultra%20VNC-sc.html

Sites to read about AngularJS and Ionic

Top 10 angularjs mistakes:
https://www.airpair.com/angularjs/posts/top-10-mistakes-angularjs-developers-make#9-manual-testing


Best practice dir structure angularjs/ionic:
https://scotch.io/tutorials/angularjs-best-practices-directory-structure
I've tried both. Unsure which one I think is best. I think the most important point is to create a directory structure to group your files according to how they are used. So that other people can understand how you program is structured more quickly.


Tips for ionic:
http://www.betsmartmedia.com/what-i-learned-building-an-app-with-ionic-framework (https://web.archive.org/web/20150925034548/http://www.betsmartmedia.com/what-i-learned-building-an-app-with-ionic-framework)


Intro and how to use jasmine unit test framework:
http://jasmine.github.io/edge/introduction.html
Great test tool!

Promises in AngularJS

 

Introduction to AngularJS promises (as a cartoon):
http://andyshora.com/promises-angularjs-explained-as-cartoon.html

Promise anti-patterns
Flatten chaining, clean up, and broken chains:
http://taoofcode.net/promise-anti-patterns/

HTTP promise not like Q promise, use deferred:
http://weblog.west-wind.com/posts/2014/Oct/24/AngularJs-and-Promises-with-the-http-Service

AngularJS Q:
https://docs.angularjs.org/api/ng/service/$q

Nice features to put on headless linux box

 

Unattended-upgrades:

Automatically download and install stable updates.

Install using:
sudo apt-get install unattended-upgrades
Configure using:
sudo dpkg-reconfigure -plow unattended-upgrades

Source:
http://raspberrypi.stackexchange.com/questions/4698/how-can-i-keep-my-raspbian-wheezy-up-to-date
https://help.ubuntu.com/community/AutomaticSecurityUpdates


Fail2ban:

http://www.fail2ban.org/wiki/index.php/Main_Page
https://en.wikipedia.org/wiki/Fail2ban





Imapgrab:

To automatically backup email:


http://www.linux-magazine.com/Online/Blogs/Productivity-Sauce/Back-up-Email-with-a-Single-Command
https://sourceforge.net/projects/imapgrab/ 

Android LayerDrawable in HTML using CSS

So a neat feature for Android is the LayerDrawable which lets you make an image compiled from other images. You define an array of images that shuld be drawn on top of each other and receive the resulting image.
This feature exists in CSS too. It's called background-image and lets you define one or more images to be displayed as a background of a container.

background-image: url(front.png), url(behind.png);





Remember to make the container a block element and if you don't have any content in it you should set the container height and width or a padding-bottom to give it size (Otherwise you will be shown approximately 0px of your background). See http://stackoverflow.com/questions/1495407/css-a-way-to-maintain-aspect-ratio-when-resizing-a-div

.container-with-background-image {
    width: 100%;
    padding-bottom: 75%;
}


Source:
http://www.css3.info/preview/multiple-backgrounds/
http://stackoverflow.com/questions/5846637/why-an-inline-background-image-style-doesnt-work-in-chrome-10-and-internet-ex

Wednesday, September 23, 2015

Using gettext Library for Translations

GNU gettext is a C library which lets you add translation support to your program with fallbacks to original text if no translation is available.

Here's a list of links to how you use the different utils in the gettext package:

See http://stackoverflow.com/questions/1003360/complete-c-i18n-gettext-hello-world-example for a hello world example using gettext.

See http://www.gnu.org/software/libc/manual/html_node/Locating-gettext-catalog.html for selecting translation file in code.
See http://www.gnu.org/software/libc/manual/html_node/Translation-with-gettext.html for using gettext in code.
See http://www.gnu.org/software/gettext/manual/html_node/lib_002fgettext_002eh.html for using gettext.h instead of libintl.h to get extended capabilities such as pgettext (adds context) and support for replacing gettext commmands with no-op if gettext is not installed on system. See http://www.gnu.org/software/gettext/manual/html_node/PO-Files.html for how PO files work (text files translations).
See http://www.heiner-eichmann.de/autotools/using_gettext.html, http://www.gnu.org/software/gettext/FAQ.html, and http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/xgettext-Invocation.html for some information on using xgettext to extract strings for translation into a POT file.
See http://www.gnu.org/software/gettext/manual/html_node/msginit-Invocation.html on how msginit generates language specific translation files (PO) from POT file.
See http://www.gnu.org/software/gettext/manual/html_node/msgmerge-Invocation.html on how msgmerge generates updated language specific translation files (PO) from a changed POT file.
See http://www.gnu.org/software/gettext/manual/html_node/msgfmt-Invocation.html on how msgfmt generates binary translation files (MO) from human readable translation files (PO).


See http://www.gnu.org/software/gettext/FAQ.html#integrating_noop for troubleshooting missing translations


Simple example

  1. Copy gettext.h from /usr/share/gettext/ to your component or project.
  2. Set macro ENABLE_NLS to 1
  3. Include gettext.h in your cpp file.
  4. Example code:
    ::setlocale(LC_ALL, "");
    
    // Set directory to search for translation files in (which contains
    
    // en_US.UTF-8/LC_MESSAGES/hellogt.mo)
    
    bindtextdomain("hellogt", ".");
    
    // Set name of translation file to use (hellogt.mo)
    
    textdomain( "hellogt");
    
    std::cout << gettext("hi") << std::endl;
    
    std::cout << pgettext("bob", "hello, world!") << std::endl;
    

Generate translation files

#Common
find . -name "*.cpp" > files.txt
xgettext --package-name mygt --package-version 1.2 --default-domain mygt --output mygt.pot -f files.txt

#Spanish translations
sudo locale-gen es_MX.UTF-8
msginit --no-translator --locale es_MX --output-file mygt_spanish.po --input mygt.pot
mkdir -p ./es_MX.UTF-8/LC_MESSAGES
msgfmt --check --verbose --output-file ./es_MX.UTF-8/LC_MESSAGES/mygt.mo mygt_spanish.po

#Norwegian translations
sudo locale-gen nb_NO.UTF-8
msginit --no-translator --locale nb_NO --output-file mygt_norwegian.po --input mygt.pot
mkdir -p ./nb_NO.UTF-8/LC_MESSAGES
msgfmt --check --verbose --output-file ./nb_NO.UTF-8/LC_MESSAGES/mygt.mo mygt_norwegian.po

Update translation files

Add changes in code to language specific translation file:
xgettext --package-name mytgt --package-version 1.2 --default-domain mygt --output mygt.pot -f files.txt
msgmerge mygt_norwegian.po_old mygt.pot --output-file=mygt_norwegian.po_new
use msgfmt to generate new mygt.mo file from mygt_norwegian.po_new.