From bc543f19149901d506d8a00be839fadb41629548 Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Fri, 23 Feb 2024 13:58:27 +0100
Subject: [PATCH] add the ability to read the enabled state per device

---
 coincell.cpp |  5 +++++
 coincell.h   |  1 +
 heater.cpp   | 38 ++++++++++++++++++++++++++++++++++++++
 heaters.h    |  3 +++
 test.cpp     | 14 +++++++++-----
 5 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/coincell.cpp b/coincell.cpp
index a27280b..60a1f21 100644
--- a/coincell.cpp
+++ b/coincell.cpp
@@ -72,6 +72,11 @@ bool CoinCell::getSetpoint(float& temperature)
 	return ret;
 }
 
+bool CoinCell::getEnabled()
+{
+	return heaters->getHeaterEnabled(heaterId);
+}
+
 bool  CoinCell::setConnected(bool connected)
 {
 	bool ret = true;
diff --git a/coincell.h b/coincell.h
index fa766ad..0d81491 100644
--- a/coincell.h
+++ b/coincell.h
@@ -36,6 +36,7 @@ public:
 	bool isShorted(bool& detected);
 	bool measureEis(std::filesystem::path path, std::string userStr = "");
 	bool setEnabled(bool enable);
+	bool getEnabled();
 	double getLastKnownCapacity();
 	void setLastKnownCapacity(double cap);
 	double getFullVoltage();
diff --git a/heater.cpp b/heater.cpp
index 8e0557d..cf9382a 100644
--- a/heater.cpp
+++ b/heater.cpp
@@ -299,6 +299,44 @@ bool Heaters::getSetpoint(int heater, float& temperature)
 	return true;
 }
 
+bool Heaters::getHeaterState(int heater, struct heater_state& state)
+{
+	int device = getAddress(heater).first;
+	int heaterId = getAddress(heater).second;
+
+	if(devices.size() <= device)
+		return false;
+
+	if(deviceBad(device))
+		return false;
+
+	devices[device].mutex->lock();
+	int ret = coincellhell_get_state(&devices[device].device, heaterId, &state);
+	devices[device].mutex->unlock();
+	if(ret != 0)
+	{
+		deviceError(device, ERR_CONNECTION);
+		if(deviceBad(device))
+		{
+			return false;
+		}
+		else
+		{
+			return getHeaterState(heater, state);
+		}
+	}
+	return true;
+}
+
+bool Heaters::getHeaterEnabled(int heater)
+{
+	struct heater_state state;
+	bool ret = getHeaterState(heater, state);
+	if(!ret)
+		return false;
+	else
+		return state.enabled;
+}
 
 bool Heaters::getTemperature(int heater, float& temperature)
 {
diff --git a/heaters.h b/heaters.h
index 0b124d2..d254f54 100644
--- a/heaters.h
+++ b/heaters.h
@@ -45,6 +45,8 @@ private:
 	bool heaterBad(int heater);
 	void recoverDevice(int device);
 
+	bool getHeaterState(int heater, struct heater_state& state);
+
 public:
 
 	Heaters(const std::vector<uint16_t> serials,
@@ -56,6 +58,7 @@ public:
 	void reconnectDevices();
 
 	bool setHeaterEnabled(int heater, bool enabled);
+	bool getHeaterEnabled(int heater);
 	bool setSetpoint(int heater, float temperature);
 	bool getSetpoint(int heater, float& temperature);
 	bool setRamp(int heater, time_t endTime, float temperature);
diff --git a/test.cpp b/test.cpp
index 941cbb4..83995ec 100644
--- a/test.cpp
+++ b/test.cpp
@@ -168,6 +168,10 @@ int main(int argc, char** argv)
 						else
 							Log(Log::INFO, true, false)<<"Cell "<<i<<" temperature: "<<temperature;
 					}
+
+					auto connected = multiplexers.getConnected();
+					for(size_t i = 0; i < connected.size(); ++i)
+						Log(Log::INFO, true, false)<<"multiplexer channel "<<i<<' '<<(connected[i] ? "connected" : "disconnected");
 				}
 				else
 				{
@@ -188,12 +192,12 @@ int main(int argc, char** argv)
 						continue;
 					}
 
-					Log(Log::ERROR, true, false)<<"Cell "<<coincellId<<" temperature: "<<temperature<<" setpoint: "<<setpoint;
-				}
+					bool enabled = coinCells[coincellId]->getEnabled();
 
-				auto connected = multiplexers.getConnected();
-				for(size_t i = 0; i < connected.size(); ++i)
-					Log(Log::INFO, true, false)<<"multiplexer channel "<<i<<' '<<(connected[i] ? "connected" : "disconnected");
+					Log(Log::ERROR, true, false)<<"Cell "<<coincellId<<':';
+					Log(Log::ERROR, true, false)<<"\ttemperature: "<<temperature<<"\n\tsetpoint: "<<setpoint<<"\t\nenabled: "<<enabled;
+
+				}
 			}
 			else if(tokens[0] == "clear")
 			{
-- 
GitLab