Skip to content

Commit

Permalink
fix percentage calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Aug 25, 2024
1 parent 7c37fe8 commit c0d5a6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
18 changes: 10 additions & 8 deletions internal/light/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const (
)

type Light interface {
SetBrightness(percentage int) error
GetBrightness() (percentage int, err error)
// SetBrightness sets the brightness of this light as a percentage in range [0..1]
SetBrightness(percentage float64) error
// GetBrightness returns the brightness of this light as a percentage in range [0..1]
GetBrightness() (percentage float64, err error)
}

type light struct {
path string
maxBrightness int
maxBrightness float64
}

func NewLight(path string) Light {
Expand All @@ -30,17 +32,17 @@ func NewLight(path string) Light {

return &light{
path: path,
maxBrightness: m,
maxBrightness: float64(m),
}
}

func (f *light) GetBrightness() (percentage int, err error) {
func (f *light) GetBrightness() (percentage float64, err error) {
rawBrightness, err := util.ReadIntFromFile(f.path + string(os.PathSeparator) + Brightness)
mappedToPercentage := int(math.Round(float64(rawBrightness) / float64(f.maxBrightness)))
mappedToPercentage := math.Round(float64(rawBrightness) / float64(f.maxBrightness))
return mappedToPercentage, err
}

func (f *light) SetBrightness(percentage int) (err error) {
mappedToRange := int(math.Round(float64(percentage) * (float64(f.maxBrightness) / 100)))
func (f *light) SetBrightness(percentage float64) (err error) {
mappedToRange := int(math.Round((percentage * 100) * (float64(f.maxBrightness) / 100)))
return util.WriteIntToFile(mappedToRange, f.path+string(os.PathSeparator)+Brightness)
}
21 changes: 13 additions & 8 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ var (
)

type KbdService struct {
initialized bool
idleTimeout time.Duration
light light.Light
targetBrightness int
userIdle bool
userIdleTimer *time.Timer
initialized bool
// idleTimeout specified the time to wait before assuming that the user is idle
idleTimeout time.Duration
// light the light instance to control through this KbdService instance
light light.Light
// targetBrightness the currant target brightness
targetBrightness float64
// userIdle true if the user is currently idling for at least the duration of idleTimeout
userIdle bool
// idleUserTimer timer instance used to toggle userIdle flag
userIdleTimer *time.Timer
}

func NewKbdService(c config.Configuration, l light.Light) *KbdService {
Expand All @@ -51,9 +56,9 @@ func (s *KbdService) Run() {

b, err := s.light.GetBrightness()
if err != nil || b <= 0 {
b = 255
b = 1
}
s.targetBrightness = b * 255
s.targetBrightness = b

ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit c0d5a6a

Please sign in to comment.