os x

分区

1
2
3
4
5
6
$ diskutil partitionDisk disk4 GPT fat32 Linux 10% ExFat d2 10% ExFat d3 80%
# 对 disk4 进行分区,GPT格式
# 三个分区:
# Linux (fat32格式,10%空间)
# d2 (ExFat格式,10%空间)
# d3 (ExFat格式,80%空间)

Write Bootable ISO

目标磁盘尽量格式化为 Mac OS Extended (Journaled),通过 ls /Volumns 查看目标磁盘。

Mac OS

Big Sur

1
2
3
4
5
6
7
8
$ sudo /Applications/Install\ macOS\ Big\ Sur.app/Contents/Resources/createinstallmedia --volume /Volumes/Big\ Sur --nointeraction # "/Volumes/Big\ Sur" 需要定制
# output
Erasing disk: 0%... 10%... 20%... 30%... 100%
Copying to disk: 0%... 10%... 20%... 30%... 100%
Making disk bootable...
Install media now available at "/Volumes/Install macOS Big Sur"

# 如果不显示进度,尝试重启终端 / 在 Disk Utility 中重新挂在卷

Catalina

1
$ sudo /Applications/Install\ macOS\ Catalina.app/Contents/Resources/createinstallmedia --volume /Volumes/Catalina --nointeraction

参考

Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 注: 暂时废弃

#### 1. Convert ISO to DMG
$ hdiutil convert -format UDRW -o cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.img cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.iso
# output 64_dvd_7134ba4b.iso
Reading CPBA_X64FRE_ZH-CN_DV9 (Apple_UDF : 0)…
..............................................................................................................................................................................................
Elapsed Time: 12.059s
Speed: 420.2Mbytes/sec
Savings: 0.0%
created: /Users/wii/Downloads/cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.img.dmg

#### 2. Rename
$ mv cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.img.dmg cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.img

#### 3. 写入
$ sudo dd if=cn_windows_10_business_editions_version_2004_updated_sep_2020_x64_dvd_7134ba4b.img of=/dev/rdisk2s5 bs=1m

unraid 插件

插件

1
2
3
4
5
6
$ vi /etc/hosts
199.232.68.133 raw.githubusercontent.com

# Plugin
https://github.com/dlandon/unassigned.devices/raw/master/unassigned.devices.plg
https://raw.githubusercontent.com/Squidly271/community.applications/master/plugins/community.applications.plg

VFIO

1
vfio-pci.ids=054c:0899

备份 USB 内容

Main -> Flash -> Flash Backup

image-20231207101821105

Problems

Requested operation is not valid: cannot undefine domain with nvram

1
$ virsh undefine --nvram "Macinabox BigSur"

设置 DNS

1
2
3
$ vim /boot/config/go
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
echo "nameserver 4.4.4.4" >> /etc/resolv.conf

下载 App 网络错误

查找 IP

1
2
3
# vim /etc/hosts
# 添加如下内容
<ip> raw.githubusercontent.com

保存数据

Unraid 系统中,/boot/mnt 之外的路径均在内存中,在下次重启后,会丢失。可以用如下方式来保存数据。

  • 第一步,数据保存在 /boot 路径下
  • 第二步,在 Unraid 系统启动时把文件拷贝到目标位置,有两种方式
    • 其一,更改 /boot/config/go 文件(shell 脚本),在 Unraid 系统启动时把文件拷贝到目标位置
    • 其二,使用 User Scripts 插件,在 Apps 中搜索安装,在 Plugins 中使用
      • 可以选择脚本运行时间,如果只需运行一次,选 At First Array Start Only 即可
      • 脚本路径在界面有显示,比如 /boot/config/plugins/user.scripts/scripts/dockerInit,这是一个目录,下面有个 script 文件用于编写命令

Docker

重启 Docker

1
$ /etc/rc.d/rc.docker restart

django startup

安装

1
2
3
4
5
# 安装最新
$ pip3 install django

# 指定版本
$ pip3 install django==3.0.7

创建应用

1
2
3
4
5
# 创建Project
$ django-admin startproject mysite

# 创建应用
$ python3 manage.py startapp polls

配置

数据库

MySQL

使用 pymysql 库替代 mysqldb

1
2
3
4
5
6
7
8
# 安装
$ pip install pymysql

# 替换
## settings.py 中添加
import pymysql
pymysql.version_info = (1, 4, 13, 'final', 0)
pymysql.install_as_MySQLdb()

配置

1
2
3
4
5
6
7
8
9
10
11
12
# settings.py

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test-db',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'root',
'PASSWORD': 'root'
}
}

python virtualenv

安装

1
2
3
4
5
6
7
8
# ubuntu
$ sudo apt install python3-pip # 安装pip3
## OR
$ sudo apt-get install python-pip python-dev python-virtualenv # for Python 2.7
$ sudo apt-get install python3-pip python3-dev python-virtualenv # for Python 3.n

# pip
$ pip3 install virtualenv

新建虚拟环境

1
2
3
4
5
6
$ virtualenv venv	# sample

$ virtualenv --no-site-packages venv # 不使用外部库; 默认不使用,在virtualenv>=20版本中移除,添加上可能会报错
$ virtualenv --system-site-packages venv # 使用系统库
$ virtualenv -p python3 venv # 指定Python版本
$ virtualenv --copies venv # 复制Python程序等,而不是链接

激活新环境

1
2
3
4
5
6
7
# 进入环境
$ cd venv
# 激活
$ source bin/activate

$ source bin/activate # bash, sh, ksh, or zsh
$ source bin/activate.csh # csh or tcsh

退出环境

1
deactivate

配合supervisor

supervisor配置文件

1
2
3
4
5
6
7
8
9
10
11
[program:myproj-uwsgi]
process_name=myproj-uwsgi
command=/home/myuser/.virtualenvs/myproj/bin/uwsgi
--chdir /home/myuser/projects/myproj
-w myproj:app
environment=PATH="/home/myuser/.virtualenvs/myproj/bin:%(ENV_PATH)s"
user=myuser
group=myuser
killasgroup=true
startsecs=5
stopwaitsecs=10

导出环境

1
2
3
4
# 导出
$ pip freeze > requirements
# 安装
$ pip install -r requirements

python requests

安装

1
$ pip3 install requests

引入

1
import requests

请求

1
2
headers = {'user-agent': '...'}
requests.get(url, headers=headers)

参数

1
2
3
4
5
6
7
8
# Get
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get(url, params=payload)

# Post Json
data = {'key': 'value'}
r = requests.post(url, data=json.dumps(data), headers=headers) # OR
r = requests.post(url, json=data)

读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 返回码
r.status_code

# Json 数据
r.json()

# 读取二进制数据
r.content

# 读取图片
from PIL import Image
from io import BytesIO

i = Image.open(BytesIO(r.content))

# 保存到文件
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)

树莓派配置

Wifi认证及静态IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#### Raspbian
$ cat /etc/network/interfaces
# ...
source-directory /etc/network/interfaces.d

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.8.160
netmask 255.255.255.0
network 192.168.8.1
gateway 192.168.8.1
broadcast 192.168.8.255
wpa-essid "..."
wpa-psk "..."

# wpa-essid: wifi名称
# wpa-psk: wifi 密码,psk认证

树莓派连接游戏手柄

驱动

xpadneo

依赖

1
2
# Raspbian
$ sudo apt install dkms raspberrypi-kernel-headers

安装

1
2
3
4
5
6
7
8
9
$ git clone https://github.com/atar-axis/xpadneo.git
$ cd xpadneo
$ sudo ./install.sh

# 如遇如下报错
# Your kernel headers for kernel 4.19.97-v7l+ cannot be found at
# /lib/modules/4.19.97-v7l+/build or /lib/modules/4.19.97-v7l+/source.
# 将 /lib/modules 下拷贝一份至缺失版本,如:
# sudo cp -r /lib/modules/5.4.79-v7l+ /lib/modules/4.19.97-v7l+

连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ bluetoothctl
$ [bluetooth]# scan on
...
# 长按xbox前侧配对键,至指示灯快速闪烁,出下如下新设备,记录其mac地址
[NEW] Device <MAC> Xbox Wireless Controller
...
[bluetooth]# pairable on
[bluetooth]# power on
[bluetooth]# pair <MAC>
[bluetooth]# trust <MAC>
[bluetooth]# connect <MAC>

# 如若报 Failed to connect: org.bluez.Error.Failed 错误,执行如下命令并重启系统
$ echo 'options bluetooth disable_ertm=Y' | sudo tee -a /etc/modprobe.d/bluetooth.conf

其他

1
2
# 查看设备
$ bluetoothctl devices

使用

Python

安装依赖

1
$ sudo pip3 install evdev asyncio

测试

1
2
3
4
5
6
$ cd xpadneo/misc/examples # 克隆下来的Git仓库
$ python3 python_asyncio_evdev/gamepad.py
press x to stop, Y to rumble light, B to rumble once, trigger and joystick right to see analog value
trigger_right = 32.98 joystick_right_x = 0

# 如果一切顺利,按手柄Y会感受到长震动;按B会感受到短震动;按X退出程序

按键

按键 code type 值类型
LT 10 03 区间,0~1023
RT 09 03 区间,0~1023
LB 310 01 单值,00,01
RB 311 01 单值,00,01
左摇杆 Y轴 01 03 区间,0~65535
左摇杆 X轴 00 03 区间,0~65535
右摇杆 Y轴 05 03 区间,0~65535
右摇杆 X轴 02 03 区间,0~65535
视图 158 01 单值,00,01
菜单 315 01 单值,00,01
十字键 上 17 03 单值,00,-1
十字键 下 17 03 单值,00,01
十字键 左 16 03 单值,00,-1
十字键 右 16 03 单值,00,01
Y 308 01 单值,00,01
X 307 01 单值,00,01
A 304 01 单值,00,01
B 305 01 单值,00,01

  • 摇杆X轴左侧为0,Y轴上侧为0

树莓派连接硬件

Speaker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# play audio
$ sudo apt-get install sox libsox-fmt-all
$ play file-name.mp3

# turn volume
$ amixer set Master 50%
$ amixer set Master 10%+

# slelect speaker
$ alsamixer
## F6 to select device

# set volume over 100%
$ sudo apt install pulseaudio
$ pulseaudio --start -D
$ pulseaudio --check -v # check
$ sudo apt-get install pulseaudio-utils
$ pactl set-sink-volume 0 300%

Camera

Web Stream

1
2
# install
$ sudo apt-get install libjpeg8-dev imagemagick

Reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# setup and test
https://www.raspberrypi.org/documentation/configuration/camera.md

# official documents
https://www.raspberrypi.org/documentation/raspbian/applications/camera.md

# web stream
https://blog.miguelgrinberg.com/post/stream-video-from-the-raspberry-pi-camera-to-web-browsers-even-on-ios-and-android
https://blog.miguelgrinberg.com/post/how-to-build-and-run-mjpg-streamer-on-the-raspberry-pi
## 注释 mjpg-streamer/util.c stats.h
# // #include <linux/stat.h>
# // #include <sys/stat.h>

# generate stream
$ raspistill --nopreview -w 640 -h 480 -q 5 -o /home/pi/test.jpg -tl 100 -t 9999999 -th 0:0:0

# serve stream
$ LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /home/pi -n test.jpg" -o "output_http.so -w /usr/local/www"

# watch
http://rpi-ip:8080

L298N

参考