Skip to content

Commit 8ae055b

Browse files
committed
Added mainmenu with several handy functions
- introduced mainmenu with several handy functions (abort printjob, preheat, cooldown, fans on/off, homing, motors off) - changed actionbar in main view for more intuitive handling
1 parent 8ead7cf commit 8ae055b

File tree

12 files changed

+265
-46
lines changed

12 files changed

+265
-46
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
"projectType": "native",
2525
"resources": {
2626
"media": [
27+
{
28+
"file": "images/icon_refresh.png",
29+
"name": "REFRESH_ICON",
30+
"targetPlatforms": null,
31+
"type": "bitmap"
32+
},
2733
{
2834
"file": "data/pdc_question.pdc",
2935
"name": "QUESTION_PDC",
@@ -79,12 +85,6 @@
7985
"targetPlatforms": null,
8086
"type": "bitmap"
8187
},
82-
{
83-
"file": "images/icon_cancel.png",
84-
"name": "CANCEL_ICON",
85-
"targetPlatforms": null,
86-
"type": "bitmap"
87-
},
8888
{
8989
"file": "images/icon_bed.png",
9090
"name": "BED_ICON",

resources/images/icon_cancel.png

-1.02 KB
Binary file not shown.

resources/images/icon_refresh.png

188 Bytes
Loading

src/c/messaging.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void messaging_inbox_dropped(AppMessageResult reason, void *context) {
8787

8888

8989
// send an outgoing message
90-
void messaging_outbox_send(const char *value) {
90+
void messaging_outbox_send(const char *command, const char *value) {
9191
DictionaryIterator *iter;
9292
app_message_outbox_begin(&iter);
9393

@@ -97,7 +97,8 @@ void messaging_outbox_send(const char *value) {
9797
return;
9898
}
9999

100-
dict_write_cstring(iter, 0, value);
100+
dict_write_cstring(iter, 0, command);
101+
dict_write_cstring(iter, 1, value);
101102
dict_write_end(iter);
102103

103104
app_message_outbox_send();

src/c/messaging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
//static void messaging_inbox_received(DictionaryIterator *, void *);
44
//static void messaging_inbox_dropped(AppMessageResult, void *);
5-
void messaging_outbox_send(const char *);
5+
void messaging_outbox_send(const char *, const char *);
66
void messaging_init(void);
77
void messaging_destroy(void);

src/c/window_main.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <pebble.h>
88
#include "window_main.h"
9-
#include "window_confirm.h"
9+
#include "window_menu.h"
1010
#include "printer.h"
1111
#include "messaging.h"
1212
#include "progress_layer.h"
@@ -19,9 +19,9 @@ static ActionBarLayer *s_actionbar;
1919

2020
static GBitmap *image_connect;
2121
static GBitmap *image_refresh;
22+
static GBitmap *image_menu;
2223
static GBitmap *image_pause;
2324
static GBitmap *image_start;
24-
static GBitmap *image_cancel;
2525
static GBitmap *image_nozzle;
2626
static GBitmap *image_bed;
2727

@@ -39,28 +39,21 @@ static BitmapLayer *s_bitmaplayer_nozzle;
3939
static BitmapLayer *s_bitmaplayer_bed;
4040

4141

42-
// user canceled printjob
43-
static void window_main_cancel_printjob(void) {
44-
messaging_outbox_send("cancel");
45-
}
46-
47-
4842
// handle click on up button
4943
static void window_main_actionbar_up(ClickRecognizerRef recognizer, void *context) {
50-
messaging_outbox_send("pause");
44+
messaging_outbox_send("pause", "");
5145
}
5246

5347

5448
// handle click on select button
5549
static void window_main_actionbar_select(ClickRecognizerRef recognizer, void *context) {
56-
messaging_outbox_send("update");
50+
window_menu_init();
5751
}
5852

5953

6054
// handle click on down button
6155
static void window_main_actionbar_down(ClickRecognizerRef recognizer, void *context) {
62-
//messaging_outbox_send("cancel");
63-
window_confirm_init_custom("Abort printjob?", PBL_IF_COLOR_ELSE(GColorRed, GColorWhite), window_main_cancel_printjob);
56+
messaging_outbox_send("update", "");
6457
}
6558

6659

@@ -115,10 +108,10 @@ static void window_main_load_handler(Window *window) {
115108

116109
// load images
117110
image_connect = gbitmap_create_with_resource(RESOURCE_ID_CONNECT_ICON);
118-
image_refresh = gbitmap_create_with_resource(RESOURCE_ID_ELLIPSIS_ICON);
111+
image_refresh = gbitmap_create_with_resource(RESOURCE_ID_REFRESH_ICON);
112+
image_menu = gbitmap_create_with_resource(RESOURCE_ID_ELLIPSIS_ICON);
119113
image_start = gbitmap_create_with_resource(RESOURCE_ID_PLAY_ICON);
120114
image_pause = gbitmap_create_with_resource(RESOURCE_ID_PAUSE_ICON);
121-
image_cancel = gbitmap_create_with_resource(RESOURCE_ID_CANCEL_ICON);
122115
image_nozzle = gbitmap_create_with_resource(RESOURCE_ID_NOZZLE_ICON);
123116
image_bed = gbitmap_create_with_resource(RESOURCE_ID_BED_ICON);
124117

@@ -127,8 +120,8 @@ static void window_main_load_handler(Window *window) {
127120
action_bar_layer_add_to_window(s_actionbar, window);
128121
action_bar_layer_set_click_config_provider(s_actionbar, window_main_actionbar_config);
129122
action_bar_layer_set_icon(s_actionbar, BUTTON_ID_UP, image_connect);
130-
action_bar_layer_set_icon(s_actionbar, BUTTON_ID_SELECT, image_refresh);
131-
action_bar_layer_set_icon(s_actionbar, BUTTON_ID_DOWN, image_cancel);
123+
action_bar_layer_set_icon(s_actionbar, BUTTON_ID_SELECT, image_menu);
124+
action_bar_layer_set_icon(s_actionbar, BUTTON_ID_DOWN, image_refresh);
132125

133126
// background layer
134127
s_bg_layer = layer_create(GRect(2, 2, (bounds.size.w - ACTION_BAR_WIDTH - 4), bounds.size.h - 4));
@@ -217,9 +210,9 @@ static void window_main_unload_handler(Window *window) {
217210
// destroy images
218211
gbitmap_destroy(image_connect);
219212
gbitmap_destroy(image_refresh);
213+
gbitmap_destroy(image_menu);
220214
gbitmap_destroy(image_pause);
221215
gbitmap_destroy(image_start);
222-
gbitmap_destroy(image_cancel);
223216
gbitmap_destroy(image_nozzle);
224217
gbitmap_destroy(image_bed);
225218

@@ -301,7 +294,7 @@ void window_main_set_state(const char *value) {
301294
// update data
302295
void window_main_timer_fired(struct tm *tick_time, TimeUnits units_changed) {
303296
APP_LOG(APP_LOG_LEVEL_DEBUG, "timer fired, reloading");
304-
messaging_outbox_send("update");
297+
messaging_outbox_send("update", "");
305298
}
306299

307300

src/c/window_menu.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// OctoWatch2
2+
// A Pebble watch app for monitoring and basic controlling of 3D printers via Octoprint
3+
//
4+
// Licence: CC BY-SA 3.0, http://creativecommons.org/licenses/by-sa/3.0/
5+
// Author: Dominik Scholz <[email protected]>, go4u.de Webdesign <[email protected]>
6+
7+
#include <pebble.h>
8+
#include "window_menu.h"
9+
#include "window_confirm.h"
10+
#include "messaging.h"
11+
12+
#define MENU_SECTIONS 3
13+
#define MENU_FIRST_ITEMS 1
14+
#define MENU_SECOND_ITEMS 4
15+
#define MENU_THIRD_ITEMS 2
16+
17+
static Window *s_window_main;
18+
19+
static SimpleMenuLayer *s_layer_simplemenu;
20+
static SimpleMenuSection s_menu_sections[MENU_SECTIONS];
21+
static SimpleMenuItem s_menu_items_first[MENU_FIRST_ITEMS];
22+
static SimpleMenuItem s_menu_items_second[MENU_SECOND_ITEMS];
23+
static SimpleMenuItem s_menu_items_third[MENU_THIRD_ITEMS];
24+
25+
26+
// user canceled printjob
27+
static void window_menu_cancel_printjob(void) {
28+
messaging_outbox_send("cancel", "");
29+
}
30+
31+
32+
// confirm cancel printjob
33+
static void window_menu_confirm_cancel_printjob(int s, void *ctx) {
34+
window_confirm_init_custom("Abort printjob?", PBL_IF_COLOR_ELSE(GColorRed, GColorWhite), window_menu_cancel_printjob);
35+
}
36+
37+
38+
// preheat bed and nozzle
39+
static void window_menu_preheat(int s, void *ctx) {
40+
messaging_outbox_send("preheat", "");
41+
}
42+
43+
44+
// cooldown bed and nozzle
45+
static void window_menu_cooldown(int s, void *ctx) {
46+
messaging_outbox_send("gcode", "M117 Cooldown...\nM140 S0\nM104 S0");
47+
}
48+
49+
50+
// turn fans on
51+
static void window_menu_fans_on(int s, void *ctx) {
52+
messaging_outbox_send("gcode", "M117 Fans on...\nM106 S255");
53+
}
54+
55+
56+
// turn fans off
57+
static void window_menu_fans_off(int s, void *ctx) {
58+
messaging_outbox_send("gcode", "M117 Fans off...\nM106 S0");
59+
}
60+
61+
62+
// home all axes
63+
static void window_menu_home_axes(int s, void *ctx) {
64+
messaging_outbox_send("gcode", "M117 Homing...\nG28");
65+
}
66+
67+
68+
// turn all motors off
69+
static void window_menu_motors_off(int s, void *ctx) {
70+
messaging_outbox_send("gcode", "M117 Motors off...\nM18");
71+
}
72+
73+
74+
// load window handler
75+
static void window_menu_load_handler(Window *window) {
76+
// create window layer
77+
Layer *window_layer = window_get_root_layer(window);
78+
GRect bounds = layer_get_bounds(window_layer);
79+
80+
// create first menu section
81+
s_menu_items_first[0] = (SimpleMenuItem) {
82+
.title = "Abort printjob",
83+
.subtitle = "Stop running printjob",
84+
.callback = window_menu_confirm_cancel_printjob
85+
};
86+
s_menu_sections[0] = (SimpleMenuSection) {
87+
.title = "Printjob",
88+
.num_items = MENU_FIRST_ITEMS,
89+
.items = s_menu_items_first
90+
};
91+
92+
// create second menu section
93+
s_menu_items_second[0] = (SimpleMenuItem) {
94+
.title = "Preheat",
95+
.subtitle = "Preheat nozzle and bed",
96+
.callback = window_menu_preheat
97+
};
98+
s_menu_items_second[1] = (SimpleMenuItem) {
99+
.title = "Cooldown",
100+
.subtitle = "Cooldown nozzle and bed",
101+
.callback = window_menu_cooldown
102+
};
103+
s_menu_items_second[2] = (SimpleMenuItem) {
104+
.title = "Fans on",
105+
.subtitle = "Turn the fans on",
106+
.callback = window_menu_fans_on
107+
};
108+
s_menu_items_second[3] = (SimpleMenuItem) {
109+
.title = "Fans off",
110+
.subtitle = "Turn the fans off",
111+
.callback = window_menu_fans_off
112+
};
113+
s_menu_sections[1] = (SimpleMenuSection) {
114+
.title = "Temperature",
115+
.num_items = MENU_SECOND_ITEMS,
116+
.items = s_menu_items_second
117+
};
118+
119+
// create third menu section
120+
s_menu_items_third[0] = (SimpleMenuItem) {
121+
.title = "Home all",
122+
.subtitle = "Home all axes (XYZ)",
123+
.callback = window_menu_home_axes
124+
};
125+
s_menu_items_third[1] = (SimpleMenuItem) {
126+
.title = "Motors off",
127+
.subtitle = "Turn the motors off",
128+
.callback = window_menu_motors_off
129+
};
130+
s_menu_sections[2] = (SimpleMenuSection) {
131+
.title = "Motion",
132+
.num_items = MENU_THIRD_ITEMS,
133+
.items = s_menu_items_third
134+
};
135+
136+
// create simplemenulayer
137+
s_layer_simplemenu = simple_menu_layer_create(bounds, window, s_menu_sections, MENU_SECTIONS, NULL);
138+
menu_layer_set_highlight_colors(simple_menu_layer_get_menu_layer(s_layer_simplemenu), PBL_IF_COLOR_ELSE(GColorVividCerulean, GColorBlack), PBL_IF_COLOR_ELSE(GColorBlack, GColorWhite));
139+
layer_add_child(window_layer, simple_menu_layer_get_layer(s_layer_simplemenu));
140+
}
141+
142+
143+
// unload window handler
144+
static void window_menu_unload_handler(Window *window) {
145+
// destroy simple menu layer
146+
simple_menu_layer_destroy(s_layer_simplemenu);
147+
148+
// destroy window
149+
window_destroy(s_window_main);
150+
}
151+
152+
153+
// create new window
154+
void window_menu_init(void) {
155+
s_window_main = window_create();
156+
window_set_background_color(s_window_main, PBL_IF_COLOR_ELSE(GColorWhite, GColorWhite));
157+
window_set_window_handlers(s_window_main, (WindowHandlers) {
158+
.load = window_menu_load_handler,
159+
.unload = window_menu_unload_handler
160+
});
161+
window_stack_push(s_window_main, true);
162+
}
163+
164+
165+
// destroy window
166+
void window_menu_destroy(void) {
167+
window_stack_pop(true);
168+
}

src/c/window_menu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
void window_menu_init(void);
4+
void window_menu_destroy(void);

src/pkjs/ajax.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
var Ajax = {
2-
3-
usessl: localStorage.getItem('octoprintusessl') > 0,
4-
host: localStorage.getItem('octoprinthost'),
5-
port: localStorage.getItem('octoprintport'),
6-
apikey: localStorage.getItem('octoprintapikey'),
7-
usebasicauth: localStorage.getItem('octoprintusebasicauth'),
8-
auth: {
9-
user: localStorage.getItem('octoprintuser'),
10-
password: localStorage.getItem('octoprintpassword')
11-
},
12-
13-
2+
143
// send ajax call
154
send: function(method, path, params, onSuccess, onError) {
165

17-
var url = 'http' + (Ajax.usessl ? 's' : '') + '://' + Ajax.host + ':' + Ajax.port + path;
6+
var usessl = localStorage.getItem('octoprintusessl') > 0,
7+
host = localStorage.getItem('octoprinthost'),
8+
port = localStorage.getItem('octoprintport'),
9+
apikey = localStorage.getItem('octoprintapikey'),
10+
usebasicauth = localStorage.getItem('octoprintusebasicauth'),
11+
user = localStorage.getItem('octoprintuser'),
12+
password = localStorage.getItem('octoprintpassword'),
13+
url = 'http' + (usessl ? 's' : '') + '://' + host + ':' + port + path;
14+
1815
console.log("calling " + url);
1916

2017
var req = new XMLHttpRequest(),
@@ -23,7 +20,7 @@ var Ajax = {
2320

2421
// add apikey to params, if get
2522
if (!isMethodPost)
26-
params.apikey = Ajax.apikey;
23+
params.apikey = apikey;
2724

2825
// merge params
2926
for (var key in params)
@@ -34,13 +31,13 @@ var Ajax = {
3431
req.open(method.toUpperCase(), mergedUrl, true);
3532

3633
// if credentials given, use http basic auth
37-
if (Ajax.usebasicauth)
38-
req.setRequestHeader('Authorization', 'Basic ' + Base64.encode(Ajax.auth.user + ':' + Ajax.auth.password));
34+
if (usebasicauth)
35+
req.setRequestHeader('Authorization', 'Basic ' + Base64.encode(user + ':' + password));
3936

4037
// set proper header for post
4138
if (isMethodPost) {
4239
req.setRequestHeader('Content-Type', 'application/json');
43-
req.setRequestHeader('X-API-KEY', Ajax.apikey);
40+
req.setRequestHeader('X-API-KEY', apikey);
4441
}
4542

4643
// register callback handlers

0 commit comments

Comments
 (0)