Skip to content
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
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ list(APPEND SOURCE_FILES
components/settings/Settings.cpp
components/timer/Timer.cpp
components/alarm/AlarmController.cpp
components/stopwatch/StopWatchController.cpp
components/fs/FS.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
Expand Down Expand Up @@ -536,6 +537,7 @@ list(APPEND RECOVERY_SOURCE_FILES
components/settings/Settings.cpp
components/timer/Timer.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/Timer.h
components/alarm/AlarmController.h
components/stopwatch/StopWatchController.h
drivers/Cst816s.h
FreeRTOS/portmacro.h
FreeRTOS/portmacro_cmsis.h
Expand Down
72 changes: 72 additions & 0 deletions src/components/stopwatch/StopWatchController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "components/stopwatch/StopWatchController.h"

using namespace Pinetime::Controllers;

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

// State Change

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

void StopWatchController::Pause() {
timeElapsedPreviously = GetElapsedTime();
currentState = StopWatchStates::Paused;
}

void StopWatchController::Clear() {
currentState = StopWatchStates::Cleared;
timeElapsedPreviously = 0;

for (uint8_t i = 0; i < histSize; i++) {
history[i].number = 0;
history[i].timeSinceStart = 0;
}
maxLapNumber = 0;
}

// Lap

void StopWatchController::AddLapToHistory() {
TickType_t lapEnd = GetElapsedTime();
history--;
history[0].timeSinceStart = lapEnd;
history[0].number = ++maxLapNumber % lapNumberBoundary;
}

uint16_t StopWatchController::GetMaxLapNumber() {
return maxLapNumber;
}

std::optional<LapInfo> StopWatchController::GetLapFromHistory(uint8_t index) {
if (index >= histSize || history[index].number == 0) {
return {};
}
return history[index];
}

// Data / State acess

TickType_t StopWatchController::GetElapsedTime() {
if (!IsRunning()) {
return timeElapsedPreviously;
}
TickType_t delta = xTaskGetTickCount() - startTime;
return (timeElapsedPreviously + delta) % elapsedTimeBoundary;
}

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

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

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

#include <FreeRTOS.h>
#include <optional>
#include <timers.h>
#include "utility/CircularBuffer.h"

namespace Pinetime {
namespace System {
class SystemTask;
}

namespace Controllers {

enum class StopWatchStates { Cleared, Running, Paused };

struct LapInfo {
uint16_t number = 0; // Used to label the lap
TickType_t timeSinceStart = 0; // Excluding pauses
};

class StopWatchController {
public:
StopWatchController();

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

TickType_t GetElapsedTime();

// Lap functionality

/// Only the latest histSize laps are stored
void AddLapToHistory();

/// Returns maxLapNumber
uint16_t GetMaxLapNumber();

/// Indexes into lap history, with 0 being the latest lap.
std::optional<LapInfo> GetLapFromHistory(uint8_t index);

bool IsRunning();
bool IsCleared();
bool IsPaused();

private:
// Time at which stopwatch wraps around to zero (1000 hours)
static constexpr TickType_t elapsedTimeBoundary = static_cast<TickType_t>(configTICK_RATE_HZ) * 60 * 60 * 1000;
// Current state of stopwatch
StopWatchStates currentState = StopWatchStates::Cleared;
// Start time of current duration
TickType_t startTime;
// How much time was elapsed before current duration
TickType_t timeElapsedPreviously;

// Maximum number of stored laps
static constexpr uint8_t histSize = 4;
// Value at which lap numbers wrap around to zero
static constexpr uint16_t lapNumberBoundary = 1000;
// Lap storage
Utility::CircularBuffer<LapInfo, histSize> history;
// Highest lap number; less than lapNumberBoundary, may exceed histSize
uint16_t maxLapNumber;
};
}
}
2 changes: 2 additions & 0 deletions src/displayapp/Controllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Pinetime {
class MotorController;
class MotionController;
class AlarmController;
class StopWatchController;
class BrightnessController;
class SimpleWeatherService;
class FS;
Expand All @@ -42,6 +43,7 @@ namespace Pinetime {
Pinetime::Controllers::MotorController& motorController;
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::AlarmController& alarmController;
Pinetime::Controllers::StopWatchController& stopWatchController;
Pinetime::Controllers::BrightnessController& brightnessController;
Pinetime::Controllers::SimpleWeatherService* weatherController;
Pinetime::Controllers::FS& filesystem;
Expand Down
4 changes: 4 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem,
Expand All @@ -95,6 +96,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
motorController {motorController},
motionController {motionController},
alarmController {alarmController},
stopWatchController {stopWatchController},
brightnessController {brightnessController},
touchHandler {touchHandler},
filesystem {filesystem},
Expand All @@ -110,6 +112,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
motorController,
motionController,
alarmController,
stopWatchController,
brightnessController,
nullptr,
filesystem,
Expand Down Expand Up @@ -527,6 +530,7 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
batteryController,
bleController,
alarmController,
stopWatchController,
dateTimeController,
filesystem,
std::move(apps));
Expand Down
3 changes: 3 additions & 0 deletions src/displayapp/DisplayApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "displayapp/screens/Screen.h"
#include "components/timer/Timer.h"
#include "components/alarm/AlarmController.h"
#include "components/stopwatch/StopWatchController.h"
#include "touchhandler/TouchHandler.h"

#include "displayapp/Messages.h"
Expand Down Expand Up @@ -64,6 +65,7 @@ namespace Pinetime {
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::FS& filesystem,
Expand Down Expand Up @@ -94,6 +96,7 @@ namespace Pinetime {
Pinetime::Controllers::MotorController& motorController;
Pinetime::Controllers::MotionController& motionController;
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 @@ -22,6 +22,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::MotorController& /*motorController*/,
Pinetime::Controllers::MotionController& /*motionController*/,
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 @@ -32,6 +32,7 @@ namespace Pinetime {
class TouchHandler;
class MotorController;
class AlarmController;
class StopWatchController;
class BrightnessController;
class FS;
class SimpleWeatherService;
Expand All @@ -58,6 +59,7 @@ namespace Pinetime {
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
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/screens/ApplicationList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ApplicationList::ApplicationList(DisplayApp* app,
const Pinetime::Controllers::Battery& batteryController,
const Pinetime::Controllers::Ble& bleController,
const Pinetime::Controllers::AlarmController& alarmController,
const Pinetime::Controllers::StopWatchController& stopWatchController,
Controllers::DateTime& dateTimeController,
Pinetime::Controllers::FS& filesystem,
std::array<Tile::Applications, UserAppTypes::Count>&& apps)
Expand All @@ -30,6 +31,7 @@ ApplicationList::ApplicationList(DisplayApp* app,
batteryController {batteryController},
bleController {bleController},
alarmController {alarmController},
stopWatchController {stopWatchController},
dateTimeController {dateTimeController},
filesystem {filesystem},
apps {std::move(apps)},
Expand Down
2 changes: 2 additions & 0 deletions src/displayapp/screens/ApplicationList.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Pinetime {
const Pinetime::Controllers::Battery& batteryController,
const Pinetime::Controllers::Ble& bleController,
const Pinetime::Controllers::AlarmController& alarmController,
const Pinetime::Controllers::StopWatchController& stopWatchController,
Controllers::DateTime& dateTimeController,
Pinetime::Controllers::FS& filesystem,
std::array<Tile::Applications, UserAppTypes::Count>&& apps);
Expand All @@ -34,6 +35,7 @@ namespace Pinetime {
const Pinetime::Controllers::Battery& batteryController;
const Pinetime::Controllers::Ble& bleController;
const Pinetime::Controllers::AlarmController& alarmController;
const Pinetime::Controllers::StopWatchController& stopWatchController;
Controllers::DateTime& dateTimeController;
Pinetime::Controllers::FS& filesystem;
std::array<Tile::Applications, UserAppTypes::Count> apps;
Expand Down
Loading