Skip to content

スクリプトによるRaspberry Pi設定の自動化 #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
df3aec5
/boot/firmware/confit.txtにdtoverlayとdtparamを追記するスクリプトを作成
KuraZuzu Sep 5, 2024
4625ff3
カーネルのバージョンに応じてdtoverlayの設定を挿入するかの分岐条件を追加
KuraZuzu Sep 5, 2024
b561e60
spiとi2cをonにする処理を追加
KuraZuzu Sep 5, 2024
d5aa88a
OSが32bit版であれば"arm_64bit=0"を追記するように変更
KuraZuzu Sep 5, 2024
83c5af9
Raspberry Pi Mouse V3の設定である旨のコメントを追記する処理追加
KuraZuzu Sep 5, 2024
af8636b
Merge branch 'master' into feature/setting-configs
KuraZuzu Sep 9, 2024
de06624
READMEを整備し、設定スクリプトをshからbashに変更
KuraZuzu Sep 9, 2024
8035705
config.txtに追記する手順の説明方法を以前からあるものに統一した
KuraZuzu Sep 9, 2024
2b08247
arm_64bit=0追記時のメッセージ標準出力で最初に無駄なスペースが入る点を修正
KuraZuzu Sep 11, 2024
6125959
"dtparam=i2c_arm=on"と"dtparam=spi=on"の戦闘に#が付いてコメント担っていた場合は解除するように修正
KuraZuzu Sep 11, 2024
cce3fcc
動作しているOSのbit数を判定するコマンドを修正(uname -mだとRaspberry Pi OSの32bitでも64bitと同じ結果…
KuraZuzu Sep 12, 2024
0cbb89e
64bitOSなのにarm_64bit=0が合った場合に削除するよう変更
KuraZuzu Sep 12, 2024
524c6e9
カーネルバージョンの比較が正しく行われていなかったため、整数に変換して比較するように変更
KuraZuzu Sep 12, 2024
cd239dc
Merge branch 'master' into feature/setting-configs
KuraZuzu Sep 13, 2024
92e12c2
UbuntuとRaspberry Pi OSでカーネルヘッダーの手順が違うのでREADMEを修正
KuraZuzu Sep 13, 2024
bae52c3
CI結果のバッチを修正
KuraZuzu Sep 17, 2024
374da7c
README.mdの文中の無駄な空白を削除
KuraZuzu Sep 17, 2024
90cd60d
リポジトリのcloneとRaspberry Piの設定を行う項目の説明を修正
KuraZuzu Sep 17, 2024
f1b74a0
"/dev/null"へのリダイレクトを">"に統一
KuraZuzu Sep 17, 2024
606e958
カーネルバージョンを扱う箇所を変更
KuraZuzu Sep 17, 2024
5d1935f
カーネルバージョン比較のときに整数にしている理由とマイナーバージョンのゼロ埋めについてコメントを追記
KuraZuzu Sep 17, 2024
a17dc19
設定に関していくつか変更を加えました。
KuraZuzu Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
# RaspberryPiMouse

![CI](https://github.com/rt-net/RaspberryPiMouse/workflows/CI/badge.svg?branch=master)
[![CI](https://github.com/rt-net/RaspberryPiMouse/actions/workflows/driver-cross-build.yml/badge.svg)](https://github.com/rt-net/RaspberryPiMouse/actions/workflows/driver-cross-build.yml)

This repository has the source code and kernel objects
for the Raspberry Pi Mouse.

## Installation

Run the installation script ([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash)).
Run the following scripts.

インストール用のシェルスクリプト([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash))を実行します。
- setting script ([`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash))
- installation script ([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash))

### for Raspberry Pi OS

`/boot/firmware/config.txt`を編集し、ファイル末尾に以下の設定を追加します。

```sh
arm_64bit=0 # "64-bit"版では不要です
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000 # カーネル5.16未満の場合は不要です
dtparam=i2c_baudrate=62500
```
以下のスクリプトを実行します
- setting script([`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash))
- installation script([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash))

Raspberry Piを再起動します。
### for `Raspberry Pi OS`・`Ubuntu`

以下のコマンドでインストールを実行します
以下のコマンドで本リポジトリをダウンロードし、Raspberry Pi本体の設定を行います

```sh
```bash
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
$ cd RaspberryPiMouse/utils
$ sudo apt install raspberrypi-kernel-headers build-essential
$ ./build_install.bash
$ ./set_configs.bash
```

### for Ubuntu
Raspberry Piを再起動し、以下のコマンドを実行してビルドに必要なファイルをインストールします。**`Ubuntu`と`Raspberry Pi OS`でコマンドが違います。**

`/boot/firmware/config.txt`を編集し、ファイル末尾に以下の設定を追加します。
```bash
# Ubuntu Serverの場合
$ sudo apt install linux-headers-$(uname -r) build-essential

```sh
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000 # "Ubuntu Server 22.04"では不要です
dtparam=i2c_baudrate=62500
# Raspberry Pi OSの場合
$ sudo apt install raspberrypi-kernel-headers build-essential
```

Raspberry Piを再起動します
以下のコマンドでビルドを実行します

以下のコマンドでインストールを実行します。

```sh
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
```bash
$ cd RaspberryPiMouse/utils
$ sudo apt install linux-headers-$(uname -r) build-essential
$ ./build_install.bash
```

### Manual installation

```sh
```bash
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
$ cd RaspberryPiMouse/src/drivers
$ make
Expand All @@ -63,23 +54,31 @@ $ sudo insmod rtmouse.ko

## Notes for the installation (ドライバの導入の際の注意)

特定の機能の有効化や、プログラムのビルドのために設定が必要な項目があります。以下の設定をご確認ください。

※[`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash)を実行すると、[自動で適切な設定に書き換わります]((https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash))。


### for Raspberry Pi OS

Raspberry Pi OSでは、SPIとI2Cがデフォルトで無効となっているため、有効化する必要があります。

#### for SPI and I2C

Enable SPI and I2C functions via `raspi-config` command.

以下の設定を確認ください。
`raspi-config` コマンドで設定します。

* SPI機能を「入」にする。
* I2C機能を「入」にする。


### for Raspberry Pi 4

Edit [`rtmouse.c`](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54) to change the defined value `RASPBERRYPI` from '2' to '4'.
Edit [`rtmouse.c`](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54) to change the defined value `RASPBERRYPI` from`2`to`4`.

Raspberry Pi 4ではCPUのレジスタがそれまでのRaspberry Piとは異なります([issues#21](https://github.com/rt-net/RaspberryPiMouse/issues/21))。
Raspberry Pi 4で本ドライバを使用する際には`rtmouse.c`の以下の行(2020年4月13日現在の最新版のv2.1.0では[54行目](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54))を`RASPBERRYPI 4`に書き換えてビルドする必要があります。

※[`./utils/build_install.bash`](./utils/build_install.bash)を実行すると、Raspberry Piのモデルに合わせて[`rtmouse.c`](./src/drivers/rtmouse.c)が[自動で書き換わります](https://github.com/rt-net/RaspberryPiMouse/blob/a9af4fa2b2a8e34c0f93a6ce5cf88ebd50ff39c2/utils/build_install.raspi4ubuntu.bash#L13-L14)。

```c
Expand All @@ -91,6 +90,24 @@ Raspberry Pi 4で本ドライバを使用する際には`rtmouse.c`の以下の
#define RASPBERRYPI 2
```

### for 32-bit OS

Set 32bit-setting to `/boot/firmware/config.txt`.

32-bit版のOSではビルドするために、`/boot/firmware/config.txt`に以下の1行を追加する必要があります。

```bash
arm_64bit=0
```

### デバイスツリーオーバーレイについて

kernel `5.16`以降では`/boot/firmware/config.txt`に以下の設定を記述し、dtoverlayを設定する必要があります。※[`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash)を実行すると、設定は[自動で書き換わります]((https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash#L35-#L49))。

```bash
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000
```

### パルスカウンタについて

パルスカウンタは値の読み取りにI2Cを使用しています。仕様上は400kHzまでbaudrateを上げることができます(※1)。
Expand All @@ -102,9 +119,11 @@ it may be necessary to set the I2C baudrate lower than the default value.
Add a following new line in `/boot/firmware/config.txt` to change the i2c_baudrate to 62.5 kHz.

`/boot/firmware/config.txt`に以下の1行を追加することでI2Cのbaudrateを62.5kHzに固定することができます。
```

```bash
dtparam=i2c_baudrate=62500
```

※1 Raspberry Pi 4 Model B(Ubuntu Server `18.04` / `20.04` / `22.04` / `24.04`)を搭載して400kHzで通信できることを確認しています。
※2 現在設定されているI2Cのbaudrateは以下のコマンドを実行することで確認できます。
```
Expand Down
91 changes: 91 additions & 0 deletions utils/set_configs.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -eu

# settings comment
SETTING_COMMENT='# Raspberry Pi Mouse V3 settings'

# OS architecture (32-bit or 64-bit)
ARCHITECTURE=$(getconf LONG_BIT)

# dtoverlay setting
DTOVERLAY='dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000'

# i2c_baudrate-setting
DTPARAM='dtparam=i2c_baudrate=62500'

# config-file PATH
CONFIG_FILE='/boot/firmware/config.txt'

# compare kernel versions as integers for accurate version comparison (excluding minor versions).
GET_KERNEL_VERSION_INT() {
# 0-padding is used to avoid minor versions being compared by their first digit.
echo "$1" | awk -F. '{ printf "%d%02d", $1, $2 }'
}

# kernel version
KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
KERNEL_VERSION_INT=$(GET_KERNEL_VERSION_INT "$KERNEL_VERSION")

# add "Raspberry Pi Mouse v3" settings
if ! grep -qxF "$SETTING_COMMENT" "$CONFIG_FILE"; then
echo "$SETTING_COMMENT" | sudo tee -a "$CONFIG_FILE" > /dev/null
fi

# check if the OS is running in 32-bit mode
if [[ "$ARCHITECTURE" == "32" ]]; then
if ! grep -qxF "arm_64bit=0" "$CONFIG_FILE"; then
echo "arm_64bit=0" | sudo tee -a "$CONFIG_FILE"
echo "Add \"arm_64bit=0\" > $CONFIG_FILE"
fi
elif [[ "$ARCHITECTURE" == "64" ]]; then
# remove arm_64bit=0 if present in a 64-bit environment
if grep -qxF "arm_64bit=0" "$CONFIG_FILE"; then
sudo sed -i '/arm_64bit=0/d' "$CONFIG_FILE"
echo "Removed \"arm_64bit=0\" from $CONFIG_FILE"
fi
fi

# add dtparam-setting for "/boot/firmware/config.txt"
if ! grep -qxF "$DTPARAM" "$CONFIG_FILE"; then
echo "$DTPARAM" | sudo tee -a "$CONFIG_FILE" > /dev/null
echo "Add \"$DTPARAM\" > $CONFIG_FILE"
fi

# use device-tree-overlay when the kernel is 5.16 or higher
if (( KERNEL_VERSION_INT >= $(GET_KERNEL_VERSION_INT 5.16) )); then
# add dtoverlay-setting for "/boot/firmware/config.txt"
if ! grep -qxF "$DTOVERLAY" "$CONFIG_FILE"; then
echo "$DTOVERLAY" | sudo tee -a "$CONFIG_FILE" > /dev/null
echo "Add \"$DTOVERLAY\" > $CONFIG_FILE"
fi
else
# remove dtoverlay-setting if kernel is less than 5.16
if grep -qxF "$DTOVERLAY" "$CONFIG_FILE"; then
sudo sed -i "/$DTOVERLAY/d" "$CONFIG_FILE"
echo "Removed \"$DTOVERLAY\" from $CONFIG_FILE"
fi
fi

# replace "dtparam=i2c_arm=off" with "dtparam=i2c_arm=on"
if grep -qxF 'dtparam=i2c_arm=off' "$CONFIG_FILE"; then
sudo sed -i 's/dtparam=i2c_arm=off/dtparam=i2c_arm=on/' "$CONFIG_FILE"
echo "Changed \"dtparam=i2c_arm=off\" to \"dtparam=i2c_arm=on\" in $CONFIG_FILE"
fi

# replace "dtparam=spi=off" with "dtparam=spi=on"
if grep -qxF 'dtparam=spi=off' "$CONFIG_FILE"; then
sudo sed -i 's/dtparam=spi=off/dtparam=spi=on/' "$CONFIG_FILE"
echo "Changed \"dtparam=spi=off\" to \"dtparam=spi=on\" in $CONFIG_FILE"
fi

# uncomment "dtparam=i2c_arm=on" if it is commented
if grep -qxF '#dtparam=i2c_arm=on' "$CONFIG_FILE"; then
sudo sed -i 's/#dtparam=i2c_arm=on/dtparam=i2c_arm=on/' "$CONFIG_FILE"
echo "Uncommented \"dtparam=i2c_arm=on\" in $CONFIG_FILE"
fi

# uncomment "dtparam=spi=on" if it is commented
if grep -qxF '#dtparam=spi=on' "$CONFIG_FILE"; then
sudo sed -i 's/#dtparam=spi=on/dtparam=spi=on/' "$CONFIG_FILE"
echo "Uncommented \"dtparam=spi=on\" in $CONFIG_FILE"
fi
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy