combine with desktop project
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
37
components/justus/CMakeLists.txt
Normal file
37
components/justus/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
src/common/PSMenu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
return()
|
||||
endif()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(justus)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC
|
||||
src/common/PSMenu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
u8g2
|
||||
peter
|
||||
)
|
18
components/justus/include/MenuOptions.h
Normal file
18
components/justus/include/MenuOptions.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "common/Widget.h"
|
||||
|
||||
class Widget;
|
||||
|
||||
typedef struct {
|
||||
u8g2_t* u8g2;
|
||||
|
||||
std::function<void(std::shared_ptr<Widget>)> setScreen;
|
||||
std::function<void(std::shared_ptr<Widget>)> pushScreen;
|
||||
std::function<void()> popScreen;
|
||||
|
||||
std::function<void(uint8_t button)> onButtonClicked;
|
||||
} menu_options_t;
|
39
components/justus/include/common/PSMenu.h
Normal file
39
components/justus/include/common/PSMenu.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "MenuOptions.h"
|
||||
#include "Widget.h"
|
||||
#include "data/MenuItem.h"
|
||||
|
||||
typedef std::function<void(uint8_t)> MenuCallback;
|
||||
|
||||
class PSMenu : public Widget {
|
||||
public:
|
||||
explicit PSMenu(menu_options_t* options);
|
||||
~PSMenu() override;
|
||||
|
||||
void render() override;
|
||||
void onButtonClicked(uint8_t button) override;
|
||||
|
||||
void addText(const std::string& text, const MenuCallback& callback);
|
||||
void addSwitch(const std::string& text, std::string& value, const MenuCallback& callback);
|
||||
void addNumber(const std::string& text, std::string& value, const MenuCallback& callback);
|
||||
|
||||
private:
|
||||
void onPressedDown();
|
||||
void onPressedUp();
|
||||
void onPressedLeft();
|
||||
void onPressedRight();
|
||||
void onPressedSelect() const;
|
||||
void onPressedBack() const;
|
||||
|
||||
void drawScrollBar() const;
|
||||
void drawSelectionBox() const;
|
||||
|
||||
void renderWidget(uint8_t type, const uint8_t *font, int x, int y, const char *text) const;
|
||||
|
||||
size_t m_selected_item = 0;
|
||||
std::vector<MenuItem> m_items;
|
||||
menu_options_t *m_options;
|
||||
};
|
23
components/justus/include/common/ScrollBar.h
Normal file
23
components/justus/include/common/ScrollBar.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "MenuOptions.h"
|
||||
#include "Widget.h"
|
||||
|
||||
class ScrollBar final : public Widget {
|
||||
public:
|
||||
ScrollBar(const menu_options_t *options, size_t x, size_t y, size_t width, size_t height);
|
||||
void render() override;
|
||||
void refresh(size_t value, size_t max, size_t min = 0);
|
||||
|
||||
private:
|
||||
size_t m_x;
|
||||
size_t m_y;
|
||||
size_t m_width;
|
||||
size_t m_height;
|
||||
size_t m_value;
|
||||
size_t m_max;
|
||||
size_t m_min;
|
||||
|
||||
size_t m_thumbHeight;
|
||||
size_t m_thumbY;
|
||||
};
|
21
components/justus/include/common/Widget.h
Normal file
21
components/justus/include/common/Widget.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "u8g2.h"
|
||||
|
||||
class Widget {
|
||||
public:
|
||||
explicit Widget(u8g2_t* u8g2);
|
||||
|
||||
virtual ~Widget() = default;
|
||||
|
||||
virtual void update(uint64_t dt);
|
||||
|
||||
virtual void render();
|
||||
|
||||
virtual void onButtonClicked(uint8_t button);
|
||||
|
||||
protected:
|
||||
u8g2_t* u8g2;
|
||||
};
|
27
components/justus/include/data/MenuItem.h
Normal file
27
components/justus/include/data/MenuItem.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
class MenuItem {
|
||||
public:
|
||||
MenuItem(uint8_t type, std::string text, std::function<void(uint8_t)> callback);
|
||||
MenuItem(
|
||||
uint8_t type,
|
||||
std::string text,
|
||||
std::string value,
|
||||
std::function<void(uint8_t)> callback);
|
||||
[[nodiscard]] uint8_t getType() const;
|
||||
[[nodiscard]] const std::string &getText() const;
|
||||
[[nodiscard]] const std::string &getValue() const;
|
||||
void setValue(const std::string &value);
|
||||
void callback(uint8_t id) const;
|
||||
[[nodiscard]] bool hasCallback() const;
|
||||
|
||||
private:
|
||||
uint8_t m_type;
|
||||
std::string m_text;
|
||||
std::string m_value;
|
||||
std::function<void(uint8_t)> m_callback;
|
||||
};
|
9
components/justus/include/ui/LightMenu.h
Normal file
9
components/justus/include/ui/LightMenu.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
class LightMenu : public PSMenu
|
||||
{
|
||||
public:
|
||||
explicit LightMenu(menu_options_t* options);
|
||||
};
|
13
components/justus/include/ui/MainMenu.h
Normal file
13
components/justus/include/ui/MainMenu.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
class MainMenu final : public PSMenu {
|
||||
public:
|
||||
explicit MainMenu(menu_options_t* options);
|
||||
|
||||
private:
|
||||
void onSelect(uint8_t id) const;
|
||||
|
||||
menu_options_t* m_options;
|
||||
};
|
9
components/justus/include/ui/SettingsMenu.h
Normal file
9
components/justus/include/ui/SettingsMenu.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
class SettingsMenu : public PSMenu
|
||||
{
|
||||
public:
|
||||
explicit SettingsMenu(menu_options_t* options) ;
|
||||
};
|
14
components/justus/include/ui/SplashScreen.h
Normal file
14
components/justus/include/ui/SplashScreen.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "MenuOptions.h"
|
||||
#include "common/Widget.h"
|
||||
|
||||
class SplashScreen final : public Widget {
|
||||
public:
|
||||
explicit SplashScreen(menu_options_t* options);
|
||||
void update(uint64_t dt) override;
|
||||
void render() override;
|
||||
|
||||
private:
|
||||
menu_options_t* m_options;
|
||||
};
|
161
components/justus/src/common/PSMenu.cpp
Normal file
161
components/justus/src/common/PSMenu.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "common/ScrollBar.h"
|
||||
#include "PushButton.h"
|
||||
|
||||
PSMenu::PSMenu(menu_options_t* options)
|
||||
: Widget(options->u8g2)
|
||||
, m_options(options) {
|
||||
m_options->onButtonClicked = [this](const uint8_t button) { onButtonClicked(button); };
|
||||
}
|
||||
|
||||
PSMenu::~PSMenu() {
|
||||
m_options->onButtonClicked = nullptr;
|
||||
}
|
||||
|
||||
void PSMenu::render() {
|
||||
if(m_selected_item < 0) {
|
||||
onPressedDown();
|
||||
}
|
||||
|
||||
u8g2_SetDrawColor(u8g2, 0);
|
||||
u8g2_DrawBox(u8g2, 0, 0, u8g2->width, u8g2->height);
|
||||
|
||||
u8g2_SetDrawColor(u8g2, 1);
|
||||
|
||||
drawScrollBar();
|
||||
drawSelectionBox();
|
||||
|
||||
int x = 8; // sure?
|
||||
auto widget = m_items.at(m_selected_item);
|
||||
renderWidget(
|
||||
widget.getType(), u8g2_font_helvB08_tr, x, u8g2->height / 2 + 3, widget.getText().c_str());
|
||||
|
||||
if(m_selected_item > 0) {
|
||||
auto item = m_items.at(m_selected_item - 1);
|
||||
renderWidget(item.getType(), u8g2_font_haxrcorp4089_tr, x, 14, item.getText().c_str());
|
||||
}
|
||||
if(m_selected_item < m_items.size() - 1) {
|
||||
auto item = m_items.at(m_selected_item + 1);
|
||||
renderWidget(
|
||||
item.getType(),
|
||||
u8g2_font_haxrcorp4089_tr,
|
||||
x,
|
||||
u8g2->height - 10,
|
||||
item.getText().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::renderWidget(
|
||||
const uint8_t type,
|
||||
const uint8_t* font,
|
||||
const int x,
|
||||
const int y,
|
||||
const char* text) const {
|
||||
switch(type) {
|
||||
case 0: // text
|
||||
u8g2_SetFont(u8g2, font);
|
||||
u8g2_DrawStr(u8g2, x, y, text);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onButtonClicked(uint8_t button) {
|
||||
switch(button) {
|
||||
case BUTTON_UP:
|
||||
onPressedUp();
|
||||
break;
|
||||
|
||||
case BUTTON_DOWN:
|
||||
onPressedDown();
|
||||
break;
|
||||
|
||||
case BUTTON_LEFT:
|
||||
onPressedLeft();
|
||||
break;
|
||||
|
||||
case BUTTON_RIGHT:
|
||||
onPressedRight();
|
||||
break;
|
||||
|
||||
case BUTTON_SELECT:
|
||||
onPressedSelect();
|
||||
break;
|
||||
|
||||
case BUTTON_BACK:
|
||||
onPressedBack();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedDown() {
|
||||
if(m_selected_item == m_items.size() - 1) {
|
||||
m_selected_item = 0;
|
||||
} else {
|
||||
m_selected_item++;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedUp() {
|
||||
if(m_selected_item == 0) {
|
||||
m_selected_item = m_items.size() - 1;
|
||||
} else {
|
||||
m_selected_item--;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedLeft() {
|
||||
//
|
||||
}
|
||||
|
||||
void PSMenu::onPressedRight() {
|
||||
///
|
||||
}
|
||||
|
||||
void PSMenu::onPressedSelect() const {
|
||||
m_items.at(m_selected_item).callback(m_selected_item);
|
||||
}
|
||||
|
||||
void PSMenu::onPressedBack() const {
|
||||
if(m_options && m_options->popScreen) {
|
||||
m_options->popScreen();
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::addText(const std::string& text, const MenuCallback& callback) {
|
||||
m_items.emplace_back(0, text, callback);
|
||||
}
|
||||
|
||||
void PSMenu::addSwitch(const std::string& text, std::string& value, const MenuCallback& callback) {
|
||||
m_items.emplace_back(1, text, value, callback);
|
||||
}
|
||||
|
||||
void PSMenu::addNumber(const std::string& text, std::string& value, const MenuCallback& callback) {
|
||||
m_items.emplace_back(2, text, value, callback);
|
||||
}
|
||||
|
||||
void PSMenu::drawScrollBar() const {
|
||||
ScrollBar scrollBar(m_options, u8g2->width - 3, 3, 1, u8g2->height - 6);
|
||||
scrollBar.refresh(m_selected_item, m_items.size());
|
||||
scrollBar.render();
|
||||
}
|
||||
|
||||
void PSMenu::drawSelectionBox() const {
|
||||
const auto displayHeight = u8g2->height;
|
||||
const auto displayWidth = u8g2->width;
|
||||
constexpr auto rightPadding = 8;
|
||||
const auto boxHeight = displayHeight / 3;
|
||||
const auto y = boxHeight * 2 - 2;
|
||||
const auto x = displayWidth - rightPadding;
|
||||
|
||||
u8g2_DrawRFrame(u8g2, 2, boxHeight, displayWidth - rightPadding, boxHeight, 3);
|
||||
u8g2_DrawLine(u8g2, 4, y, displayWidth - rightPadding, y);
|
||||
u8g2_DrawLine(u8g2, x, y - boxHeight + 3, x, y - 1);
|
||||
}
|
42
components/justus/src/common/ScrollBar.cpp
Normal file
42
components/justus/src/common/ScrollBar.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "common/ScrollBar.h"
|
||||
|
||||
ScrollBar::ScrollBar(
|
||||
const menu_options_t* options,
|
||||
const size_t x,
|
||||
const size_t y,
|
||||
const size_t width,
|
||||
const size_t height)
|
||||
: Widget(options->u8g2)
|
||||
, m_x(x)
|
||||
, m_y(y)
|
||||
, m_width(width)
|
||||
, m_height(height)
|
||||
, m_value(0)
|
||||
, m_max(0)
|
||||
, m_min(0)
|
||||
, m_thumbHeight(0)
|
||||
, m_thumbY(0) {
|
||||
}
|
||||
|
||||
void ScrollBar::render() {
|
||||
if(1 == m_max) return;
|
||||
|
||||
// draw dotted line
|
||||
for(size_t y = m_y; y < m_y + m_height; y++) {
|
||||
if(y % 2 == 0) {
|
||||
u8g2_DrawPixel(u8g2, m_x, y);
|
||||
}
|
||||
}
|
||||
|
||||
// draw thumb
|
||||
u8g2_DrawBox(u8g2, u8g2->width - 4, m_thumbY, 3, m_thumbHeight);
|
||||
}
|
||||
|
||||
void ScrollBar::refresh(const size_t value, const size_t max, const size_t min) {
|
||||
m_value = value;
|
||||
m_max = max;
|
||||
m_min = min;
|
||||
|
||||
m_thumbHeight = m_height / m_max;
|
||||
m_thumbY = m_y + (m_value - m_min) * m_thumbHeight;
|
||||
}
|
14
components/justus/src/common/Widget.cpp
Normal file
14
components/justus/src/common/Widget.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "common/Widget.h"
|
||||
|
||||
Widget::Widget(u8g2_t* u8g2)
|
||||
: u8g2(u8g2) {
|
||||
}
|
||||
|
||||
void Widget::update(uint64_t dt) {
|
||||
}
|
||||
|
||||
void Widget::render() {
|
||||
}
|
||||
|
||||
void Widget::onButtonClicked(uint8_t button) {
|
||||
}
|
54
components/justus/src/data/MenuItem.cpp
Normal file
54
components/justus/src/data/MenuItem.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "data/MenuItem.h"
|
||||
|
||||
MenuItem::MenuItem(const uint8_t type, std::string text, std::function<void(uint8_t)> callback)
|
||||
: m_type(type)
|
||||
, m_text(std::move(text))
|
||||
, m_callback(std::move(callback)) {
|
||||
}
|
||||
|
||||
MenuItem::MenuItem(
|
||||
const uint8_t type,
|
||||
std::string text,
|
||||
std::string value,
|
||||
std::function<void(uint8_t)> callback)
|
||||
: m_type(type)
|
||||
, m_text(std::move(text))
|
||||
, m_value(std::move(value))
|
||||
, m_callback(std::move(callback)) {
|
||||
}
|
||||
|
||||
uint8_t MenuItem::getType() const {
|
||||
return m_type;
|
||||
}
|
||||
|
||||
const std::string &MenuItem::getText() const
|
||||
{
|
||||
return m_text;
|
||||
}
|
||||
|
||||
const std::string &MenuItem::getValue() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void MenuItem::setValue(const std::string &value)
|
||||
{
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
void MenuItem::callback(const uint8_t id) const
|
||||
{
|
||||
if (m_callback)
|
||||
{
|
||||
m_callback(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
///
|
||||
}
|
||||
}
|
||||
|
||||
bool MenuItem::hasCallback() const
|
||||
{
|
||||
return (m_callback != nullptr);
|
||||
}
|
12
components/justus/src/ui/LightMenu.cpp
Normal file
12
components/justus/src/ui/LightMenu.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "ui/LightMenu.h"
|
||||
|
||||
void demoL(uint8_t id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
LightMenu::LightMenu(menu_options_t* options) : PSMenu(options)
|
||||
{
|
||||
addText("Tag/Nacht", nullptr);
|
||||
addText("LED Einstellungen", demoL);
|
||||
}
|
27
components/justus/src/ui/MainMenu.cpp
Normal file
27
components/justus/src/ui/MainMenu.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "ui/MainMenu.h"
|
||||
|
||||
#include "common/Widget.h"
|
||||
#include "ui/LightMenu.h"
|
||||
#include "ui/SettingsMenu.h"
|
||||
|
||||
MainMenu::MainMenu(menu_options_t* options)
|
||||
: PSMenu(options)
|
||||
, m_options(options) {
|
||||
addText("Lichtsteuerung", [this](const uint8_t button) { onSelect(button); });
|
||||
addText("Einstellungen", [this](const uint8_t button) { onSelect(button); });
|
||||
}
|
||||
|
||||
void MainMenu::onSelect(const uint8_t id) const {
|
||||
std::shared_ptr<Widget> widget;
|
||||
switch(id) {
|
||||
case 0:
|
||||
widget = std::make_shared<LightMenu>(m_options);
|
||||
break;
|
||||
case 1:
|
||||
widget = std::make_shared<SettingsMenu>(m_options);
|
||||
break;
|
||||
}
|
||||
if(m_options && m_options->pushScreen) {
|
||||
m_options->pushScreen(widget);
|
||||
}
|
||||
}
|
11
components/justus/src/ui/SettingsMenu.cpp
Normal file
11
components/justus/src/ui/SettingsMenu.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "ui/SettingsMenu.h"
|
||||
|
||||
void demo(uint8_t id)
|
||||
{
|
||||
///
|
||||
}
|
||||
|
||||
SettingsMenu::SettingsMenu(menu_options_t* options) : PSMenu(options)
|
||||
{
|
||||
addText("OTA Einspielen", demo);
|
||||
}
|
37
components/justus/src/ui/SplashScreen.cpp
Normal file
37
components/justus/src/ui/SplashScreen.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ui/SplashScreen.h"
|
||||
|
||||
#include "ui/MainMenu.h"
|
||||
|
||||
#ifdef ESP32
|
||||
#else
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
uint64_t counter = 0;
|
||||
|
||||
SplashScreen::SplashScreen(menu_options_t* options)
|
||||
: Widget(options->u8g2)
|
||||
, m_options(options) {
|
||||
}
|
||||
|
||||
void SplashScreen::update(const uint64_t dt) {
|
||||
counter += dt;
|
||||
if(counter > 200000) {
|
||||
counter = 0;
|
||||
if(m_options && m_options->setScreen) {
|
||||
m_options->setScreen(std::make_shared<MainMenu>(m_options));
|
||||
}
|
||||
}
|
||||
#ifndef ESP32
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
#endif
|
||||
}
|
||||
|
||||
void SplashScreen::render() {
|
||||
u8g2_SetFont(u8g2, u8g2_font_DigitalDisco_tr);
|
||||
u8g2_DrawStr(u8g2, 28, u8g2->height / 2 - 10, "HO Anlage");
|
||||
u8g2_DrawStr(u8g2, 30, u8g2->height / 2 + 5, "Axel Janz");
|
||||
u8g2_SetFont(u8g2, u8g2_font_haxrcorp4089_tr);
|
||||
u8g2_DrawStr(u8g2, 35, 50, "Initialisierung...");
|
||||
}
|
Reference in New Issue
Block a user