2016年8月31日 星期三

Raspberry Pi: small TFT LCD

since: 2016/08/31
update: 2016/09/13
reference:
1. Home · notro/fbtft Wiki

測試硬體: 1.8" TFT LCD ST7735R (160x128)

A. Pi 啟用 SPI 功能
     $ sudo raspi-config
        > Advanced Options
        > A5 SPI    Enable/Disable automatic loading of SPI kernel module (needed for e.g. PiFace)
        > Enable

-----------------------------------------------------------------------------------------------

B. 查詢支援的 fbtft_device

    $ sudo modprobe fbtft_device name=list; dmesg | tail -50
      ...
      [ 1262.538743] fbtft_device: Supported displays:
      [ 1262.538751] fbtft_device: adafruit18
      [ 1262.538758] fbtft_device: adafruit18_green
      ....


-----------------------------------------------------------------------------------------------

C. 載入 Framebuffer TFT LCD 設備
    $ sudo modprobe fbtft_device name=adafruit18

    p.s. There is one required module parameter, and that is name. It specifies which display (device) to register.

-----------------------------------------------------------------------------------------------

D. 查詢是否載入成功
    $ ls -al /dev/fb*
    /dev/fb0 ----> default display output
    /dev/fb1 ----> fbtft


    $ dmesg
[ 2532.384028] fbtft_device: module is from the staging directory, the quality is unknown, you have been warned.
[ 2532.391421] spidev spi0.0: spidev spi0.0 500kHz 8 bits mode=0x00
[ 2532.391453] spidev spi0.1: spidev spi0.1 500kHz 8 bits mode=0x00
[ 2532.391494] bcm2708_fb soc:fb: soc:fb id=-1 pdata? no
[ 2532.391549] spidev spi0.0: Deleting spi0.0
[ 2532.392348] fbtft_device: GPIOS used by 'adafruit18':
[ 2532.392368] fbtft_device: 'reset' = GPIO25
[ 2532.392377] fbtft_device: 'dc' = GPIO24
[ 2532.392385] fbtft_device: 'led' = GPIO18
[ 2532.392402] spidev spi0.1: spidev spi0.1 500kHz 8 bits mode=0x00
[ 2532.392418] spi spi0.0: fb_st7735r spi0.0 32000kHz 8 bits mode=0x00
[ 2532.425447] fb_st7735r: module is from the staging directory, the quality is unknown, you have been warned.
[ 2533.359781] graphics fb1: fb_st7735r frame buffer, 128x160, 40 KiB video memory, 4 KiB DMA buffer memory, fps=20, spi0.0 at 32 MHz


    $ dmesg | grep graphics
[ 2067.729317] graphics fb1: fb_st7735r frame buffer, 128x160, 40 KiB video memory, 4 KiB DMA buffer memory, fps=20, spi0.0 at 32 MHz

-----------------------------------------------------------------------------------------------

E. 測試:
     1. Console
          > Map console 1 to framebuffer 1, login screen will show up on the display
         $ con2fbmap 1 1

     2. Image viewer
         $ sudo apt-get -y install fbi
         $ wget http://art110.wikispaces.com/file/view/Mystery-100x100.jpg/30649064/Mystery-100x100.jpg

          $ chmod 755 Mystery-100x100.jpg
          $ fbi -d /dev/fb1 -T 1 -noverbose -a Mystery-100x100.jpg

     3. mplayer
         $ sudo apt-get install -y mplayer
         $ wget http://fredrik.hubbe.net/plugger/test.mpg
         $ chmod 755 test.mpg

          // play with loop infinitely
          sudo SDL_VIDEODRIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -loop 0 -vo sdl -framedrop test.mpg

-----------------------------------------------------------------------------------------------

F. 開機時自動啟動播放影片(建議方式)
$ sudo nano .bashrc
....
# auto load fbtft_device:adafruit18 &  play loop video: test.mpg
sudo modprobe fbtft_device name=adafruit18

sudo SDL_VIDEODRIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -loop 0 -vo sdl -framedrop test.mpg


-----------------------------------------------------------------------------------------------

G. Shell Script
     檔名: run_nuit_blanche.sh

#!/bin/bash
sudo modprobe fbtft_device name=adafruit18

sudo SDL_VIDEODRIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -loop 0 -vo sdl -framedrop nuit_blanche.mpg


-----------------------------------------------------------------------------------------------

H. Framebuffer mirroring
    1. 說明:
        By mirroring /dev/fb0 onto /dev/fb1, we can take advantage of the GPU for
        hardware accelrated video playback.

       fbcp takes a snapshot of /dev/fb0, copies it to /dev/fb1 and waits 25ms before
       repeating.

       Snapshotting takes ~10ms and with a 25ms delay it gives roughly
       1000/(10+25) = 28fps

       CPU usage: ~2%
       Note: Snapshot and /dev/fb1 driver refresh is not syncronized.


    2. Install fbcp:
        $ sudo apt-get install cmake       
        $ git clone https://github.com/tasanakorn/rpi-fbcp
        $ cd rpi-fbcp/
        $ mkdir build
        $ cd build/
        $ cmake ..
        $ make
        $ sudo install fbcp /usr/local/bin/fbcp

    3. Load drivers and fbcp:
        $ sudo nano .bashrc
....

############
# start fbcp
echo "tty = $(tty)"

if [ $(tty) == "/dev/tty1" ]; then

  echo "We have a match."
  cd

 
  # Load fbcp drivers and run fbcp
  sudo modprobe fbtft dma
  sudo modprobe fbtft_device name=adafruit18
 
  #
rotate=90 speed=48000000 fps=50

  # Start fb copying process in the background
  sleep 3
  fbcp &

  #/usr/local/bin/fbcp &

  # Play video on /dev/fb0, which will also show up on /dev/fb1
  # 720x480
  # --win x1,y1,x2,y2
  sleep 1
  omxplayer -b --loop --win 180,120,540,360 160x128.mp4


  # Stop framebuffer copy
  #killall fbcp 
 
else
  echo "We dont match."
fi


-----------------------------------------------------------------------------------------------

I. 其他
   1. 檢查沒有接螢幕時的預設視窗大小
       $ /opt/vc/bin/tvservice -s
       state 0x120006 [DVI DMT (4) RGB full 4:3], 720x480 @ 60.00Hz, progressive

   2. 設定預設視窗大小
       $ sudo nano /boot/config.txt
....
# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

#framebuffer_width=720
#framebuffer_height=480

....

2016年8月27日 星期六

Android: Google Maps

since: 2016/08/27
update: 2016/10/13

reference:

A.  安裝 Google Play services SDK
      1. Android Studio > Tools > Android > SDK Manager

      2. SDK Tools > Google Play services (確認勾選) > OK

-----------------------------------------------------------------------------------------------

B. 新增 Maps 專案
    1. Start a new Android Studio project

    2. 設定好專案相關資料後 > Next

    3. Minimum SDK 選擇 API 18: Android 4.3 (Jelly Bean)
        > Next

    4. 選擇 Google Maps Activity > Next

    5. Customize the Activity: 除了 Title 外, 使用預設即可 > Finish

-----------------------------------------------------------------------------------------------

C.  申請 Google Maps API Key (開發憑證)
    1. 先查看自動開啟的檔案: google_maps_api.xml

<resources>
    <!--
    TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:
 https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27%3Bpipi.myhome.mapviewer

    You can also add your credentials to an existing key, using this line:
    FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27;pipi.myhome.mapviewer

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->

    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>
</resources>


    2. 將申請 API Key 的網址 copy & paste 到瀏覽器上
         > 繼續

    3. > 前往 "憑證"

    4. > 建立

    5. > 確定
        > 記下產生的金鑰: AIzaSyASXfTVIM2s2qhj0n2GAu6gemqDoqqO6EY

    6. 結果

-----------------------------------------------------------------------------------------------

D. 專案設定

     1. 將剛剛的 金鑰 copy & paste 到 google_maps_api.xml  裡的 YOUR_KEY_HERE
....
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyASXfTVIM2s2qhj0n2GAu6gemqDoqqO6EY</string>
....


     2. Tools > Android > Sync Project with Gradle Files

-----------------------------------------------------------------------------------------------

E. 查看專案的
AndroidManifest.xml 檔案

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pipi.myhome.mapviewer">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

                                          
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


-----------------------------------------------------------------------------------------------

F. 查看專案的 build.gradle(Module: app) 檔案
....
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
}


-----------------------------------------------------------------------------------------------

G. 查看專案的 activity_maps.xml 檔案
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="pipi.myhome.mapviewer.MapsActivity" />


-----------------------------------------------------------------------------------------------

H.
查看專案的 MapsActivity.java
檔案
package pipi.myhome.mapviewer;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}


-----------------------------------------------------------------------------------------------

I. 執行:

-----------------------------------------------------------------------------------------------

J. 讓 API 金鑰可以在不同 app (或不同 "開發" 電腦上) 使用
   1. 建立憑證指紋:
       $ cd
       $ keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey  -storepass android -keypass android

   2. 查詢憑證指紋:
       $ cd
      
$ cd .android
       $ keytool -list -v -keystore debug.keystore

輸入金鑰儲存庫密碼:  android

金鑰儲存庫類型: JKS
金鑰儲存庫提供者: SUN

您的金鑰儲存庫包含 1 項目

別名名稱: androiddebugkey
建立日期: 2016/7/9
項目類型: PrivateKeyEntry
憑證鏈長度: 1
憑證 [1]:
擁有者: C=US, O=Android, CN=Android Debug
發出者: C=US, O=Android, CN=Android Debug
序號: 1
有效期自: Sat Jul 09 18:26:27 CST 2016 到: Mon Jul 02 18:26:27 CST 2046
憑證指紋:
     MD5:  84:AB:96:D7:79:CE:90:D4:39:79:60:1A:CE:13:6B:1A
     SHA1: FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27
     SHA256: 89:77:ED:02:9B:37:12:58:86:75:38:73:27:F0:F6:65:84:6F:D1:2F:3E:D2:75:03:D7:FB:D0:9D:CC:C5:59:E2
     簽章演算法名稱: SHA1withRSA
     版本: 1


   4. 到 Google API 的憑證網址:
       https://console.developers.google.com/apis/credentials/

   5. 新增套件名稱指紋

-----------------------------------------------------------------------------------------------

K. Add permission check:


-----------------------------------------------------------------------------------------------

L. 產生給 app 上架使用的 Google Map API 金鑰



-----------------------------------------------------------------------------------------------

M. Google Play change to Android ACCESS_FINE_LOCATION permissions

Action required: If your app requires GPS hardware to operate properly, you will need to explicitly add the "android.hardware.location.gps
" uses-feature to your manifest.

What’s changing
We’re making a change on October 15th, 2016 that will affect apps targeting API version 21 (Android 5.0, Lollipop) or higher that use ACCESS_FINE_LOCATION but don't explicitly have the "android.hardware.location.gps" uses-feature. Going forward, these apps will be available to install on devices that don't have GPS hardware. In most cases this won't be an issue since Wi-Fi and Cell-ID based location provides high enough fidelity for the typical operation of these apps. However, any apps that require GPS hardware, such as GPS navigators, should explicitly add the "android.hardware.location.gps" uses-feature to their manifest.
If your app requires GPS to function properly and you do not include android.hardware.location.gps in your manifest declaration, your users may have a poor app experience.
Also, if you’re using the fused location provider and wish to receive the most accurate location samples from GPS (i.e. with PRIORITY_HIGH_ACCURACY), you must include the "android.hardware.location.gps" feature in your app’s manifest to ensure that Google Play only distributes your app to devices with GPS sensors.

-----------------------------------------------------------------------------------------------

N. Problem with GPS -- Intermittent operation

Nextbit Community - Problem with GPS -- Intermittent operation - Nextbit Community

Changing the type of data network. Allowing LTE or 3G connections brings up the problem,
forcing a 2G connection "fixes" the problem, i.e. GPS work ok.


2016年8月19日 星期五

Android: Developement Memo

since: 2016/08/19
update: 2016/10/13

reference:



-----------------------------------------------------------------------------------------------







-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

震動提示(Vibrate): (平板可能不會 work)

1. AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>

2. 匯入套件:
 import android.os.Vibrator;
3. 震動提示(Vibrate):
    Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
   
 // Vibrate for 500 milliseconds
 vibrator.vibrate(500);


-----------------------------------------------------------------------------------------------Refator:


-----------------------------------------------------------------------------------------------

Generate Signed APK Error:


=> 解決方式:
     只 import 會使用到的套件名稱.
     ex:
//@Google Play ------
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;


iOS: Developement Memo

since: 2016/08/19
update: 2016/10/13

reference: 1. iphone - Why does viewWillAppear not get called when an app comes back from the background?






-----------------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------

震動提示(Vibrate):

//@Vibration ------
import AudioToolbox

....
//@Vibration ------

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
....

-----------------------------------------------------------------------------------------------
Detect when an app comes back from the background:
....
    override func viewDidLoad() {
        super.viewDidLoad()


        // set observer for WillEnterForeground
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(willEnterForeground), name: UIApplicationWillEnterForegroundNotification, object: nil)
    }

    // my selector that was defined above
    func willEnterForeground() {
        // do stuff
    }

....
-----------------------------------------------------------------------------------------------
Xcode Error Message: [LogMessageLogging] 6.1 <private>

參考: アプリ開発ブログ(仮): 変なメッセージ:Xcode8、iOS10でいっぱい出た




-----------------------------------------------------------------------------------------------來自 Apple:
    Your app uses or references the following non-public APIs:

    stopTime

    The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.


=> use: grep -R 'stopTime' * to search the string



-----------------------------------------------------------------------------------------------
Apple Map:



-----------------------------------------------------------------------------------------------Build Phases:


-----------------------------------------------------------------------------------------------

Info.plist





2016年8月15日 星期一

Android: Generate Launcher Icons

since: 2016/08/15
update: 2016/09/03

reference:

A. Android Studio > File > New > Image Asset

-----------------------------------------------------------------------------------------------

B. Configure Image Asset:
     Image Asset: Launcher Icons
     Name: cat_launcher
     Asset Type: Image
     Path: /Users/Lanli/Desktop/cat.png
     Trim?: Yes
     Shape: Square
     (說明: Shape 為 None 時, 圖示最大)


     > Next

-----------------------------------------------------------------------------------------------

C. Finish

-----------------------------------------------------------------------------------------------

D. 結果:
     > 開啟 AndroidManifest.xml 修改 application 下的:
        android:icon="@mipmap/cat_launcher"

-----------------------------------------------------------------------------------------------

E. 安裝到 Android 上: