Skip to content

Commit

Permalink
Merge pull request #26 from SeedLabs-it/1-onboarding-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhampuswall authored Jan 19, 2024
2 parents 8379357 + b35fff7 commit 23acde3
Show file tree
Hide file tree
Showing 32 changed files with 981 additions and 294 deletions.
10 changes: 5 additions & 5 deletions firmware/src/app_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ void AppTask::run()
log("Giving 0.5s for Apps to initialize");
delay(500);

apps->setActive(0);
apps->setActive(menu_type, 0);
applyConfig(apps->getActiveMotorConfig(), false);
motor_task_.addListener(knob_state_queue_);

plaintext_protocol_.init([this]()
{ changeConfig(0); },
{ changeConfig(std::make_pair(menu_type, 0)); },
[this]()
{
if (!configuration_loaded_)
Expand Down Expand Up @@ -199,7 +199,7 @@ void AppTask::run()
if (app_state.screen_state.has_been_engaged == false &&
app_state.screen_state.brightness > app_state.screen_state.MIN_LCD_BRIGHTNESS &&
millis() > app_state.screen_state.awake_until)
{
{
// TODO, this can be timed better (ideally by subtracting MAX_BRIGHTNESS - MIN_BRIGHTNESS/fps/second_of_animation)
app_state.screen_state.brightness = app_state.screen_state.brightness - 1000;
if (app_state.screen_state.brightness < app_state.screen_state.MIN_LCD_BRIGHTNESS)
Expand Down Expand Up @@ -291,9 +291,9 @@ void AppTask::log(const char *msg)
xQueueSendToBack(log_queue_, &msg_str, 0);
}

void AppTask::changeConfig(uint32_t id)
void AppTask::changeConfig(std::pair<app_types, uint8_t> next)
{
apps->setActive(id);
apps->setActive(next.first, next.second); // TODO LOOK OVER

applyConfig(apps->getActiveMotorConfig(), false);
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/app_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class AppTask : public Task<AppTask>,
SerialProtocolPlaintext plaintext_protocol_;
SerialProtocolProtobuf proto_protocol_;

void changeConfig(uint32_t id);
void changeConfig(std::pair<app_types, uint8_t> next);
void updateHardware(AppState app_state);
void publishState();
void applyConfig(PB_SmartKnobConfig config, bool from_remote);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ PrinterChamberApp::PrinterChamberApp(TFT_eSprite *spr_, std::string entity_name)
friendly_name = "3D Printer";
}

uint8_t PrinterChamberApp::navigationNext()
{
// back to menu
return 0;
}

EntityStateUpdate PrinterChamberApp::updateStateFromKnob(PB_SmartKnobState state)
{
wanted_temperature_position = state.current_position;
Expand Down
1 change: 0 additions & 1 deletion firmware/src/apps/3d_printer_chamber.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class PrinterChamberApp : public App
TFT_eSprite *render();
EntityStateUpdate updateStateFromKnob(PB_SmartKnobState state);
void updateStateFromSystem(AppState state);
uint8_t navigationNext();

private:
uint8_t current_temperature = 0;
Expand Down
20 changes: 17 additions & 3 deletions firmware/src/apps/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ const char APP_SLUG_3D_PRINTER[48] = "3d_printer";
const char APP_SLUG_LIGHT_DIMMER[48] = "light_dimmer";
const char APP_SLUG_LIGHT_SWITCH[48] = "light_switch";
const char APP_SLUG_STOPWATCH[48] = "stopwatch";

enum app_types
{
menu_type = 1,
apps_type = 2
};

typedef uint8_t id;
class App
{
public:
const app_types type = apps_type;
App(TFT_eSprite *spr_)
{
this->spr_ = spr_;
}
virtual ~App() {}
};
virtual TFT_eSprite *render();
virtual EntityStateUpdate updateStateFromKnob(PB_SmartKnobState state);
virtual void updateStateFromSystem(AppState state);
Expand All @@ -35,7 +43,13 @@ class App
return "App";
}

virtual uint8_t navigationNext();
virtual std::pair<app_types, uint8_t> navigationNext()
{
if (type == menu_type)
return std::make_pair(apps_type, 1);

return std::make_pair(menu_type, 0);
}

const unsigned char *small_icon;
const unsigned char *big_icon;
Expand Down
72 changes: 38 additions & 34 deletions firmware/src/apps/menu.cpp → firmware/src/apps/app_menu.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "menu.h"
#include "app_menu.h"

MenuApp::MenuApp(TFT_eSprite *spr_) : App(spr_)
{
sprintf(room, "%s", "Office");

// mutex
mutex = xSemaphoreCreateMutex();

motor_config = PB_SmartKnobConfig{
1,
0,
0,
0,
1,
0,
-1, // max position < min position indicates no bounds
25 * PI / 180,
Expand All @@ -25,6 +28,9 @@ MenuApp::MenuApp(TFT_eSprite *spr_) : App(spr_)
EntityStateUpdate MenuApp::updateStateFromKnob(PB_SmartKnobState state)
{
// TODO: cache menu size
// might be needed to make sure first item is selected on "reload" but might fuck if user returns from app to menu again
// motor_config.position_nonce = 1;
// motor_config.position = 1;

int32_t position_for_menu_calc = state.current_position;

Expand All @@ -44,28 +50,16 @@ EntityStateUpdate MenuApp::updateStateFromKnob(PB_SmartKnobState state)

void MenuApp::updateStateFromSystem(AppState state) {}

uint8_t MenuApp::navigationNext()
std::pair<app_types, uint8_t> MenuApp::navigationNext()
{
return current_menu_position + 1; // +1 to shift from 0 position which is menu itself
return std::make_pair(type, find_item(current_menu_position)->app_id);
}

TFT_eSprite *MenuApp::render()
{
// ESP_LOGD("menu.cpp", "called real method");
// return;
// spr_->fillRect(0, 0, TFT_WIDTH, TFT_HEIGHT, TFT_ORANGE);

// spr_->setTextColor(TFT_BLACK);
// spr_->setFreeFont(&Roboto_Thin_24);
// spr_->drawString("Menu", TFT_WIDTH / 2, TFT_HEIGHT / 2, 1);

// current_menu_position = 0;
lock();
current_item = find_item(current_menu_position);

// ESP_LOGD("menu.cpp", "pre-render");

MenuItem current_item = find_item(current_menu_position);
MenuItem prev_item;
MenuItem next_item;
if (current_menu_position == 0)
{
prev_item = find_item(items.size() - 1);
Expand All @@ -81,9 +75,9 @@ TFT_eSprite *MenuApp::render()
prev_item = find_item(current_menu_position - 1);
next_item = find_item(current_menu_position + 1);
}
// ESP_LOGD("menu.cpp", "mid-render");
render_menu_screen(current_item, prev_item, next_item);
// ESP_LOGD("menu.cpp", "post-render");
unlock();

render_menu_screen();
return this->spr_;
}

Expand All @@ -93,21 +87,21 @@ std::string MenuApp::getClassName()
}

// TODO: add protection, could cause panic
MenuItem MenuApp::find_item(uint8_t id)
MenuItemOld *MenuApp::find_item(uint8_t id)
{
return (*items.find(id)).second;
return &(*items.find(id)).second;
}

// TODO: add protection of overwriting same items
void MenuApp::add_item(uint8_t id, MenuItem item)
void MenuApp::add_item(uint8_t id, MenuItemOld item)
{
// items[id] = item;
items.insert(std::make_pair(id, item));
}

void MenuApp::render_menu_screen(MenuItem current, MenuItem prev, MenuItem next)
void MenuApp::render_menu_screen()
{
uint32_t color_active = current.color;
lock();
uint32_t color_active = current_item->color;
uint32_t color_inactive = spr_->color565(150, 150, 150);
uint32_t label_color = color_inactive;
uint32_t background = spr_->color565(0, 0, 0);
Expand All @@ -132,16 +126,26 @@ void MenuApp::render_menu_screen(MenuItem current, MenuItem prev, MenuItem next)
spr_->setFreeFont(&Roboto_Thin_Bold_24);
spr_->drawString(room, center_h, label_vertical_offset + room_lable_h / 2 - 1, 1);

spr_->drawBitmap(center_h - icon_size_active / 2, center_v - icon_size_active / 2, current.big_icon, icon_size_active, icon_size_active, color_active, background);
// ESP_LOGD("menu.cpp", "%s", current.screen_name);
spr_->drawBitmap(center_h - icon_size_active / 2, center_v - icon_size_active / 2, current_item->big_icon, icon_size_active, icon_size_active, color_active, background);

// left one
spr_->drawBitmap(center_h - icon_size_active / 2 - 20 - icon_size_inactive, center_v - icon_size_inactive / 2, prev.small_icon, icon_size_inactive, icon_size_inactive, color_inactive, background);
spr_->drawBitmap(center_h - icon_size_active / 2 - 20 - icon_size_inactive, center_v - icon_size_inactive / 2, prev_item->small_icon, icon_size_inactive, icon_size_inactive, color_inactive, background);

// right one
spr_->drawBitmap(center_h + icon_size_active / 2 + 20, center_v - icon_size_inactive / 2, next.small_icon, icon_size_inactive, icon_size_inactive, color_inactive, background);
spr_->drawBitmap(center_h + icon_size_active / 2 + 20, center_v - icon_size_inactive / 2, next_item->small_icon, icon_size_inactive, icon_size_inactive, color_inactive, background);

spr_->setTextColor(color_active);
spr_->setFreeFont(&Roboto_Thin_24);
spr_->drawString(current.screen_name, center_h, center_v + icon_size_active / 2 + 30, 1);
};
spr_->drawString(current_item->screen_name, center_h, center_v + icon_size_active / 2 + 30, 1);
unlock();
};

void MenuApp::lock()
{
xSemaphoreTake(mutex, portMAX_DELAY);
}

void MenuApp::unlock()
{
xSemaphoreGive(mutex);
}
48 changes: 48 additions & 0 deletions firmware/src/apps/app_menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include "app.h"
#include "font/roboto_thin_bold_24.h"

#include <map>

const uint8_t SCREEN_NAME_LENGTH = 20;
const uint8_t MEX_MENU_ITEMS = 12;

struct MenuItemOld
{
const char *screen_name;
uint16_t app_id;
uint32_t color;
const unsigned char *small_icon;
const unsigned char *big_icon;
};

class MenuApp : public App
{
public:
MenuApp(TFT_eSprite *spr_);
TFT_eSprite *render();
EntityStateUpdate updateStateFromKnob(PB_SmartKnobState state);
void updateStateFromSystem(AppState state);

void add_item(uint8_t id, MenuItemOld item);
MenuItemOld *find_item(uint8_t id);
std::string getClassName();
std::pair<app_types, uint8_t> navigationNext();

void lock();

private:
uint8_t menu_items_count = 0;
uint8_t current_menu_position = 0;
std::map<uint8_t, MenuItemOld> items;
char room[12];
void render_menu_screen();

SemaphoreHandle_t mutex;

MenuItemOld *current_item;
MenuItemOld *prev_item;
MenuItemOld *next_item;

void unlock();
};
Loading

0 comments on commit 23acde3

Please sign in to comment.