# [20190505 - Raspberry Pi](https://hackmd.io/s/SkgLJCCnV)
###### tags: `tcfst`
Author
- Yuan < e61983@gmail.com >
- Wei < demon0303loki@gmail.com >
- Jia-Hao,Hsu < jhs.8901.3737@gmail.com >
## License

## Command
### Show IP address
```shell=
ifconfig
```
### Show Pin-Map
```shell=
pinout
```
Output:
<div>
<img src="https://i.imgur.com/KbLbLBR.png" style="width: 58%; height: auto;">
<img src="https://i.imgur.com/IY7xr0z.png" style="width: 40%; height: auto; float: auto;">
</div>
### Reference
- [offcial][pintout-offcial]
source code
- [pinout source code][pinout-source]
### gpio - wiringPI
```shell
gpio readall
# Output:
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 0 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | OUT | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
```
### Setting WIFI
```shell=
sudo leafpad /etc/wpa_supplicant/wpa_supplicant.conf
```
### Show Route
```shell=
# Show current route
route -n
# Add a new route rule
route add -net IP_ADDRESS gateway GATEWAY dev INTERFACE
# Delete a route rule
route del -net IP_ADDRESS dev INTERFACE
# Set Default gateway
route add default gw GATWAY
```
- Reference
[鳥哥的 Linux 私房菜 - 第五章、 Linux 常用網路指令][鳥哥的 Linux 私房菜 - 第五章、 Linux 常用網路指令]
### Setting Static IP Address
早期在 `/etc/network/interface`
現在在 `/etc/dhcpcd.conf`
### Samba
- Install
```shell=
sudo apt install -y samba
```
- Setting
```shell=
vim /etc/samba/smb.conf
# Setting
[global]
netbios name=RPi-File-Server # HostName
workgroup = WORKGROUP # Group Name
security=user # Security
[share]
path =/home/pi/share # Abs path
browseable =yes
read only =no
valid users =pi
create mask =0775
directory mask =0775
```
:::danger
smbpasswd 已經不用了改用 pdbedit
:::
:::warning
Samba 2 版以前的帳號管理是用 smbpasswd , 密碼檔為文字檔 , 使用者多的話 , 效率較差 .
Samba 3 版的使用者帳號管理預設是使用後端 tdbsam 資料庫管理機制 .
管理 Samba 使用者是用 pdbedit 這個指令 , 參數满多 ,
下面提列幾個簡單常用的參數 : (詳細使用方式請參考 man pdbedit)
pdbedit -L # 列出使用者列表 .
pdbedit -Lv # 列出詳細的使用者列表 .
pdbedit -Lw # 列出同 smbpasswd 格式的使用者列表 .
pdbedit -a user # 新增 user 這個帳號 .
pdbedit -x user # 刪除 user 這個帳號 .
pdbedit -c "[D]" -u user # 暫時停用 user 這個帳號 .
pdbedit -c "[]" -u user # 恢復使用 user 這個帳號 .
:::
- Reference
[鳥哥的Linux私房菜 - samba][鳥哥的Linux私房菜 - samba]
---
## GPIO using Python
### Version
```shell=
python -V
```
### Install Library
```shelll=
sudo apt install python-rpi.gpio
```
### Print
```python
str = "the length of (%s) is %d" %('runoob',len('runoob'))
print(str)
# output: the length of (runoob) is 6
```
### Array
```python
#!/usr/bin/env python
#
a = [2, 3, 4 , 5 ,6 ,7]
# b = a[index:index-1] => index 1 - index 4
b = a[2:5]
print(b)
# output: [4, 5, 6]
```
### Class
```python
class Duck:
def fly(self):
print("Duck flying")
class Airplane:
def fly(self):
print("Airplane flying")
class Whale:
def swim(self):
print("Whale swimming")
def fly(entity):
entity.fly()
duck = Duck()
airplane = Airplane()
whale = Whale()
fly(duck) # prints `Duck flying`
fly(airplane) # prints `Airplane flying`
fly(whale) # Throws the error `'Whale' object has no attribute 'fly'`
```
### try, raise
```python=
import traceback
try:
input = int(input('輸入整數:'))
print('{0} 為 {1}'.format(input, '奇數' if input % 2 else '偶數'))
except ValueError:
print('請輸入阿拉伯數字')
except (EOFError, KeyboardInterrupt):
print('使用者中斷程式')
except:
print('不明的程式中斷')
traceback.print_exc()
```
### Import
```python=
# Import a module
import [module]
# Import a part of module
from [module] import [name1, name2, ...]
# Alias
import [module] as [new_name]
```
- Reference
[Python 的 Import 陷阱][Python 的 Import 陷阱]
### GPIO
```python=
import RPi.GPIO as GPIO
import time
var LED_PIN = 23
var INPUT_PIN = 17
"""
Pin numbering
There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO.
The first is using the BOARD numbering system.
This refers to the pin numbers on the P1 header of the Raspberry Pi board.
The advantage of using this numbering system is that your hardware will always work,
regardless of the board revision of the RPi.
You will not need to rewire your connector or change your code.
The second numbering system is the BCM numbers. This is a lower level way of working -
it refers to the channel numbers on the Broadcom SOC.
You have to always work with a diagram of which channel number goes to which pin on the
RPi board.
Your script could break between revisions of Raspberry Pi boards.
To specify which you are using using (mandatory):
GPIO.setmode(GPIO.BOARD)
# or
GPIO.setmode(GPIO.BCM)
"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
"""
time.sleep(secs)
Suspend execution of the calling thread for the given number of seconds.
"""
time.sleep(1)
GPIO.output(LED_PIN, GPIO.HIGH)
GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if GPIO.input(INPUT_PIN):
print('Input was HIGH')
else:
print('Input was LOW')
GPIO.cleanup()
"""
To output to several channels at the same time:
chan_list = (11,12)
GPIO.output(chan_list, GPIO.LOW) # all LOW
GPIO.output(chan_list, (GPIO.HIGH,GPIO.LOW)) # first LOW, second HIGH
"""
```
- Reference
[RPi.GPIO Official][RPi.GPIO Official]
## Kernel
- Module path: /lib/modules/\`uname -r \`/build
課程中的Linux 原始碼放在 ~/share/linux
```shell=sh
sudo ln -s ~/share/linux /lib/modules/`uname -r`/build
```
### Tailoring
```shell=
make menuconfig
```
### Hello World
Please refer to https://gitlab.com/e61983/linux_driver
- Reference
[linux-source][linux-source]
### THIS_MODULE ?
- [深入淺出 insmod, #1][深入淺出 insmod, #1]
- [THIS_MODULE][THIS_MODULE]
### Virtual Filesystem
- Document Path
`KERNEL_SOURCE/Documentation/filesystems/vfs.txt` or [Kernel-vfs.txt][Kernel-vfs.txt]
### Sysfs
### Add Module into Kernel
```shell=
cp demo_driver KERNEL_SOURCE/drivers/char
```
### udev rules
udev 規則路徑: /etc/udev/rules.d/
我們可以自訂規則改變裝置節點的權限
```shell=
#/etc/udev/rules.d/71.customized_device_rw.rules
ACTION=="add", KERNEL=="demo_*", SUBSYSTEM=="demo", GROUP="user", MODE= 0660
```
如果不知道要如何撰寫,可以參考 udevadm info 的輸出
```shell=sh
udevadm info -a --name /dev/xxxxxx # 可作為 udev rules 參考
```
#### Reference:
- [udevadm info][udevadm info]
## Software Note
- [RealVNC][RealVNC]
- [Anydesk][Anydesk]
- [Python On-Line IDE][Python On-Line IDE]
---
## Module Pins
### LED & Bottom module

## 場外
[mopcon-2018-warm-up][mopcon-2018-warm-up]
[Mosky - Python][Mosky - Python]
---
## 梗區
> ~~vim很難用千萬不要用~~
> [name=李老師]
> 重開機治百病
> [name=李芫]
[pintout-offcial]:
https://pinout.xyz/
[pinout-source]:
https://github.com/Gadgetoid/Pinout.xyz
[mopcon-2018-warm-up]:
https://hackmd.io/c/mopcon-2018-warm-up/%2FVP9XaAm_RrOPGbbpstkJiw
[Mosky - Python]:
https://www.youtube.com/watch?v=GhQH1l-MjGc
[Anydesk]:
https://anydesk.com/zhs/
[RealVNC]:
https://www.realvnc.com/en/connect/download/viewer/
[鳥哥的Linux私房菜 - samba]: http://linux.vbird.org/linux_server/0370samba/0370samba-centos4.php
[鳥哥的 Linux 私房菜 - 第五章、 Linux 常用網路指令]:
http://linux.vbird.org/linux_server/0140networkcommand.php#route
[Python On-Line IDE]:
https://www.tutorialspoint.com/execute_python_online.php
[Python 的 Import 陷阱]:
https://medium.com/pyladies-taiwan/python-%E7%9A%84-import-%E9%99%B7%E9%98%B1-3538e74f57e3
[RPi.GPIO Official]:
https://pypi.org/project/RPi.GPIO/
[linux-source]:
https://elixir.bootlin.com/linux/v4.19.34/source
[深入淺出 insmod, #1]:
http://www.jollen.org/blog/2006/11/hello_world_insmod_1.html
[THIS_MODULE]:
https://blog.csdn.net/a954423389/article/details/6101369
[Kernel-vfs.txt]:
https://elixir.bootlin.com/linux/v4.19.34/source/Documentation/filesystems/vfs.txt
[udevadm info]: https://www.ibm.com/developerworks/cn/linux/l-cn-udev/index.html