Skip to content

Commit

Permalink
Code Clean up
Browse files Browse the repository at this point in the history
removed some unused code. Didn't reduce RAM usage.
  • Loading branch information
cyberneel committed Dec 9, 2024
1 parent 8cfabea commit 860fcb6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 115 deletions.
128 changes: 64 additions & 64 deletions src/components/infinisleep/InfiniSleepController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void InfiniSleepController::UpdateTracker() {
NRF_LOG_INFO("[InfiniSleepController] Updating tracker");

if (infiniSleepSettings.heartRateTracking) {
UpdateBPM();
// UpdateBPM();
}
systemTask->PushMessage(System::Messages::SleepTrackerUpdate);

Expand Down Expand Up @@ -200,7 +200,7 @@ void InfiniSleepController::UpdateGradualWake() {

// Calculate the period for the gradualWakeTimer
if (infiniSleepSettings.graddualWake && gradualWakeStep > 0) {
int64_t gradualWakePeriod = ((SecondsToWakeAlarm() - gradualWakeSteps[-1 + gradualWakeStep])) * (configTICK_RATE_HZ);
uint64_t gradualWakePeriod = ((SecondsToWakeAlarm() - gradualWakeSteps[-1 + gradualWakeStep])) * (configTICK_RATE_HZ);
xTimerChangePeriod(gradualWakeTimer, gradualWakePeriod, 0);
xTimerStart(gradualWakeTimer, 0);
} else {
Expand All @@ -216,70 +216,70 @@ void InfiniSleepController::StopAlerting() {

/* Sleep Tracking Section */

void InfiniSleepController::UpdateBPM() {
// Get the heart rate from the controller
prevBpm = bpm;
bpm = heartRateController.HeartRate();

if (prevBpm != 0)
rollingBpm = (rollingBpm + bpm) / 2;
else
rollingBpm = bpm;

// Get the current time from DateTimeController
int hours = dateTimeController.Hours();
int minutes = dateTimeController.Minutes();
int seconds = dateTimeController.Seconds();

// Log the BPM and current time
NRF_LOG_INFO("BPM: %d at %02d:%02d:%02d", rollingBpm, hours, minutes, seconds);

// Write data to CSV
// const int motion = 0; // Placeholder for motion data
// std::tuple<int, int, int, int, int> data[1] = {std::make_tuple(hours, minutes, seconds, bpm, motion)};
// WriteDataCSV(TRACKER_DATA_FILE_NAME, data, 1);
}

void InfiniSleepController::WriteDataCSV(const char* fileName, const std::tuple<int, int, int, int, int>* data, int dataSize) const {
lfs_file_t file;
int err = fs.FileOpen(&file, fileName, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND);
if (err < 0) {
// Handle error
NRF_LOG_INFO("Error opening file: %d", err);
return;
}

for (int i = 0; i < dataSize; ++i) {
int hours, minutes, seconds, bpm, motion;
std::tie(hours, minutes, seconds, bpm, motion) = data[i];
char buffer[64];
int len = snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d,%d,%d\n", hours, minutes, seconds, bpm, motion);
err = fs.FileWrite(&file, reinterpret_cast<const uint8_t*>(buffer), len);
if (err < 0) {
// Handle error
NRF_LOG_INFO("Error writing to file: %d", err);
fs.FileClose(&file);

return;
}
}

fs.FileClose(&file);
}
// void InfiniSleepController::UpdateBPM() {
// // Get the heart rate from the controller
// prevBpm = bpm;
// bpm = heartRateController.HeartRate();

// if (prevBpm != 0)
// rollingBpm = (rollingBpm + bpm) / 2;
// else
// rollingBpm = bpm;

// // Get the current time from DateTimeController
// int hours = dateTimeController.Hours();
// int minutes = dateTimeController.Minutes();
// int seconds = dateTimeController.Seconds();

// // Log the BPM and current time
// NRF_LOG_INFO("BPM: %d at %02d:%02d:%02d", rollingBpm, hours, minutes, seconds);

// // Write data to CSV
// // const int motion = 0; // Placeholder for motion data
// // std::tuple<int, int, int, int, int> data[1] = {std::make_tuple(hours, minutes, seconds, bpm, motion)};
// // WriteDataCSV(TRACKER_DATA_FILE_NAME, data, 1);
// }

// void InfiniSleepController::WriteDataCSV(const char* fileName, const std::tuple<int, int, int, int, int>* data, int dataSize) const {
// lfs_file_t file;
// int err = fs.FileOpen(&file, fileName, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND);
// if (err < 0) {
// // Handle error
// NRF_LOG_INFO("Error opening file: %d", err);
// return;
// }

// for (int i = 0; i < dataSize; ++i) {
// int hours, minutes, seconds, bpm, motion;
// std::tie(hours, minutes, seconds, bpm, motion) = data[i];
// char buffer[64];
// int len = snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d,%d,%d\n", hours, minutes, seconds, bpm, motion);
// err = fs.FileWrite(&file, reinterpret_cast<const uint8_t*>(buffer), len);
// if (err < 0) {
// // Handle error
// NRF_LOG_INFO("Error writing to file: %d", err);
// fs.FileClose(&file);

// return;
// }
// }

// fs.FileClose(&file);
// }

// Clear data in CSV
void InfiniSleepController::ClearDataCSV(const char* filename) const {
lfs_file_t file;
int err = fs.FileOpen(&file, filename, LFS_O_WRONLY | LFS_O_TRUNC);
if (err < 0) {
// Handle error
NRF_LOG_INFO("Error opening file: %d", err);
return;
}

fs.FileClose(&file);
NRF_LOG_INFO("CSV data cleared");
}
// void InfiniSleepController::ClearDataCSV(const char* filename) const {
// lfs_file_t file;
// int err = fs.FileOpen(&file, filename, LFS_O_WRONLY | LFS_O_TRUNC);
// if (err < 0) {
// // Handle error
// NRF_LOG_INFO("Error opening file: %d", err);
// return;
// }

// fs.FileClose(&file);
// NRF_LOG_INFO("CSV data cleared");
// }

/* Sleep Tracking Section End */

Expand Down
56 changes: 11 additions & 45 deletions src/components/infinisleep/InfiniSleepController.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,6 @@ namespace Pinetime {
void DisableTracker();
void UpdateTracker();

bool BodyTrackingEnabled() const {
return infiniSleepSettings.bodyTracking;
}

void SetBodyTrackingEnabled(bool enabled) {
infiniSleepSettings.bodyTracking = enabled;
}

bool HeartRateTrackingEnabled() const {
return infiniSleepSettings.heartRateTracking;
}

void SetHeartRateTrackingEnabled(bool enabled) {
infiniSleepSettings.heartRateTracking = enabled;
}

bool GradualWakeEnabled() const {
return infiniSleepSettings.graddualWake;
}

void SetGradualWakeEnabled(bool enabled) {
infiniSleepSettings.graddualWake = enabled;
}

bool SmartAlarmEnabled() const {
return infiniSleepSettings.smartAlarm;
}

void SetSmartAlarmEnabled(bool enabled) {
infiniSleepSettings.smartAlarm = enabled;
}

void SetSettingsChanged() {
settingsChanged = true;
}
Expand All @@ -149,14 +117,12 @@ namespace Pinetime {
WakeAlarmSettings wakeAlarm;

// Dertermine the steps for the gradual wake alarm, the corresponding vibration durations determine the power of the vibration
static constexpr uint16_t gradualWakeSteps[9] = {30, 60, 90, 120, 180, 240, 300, 350, 600}; // In seconds
static constexpr uint16_t gradualWakeVibrationDurations[9] = {1000, 1000, 900, 800, 800, 700, 700, 700, 500}; // In ms
static constexpr uint16_t gradualWakeSteps[9] = {30, 60, 90, 120, 180, 240, 300, 350, 600}; // In seconds

int8_t gradualWakeStep = 9; // used to keep track of which step to use, in position form not idex
uint8_t gradualWakeStep = 9; // used to keep track of which step to use, in position form not idex

uint16_t GetSleepCycles() {
uint16_t totalMinutes = GetTotalSleep();
return (totalMinutes * 100 / infiniSleepSettings.sleepCycleDuration);
uint16_t GetSleepCycles() const {
return (GetTotalSleep() * 100 / infiniSleepSettings.sleepCycleDuration);
}

uint16_t GetTotalSleep() const {
Expand All @@ -177,7 +143,7 @@ namespace Pinetime {
return sleepMinutes;
}

uint16_t GetSuggestedSleepTime() {
uint16_t GetSuggestedSleepTime() const {
return infiniSleepSettings.desiredCycles * infiniSleepSettings.sleepCycleDuration;
}

Expand Down Expand Up @@ -230,10 +196,6 @@ namespace Pinetime {
return dateTimeController.Minutes();
}

int bpm = 0;
int prevBpm = 0;
int rollingBpm = 0;

void UpdateBPM();

uint8_t GetGradualWakeStep() const {
Expand All @@ -247,6 +209,10 @@ namespace Pinetime {
bool isEnabled = false;
bool settingsChanged = false;

// uint8_t bpm = 0;
// uint8_t prevBpm = 0;
// uint8_t rollingBpm = 0;

Controllers::DateTime& dateTimeController;
Controllers::FS& fs;
Controllers::HeartRateController& heartRateController;
Expand All @@ -262,8 +228,8 @@ namespace Pinetime {
void SavePrevSessionData() const;

// For File IO
void WriteDataCSV(const char* fileName, const std::tuple<int, int, int, int, int>* data, int dataSize) const;
void ClearDataCSV(const char* fileName) const;
// void WriteDataCSV(const char* fileName, const std::tuple<int, int, int, int, int>* data, int dataSize) const;
// void ClearDataCSV(const char* fileName) const;
};
}

Expand Down
3 changes: 0 additions & 3 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,6 @@ void DisplayApp::Refresh() {
if (infiniSleepController.IsEnabled()) {
if (currentApp != Apps::Sleep) {
LoadNewScreen(Apps::Sleep, DisplayApp::FullRefreshDirections::Up);
// Wait for the sleep app to load before moving on.
while (!lv_task_handler()) {
};
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/displayapp/screens/Sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ namespace {
}
}


Sleep::Sleep(Controllers::InfiniSleepController& infiniSleepController,
Controllers::Settings::ClockType clockType,
System::SystemTask& systemTask,
Expand All @@ -52,7 +51,7 @@ Sleep::Sleep(Controllers::InfiniSleepController& infiniSleepController,
clockType {clockType},
displayApp {displayApp} {

infiniSleepController.SetHeartRateTrackingEnabled(false);
infiniSleepController.infiniSleepSettings.heartRateTracking = false;
infiniSleepController.SetSettingsChanged();
UpdateDisplay();
taskRefresh = lv_task_create(RefreshTaskCallback, 2000, LV_TASK_PRIO_MID, this);
Expand Down Expand Up @@ -494,7 +493,9 @@ void Sleep::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
infiniSleepController.infiniSleepSettings.graddualWake = true;
}
infiniSleepController.SetSettingsChanged();
const char* mode = infiniSleepController.infiniSleepSettings.graddualWake ? "Grad." : infiniSleepController.infiniSleepSettings.naturalWake ? "Nat." : "Off";
const char* mode = infiniSleepController.infiniSleepSettings.graddualWake ? "Grad."
: infiniSleepController.infiniSleepSettings.naturalWake ? "Nat."
: "Off";
lv_label_set_text_static(lv_obj_get_child(obj, nullptr), mode);
return;
}
Expand Down

0 comments on commit 860fcb6

Please sign in to comment.