Skip to content
Closed
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 @@ -482,6 +482,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 @@ -552,6 +553,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 @@ -660,6 +662,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 @@ -10,6 +10,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 @@ -95,6 +96,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 @@ -110,6 +112,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
motionController {motionController},
timerController {timerController},
alarmController {alarmController},
stopWatchController {stopWatchController},
touchHandler {touchHandler} {
}

Expand Down Expand Up @@ -424,7 +427,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){ Start(); };
Expand Down
Loading