Skip to content

Commit

Permalink
Add some simple graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
DDRBoxman committed Sep 4, 2017
1 parent d84283a commit c43ebc4
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 4 deletions.
89 changes: 89 additions & 0 deletions floatbuffer/floatbuffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package floatbuffer

// Updated to use floats from https://github.com/armon/circbuf

import (
"fmt"
)

// Buffer implements a circular buffer. It is a fixed size,
// and new writes overwrite older data, such that for a buffer
// of size N, for any amount of writes, only the last N floats
// are retained.
type Buffer struct {
data []float32
size int64
writeCursor int64
written int64
}

// NewBuffer creates a new buffer of a given size. The size
// must be greater than 0.
func NewBuffer(size int64) (*Buffer, error) {
if size <= 0 {
return nil, fmt.Errorf("Size must be positive")
}

b := &Buffer{
size: size,
data: make([]float32, size),
}
return b, nil
}

// Write writes up to len(buf) floats to the internal ring,
// overriding older data if necessary.
func (b *Buffer) Write(buf ...float32) (int, error) {
// Account for total floats written
n := len(buf)
b.written += int64(n)

// If the buffer is larger than ours, then we only care
// about the last size floats anyways
if int64(n) > b.size {
buf = buf[int64(n)-b.size:]
}

// Copy in place
remain := b.size - b.writeCursor
copy(b.data[b.writeCursor:], buf)
if int64(len(buf)) > remain {
copy(b.data, buf[remain:])
}

// Update location of the cursor
b.writeCursor = ((b.writeCursor + int64(len(buf))) % b.size)
return n, nil
}

// Size returns the size of the buffer
func (b *Buffer) Size() int64 {
return b.size
}

// TotalWritten provides the total number of floats written
func (b *Buffer) TotalWritten() int64 {
return b.written
}

// Floats provides a slice of the floats written. This
// slice should not be written to.
func (b *Buffer) Floats() []float32 {
switch {
case b.written >= b.size && b.writeCursor == 0:
return b.data
case b.written > b.size:
out := make([]float32, b.size)
copy(out, b.data[b.writeCursor:])
copy(out[b.size-b.writeCursor:], b.data[:b.writeCursor])
return out
default:
return b.data[:b.writeCursor]
}
}

// Reset resets the buffer so it has no content.
func (b *Buffer) Reset() {
b.writeCursor = 0
b.written = 0
}
56 changes: 52 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/karalabe/hid"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"

"github.com/DDRBoxman/deckstats/floatbuffer"
)

var NUM_KEYS = 15
Expand All @@ -27,6 +29,9 @@ var brightness = []byte{0x05, 0x55, 0xAA, 0xD1, 0x01, 0, 0x00,

var streamDeck *hid.Device

var cpuTemps *floatbuffer.Buffer
var gpuTemps *floatbuffer.Buffer

type OHMSensor struct {
Name string
Value float32
Expand All @@ -35,9 +40,19 @@ type OHMSensor struct {
func main() {
draw2d.SetFontFolder("./fonts")

var err error
cpuTemps, err = floatbuffer.NewBuffer(288) // store 4 seconds per pixel (almost 5 minutes)
if err != nil {
log.Fatal(err)
}

gpuTemps, err = floatbuffer.NewBuffer(288) // store 4 seconds per pixel (almost 5 minutes)
if err != nil {
log.Fatal(err)
}

devices := hid.Enumerate(4057, 96)

var err error
streamDeck, err = devices[0].Open()
if err != nil {
log.Fatal(err)
Expand All @@ -49,7 +64,7 @@ func main() {
go readLoop(streamDeck)

for {
time.Sleep(1000)
time.Sleep(2000)

var dst []OHMSensor
q := `Select * from Sensor Where (Parent LIKE "/intelcpu/[0-9]" OR Parent LIKE "/amdcpu/[0-9]") AND SensorType = "Temperature"`
Expand All @@ -60,18 +75,51 @@ func main() {

drawTempToKey("CPU", dst[4].Value, 4)

cpuTemps.Write(dst[4].Value)

drawTempGraphToKey(cpuTemps, 9)

q = `Select * from Sensor Where (Parent LIKE "/nvidiagpu/[0-9]" OR Parent LIKE "/atigpu/[0-9]") AND SensorType = "Temperature"`
err = wmi.Query(q, &dst, ".", "root\\OpenHardwareMonitor")
if err != nil {
log.Fatal(err)
}

drawTempToKey("GPU", dst[0].Value, 3)

gpuTemps.Write(dst[0].Value)

drawTempGraphToKey(gpuTemps, 8)
}

wg.Wait()
}

func drawTempGraphToKey(tempBuffer *floatbuffer.Buffer, key int) {
dest := image.NewRGBA(image.Rect(0, 0, ICON_SIZE, ICON_SIZE))
gc := draw2dimg.NewGraphicContext(dest)

gc.SetStrokeColor(color.RGBA{0xff, 0x00, 0x00, 0xff})
gc.SetLineWidth(1)

temps := tempBuffer.Floats()

for i := 0; i<len(temps); i+=4 {
reading := temps[i:i+4]

var total float32 = 0
for _, value:= range reading {
total += value
}

gc.MoveTo(float64(i / 4), float64(ICON_SIZE))
gc.LineTo(float64(i / 4), float64(ICON_SIZE) - float64(total / float32(4)))
gc.Stroke()
}

writeImageToKey(dest, key)
}

func drawTempToKey(label string, value float32, key int) {
dest := image.NewRGBA(image.Rect(0, 0, ICON_SIZE, ICON_SIZE))
gc := draw2dimg.NewGraphicContext(dest)
Expand All @@ -84,9 +132,9 @@ func drawTempToKey(label string, value float32, key int) {
Name: "Roboto",
})

gc.FillStringAt(fmt.Sprintf("%.0f°", value), 10, 32+20)
gc.FillStringAt(fmt.Sprintf("%.0f°", value), 10, 32+12)

gc.SetFontSize(8)
gc.SetFontSize(14)
gc.FillStringAt(label, 10, 72-8)

writeImageToKey(dest, key)
Expand Down

0 comments on commit c43ebc4

Please sign in to comment.