diff --git a/device/PowerSensor/PowerSensor.ino b/device/PowerSensor/PowerSensor.ino index e78764b9..fbcd9e94 100644 --- a/device/PowerSensor/PowerSensor.ino +++ b/device/PowerSensor/PowerSensor.ino @@ -92,6 +92,7 @@ bool sendMarkerNext = false; struct Sensor { char type[16]; + char pairName[16]; float vref; float sensitivity; bool inUse; diff --git a/host/include/PowerSensor.hpp b/host/include/PowerSensor.hpp index 9cd66af1..7dd514b7 100644 --- a/host/include/PowerSensor.hpp +++ b/host/include/PowerSensor.hpp @@ -58,17 +58,20 @@ class PowerSensor { void writeSensorsToEEPROM(); void setType(unsigned int sensorID, const std::string type); + void setPairName(unsigned int sensorID, const std::string pairName); void setVref(unsigned int sensorID, const float vref); void setSensitivity(unsigned int sensorID, const float slope); void setInUse(unsigned int sensorID, const bool inUse); std::string getType(unsigned int sensorID) const; + std::string getPairName(unsigned int sensorID) const; float getVref(unsigned int sensorID) const; float getSensitivity(unsigned int sensorID) const; bool getInUse(unsigned int sensorID) const; private: static const unsigned MAX_TYPE_LENGTH = 16; + static const unsigned MAX_PAIRNAME_LENGTH = 16; int fd; int pipe_fd; @@ -104,6 +107,8 @@ class PowerSensor { struct EEPROM { /** @brief Sensor type */ char type[MAX_TYPE_LENGTH]; + /** @brief Sensor pair name */ + char pairName[MAX_PAIRNAME_LENGTH]; /** @brief Sensor reference voltage */ float vref; /** @brief Sensor sensitivity (V/A for current sensors, unitless gain for voltage sensors) */ @@ -113,12 +118,14 @@ class PowerSensor { } __attribute__((packed)); std::string type; + std::string pairName; float vref; float sensitivity; bool inUse; uint16_t level; double valueAtLastMeasurement; void setType(const std::string type); + void setPairName(const std::string pairName); void setVref(const float vref); void setSensitivity(const float slope); void setInUse(const bool inUse); diff --git a/host/src/PowerSensor.cc b/host/src/PowerSensor.cc index 9247ae96..42bb1ef5 100644 --- a/host/src/PowerSensor.cc +++ b/host/src/PowerSensor.cc @@ -561,6 +561,16 @@ namespace PowerSensor3 { return sensors[sensorID].type; } + /** + * @brief Get sensor pair name of given sensor + * + * @param sensorID + * @return std::string + */ + std::string PowerSensor::getPairName(unsigned int sensorID) const { + return sensors[sensorID].pairName; + } + /** * @brief Get reference voltage (V) of given sensor * @@ -601,6 +611,16 @@ namespace PowerSensor3 { sensors[sensorID].setType(type); } + /** + * @brief Set sensor pair name of given sensor + * + * @param sensorID + * @param pairName + */ + void PowerSensor::setPairName(unsigned int sensorID, const std::string pairName) { + sensors[sensorID].setPairName(pairName); + } + /** * @brief Set reference voltage (V) of given sensor * diff --git a/host/src/psconfig.cc b/host/src/psconfig.cc index 40ab190a..63fd7f6c 100644 --- a/host/src/psconfig.cc +++ b/host/src/psconfig.cc @@ -148,6 +148,7 @@ void print() { std::cout << "sensor " << sensor << " (" << sensorType << "): " "type: " << powerSensor->getType(sensor) << ", " + "name: " << powerSensor->getPairName(sensor) << ", " "Vref: " << powerSensor->getVref(sensor) << " V, " << sensitivityName << ": " << factor * powerSensor->getSensitivity(sensor) << unit << ", " "Status: " << (powerSensor->getInUse(sensor) ? "on" : "off") << std::endl; @@ -171,6 +172,7 @@ void usage(char *argv[]) { std::cerr << "-s selects the sensor (0-" << PowerSensor3::MAX_SENSORS << ")" << std::endl; std::cerr << "-t sets the sensor type. This also sets the sensitivity to the default value if " "the sensor is of a type known to this programme (see list at the bottom of this help)." << std::endl; + std::cerr << "-m sets the sensor pair name." << std::endl; std::cerr << "-v sets the reference voltage level" << std::endl; std::cerr << "-a automatically calibrate vref of the current sensor. " "The input to the sensor must be zero volt or ampere" << std::endl; @@ -191,7 +193,7 @@ int main(int argc, char *argv[]) { bool doPrint = false; std::cout << "psconfig version " << PowerSensor3::POWERSENSOR_VERSION << std::endl << std::endl; - for (int opt; (opt = getopt(argc, argv, "d:s:i:t:av:n:o:ph")) >= 0;) { + for (int opt; (opt = getopt(argc, argv, "d:s:i:t:m:av:n:o:ph")) >= 0;) { switch (opt) { // device select case 'd': @@ -214,6 +216,17 @@ int main(int argc, char *argv[]) { break; } + // sensor pair name + case 'm': + getPowerSensor(device)->setPairName(sensor, optarg); + // ensure to set same pair name for both the current and voltage sensor + if ((sensor % 2) == 0) { + getPowerSensor(device)->setPairName(sensor + 1, optarg); + } else { + getPowerSensor(device)->setPairName(sensor - 1, optarg); + } + break; + // sensor auto calibration of reference voltage case 'a': getPowerSensor(device); diff --git a/host/src/sensors.cc b/host/src/sensors.cc index c533a221..5ead44f2 100644 --- a/host/src/sensors.cc +++ b/host/src/sensors.cc @@ -27,11 +27,19 @@ void PowerSensor::Sensor::readFromEEPROM(int fd) { // If EEPROM is corrupted, type name may be too long. Avoid unrecoverable situation by changing type if that happens std::string type = eeprom.type; if (type.length() >= MAX_TYPE_LENGTH) { - std::cerr << "Read invalid type from device, EEPROM data may be corrupt" << std::endl; + std::cerr << "Read invalid sensor type from device, EEPROM data may be corrupt" << std::endl; type = "INVALID"; } + // Same check as for type name + std::string pairName = eeprom.pairName; + if (pairName.length() >= MAX_PAIRNAME_LENGTH) { + std::cerr << "Read invalid sensor pair name from device, EEPROM data may be corrupt" << std::endl; + pairName = "INVALID"; + } + setType(type); + setPairName(pairName); setVref(eeprom.vref); setSensitivity(eeprom.sensitivity); setInUse(eeprom.inUse); @@ -48,6 +56,7 @@ void PowerSensor::Sensor::writeToEEPROM(int fd) const { EEPROM eeprom; strncpy(eeprom.type, type.c_str(), type.length() + 1); // plus one for null termination character + strncpy(eeprom.pairName, pairName.c_str(), pairName.length() + 1); // plus one for null termination character eeprom.vref = vref; eeprom.sensitivity = sensitivity; eeprom.inUse = inUse; @@ -103,6 +112,22 @@ void PowerSensor::Sensor::setType(const std::string type) { } } +/** + * @brief Set name of sensor pair + * + * @param pairName + */ +void PowerSensor::Sensor::setPairName(const std::string pairName) { + if (pairName.length() >= MAX_PAIRNAME_LENGTH) { + // MAX_PAIRNAME_LENGTH includes null termination character + std::cerr << "Sensor pair name can be at most " << MAX_PAIRNAME_LENGTH - 1 << " characters" << std::endl; + exit(1); + } else { + this->pairName = pairName; + } +} + + /** * @brief Set reference voltage of sensor *