Skip to content

Soft-off after 10 seconds not earlier #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .github/fake-cc
100755 → 100644
Empty file.
4 changes: 0 additions & 4 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified README.md
100755 → 100644
Empty file.
30 changes: 13 additions & 17 deletions src/OpenBikeSensorFirmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Gps gps;
static const long BLUETOOTH_INTERVAL_MILLIS = 100;
static long lastBluetoothInterval = 0;

static const long DISPLAY_INTERVAL_MILLIS = 200;
static const long DISPLAY_INTERVAL_MILLIS = 300;
static long lastDisplayInterval = 0;

float TemperatureValue = -1;
Expand All @@ -103,7 +103,7 @@ CircularBuffer<DataSet*, 10> dataBuffer;
FileWriter* writer;

const uint8_t displayAddress = 0x3c;

constexpr uint16_t BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS = 5000;

// Enable dev-mode. Allows to
// - set wifi config
Expand Down Expand Up @@ -233,35 +233,31 @@ static uint8_t shutdownState = 0;
// Power-management keep alive timer
// This function is called every 100 ms
static unsigned long timeOfLastPowerKeepAlive = 0;
static uint8_t buttonPressedCounter = 0;
static void powerKeepAliveTimerISR()
{
unsigned long now = millis();

// Send "keep alive" trigger to power management module
// This is done by toggling the pin every 300 ms or more
if(shutdownState == 0)
{
if(!digitalRead(IP5306_BUTTON) && millis() - timeOfLastPowerKeepAlive > POWER_KEEP_ALIVE_INTERVAL_MS)
unsigned long timeSinceLastPowerKeepAlive = now - timeOfLastPowerKeepAlive;
bool ip5306ButtonState = digitalRead(IP5306_BUTTON);

if(!ip5306ButtonState && timeSinceLastPowerKeepAlive > POWER_KEEP_ALIVE_INTERVAL_MS)
{
timeOfLastPowerKeepAlive = millis();
timeOfLastPowerKeepAlive = now;
digitalWrite(IP5306_BUTTON, HIGH);
}
else if(digitalRead(IP5306_BUTTON) && millis() - timeOfLastPowerKeepAlive > 300)
else if(ip5306ButtonState && timeSinceLastPowerKeepAlive > 300)
{
timeOfLastPowerKeepAlive = millis();
timeOfLastPowerKeepAlive = now;
digitalWrite(IP5306_BUTTON, LOW);
}
}

// Soft power-off OBSPro when button is pressed for more than 2 seconds
if(button.read())
{
if(buttonPressedCounter < 255)
buttonPressedCounter++;
}
else
buttonPressedCounter = 0;

if(shutdownState == 0 && buttonPressedCounter >= 50) {
if(shutdownState == 0 && button.handle(now)
&& button.getCurrentStateMillis() >= BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS) {
shutdownState = 1;
}
switch(shutdownState)
Expand Down
2 changes: 2 additions & 0 deletions src/configServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ String getIp() {
}

void updateDisplay(DisplayDevice * const display, String action = "") {
obsDisplay->handleShutdownDisplay();
if (action.isEmpty()) {
display->showTextOnGrid(0, 0, "Ver.:");
display->showTextOnGrid(1, 0, OBSVersion);
Expand Down Expand Up @@ -2068,6 +2069,7 @@ void configServerHandle() {
if (obsImprov) {
obsImprov->handle();
}
obsDisplay->handleShutdownDisplay();
}

std::vector<std::pair<String,String>> extractParameters(HTTPRequest *req) {
Expand Down
27 changes: 25 additions & 2 deletions src/displays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ void DisplayDevice::displaySimple(uint16_t value) {
this->prepareTextOnGrid(3, 2, "cm", MEDIUM_FONT, -7, -5);
}

bool startedShutdownDisplay = false;

bool DisplayDevice::handleShutdownDisplay()
{
bool updated = false;
#ifdef OBSPRO
bool buttonPressed = button.getState();
if (buttonPressed && button.getCurrentStateMillis() > 3000)
{
auto pressTimeMs = button.getCurrentStateMillis() - 3000;
// black out the display from top to bottom for so that after 7000 ms the whole display is black
auto linesToFill = (pressTimeMs * 64) / (BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS - 3000);

m_display->drawBox(0, 0, 128, linesToFill);
startedShutdownDisplay = true;
} else if (startedShutdownDisplay) {
startedShutdownDisplay = false;
clear();
updated = true;
}
#endif
return updated;
}

void DisplayDevice::showValues(
uint16_t sensor1MinDistance, const char* sensor1Location, uint16_t sensor1RawDistance,
uint16_t sensor2MinDistance, const char* sensor2Location, uint16_t sensor2RawDistance, uint16_t sensor2Distance,
Expand Down Expand Up @@ -138,9 +162,8 @@ void DisplayDevice::showValues(
if(BMP280_active == true)
showTemperatureValue(TemperaturValue);
}

handleShutdownDisplay();
m_display->updateDisplay();

}

void DisplayDevice::showGPS(uint8_t sats) {
Expand Down
1 change: 1 addition & 0 deletions src/displays.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class DisplayDevice {
uint8_t scrollUp();
uint8_t startLine();
void highlight(uint32_t highlightTimeMillis = 500);
bool handleShutdownDisplay();

//##############################################################
// Basic display configuration
Expand Down
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ extern Gps gps;

extern const uint8_t LEFT_SENSOR_ID;
extern const uint8_t RIGHT_SENSOR_ID;
extern const uint16_t BUTTON_PRESS_THRESHOLD_FOR_SHUTDOWN_MS;

#endif
7 changes: 4 additions & 3 deletions src/utils/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Button::Button(int pin) : mPin(pin) {
mLastState = mLastRawState = read();
}

void Button::handle() {
handle(millis());
bool Button::handle() {
return handle(millis());
}

void Button::handle(unsigned long millis) {
bool Button::handle(unsigned long millis) {
const int state = read();

if (state != mLastRawState) {
Expand All @@ -52,6 +52,7 @@ void Button::handle(unsigned long millis) {
mReleaseEvents++;
}
}
return state;
}

bool Button::gotPressed() {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
class Button {
public:
explicit Button(int pin);
void handle();
void handle(unsigned long millis);
bool handle();
bool handle(unsigned long millis);
int read() const;
int getState() const;
bool gotPressed();
Expand Down