From f3409daff964c1e81cebbff0f3dc942dad031d39 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Sun, 19 Sep 2021 17:16:59 -0400 Subject: [PATCH] Add panel boilerplate and weather boilerplate --- src/board.cpp | 54 +++++++++++++++++++++++++++++++++++- src/board.hpp | 10 ++++--- src/config.def.hpp | 13 +++++++-- src/panel/panel.hpp | 2 +- src/panel/weather.cpp | 50 +++++++++++++++++++++++++++++++++ src/panel/weather.hpp | 33 ++++++++++++++++++++++ src/panel/weather_config.hpp | 21 ++++++++++++++ 7 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 src/panel/weather.cpp create mode 100644 src/panel/weather.hpp create mode 100644 src/panel/weather_config.hpp diff --git a/src/board.cpp b/src/board.cpp index e1bb378..8775ac1 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -370,12 +370,64 @@ int board::init(){ // This is where most of the logic lives void board::start(){ initConstResources(); + //get Panel size + const size_t panel_count = sizeof(PANELS) / sizeof(PANELS[0]); + size_t i = 0; + + //timing, for dealing with framerates + auto start = std::chrono::high_resolution_clock::now(); + static const std::chrono::nanoseconds s{1000000000}; + + //frame counter + static unsigned long long int fcount = 0; SDL_Log("Starting main loop...\n"); for(;;){ + start = std::chrono::high_resolution_clock::now(); + fcount++; + //check if we can loop back over + if(i > panel_count) + i = 0; + + //SDL_RenderClear(_renderer); + + //PLACEHOLDER, cycle color + { + static uint8_t red = 0; + static bool up = true; + + SDL_SetRenderDrawColor(_renderer, red, + BOARD_GREEN, BOARD_BLUE, SDL_ALPHA_OPAQUE); + + SDL_RenderClear(_renderer); + + if(up){ + if(red == 254) + up = false; + red++; + } else { + if(red == 1) + up = true; + red --; + } + } + //END PLACEHOLDER + + + //call draw on the current panel + PANELS[i]->draw(); + + std::cerr << "Current frame: " << fcount << "\n"; + + SDL_RenderPresent(_renderer); + + + //wait for frame + std::this_thread::sleep_for((s / MAX_FRAMERATE) - + std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - start)); } - } /////////////////////////////////////// diff --git a/src/board.hpp b/src/board.hpp index 104ce79..26e6178 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -9,10 +9,13 @@ #include "config.hpp" #include "util/lru.hpp" +#include "panel/panel.hpp" #include #include #include +#include +#include #include #include @@ -29,7 +32,6 @@ namespace dashboard { bool operator< (const font_and_size&) const; bool operator>=(const font_and_size&) const; bool operator<=(const font_and_size&) const; - }; struct string_and_font { @@ -126,7 +128,6 @@ namespace dashboard { SDL_Texture* setImage (const std::string&); TTF_Font* setFont (const font_and_size&); - //TODO make set* functions that accept SDL_*_Wrapper objects SDL_Texture* setString(const string_and_font&, const SDL_Texture_Wrapper&); SDL_Texture* setImage (const std::string&, const SDL_Texture_Wrapper&); TTF_Font* setFont (const font_and_size&, const SDL_Font_Wrapper&); @@ -138,11 +139,12 @@ namespace dashboard { inline static std::unordered_map _strings; - static clortox::LRUCache _dynamic_strings; - + //TODO: Dynamic images? //TODO: Dynamic Fonts? + //local pointers to the globals SDL_Window* _window; diff --git a/src/config.def.hpp b/src/config.def.hpp index 185e1d8..787fbd2 100644 --- a/src/config.def.hpp +++ b/src/config.def.hpp @@ -71,6 +71,16 @@ constexpr int IMG_FLAGS = 0 //| IMG_INIT_TIF ; +// PANELS +// A list of all panels to be displayed, in order +// Be sure to include the panel definition below, then add it to the system +#include "panel/panel.hpp" +#include "panel/weather.hpp" +static dashboard::panel::panel* PANELS[] = { + new dashboard::panel::weather(), + +}; + // DATA_DIR // Where all resources will be // Keep this as DATA to use the install dir set in the makefile @@ -81,9 +91,6 @@ constexpr char DATA_DIR[] = DATA_ ; // Keep this a DATA_IMG_ to use the DATA_IMG dir defined in the makefile constexpr char DATA_IMG[] = DATA_IMG_; -//TODO: Add directory prefix for each of these, and change it so it doesnt use -//that whole path as the key for the file - // IMAGE_LOCATIONS // Locations of all static images used static const char* IMAGE_LOCATIONS[] = { diff --git a/src/panel/panel.hpp b/src/panel/panel.hpp index 172890a..b1dee5b 100644 --- a/src/panel/panel.hpp +++ b/src/panel/panel.hpp @@ -17,7 +17,7 @@ #include #include -namespace dashboard { +namespace dashboard::panel { class panel { public: panel() = default; diff --git a/src/panel/weather.cpp b/src/panel/weather.cpp new file mode 100644 index 0000000..005a654 --- /dev/null +++ b/src/panel/weather.cpp @@ -0,0 +1,50 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 19-9-21 +// weather panel +// + +#include "weather.hpp" + +using namespace dashboard::panel; + +/////////////////////////////////////////////////////////////////////////////// +// Constructors /////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +weather::weather(){ + _time_on_screen = WEATHER_DEFAULT_ON_SCREEN_TIME; + _update_interval = std::chrono::milliseconds{UPDATE_INTERVAL}; + _last_update = std::chrono::high_resolution_clock::now(); +} + +weather::~weather(){ + +} + +/////////////////////////////////////////////////////////////////////////////// +// Draw function ////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +void weather::draw(){ + std::cerr << "WEATHER DRAW FUNC\n"; + std::cerr << "url_source : " << WEATHER_URL_SOURCE << "\n"; + + //check if its time to update + if((std::chrono::high_resolution_clock::now() - _last_update) > _update_interval){ + //TODO multithread this + update(); + } + +} + +/////////////////////////////////////////////////////////////////////////////// +// Helper functions /////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +void weather::update() { + std::cerr << "WEATHER::UPDATE\n"; + _last_update = std::chrono::high_resolution_clock::now(); + + //fetch updates +} diff --git a/src/panel/weather.hpp b/src/panel/weather.hpp new file mode 100644 index 0000000..8a0d914 --- /dev/null +++ b/src/panel/weather.hpp @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 19-9-21 +// weather panel +// + +#pragma once + +#include "panel.hpp" + +#include "weather_config.hpp" +#include + +#include +#include +#include + +#include + +namespace dashboard::panel { + class weather : public panel { + public: + weather(); + ~weather(); + + void draw(); + + private: + std::chrono::time_point _last_update; + std::chrono::milliseconds _update_interval; + void update(); + }; +} diff --git a/src/panel/weather_config.hpp b/src/panel/weather_config.hpp new file mode 100644 index 0000000..7045848 --- /dev/null +++ b/src/panel/weather_config.hpp @@ -0,0 +1,21 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 19-9-21 +// weather panel configuration +// + +#pragma once + +namespace dashboard::panel { + //New York RSS feed + static const char* WEATHER_URL_SOURCE = "http://rss.accuweather.com/rss/liveweather_rss.asp?locCode=10007"; + + //Default time the slide is shown on screen, in ms + //Default 5s + constexpr size_t WEATHER_DEFAULT_ON_SCREEN_TIME = 5000; + + //How long should we wait between updates? in ms + //Default 1 hour + constexpr int UPDATE_INTERVAL = 3600000; +} +