Skip to content

Commit

Permalink
Merge pull request #132 from nlesc-recruit/pause-display
Browse files Browse the repository at this point in the history
Pause display when measuring
  • Loading branch information
loostrum authored Jul 10, 2023
2 parents bba514a + 5910fae commit 2ca98f9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 70 deletions.
37 changes: 17 additions & 20 deletions device/PowerSensor/PowerSensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -368,9 +367,7 @@ void setup() {
// configure hardware (GPIO, DMA, ADC)
configureDevice();
#ifndef NODISPLAY
if (displayEnabled) {
initDisplay();
}
#endif
Blink(1);
}
Expand All @@ -384,7 +381,7 @@ void loop() {
serialEvent();
// update display if enabled
#ifndef NODISPLAY
if (displayEnabled) {
if (!displayPaused) {
updateDisplay();
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions device/PowerSensor/device_specific/DISCO_F407VG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions device/PowerSensor/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <STM32F4_SPI_DMA.h>

Expand All @@ -42,8 +43,10 @@ void initDisplay() {
#endif
clearDisplay();
analogWrite(TFT_BLK, 120);

displayInitialValues();
}

void deinitDisplay() {
clearDisplay();
analogWrite(TFT_BLK, 0);
Expand Down Expand Up @@ -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");
}
1 change: 1 addition & 0 deletions device/PowerSensor/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
1 change: 0 additions & 1 deletion host/include/PowerSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 4 additions & 30 deletions host/src/PowerSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(mutex);

Expand Down
10 changes: 1 addition & 9 deletions host/src/psconfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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':
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions python/PyPowerSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void (PowerSensor3::PowerSensor::*)(char)>(&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_<PowerSensor3::State>(m, "State")
.def_readonly("consumed_energy", &PowerSensor3::State::consumedEnergy,
Expand Down

0 comments on commit 2ca98f9

Please sign in to comment.