Full color support, rework the directory structure
This commit is contained in:
223
firmware/Bsp/Time.h
Normal file
223
firmware/Bsp/Time.h
Normal file
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Max Regan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Common {
|
||||
|
||||
using time_t = uint64_t;
|
||||
|
||||
class Time final {
|
||||
public:
|
||||
static constexpr uint64_t MILLIS_PER_SEC = 1e3;
|
||||
static constexpr uint64_t MICROS_PER_SEC = 1e6;
|
||||
static constexpr uint64_t NANOS_PER_SEC = 1e9;
|
||||
|
||||
static constexpr uint64_t MICROS_PER_MILLI = 1e3;
|
||||
static constexpr uint64_t NANOS_PER_MILLI = 1e6;
|
||||
|
||||
static constexpr uint64_t NANOS_PER_MICRO = 1e3;
|
||||
|
||||
static inline time_t nanos(uint64_t value)
|
||||
{
|
||||
return value / NANOS_PER_MICRO;
|
||||
}
|
||||
|
||||
static inline time_t micros(uint64_t value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
static inline time_t millis(uint64_t value)
|
||||
{
|
||||
return value * MICROS_PER_MILLI;
|
||||
}
|
||||
|
||||
static inline time_t seconds(uint64_t value)
|
||||
{
|
||||
return value * MICROS_PER_SEC;
|
||||
}
|
||||
|
||||
static inline uint64_t to_nanos(time_t value)
|
||||
{
|
||||
return value * NANOS_PER_MICRO;
|
||||
}
|
||||
|
||||
static inline uint64_t to_micros(time_t value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
static inline uint64_t to_millis(time_t value)
|
||||
{
|
||||
return value / MICROS_PER_MILLI;
|
||||
}
|
||||
|
||||
static inline uint64_t to_seconds(time_t value)
|
||||
{
|
||||
return value / MICROS_PER_SEC;
|
||||
}
|
||||
};
|
||||
|
||||
class WallClockTime {
|
||||
|
||||
// TODO: Implement a saturating counter?
|
||||
|
||||
public:
|
||||
WallClockTime()
|
||||
: m_hours(0)
|
||||
, m_minutes(0)
|
||||
, m_seconds(0)
|
||||
{}
|
||||
|
||||
WallClockTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
|
||||
: m_hours(hours)
|
||||
, m_minutes(minutes)
|
||||
, m_seconds(seconds)
|
||||
{}
|
||||
|
||||
static inline uint8_t hour24_to_hour12(uint8_t hour24) {
|
||||
if (hour24 == 0) {
|
||||
return 12;
|
||||
} else if (hour24 > 12) {
|
||||
return hour24 - 12;
|
||||
} else {
|
||||
return hour24;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint8_t hour24_is_am(uint8_t hour24) {
|
||||
return hour24 >= 12;
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_12() const {
|
||||
return hour24_to_hour12(m_hours);
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_12_tens() const {
|
||||
return get_hours_12() / 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_12_ones() const {
|
||||
return get_hours_12() % 10;
|
||||
}
|
||||
|
||||
inline bool get_is_pm() const {
|
||||
return m_hours >= 12;
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_24() const {
|
||||
return m_hours;
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_24_ones() const {
|
||||
return m_hours % 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_hours_24_tens() const {
|
||||
return m_hours / 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_minutes() const {
|
||||
return m_minutes;
|
||||
}
|
||||
|
||||
inline uint8_t get_minutes_ones() const {
|
||||
return m_minutes % 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_minutes_tens() const {
|
||||
return m_minutes / 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_seconds() const {
|
||||
return m_seconds;
|
||||
}
|
||||
|
||||
inline uint8_t get_seconds_ones() const {
|
||||
return m_seconds % 10;
|
||||
}
|
||||
|
||||
inline uint8_t get_seconds_tens() const {
|
||||
return m_seconds / 10;
|
||||
}
|
||||
|
||||
inline void increment_hours() {
|
||||
m_hours++;
|
||||
if (m_hours >= 24) {
|
||||
m_hours = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline void increment_minutes() {
|
||||
m_minutes++;
|
||||
if (m_minutes >= 60) {
|
||||
m_minutes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline void increment_seconds() {
|
||||
m_seconds++;
|
||||
if (m_seconds >= 60) {
|
||||
m_seconds = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline void decrement_hours() {
|
||||
if (m_hours == 0) {
|
||||
m_hours = 23;
|
||||
} else {
|
||||
m_hours--;
|
||||
}
|
||||
}
|
||||
|
||||
inline void decrement_minutes() {
|
||||
if (m_minutes == 0) {
|
||||
m_minutes = 59;
|
||||
} else {
|
||||
m_minutes--;
|
||||
}
|
||||
}
|
||||
|
||||
inline void decrement_seconds() {
|
||||
if (m_seconds == 0) {
|
||||
m_seconds = 59;
|
||||
} else {
|
||||
m_seconds--;
|
||||
}
|
||||
}
|
||||
|
||||
inline void toggle_am_pm() {
|
||||
if (m_hours < 12) {
|
||||
m_hours += 12;
|
||||
} else {
|
||||
m_hours -= 12;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t m_hours;
|
||||
uint8_t m_minutes;
|
||||
uint8_t m_seconds;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user