pwmfan

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2018 License: MIT Imports: 8 Imported by: 1

README

pwmfan-go

A Pulse Width Modulation Fan For RaspberryPi written in Go
Fan speed is adjust by CPU temperature, to reduce fan noise and power consumption
Go Report Card GoDoc

Warning:

This program still in VERY EARLY development, function may missing or not work properly, use with caution.
This library still in VERY EARLY development, APIs may change, use with caution.

Pros
  1. Easy to use, just a binary file and a simple configuration file
  2. Easy to compile and/or cross compile
  3. Easy development
Cons
  1. (Compare to pure C/C++ program,) A slightly big binary file size
  2. (Compare to pure C/C++ program,) A slightly big memory consumption

Hardware Requirement

  1. A Raspberry Pi2 or Raspberry Pi3 serial
  2. A fan that support PWM signal, or you can build one by add a transistor to you origianl fan
Remember:

Raspberry Pi support max +5V for power pin, and use +3.3V for other gpio pin, double check before connect anything to you pi

Usage

Because this program uses pwm to control fan, it only works with root privillage

Install from binary release:

Download release package from Releases to your Pi, then modify configuration file and run.

Install from source:

On your Pi

go get -v -u -d github.com/eZioPan/pwmfan-go
cd $/GOPATH/src/github.com/eZioPan/pwmfan-go/_example
GOOS=linux GOARCH=arm go build -o PWMFan

Then modify configuration file and run.

Confugration File

By default, the program use JSON style "config.json" file in the same directory of binary file, you can change it using -conifg command line parameter.
Here is an example of configuration file:

{
	"Pin":18,
	"CPUTempPath":"/sys/class/thermal/thermal_zone0/temp",
	"StartCount":5,
	"StopCount":5,
	"StartTemp":33,
	"LowTemp":30,
	"HighTemp":40,
	"PwmFreq":600,
	"StartCycle":50,
	"LowCycle":80,
	"HighCycle":100,
	"FullCycle":100
}

Pin defines the gpio pin number of PWM. This uses BCM Pinout number, you can check here.
CPUTempPath defines the system file path to read cpu's temperature.
StartCount after counter reaches this count fan will start.
StopCount after counter reaches this count fan will stop.
StartTemp when temperature higher than this value, fan will start.
LowTemp when temperature lower than this value, fan will stop.
HighTemp when temperature higher than this value, fan will run in HighCycle defined speed.
PwmFreq PWM signal frequence, should be set high enough for PWM signal.
StartCycle when fan start from full stop, it needs a little higher speed to kick it start.
LowCycle is the lowest speed fan will run, before fan fully stopped.
HighCycle is the highest speed fan will run. This value shoudn't greater than FullCycle.
FullCycle is maximum speed which fan is able to run at.

For Developers

go get -v -u -d github.com/eZioPan/pwmfan-go
import "github.com/eZioPan/pwmfan-go"

Dependencices

stianeikeland/go-rpio for accessing RasberryPi gpio in pure go

TODO

  • run as a Systemd service
  • Write fan log to system log
  • Network function to read fan state
  • Network function to change fan parameter

Changelog

  • v0.1.0
    Reconstruct code
    Support StartCount and StopCount parameter

  • v0.0.1
    Init Release
    Basic function

Documentation

Overview

Package pwmfan is a set of toolkits to build a pwm fan control program

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LinearClampRemap

func LinearClampRemap(input float64, opt ...float64) (output float64)

LinearClampRemap is a linear remap function that will clamp output in between opt[2] and opt[3]

func LinearRemap

func LinearRemap(input float64, opt ...float64) (output float64)

LinearRemap is a linear remap function

func PullCPUTemp

func PullCPUTemp(CPUTempPath string, Divider float64, Temp chan<- float64)

PullCPUTemp read from a system file constantanious and put temperature into a predifined channel

Note: As for raspberry Pi, CPUTempPath is /sys/class/thermal/thermal_zone0/temp Divider should be set to 1000

func ReadCPUTemperature

func ReadCPUTemperature(CPUTempPath string, Divider float64) (Temperature float64)

ReadCPUTemperature read temprature once from a file and divie raw data by a divider

Note: As for raspberry Pi, CPUTempPath is /sys/class/thermal/thermal_zone0/temp Divider should be set to 1000

func SignalProcess

func SignalProcess(Process *os.Process, SigChan <-chan os.Signal, Action func())

SignalProcess listen from a os.Notify function's output, when catch signal, Process the Action, then send original to the Process

func TriangularWave

func TriangularWave(small float64, big float64, step float64, ch chan<- float64)

TriangularWave is a linear remap will read input and generate a oscillation wave

Types

type Config

type Config struct {
	Pin         uint
	CPUTempPath string
	StartCount  uint
	StopCount   uint
	StartTemp   float64
	LowTemp     float64
	HighTemp    float64
	PwmFreq     uint
	StopCycle   uint
	StartCycle  uint
	LowCycle    uint
	HighCycle   uint
	FullCycle   uint
	SampleRate  uint
}

Config contain all information that define a PWM fan's user configuration

func ParseJSON

func ParseJSON(cfgFilePath string) (cfg Config)

ParseJSON parse json file into a PwmFanCfg structure

type Fan

type Fan struct {
	State        FanState
	Cycle        uint
	Temp         float64
	Cfg          Config
	Pin          rpio.Pin
	StopCounter  uint
	StartCounter uint
}

Fan represent a Fan

State, Cycle, Temp should be updated 'in realtime' Pin better not be modified after call NewFan()

func NewFan

func NewFan(cfg Config) (fan *Fan)

NewFan initialize a Fan object

func (Fan) GetCfg

func (fan Fan) GetCfg() (cfg Config)

GetCfg get a fan's configuration

func (Fan) GetCycle

func (fan Fan) GetCycle() (cycle uint)

GetCycle get fan's current pwm duty cycle information

func (Fan) GetStartCounter

func (fan Fan) GetStartCounter() (num uint)

GetStartCounter get fan's current start counter

func (Fan) GetState

func (fan Fan) GetState() (state FanState)

GetState get a fan's current running state

func (Fan) GetStopCounter

func (fan Fan) GetStopCounter() (num uint)

GetStopCounter get fan's current stop counter

func (Fan) GetTemp

func (fan Fan) GetTemp() (temp float64)

GetTemp get fan's current loaded temperature information

func (*Fan) Monitor

func (fan *Fan) Monitor()

Monitor is in todo list

func (*Fan) SetCfg

func (fan *Fan) SetCfg(cfg Config)

SetCfg set a fan's configuration

func (*Fan) SetCycle

func (fan *Fan) SetCycle(cycle uint)

SetCycle set fan's current pwm duty cycle information

func (*Fan) SetStartCounter

func (fan *Fan) SetStartCounter(num uint)

SetStartCounter set fan's current start counter

func (*Fan) SetState

func (fan *Fan) SetState(state FanState)

SetState set a fan's current running state

func (*Fan) SetStopCounter

func (fan *Fan) SetStopCounter(num uint)

SetStopCounter set fan's current stop counter

func (*Fan) SetTemp

func (fan *Fan) SetTemp(temp float64)

SetTemp set fan's current loaded temperature information

func (*Fan) UpdateCycleFromState

func (fan *Fan) UpdateCycleFromState(remapper RemapFunc)

UpdateCycleFromState update pwm fan's Cycle information from State information

type FanState

type FanState uint

FanState represent a PWM Fan's running state

const (
	// Stop represent Pwm Fan stay in a full stop state
	Stop FanState = iota
	// Start represent Pwm Fan enter in s Start state
	Start
	// Run represent Pwm Fan stay in a running state
	Run
)

type RemapFunc

type RemapFunc func(float64, ...float64) float64

RemapFunc is a function that read at least one float64 input and output a float64 data

More specific, this function should take 1st argument as an input data, then read more input, and output an data Example: LinearRemap and LinearClampRemap

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL