diff --git a/device/PowerSensor/PowerSensor.ino b/device/PowerSensor/PowerSensor.ino index 58af81b4..10b16f75 100644 --- a/device/PowerSensor/PowerSensor.ino +++ b/device/PowerSensor/PowerSensor.ino @@ -51,7 +51,7 @@ float voltageValues[PAIRS]; float currentValues[PAIRS]; float powerValues[PAIRS]; float totalPower; -bool displayEnabled = true; +bool displayPaused = false; #endif const uint32_t ADC_SCANMODES[] = {LL_ADC_REG_SEQ_SCAN_DISABLE, LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS, @@ -226,16 +226,28 @@ void serialEvent() { sendSingleValue = true; break; case 'S': - // Enable streaming of data + // Enable streaming of data and pause display +#ifndef NODISPLAY + displayPaused = true; + displayMeasurementInProgress(); +#endif counter = 0; streamValues = true; break; case 'T': - // Disable streaming of data + // Disable streaming of data and unpause display streamValues = false; +#ifndef NODISPLAY + displayPaused = false; + displayInitialValues(); +#endif break; case 'X': - // Shutdown, shuts off IO thread on host + // Shutdown and unpause display, shuts off IO thread on host +#ifndef NODISPLAY + displayPaused = false; + displayInitialValues(); +#endif streamValues = false; delay(100); // on an RPi host the stop does not arrive at the host properly unless there is a delay here Serial.write((const uint8_t[]) { 0xFF, 0x3F}, 2); @@ -262,19 +274,6 @@ void serialEvent() { // Reset device to bootloader, enables DFU mode JumpToBootloader(); break; -#ifndef NODISPLAY - case 'D': - // toggle display - displayEnabled = !displayEnabled; - if (displayEnabled) { - Serial.write('L'); - initDisplay(); - } else { - deinitDisplay(); - Serial.write('l'); - } - break; -#endif } } } @@ -368,9 +367,7 @@ void setup() { // configure hardware (GPIO, DMA, ADC) configureDevice(); #ifndef NODISPLAY - if (displayEnabled) { initDisplay(); - } #endif Blink(1); } @@ -384,7 +381,7 @@ void loop() { serialEvent(); // update display if enabled #ifndef NODISPLAY - if (displayEnabled) { + if (!displayPaused) { updateDisplay(); } #endif diff --git a/device/PowerSensor/device_specific/BLACKPILL_F401CC_F411CE.hpp b/device/PowerSensor/device_specific/BLACKPILL_F401CC_F411CE.hpp index e9f661af..1ce8308d 100644 --- a/device/PowerSensor/device_specific/BLACKPILL_F401CC_F411CE.hpp +++ b/device/PowerSensor/device_specific/BLACKPILL_F401CC_F411CE.hpp @@ -121,10 +121,8 @@ extern "C" void DMA2_Stream0_IRQHandler() { } level /= numSampleToAverage; #ifndef NODISPLAY - if (displayEnabled) { - // store in sensorValues for display purposes - sensorLevels[i] = level; - } + // store in sensorValues for display purposes + sensorLevels[i] = level; #endif // add metadata to remaining bits: 2 bytes available with 10b sensor value // First byte: 1 iii aaaa diff --git a/device/PowerSensor/device_specific/DISCO_F407VG.hpp b/device/PowerSensor/device_specific/DISCO_F407VG.hpp index 4aaf0bc3..30327e7a 100644 --- a/device/PowerSensor/device_specific/DISCO_F407VG.hpp +++ b/device/PowerSensor/device_specific/DISCO_F407VG.hpp @@ -115,10 +115,8 @@ extern "C" void DMA2_Stream0_IRQHandler() { level = __LL_ADC_MULTI_CONV_DATA_MASTER_SLAVE(LL_ADC_MULTI_SLAVE, dmaBuffer[i/2]); } #ifndef NODISPLAY - if (displayEnabled) { - // store in sensorValues for display purposes - sensorLevels[i] = level; - } + // store in sensorValues for display purposes + sensorLevels[i] = level; #endif // add metadata to remaining bits: 2 bytes available with 10b sensor value // First byte: 1 iii aaaa diff --git a/device/PowerSensor/display.cpp b/device/PowerSensor/display.cpp index 15abd9fc..4dccde9a 100644 --- a/device/PowerSensor/display.cpp +++ b/device/PowerSensor/display.cpp @@ -21,6 +21,7 @@ #define OFFSET 24 // vertical offset: pixel 0 is outside of the screen #define OFFSET_MAIN (OFFSET + 10) #define OFFSET_BOTTOM (OFFSET + 65) +#define OFFSET_PAUSE_TEXT (OFFSET + 15) #include @@ -42,8 +43,10 @@ void initDisplay() { #endif clearDisplay(); analogWrite(TFT_BLK, 120); + displayInitialValues(); } + void deinitDisplay() { clearDisplay(); analogWrite(TFT_BLK, 0); @@ -117,3 +120,15 @@ void displaySensor(const int sensorPairName, const float amp, const float volt, dtostrf(watt, 5, 1, buf); tft.drawFastNumber(buf, 5); } + +void displayMeasurementInProgress() { + clearDisplay(); + + tft.setTextColor(ST77XX_YELLOW); + tft.setTextSize(2); + + tft.setCursor(0, OFFSET_PAUSE_TEXT); + tft.println("Measurement"); + tft.println("in"); + tft.println("progress"); +} diff --git a/device/PowerSensor/display.hpp b/device/PowerSensor/display.hpp index 693c9734..634dfc61 100644 --- a/device/PowerSensor/display.hpp +++ b/device/PowerSensor/display.hpp @@ -4,3 +4,4 @@ void clearDisplay(); void displayInitialValues(); void displaySensor(const int sensorPairName, const float amp, const float volt, const float watt, const float totalWatt); +void displayMeasurementInProgress(); diff --git a/host/include/PowerSensor.hpp b/host/include/PowerSensor.hpp index fe5ec5e3..8539d6f4 100644 --- a/host/include/PowerSensor.hpp +++ b/host/include/PowerSensor.hpp @@ -55,7 +55,6 @@ class PowerSensor { void dump(const std::string dumpFileName); // dumpFileName == 0 --> stop dumping void mark(char name); void mark(const State &startState, const State &stopState, const std::string name = 0, unsigned int tag = 0) const; - bool toggleDisplay(); void writeSensorsToEEPROM(); void setType(unsigned int sensorID, const std::string type); diff --git a/host/src/PowerSensor.cc b/host/src/PowerSensor.cc index 8ab18d69..2333cfaa 100644 --- a/host/src/PowerSensor.cc +++ b/host/src/PowerSensor.cc @@ -357,44 +357,18 @@ namespace PowerSensor3 { } } - /** - * @brief Toggle device display on/off - * - * @return New status of display: 1 for on, 0 for off - */ - bool PowerSensor::toggleDisplay() { - // ensure no data is streaming to host - stopIOThread(); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - // drain any remaining incoming data - tcflush(fd, TCIFLUSH); - - writeCharToDevice('D'); - // if the display is now on, the device sends and L, else l - char buffer; - buffer = readCharFromDevice(); - if (buffer == 'L') { - return true; - } else if (buffer == 'l') { - return false; - } else { - std::cerr << "Expected to receive 'l' or 'L' from device after toggling display, but got " << buffer << std::endl; - exit(1); - } - - // restart IO thread - startIOThread(); - } - /** * @brief thread to continuously read sensor values from device * */ void PowerSensor::IOThread() { - threadStarted.up(); unsigned int sensorNumber, marker = 0, sensorsRead = 0; uint16_t level; + // wait until we can read values from the device + readLevelFromDevice(&sensorNumber, &level, &marker); + threadStarted.up(); + while (readLevelFromDevice(&sensorNumber, &level, &marker)) { std::unique_lock lock(mutex); diff --git a/host/src/psconfig.cc b/host/src/psconfig.cc index e36ed9b7..40ab190a 100644 --- a/host/src/psconfig.cc +++ b/host/src/psconfig.cc @@ -178,7 +178,6 @@ void usage(char *argv[]) { "or unitless gain for voltage sensors (odd sensors)" << std::endl; std::cerr << "-o turns a sensor on (1) or off (0)" << std::endl; std::cerr << "-p prints configured values" << std::endl; - std::cerr << "-l toggles device display on/off" << std::endl; std::cerr << "example: " << argv[0] << " -d /dev/ttyACM0 -s 0 -t MLX10 -v 1.65 " "-o 1 -s 1 -t voltage0 -v 0 -n 0.95 -o 1 -p" << std::endl; std::cerr << "Known current sensor types: MLX10, MLX20, MLX50, MLX75." << std::endl; @@ -192,7 +191,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:lph")) >= 0;) { + for (int opt; (opt = getopt(argc, argv, "d:s:i:t:av:n:o:ph")) >= 0;) { switch (opt) { // device select case 'd': @@ -242,13 +241,6 @@ int main(int argc, char *argv[]) { doWriteConfig = true; break; - // toggle display - case 'l': { - bool displayStatus = getPowerSensor(device)->toggleDisplay(); - std::cout << "Display is now " << (displayStatus ? "on" : "off") << "." << std::endl; - break; - } - // print case 'p': doPrint = true; diff --git a/python/PyPowerSensor.cc b/python/PyPowerSensor.cc index 01f9aa10..52fe9fc9 100644 --- a/python/PyPowerSensor.cc +++ b/python/PyPowerSensor.cc @@ -23,8 +23,7 @@ PYBIND11_MODULE(powersensor, m) { .def("dump", &PowerSensor3::PowerSensor::dump, "Dump sensor values to file. Set filename to empty string to stop dumping", py::arg("filename")) .def("mark", static_cast(&PowerSensor3::PowerSensor::mark), - "Add given marker character to dump file", py::arg("Marker character")) - .def("toggle_display", &PowerSensor3::PowerSensor::toggleDisplay, "Toggle device display on/off"); + "Add given marker character to dump file", py::arg("Marker character")); py::class_(m, "State") .def_readonly("consumed_energy", &PowerSensor3::State::consumedEnergy,