Wednesday, April 22, 2015

Reflex - trigger an execution of command on a file change event

How many times you were working on some tiny project that needs to be rebuild / restarted after you modify the code? I guess often.

After experimenting with some solutions I found an ultimate one: https://github.com/cespare/reflex
Installation:
go get github.com/cespare/reflex
Automatically recompiling markdown file into html every time the file is changed:
reflex -r '\.md$' blackfriday-tool -page article.md article.html
Or you can restart Go app when saved files:
reflex -r '\.go$' -- go run main.go

Wednesday, April 15, 2015

xmonad and screen off

In Ubuntu with default windows manager it just works, with Xmonad it doesn't, turning the screen off and making the eco-people happy (saving your electricity bill).

The inspiration for solution comes from:
http://superuser.com/questions/590225/disable-screen-turn-off

Checking xset tool in terminal shows smth:
xset
usage:  xset [-display host:dpy] option ...
    To turn bell off:
 -b                b off               b 0
    To set bell volume, pitch and duration:
  b [vol [pitch [dur]]]          b on
    To disable bug compatibility mode:
 -bc
    To enable bug compatibility mode:
 bc
    To turn keyclick off:
 -c                c off               c 0
    To set keyclick volume:
  c [0-100]        c on
    To control Energy Star (DPMS) features:
 -dpms      Energy Star features off
 +dpms      Energy Star features on
  dpms [standby [suspend [off]]]     
       force standby 
       force suspend 
       force off 
       force on 
       (also implicitly enables DPMS features) 
       a timeout value of zero disables the mode 
    To set the font path:
  fp= path[,path...]
    To restore the default font path:
  fp default
    To have the server reread font databases:
  fp rehash
    To remove elements from font path:
 -fp path[,path...]  fp- path[,path...]
    To prepend or append elements to font path:
 +fp path[,path...]  fp+ path[,path...]
    To set LED states off or on:
 -led [1-32]         led off
  led [1-32]         led on
 -led named 'name'   led off
  led named 'name'   led on
    To set mouse acceleration and threshold:
  m [acc_mult[/acc_div] [thr]]    m default
    To set pixel colors:
  p pixel_value color_name
    To turn auto-repeat off or on:
 -r [keycode]        r off
  r [keycode]        r on
  r rate [delay [rate]]
    For screen-saver control:
  s [timeout [cycle]]  s default    s on
  s blank              s noblank    s off
  s expose             s noexpose
  s activate           s reset
    For status information:  q
    To print version: -version

The interesting part is dpms. To force turn off the screen one has to run:
xset dpms force off
And it would be great to integrate it with a 'lock screen' keyboard shortcut in Xmonad configuration:
    -- lock screen
     , ((modm .|. shiftMask, xK_l     ), spawn "gnome-screensaver-command -l && xset dpms force off")

Thursday, December 18, 2014

Sunday, November 30, 2014

Spark Core without Internet and Cloud

I recently got a Spark Core. I don't find a need to connect my device to cloud. What's even more I don't want to do that. My home automation system is suppose to be cloud free. I want to make it a loose connection of Spark Core (in future Photon P0/P1) as controllers and data collectors, and Raspberry Pi as a brain device (exposing API or syncing with cloud of my choice).
First, one needs to remember that Spark is an embed device. System is linked with one's application. Every time one wants to update a program, one needs to recompile firmware (compile one's application as a compilation unit and link with the rest of firmware).

Installing firmware

The firmware repository is located here: https://github.com/spark/firmware Following instruction: https://github.com/spark/firmware#1-download-and-install-dependencies on Ubuntu means:
sudo apt-get install gcc-arm-none-eabi # wrong version
sudo apt-get install automake
sudo apt-get install dfu-util # wrong version
sudo apt-get install git
Do NOT do it at home. One needs to install gcc by:
sudo apt-get remove binutils-arm-none-eabi gcc-arm-none-eabi
sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi
More about problems with gcc for arm can be read: and dfu-util from source (a dfu-util build manual http://dfu-util.sourceforge.net/build.html):
sudo apt-get install libusb-1.0-0-dev
wget http://dfu-util.sourceforge.net/releases/dfu-util-0.8.tar.gz
tar -zxvf dfu-util-0.8.tar.gz dfu-util-0.8
cd dfu-util-0.8
./autogen.sh
# you need to have autoreconf, if error, try: sudo aptitude install dh-autoreconf
./configure
make
sudo make install
Reasons:
  • gcc-arm-none-eabi is missing nano.specs and <cctype> header file used in core-firmware/inc/spark_wiring_character.h this will cause your compilation to throw errors like this:
    4.8 arm-none-eabi-g++: error: nano.specs: No such file or directory
    
  • Ubuntu have dfu-util version 0.5, the most recent version is 0.8. In an old version running dfu-util -l will give you:
    Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=0, name="UNDEFINED"
    Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=1, name="UNDEFINED"
    
  • Notice "UNDEFINED". Using new version will print something more similar to:
    Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash  /0x08000000/20*001Ka,108*001Kg" 
    Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=1, name="@SPI Flash : SST25x/0x00000000/512*04Kg"
    
    But what's more, the old version is not able to flash Spark Core. Flashing and listing devices required to run it with root privilages (sudo).

Getting source

Note, that makefile for firmware assumes that all 3 projects are downloaded/cloned to the same directory: core-firmware, core-common-lib, core-communication-lib.

FDU mode

Flash it part of firmware manual requires you to put Core in FDU mode. If you wonder how DFU mode on Core looks: https://vine.co/v/MahhI1Fg7O6:

Sunday, November 16, 2014

Google: "GoLang compute md5 of file"

Title of this post is my last Google query.
It got me there: https://www.socketloop.com/tutorials/how-to-generate-checksum-for-file-in-go. After reading I was surprise that Go doesn't have ReaderWriter which reads data from some object implementing io.Reader and writes data to some other object implementing io.Writer. I've grabbed the code and started cleaning it but keeping all file content in memory made me search.
I went to GoLang official doc and found: func Copy(dst Writer, src Reader) (written int64, err error)
Copy copies from src to dst until either EOF is reached on src or an error occurs.
To summarize:

Below is the full source of main.go:
package main

import (
  "crypto/md5"
  "fmt"
  "io"
  "os"
)

func ComputeMd5(filePath string) ([]byte, error) {
  var result []byte
  file, err := os.Open(filePath)
  if err != nil {
    return result, err
  }
  defer file.Close()

  hash := md5.New()
  if _, err := io.Copy(hash, file); err != nil {
    return result, err
  }

  return hash.Sum(result), nil
}

func main() {
  if b, err := ComputeMd5("main.go"); err != nil {
    fmt.Printf("Err: %v", err)
  } else {
    fmt.Printf("main.go md5 checksum is: %x", b)
  }
}

$ go run main.go
main.go md5 checksum is: facd74ec8975d8fd84897fb352f8f87e