Sunday, December 13, 2015

Setting Up Raspberry Pi

Preface

Cool, I've tried Arduino, BeagleBone, and STM boards before, but this would be my first experience in coding on Raspberry Pi, which quite famous and cheap as well. In this post, I would write down my steps for setting up Raspbian, a Linux distribution designed for Raspberry Pi. Although there are already tons of tutorial regarding to this, I will include some troubleshooting as well.

Equipments

  1. Raspberry Pi Model B
  2. HDMI Cable
  3. Monitor supports HDMI
  4. MicroSD Card (at least 8G)
  5. USB Keyboard / USB Mouse
  6. USB wireless adapter - Edimax EW-7811UN
  7. USB Cable (A type to Micro)

Steps

Build OS Image on SD Card

Normally, this step should be easy, only one or two commands are needed to get a SD with our OS in it. However, I found a problem while doing this step this time - my SD card is in read-only mode. Usually, this is cause by the physical lock switch on the card; however, no matter I switch it on or off, it's always locked. By applying the commands I noted in the last post, we can learn its details:
> diskutil info disk2
   Device Identifier:        disk2
   Device Node:              /dev/disk2
   Whole:                    Yes
   Part of Whole:            disk2
   Device / Media Name:      SD Card Reader
   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None
   Content (IOContent):      FDisk_partition_scheme
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 USB
   SMART Status:             Not Supported
   Total Size:               15.9 GB (15931539456 Bytes) (exactly 31116288 512-Byte-Units)
   Volume Free Space:        Not applicable (no file system)
   Device Block Size:        512 Bytes
   Read-Only Media:          Yes
   Read-Only Volume:         Not applicable (no file system)
   Device Location:          Internal
   Removable Media:          Yes
   Media Removal:            Software-Activated
   Virtual:                  No
   OS 9 Drivers:             No
   Low Level Format:         Not supported

And, whenever I tried to format or write onto the card, I got:

> sudo -s -- 'dd bs=1m if=/dev/zero of=/dev/disk2'
Password:
dd: /dev/disk2: Permission denied

After several trials and Googling, I found the problem is caused by the adaptor that adapts the small Micro SD back to the normal size where its switch isn't functioning well. Funny discussion regarding to this issue can be found online, such as "it worked when it was about 75% towards the unlocked position, but you might need to fiddle with it a bit". No matter how I place the switch lock, it never works.

So, I then brought a small Micro SD Card Reader, which costs around USD2.5, and it fixes all the problem nicely.

> diskutil info disk3
   Device Identifier:        disk3
   Device Node:              /dev/disk3
   Whole:                    Yes
   Part of Whole:            disk3
   Device / Media Name:      STORAGE DEVICE
   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None
   Content (IOContent):      FDisk_partition_scheme
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 USB
   SMART Status:             Not Supported
   Total Size:               15.9 GB (15931539456 Bytes) (exactly 31116288 512-Byte-Units)
   Volume Free Space:        Not applicable (no file system)
   Device Block Size:        512 Bytes
   Read-Only Media:          No
   Read-Only Volume:         Not applicable (no file system)
   Device Location:          External
   Removable Media:          Yes
   Media Removal:            Software-Activated
   Virtual:                  No
   OS 9 Drivers:             No
   Low Level Format:         Not supported
~
> diskutil unmountDisk /dev/disk3
Unmount of all volumes on disk3 was successful
~
> sudo -s -- 'dd bs=1m if=/Users/heron/Desktop/2015-11-21-raspbian-jessie-lite.img | pv | dd of=/dev/disk3'
I am using pv in the middle instead of barely using dd is because that dd doesn't show the process bar, which is hard for me to know how long will it take, and how much is done.

Power Up

Basically, the followings would be easy and we are already about to finish. Plug in the cables as below, and it will power up right after the micro USB is plugged in.


From the photo, one can see that I have:
  • 1 Micro USB for power
  • 1 HDMI for display
  • 2 Logitech Unifying Receiver for keyboard and mouse
  • 1 USB wireless adapter for Wifi
  • 1 USB microphone (for application purpose)

Keyboard Layout Issue

Everything should be set so far, and if you're using NOOBS system installer, it will do rest of the jobs. The only thing is you have to do is to select your settings or system you wish to install.

However, there's one issue I found when I just setup Raspbian, the operating system designed for Raspberry Pi based on Debian. It's using different keyboard layout which is a general one that I used to. So, to fix the problem, we have to fire up keyboard-configuration and pick the right setting. Steps are in this link. The settings will start to affect only after reboot.

Reference

Saturday, December 12, 2015

Convert GIF Background from Transparency to a Solid Color

Preface

Last day, I got a new Pebble watch, then I started to code something on it. Within one day, I've tried out its dictation API with a small but fun app. Also, I wrote a watchface which has a circle progress bar showing the percentage of the time had past today (the idea is from one of Apple Watch watchface, "Solar").

However, when I was trying to put the animation on my watchface, it requires an "Animated Portable Network Graphics (APNG) file format" with solid color background. Therefore, I have to somehow find a way to convert one GIF file I have into that format while my GIF file is having a transparent background.

Steps

"Convert" written by ImageMagick is a powerful tool in this situation, I've been using it from the first year I started to use unix-like systems. To install (for Mac):
> sudo port install ImageMagick
To fill solid background (I use black here) on my GIF file:
> convert input.gif -background black -alpha remove result.gif
where option background sets the color of background, and option alpha is set to "remove" for removing its transparency.

And, to convert GIT file into APNG file, we can use a tool called gif2apng. It can be downloaded here. How I use it is written below:
> unzip gif2apng-1.9-bin-macosx.zip
> cp gif2apng ~/bin
> export PATH=~/bin:$PATH # this depends on the setting you prefer, I like to put this kind of tool under ~/bin
> gif2apng result.gif result.png

gif2apng 1.9 using 7ZIP with 15 iterations

Reading 'result.gif'...
9 frames.
Writing 'result.png'...
9 frames.

More

One to step to go in order to fit into the Pebble Watch, which may be a little irrelevant to this post. I have to crop the image since it's too big. And, what I wanted to do is to remove the upper part. It's originally at size of 100 * 120, and I would like it became 100 * 100. So, there's the magic:
> convert input.gif -coalesce -repage 0x0 -crop 100x100+0+20 +repage result.gif
Finally, we can combine all together as below:
> convert input.gif -coalesce -repage 0x0 -crop 100x100+0+20 +repage -background black -alpha remove result.gif; gif2apng result.gif input.png