refactor tree, add ecad, mcad
This commit is contained in:
191
firmware/Application/Screens/AnalogTimeScreen.cpp
Normal file
191
firmware/Application/Screens/AnalogTimeScreen.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* 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 <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "Application/Screens/AnalogTimeScreen.h"
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/LowPower.h"
|
||||
|
||||
using BSP::ReturnCode;
|
||||
using BSP::Time;
|
||||
using BSP::Schedule::NextTime;
|
||||
using Color = BSP::DisplayDriver::Color;
|
||||
|
||||
// TODO: remove all calls to std::cos and std::sin?
|
||||
|
||||
AnalogTimeScreen::AnalogTimeScreen(BSP::DisplayDriver &driver,
|
||||
ScreenManager &manager,
|
||||
Screen &menu_screen)
|
||||
: m_driver(driver)
|
||||
, m_last_time()
|
||||
, m_manager(manager)
|
||||
, m_menu_screen(menu_screen)
|
||||
, m_display_seconds(true)
|
||||
{}
|
||||
|
||||
ReturnCode AnalogTimeScreen::init()
|
||||
{
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::draw_ticks() {
|
||||
|
||||
// After profiling, using lookup tables is 6-10% faster than using std::sin & std::cos.
|
||||
static constexpr float sin_tbl[8] = {
|
||||
0.0,
|
||||
0.10452539173303049,
|
||||
0.20790564888802265,
|
||||
0.30900818248165035,
|
||||
0.4067253572759511,
|
||||
0.4999866265466325,
|
||||
0.5877702605258084,
|
||||
0.6691145400274635,
|
||||
};
|
||||
|
||||
static constexpr float cos_tbl[8] = {
|
||||
1.0,
|
||||
0.9945222181947755,
|
||||
0.9781488849661131,
|
||||
0.9510593794077146,
|
||||
0.9135504823209005,
|
||||
0.8660331248136633,
|
||||
0.8090278863187741,
|
||||
0.7431592913526923,
|
||||
};
|
||||
|
||||
const uint32_t y_center = m_driver.get_height() / 2;
|
||||
const uint32_t x_center = m_driver.get_width() / 2;
|
||||
const uint32_t radius = std::min(m_driver.get_width(), m_driver.get_height()) / 2;
|
||||
|
||||
// This only works for square screens, but saves a a bunch of float math and calls to trig functions
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
const uint32_t len = (i % 5 == 0) ? 10 : 5;
|
||||
const uint32_t width = (i % 5 == 0) ? 3 : 1;
|
||||
const uint32_t end1_offset = sin_tbl[i] * radius;
|
||||
const uint32_t end2_offset = cos_tbl[i] * radius;
|
||||
const uint32_t start1_offset = sin_tbl[i] * (radius - len);
|
||||
const uint32_t start2_offset = cos_tbl[i] * (radius - len);
|
||||
|
||||
m_driver.draw_line(x_center + start1_offset, y_center + start2_offset,
|
||||
x_center + end1_offset, y_center + end2_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center - start1_offset, y_center + start2_offset,
|
||||
x_center - end1_offset, y_center + end2_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center + start1_offset, y_center - start2_offset,
|
||||
x_center + end1_offset, y_center - end2_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center - start1_offset, y_center - start2_offset,
|
||||
x_center - end1_offset, y_center - end2_offset,
|
||||
Color::BLACK, width);
|
||||
|
||||
m_driver.draw_line(x_center + start2_offset, y_center + start1_offset,
|
||||
x_center + end2_offset, y_center + end1_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center - start2_offset, y_center + start1_offset,
|
||||
x_center - end2_offset, y_center + end1_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center + start2_offset, y_center - start1_offset,
|
||||
x_center + end2_offset, y_center - end1_offset,
|
||||
Color::BLACK, width);
|
||||
m_driver.draw_line(x_center - start2_offset, y_center - start1_offset,
|
||||
x_center - end2_offset, y_center - end1_offset,
|
||||
Color::BLACK, width);
|
||||
}
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::draw_hand(uint32_t ticks, uint32_t len, int32_t width, Color color) {
|
||||
|
||||
const uint32_t y_center = m_driver.get_height() / 2;
|
||||
const uint32_t x_center = m_driver.get_width() / 2;
|
||||
|
||||
uint32_t x_end = x_center + std::sin(ticks * TURNS_PER_TICK) * len;
|
||||
uint32_t y_end = y_center - std::cos(ticks * TURNS_PER_TICK) * len;
|
||||
|
||||
m_driver.draw_line(x_center, y_center, x_end, y_end, color, width);
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::display_time()
|
||||
{
|
||||
BSP::WallClockTime time;
|
||||
BSP::RtcDriver::get_time(time);
|
||||
|
||||
m_driver.clear(Color::WHITE);
|
||||
|
||||
draw_ticks();
|
||||
|
||||
const uint32_t seconds_len = std::min(m_driver.get_width(), m_driver.get_height()) * 9 / 20;
|
||||
const uint32_t minutes_len = std::min(m_driver.get_width(), m_driver.get_height()) * 17 / 40;
|
||||
const uint32_t hours_len = std::min(m_driver.get_width(), m_driver.get_height()) * 6 / 20;
|
||||
|
||||
draw_hand(time.get_hours_12() * 5, hours_len, 3, Color::BLACK);
|
||||
draw_hand(time.get_minutes(), minutes_len, 3, Color::BLACK);
|
||||
|
||||
if (m_display_seconds) {
|
||||
draw_hand(time.get_seconds(), seconds_len, 2, Color::RED);
|
||||
}
|
||||
|
||||
m_last_time = time;
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
NextTime AnalogTimeScreen::execute()
|
||||
{
|
||||
display_time();
|
||||
|
||||
BSP::time_t now;
|
||||
BSP::SystemTimer::get_time(now);
|
||||
if (m_display_seconds) {
|
||||
return NextTime::in(Time::seconds(1));
|
||||
} else {
|
||||
|
||||
BSP::WallClockTime wall_time;
|
||||
BSP::RtcDriver::get_time(wall_time);
|
||||
return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AnalogTimeScreen::enable() {
|
||||
m_last_time = {};
|
||||
display_time();
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::disable() {
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::notify_up_button() {
|
||||
/* TODO: This should open a menu first */
|
||||
m_manager.push_screen(m_menu_screen);
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::notify_middle_button() {
|
||||
|
||||
}
|
||||
|
||||
void AnalogTimeScreen::notify_down_button() {
|
||||
|
||||
}
|
||||
61
firmware/Application/Screens/AnalogTimeScreen.h
Normal file
61
firmware/Application/Screens/AnalogTimeScreen.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 "Bsp/macros.h"
|
||||
|
||||
#include "Application/ScreenManager.h"
|
||||
#include "Application/Screens/Screen.h"
|
||||
|
||||
class AnalogTimeScreen : public Screen {
|
||||
public:
|
||||
|
||||
AnalogTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager,
|
||||
Screen &m_menu_screen);
|
||||
|
||||
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:
|
||||
|
||||
static constexpr float TURNS_PER_TICK = 2 * 3.1415 / 60;
|
||||
|
||||
void draw_hand(uint32_t ticks, uint32_t len, int32_t width, BSP::DisplayDriver::Color color);
|
||||
void draw_ticks();
|
||||
void display_time();
|
||||
|
||||
BSP::DisplayDriver &m_driver;
|
||||
BSP::WallClockTime m_last_time;
|
||||
|
||||
ScreenManager &m_manager;
|
||||
Screen &m_menu_screen;
|
||||
|
||||
const bool m_display_seconds;
|
||||
};
|
||||
@@ -19,18 +19,18 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "Application/Screens/DisplayTimeScreen.h"
|
||||
#include "Application/Screens/BigDigitalTimeScreen.h"
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/LowPower.h"
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Time;
|
||||
using Common::Schedule::NextTime;
|
||||
using BSP::ReturnCode;
|
||||
using BSP::Time;
|
||||
using BSP::Schedule::NextTime;
|
||||
using Color = BSP::DisplayDriver::Color;
|
||||
|
||||
DisplayTimeScreen::DisplayTimeScreen(BSP::DisplayDriver &driver,
|
||||
BigDigitalTimeScreen::BigDigitalTimeScreen(BSP::DisplayDriver &driver,
|
||||
ScreenManager &manager,
|
||||
Screen &menu_screen)
|
||||
: m_driver(driver)
|
||||
@@ -48,14 +48,12 @@ static char get_char_for_digit(uint8_t bcd_digit)
|
||||
return bcd_digit + '0';
|
||||
}
|
||||
|
||||
ReturnCode DisplayTimeScreen::init()
|
||||
ReturnCode BigDigitalTimeScreen::init()
|
||||
{
|
||||
SET(RCC->CFGR, RCC_CFGR_SW_HSI);
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f)
|
||||
void BigDigitalTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f)
|
||||
{
|
||||
char time_str[3] = { 0 };
|
||||
|
||||
@@ -63,12 +61,12 @@ void DisplayTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, u
|
||||
time_str[1] = get_char_for_digit(ones);
|
||||
time_str[2] = '\0';
|
||||
|
||||
m_driver.string_at(x, y, time_str, &f, Color::WHITE);
|
||||
m_driver.string_at(x, y, time_str, &f, Color::BLACK);
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::display_time()
|
||||
void BigDigitalTimeScreen::display_time()
|
||||
{
|
||||
Common::WallClockTime time;
|
||||
BSP::WallClockTime time;
|
||||
BSP::RtcDriver::get_time(time);
|
||||
const struct font &font = font_large_digits;
|
||||
const uint32_t y_space = (m_driver.get_height() - (2 * font.height)) / 3;
|
||||
@@ -76,7 +74,7 @@ void DisplayTimeScreen::display_time()
|
||||
|
||||
uint32_t x = 0;
|
||||
|
||||
m_driver.clear(Color::BLACK);
|
||||
m_driver.clear(Color::WHITE);
|
||||
|
||||
x = x_space;
|
||||
display_number(&x, y_space,
|
||||
@@ -90,40 +88,40 @@ void DisplayTimeScreen::display_time()
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
NextTime DisplayTimeScreen::execute()
|
||||
NextTime BigDigitalTimeScreen::execute()
|
||||
{
|
||||
display_time();
|
||||
|
||||
Common::time_t now;
|
||||
BSP::time_t now;
|
||||
BSP::SystemTimer::get_time(now);
|
||||
if (m_display_seconds) {
|
||||
return NextTime::in(Time::seconds(1));
|
||||
} else {
|
||||
|
||||
Common::WallClockTime wall_time;
|
||||
BSP::WallClockTime wall_time;
|
||||
BSP::RtcDriver::get_time(wall_time);
|
||||
return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DisplayTimeScreen::enable() {
|
||||
void BigDigitalTimeScreen::enable() {
|
||||
m_last_time = {};
|
||||
display_time();
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::disable() {
|
||||
void BigDigitalTimeScreen::disable() {
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_up_button() {
|
||||
void BigDigitalTimeScreen::notify_up_button() {
|
||||
/* TODO: This should open a menu first */
|
||||
m_manager.push_screen(m_menu_screen);
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_middle_button() {
|
||||
void BigDigitalTimeScreen::notify_middle_button() {
|
||||
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_down_button() {
|
||||
void BigDigitalTimeScreen::notify_down_button() {
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
*
|
||||
* 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
|
||||
* 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
|
||||
@@ -26,15 +26,15 @@
|
||||
#include "Application/ScreenManager.h"
|
||||
#include "Application/Screens/Screen.h"
|
||||
|
||||
class DisplayTimeScreen : public Screen {
|
||||
class BigDigitalTimeScreen : public Screen {
|
||||
public:
|
||||
|
||||
DisplayTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager,
|
||||
Screen &m_menu_screen);
|
||||
BigDigitalTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager,
|
||||
Screen &m_menu_screen);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::Schedule::NextTime execute() override;
|
||||
BSP::ReturnCode init();
|
||||
BSP::Schedule::NextTime execute() override;
|
||||
|
||||
void enable() override;
|
||||
void disable() override;
|
||||
@@ -49,7 +49,7 @@ private:
|
||||
void display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const font &f);
|
||||
|
||||
BSP::DisplayDriver &m_driver;
|
||||
Common::WallClockTime m_last_time;
|
||||
BSP::WallClockTime m_last_time;
|
||||
|
||||
ScreenManager &m_manager;
|
||||
Screen &m_menu_screen;
|
||||
@@ -39,8 +39,8 @@ public:
|
||||
, m_manager(manager)
|
||||
{}
|
||||
|
||||
Common::ReturnCode init() {
|
||||
return Common::ReturnCode::OK;
|
||||
BSP::ReturnCode init() {
|
||||
return BSP::ReturnCode::OK;
|
||||
}
|
||||
|
||||
void render() {
|
||||
@@ -58,9 +58,9 @@ public:
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
Common::Schedule::NextTime execute() override {
|
||||
BSP::Schedule::NextTime execute() override {
|
||||
render();
|
||||
return Common::Schedule::NextTime::never();
|
||||
return BSP::Schedule::NextTime::never();
|
||||
}
|
||||
|
||||
void enable() override {
|
||||
|
||||
@@ -124,12 +124,12 @@ public:
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
Common::ReturnCode init() {
|
||||
return Common::ReturnCode::OK;
|
||||
BSP::ReturnCode init() {
|
||||
return BSP::ReturnCode::OK;
|
||||
}
|
||||
|
||||
Common::Schedule::NextTime execute() override {
|
||||
return Common::Schedule::NextTime::never();
|
||||
BSP::Schedule::NextTime execute() override {
|
||||
return BSP::Schedule::NextTime::never();
|
||||
}
|
||||
|
||||
void enable() override {
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "Bsp/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
|
||||
class Screen : public Common::Schedule::Task {
|
||||
class Screen : public BSP::Schedule::Task {
|
||||
public:
|
||||
|
||||
virtual void enable() = 0;
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Time;
|
||||
using Common::Schedule::NextTime;
|
||||
using BSP::ReturnCode;
|
||||
using BSP::Time;
|
||||
using BSP::Schedule::NextTime;
|
||||
|
||||
SetDateScreen::SetDateScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
|
||||
@@ -36,8 +36,8 @@ public:
|
||||
SetDateScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::Schedule::NextTime execute() override;
|
||||
BSP::ReturnCode init();
|
||||
BSP::Schedule::NextTime execute() override;
|
||||
|
||||
void enable() override;
|
||||
void disable() override;
|
||||
@@ -70,7 +70,7 @@ private:
|
||||
|
||||
SetState m_state;
|
||||
bool m_is_acked = false;
|
||||
Common::WallClockTime m_time;
|
||||
BSP::WallClockTime m_time;
|
||||
const struct font &m_font;
|
||||
const uint32_t m_row_spacing;
|
||||
const uint32_t m_row_0_y;
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Time;
|
||||
using Common::Schedule::NextTime;
|
||||
using BSP::ReturnCode;
|
||||
using BSP::Time;
|
||||
using BSP::Schedule::NextTime;
|
||||
|
||||
SetTimeScreen::SetTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
|
||||
@@ -36,8 +36,8 @@ public:
|
||||
SetTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::Schedule::NextTime execute() override;
|
||||
BSP::ReturnCode init();
|
||||
BSP::Schedule::NextTime execute() override;
|
||||
|
||||
void enable() override;
|
||||
void disable() override;
|
||||
@@ -70,7 +70,7 @@ private:
|
||||
|
||||
SetState m_state;
|
||||
bool m_is_acked = false;
|
||||
Common::WallClockTime m_time;
|
||||
BSP::WallClockTime m_time;
|
||||
const struct font &m_font;
|
||||
const uint32_t m_row_spacing;
|
||||
const uint32_t m_row_0_y;
|
||||
|
||||
Reference in New Issue
Block a user