Skip to content

Commit

Permalink
PR InfiniTimeOrg#783: Stopwatch Persistence
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit ec72f56
Author: Louis Pearson <louispearson@librem.one>
Date:   Tue Oct 26 19:03:46 2021 -0600

    Add battery icon to stopwatch screen

commit 8fbc164
Author: Louis Pearson <louispearson@librem.one>
Date:   Tue Oct 26 18:23:05 2021 -0600

    Make `make all` complete successfully

    - Added StopWatchController to DisplayAppRecovery
    - Added StopWatchController to everywhere needed in CMakeLists
    - Fixed the reorder warning

commit a4a0a4c
Author: Louis Pearson <louispearson@librem.one>
Date:   Mon Oct 25 07:13:40 2021 -0600

    Prevent lap count check when watch is stopped

commit c7c6309
Author: Louis Pearson <louispearson@librem.one>
Date:   Mon Oct 25 06:53:05 2021 -0600

    Run clang-format on files changed in branch

commit 858edfc
Author: Louis Pearson <louispearson@librem.one>
Date:   Mon Oct 25 06:43:16 2021 -0600

    Make function names more descriptive

    Also removes OnButtonPressed. I prefer the button's function to stay the
    same when possible, but I'll change it if needed for the PR.

commit 9ea6bc3
Author: Louis Pearson <louispearson@librem.one>
Date:   Mon Oct 25 06:11:27 2021 -0600

    Removed unsigned ints

    These appear to have been the cause of my issues. Not sure why, I'd have
    to know the nrf52832 better to diagnose it.

commit 2fd2cdb
Author: Louis Pearson <louispearson@librem.one>
Date:   Sun Oct 24 20:41:02 2021 -0600

    Implement lap tracking

commit 04a6b70
Author: Louis Pearson <louispearson@librem.one>
Date:   Sun Oct 24 10:29:34 2021 -0600

    Fix stopwatch display issues

commit ec2b673
Author: Louis Pearson <louispearson@librem.one>
Date:   Sun Oct 24 08:46:38 2021 -0600

    Implement stop watch controller

commit c75102f
Author: Louis Pearson <louispearson@librem.one>
Date:   Sat Oct 23 23:28:07 2021 -0600

    This may have been a bad idea

commit 3a51035
Author: Louis Pearson <louispearson@librem.one>
Date:   Sat Oct 23 21:18:45 2021 -0600

    Add time of day to stopwatch app
  • Loading branch information
FintasticMan committed Dec 13, 2021
1 parent 9777107 commit 140a157
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 108 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ list(APPEND SOURCE_FILES
components/motor/MotorController.cpp
components/settings/Settings.cpp
components/timer/TimerController.cpp
components/stopwatch/StopWatchController.cpp
components/alarm/AlarmController.cpp
components/fs/FS.cpp
drivers/Cst816s.cpp
Expand Down Expand Up @@ -568,6 +569,7 @@ list(APPEND RECOVERY_SOURCE_FILES
components/settings/Settings.cpp
components/timer/TimerController.cpp
components/alarm/AlarmController.cpp
components/stopwatch/StopWatchController.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
FreeRTOS/port_cmsis_systick.c
Expand Down Expand Up @@ -681,6 +683,7 @@ set(INCLUDE_FILES
components/settings/Settings.h
components/timer/TimerController.h
components/alarm/AlarmController.h
components/stopwatch/StopWatchController.h
drivers/Cst816s.h
FreeRTOS/portmacro.h
FreeRTOS/portmacro_cmsis.h
Expand Down
103 changes: 103 additions & 0 deletions src/components/stopwatch/StopWatchController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "StopWatchController.h"
#include <cstdlib>
#include <cstring>

using namespace Pinetime::Controllers;

namespace {
TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) {
TickType_t delta = 0;
// Take care of overflow
if (startTime > currentTime) {
delta = 0xffffffff - startTime;
delta += (currentTime + 1);
} else {
delta = currentTime - startTime;
}
return delta;
}
}

StopWatch::StopWatch() {
clear();
}

// State Change

void StopWatch::start(TickType_t start) {
currentState = StopWatchStates::Running;
startTime = start;
}

void StopWatch::pause(TickType_t end) {
currentState = StopWatchStates::Paused;
timeElapsedPreviously += calculateDelta(startTime, end);
}

void StopWatch::clear() {
currentState = StopWatchStates::Cleared;
timeElapsedPreviously = 0;

for (int i = 0; i < LAP_CAPACITY; i++) {
laps[i].count = 0;
laps[i].time = 0;
}
lapCount = 0;
lapHead = 0;
}

// Lap

void StopWatch::pushLap(TickType_t lapEnd) {
laps[lapHead].time = lapEnd;
laps[lapHead].count = lapCount + 1;
lapCount += 1;
lapHead = lapCount % LAP_CAPACITY;
}

int StopWatch::getLapNum() {
if (lapCount < LAP_CAPACITY)
return lapCount;
else
return LAP_CAPACITY;
}

int StopWatch::getLapCount() {
return lapCount;
}

int wrap(int index) {
return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY;
}

LapInfo_t* StopWatch::lastLap(int lap) {
if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) {
// Return "empty" LapInfo_t
return &emptyLapInfo;
}
// Index backwards
int index = wrap(lapHead - lap);
return &laps[index];
}

// Data acess

TickType_t StopWatch::getStart() {
return startTime;
}

TickType_t StopWatch::getElapsedPreviously() {
return timeElapsedPreviously;
}

bool StopWatch::isRunning() {
return currentState == StopWatchStates::Running;
}

bool StopWatch::isCleared() {
return currentState == StopWatchStates::Cleared;
}

bool StopWatch::isPaused() {
return currentState == StopWatchStates::Paused;
}
67 changes: 67 additions & 0 deletions src/components/stopwatch/StopWatchController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <cstdint>
#include "FreeRTOS.h"

#define LAP_CAPACITY 2

namespace Pinetime {
namespace System {
class SystemTask;
}
namespace Controllers {

enum class StopWatchStates { Cleared, Running, Paused };

struct LapInfo_t {
int count = 0; // Used to label the lap
TickType_t time = 0; // delta time from beginning of stopwatch
};

class StopWatch {
public:
StopWatch();

// StopWatch functionality and data
void start(TickType_t start);
void pause(TickType_t end);
void clear();

TickType_t getStart();
TickType_t getElapsedPreviously();

// Lap functionality

/// Only the latest laps are stored, the lap count is saved until reset
void pushLap(TickType_t lapEnd);

/// Returns actual count of stored laps
int getLapNum();

/// Returns lapCount
int getLapCount();

/// Indexes into lap history, with 0 being the latest lap.
/// If the lap is unavailable, count and time will be 0. If there is a
/// real value, count should be above 0
LapInfo_t* lastLap(int lap = 0);

bool isRunning();
bool isCleared();
bool isPaused();

private:
// Current state of stopwatch
StopWatchStates currentState = StopWatchStates::Cleared;
// Start time of current duration
TickType_t startTime = 0;
// How much time was elapsed before current duration
TickType_t timeElapsedPreviously = 0;
// Stores lap times
LapInfo_t laps[LAP_CAPACITY];
LapInfo_t emptyLapInfo = {.count = 0, .time = 0};
int lapCount = 0;
int lapHead = 0;
};
}
}
5 changes: 4 additions & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "components/ble/NotificationManager.h"
#include "components/motion/MotionController.h"
#include "components/motor/MotorController.h"
#include "components/stopwatch/StopWatchController.h"
#include "displayapp/screens/ApplicationList.h"
#include "displayapp/screens/Brightness.h"
#include "displayapp/screens/Clock.h"
Expand Down Expand Up @@ -99,6 +100,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatch& stopWatchController,
Pinetime::Controllers::TouchHandler& touchHandler)
: lcd {lcd},
lvgl {lvgl},
Expand All @@ -114,6 +116,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
motionController {motionController},
timerController {timerController},
alarmController {alarmController},
stopWatchController {stopWatchController},
touchHandler {touchHandler} {
}

Expand Down Expand Up @@ -437,7 +440,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break;
case Apps::StopWatch:
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask, dateTimeController, stopWatchController, batteryController);
break;
case Apps::Twos:
currentScreen = std::make_unique<Screens::Twos>(this);
Expand Down
4 changes: 4 additions & 0 deletions src/displayapp/DisplayApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "components/timer/TimerController.h"
#include "components/alarm/AlarmController.h"
#include "touchhandler/TouchHandler.h"
#include "components/stopwatch/StopWatchController.h"

#include "displayapp/Messages.h"
#include "BootErrors.h"
Expand All @@ -36,6 +37,7 @@ namespace Pinetime {
class HeartRateController;
class MotionController;
class TouchHandler;
class StopWatch;
}

namespace System {
Expand All @@ -61,6 +63,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatch& stopWatchController,
Pinetime::Controllers::TouchHandler& touchHandler);
void Start(System::BootErrors error);
void PushMessage(Display::Messages msg);
Expand All @@ -87,6 +90,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::TimerController& timerController;
Pinetime::Controllers::AlarmController& alarmController;
Pinetime::Controllers::StopWatch& stopWatchController;
Pinetime::Controllers::TouchHandler& touchHandler;

Pinetime::Controllers::FirmwareValidator validator;
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayAppRecovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatch& stopWatchController,
Pinetime::Controllers::TouchHandler& touchHandler)
: lcd {lcd}, bleController {bleController} {
}
Expand Down
2 changes: 2 additions & 0 deletions src/displayapp/DisplayAppRecovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Pinetime {
class MotorController;
class TimerController;
class AlarmController;
class StopWatch;
}

namespace System {
Expand All @@ -57,6 +58,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatch& stopWatchController,
Pinetime::Controllers::TouchHandler& touchHandler);
void Start();
void Start(Pinetime::System::BootErrors) {
Expand Down
Loading

0 comments on commit 140a157

Please sign in to comment.