2016年2月7日 星期日

php 的一些基本研究



執行  php file....

php   index.php



PHP 支援八種基本型態 (primitive type)

這八種分別是
  • 布林 (boolean)
  • 整數 (integer)
  • 浮點數 (floating point number)
  • 字串 (string)
  • 陣列 (array)
  • 物件 (object)
  • 資源 (resource)
  • NULL
基本上程式 (program) 會自動判斷變數 (variable) 的資料型態 (data type) , PHP 的變數寫法就比較特別一點,用金錢符號 $ 開頭,其後可接英文字母,如下 $b 為布林型態, $i 為整數型態, $f 為浮點數型態, $s 為字串型態

$b = True; // 布林型態
$i = 1234; // 整數型態
$f = 12.3; // 浮點數型態
$s = "12"; // 字串型態

陣列就比較複雜一點,因為陣列分成兩部份,第一部分為索引,第二部份則是資料,兩者可以是上面四者型態任一,資料還可以是其他的陣列,例如
$a1 = array(10, 20, 30, 40);
$a2 = array(
    False => True,
    33 => "Tony",
    "A" => 33.06,
    6.32 => array(1, 2),
);
建立陣列需要用到內建函數 (function) array() ,索引與資料放在參數列 (parameter list) ,若無提供索引,如 $a1 ,索引會自動由 0 遞增填補,因此存取 $a1 如下
echo $a1[0];
echo $a1[1];
echo $a1[2];
echo $a1[3];

來個  imagettftext 的例子....

由於他需要  PHP GD2 library

sudo apt-get install php5-gd



<?php// Set the content-typeheader('Content-Type: image/png');// Create the image$im = imagecreatetruecolor(400, 30);// Create some colors$white = imagecolorallocate($im, 255, 255, 255);$grey = imagecolorallocate($im, 128, 128, 128);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, 399, 29, $white);// The text to draw$text = 'Testing...';// Replace path by your own font path$font = 'arial.ttf';// Add some shadow to the textimagettftext($im, 20, 0, 11, 21, $grey, $font, $text);// Add the textimagettftext($im, 20, 0, 10, 20, $black, $font, $text);// Using imagepng() results in clearer text compared with imagejpeg()imagepng($im);imagedestroy($im);?>

改一下例子....

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 400);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 399, $white);

// The text to draw
//$text = 'Testing...';
$text = '紀富中';
// Replace path by your own font path
//$font = '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf';
$font1 = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc';
$font2 = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc';

// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 30, 0, 40, 40, $black, $font1, $text);
imagettftext($im, 30, 0, 80, 80, $black, $font2, $text);
imagepng($im);
imagedestroy($im);
?>

執行結果如下 :


還有一些可以畫圖畫線的library....

http://jpgraph.net/download/
http://pydoing.blogspot.tw/2012/11/php-How-to-Execute.html

一些字型下載的網址
http://fonts.mobanwang.com/

2016年2月6日 星期六

mini laser

https://makersattic.hackpad.com/mini-laser-hZpH6GknKdn

手把手打造C compiler



https://embedded2016.hackpad.com/-C--ExzwrKJwZCJ

http://lotabout.me/2015/write-a-C-interpreter-0/

在 Raspberry 上面建立 php 的網頁server



sudo apt-get install apache2 -y

用web browse 去  http://192.168.1.9  (RPI 的ip.... 這不一定是你的...請用ifconfig 去查看)

如果有看到下面的畫面....代表apache php server 已經啟動了


  The default web is in 
/var/www/html/index.html.

cd /var/www/html
ls -al

ADDITIONAL - INSTALL PHP

To allow your Apache server to process PHP files, you'll need to install PHP5 and the PHP5 module for Apache. Type the following command to install these:
sudo apt-get install php5 libapache2-mod-php5 -y

Now remove the index.html file:
sudo rm index.html
and create the file index.php:
sudo nano index.php

<?php echo "hello world"; ?>


or show your PHP info:
<?php phpinfo(); ?>

apache2 server operation

  • stop it: sudo /etc/init.d/apache2 stop
  • start it with: sudo /etc/init.d/apache2 start
  • reload the config with: sudo /etc/init.d/apache2 reload
  • restart (stop followed by start): sudo /etc/init.d/apache2 restart


Reference : https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md

2016年2月5日 星期五

智慧小車的初始程式

參考網站所寫 ....不過一直弄不懂 GPIO.BOARD和 GPIO.BCM的分別....

BOARD -> pin number

BCM     -> GPIO number

舉例來說....如果你要用GPIO 2   ....

如果你設的是BCM....就是 2...

                        BOARD ..就是 3




#!/usr/bin/python

import RPi.GPIO as GPIO

dout1 = 6 # Broadcom pin 3
dout2 = 13 # Broadcom pin 5
dout3 = 19 # Broadcom pin 7
dout4 = 26 # Broadcom pin 11

GPIO.setmode(GPIO.BCM)
GPIO.setup(dout1,GPIO.OUT)
GPIO.setup(dout2,GPIO.OUT)
GPIO.setup(dout3,GPIO.OUT)
GPIO.setup(dout4,GPIO.OUT)
GPIO.output(dout1, GPIO.LOW)
GPIO.output(dout2, GPIO.LOW)
GPIO.output(dout3, GPIO.LOW)
GPIO.output(dout4, GPIO.LOW)
while True:
     print "please input key : (a -> left, w -> front, d -> right, x -> back, c -> quit)"
     a = raw_input()
     print "you input is " + str(a)

     if a == 'd': # right
        GPIO.output(dout1, GPIO.LOW)
        GPIO.output(dout2, GPIO.HIGH)
        GPIO.output(dout3, GPIO.HIGH)
        GPIO.output(dout4, GPIO.LOW)
     elif a == 'a': # left
        GPIO.output(dout1, GPIO.HIGH)
        GPIO.output(dout2, GPIO.LOW)
        GPIO.output(dout3, GPIO.LOW)
        GPIO.output(dout4, GPIO.HIFG)
     elif a == 'x': # back
        GPIO.output(dout1, GPIO.LOW)
        GPIO.output(dout2, GPIO.HIGH)
        GPIO.output(dout3, GPIO.LOW)
        GPIO.output(dout4, GPIO.HIGH)
     elif a == 'w': # front
        GPIO.output(dout1, GPIO.HIGH)
        GPIO.output(dout2, GPIO.LOW)
        GPIO.output(dout3, GPIO.HIGH)
        GPIO.output(dout4, GPIO.LOW)
     elif a == 'c':  # quit
        break;



GPIO.output(dout1, GPIO.LOW)
GPIO.output(dout2, GPIO.LOW)
GPIO.output(dout3, GPIO.LOW)
GPIO.output(dout4, GPIO.LOW)
GPIO.cleanup()


=============================================================

2/7  update...  加入聲控.....

http://0975128810.blogspot.tw/2016/02/use-raspberry-for-voice-control.html


=============================================================





使用usb行動電源來當5V的電池


 首先來看一下usb 的 pin 腳....他其實就是四根.... VCC , GND, D+, D-...

而我們的目標要把VCC和GND拿出來用....

可以發現的是   VCC ->  pin1
                           GND -> pin 4


但是你如果直接把VCC和GND接出來用....你會發現...VCC根本沒有5V

這是因為...當usb 的行動輸出時...他會用D+和D- 來判斷你是甚麼樣的裝置...

如果D+ 和 D-空接的話...他會認為目前沒有裝置...不需要供電....然後馬上斷電...

有網友熱心分享.... 其實只要把 D+ 和 D- 接在一起(其實是200歐姆)....他就會認為你是

裝置...進而供電.....


接在一起後...真的就有電輸出了...