Skip to content

Commit

Permalink
Merge pull request #2 from radxa/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shawnchainw authored Mar 11, 2024
2 parents 070c353 + 831d41c commit 792ab89
Show file tree
Hide file tree
Showing 28 changed files with 393 additions and 172 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build-deb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build DEB Package

Check failure on line 1 in .github/workflows/build-deb.yml

View workflow job for this annotation

GitHub Actions / build

.github/workflows/build-deb.yml#L1

This run was manually canceled.

on:
push:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build DEB package
run: |
chmod -R 775 rockpi-penta/DEBIAN/
dpkg-deb --build -Z gzip rockpi-penta
- name: Upload DEB package
uses: actions/upload-artifact@v4
with:
name: rockpi-penta.deb
path: rockpi-penta.deb
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -102,11 +101,8 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

Expand Down
10 changes: 0 additions & 10 deletions DEBIAN/postinst

This file was deleted.

6 changes: 3 additions & 3 deletions DEBIAN/control → rockpi-penta/DEBIAN/control
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: rockpi-penta
Version: 0.10
Version: 0.2
Architecture: all
Maintainer: Radxa <[email protected]>
Depends: python3
Depends: python3, python3-libgpiod, python3-pillow, python3-pip
Section: utils
Priority: optional
Homepage: https://github.com/akgnah/rockpi-penta
Homepage: https://github.com/radxa/rockpi-penta
Description: Rockpi Penta Hat
98 changes: 98 additions & 0 deletions rockpi-penta/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash -e

checkReboot() {
echo Installation will take effect after reboot!!!
}

is_armbian() {
if [ -f /boot/armbianEnv.txt ] || [ -f /boot/dietpiEnv.txt ]; then
echo true
else
echo false
fi
}

armbian_require_overlay() {
envFile='/boot/armbianEnv.txt'
[ -f /boot/dietpiEnv.txt ] && envFile='/boot/dietpiEnv.txt'
overlays=$(grep '^overlays=' "$envFile" | cut -d'=' -f2)
for arg in "$@"; do
if grep -qE "(^|[[:space:]])$arg([[:space:]]|$)" <<< $overlays; then
echo "Overlay ${arg} was already added to $envFile, skip..."
else
overlays="${overlays} ${arg}"
echo "Added overlay: $arg"
fi
done
if grep -q "^overlays=" $envFile; then
sed -i '/^overlays=/c\'"overlays=$overlays" $envFile
else
echo "overlays=$overlays" >> $envFile
fi
}

if [ "$(python3 -c 'import sys; print(f"{sys.version_info.minor >= 11}")')" = "True" ]; then
pip3 install adafruit-circuitpython-ssd1306 --break-system-packages
else
pip3 install adafruit-circuitpython-ssd1306
fi

systemctl enable rockpi-penta.service

model=$(tr -d '\0' </proc/device-tree/model)

case "$model" in
*"Raspberry Pi 5"*)
raspi-config nonint do_i2c 0 >/dev/null || true
cp /usr/bin/rockpi-penta/env/rpi5.env /etc/rockpi-penta.env
systemctl start rockpi-penta.service
;;
*"Raspberry Pi 4"*)
raspi-config nonint do_i2c 0 >/dev/null || true
mv /usr/bin/rockpi-penta/env/rpi4.env /etc/rockpi-penta.env
systemctl start rockpi-penta.service
;;
*"ROCK 5"*)
if [ "$(is_armbian)" = "true" ]; then
armbian_require_overlay rk3588-i2c8-m2 rk3588-pwm14-m1
cp /usr/bin/rockpi-penta/env/rock_5a_armbian.env /etc/rockpi-penta.env
else
mv /boot/dtbo/rk3588-i2c8-m2.dtbo.disabled /boot/dtbo/rk3588-i2c8-m2.dtbo || true
mv /boot/dtbo/rk3588-pwm14-m1.dtbo.disabled /boot/dtbo/rk3588-pwm14-m1.dtbo || true
cp /usr/bin/rockpi-penta/env/rock_5a.env /etc/rockpi-penta.env
u-boot-update || true
fi
checkReboot
;;
*"ROCK3 Model C"*)
mv /boot/dtbo/rk3568-i2c3-m0.dtbo.disabled /boot/dtbo/rk3568-i2c3-m0.dtbo || true
cp /usr/bin/rockpi-penta/env/rock_3c.env /etc/rockpi-penta.env
u-boot-update || true
checkReboot
;;
*"ROCK3 Model A"*)
mv /boot/dtbo/rk3568-i2c3-m0.dtbo.disabled /boot/dtbo/rk3568-i2c3-m0.dtbo || true
mv /boot/dtbo/rk3568-pwm15-m0.dtbo.disabled /boot/dtbo/rk3568-pwm15-m0.dtbo || true
cp /usr/bin/rockpi-penta/env/rock_pi_3.env /etc/rockpi-penta.env
u-boot-update || true
checkReboot
;;
*"Radxa ROCK 4SE"* | *"ROCK Pi 4"*)
if [ "$(is_armbian)" = "true" ]; then
cp /usr/bin/rockpi-penta/overlays/rk3399-pwm1.dtbo /boot/dtb/rockchip/overlay/rockchip-rk3399-pwm1-by-rockpi-penta.dtbo
chmod +x /boot/dtb/rockchip/overlay/rockchip-rk3399-pwm1-by-rockpi-penta.dtbo
armbian_require_overlay rk3399-i2c7 rk3399-pwm1-by-rockpi-penta
cp /usr/bin/rockpi-penta/env/rock_pi_4_armbian.env /etc/rockpi-penta.env
else
mv /boot/dtbo/rk3399-i2c7.dtbo.disabled /boot/dtbo/rk3399-i2c7.dtbo || true
mv /boot/dtbo/rk3399-pwm1.dtbo.disabled /boot/dtbo/rk3399-pwm1.dtbo || true
cp /usr/bin/rockpi-penta/env/rock_pi_4.env /etc/rockpi-penta.env
u-boot-update || true
fi
checkReboot
;;
*)
echo Not support in your board.
exit 2
;;
esac
5 changes: 3 additions & 2 deletions DEBIAN/prerm → rockpi-penta/DEBIAN/prerm
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/sh
set -e
#!/bin/sh -e

rm /etc/rockpi-penta.env > /dev/null || true

systemctl stop rockpi-penta.service
systemctl disable rockpi-penta.service
Expand Down
5 changes: 5 additions & 0 deletions rockpi-penta/DEBIAN/templates
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Template: rockpi-penta/reboot-system
Type: boolean
Default: false
Description: Do you want to reboot the system now?
Installation will take effect after reboot.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lv3 = 50
# switch: fan turn on/off switch
# reboot, poweroff
# If you have any good suggestions for key functions,
# please add an issue on https://setq.me/rockpi-penta
# please add an issue on https://github.com/radxa/rockpi-penta/issues
click = slider
twice = switch
press = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Description=Rockpi SATA Hat

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/rockpi-penta/main.py on
ExecStop=/usr/bin/python3 /usr/bin/rockpi-penta/main.py off
ExecStart=/usr/bin/python3 /usr/bin/rockpi-penta/main.py
KillSignal=SIGINT
EnvironmentFile=/etc/rockpi-penta.env
Restart=on-failure
WorkingDirectory=/usr/bin/rockpi-penta

Expand Down
8 changes: 8 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_3c.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SDA=GPIO1_A0
SCL=GPIO1_A1
OLED_RESET=GPIO3_B1
BUTTON_CHIP=3
BUTTON_LINE=1
FAN_CHIP=3
FAN_LINE=2
HARDWARE_PWM=0
7 changes: 7 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_5a.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SDA=I2C8_SDA_M4
SCL=I2C8_SCL_M4
OLED_RESET=GPIO1_A5
PWMCHIP=14
BUTTON_CHIP=4
BUTTON_LINE=11
HARDWARE_PWM=1
7 changes: 7 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_5a_armbian.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SDA=I2C8_SDA_M4
SCL=I2C8_SCL_M4
OLED_RESET=GPIO1_A5
PWMCHIP=1
BUTTON_CHIP=4
BUTTON_LINE=11
HARDWARE_PWM=1
7 changes: 7 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_pi_3.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SDA=I2C3_SDA_M0
SCL=I2C3_SCL_M0
OLED_RESET=GPIO0_B6
PWMCHIP=15
BUTTON_CHIP=3
BUTTON_LINE=20
HARDWARE_PWM=1
7 changes: 7 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_pi_4.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SDA=I2C7_SDA
SCL=I2C7_SCL
OLED_RESET=GPIO4_D2
PWMCHIP=1
BUTTON_CHIP=4
BUTTON_LINE=18
HARDWARE_PWM=1
7 changes: 7 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rock_pi_4_armbian.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SDA=I2C7_SDA
SCL=I2C7_SCL
OLED_RESET=GPIO4_D2
PWMCHIP=0
BUTTON_CHIP=4
BUTTON_LINE=18
HARDWARE_PWM=1
8 changes: 8 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rpi4.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SDA=SDA
SCL=SCL
OLED_RESET=D23
BUTTON_CHIP=0
BUTTON_LINE=17
FAN_CHIP=0
FAN_LINE=27
HARDWARE_PWM=0
8 changes: 8 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/env/rpi5.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SDA=SDA
SCL=SCL
OLED_RESET=D23
BUTTON_CHIP=4
BUTTON_LINE=17
FAN_CHIP=4
FAN_LINE=27
HARDWARE_PWM=0
108 changes: 108 additions & 0 deletions rockpi-penta/usr/bin/rockpi-penta/fan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python3
import os.path
import time
import traceback
import threading

import gpiod

import misc

pin = None


class Pwm:
def __init__(self, chip):
self.period_value = None
try:
int(chip)
chip = f'pwmchip{chip}'
except ValueError:
pass
self.filepath = f"/sys/class/pwm/{chip}/pwm0/"
try:
with open(f"/sys/class/pwm/{chip}/export", 'w') as f:
f.write('0')
except OSError:
print("Waring: init pwm error")
traceback.print_exc()

def period(self, ns: int):
self.period_value = ns
with open(os.path.join(self.filepath, 'period'), 'w') as f:
f.write(str(ns))

def period_us(self, us: int):
self.period(us * 1000)

def enable(self, t: bool):
with open(os.path.join(self.filepath, 'enable'), 'w') as f:
f.write(f"{int(t)}")

def write(self, duty: float):
assert self.period_value, "The Period is not set."
with open(os.path.join(self.filepath, 'duty_cycle'), 'w') as f:
f.write(f"{int(self.period_value * duty)}")


class Gpio:

def tr(self):
while True:
self.line.set_value(1)
time.sleep(self.value[0])
self.line.set_value(0)
time.sleep(self.value[1])

def __init__(self, period_s):
self.line = gpiod.Chip(os.environ['FAN_CHIP']).get_line(int(os.environ['FAN_LINE']))
self.line.request(consumer='fan', type=gpiod.LINE_REQ_DIR_OUT)
self.value = [period_s / 2, period_s / 2]
self.period_s = period_s
self.thread = threading.Thread(target=self.tr, daemon=True)
self.thread.start()

def write(self, duty):
self.value[1] = duty * self.period_s
self.value[0] = self.period_s - self.value[1]


def read_temp():
with open('/sys/class/thermal/thermal_zone0/temp') as f:
t = int(f.read().strip()) / 1000.0
return t


def get_dc(cache={}):
if misc.conf['run'].value == 0:
return 0.999

if time.time() - cache.get('time', 0) > 60:
cache['time'] = time.time()
cache['dc'] = misc.fan_temp2dc(read_temp())

return cache['dc']


def change_dc(dc, cache={}):
if dc != cache.get('dc'):
cache['dc'] = dc
pin.write(dc)


def running():
global pin
if os.environ['HARDWARE_PWM'] == '1':
chip = os.environ['PWMCHIP']
pin = Pwm(chip)
pin.period_us(40)
pin.enable(True)
else:
pin = Gpio(0.025)
while True:
change_dc(get_dc())
time.sleep(1)


if __name__ == '__main__':
running()
Loading

0 comments on commit 792ab89

Please sign in to comment.