-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add power devices menu #75
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
696fbb3
add power devices menu
kevdliu ca7de69
add icon for power devices menu
kevdliu 4101d22
Merge branch 'main' of https://github.com/ballaswag/guppyscreen into …
kevdliu e41d4f4
Merge branch 'ballaswag:main' into power_devices
kevdliu 2e5f7f9
Merge branch 'power_devices' of https://github.com/kevdliu/guppyscree…
kevdliu 8dc22e9
recreate views when creating devices
kevdliu d0ac52c
use print icon for zbolt
kevdliu fbde711
fix zbolt image
kevdliu 1f7dcaf
Merge branch 'main' of https://github.com/ballaswag/guppyscreen into …
kevdliu 364c33c
Merge branch 'main' of https://github.com/ballaswag/guppyscreen into …
kevdliu 33aaef9
fix duplicate entries
kevdliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#include "power_panel.h" | ||
#include "utils.h" | ||
#include "spdlog/spdlog.h" | ||
|
||
#include <map> | ||
|
||
LV_IMG_DECLARE(back); | ||
|
||
PowerPanel::PowerPanel(KWebSocketClient &websocket_client, std::mutex &l) | ||
: ws(websocket_client) | ||
, lv_lock(l) | ||
, cont(lv_obj_create(lv_scr_act())) | ||
, back_btn(cont, &back, "Back", &PowerPanel::_handle_callback, this) | ||
{ | ||
lv_obj_move_background(cont); | ||
lv_obj_set_size(cont, LV_PCT(100), LV_PCT(100)); | ||
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN); | ||
lv_obj_set_flex_align(cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START); | ||
|
||
lv_obj_add_flag(back_btn.get_container(), LV_OBJ_FLAG_FLOATING); | ||
lv_obj_align(back_btn.get_container(), LV_ALIGN_BOTTOM_RIGHT, 0, 0); | ||
} | ||
|
||
PowerPanel::~PowerPanel() { | ||
if (cont != NULL) { | ||
lv_obj_del(cont); | ||
cont = NULL; | ||
} | ||
|
||
devices.clear(); | ||
} | ||
|
||
void PowerPanel::create_device(json &j) { | ||
std::string name = j["device"].template get<std::string>(); | ||
|
||
lv_obj_t *power_device_toggle; | ||
auto entry = devices.find(name); | ||
if (entry == devices.end()) { | ||
lv_obj_t *power_device_toggle_cont = lv_obj_create(cont); | ||
lv_obj_set_size(power_device_toggle_cont, LV_PCT(100), LV_SIZE_CONTENT); | ||
lv_obj_set_style_pad_all(power_device_toggle_cont, 0, 0); | ||
|
||
lv_obj_t *l = lv_label_create(power_device_toggle_cont); | ||
lv_label_set_text(l, name.c_str()); | ||
lv_obj_align(l, LV_ALIGN_LEFT_MID, 0, 0); | ||
|
||
power_device_toggle = lv_switch_create(power_device_toggle_cont); | ||
lv_obj_align(power_device_toggle, LV_ALIGN_RIGHT_MID, 0, 0); | ||
|
||
lv_obj_add_event_cb(power_device_toggle, &PowerPanel::_handle_callback, | ||
LV_EVENT_VALUE_CHANGED, this); | ||
|
||
devices.insert({name, power_device_toggle}); | ||
} else { | ||
power_device_toggle = entry->second; | ||
} | ||
|
||
std::string status = j["status"].template get<std::string>(); | ||
spdlog::debug("Fetched initial status for power device {}: {}", name, status); | ||
|
||
if (status == "on") { | ||
lv_obj_add_state(power_device_toggle, LV_STATE_CHECKED); | ||
} else { | ||
lv_obj_clear_state(power_device_toggle, LV_STATE_CHECKED); | ||
} | ||
} | ||
|
||
void PowerPanel::create_devices(json &j) { | ||
std::lock_guard<std::mutex> lock(lv_lock); | ||
|
||
if (j.contains("result")) { | ||
json result = j["result"]; | ||
if (result.contains("devices")) { | ||
json devices = result["devices"]; | ||
for (auto &device : devices.items()) { | ||
create_device(device.value()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void PowerPanel::handle_device_callback(json &j) { | ||
std::lock_guard<std::mutex> lock(lv_lock); | ||
|
||
if (j.contains("result")) { | ||
json result = j["result"]; | ||
for (auto &device : result.items()) { | ||
auto entry = devices.find(device.key()); | ||
if (entry != devices.end()) { | ||
std::string new_status = device.value(); | ||
|
||
spdlog::debug("Fetched new status for power device {}: {}", entry->first, new_status); | ||
|
||
if (new_status == "on") { | ||
lv_obj_add_state(entry->second, LV_STATE_CHECKED); | ||
} else { | ||
lv_obj_clear_state(entry->second, LV_STATE_CHECKED); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void PowerPanel::foreground() { | ||
lv_obj_move_foreground(cont); | ||
|
||
json params; | ||
for (auto &device : devices) { | ||
params[device.first] = 0; // value is ignored by moonraker | ||
} | ||
ws.send_jsonrpc("machine.device_power.status", params, [this](json& j) { | ||
this->handle_device_callback(j); | ||
}); | ||
} | ||
|
||
void PowerPanel::handle_callback(lv_event_t *e) { | ||
if (lv_event_get_code(e) == LV_EVENT_CLICKED) { | ||
lv_obj_t *btn = lv_event_get_current_target(e); | ||
if (btn == back_btn.get_container()) { | ||
lv_obj_move_background(cont); | ||
} | ||
} else if (lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED) { | ||
lv_obj_t *obj = lv_event_get_target(e); | ||
for (auto &device : devices) { | ||
if (obj == device.second) { | ||
bool turnOn = lv_obj_has_state(device.second, LV_STATE_CHECKED); | ||
std::string status = turnOn ? "on" : "off"; | ||
|
||
spdlog::debug("Turning power device {} {}", device.first, status); | ||
|
||
json params; | ||
params["device"] = device.first; | ||
params["action"] = status; | ||
ws.send_jsonrpc("machine.device_power.post_device", params, [this](json& j) { | ||
this->handle_device_callback(j); | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef __POWER_PANEL_H__ | ||
#define __POWER_PANEL_H__ | ||
|
||
#include "button_container.h" | ||
#include "lvgl/lvgl.h" | ||
#include "websocket_client.h" | ||
|
||
#include <string> | ||
#include <mutex> | ||
|
||
class PowerPanel { | ||
public: | ||
PowerPanel(KWebSocketClient &ws, std::mutex &l); | ||
~PowerPanel(); | ||
|
||
void create_devices(json &j); | ||
|
||
void foreground(); | ||
void handle_callback(lv_event_t *event); | ||
|
||
static void _handle_callback(lv_event_t *event) { | ||
PowerPanel *panel = (PowerPanel*)event->user_data; | ||
panel->handle_callback(event); | ||
}; | ||
|
||
private: | ||
void create_device(json &j); | ||
void handle_device_callback(json &j); | ||
|
||
KWebSocketClient &ws; | ||
std::mutex &lv_lock; | ||
|
||
lv_obj_t *cont; | ||
ButtonContainer back_btn; | ||
|
||
std::map<std::string, lv_obj_t*> devices; | ||
|
||
}; | ||
|
||
#endif //__POWER_PANEL_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ LV_IMG_DECLARE(chart_img); | |
|
||
#ifndef ZBOLT | ||
LV_IMG_DECLARE(belts_calibration_img); | ||
LV_IMG_DECLARE(power_devices_img); | ||
#else | ||
LV_IMG_DECLARE(print); | ||
#endif | ||
|
||
PrinterTunePanel::PrinterTunePanel(KWebSocketClient &c, std::mutex &l, lv_obj_t *parent, FineTunePanel &finetune) | ||
|
@@ -26,6 +29,7 @@ PrinterTunePanel::PrinterTunePanel(KWebSocketClient &c, std::mutex &l, lv_obj_t | |
, belts_calibration_panel(c, l) | ||
, tmc_tune_panel(c) | ||
, tmc_status_panel(c, l) | ||
, power_panel(c, l) | ||
, bedmesh_btn(cont, &bedmesh_img, "Bed Mesh", &PrinterTunePanel::_handle_callback, this) | ||
, finetune_btn(cont, &fine_tune_img, "Fine Tune", &PrinterTunePanel::_handle_callback, this) | ||
, inputshaper_btn(cont, &inputshaper_img, "Input Shaper", &PrinterTunePanel::_handle_callback, this) | ||
|
@@ -37,6 +41,11 @@ PrinterTunePanel::PrinterTunePanel(KWebSocketClient &c, std::mutex &l, lv_obj_t | |
, limits_btn(cont, &limit_img, "Limits", &PrinterTunePanel::_handle_callback, this) | ||
, tmc_tune_btn(cont, &motor_img, "TMC Autotune", &PrinterTunePanel::_handle_callback, this) | ||
, tmc_status_btn(cont, &chart_img, "TMC Metrics", &PrinterTunePanel::_handle_callback, this) | ||
#ifndef ZBOLT | ||
, power_devices_btn(cont, &power_devices_img, "Power Devices", &PrinterTunePanel::_handle_callback, this) | ||
#else | ||
, power_devices_btn(cont, &print, "Power Devices", &PrinterTunePanel::_handle_callback, this) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just start using material icons for zbolt too when there's no zbolt equivalent. |
||
#endif | ||
{ | ||
lv_obj_move_background(cont); | ||
|
||
|
@@ -61,6 +70,7 @@ PrinterTunePanel::PrinterTunePanel(KWebSocketClient &c, std::mutex &l, lv_obj_t | |
lv_obj_set_grid_cell(limits_btn.get_container(), LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_START, 2, 1); | ||
lv_obj_set_grid_cell(tmc_tune_btn.get_container(), LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_START, 2, 1); | ||
lv_obj_set_grid_cell(tmc_status_btn.get_container(), LV_GRID_ALIGN_CENTER, 2, 1, LV_GRID_ALIGN_START, 2, 1); | ||
lv_obj_set_grid_cell(power_devices_btn.get_container(), LV_GRID_ALIGN_CENTER, 3, 1, LV_GRID_ALIGN_START, 2, 1); | ||
// lv_obj_set_grid_cell(restart_firmware_btn.get_container(), LV_GRID_ALIGN_CENTER, 3, 1, LV_GRID_ALIGN_START, 2, 1); | ||
} | ||
|
||
|
@@ -79,6 +89,10 @@ BedMeshPanel& PrinterTunePanel::get_bedmesh_panel() { | |
return bedmesh_panel; | ||
} | ||
|
||
PowerPanel& PrinterTunePanel::get_power_panel() { | ||
return power_panel; | ||
} | ||
|
||
void PrinterTunePanel::init(json &j) { | ||
limits_panel.init(j); | ||
|
||
|
@@ -123,6 +137,9 @@ void PrinterTunePanel::handle_callback(lv_event_t *event) { | |
} else if (btn == tmc_status_btn.get_container()) { | ||
spdlog::trace("tmc metrics pressed"); | ||
tmc_status_panel.foreground(); | ||
} else if (btn == power_devices_btn.get_container()) { | ||
spdlog::trace("power devices pressed"); | ||
power_panel.foreground(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably need to clear the devices map here for when the printer re-initializes, so if any devices removed physically will also get removed from in the UI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Do you know how I can remove existing UI widgets in LVGL? Simply clearing the map still leaves the UI widget on screen and causes duplicate entries.