Skip to content

Commit

Permalink
Merge pull request #163 from markusressel/bugfix/#161_improve_pwm_map…
Browse files Browse the repository at this point in the history
…_computation

improve pwm map calculation
  • Loading branch information
markusressel authored Oct 1, 2022
2 parents 9a07eb8 + da9f514 commit 2516bc0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
11 changes: 1 addition & 10 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,8 @@ func (f *fanController) computePwmMap() {
}

func (f *fanController) updateDistinctPwmValues() {
var keys []int

lastDistinctOutput := -1
for input, output := range f.pwmMap {
if lastDistinctOutput == -1 || lastDistinctOutput != output {
lastDistinctOutput = output
keys = append(keys, input)
}
}
var keys = util.ExtractKeysWithDistinctValues(f.pwmMap)
sort.Ints(keys)

f.pwmValuesWithDistinctTarget = keys

ui.Debug("Distinct PWM value targets of fan %s: %v", f.fan.GetId(), keys)
Expand Down
17 changes: 17 additions & 0 deletions internal/util/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

func ExtractKeysWithDistinctValues(input map[int]int) []int {
var result []int

var keys = SortedKeys(input)

lastDistinctOutput := -1
for _, key := range keys {
value := input[key]
if lastDistinctOutput == -1 || lastDistinctOutput != value {
lastDistinctOutput = value
result = append(result, key)
}
}
return result
}
48 changes: 48 additions & 0 deletions internal/util/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"github.com/stretchr/testify/assert"
"sort"
"testing"
)

func TestExtractKeysWithDistinctValues(t *testing.T) {
// GIVEN
pwmMap := map[int]int{
0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0,
10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0,
21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0,
30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0,
40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0,
50: 0, 51: 0, 52: 0, 53: 0, 54: 0, 55: 0, 56: 0, 57: 0, 58: 0, 59: 0,
60: 0, 61: 0, 62: 0, 63: 0, 64: 128, 65: 128, 66: 128, 67: 128, 68: 128, 69: 128,
70: 128, 71: 128, 72: 128, 73: 128, 74: 128, 75: 128, 76: 128, 77: 128, 78: 128, 79: 128,
80: 128, 81: 128, 82: 128, 83: 128, 84: 128, 85: 128, 86: 128, 87: 128, 88: 128, 89: 128,
90: 128, 91: 128, 92: 128, 93: 128, 94: 128, 95: 128, 96: 128, 97: 128, 98: 128, 99: 128,
100: 128, 101: 128, 102: 128, 103: 128, 104: 128, 105: 128, 106: 128, 107: 128, 108: 128, 109: 128,
110: 128, 111: 128, 112: 128, 113: 128, 114: 128, 115: 128, 116: 128, 117: 128, 118: 128, 119: 128,
120: 128, 121: 128, 122: 128, 123: 128, 124: 128, 125: 128, 126: 128, 127: 128, 128: 128, 129: 128,
130: 128, 131: 128, 132: 128, 133: 128, 134: 128, 135: 128, 136: 128, 137: 128, 138: 128, 139: 128,
140: 128, 141: 128, 142: 128, 143: 128, 144: 128, 145: 128, 146: 128, 147: 128, 148: 128, 149: 128,
150: 128, 151: 128, 152: 128, 153: 128, 154: 128, 155: 128, 156: 128, 157: 128, 158: 128, 159: 128,
160: 128, 161: 128, 162: 128, 163: 128, 164: 128, 165: 128, 166: 128, 167: 128, 168: 128, 169: 128,
170: 128, 171: 128, 172: 128, 173: 128, 174: 128, 175: 128, 176: 128, 177: 128, 178: 128, 179: 128,
180: 128, 181: 128, 182: 128, 183: 128, 184: 128, 185: 128, 186: 128, 187: 128, 188: 128, 189: 128,
190: 128, 191: 128, 192: 128, 193: 128, 194: 128, 195: 128, 196: 128, 197: 128, 198: 128, 199: 128,
200: 128, 201: 128, 202: 128, 203: 128, 204: 128, 205: 128, 206: 128, 207: 128, 208: 128, 209: 128,
210: 128, 211: 128, 212: 128, 213: 128, 214: 128, 215: 128, 216: 128, 217: 128, 218: 128, 219: 128,
220: 128, 221: 128, 222: 128, 223: 128, 224: 128, 225: 128, 226: 128, 227: 128, 228: 128, 229: 128,
230: 128, 231: 128, 232: 128, 233: 128, 234: 128, 235: 128, 236: 128, 237: 128, 238: 128, 239: 128,
240: 128, 241: 128, 242: 128, 243: 128, 244: 0, 245: 0, 246: 0, 247: 0, 248: 0, 249: 0, 250: 0, 251: 0,
252: 0, 253: 0, 254: 0, 255: 0,
}

// WHEN
result := ExtractKeysWithDistinctValues(pwmMap)
sort.Ints(result)

// THEN
assert.Equal(t, []int{
0, 64, 244,
}, result)
}
17 changes: 13 additions & 4 deletions internal/util/slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "sort"
import (
"golang.org/x/exp/constraints"
"sort"
)

func ContainsString(s []string, e string) bool {
for _, a := range s {
Expand Down Expand Up @@ -43,11 +46,17 @@ func Max(s []float64) float64 {
return result
}

func SortedKeys(input map[int]float64) []int {
result := make([]int, 0, len(input))
func sortSlice[T constraints.Ordered](s []T) {
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
}

func SortedKeys[T constraints.Ordered, K any](input map[T]K) []T {
result := make([]T, 0, len(input))
for k, _ := range input {
result = append(result, k)
}
sort.Ints(result)
sortSlice(result)
return result
}

0 comments on commit 2516bc0

Please sign in to comment.