Skip to content

Commit

Permalink
bulk:
Browse files Browse the repository at this point in the history
- cleanup of device init code
- cleanup of web templates
- decouple rgb from single file to per device
- minor fixups and typos
  • Loading branch information
jurkovic-nikola committed Dec 1, 2024
1 parent 8ff0c20 commit 3e9a8d4
Show file tree
Hide file tree
Showing 66 changed files with 5,030 additions and 4,445 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Open source Linux interface for iCUE LINK Hub and other Corsair AIOs, Hubs.
| K65 PLUS | `1b1c` | `2b10`<br />`2b07` | USB<br />Wireless |
| K100 AIR RGB | `1b1c` | `1bab`<br />`1bdc` | USB<br />Wireless |
| KATAR PRO | `1b1c` | `1b93` | DPI Control<br />RGB Control |
| IRONCLAW RGB | `1b1c` | `1b5d` | DPI Control<br />RGB Control |
| ST100 RGB | `1b1c` | `0a34` | RGB |
| MM700 RGB | `1b1c` | `1b9b` | RGB |
| LT100 Smart Lighting Tower | `1b1c` | `0c23` | RGB |
Expand Down
1 change: 1 addition & 0 deletions database/rgb.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"keyboard": {},
"stand": {},
"mousepad": {},
"mouse": {},
"rainbow": {
"speed": 4,
"brightness": 1
Expand Down
15 changes: 15 additions & 0 deletions src/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
)

// FileExists will check if given filename exists
Expand Down Expand Up @@ -142,3 +144,16 @@ func FromLinear11(bytes []byte) float32 {
}
return float32(fraction) * float32(math.Pow(2, float64(exp)))
}

// GetTime will return current time as string
func GetTime() string {
t := time.Now()
hour, minute, second := t.Clock()
return itoaTwoDigits(hour) + ":" + itoaTwoDigits(minute) + ":" + itoaTwoDigits(second)
}

// itoaTwoDigits time.Clock returns one digit on values, so we make sure to convert to two digits
func itoaTwoDigits(i int) string {
b := "0" + strconv.Itoa(i)
return b[len(b)-2:]
}
114 changes: 107 additions & 7 deletions src/devices/cc/cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ type Device struct {
GpuTemp float32
FreeLedPorts map[int]string
FreeLedPortLEDs map[int]string
Rgb *rgb.RGB
}

/*
Expand Down Expand Up @@ -369,6 +370,7 @@ func Init(vendorId, productId uint16, serial string) *Device {
d.getManufacturer() // Manufacturer
d.getProduct() // Product
d.getSerial() // Serial
d.loadRgb() // Load RGB
d.loadDeviceProfiles() // Load all device profiles
d.getDeviceLcd() // Check if LCD pump cover is installed
d.getDeviceProfile() // Get device profile if any
Expand Down Expand Up @@ -442,6 +444,81 @@ func (d *Device) Stop() {
}
}

// loadRgb will load RGB file if found, or create the default.
func (d *Device) loadRgb() {
rgbDirectory := pwd + "/database/rgb/"
rgbFilename := rgbDirectory + d.Serial + ".json"

// Check if filename has .json extension
if !common.IsValidExtension(rgbFilename, ".json") {
return
}

if !common.FileExists(rgbFilename) {
profile := rgb.GetRGB()
profile.Device = d.Product

// Convert to JSON
buffer, err := json.MarshalIndent(profile, "", " ")
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to encode RGB json")
return
}

// Create profile filename
file, err := os.Create(rgbFilename)
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to create RGB json file")
return
}

// Write JSON buffer to file
_, err = file.Write(buffer)
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to write to RGB json file")
return
}

// Close file
err = file.Close()
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to close RGB json file")
return
}
}

file, err := os.Open(rgbFilename)
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to load RGB")
return
}
if err = json.NewDecoder(file).Decode(&d.Rgb); err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": rgbFilename}).Warn("Unable to decode profile")
return
}
err = file.Close()
if err != nil {
logger.Log(logger.Fields{"location": rgbFilename, "serial": d.Serial}).Warn("Failed to close file handle")
}
}

// GetRgbProfile will return rgb.Profile struct
func (d *Device) GetRgbProfile(profile string) *rgb.Profile {
if d.Rgb == nil {
return nil
}

if val, ok := d.Rgb.Profiles[profile]; ok {
return &val
}
return nil
}

// GetDeviceTemplate will return device template name
func (d *Device) GetDeviceTemplate() string {
return d.Template
}

// loadDeviceProfiles will load custom user profiles
func (d *Device) loadDeviceProfiles() {
profileList := make(map[string]*DeviceProfile, 0)
Expand Down Expand Up @@ -471,6 +548,17 @@ func (d *Device) loadDeviceProfiles() {
continue
}

fileSerial := ""
if strings.Contains(fileName, "-") {
fileSerial = strings.Split(fileName, "-")[0]
} else {
fileSerial = fileName
}

if fileSerial != d.Serial {
continue
}

file, err := os.Open(profileLocation)
if err != nil {
logger.Log(logger.Fields{"error": err, "serial": d.Serial, "location": profileLocation}).Warn("Unable to load profile")
Expand Down Expand Up @@ -733,7 +821,7 @@ func (d *Device) setDeviceColor() {
}
if s > 0 || l > 0 { // We have some values
if s == l { // number of devices matches number of devices with static profile
profile := rgb.GetRgbProfile("static")
profile := d.GetRgbProfile("static")
if d.DeviceProfile.Brightness != 0 {
profile.StartColor.Brightness = rgb.GetBrightnessValue(d.DeviceProfile.Brightness)
}
Expand Down Expand Up @@ -797,7 +885,7 @@ func (d *Device) setDeviceColor() {
}

rgbCustomColor := true
profile := rgb.GetRgbProfile(d.RgbDevices[k].RGB)
profile := d.GetRgbProfile(d.RgbDevices[k].RGB)
if profile == nil {
for i := 0; i < int(d.RgbDevices[k].LedChannels); i++ {
buff = append(buff, []byte{0, 0, 0}...)
Expand Down Expand Up @@ -1058,7 +1146,7 @@ func (d *Device) getRgbDevices() {
// Profile is set
if rp, ok := d.DeviceProfile.RGBProfiles[i]; ok {
// Profile device channel exists
if rgb.GetRgbProfile(rp) != nil { // Speed profile exists in configuration
if d.GetRgbProfile(rp) != nil { // Speed profile exists in configuration
// Speed profile exists in configuration
rgbProfile = rp
} else {
Expand Down Expand Up @@ -1112,7 +1200,7 @@ func (d *Device) getRgbDevices() {
// Profile is set
if rp, ok := d.DeviceProfile.RGBProfiles[i]; ok {
// Profile device channel exists
if rgb.GetRgbProfile(rp) != nil { // Speed profile exists in configuration
if d.GetRgbProfile(rp) != nil { // Speed profile exists in configuration
// Speed profile exists in configuration
rgbProfile = rp
} else {
Expand Down Expand Up @@ -1695,7 +1783,7 @@ func (d *Device) UpdateRGBDeviceLabel(channelId int, label string) uint8 {
}

// UpdateDeviceLcd will update device LCD
func (d *Device) UpdateDeviceLcd(mode uint8) uint8 {
func (d *Device) UpdateDeviceLcd(_, mode uint8) uint8 {
mutex.Lock()
defer mutex.Unlock()

Expand All @@ -1709,7 +1797,7 @@ func (d *Device) UpdateDeviceLcd(mode uint8) uint8 {
}

// UpdateDeviceLcdRotation will update device LCD rotation
func (d *Device) UpdateDeviceLcdRotation(rotation uint8) uint8 {
func (d *Device) UpdateDeviceLcdRotation(_, rotation uint8) uint8 {
mutex.Lock()
defer mutex.Unlock()

Expand Down Expand Up @@ -1897,7 +1985,7 @@ func (d *Device) getTemperatureProbe() {

// UpdateRgbProfile will update device RGB profile
func (d *Device) UpdateRgbProfile(channelId int, profile string) uint8 {
if rgb.GetRgbProfile(profile) == nil {
if d.GetRgbProfile(profile) == nil {
logger.Log(logger.Fields{"serial": d.Serial, "profile": profile}).Warn("Non-existing RGB profile")
return 0
}
Expand Down Expand Up @@ -2491,6 +2579,18 @@ func (d *Device) setupLCD() {
)
d.transferToLcd(buffer)
}
case lcd.DisplayTime:
{
buffer := lcd.GenerateScreenImage(
lcd.DisplayTime,
0,
0,
0,
0,
d.getLCDRotation(),
)
d.transferToLcd(buffer)
}
}
case <-lcdRefreshChan:
lcdTimer.Stop()
Expand Down
Loading

0 comments on commit 3e9a8d4

Please sign in to comment.