Files
timely-reference/firmware/Application/ScreenManager.cpp
Max Regan 573504547c Add button tests
This is implemented by connecting the DTR pin of the serial device to the BTN_UP pin of the watch.

Also, make it possible to flash different applications with the Makefile.

Resolves #4
2020-06-11 16:39:51 +00:00

137 lines
3.9 KiB
C++

/*
* 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 BSP::Schedule::Task;
using BSP::Schedule::NextTime;
using BSP::ReturnCode;
using BSP::ButtonManager;
using Button = ButtonManager::Button;
using ButtonState = ButtonManager::ButtonState;
ScreenManager::ScreenManager(BSP::Schedule::TaskScheduler &scheduler,
BSP::DisplayDriver &display,
ButtonManager &buttons)
: m_scheduler(scheduler)
, m_screen_stack{nullptr}
, m_screen_stack_depth(0)
, m_root_initialized(false)
, m_display(display)
, m_buttons(buttons)
{
}
ReturnCode ScreenManager::init()
{
m_buttons.set_callback(
Button::UP,
[this](ButtonState state) {
if (state == ButtonState::PRESSED) {
current_screen()->notify_up_button();
}});
m_buttons.set_callback(
Button::MID,
[this](ButtonState state) {
if (state == ButtonState::PRESSED) {
return current_screen()->notify_middle_button();
}});
m_buttons.set_callback(
Button::DOWN,
[this](ButtonState state) {
if (state == 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[0] = &screen;
if (!m_root_initialized) {
m_screen_stack_depth = 1;
current_screen()->enable();
m_scheduler.add_task(*this, NextTime::asap());
m_root_initialized = true;
}
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;
}