Skip to content

Commit

Permalink
Fully implemented scene with menu and appbar renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Aug 31, 2023
1 parent 1dd67d7 commit c6349d6
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/planet096_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ Adafruit_SSD1306 Planet096App::getI2CScreen() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
return NULL;

display.clearDisplay();
return display;
}

void Planet096App::start(Planet096Scene scene) {
scene.render(Planet096App::getI2CScreen());
void Planet096App::start(Planet096Scene &scene) {
scene.render();
}
2 changes: 1 addition & 1 deletion src/planet096_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Planet096App {
public:
static Adafruit_SSD1306 getI2CScreen();
static void start(Planet096Scene scene);
static void start(Planet096Scene &scene);
};

#endif
167 changes: 141 additions & 26 deletions src/planet096_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@
#include <Adafruit_SSD1306.h>
#include <List.hpp>

#include "planet096_app.h"
#include "planet096_scene.h"

Planet096Scene::Planet096Scene() { }
Planet096Scene::Planet096Scene() {
this->display = Planet096App::getI2CScreen();
}

Planet096Scene::Planet096Scene(String title):
title(title) { }
Planet096Scene::Planet096Scene(const char* title):
title(title) {
this->display = Planet096App::getI2CScreen();
}

void Planet096Scene::setAppBarTitle(String title) {
void Planet096Scene::setAppBarTitle(const char* title) {
this->title = title;

if(this->has_rendered)
this->renderAppBar();
}

String Planet096Scene::getAppBarTitle() {
const char* Planet096Scene::getAppBarTitle() {
return this->title;
}

void Planet096Scene::setAppBarStyle(uint8_t appbar_style) {
this->appbar_style = appbar_style;

if(this->has_rendered)
this->renderAppBar();
}

uint8_t Planet096Scene::getAppbarStyle() {
Expand All @@ -27,43 +38,147 @@ uint8_t Planet096Scene::getAppbarStyle() {

void Planet096Scene::setAppBarAlignment(uint8_t appbar_align) {
this->appbar_align = appbar_align;

if(this->has_rendered)
this->renderAppBar();
}

uint8_t Planet096Scene::getAppBarAlignment() {
return this->appbar_align;
}

void Planet096Scene::render(Adafruit_SSD1306 display) {
display.clearDisplay();
display.setTextSize(1);
void Planet096Scene::setSceneMenu(
const char* menu_left,
const char* menu_right
) {
this->menu_left = menu_left;
this->menu_right = menu_right;

if(this->appbar_style == APPBAR_NONE)
return;
if(this->has_rendered)
this->renderMenu();
}

switch(this->appbar_align) {
case APPBAR_ALIGN_CENTER:
display.setCursor(59 - ((this->title.length() * 5) / 2), 2);
break;
case APPBAR_ALIGN_LEFT:
display.setCursor(4, 2);
break;
case APPBAR_ALIGN_RIGHT:
display.setCursor((124 - this->title.length()) - (this->title.length() * 5), 2);
break;
void Planet096Scene::setSceneMenu(
const char* menu_left,
const char* menu_center,
const char* menu_right
) {
this->menu_left = menu_left;
this->menu_right = menu_right;
this->menu_center = menu_center;

if(this->has_rendered)
this->renderMenu();
}

const char** Planet096Scene::getSceneMenu() {
const char* menus[this->menu_center != "_" ? 3 : 2];
int idx = 0;

if(this->menu_left != NULL)
menus[idx++] = this->menu_left;

if(this->menu_center != NULL)
menus[idx++] = this->menu_center;

if(this->menu_right != NULL)
menus[idx] = this->menu_right;

return menus;
}

void Planet096Scene::setSceneMenuStyle(uint8_t scene_menu_style) {
this->scene_menu_style = scene_menu_style;

if(this->has_rendered)
this->renderMenu();
}

uint8_t Planet096Scene::getSceneMenuStyle() {
return this->scene_menu_style;
}

void Planet096Scene::renderAppBar() {
this->display.fillRect(0, 0, 128, 10, BLACK);
this->display.setTextSize(1);

if(this->appbar_style != APPBAR_NONE) {
int title_length = strlen(this->title);
switch(this->appbar_align) {
case APPBAR_ALIGN_CENTER:
this->display.setCursor(59 - ((title_length * 5) / 2), 2);
break;
case APPBAR_ALIGN_LEFT:
this->display.setCursor(4, 2);
break;
case APPBAR_ALIGN_RIGHT:
this->display.setCursor((124 - title_length) - (title_length * 5), 2);
break;
}
}

switch(this->appbar_style) {
case APPBAR_NONE:
break;

case APPBAR_NORMAL:
display.fillRect(0, 0, 128, 10, WHITE);
display.setTextColor(BLACK, WHITE);
this->display.fillRect(0, 0, 128, 10, WHITE);
this->display.setTextColor(BLACK, WHITE);

break;

case APPBAR_LINE_BORDER:
display.drawLine(0, 10, 128, 10, WHITE);
display.setTextColor(WHITE, BLACK);
this->display.drawLine(0, 10, 128, 10, WHITE);
this->display.setTextColor(WHITE, BLACK);

break;
}

display.println(this->title);
display.display();
this->display.println(this->title);
this->display.display();
}

void Planet096Scene::renderMenu() {
switch(this->scene_menu_style) {
case SCENE_MENU_BUTTONS:
if(this->menu_center != NULL) {
this->display.drawRect(4, 46, 38, 12, WHITE);
this->display.setTextSize(1);
this->display.setTextColor(WHITE);
this->display.setCursor(6 + ((32 - (strlen(this->menu_left) * 5)) / 2), 48);
this->display.println(this->menu_left);

this->display.drawRect(46, 46, 38, 12, WHITE);
this->display.setTextSize(1);
this->display.setTextColor(WHITE);
this->display.setCursor(48 + ((32 - (strlen(this->menu_center) * 5)) / 2), 48);
this->display.println(this->menu_center);

this->display.drawRect(88, 46, 38, 12, WHITE);
this->display.setTextSize(1);
this->display.setTextColor(WHITE);
this->display.setCursor(90 + ((32 - (strlen(this->menu_right) * 5)) / 2), 48);
this->display.println(this->menu_right);
}

break;
case SCENE_MENU_DIVIDER:
break;
case SCENE_MENU_PLAIN:
break;
}

this->display.display();
}

void Planet096Scene::renderWidgets() { }

void Planet096Scene::render() {
this->display.clearDisplay();

this->renderAppBar();
this->renderWidgets();
this->renderMenu();

this->has_rendered = true;
}
48 changes: 41 additions & 7 deletions src/planet096_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <Adafruit_SSD1306.h>
#include <List.hpp>

#include <planet096_app.h>

#define APPBAR_NONE 0x00
#define APPBAR_NORMAL 0x01
#define APPBAR_LINE_BORDER 0x02
Expand All @@ -13,27 +15,59 @@
#define APPBAR_ALIGN_CENTER 0x01
#define APPBAR_ALIGN_RIGHT 0x02

#define SCENE_MENU_BUTTONS 0x00
#define SCENE_MENU_DIVIDER 0x01
#define SCENE_MENU_PLAIN 0x02

class Planet096Scene {
public:
Planet096Scene();
Planet096Scene(String title);
Planet096Scene(const char* title);

void setAppBarTitle(String title);
String getAppBarTitle();
void setAppBarTitle(const char* title);
const char* getAppBarTitle();

void setAppBarStyle(uint8_t appbar_style);
uint8_t getAppbarStyle();

void setAppBarAlignment(uint8_t appbar_align);
uint8_t getAppBarAlignment();

void render(Adafruit_SSD1306 display);
void setSceneMenu(
const char* menu_left,
const char* menu_right
);

void setSceneMenu(
const char* menu_left,
const char* menu_center,
const char* menu_right
);

const char** getSceneMenu();

void setSceneMenuStyle(uint8_t scene_menu_style);
uint8_t getSceneMenuStyle();

void render();

private:
String title;
char *title = "_";
char *menu_left = NULL,
*menu_center = NULL,
*menu_right = NULL;

uint8_t appbar_style = APPBAR_NORMAL;
uint8_t appbar_align = APPBAR_ALIGN_CENTER;
uint8_t scene_menu_style = SCENE_MENU_BUTTONS;

bool has_rendered = false;

Adafruit_SSD1306 display;

uint8_t appbar_style;
uint8_t appbar_align;
void renderAppBar();
void renderMenu();
void renderWidgets();
};

#endif

0 comments on commit c6349d6

Please sign in to comment.