Add Stopwatch application
It can be started, stopped, and cleared. It also continues to run if you leave the screen.
This commit is contained in:
@@ -100,7 +100,7 @@ public:
|
||||
void render() {
|
||||
const struct font &font = default_font;
|
||||
|
||||
//m_driver.clear();
|
||||
m_driver.clear();
|
||||
|
||||
uint32_t x = 0;
|
||||
uint32_t y = 0;
|
||||
@@ -124,6 +124,16 @@ public:
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
BSP::ReturnCode add_item(MenuScreenItem item) {
|
||||
if (m_num_items == MAX_ITEMS) {
|
||||
return BSP::ReturnCode::FAIL;
|
||||
}
|
||||
|
||||
m_items[m_num_items++] = item;
|
||||
|
||||
return BSP::ReturnCode::OK;
|
||||
}
|
||||
|
||||
BSP::ReturnCode init() {
|
||||
return BSP::ReturnCode::OK;
|
||||
}
|
||||
@@ -134,7 +144,6 @@ public:
|
||||
|
||||
void enable() override {
|
||||
m_selected = 0;
|
||||
m_driver.clear();
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,6 @@ ReturnCode SetTimeScreen::init()
|
||||
|
||||
NextTime SetTimeScreen::execute()
|
||||
{
|
||||
//TODO: Fix this so it doesn't constantly refresh
|
||||
refresh();
|
||||
|
||||
return NextTime::never();
|
||||
|
||||
173
firmware/Application/Screens/StopwatchScreen.cpp
Normal file
173
firmware/Application/Screens/StopwatchScreen.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "Application/Screens/StopwatchScreen.h"
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
|
||||
using BSP::ReturnCode;
|
||||
using BSP::Time;
|
||||
using BSP::Schedule::NextTime;
|
||||
|
||||
StopwatchScreen::StopwatchScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
: m_display(display)
|
||||
, m_manager(manager)
|
||||
, m_is_running(false)
|
||||
, m_accumulated_time()
|
||||
, m_last_time()
|
||||
, m_font(default_font)
|
||||
|
||||
{}
|
||||
|
||||
ReturnCode StopwatchScreen::init()
|
||||
{
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime StopwatchScreen::execute()
|
||||
{
|
||||
refresh();
|
||||
|
||||
if (m_is_running) {
|
||||
add_time();
|
||||
}
|
||||
|
||||
return NextTime::asap();
|
||||
}
|
||||
|
||||
static char get_char_for_digit(uint8_t bcd_digit)
|
||||
{
|
||||
return (bcd_digit % 10) + '0';
|
||||
}
|
||||
|
||||
void StopwatchScreen::display_number(uint32_t *x, uint32_t y,
|
||||
uint32_t tens, uint32_t ones)
|
||||
{
|
||||
char time_str[3] = { 0 };
|
||||
time_str[0] = get_char_for_digit(tens);
|
||||
time_str[1] = get_char_for_digit(ones);
|
||||
time_str[2] = '\0';
|
||||
|
||||
m_display.string_at(x, y, time_str, &m_font);
|
||||
}
|
||||
|
||||
void StopwatchScreen::display_number(uint32_t *x, uint32_t y,
|
||||
uint32_t hundreds, uint32_t tens, uint32_t ones)
|
||||
{
|
||||
char time_str[4] = { 0 };
|
||||
time_str[0] = get_char_for_digit(hundreds);
|
||||
time_str[1] = get_char_for_digit(tens);
|
||||
time_str[2] = get_char_for_digit(ones);
|
||||
time_str[3] = '\0';
|
||||
|
||||
m_display.string_at(x, y, time_str, &m_font);
|
||||
}
|
||||
|
||||
void StopwatchScreen::refresh()
|
||||
{
|
||||
m_display.clear();
|
||||
|
||||
//uint32_t width = m_display.get_width();
|
||||
uint32_t x = (m_display.get_width() - m_font.width * 7) / 2;
|
||||
uint32_t y = (m_display.get_height() - m_font.height * 2) / 2;
|
||||
|
||||
uint64_t total_millis = BSP::Time::to_millis(m_accumulated_time);
|
||||
uint32_t millis = total_millis % 1000;
|
||||
|
||||
uint64_t total_seconds = total_millis / 1000;
|
||||
uint32_t seconds = total_seconds % 60;
|
||||
|
||||
uint64_t total_minutes = total_seconds / 60;
|
||||
uint32_t minutes = total_minutes % 60;
|
||||
|
||||
uint64_t hours = total_minutes / 60;
|
||||
|
||||
display_number(&x, y,
|
||||
hours / 100, hours / 10, hours);
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
minutes / 10, minutes % 10);
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
y += m_font.height;
|
||||
x = (m_display.get_width() - 6 * m_font.width) / 2 ;
|
||||
|
||||
display_number(&x, y,
|
||||
seconds / 10, seconds);
|
||||
|
||||
m_display.string_at(&x, y, ".", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
millis / 100, millis / 10, millis);
|
||||
|
||||
m_display.refresh();
|
||||
}
|
||||
|
||||
void StopwatchScreen::add_time()
|
||||
{
|
||||
BSP::time_t time;
|
||||
|
||||
BSP::SystemTimer::get_time(time);
|
||||
|
||||
m_accumulated_time += time - m_last_time;
|
||||
|
||||
m_last_time = time;
|
||||
}
|
||||
|
||||
void StopwatchScreen::notify_up_button()
|
||||
{
|
||||
m_manager.pop_screen();
|
||||
return;
|
||||
}
|
||||
|
||||
void StopwatchScreen::notify_middle_button()
|
||||
{
|
||||
BSP::SystemTimer::get_time(m_last_time);
|
||||
m_accumulated_time = 0;
|
||||
}
|
||||
|
||||
void StopwatchScreen::notify_down_button()
|
||||
{
|
||||
if (!m_is_running) {
|
||||
// Start
|
||||
BSP::SystemTimer::get_time(m_last_time);
|
||||
} else {
|
||||
// Stop
|
||||
add_time();
|
||||
}
|
||||
|
||||
m_is_running = !m_is_running;
|
||||
return;
|
||||
}
|
||||
|
||||
void StopwatchScreen::enable()
|
||||
{
|
||||
}
|
||||
|
||||
void StopwatchScreen::disable()
|
||||
{
|
||||
}
|
||||
59
firmware/Application/Screens/StopwatchScreen.h
Normal file
59
firmware/Application/Screens/StopwatchScreen.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 "Application/ScreenManager.h"
|
||||
#include "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
#include "Bsp/Time.h"
|
||||
|
||||
class StopwatchScreen : public Screen {
|
||||
public:
|
||||
|
||||
StopwatchScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager);
|
||||
|
||||
BSP::ReturnCode init();
|
||||
BSP::Schedule::NextTime execute() override;
|
||||
|
||||
void enable() override;
|
||||
void disable() override;
|
||||
void notify_up_button() override;
|
||||
void notify_middle_button() override;
|
||||
void notify_down_button() override;
|
||||
|
||||
private:
|
||||
|
||||
void display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones);
|
||||
void display_number(uint32_t *x, uint32_t y, uint32_t hundreds, uint32_t tens, uint32_t ones);
|
||||
void refresh();
|
||||
void add_time();
|
||||
|
||||
BSP::DisplayDriver &m_display;
|
||||
ScreenManager &m_manager;
|
||||
|
||||
bool m_is_running;
|
||||
BSP::time_t m_accumulated_time;
|
||||
BSP::time_t m_last_time;
|
||||
const struct font &m_font;
|
||||
};
|
||||
Reference in New Issue
Block a user