From f026295368b30b8ccfac73177b415e9ab81f78a2 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Wed, 1 Feb 2017 22:59:25 +0000
Subject: [PATCH] Work on GetConfiguration.

---
 cmd/gqgmc/main.go        |  5 ++-
 devices/geiger/geiger.go |  4 +-
 devices/geiger/gqgmc.go  | 89 ++++++++++++++++++++++++++++++++++------
 3 files changed, 82 insertions(+), 16 deletions(-)

diff --git a/cmd/gqgmc/main.go b/cmd/gqgmc/main.go
index e22f9eb..4aa35ab 100644
--- a/cmd/gqgmc/main.go
+++ b/cmd/gqgmc/main.go
@@ -17,6 +17,7 @@ import (
 func main() {
 	var (
 		gc       geiger.Counter
+		cfg      *geiger.DevConfig
 		cps, cpm uint16
 		volts    int16
 		temp     float64
@@ -72,9 +73,11 @@ func main() {
 		fmt.Printf("Temp: %g\n", temp)
 	}
 
-	err = gc.GetConfiguration()
+	cfg, err = gc.GetConfiguration()
 	if err != nil {
 		fmt.Printf("Failed to get config: '%s'\n", err)
+	} else {
+		fmt.Printf("Cfg: %+v\n", cfg)
 	}
 
 }
diff --git a/devices/geiger/geiger.go b/devices/geiger/geiger.go
index 60d1e60..0200c6e 100644
--- a/devices/geiger/geiger.go
+++ b/devices/geiger/geiger.go
@@ -27,12 +27,12 @@ type Counter interface {
 	GetCPM() (uint16, error)
 	GetCPS() (uint16, error)
 	Volts() (int16, error)
-	GetHistoryData()
+	GetHistory()
 	TurnOnCPS() error
 	TurnOffCPS() error
 	GetAutoCPS() (uint16, error)
 	TurnOffPower()
-	GetConfiguration() error
+	GetConfiguration() (*DevConfig, error)
 	SetConfiguration()
 	SetTime(time.Time)
 	GetTime() (time.Time, error)
diff --git a/devices/geiger/gqgmc.go b/devices/geiger/gqgmc.go
index 3bc9975..257184a 100644
--- a/devices/geiger/gqgmc.go
+++ b/devices/geiger/gqgmc.go
@@ -8,6 +8,7 @@
 package geiger
 
 import (
+	"encoding/binary"
 	"encoding/hex"
 	"errors"
 	"fmt"
@@ -15,6 +16,7 @@ import (
 	"sync"
 	"time"
 
+	"github.com/go-restruct/restruct"
 	"github.com/tarm/serial"
 )
 
@@ -59,6 +61,66 @@ type GQGMCCounter struct {
 	mutex *sync.Mutex
 }
 
+// DevConfig is the gcgmc config block.
+type DevConfig struct {
+	PowerOnOff                   int8  `struct:"int8"`
+	AlarmOnOff                   int8  `struct:"int8"`
+	SpeakerOnOff                 int8  `struct:"int8"`
+	GraphicModeOnOff             int8  `struct:"int8"`
+	BackLightTimeoutSeconds      int8  `struct:"int8"`
+	IdleTitleDisplayMode         int8  `struct:"int8"`
+	AlarmCPMValue                int16 `struct:"int16,big"`
+	CalibrationCPMHiByte0        byte  `struct:"byte"`
+	CalibrationCPMLoByte0        byte  `struct:"byte"`
+	CalibrationSvUcByte3p0       byte  `struct:"byte"`
+	CalibrationSvUcByte2p0       byte  `struct:"byte"`
+	CalibrationSvUcByte1p0       byte  `struct:"byte"`
+	CalibrationSvUcByte0p0       byte  `struct:"byte"`
+	CalibrationCPMHiByte1        byte  `struct:"byte"`
+	CalibrationCPMLoByte1        byte  `struct:"byte"`
+	CalibrationSvUcByte3p1       byte  `struct:"byte"`
+	CalibrationSvUcByte2p1       byte  `struct:"byte"`
+	CalibrationSvUcByte1p1       byte  `struct:"byte"`
+	CalibrationSvUcByte0p1       byte  `struct:"byte"`
+	CalibrationCPMHiByte2        byte  `struct:"byte"`
+	CalibrationCPMLoByte2        byte  `struct:"byte"`
+	CalibrationSvUcByte3p2       byte  `struct:"byte"`
+	CalibrationSvUcByte2p2       byte  `struct:"byte"`
+	CalibrationSvUcByte1p2       byte  `struct:"byte"`
+	CalibrationSvUcByte0p2       byte  `struct:"byte"`
+	IdleDisplayMode              byte  `struct:"byte"`
+	AlarmValueuSvByte3           byte  `struct:"byte"`
+	AlarmValueuSvByte2           byte  `struct:"byte"`
+	AlarmValueuSvByte1           byte  `struct:"byte"`
+	AlarmValueuSvByte0           byte  `struct:"byte"`
+	AlarmType                    byte  `struct:"byte"`
+	SaveDataType                 byte  `struct:"byte"`
+	SwivelDisplay                byte  `struct:"byte"`
+	ZoomByte3                    byte  `struct:"byte"`
+	ZoomByte2                    byte  `struct:"byte"`
+	ZoomByte1                    byte  `struct:"byte"`
+	ZoomByte0                    byte  `struct:"byte"`
+	SPIDataSaveAddress2          byte  `struct:"byte"`
+	SPIDataSaveAddress1          byte  `struct:"byte"`
+	SPIDataSaveAddress0          byte  `struct:"byte"`
+	SPIDataReadAddress2          byte  `struct:"byte"`
+	SPIDataReadAddress1          byte  `struct:"byte"`
+	SPIDataReadAddress0          byte  `struct:"byte"`
+	PowerSavingMode              int8  `struct:"int8"`
+	SensitivityMode              int8  `struct:"int8"`
+	CounterDelay                 int16 `struct:"int16,big"`
+	VoltageOffset                int8  `struct:"int8"`
+	MaxCPM                       int16 `struct:"int16,big"`
+	SensitivityAutoModeThreshold int8  `struct:"int8"`
+	SaveDateTimeStamp6           byte  `struct:"byte"`
+	SaveDateTimeStamp5           byte  `struct:"byte"`
+	SaveDateTimeStamp4           byte  `struct:"byte"`
+	SaveDateTimeStamp3           byte  `struct:"byte"`
+	SaveDateTimeStamp2           byte  `struct:"byte"`
+	SaveDateTimeStamp1           byte  `struct:"byte"`
+	MaximumBytes                 byte  `struct:"byte"`
+}
+
 // NewGQGMC creates a new GQGMC Counter instance
 func NewGQGMC(c Config) (*GQGMCCounter, error) {
 	var gc GQGMCCounter
@@ -86,7 +148,7 @@ func NewGQGMC(c Config) (*GQGMCCounter, error) {
 			gc.serial = hex.EncodeToString(buf)
 		}
 	}
-	//getConfigurationData()
+	//getConfiguration()
 	return &gc, nil
 }
 
@@ -153,8 +215,8 @@ func (gc *GQGMCCounter) Volts() (int16, error) {
 	return int16(volts[0]), err
 }
 
-// GetHistoryData Should return history data but is unimplemented for now
-func (gc *GQGMCCounter) GetHistoryData() {
+// GetHistory Should return history data but is unimplemented for now
+func (gc *GQGMCCounter) GetHistory() {
 	// It's not recommended to use this so blank for now.
 	return
 }
@@ -235,20 +297,22 @@ func (gc *GQGMCCounter) TurnOffPower() {
 }
 
 // GetConfiguration reads configuration data
-func (gc *GQGMCCounter) GetConfiguration() error {
+func (gc *GQGMCCounter) GetConfiguration() (*DevConfig, error) {
 	if !gc.supportedModels(mod280n300) {
-		return errors.New("Unsupported Model")
+		return nil, errors.New("Unsupported Model")
 	}
 	if gc.versionLT("Re 2.10") {
-		return errors.New("Unsupported version")
+		return nil, errors.New("Unsupported version")
 	}
 
-	cfg, err := gc.communicate(cmdGetCfg, 256)
+	data, err := gc.communicate(cmdGetCfg, 256)
 	if err != nil {
-		return err
+		return nil, err
 	}
-	fmt.Printf("Configuration: %+v\n", cfg)
-	return nil
+	var cfg DevConfig
+	restruct.Unpack(data[:58], binary.BigEndian, &cfg)
+	fmt.Printf("Configuration: %+v\n", data)
+	return &cfg, nil
 }
 
 // SetConfiguration writes configuration data
@@ -452,8 +516,9 @@ func (gc *GQGMCCounter) recv(length int) ([]byte, error) {
 	}
 	read := n
 	if n != length {
+		// Handle the case where we couldn't read it all.
+		// Really only happens for length > 32.
 		for i := 0; i < 20; i++ {
-			fmt.Printf("%d(%d) ", n, read)
 			n, err = gc.port.Read(buf[read:])
 			if err != nil {
 				return nil, err
@@ -463,10 +528,8 @@ func (gc *GQGMCCounter) recv(length int) ([]byte, error) {
 				break
 			}
 		}
-		fmt.Printf("%d(%d)\n", n, read)
 	}
 	if read != length {
-		fmt.Printf("Short read: %+v\n", buf)
 		return nil, fmt.Errorf("Short read (got: %d, wanted: %d)", n, length)
 	}
 	return buf, nil
-- 
GitLab