mirror of
https://github.com/compute-blade-community/compute-blade-agent.git
synced 2026-04-16 15:35:42 +02:00
* chore: update repository references from uptime-industries to compute-blade-community chore: update repository references from uptime-industries to compute-blade-community for consistency and clarity across all files fix: update links in CHANGELOG.md and README.md to point to the new repository location for accurate documentation fix: update Dockerfile and systemd service file to reflect the new repository URL for proper source tracking refactor: change import paths in Go files to use the new repository name for correct package referencing * chore: Add CODEOWNERS * feat: add auto-labeling --------- Co-authored-by: Cedric Kienzler <cedric@specht-labs.de>
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
package smartfanunit
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/compute-blade-community/compute-blade-agent/pkg/hal/led"
|
|
"github.com/compute-blade-community/compute-blade-agent/pkg/smartfanunit/proto"
|
|
)
|
|
|
|
// Blade -> FanUnit communication
|
|
const (
|
|
// CmdSetFanSpeedPercent sets the fan speed as a percentage, sent from the blade to the fan unit.
|
|
CmdSetFanSpeedPercent proto.Command = 0x01
|
|
|
|
// CmdSetLED represents the command to set the LED color, sent from the blade to the fan unit.
|
|
CmdSetLED proto.Command = 0x02
|
|
)
|
|
|
|
// FanUnit -> Blade, sent in regular intervals
|
|
const (
|
|
// NotifyButtonPress represents a command sent from the fan unit to indicate a button press event.
|
|
NotifyButtonPress proto.Command = 0xa1
|
|
|
|
// NotifyAirFlowTemperature represents a command sent from the fan unit to report the current air flow temperature.
|
|
NotifyAirFlowTemperature proto.Command = 0xa2
|
|
|
|
// NotifyFanSpeedRPM is a command used to report the current fan speed in RPM from the fan unit to the blade.
|
|
NotifyFanSpeedRPM proto.Command = 0xa3
|
|
)
|
|
|
|
var ErrInvalidCommand = errors.New("invalid command")
|
|
|
|
type PacketGenerator interface {
|
|
Packet() proto.Packet
|
|
}
|
|
|
|
// SetFanSpeedPercentPacket is sent from the blade to the fan unit to set the fan speed in percent.
|
|
type SetFanSpeedPercentPacket struct {
|
|
Percent uint8
|
|
}
|
|
|
|
func (p *SetFanSpeedPercentPacket) Packet() proto.Packet {
|
|
return proto.Packet{
|
|
Command: CmdSetFanSpeedPercent,
|
|
Data: proto.Data{p.Percent, 0, 0},
|
|
}
|
|
}
|
|
|
|
func (p *SetFanSpeedPercentPacket) FromPacket(packet proto.Packet) error {
|
|
if packet.Command != CmdSetFanSpeedPercent {
|
|
return ErrInvalidCommand
|
|
}
|
|
p.Percent = packet.Data[0]
|
|
return nil
|
|
}
|
|
|
|
// SetLEDPacket is sent from the blade to the fan unit to set the LED color.
|
|
type SetLEDPacket struct {
|
|
Color led.Color
|
|
}
|
|
|
|
func (p *SetLEDPacket) Packet() proto.Packet {
|
|
return proto.Packet{
|
|
Command: CmdSetLED,
|
|
Data: proto.Data{p.Color.Blue, p.Color.Green, p.Color.Red},
|
|
}
|
|
}
|
|
|
|
func (p *SetLEDPacket) FromPacket(packet proto.Packet) error {
|
|
if packet.Command != CmdSetLED {
|
|
return ErrInvalidCommand
|
|
}
|
|
p.Color = led.Color{
|
|
Blue: packet.Data[0],
|
|
Green: packet.Data[1],
|
|
Red: packet.Data[2],
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ButtonPressPacket is sent from the fan unit to the blade when the button is pressed.
|
|
type ButtonPressPacket struct{}
|
|
|
|
func (p *ButtonPressPacket) Packet() proto.Packet {
|
|
return proto.Packet{
|
|
Command: NotifyButtonPress,
|
|
Data: proto.Data{},
|
|
}
|
|
}
|
|
|
|
func (p *ButtonPressPacket) FromPacket(packet proto.Packet) error {
|
|
if packet.Command != NotifyButtonPress {
|
|
return ErrInvalidCommand
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AirFlowTemperaturePacket is sent from the fan unit to the blade to report the current air flow temperature.
|
|
type AirFlowTemperaturePacket struct {
|
|
Temperature float32
|
|
}
|
|
|
|
func (p *AirFlowTemperaturePacket) Packet() proto.Packet {
|
|
return proto.Packet{
|
|
Command: NotifyAirFlowTemperature,
|
|
Data: float32To24Bit(p.Temperature),
|
|
}
|
|
}
|
|
|
|
func (p *AirFlowTemperaturePacket) FromPacket(packet proto.Packet) error {
|
|
if packet.Command != NotifyAirFlowTemperature {
|
|
return ErrInvalidCommand
|
|
}
|
|
p.Temperature = float32From24Bit(packet.Data)
|
|
return nil
|
|
}
|
|
|
|
// FanSpeedRPMPacket is sent from the fan unit to the blade to report the current fan speed in RPM.
|
|
type FanSpeedRPMPacket struct {
|
|
RPM float32
|
|
}
|
|
|
|
func (p *FanSpeedRPMPacket) Packet() proto.Packet {
|
|
return proto.Packet{
|
|
Command: NotifyFanSpeedRPM,
|
|
Data: float32To24Bit(p.RPM),
|
|
}
|
|
}
|
|
func (p *FanSpeedRPMPacket) FromPacket(packet proto.Packet) error {
|
|
if packet.Command != NotifyFanSpeedRPM {
|
|
return ErrInvalidCommand
|
|
}
|
|
p.RPM = float32From24Bit(packet.Data)
|
|
return nil
|
|
}
|