Thursday, December 3, 2015

Science-fiction in one word is "fiction", and not "science"!

By vectorbelly (heh?!) via this post. in the Science channel of G+

I do love science-fiction. But science-fiction in one word is "fiction", not "science".

Wednesday, December 2, 2015

Rewriting a bare millis() clock on an AVR without the Arduino environment

This is a short stand-alone C source code that I wrote to simulate the Arduino millis() function on an Atmel AVR 328p microcontroller. It is part of a growing collection of AVR routines.

AVR 328p minimalist serial read/write source code

This is a C source code I wrote to do serial read/write on an Atmel AVR 328p microcontroller.
This is the same processor as the Arduino Uno for example. It is part of a growing collection of AVR routines.

And since you can set your own limits, you can use this code in place of the usual Serial class, as the default buffers are quite small and they may overflow if you are not processing them fast enough, or if you are sending too much data at once (eg. file copy/paste). Note that some terminals let you set add a small delay automatically after each line is sent, which is the way to go so as to leave enough time for your program to process the incoming flow.

Sunday, November 29, 2015

Small and useful bash scripts

A growing collection of scripts and headers for your bash scripts.

Check and exit if a script is already running

This is to prevent multiple running instances of the same script:
test "$(pidof -x "$(basename $0))" != $$ && exit
# or (bash only):
[[ "$(pidof -x "$(basename $0))" != $$ ]] && exit

Get the full directory name of the script

This works no matter where it is being called from:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Automatically manage cronjob additions and removal of commands

Use command lines to add or remove scheduled jobs in your ''crontab'':
croncmd="/home/me/myfunction start 2> /home/me/myfunction/cron_errors < /dev/null"
cronjob="0 0 * * * $croncmd"
# Add
( crontab -l | grep -v "$croncmd" ; echo "$cronjob" ) | crontab -
# Remove
( crontab -l | grep -v "$croncmd" ) | crontab -

Use pv to get a progress bar with dd

''pv'' can be used in many contexts to provide a progress bar:
dd bs=4M if=image.dd | pv | dd of=/dev/sdz

Re-index a set of files (typically images, to build a timelapse)

This works by extracting the number in their name and add non-significant leading zeros (bash)
for f in ​*​.jpg; do mv "$f" /tmp/$(printf "%05d" ${f//[!0-9]/}).jpeg; done

Ubuntu: find the biggest installed packages

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}' | tail -50
Then after removing it, ''apt-get clean'' will also purge the package cache (a good thing to free some harddrive space)

Compute date difference with flexible parametrization (very bashic!)

Here is '''':
set -- "${1:-$(</dev/stdin)}" "${@:2}"
[[ -f "$1" ]] && set -- "$(<$1)" "${@:2}"
echo $( date -d@$(( ( $(date +'%s') - $(date -d "$1" '+%s') ) )) +%s ) 3600 / p | dc
root:~# ./ "2016-08-09 06:03"

This is wwenty-seven hours from now to the recorded date.

The second and third very bash-specific line in the script makes it possible to pass the date in other ways. Say you stored a timestamp with ''date +'%Y-%m-%d %H:%M' > last.txt''. Then:
root:~# cat last.txt | ./ 
root:~# ./ last.txt 

Switch my wifi router off when not in use (WR703N TP-link)

Automatic wifi shutdown when idle on a small WRT router

The script below runs on my OpenWRT wifi router (a cheap but effective $20 TP-Link TL-WR703N, which I flashed with OpenWRT -- see the how to here).

It is configured as a wifi router, when switched on.

But when no one connects to it, it will blink its led for a while before it shuts down by itself. So it does not waste useless energy, it is more secure at home and we live in one less wifi network.

Monday, November 16, 2015

Easily count tiny items (200 PCB vias here)

One way to check the number of items I just bought on ebay.

How can I check that there are really 200 vias in these bags?
I bought packs of 200 tiny rivets for vias. Since I am making my PCB on a mill and I often only use only single-sided PCB copper clads, it gets tricky to solder the headers from below. This is very annoying for headers and Arduino shields for example, as they protrude from below also...

I was somehow able to do it but a proper way is to insert so called PCB "through hole rivets" first. This way, the headers can be conveniently soldered from the top, once they are themselves inserted in the rivets.

But such rivets are not cheap (I got 200 of them for $20 on ebay!). I felt I had been cheated on when seeing the small volume it represented. Really 200?

Thursday, October 15, 2015

Easy formatting of source code in blogger: inlined and blocks

Add wiki-like code blocks and inlined code snippets in your blogger posts

Posting pieces of code to blogger is annoying. Reverting to HTML editor mode to insert special tags is not fun at all, and prone to inconsistency. I tried using external code-formatting libraries, such as the very nice SyntaxHighlighter, but it is very heavy and makes your site slow to load imho.

So here is a short hack to handle simple pieces of codes. Moreover it handles angled brackets completely transparently (resp. the < and > symbols), that often pollutes copy/pasted codes.

It only requires twenty lines of javascript and CSS to be copy/pasted in your blog.

SSH tunneling: handling your mom's linux box, remotely and anywhere.

I want to be able to log on the linux box of my mom, wherever she is.

This helps both me and her, so I can administrate, debug or backup her computer without having to tell her bad words. Here is what I did, thanks to an automatic SSH tunneling from her to me.

The fact that she is here and there all the times makes it impractical to configure the different internet boxes, routers and NATs. I want to be able to reach here even when she is in a cyber-café (not her kind, but this is out of topic).

Thursday, October 8, 2015

Linux log files: discard verbose useless lines

Your linux log files are "spammed" by verbose programs or activities that you would like to silence? Here is a short how to to filter some specific lines.

Tuesday, April 28, 2015

increment a C float until it fails: maximum value is about 16,777,216

Higher limit of an incremented float in C

So I finally made it clear. The first increment that has no effect on a C ''float'' variable is around +16,777,216. So four-byte wise, a float still fails later than what I thought compared to a ''uint32_t''.