Skip to content

Commit

Permalink
Merge pull request #174 from DeskPi-Team/feature/bookworm
Browse files Browse the repository at this point in the history
Update installation script for bookworm 64bit
  • Loading branch information
yoyojacky authored Jun 25, 2024
2 parents 131e997 + 2dee8a6 commit 919e8f9
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 0 deletions.
9 changes: 9 additions & 0 deletions installation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Installation script for Raspberry Pi OS 64bit bookworm
## How to install driver by using shell script
```bash
cd ~
git clone -b feature/bookworm https://github.com/deskpi-team/deskpi
cd deskpi/installation/
sudo ./install.sh
```

103 changes: 103 additions & 0 deletions installation/deskpi-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash
# This is a fan speed control utility tool for user to customize fan speed.
# Priciple: send speed argument to the MCU
# Technical Part
# There are four arguments:
# pwm_025 means sending 25% PWM signal to MCU. The fan will run at 25% speed level.
# pwm_050 means sending 50% PWM signal to MCU. The fan will run at 50% speed level.
# pwm_075 means sending 75% PWM signal to MCU. The fan will run at 75% speed level.
# pwm_100 means sending 100% PWM signal to MCU.The fan will run at 100% speed level.
#
. /lib/lsb/init-functions
# This is the serial port that connect to deskPi mainboard and it will
# communicate with Raspberry Pi and get the signal for fan speed adjusting.
serial_port='/dev/ttyUSB0'

# Stop deskpi.service so that user can define the speed level.
sudo systemctl stop deskpi.service
sudo systemctl daemon-reload

# Define the function of set_config
function set_config() {
if [ -e /etc/deskpi.conf ]; then
sudo sh -c "rm -f /etc/deskpi.conf"
fi
sudo sh -c "sudo touch /etc/deskpi.conf"
sudo sh -c "sudo chmod 777 /etc/deskpi.conf"
echo "Under normal circumstances, we recommend four gears. The
following requires you to control the fan's operating status according to
the temperature and speed defined by yourself, and you need to input 4
different temperature thresholds (for example: 42, 50, 60, 70) , And 4 PWM
values of different speeds parameters(for example 25, 50, 75, 100, this is the default
value),you can define the speed level during 0-100."
for i in `seq 1 4`;
do
echo -e "\e[32;40mCurrent CPU Temperature:\e[0m \e[31;40m`vcgencmd measure_temp`\e[0m\n"
read -p "Temperature_threshold_$i:" temp
read -p "Fan_Speed level_$i:" fan_speed_level
sudo sh -c "echo $temp" >> /etc/deskpi.conf
sudo sh -c "echo $fan_speed_level" >> /etc/deskpi.conf
done
echo "Configuration file has been created on /etc/deskpi.conf"
}

# Greetings and information for user.
#
echo "DESKPI PRO FAN CONTROL" | figlet -c
echo "Please select speed level that you want: "
echo "It will always run at the speed level that you choosed."
echo "---------------------------------------------------------------"
echo "1 - set fan speed level to 25%"
echo "2 - set fan speed level to 50%"
echo "3 - set fan speed level to 75%"
echo "4 - set fan speed level to 100%"
echo "5 - Turn off Fan"
echo "6 - Adjust the start speed level according to the temperature"
echo "7 - Cancel manual control and enable automatical fan control"
echo "---------------------------------------------------------------"
echo "Just input the number and press enter."
read -p "Your choice:" levelNumber
case $levelNumber in
1)
echo "You've select 25% speed level"
sudo sh -c "echo pwm_025 > $serial_port"
echo "Fan speed level has been change to 25%"
;;
2)
echo "You've select 50% speed level"
sudo sh -c "echo pwm_050 > $serial_port"
echo "Fan speed level has been change to 50%"
;;
3)
echo "You've select 75% speed level"
sudo sh -c "echo pwm_075 > $serial_port"
echo "Fan speed level has been change to 75%"
;;
4)
echo "You'tve select 100% speed level"
sudo sh -c "echo pwm_100 > $serial_port"
echo "Fan speed level has been change to 100%"
;;
5)
echo "Turn off fan"
sudo sh -c "echo pwm_000 > $serial_port"
echo "Fan speed level has been turned off."
;;
6)
echo "Customizing the start speed level according the temperature"
sudo systemctl stop deskpi.service
sudo systemctl daemon-reload
set_config
sudo systemctl daemon-reload
sudo systemctl start deskpi.service
;;
7)
echo "Cancel manual control and enable automatical fan control"
sudo systemctl daemon-reload
sudo systemctl restart deskpi.service
;;
*)
echo "You type the wrong selection, please try again!"
. /usr/bin/deskpi-config
;;
esac
39 changes: 39 additions & 0 deletions installation/drivers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Control Your Fan througn PWM signal via Serial port(OTG)
## Configure /boot/config.txt to enbale otg function.
```bash
sudo vim.tiny /boot/firmware/config.txt
```
add:
```bash
dtoverlay=dwc2,dr_mode=host
```
save it and reboot Raspberry Pi.
## C Language
* 1. At First, get the demo code from github.
```bash
cd ~
git clone -b feature/bookworm https://github.com/DeskPi-Team/deskpi.git
cd ~/deskpi/installation/drivers/c/
```
* 2. How to compile it.
```bash
make
```
* 3. How to run it.
```bash
sudo ./pwmControlFan64
```
* 4. How to stop it.
Press "Ctrl + C" on keyboard.
* 5. How to clean the source code directory.
```bash
make clean
```
## How to change speed of the fan.
This program is send the pwm signal from Raspberry Pi to the extension board via OTG serial port, which will be recognized by your Raspberry Pi as "/dev/DeskPi_FAN" device which is symboliclink to /dev/ttyUSB0. so if you want to control the fan as your wish, you can modify pwmControlFan.c code and recompile it.
* In the default code, we have set 4 level for you Fan on pwm signal:
* Level 0: 0% speed-> send "pwm_000" to /dev/DeskPi_FAN", means to turn off the fan
* Level 1: 25% speed-> send "pwm_025" to /dev/DeskPi_FAN", means to set fan speed to 25%
* Level 2: 50% speed-> send "pwm_050" to /dev/DeskPi_FAN", means to set fan speed to 50%
* Level 3: 75% speed-> send "pwm_075" to /dev/DeskPi_FAN", means to set fan speed to 75%
* Level 4:100% speed-> send "pwm_100" to /dev/DeskPi_FAN", means to set fan speed to 100%
16 changes: 16 additions & 0 deletions installation/drivers/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DEFAULT: all
.PHONY: install

all: pwmControlFan64 safeCutOffPower64

install:
sudo sh -c "sudo chmod 755 pwmControlFan64 safeCutOffPower64"
sudo sh -c "sudo cp pwmControlFan64 safeCutOffPower64 /usr/bin/"
sudo sh -c "sudo chcon -u system_u -t bin_t /usr/bin/pwmControlFan64 /usr/bin/safeCutOffPower64"

pwmControlFan64: pwmControlFan.c
gcc -o pwmControlFan64 pwmControlFan.c
safeCutOffPower64: safeCutOffPower.c
gcc -o safeCutOffPower64 safeCutOffPower.c
clean:
rm -rf safeCutOffPower64 pwmControlFan64
205 changes: 205 additions & 0 deletions installation/drivers/c/pwmControlFan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

//#define TEST_MODE
#define PERCENTAGE 10 // switch over percentage range


static int serial_port=0;
/* initialized the serial port*/
int init_serial( char *serial_name)
{
serial_port = open(serial_name, O_RDWR);

if (serial_port < 0){
printf("Can not open /dev/DeskPi_FAN serial port ErrorCode: %s\n", strerror(errno));
printf("Please check udev file in /etc/udev/rules.d/10-deskpi.rules");
printf("Please check the /boot/firmware/config.txt file and add dtoverlay=dwc2, dr_mode=host and reboot RPi \n");
}

struct termios tty;

if(tcgetattr(serial_port, &tty) !=0){
printf("Please check serial port over OTG\n");
}

tty.c_cflag &= ~PARENB;
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag |= CSTOPB;

tty.c_cflag |= CS5;
tty.c_cflag |= CS6;
tty.c_cflag |= CS7;
tty.c_cflag |= CS8;

tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CRTSCTS;

tty.c_cflag |= CREAD | CLOCAL;

tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO;
tty.c_lflag &= ~ECHOE;
tty.c_lflag &= ~ECHONL;
tty.c_lflag &= ~ISIG;

tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);

tty.c_oflag &= ~OPOST;
tty.c_oflag &= ~ONLCR;

tty.c_cc[VTIME] = 10;
tty.c_cc[VMIN] = 0;

cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);

if (tcsetattr(serial_port, TCSANOW, &tty) !=0){
printf("---Serial Port Can not detected---\n");
}
return 0;
}

/* send data to serial port function*/
int send_serial(char *data,size_t data_len)
{
return write(serial_port, data, data_len);
}

/* close the serial port function*/
int __init_serial()
{
return close(serial_port);
}
/* read cpu temperature function */
unsigned int read_cpu_tmp()
{
FILE *fp=NULL;
unsigned int cpu_temp=0;
char buff[255];
bzero(buff, sizeof(buff));
fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if( NULL != fp)
{
if (fgets(buff, 255, (FILE*)fp) != NULL){
cpu_temp = atoi(buff) /1000 ;
}
fclose(fp);
}
return cpu_temp;
}

/* main loop */
int main(void){
int i=0;
FILE *fp=NULL;
char buffer[100];
bzero(buffer, sizeof(buffer));

char data[8]={0};
unsigned int conf_info[8];
unsigned int cpu_temp=0;
unsigned int last_cpu_temp = 0; // last temperature level recorded
unsigned int min_temp = 0; // calculated minimum temperature based on percentage
init_serial("/dev/DeskPi_FAN");
/* default configuration if /etc/deskpi.conf dose not exist */
conf_info[0]=40;
conf_info[1]=25;

conf_info[2]=50;
conf_info[3]=50;

conf_info[4]=65;
conf_info[5]=75;

conf_info[6]=75;
conf_info[7]=100;

while(1)
{
fp = fopen("/etc/deskpi.conf", "r");
if(fp != NULL)
{
for(i=0;i<8;i++)
{
bzero(buffer, sizeof(buffer));
if (fgets(buffer, 100, fp) != NULL)
{
conf_info[i] = atoi(buffer);
}
}
fclose(fp);
}

/* Testing section
for(i=0;i<8;i++)
{
printf("temp:%d\n",conf_info[i]);
i++;
printf("pwm:%d\n",conf_info[i]);
}
*/
cpu_temp = read_cpu_tmp();
#ifdef TEST_MODE
printf("cpu_temp:%d / last_cpu_temp:%d / fan_speed:", cpu_temp, last_cpu_temp);
#endif

if (last_cpu_temp == 0 || cpu_temp > last_cpu_temp) {
if(cpu_temp < conf_info[0])
{
memcpy( (char *) &data, "pwm_000", sizeof("pwm_000"));
last_cpu_temp = 0;
}
else if(cpu_temp >= conf_info[0] && cpu_temp < conf_info[2])
{
sprintf((char *)&data, "pwm_%03d", conf_info[1]);
last_cpu_temp = conf_info[0];
}
else if(cpu_temp >= conf_info[2] && cpu_temp < conf_info[4])
{
sprintf((char *)&data, "pwm_%03d", conf_info[3]);
last_cpu_temp = conf_info[2];
}
else if(cpu_temp >= conf_info[4] && cpu_temp < conf_info[6])
{
sprintf((char *)&data, "pwm_%03d", conf_info[5]);
last_cpu_temp = conf_info[4];
}
else if(cpu_temp >= conf_info[6])
{
sprintf((char *)&data, "pwm_%03d", conf_info[7]);
last_cpu_temp = conf_info[6];
}
send_serial((char *) &data, sizeof(data));
#ifdef TEST_MODE
printf("*");
#endif
}

// leave fan speed as is if not within pre defined percentage
min_temp = last_cpu_temp - round(last_cpu_temp * PERCENTAGE / 100);
if (cpu_temp < min_temp) {
last_cpu_temp = 0; // reset level
}

#ifdef TEST_MODE
printf("%s", data);
printf(" until min_temp:%d", min_temp);
printf("\n");
#endif

sleep(1);
}
__init_serial();
return 0;
}
Binary file added installation/drivers/c/pwmControlFan64
Binary file not shown.
Loading

0 comments on commit 919e8f9

Please sign in to comment.