Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,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 @@ -539,6 +540,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 @@ -655,6 +657,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
101 changes: 101 additions & 0 deletions src/components/stopwatch/StopWatchController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "StopWatchController.h"
Comment thread
pptime02 marked this conversation as resolved.
Outdated

using namespace Pinetime::Controllers;

namespace {
TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) {
Comment thread
pptime02 marked this conversation as resolved.
Outdated
TickType_t delta = 0;
// Take care of overflow
if (startTime > currentTime) {
delta = 0xffffffff - startTime;
delta += (currentTime + 1);
} else {
delta = currentTime - startTime;
}
return delta;
}
}

StopWatchController::StopWatchController() {
Clear();
}

// State Change

void StopWatchController::Start() {
currentState = StopWatchStates::Running;
startTime = xTaskGetTickCount();
}

void StopWatchController::Pause() {
currentState = StopWatchStates::Paused;
timeElapsedPreviously += calculateDelta(startTime, xTaskGetTickCount());
}

void StopWatchController::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 StopWatchController::PushLap() {
TickType_t lapEnd = GetElapsedTime();
laps[lapHead].time = lapEnd;
laps[lapHead].count = lapCount + 1;
lapCount += 1;
lapHead = lapCount % LAP_CAPACITY;
}

int StopWatchController::GetLapNum() {
if (lapCount < LAP_CAPACITY)
return lapCount;
else
return LAP_CAPACITY;
}

int StopWatchController::GetLapCount() {
return lapCount;
}

int wrap(int index) {
Comment thread
pptime02 marked this conversation as resolved.
Outdated
return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY;
}

LapInfo_t* StopWatchController::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 / State acess

TickType_t StopWatchController::GetElapsedTime() {
if (!IsRunning()) {
return timeElapsedPreviously;
}
return timeElapsedPreviously + calculateDelta(startTime, xTaskGetTickCount());
}

bool StopWatchController::IsRunning() {
return currentState == StopWatchStates::Running;
}

bool StopWatchController::IsCleared() {
return currentState == StopWatchStates::Cleared;
}

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

#include <FreeRTOS.h>
#include <timers.h>

#define LAP_CAPACITY 2

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

enum class StopWatchStates { Cleared, Running, Paused };

struct LapInfo_t {
Comment thread
pptime02 marked this conversation as resolved.
Outdated
int count = 0; // Used to label the lap
TickType_t time = 0; // Delta time from beginning of stopwatch
};

class StopWatchController {
public:
StopWatchController();

// StopWatch functionality and data
void Start();
void Pause();
void Clear();

TickType_t GetElapsedTime();

// Lap functionality

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

/// 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;
};
}
}
4 changes: 3 additions & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem)
Expand All @@ -91,6 +92,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
motionController {motionController},
timerController {timerController},
alarmController {alarmController},
stopWatchController {stopWatchController},
brightnessController {brightnessController},
touchHandler {touchHandler},
filesystem {filesystem} {
Expand Down Expand Up @@ -449,7 +451,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, stopWatchController);
break;
case Apps::Twos:
currentScreen = std::make_unique<Screens::Twos>(this);
Expand Down
3 changes: 3 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 Down Expand Up @@ -61,6 +62,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem);
Expand Down Expand Up @@ -89,6 +91,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::TimerController& timerController;
Pinetime::Controllers::AlarmController& alarmController;
Pinetime::Controllers::StopWatchController& stopWatchController;
Pinetime::Controllers::BrightnessController& brightnessController;
Pinetime::Controllers::TouchHandler& touchHandler;
Pinetime::Controllers::FS& filesystem;
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayAppRecovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem)
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 StopWatchController;
class BrightnessController;
class FS;
}
Expand All @@ -59,6 +60,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::TimerController& timerController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatch& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem);
Expand Down
Loading