Skip to content

Commit

Permalink
Small clean up of old code
Browse files Browse the repository at this point in the history
More documentation
  • Loading branch information
florianL21 committed Mar 31, 2021
1 parent d3344d5 commit 43e56fb
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 85 deletions.
11 changes: 5 additions & 6 deletions lib/LED_clock/Config/Blynk/default/BlynkConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
if(blynkC->blynkUIUpdateRequired == true)
{
blynkC->blynkUIUpdateRequired = false;
if(ClockS->MainState == ClockState::CLOCK_MODE)
if(ClockS->getMode() == ClockState::CLOCK_MODE)
{
Blynk.virtualWrite(BLYNK_CHANNEL_TIMER_START_BUTTON, 0);
}
else if(ClockS->MainState == ClockState::ALARM_NOTIFICATION)
else if(ClockS->getMode() == ClockState::ALARM_NOTIFICATION)
{
Blynk.setProperty(BLYNK_CHANNEL_ALARM_START_BUTTON, "onLabel", "Clear");
}
Expand Down Expand Up @@ -313,16 +313,15 @@
{
TimeM->startTimer();
Serial.println("Timer Started");
ClockS->MainState = ClockState::TIMER_MODE;
ClockS->switchMode(ClockState::TIMER_MODE);
}
else
{
TimeM->stopTimer();
Serial.println("Timer Stopped");
Blynk.syncVirtual(BLYNK_CHANNEL_TIMER_TIME_INPUT);
ClockS->alarmToggleCount = 0;
BlynkC->ShelfDisplays->setGlobalBrightness(ClockS->clockBrightness);
ClockS->MainState = ClockState::CLOCK_MODE;
ClockS->switchMode(ClockState::CLOCK_MODE);
}
}

Expand Down Expand Up @@ -393,7 +392,7 @@
*/
BLYNK_WRITE(BLYNK_CHANNEL_ALARM_START_BUTTON)
{
if(ClockS->MainState == ClockState::ALARM_NOTIFICATION)
if(ClockS->getMode() == ClockState::ALARM_NOTIFICATION)
{
Blynk.setProperty(BLYNK_CHANNEL_ALARM_START_BUTTON, "onLabel", "Deactivate");
TimeM->clearAlarm();
Expand Down
89 changes: 74 additions & 15 deletions lib/LED_clock/Modules/ClockState/inc/ClockState.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* \file ClockState.h
* \author Florian Laschober
* \brief Header for class definition of the #ClockState which is used to keep track of values between modules
*/

#ifndef _CLOCK_STATE_H_
#define _CLOCK_STATE_H_

Expand All @@ -6,32 +12,85 @@
#include "TimeManager.h"
#include "DisplayManager.h"

/**
* \brief The clockState is responsible to hold all the data that needs to be communicated between components.
* It can be imagined as kind of like an "object oriented global variable"
*/
class ClockState
{
public:
/**
* \brief Avaliable clock modes each with a different behaviour
*/
enum ClockStates {CLOCK_MODE, TIMER_MODE, TIMER_NOTIFICATION, ALARM_NOTIFICATION};
ClockStates MainState;
private:
TimeManager* timeM;
DisplayManager* ShelfDisplays;
static ClockState* instance;
unsigned long lastDotFlash;
ClockStates MainState;
uint16_t alarmToggleCount;
unsigned long lastUpdateMillis;
bool currentAlarmSignalState;
bool isinNightMode;

ClockState();
public:

/**
* \brief Base brightness of the clock. The actual brightness can still change if a light sensor is used
*/
uint8_t clockBrightness;
uint16_t alarmToggleCount;

/**
* \brief Brightness of the clock driing nighttime hours define by #ClockState::NightModeStartTime and
* #ClockState::NightModeStopTime
*/
uint8_t nightModeBrightness;

/**
* \brief Any time after will be considered nighttime as long as it is still lower than #ClockState::NightModeStopTime
*/
TimeManager::TimeInfo NightModeStartTime;

/**
* \brief Any time before will be considered nighttime as long as it is still higher than #ClockState::NightModeStartTime
*/
TimeManager::TimeInfo NightModeStopTime;
uint8_t nightModeBrightness;

unsigned long lastMillis;
bool currentAlarmSignalState;
bool isinNightMode;
/**
* \brief defines the number of dots.
* \range 0 -> no dot; 1 -> one dot; 2 -> two dots; other-> one dot
*/
uint8_t numDots;

/**
* \brief Get the instance of the Clock object or create it if it was not yet instantiated.
*
* \return ClockState* returns the address to the ClockState object
*/
static ClockState* getInstance();

/**
* \brief Destroys the ClockState object and cause #ClockState::getInstance to create a new object the next time it is called
*/
~ClockState();
static ClockState* getInstance();
void handleStates();
private:
TimeManager* timeM;
DisplayManager* ShelfDisplays;
static ClockState* instance;
unsigned long lastDotFlash;

ClockState();
/**
* \brief Switch the current mode of the clock
*/
void switchMode(ClockStates newState);

/**
* \brief Returns the current mode of the clock
*/
ClockStates getMode();

/**
* \brief Has to be called periodically to update the screen and process state transitions within the state machine.
*
*/
void handleStates();
};

#endif
#endif
20 changes: 17 additions & 3 deletions lib/LED_clock/Modules/ClockState/src/ClockState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ClockState::ClockState()
nightModeBrightness = 0;
numDots = NUM_SEPERATION_DOTS;

lastMillis = millis();
lastUpdateMillis = millis();
lastDotFlash = millis();
currentAlarmSignalState = false;
isinNightMode = false;
Expand All @@ -36,11 +36,25 @@ ClockState* ClockState::getInstance()
return instance;
}

void ClockState::switchMode(ClockStates newState)
{
if(newState == ClockState::CLOCK_MODE)
{
alarmToggleCount = 0;
}
MainState = newState;
}

ClockState::ClockStates ClockState::getMode()
{
return MainState;
}

void ClockState::handleStates()
{
if(lastMillis + TIME_UPDATE_INTERVALL <= millis()) // update the display only in a certain intervall
if(lastUpdateMillis + TIME_UPDATE_INTERVALL <= millis()) // update the display only in a certain intervall
{
lastMillis = millis();
lastUpdateMillis = millis();
TimeManager::TimeInfo currentTime;
currentTime = timeM->getCurrentTime();
switch (MainState)
Expand Down
63 changes: 48 additions & 15 deletions lib/LED_clock/Modules/DisplayManager/inc/DisplayManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include "TimeManager.h"
#include "Configuration.h"
#include "LinkedList.h"
#include "misc.h"
#include "Animations.h"

/**
* \brief Macro to shorten then name of the function to make usage easier in the animation config files.
*/
#define SEGMENT(POSITION, DISPLAY) DisplayManager::getGlobalSegmentIndex(POSITION, DISPLAY)

/**
Expand All @@ -29,8 +31,6 @@
*/
class DisplayManager
{
public:

private:
//segment configurations
static SevenSegment::SegmentPosition SegmentPositions[NUM_SEGMENTS];
Expand All @@ -49,6 +49,7 @@ class DisplayManager
uint8_t LEDBrightnessSetPoint;
uint8_t LEDBrightnessCurrent;
uint64_t lastBrightnessChange;
CubicEase* lightSensorEasing;

typedef struct {
SegmentPositions_t segmentPosition;
Expand All @@ -73,12 +74,20 @@ class DisplayManager

DisplayManager();
public:
/**
* \brief Destroys the Display Manager object and cause #DisplayManager::getInstance to create a new object the next time it is called
*/
~DisplayManager();

/**
* \brief Get the instance of the DisplayManager object or create it if it was not yet instantiated.
*
* \return DisplayManager* returns the address to the DisplayManager object
*/
static DisplayManager* getInstance();

/**
* \brief Initialize all the segment using the configutration from DisplayConfiguration.cpp
* \brief Initialize all the segment using the configuration from \ref DisplayConfiguration.cpp
* \param indexOfFirstLed Index of the first led in the string that is part of a segment (usually 0)
* \param ledsPerSegment Sets the number of LEDs that are in one segment. this will be the same for all segments
* \param initialColor Sets the initial color of all the segments. This does not switch any segments on by it's own
Expand All @@ -87,25 +96,25 @@ class DisplayManager
void InitSegments(uint16_t indexOfFirstLed, uint8_t ledsPerSegment, CRGB initialColor, uint8_t initBrightness = 128);

/**
* \brief Sets the color of all segments and updates it immediatley for all segments that are currently switched on
* \brief Sets the color of all segments and updates it immediately for all segments that are currently switched on
* \param color Color to set the LEDs to
*/
void setAllSegmentColors(CRGB color);

/**
* \brief Sets the color of the the segments which are displaying hours and updates it immediatley for all segments that are currently switched on
* \brief Sets the color of the the segments which are displaying hours and updates it immediately for all segments that are currently switched on
* \param color Color to set the LEDs to
*/
void setHourSegmentColors(CRGB color);

/**
* \brief Sets the color of the the segments which are displaying minutes and updates it immediatley for all segments that are currently switched on
* \brief Sets the color of the the segments which are displaying minutes and updates it immediately for all segments that are currently switched on
* \param color Color to set the LEDs to
*/
void setMinuteSegmentColors(CRGB color);

/**
* \brief Displays the numbers given as they are on the crespective displays
* \brief Displays the numbers given as they are on the respective displays
* \param Hour Number to show on the hours display
* \param Minute Number to show on the minutes display
*/
Expand All @@ -129,17 +138,17 @@ class DisplayManager
void displayTimer(uint8_t hours, uint8_t minutes, uint8_t seconds);

/**
* \brief Has to be called in the cyclicly loop to enable live updating of the LEDs
* \brief Has to be called cyclicly in the loop to enable live updating of the LEDs
*/
void handle();

/**
* \brief Sets the color of the interrior LEDs and displays it immediatley
* \brief Sets the color of the interrior LEDs and displays it immediately
*/
void setInternalLEDColor(CRGB color);

/**
* \brief Sets the color of the seperation dot LEDs and displays it immediatley
* \brief Sets the color of the seperation dot LEDs and displays it immediately
*/
void setDotLEDColor(CRGB color);

Expand All @@ -149,7 +158,8 @@ class DisplayManager
void showLoadingAnimation();

/**
* \brief Stops the currently running animation after it is finished. This causes a looping animation to stop after its current cycle
* \brief Stops the currently running animation after it is finished.
* This causes a looping animation to stop after its current cycle.
*/
void stopLoadingAnimation();

Expand All @@ -163,6 +173,11 @@ class DisplayManager
*/
void turnAllSegmentsOff();

/**
* \brief Turn off all LEDs including internal LEDs
*/
void turnAllLEDsOff();

/**
* \brief Display a progress bar on the LEDs
* \param progress How much progress was done already
Expand All @@ -175,6 +190,7 @@ class DisplayManager
* \param timeInMs Delay time in ms
*/
void delay(uint32_t timeInMs);

/**
* \brief Sets the Brightness globally for all leds
* \param brightness value between 0 for lowest, and 255 for the highes brightness
Expand All @@ -183,17 +199,34 @@ class DisplayManager
void setGlobalBrightness(uint8_t brightness, bool enableSmoothTransition = true);

/**
* \brief Briefley Flash the dot in the middle of the clock face
* \brief Calling the Flash dot animation for the appropriate segments in the middle of the clock face
*/
void flashSeperationDot(uint8_t numDots);

/**
* \brief Used for testing purposes
*/
void test();

/**
* \brief get the index of a segment in regards to it's position on the clock face.
* This makes writing animations a lot easier as it will act as an abstraction layer between the animation
* config and the display config
*
* \param segmentPosition Position of a segment in the seven segment display
* \param Display Which display should be targeted
* \return int16_t index of the Segment in the #DisplayManager::SegmentPositions array
*/
static int16_t getGlobalSegmentIndex(SegmentPositions_t segmentPosition, DisplayIDs Display);

/**
* \brief Debugging function which will print out any errors that occurred when using the
* #DisplayManager::getGlobalSegmentIndex function.
* Especially useful for debugging weirdly behaving animations as animations will be configured before the
* Serial connection is up. If an error was detected by the #DisplayManager::getGlobalSegmentIndex function a
* message will be added to a buffer which can then be printed out using this function.
*/
static void printAnimationInitErrors();

void turnAllLEDsOff();
};


Expand Down

0 comments on commit 43e56fb

Please sign in to comment.