Full color support, rework the directory structure
This commit is contained in:
173
firmware/Application/ButtonManager.cpp
Normal file
173
firmware/Application/ButtonManager.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 "Application/ButtonManager.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/macros.h"
|
||||
|
||||
#include "stm32l0xx.h"
|
||||
|
||||
namespace BSP {
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Schedule::NextTime;
|
||||
|
||||
ButtonManager *ButtonManager::m_instance = nullptr;
|
||||
|
||||
ReturnCode ButtonManager::init()
|
||||
{
|
||||
for (auto &btn: m_buttons) {
|
||||
/** Enable pin for input (pulled up) */
|
||||
CLR(GPIOA->MODER, 3u << (2 * btn.m_gpio_idx));
|
||||
CLR(GPIOA->OTYPER, 1u << btn.m_gpio_idx);
|
||||
SET_TO(GPIOA->PUPDR, 3u << (2 * btn.m_gpio_idx), 1u << (2 * btn.m_gpio_idx));
|
||||
|
||||
// Unmask this interrupt
|
||||
SET(EXTI->IMR, 1u << btn.m_gpio_idx);
|
||||
// Enable this interrupt
|
||||
SET(EXTI->EMR, 1u << btn.m_gpio_idx);
|
||||
// Enable interrupt for rising edge
|
||||
SET(EXTI->RTSR, 1u << btn.m_gpio_idx);
|
||||
// Enable interrupt for falling edge
|
||||
SET(EXTI->FTSR, 1u << btn.m_gpio_idx);
|
||||
}
|
||||
|
||||
CLR(SYSCFG->EXTICR[0],
|
||||
SYSCFG_EXTICR1_EXTI3 | SYSCFG_EXTICR1_EXTI2 |
|
||||
SYSCFG_EXTICR1_EXTI1 | SYSCFG_EXTICR1_EXTI0);
|
||||
|
||||
NVIC_EnableIRQ(EXTI0_1_IRQn);
|
||||
NVIC_EnableIRQ(EXTI2_3_IRQn);
|
||||
NVIC_EnableIRQ(EXTI4_15_IRQn);
|
||||
|
||||
NVIC_SetPriority(EXTI0_1_IRQn, 1);
|
||||
NVIC_SetPriority(EXTI2_3_IRQn, 1);
|
||||
NVIC_SetPriority(EXTI4_15_IRQn, 1);
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime ButtonManager::execute()
|
||||
{
|
||||
Common::time_t systime;
|
||||
Common::time_t endtime = 0;
|
||||
BSP::SystemTimer::get_time(systime);
|
||||
|
||||
|
||||
for (auto &btn: m_buttons) {
|
||||
// Has the state changed?
|
||||
if (btn.m_prev_call_state != btn.m_state) {
|
||||
// Have we 'debounced' this state?
|
||||
if (btn.m_state_change_ts + btn.m_debounce_time < systime) {
|
||||
// It's tiiiiime
|
||||
if (btn.m_callback != nullptr) {
|
||||
btn.m_callback(btn.m_state);
|
||||
}
|
||||
btn.m_prev_call_state = btn.m_state;
|
||||
continue;
|
||||
} else {
|
||||
|
||||
// It's not time yet. Use this to figure out the next time we should check
|
||||
if (endtime == 0 || btn.m_state_change_ts + btn.m_debounce_time < endtime) {
|
||||
endtime = btn.m_state_change_ts + btn.m_debounce_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (endtime == 0) {
|
||||
return NextTime::never();
|
||||
} else {
|
||||
return NextTime::at(endtime);
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonManager::set_callback(Button btn, ChangeCallback callback)
|
||||
{
|
||||
if (btn == Button::Count) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_buttons[btn].m_callback = callback;
|
||||
}
|
||||
|
||||
void ButtonManager::remove_callback(Button btn)
|
||||
{
|
||||
if (btn == Button::Count) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_buttons[btn].m_callback = nop_callback;
|
||||
}
|
||||
|
||||
void ButtonManager::irq()
|
||||
{
|
||||
uint32_t idr = GPIOA->IDR;
|
||||
static Common::time_t systime;
|
||||
|
||||
// TODO: is this call too expensive for an interrupt handler?
|
||||
BSP::SystemTimer::get_time(systime);
|
||||
|
||||
if (!m_instance) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &btn: m_instance->m_buttons) {
|
||||
bool is_pressed = !(idr & (1 << btn.m_gpio_idx));
|
||||
|
||||
// Check if the button state has changed, and timestamp it
|
||||
if (is_pressed && (btn.m_state == ButtonState::NOT_PRESSED)) {
|
||||
btn.m_state = ButtonState::PRESSED;
|
||||
btn.m_state_change_ts = systime;
|
||||
} else if (!is_pressed && (btn.m_state == ButtonState::PRESSED)) {
|
||||
btn.m_state = ButtonState::NOT_PRESSED;
|
||||
btn.m_state_change_ts = systime;
|
||||
}
|
||||
|
||||
// Clear the event
|
||||
SET(EXTI->PR, 1u << btn.m_gpio_idx);
|
||||
}
|
||||
|
||||
if (m_instance != nullptr) {
|
||||
m_instance->m_scheduler.add_task(*m_instance, NextTime::asap());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extern "C" void EXTI_1_0_IRQHandler() {
|
||||
static uint32_t irq_count = 0;
|
||||
ButtonManager::irq();
|
||||
irq_count++;
|
||||
}
|
||||
|
||||
extern "C" void EXTI_3_2_IRQHandler() {
|
||||
static uint32_t irq_count = 0;
|
||||
ButtonManager::irq();
|
||||
irq_count++;
|
||||
}
|
||||
|
||||
extern "C" void EXTI_15_4_IRQHandler() {
|
||||
static uint32_t irq_count = 0;
|
||||
ButtonManager::irq();
|
||||
irq_count++;
|
||||
}
|
||||
|
||||
}
|
||||
105
firmware/Application/ButtonManager.h
Normal file
105
firmware/Application/ButtonManager.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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 <functional>
|
||||
|
||||
#include "Bsp/TaskScheduler.h"
|
||||
#include "Bsp/Task.h"
|
||||
#include "Bsp/ReturnCode.h"
|
||||
|
||||
namespace BSP {
|
||||
|
||||
class ButtonManager : public Common::Schedule::Task {
|
||||
public:
|
||||
ButtonManager(Common::Schedule::TaskScheduler &scheduler,
|
||||
uint8_t up_gpio_idx,
|
||||
uint8_t mid_gpio_idx,
|
||||
uint8_t down_gpio_idx,
|
||||
Common::time_t debounce_time)
|
||||
: m_scheduler(scheduler)
|
||||
, m_buttons
|
||||
{
|
||||
button_state(up_gpio_idx, debounce_time, ButtonManager::nop_callback),
|
||||
button_state(mid_gpio_idx, debounce_time, ButtonManager::nop_callback),
|
||||
button_state(down_gpio_idx, debounce_time, ButtonManager::nop_callback),
|
||||
}
|
||||
{
|
||||
m_instance = this;
|
||||
}
|
||||
|
||||
ButtonManager() = delete;
|
||||
|
||||
// TODO: Make a real singleton instead
|
||||
static ButtonManager *m_instance;
|
||||
|
||||
enum Button {
|
||||
UP = 0,
|
||||
MID,
|
||||
DOWN,
|
||||
Count
|
||||
};
|
||||
|
||||
enum class ButtonState {
|
||||
PRESSED,
|
||||
NOT_PRESSED
|
||||
};
|
||||
|
||||
using ChangeCallback = std::function<void(ButtonState)>;
|
||||
|
||||
Common::Schedule::NextTime execute() override;
|
||||
|
||||
Common::ReturnCode init();
|
||||
void set_callback(Button btn, ChangeCallback callback);
|
||||
void remove_callback(Button btn);
|
||||
|
||||
static void irq();
|
||||
|
||||
private:
|
||||
|
||||
struct button_state {
|
||||
|
||||
button_state(uint8_t gpio_index,
|
||||
Common::time_t debounce_time,
|
||||
ChangeCallback callback)
|
||||
: m_gpio_idx(gpio_index)
|
||||
, m_debounce_time(debounce_time)
|
||||
, m_state(ButtonState::NOT_PRESSED)
|
||||
, m_state_change_ts(0)
|
||||
, m_callback(callback)
|
||||
{}
|
||||
uint8_t const m_gpio_idx;
|
||||
Common::time_t const m_debounce_time;
|
||||
|
||||
ButtonState m_prev_call_state; /*<! The last state the button was in when it the callback was called */
|
||||
ButtonState m_state; /*<! The state the button was in during the last iteration */
|
||||
Common::time_t m_state_change_ts; /*<! The system time when the button entered its current state */
|
||||
ChangeCallback m_callback; /*<! The callback to call when the button has changed states (post-debounce) */
|
||||
};
|
||||
|
||||
Common::Schedule::TaskScheduler &m_scheduler;
|
||||
button_state m_buttons[Button::Count];
|
||||
|
||||
static void nop_callback(ButtonState) {};
|
||||
};
|
||||
|
||||
}
|
||||
128
firmware/Application/ScreenManager.cpp
Normal file
128
firmware/Application/ScreenManager.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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 "ScreenManager.h"
|
||||
#include "ButtonManager.h"
|
||||
|
||||
// TODO: Basically all of the error checking
|
||||
|
||||
using Common::Schedule::Task;
|
||||
using Common::Schedule::NextTime;
|
||||
using Common::ReturnCode;
|
||||
|
||||
ScreenManager::ScreenManager(Common::Schedule::TaskScheduler &scheduler,
|
||||
BSP::DisplayDriver &display,
|
||||
BSP::ButtonManager &buttons)
|
||||
: m_scheduler(scheduler)
|
||||
, m_screen_stack{nullptr}
|
||||
, m_screen_stack_depth(0)
|
||||
, m_display(display)
|
||||
, m_buttons(buttons)
|
||||
{
|
||||
}
|
||||
|
||||
ReturnCode ScreenManager::init()
|
||||
{
|
||||
m_buttons.set_callback(
|
||||
BSP::ButtonManager::Button::UP,
|
||||
[this](BSP::ButtonManager::ButtonState state) {
|
||||
if (state == BSP::ButtonManager::ButtonState::PRESSED) {
|
||||
current_screen()->notify_up_button();
|
||||
}});
|
||||
|
||||
m_buttons.set_callback(
|
||||
BSP::ButtonManager::Button::MID,
|
||||
[this](BSP::ButtonManager::ButtonState state) {
|
||||
if (state == BSP::ButtonManager::ButtonState::PRESSED) {
|
||||
return current_screen()->notify_middle_button();
|
||||
}});
|
||||
|
||||
m_buttons.set_callback(
|
||||
BSP::ButtonManager::Button::DOWN,
|
||||
[this](BSP::ButtonManager::ButtonState state) {
|
||||
if (state == BSP::ButtonManager::ButtonState::PRESSED) {
|
||||
current_screen()->notify_down_button();
|
||||
}});
|
||||
|
||||
m_display.clear();
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime ScreenManager::execute()
|
||||
{
|
||||
return current_screen()->execute();
|
||||
}
|
||||
|
||||
ReturnCode ScreenManager::set_root_screen(Screen &screen)
|
||||
{
|
||||
m_screen_stack[m_screen_stack_depth] = &screen;
|
||||
m_screen_stack_depth = 1;
|
||||
current_screen()->enable();
|
||||
m_scheduler.add_task(*this, NextTime::asap());
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
ReturnCode ScreenManager::push_screen(Screen &screen)
|
||||
{
|
||||
if (m_screen_stack_depth == MAX_SCREEN_STACK) {
|
||||
return ReturnCode::FAIL;
|
||||
}
|
||||
|
||||
// TODO: "Lock" changes during this operation. Don't allow
|
||||
// disable/enable to push/pop.
|
||||
|
||||
current_screen()->disable();
|
||||
m_screen_stack[m_screen_stack_depth] = &screen;
|
||||
m_screen_stack_depth++;
|
||||
current_screen()->enable();
|
||||
m_scheduler.add_task(*this, NextTime::asap());
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
ReturnCode ScreenManager::pop_screen() {
|
||||
if (m_screen_stack_depth == 1) {
|
||||
return ReturnCode::FAIL;
|
||||
}
|
||||
|
||||
current_screen()->disable();
|
||||
m_screen_stack_depth--;
|
||||
m_screen_stack[m_screen_stack_depth] = nullptr;
|
||||
current_screen()->enable();
|
||||
m_scheduler.add_task(*this, NextTime::asap());
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
ReturnCode ScreenManager::set_screen(Screen &screen) {
|
||||
if (m_screen_stack_depth == 1) {
|
||||
return ReturnCode::FAIL;
|
||||
}
|
||||
|
||||
current_screen()->disable();
|
||||
m_screen_stack[m_screen_stack_depth - 1] = &screen;
|
||||
current_screen()->enable();
|
||||
m_scheduler.add_task(*this, NextTime::asap());
|
||||
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
68
firmware/Application/ScreenManager.h
Normal file
68
firmware/Application/ScreenManager.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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 <array>
|
||||
|
||||
#include "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
#include "Bsp/macros.h"
|
||||
|
||||
#include "Application/ButtonManager.h"
|
||||
#include "Application/Screens/Screen.h"
|
||||
|
||||
class ScreenManager : public Common::Schedule::Task {
|
||||
public:
|
||||
|
||||
ScreenManager(Common::Schedule::TaskScheduler &scheduler,
|
||||
BSP::DisplayDriver &display,
|
||||
BSP::ButtonManager &buttons);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::ReturnCode set_root_screen(Screen &screen);
|
||||
|
||||
Common::Schedule::NextTime execute() override;
|
||||
|
||||
Common::ReturnCode pop_screen();
|
||||
Common::ReturnCode push_screen(Screen &screen);
|
||||
Common::ReturnCode set_screen(Screen &screen);
|
||||
|
||||
private:
|
||||
|
||||
inline Screen *current_screen() {
|
||||
return m_screen_stack[m_screen_stack_depth - 1];
|
||||
}
|
||||
void notify_up_screen();
|
||||
void notify_middle_screen();
|
||||
void notify_down_screen();
|
||||
|
||||
static constexpr std::size_t MAX_SCREEN_STACK = 5;
|
||||
|
||||
Common::Schedule::TaskScheduler &m_scheduler;
|
||||
|
||||
std::array<Screen *, MAX_SCREEN_STACK> m_screen_stack;
|
||||
std::size_t m_screen_stack_depth;
|
||||
|
||||
BSP::DisplayDriver &m_display;
|
||||
BSP::ButtonManager &m_buttons;
|
||||
};
|
||||
0
firmware/Application/Screens/DebugScreen.cpp
Normal file
0
firmware/Application/Screens/DebugScreen.cpp
Normal file
89
firmware/Application/Screens/DebugScreen.h
Normal file
89
firmware/Application/Screens/DebugScreen.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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/SystemFonts.h"
|
||||
#include "Application/ScreenManager.h"
|
||||
#include "Application/Screens/Screen.h"
|
||||
|
||||
class DebugScreen : public Screen {
|
||||
public:
|
||||
|
||||
static constexpr std::size_t MAX_ITEMS = 10;
|
||||
|
||||
DebugScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
: m_driver(display)
|
||||
, m_manager(manager)
|
||||
{}
|
||||
|
||||
Common::ReturnCode init() {
|
||||
return Common::ReturnCode::OK;
|
||||
}
|
||||
|
||||
void render() {
|
||||
const struct font &font = default_font;
|
||||
char buffer[32] = {0};
|
||||
|
||||
m_driver.clear();
|
||||
|
||||
uint32_t x = 0;
|
||||
uint32_t y = 0;
|
||||
// snprintf(buffer, sizeof(buffer), "Wakeups");
|
||||
m_driver.string_at(&x, y, buffer, &font);
|
||||
y += font.height;
|
||||
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
Common::Schedule::NextTime execute() override {
|
||||
render();
|
||||
return Common::Schedule::NextTime::never();
|
||||
}
|
||||
|
||||
void enable() override {
|
||||
render();
|
||||
}
|
||||
|
||||
void disable() override {
|
||||
m_driver.clear();
|
||||
}
|
||||
|
||||
void notify_up_button() override {
|
||||
}
|
||||
|
||||
void notify_middle_button() override {
|
||||
m_manager.pop_screen();
|
||||
}
|
||||
|
||||
void notify_down_button() override {
|
||||
m_manager.pop_screen();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
BSP::DisplayDriver &m_driver;
|
||||
ScreenManager &m_manager;
|
||||
};
|
||||
129
firmware/Application/Screens/DisplayTimeScreen.cpp
Normal file
129
firmware/Application/Screens/DisplayTimeScreen.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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 "Application/Screens/DisplayTimeScreen.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 Color = BSP::DisplayDriver::Color;
|
||||
|
||||
DisplayTimeScreen::DisplayTimeScreen(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)
|
||||
{}
|
||||
|
||||
static char get_char_for_digit(uint8_t bcd_digit)
|
||||
{
|
||||
if (bcd_digit > 9) {
|
||||
return '0';
|
||||
}
|
||||
return bcd_digit + '0';
|
||||
}
|
||||
|
||||
ReturnCode DisplayTimeScreen::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)
|
||||
{
|
||||
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_driver.string_at(x, y, time_str, &f, Color::WHITE);
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::display_time()
|
||||
{
|
||||
Common::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;
|
||||
const uint32_t x_space = (m_driver.get_width() - (2 * font.width)) / 2;
|
||||
|
||||
uint32_t x = 0;
|
||||
|
||||
m_driver.clear(Color::BLACK);
|
||||
|
||||
x = x_space;
|
||||
display_number(&x, y_space,
|
||||
time.get_hours_12_tens(), time.get_hours_12_ones(), font);
|
||||
|
||||
x = x_space;
|
||||
display_number(&x, y_space * 2 + font.height,
|
||||
time.get_minutes_tens(), time.get_minutes_ones(), font);
|
||||
|
||||
m_last_time = time;
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
NextTime DisplayTimeScreen::execute()
|
||||
{
|
||||
display_time();
|
||||
|
||||
Common::time_t now;
|
||||
BSP::SystemTimer::get_time(now);
|
||||
if (m_display_seconds) {
|
||||
return NextTime::in(Time::seconds(1));
|
||||
} else {
|
||||
|
||||
Common::WallClockTime wall_time;
|
||||
BSP::RtcDriver::get_time(wall_time);
|
||||
return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DisplayTimeScreen::enable() {
|
||||
m_last_time = {};
|
||||
display_time();
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::disable() {
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_up_button() {
|
||||
/* TODO: This should open a menu first */
|
||||
m_manager.push_screen(m_menu_screen);
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_middle_button() {
|
||||
|
||||
}
|
||||
|
||||
void DisplayTimeScreen::notify_down_button() {
|
||||
|
||||
}
|
||||
58
firmware/Application/Screens/DisplayTimeScreen.h
Normal file
58
firmware/Application/Screens/DisplayTimeScreen.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 DisplayTimeScreen : public Screen {
|
||||
public:
|
||||
|
||||
DisplayTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager,
|
||||
Screen &m_menu_screen);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::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_time();
|
||||
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;
|
||||
|
||||
ScreenManager &m_manager;
|
||||
Screen &m_menu_screen;
|
||||
|
||||
const bool m_display_seconds;
|
||||
};
|
||||
184
firmware/Application/Screens/MenuScreen.h
Normal file
184
firmware/Application/Screens/MenuScreen.h
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* 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 <initializer_list>
|
||||
|
||||
#include "Bsp/macros.h"
|
||||
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Application/ScreenManager.h"
|
||||
#include "Application/Screens/Screen.h"
|
||||
|
||||
struct MenuScreenItem {
|
||||
|
||||
enum class Type {
|
||||
NONE,
|
||||
SCREEN,
|
||||
FUNCTION,
|
||||
BACK,
|
||||
};
|
||||
|
||||
MenuScreenItem()
|
||||
: m_text(nullptr)
|
||||
, m_type(Type::NONE)
|
||||
{}
|
||||
|
||||
MenuScreenItem(const char *item, Screen &m_screen)
|
||||
: m_text(item)
|
||||
, m_screen(&m_screen)
|
||||
, m_type(Type::SCREEN)
|
||||
{}
|
||||
|
||||
MenuScreenItem(const char *item, void (*function)())
|
||||
: m_text(item)
|
||||
, m_function(function)
|
||||
, m_type(Type::FUNCTION)
|
||||
{}
|
||||
|
||||
MenuScreenItem(const char *item)
|
||||
: m_text(item)
|
||||
, m_type(Type::BACK)
|
||||
{}
|
||||
|
||||
const char *m_text;
|
||||
union {
|
||||
Screen *m_screen;
|
||||
void (* m_function)();
|
||||
};
|
||||
Type m_type;
|
||||
};
|
||||
|
||||
class MenuScreen : public Screen {
|
||||
public:
|
||||
|
||||
static constexpr std::size_t MAX_ITEMS = 5;
|
||||
|
||||
MenuScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager,
|
||||
const char *title,
|
||||
std::initializer_list<MenuScreenItem> items)
|
||||
: m_driver(display)
|
||||
, m_manager(manager)
|
||||
, m_title(title)
|
||||
, m_items()
|
||||
, m_num_items{0}
|
||||
, m_selected{0}
|
||||
{
|
||||
for (auto &item : items) {
|
||||
m_items[m_num_items++] = item;
|
||||
|
||||
// Leave room for "back"
|
||||
if (m_num_items == MAX_ITEMS - 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
new (&m_items[m_num_items++]) MenuScreenItem("Back");
|
||||
}
|
||||
|
||||
void render() {
|
||||
const struct font &font = default_font;
|
||||
|
||||
//m_driver.clear();
|
||||
|
||||
uint32_t x = 0;
|
||||
uint32_t y = 0;
|
||||
m_driver.string_at(&x, y, m_title, &font);
|
||||
y += font.height + 1;
|
||||
|
||||
m_driver.draw_hline(0, y++, m_driver.get_width());
|
||||
m_driver.draw_hline(0, y++, m_driver.get_width());
|
||||
|
||||
|
||||
for (std::size_t i = 0; i < m_num_items; i++) {
|
||||
const char *spacer = " ";
|
||||
if (i == m_selected) {
|
||||
spacer = ">";
|
||||
}
|
||||
x = 0;
|
||||
m_driver.string_at(&x, y, spacer, &font);
|
||||
m_driver.string_at(&x, y, m_items[i].m_text, &font);
|
||||
y += font.height;
|
||||
}
|
||||
m_driver.refresh();
|
||||
}
|
||||
|
||||
Common::ReturnCode init() {
|
||||
return Common::ReturnCode::OK;
|
||||
}
|
||||
|
||||
Common::Schedule::NextTime execute() override {
|
||||
return Common::Schedule::NextTime::never();
|
||||
}
|
||||
|
||||
void enable() override {
|
||||
m_selected = 0;
|
||||
m_driver.clear();
|
||||
render();
|
||||
}
|
||||
|
||||
void disable() override {
|
||||
}
|
||||
|
||||
void notify_up_button() override {
|
||||
if (m_selected == 0) {
|
||||
m_selected = m_num_items - 1;
|
||||
} else {
|
||||
m_selected--;
|
||||
}
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
void notify_middle_button() override {
|
||||
MenuScreenItem &item = m_items[m_selected];
|
||||
if (item.m_type == MenuScreenItem::Type::SCREEN) {
|
||||
m_manager.push_screen(*item.m_screen);
|
||||
} else if (item.m_type == MenuScreenItem::Type::FUNCTION) {
|
||||
item.m_function();
|
||||
m_manager.pop_screen();
|
||||
} else if (item.m_type == MenuScreenItem::Type::BACK) {
|
||||
m_manager.pop_screen();
|
||||
}
|
||||
}
|
||||
|
||||
void notify_down_button() override {
|
||||
if (m_selected == m_num_items - 1) {
|
||||
m_selected = 0;
|
||||
} else {
|
||||
m_selected++;
|
||||
}
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
BSP::DisplayDriver &m_driver;
|
||||
ScreenManager &m_manager;
|
||||
const char *m_title;
|
||||
MenuScreenItem m_items[MAX_ITEMS];
|
||||
std::size_t m_num_items;
|
||||
std::size_t m_selected;
|
||||
};
|
||||
36
firmware/Application/Screens/Screen.h
Normal file
36
firmware/Application/Screens/Screen.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
|
||||
class Screen : public Common::Schedule::Task {
|
||||
public:
|
||||
|
||||
virtual void enable() = 0;
|
||||
virtual void disable() = 0;
|
||||
|
||||
virtual void notify_up_button() = 0;
|
||||
virtual void notify_middle_button() = 0;
|
||||
virtual void notify_down_button() = 0;
|
||||
};
|
||||
250
firmware/Application/Screens/SetDateScreen.cpp
Normal file
250
firmware/Application/Screens/SetDateScreen.cpp
Normal file
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* 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/SetDateScreen.h"
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Time;
|
||||
using Common::Schedule::NextTime;
|
||||
|
||||
SetDateScreen::SetDateScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
: m_display(display)
|
||||
, m_manager(manager)
|
||||
, m_state(SetState::HOURS)
|
||||
, m_is_acked(false)
|
||||
, m_time()
|
||||
, m_font(default_font)
|
||||
, m_row_spacing((m_display.get_height() - m_font.height * 2) / 3)
|
||||
, m_row_0_y(m_row_spacing)
|
||||
, m_row_1_y(m_row_0_y + m_font.height + m_row_spacing + m_font.height)
|
||||
{}
|
||||
|
||||
ReturnCode SetDateScreen::init()
|
||||
{
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime SetDateScreen::execute()
|
||||
{
|
||||
//TODO: Fix this so it doesn't constantly refresh
|
||||
refresh();
|
||||
|
||||
return NextTime::never();
|
||||
}
|
||||
|
||||
static char get_char_for_digit(uint8_t bcd_digit)
|
||||
{
|
||||
if (bcd_digit > 9) {
|
||||
return '0';
|
||||
}
|
||||
return bcd_digit + '0';
|
||||
}
|
||||
|
||||
void SetDateScreen::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);
|
||||
}
|
||||
|
||||
const char *SetDateScreen::get_acknak_string()
|
||||
{
|
||||
if (!m_is_acked) {
|
||||
return "Cancel";
|
||||
} else {
|
||||
return "OK";
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SetDateScreen::get_acknak_string_len()
|
||||
{
|
||||
std::size_t len = strlen(get_acknak_string());
|
||||
return len * m_font.width - 1;
|
||||
}
|
||||
|
||||
uint32_t SetDateScreen::get_acknak_string_x_pos()
|
||||
{
|
||||
std::size_t len = strlen(get_acknak_string());
|
||||
return m_display.get_width() - len * m_font.width - 1;
|
||||
}
|
||||
|
||||
void SetDateScreen::render_time()
|
||||
{
|
||||
uint32_t x = 0;
|
||||
uint32_t y = m_row_spacing;
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_hours_12_tens(), m_time.get_hours_12_ones());
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_minutes_tens(), m_time.get_minutes_ones());
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_seconds_tens(), m_time.get_seconds_ones());
|
||||
|
||||
y = m_row_1_y;
|
||||
x = 0;
|
||||
m_display.string_at(&x, y, m_time.get_is_pm() ? "PM" : "AM", &m_font);
|
||||
|
||||
x = get_acknak_string_x_pos();
|
||||
m_display.string_at(&x, y, get_acknak_string(), &m_font);
|
||||
|
||||
m_display.refresh();
|
||||
}
|
||||
|
||||
void SetDateScreen::draw_line(uint32_t x, uint32_t y, uint32_t width)
|
||||
{
|
||||
m_display.draw_hline(x, y, width);
|
||||
m_display.draw_hline(x, y + 1, width);
|
||||
}
|
||||
|
||||
void SetDateScreen::render_selection()
|
||||
{
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
draw_line(0, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
draw_line(m_font.width * 3, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
draw_line(m_font.width * 6, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
draw_line(0, m_row_1_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
draw_line(get_acknak_string_x_pos(), m_row_1_y + m_font.height, get_acknak_string_len());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SetDateScreen::refresh()
|
||||
{
|
||||
m_display.clear();
|
||||
|
||||
render_time();
|
||||
render_selection();
|
||||
|
||||
m_display.refresh();
|
||||
}
|
||||
|
||||
void SetDateScreen::enable()
|
||||
{
|
||||
BSP::RtcDriver::get_time(m_time);
|
||||
m_state = SetState::HOURS;
|
||||
m_is_acked = true;
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetDateScreen::disable()
|
||||
{
|
||||
m_display.clear();
|
||||
}
|
||||
|
||||
void SetDateScreen::notify_up_button()
|
||||
{
|
||||
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
m_time.increment_hours();
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_time.increment_minutes();
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_time.increment_seconds();
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_time.toggle_am_pm();
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
m_is_acked = !m_is_acked;
|
||||
break;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetDateScreen::notify_middle_button()
|
||||
{
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
m_state = SetState::MINUTES;
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_state = SetState::SECONDS;
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_state = SetState::AM_PM;
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_state = SetState::ACK_NACK;
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
if (m_is_acked) {
|
||||
BSP::RtcDriver::set_time(m_time);
|
||||
}
|
||||
m_manager.pop_screen();
|
||||
return;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetDateScreen::notify_down_button()
|
||||
{
|
||||
switch(m_state) {
|
||||
case SetState::HOURS:
|
||||
m_time.decrement_hours();
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_time.decrement_minutes();
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_time.decrement_seconds();
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_time.toggle_am_pm();
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
m_is_acked = !m_is_acked;
|
||||
break;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
78
firmware/Application/Screens/SetDateScreen.h
Normal file
78
firmware/Application/Screens/SetDateScreen.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
#include "Bsp/Time.h"
|
||||
#include "Bsp/font.h"
|
||||
|
||||
class SetDateScreen : public Screen {
|
||||
public:
|
||||
|
||||
SetDateScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::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 refresh();
|
||||
void render_time();
|
||||
void render_selection();
|
||||
void display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones);
|
||||
void draw_line(uint32_t x, uint32_t y, uint32_t width);
|
||||
uint32_t get_acknak_string_x_pos();
|
||||
uint32_t get_acknak_string_len();
|
||||
const char *get_acknak_string();
|
||||
|
||||
|
||||
enum class SetState {
|
||||
HOURS,
|
||||
MINUTES,
|
||||
SECONDS,
|
||||
AM_PM,
|
||||
ACK_NACK
|
||||
};
|
||||
|
||||
BSP::DisplayDriver &m_display;
|
||||
ScreenManager &m_manager;
|
||||
|
||||
SetState m_state;
|
||||
bool m_is_acked = false;
|
||||
Common::WallClockTime m_time;
|
||||
const struct font &m_font;
|
||||
const uint32_t m_row_spacing;
|
||||
const uint32_t m_row_0_y;
|
||||
const uint32_t m_row_1_y;
|
||||
};
|
||||
253
firmware/Application/Screens/SetTimeScreen.cpp
Normal file
253
firmware/Application/Screens/SetTimeScreen.cpp
Normal file
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* 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/SetTimeScreen.h"
|
||||
#include "Application/SystemFonts.h"
|
||||
#include "Bsp/SystemTime.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
|
||||
using Common::ReturnCode;
|
||||
using Common::Time;
|
||||
using Common::Schedule::NextTime;
|
||||
|
||||
SetTimeScreen::SetTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &manager)
|
||||
: m_display(display)
|
||||
, m_manager(manager)
|
||||
, m_state(SetState::HOURS)
|
||||
, m_is_acked(false)
|
||||
, m_time()
|
||||
, m_font(default_font)
|
||||
, m_row_spacing((m_display.get_height() - m_font.height * 2) / 3)
|
||||
, m_row_0_y(m_row_spacing)
|
||||
, m_row_1_y(m_row_0_y + m_font.height + m_row_spacing + m_font.height)
|
||||
{}
|
||||
|
||||
ReturnCode SetTimeScreen::init()
|
||||
{
|
||||
return ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime SetTimeScreen::execute()
|
||||
{
|
||||
//TODO: Fix this so it doesn't constantly refresh
|
||||
refresh();
|
||||
|
||||
return NextTime::never();
|
||||
}
|
||||
|
||||
static char get_char_for_digit(uint8_t bcd_digit)
|
||||
{
|
||||
if (bcd_digit > 9) {
|
||||
return '0';
|
||||
}
|
||||
return bcd_digit + '0';
|
||||
}
|
||||
|
||||
void SetTimeScreen::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);
|
||||
}
|
||||
|
||||
const char *SetTimeScreen::get_acknak_string()
|
||||
{
|
||||
if (!m_is_acked) {
|
||||
return "Cancel";
|
||||
} else {
|
||||
return "OK";
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SetTimeScreen::get_acknak_string_len()
|
||||
{
|
||||
std::size_t len = strlen(get_acknak_string());
|
||||
return len * m_font.width - 1;
|
||||
}
|
||||
|
||||
uint32_t SetTimeScreen::get_acknak_string_x_pos()
|
||||
{
|
||||
std::size_t len = strlen(get_acknak_string());
|
||||
return m_display.get_width() - len * m_font.width - 1;
|
||||
}
|
||||
|
||||
void SetTimeScreen::render_time()
|
||||
{
|
||||
uint32_t width = m_display.get_width();
|
||||
uint32_t x = (width - m_font.width * 8) / 2;
|
||||
uint32_t y = m_row_spacing;
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_hours_12_tens(), m_time.get_hours_12_ones());
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_minutes_tens(), m_time.get_minutes_ones());
|
||||
|
||||
m_display.string_at(&x, y, ":", &m_font);
|
||||
|
||||
display_number(&x, y,
|
||||
m_time.get_seconds_tens(), m_time.get_seconds_ones());
|
||||
|
||||
y = m_row_1_y;
|
||||
x = 0;
|
||||
m_display.string_at(&x, y, m_time.get_is_pm() ? "PM" : "AM", &m_font);
|
||||
|
||||
x = get_acknak_string_x_pos();
|
||||
m_display.string_at(&x, y, get_acknak_string(), &m_font);
|
||||
|
||||
m_display.refresh();
|
||||
}
|
||||
|
||||
void SetTimeScreen::draw_line(uint32_t x, uint32_t y, uint32_t width)
|
||||
{
|
||||
m_display.draw_hline(x, y, width);
|
||||
m_display.draw_hline(x, y + 1, width);
|
||||
}
|
||||
|
||||
void SetTimeScreen::render_selection()
|
||||
{
|
||||
uint32_t time_offset = (m_display.get_width()- m_font.width * 8) / 2;
|
||||
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
draw_line(time_offset, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
draw_line(time_offset + m_font.width * 3, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
draw_line(time_offset + m_font.width * 6, m_row_0_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
draw_line(0, m_row_1_y + m_font.height, m_font.width * 2);
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
draw_line(get_acknak_string_x_pos(), m_row_1_y + m_font.height, get_acknak_string_len());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SetTimeScreen::refresh()
|
||||
{
|
||||
m_display.clear();
|
||||
|
||||
render_time();
|
||||
render_selection();
|
||||
|
||||
m_display.refresh();
|
||||
}
|
||||
|
||||
void SetTimeScreen::enable()
|
||||
{
|
||||
BSP::RtcDriver::get_time(m_time);
|
||||
m_state = SetState::HOURS;
|
||||
m_is_acked = true;
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetTimeScreen::disable()
|
||||
{
|
||||
m_display.clear();
|
||||
}
|
||||
|
||||
void SetTimeScreen::notify_up_button()
|
||||
{
|
||||
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
m_time.increment_hours();
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_time.increment_minutes();
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_time.increment_seconds();
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_time.toggle_am_pm();
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
m_is_acked = !m_is_acked;
|
||||
break;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetTimeScreen::notify_middle_button()
|
||||
{
|
||||
switch (m_state) {
|
||||
case SetState::HOURS:
|
||||
m_state = SetState::MINUTES;
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_state = SetState::SECONDS;
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_state = SetState::AM_PM;
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_state = SetState::ACK_NACK;
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
if (m_is_acked) {
|
||||
BSP::RtcDriver::set_time(m_time);
|
||||
}
|
||||
m_manager.pop_screen();
|
||||
return;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void SetTimeScreen::notify_down_button()
|
||||
{
|
||||
switch(m_state) {
|
||||
case SetState::HOURS:
|
||||
m_time.decrement_hours();
|
||||
break;
|
||||
case SetState::MINUTES:
|
||||
m_time.decrement_minutes();
|
||||
break;
|
||||
case SetState::SECONDS:
|
||||
m_time.decrement_seconds();
|
||||
break;
|
||||
case SetState::AM_PM:
|
||||
m_time.toggle_am_pm();
|
||||
break;
|
||||
case SetState::ACK_NACK:
|
||||
m_is_acked = !m_is_acked;
|
||||
break;
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
78
firmware/Application/Screens/SetTimeScreen.h
Normal file
78
firmware/Application/Screens/SetTimeScreen.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/ReturnCode.h"
|
||||
#include "Bsp/Task.h"
|
||||
#include "Bsp/Time.h"
|
||||
#include "Bsp/font.h"
|
||||
|
||||
class SetTimeScreen : public Screen {
|
||||
public:
|
||||
|
||||
SetTimeScreen(BSP::DisplayDriver &display,
|
||||
ScreenManager &m_manager);
|
||||
|
||||
Common::ReturnCode init();
|
||||
Common::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 refresh();
|
||||
void render_time();
|
||||
void render_selection();
|
||||
void display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones);
|
||||
void draw_line(uint32_t x, uint32_t y, uint32_t width);
|
||||
uint32_t get_acknak_string_x_pos();
|
||||
uint32_t get_acknak_string_len();
|
||||
const char *get_acknak_string();
|
||||
|
||||
|
||||
enum class SetState {
|
||||
HOURS,
|
||||
MINUTES,
|
||||
SECONDS,
|
||||
AM_PM,
|
||||
ACK_NACK
|
||||
};
|
||||
|
||||
BSP::DisplayDriver &m_display;
|
||||
ScreenManager &m_manager;
|
||||
|
||||
SetState m_state;
|
||||
bool m_is_acked = false;
|
||||
Common::WallClockTime m_time;
|
||||
const struct font &m_font;
|
||||
const uint32_t m_row_spacing;
|
||||
const uint32_t m_row_0_y;
|
||||
const uint32_t m_row_1_y;
|
||||
};
|
||||
27
firmware/Application/SystemFonts.h
Normal file
27
firmware/Application/SystemFonts.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 "fonts/large_digits.h"
|
||||
#include "fonts/small.h"
|
||||
|
||||
static const struct font &default_font = font_small;
|
||||
207
firmware/Application/main.cpp
Normal file
207
firmware/Application/main.cpp
Normal file
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* 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 "Bsp/LowPowerTaskScheduler.h"
|
||||
#include "Bsp/Drivers/RtcDriver.h"
|
||||
#include "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/Drivers/SpiDriver.h"
|
||||
#include "Bsp/Drivers/LptimPwm.h"
|
||||
|
||||
#include "Application/ButtonManager.h"
|
||||
|
||||
#include "Application/ScreenManager.h"
|
||||
#include "Application/Screens/DisplayTimeScreen.h"
|
||||
#include "Application/Screens/DebugScreen.h"
|
||||
#include "Application/Screens/MenuScreen.h"
|
||||
#include "Application/Screens/SetTimeScreen.h"
|
||||
#include "Application/Screens/SetDateScreen.h"
|
||||
|
||||
#include "Bsp/macros.h"
|
||||
|
||||
// TODO: Don't include this here.
|
||||
#include "stm32l0xx.h"
|
||||
|
||||
using Common::Time;
|
||||
|
||||
static Common::Schedule::LowPowerTaskScheduler<5> g_sched;
|
||||
static BSP::SpiDriver g_spi(g_sched);
|
||||
static BSP::DisplayDriver g_display(g_sched, g_spi);
|
||||
static BSP::LptimPwm g_lptim_pwm(LPTIM1);
|
||||
static BSP::ButtonManager g_btn_manager(g_sched, 2, 1, 0, Time::millis(1));
|
||||
|
||||
static ScreenManager g_screen_manager(g_sched, g_display, g_btn_manager);
|
||||
static SetTimeScreen g_set_time_screen(g_display, g_screen_manager);
|
||||
static SetTimeScreen g_set_date_screen(g_display, g_screen_manager);
|
||||
|
||||
static MenuScreen g_enable_debug(g_display,
|
||||
g_screen_manager,
|
||||
"SW Update",
|
||||
std::initializer_list<MenuScreenItem>({MenuScreenItem("Enable", []() { BSP::LowPower::enable_debug(); }),
|
||||
MenuScreenItem("Disable", []() { BSP::LowPower::disable_debug();
|
||||
})}));
|
||||
// static DebugScreen g_debug_screen(g_display, g_screen_manager);
|
||||
|
||||
static MenuScreen g_settings_menu_screen(g_display,
|
||||
g_screen_manager,
|
||||
"Settings",
|
||||
std::initializer_list<MenuScreenItem>({MenuScreenItem("Set Time", g_set_time_screen),
|
||||
MenuScreenItem("Set Date", g_set_date_screen),
|
||||
MenuScreenItem("Set Face", g_set_date_screen),
|
||||
MenuScreenItem("SW Update", g_enable_debug)}));
|
||||
static MenuScreen g_main_menu_screen(g_display,
|
||||
g_screen_manager,
|
||||
"Main Menu",
|
||||
std::initializer_list<MenuScreenItem>({MenuScreenItem("Apps", g_settings_menu_screen),
|
||||
MenuScreenItem("Settings", g_settings_menu_screen)}));
|
||||
|
||||
static DisplayTimeScreen g_display_time_screen(g_display, g_screen_manager, g_main_menu_screen);
|
||||
|
||||
extern "C" void __cxa_pure_virtual() { while(1) {} }
|
||||
|
||||
void SystemInit()
|
||||
{
|
||||
/**
|
||||
* Use the MSI for the system clock, and disable all other clocks.
|
||||
*/
|
||||
|
||||
/*!< Set MSION bit. Set by hardware to force the MSI oscillator ON
|
||||
* when exiting from Stop or Standby mode, or in case of a failure
|
||||
* of the HSE oscillator used directly or indirectly as system
|
||||
* clock. This bit cannot be cleared if the MSI is used as system
|
||||
* clock. */
|
||||
SET(RCC->CR,
|
||||
RCC_CR_MSION);
|
||||
|
||||
SET_TO(RCC->ICSCR,
|
||||
RCC_ICSCR_MSIRANGE,
|
||||
RCC_ICSCR_MSIRANGE_6);
|
||||
|
||||
/*!< Set internal representation of clock frequency to 4MHz */
|
||||
// system_clk_freq = 4u << 22;
|
||||
|
||||
/*!< Reset
|
||||
* SW[1:0] (use MSI oscillator as system clock),
|
||||
* HPRE[3:0] (do not divide AHB clock in prescaler) ,
|
||||
* PPRE1[2:0] (do not divide APB low-speed clock)
|
||||
* PPRE2[2:0] (do not divide APB high-speed clock),
|
||||
* MCOSEL[2:0] (disable MCO clock),
|
||||
* MCOPRE[2:0] (disable MCO prescaler) */
|
||||
CLR(RCC->CFGR,
|
||||
RCC_CFGR_SW | ~RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 |
|
||||
RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE);
|
||||
|
||||
/*!< Reset
|
||||
* HSION (disable HSI),
|
||||
* HSIDIVEN (disable 18MHz HSI division)
|
||||
* HSEON (disable HSE clock)
|
||||
* CSSHSEON (disable HSE clock monitoring)
|
||||
* PLLON (disable PLL)
|
||||
*/
|
||||
CLR(RCC->CR,
|
||||
RCC_CR_HSION | RCC_CR_HSIDIVEN | RCC_CR_HSEON |
|
||||
RCC_CR_CSSHSEON | RCC_CR_PLLON);
|
||||
|
||||
/*!< Reset HSEBYP bit (disable HSE bypass) */
|
||||
CLR(RCC->CR,
|
||||
RCC_CR_HSEBYP);
|
||||
|
||||
/*!< Reset
|
||||
* PLLSRC (HSI16 is the PLL source),
|
||||
* PLLMUL[3:0] (3x PLL multiplication)
|
||||
* Don't touch PLLDIV[1:0], since 0 is undefined
|
||||
*/
|
||||
CLR(RCC->CFGR,
|
||||
RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);
|
||||
|
||||
/*!< Disable all interrupts */
|
||||
RCC->CIER = 0x00000000;
|
||||
|
||||
/* Vector Table Relocation in Internal FLASH */
|
||||
SCB->VTOR = FLASH_BASE;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
typedef void (*func_ptr)(void);
|
||||
extern func_ptr __init_array_start[], __init_array_end[];
|
||||
|
||||
static void _init(void)
|
||||
{
|
||||
|
||||
for (func_ptr* func = __init_array_start; func != __init_array_end; func++) {
|
||||
(*func)();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void main() {
|
||||
|
||||
_init();
|
||||
|
||||
// Set up the system clock
|
||||
BSP::RtcDriver::init();
|
||||
BSP::SystemTimer::set_timer(BSP::RtcDriver::get_system_timer());
|
||||
BSP::LowPower::init();
|
||||
|
||||
// Initialize the tasks
|
||||
g_lptim_pwm.init();
|
||||
g_spi.init();
|
||||
g_btn_manager.init();
|
||||
g_display.init();
|
||||
g_screen_manager.init();
|
||||
g_screen_manager.set_root_screen(g_display_time_screen);
|
||||
|
||||
// Enqueue each of the tasks
|
||||
Common::Schedule::NextTime asap = Common::Schedule::NextTime::asap();
|
||||
g_sched.add_task(g_spi, asap);
|
||||
g_sched.add_task(g_btn_manager, asap);
|
||||
g_sched.add_task(g_display, asap);
|
||||
g_sched.add_task(g_screen_manager, asap);
|
||||
|
||||
// And we're off! This will never return
|
||||
g_sched.run();
|
||||
}
|
||||
|
||||
|
||||
extern "C" void NMI_Handler() {while (1);}
|
||||
extern "C" void HardFault_Handler() {while (1);}
|
||||
extern "C" void SVC_Handler() {while (1);}
|
||||
extern "C" void PendSV_Handler() {while (1);}
|
||||
extern "C" void SysTick_Handler() {while (1);}
|
||||
|
||||
extern "C" void WWDG_IRQHandler() {while (1);}
|
||||
extern "C" void PVD_IRQHandler() {while (1);}
|
||||
extern "C" void WDT_IRQHandler() {while (1);}
|
||||
extern "C" void FLASH_IRQHandler() {while (1);}
|
||||
extern "C" void RCC_CRS_IRQHandler() {while (1);}
|
||||
extern "C" void DMA1_CHANNEL1_IRQHandler() {while (1);}
|
||||
extern "C" void DMA1_CHANNEL3_2_IRQHandler() {while (1);}
|
||||
extern "C" void DMA_CHANNEL_7_4_IRQHandler() {while (1);}
|
||||
extern "C" void ADC_COMP_IRQHandler() {while (1);}
|
||||
extern "C" void LPTIM1_IRQHandler() {while (1);}
|
||||
extern "C" void USART4_USART5_IRQHandler() {while (1);}
|
||||
extern "C" void TIM2_IRQHandler() {while (1);}
|
||||
extern "C" void TIM3_IRQHandler() {while (1);}
|
||||
extern "C" void TIM6_IRQHandler() {while (1);}
|
||||
extern "C" void TIM7_IRQHandler() {while (1);}
|
||||
extern "C" void TIM21_IRQHandler() {while (1);}
|
||||
extern "C" void I2C3_IRQHandler() {while (1);}
|
||||
extern "C" void TIM22_IRQHandler() {while (1);}
|
||||
Reference in New Issue
Block a user