Skip to content

Commit c63f0d9

Browse files
authored
Merge pull request #199 from synthetos/dev-198-ignore-uart
First pass at supporting devices that mute.
2 parents a70b3d8 + 1380b2f commit c63f0d9

10 files changed

+163
-53
lines changed

g2core/config.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ nvObj_t *nv_add_conditional_message(const char *string) // conditionally add
692692
* Inputs:
693693
* json_flags = JSON_OBJECT_FORMAT - print just the body w/o header or footer
694694
* json_flags = JSON_RESPONSE_FORMAT - print a full "r" object with footer
695+
* json_flags = JSON_RESPONSE_TO_MUTED_FORMAT - JSON_RESPONSE_FORMAT, but only to muted channels
695696
*
696697
* text_flags = TEXT_INLINE_PAIRS - print text as name/value pairs on a single line
697698
* text_flags = TEXT_INLINE_VALUES - print text as comma separated values on a single line

g2core/controller.cpp

+33-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static stat_t _sync_to_planner(void);
7272
static stat_t _sync_to_tx_buffer(void);
7373
static stat_t _dispatch_command(void);
7474
static stat_t _dispatch_control(void);
75-
static void _dispatch_kernel(void);
75+
static void _dispatch_kernel(const devflags_t flags);
7676
static stat_t _controller_state(void); // manage controller state transitions
7777

7878
static Motate::OutputPin<Motate::kOutputSAFE_PinNumber> safe_pin;
@@ -191,7 +191,7 @@ static stat_t _dispatch_control()
191191
if (cs.controller_state != CONTROLLER_PAUSED) {
192192
devflags_t flags = DEV_IS_CTRL;
193193
if ((cs.bufp = xio_readline(flags, cs.linelen)) != NULL) {
194-
_dispatch_kernel();
194+
_dispatch_kernel(flags);
195195
}
196196
}
197197
return (STAT_OK);
@@ -200,17 +200,27 @@ static stat_t _dispatch_control()
200200
static stat_t _dispatch_command()
201201
{
202202
if (cs.controller_state != CONTROLLER_PAUSED) {
203-
devflags_t flags = DEV_IS_BOTH;
203+
devflags_t flags = DEV_IS_BOTH | DEV_IS_MUTED; // expressly state we'll handle muted devices
204204
if ((!mp_planner_is_full()) && (cs.bufp = xio_readline(flags, cs.linelen)) != NULL) {
205-
_dispatch_kernel();
205+
_dispatch_kernel(flags);
206206
}
207207
}
208208
return (STAT_OK);
209209
}
210210

211-
static void _dispatch_kernel()
211+
static void _dispatch_kernel(const devflags_t flags)
212212
{
213213
stat_t status;
214+
215+
if (flags & DEV_IS_MUTED) {
216+
status = STAT_INPUT_FROM_MUTED_CHANNEL_ERROR;
217+
nv_reset_nv_list(); // get a fresh nvObj list
218+
nv_add_string((const char *)"msg", "lines from muted devices are ignored");
219+
nv_print_list(status, TEXT_NO_PRINT, JSON_RESPONSE_TO_MUTED_FORMAT);
220+
221+
// It's possible to let some stuff through, but that's not happening yet.
222+
return;
223+
}
214224

215225
while ((*cs.bufp == SPC) || (*cs.bufp == TAB)) { // position past any leading whitespace
216226
cs.bufp++;
@@ -299,6 +309,23 @@ void controller_set_connected(bool is_connected) {
299309
}
300310
}
301311

312+
/*
313+
* controller_set_muted(bool) - hook for xio to tell the controller that we
314+
* have/don't have one or more muted devices.
315+
*/
316+
317+
void controller_set_muted(bool is_muted) {
318+
// TODO: care about text mode
319+
if (is_muted) {
320+
// one channel just got muted.
321+
const bool only_to_muted = true;
322+
xio_writeline("{\"muted\":true}\n", only_to_muted);
323+
} else {
324+
// one channel just got unmuted, announce it (except to the muted)
325+
xio_writeline("{\"muted\":false}\n");
326+
}
327+
}
328+
302329
/*
303330
* controller_parse_control() - return true if command is a control (versus data)
304331
* Note: parsing for control is somewhat naiive. This will need to get better
@@ -456,4 +483,4 @@ stat_t _test_system_assertions()
456483
return (STAT_OK);
457484
}
458485

459-
486+

g2core/controller.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extern controller_t cs; // controller state structure
8484
void controller_init(void);
8585
void controller_run(void);
8686
void controller_set_connected(bool is_connected);
87+
void controller_set_muted(bool is_muted);
8788
bool controller_parse_control(char *p);
8889

8990
#endif // End of include guard: CONTROLLER_H_ONCE

g2core/error.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ char *get_status_message(stat_t status);
204204
#define STAT_MAX_DEPTH_EXCEEDED 115 // JSON exceeded maximum nesting depth
205205
#define STAT_VALUE_TYPE_ERROR 116 // JSON value does not agree with variable type
206206

207-
#define STAT_ERROR_117 117
207+
#define STAT_INPUT_FROM_MUTED_CHANNEL_ERROR 117 // input from a muted channel was ignored
208208
#define STAT_ERROR_118 118
209209
#define STAT_ERROR_119 119
210210

g2core/json_parser.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ void json_print_list(stat_t status, uint8_t flags)
499499
switch (flags) {
500500
case JSON_NO_PRINT: break;
501501
case JSON_OBJECT_FORMAT: { json_print_object(nv_body); break; }
502-
case JSON_RESPONSE_FORMAT: { json_print_response(status); break; }
502+
case JSON_RESPONSE_FORMAT:
503+
case JSON_RESPONSE_TO_MUTED_FORMAT:
504+
{ json_print_response(status, flags == JSON_RESPONSE_TO_MUTED_FORMAT); break; }
503505
}
504506
}
505507

@@ -522,7 +524,7 @@ void json_print_list(stat_t status, uint8_t flags)
522524
* on all the (non-silent) responses.
523525
*/
524526

525-
void json_print_response(uint8_t status)
527+
void json_print_response(uint8_t status, const bool only_to_muted /*= false*/)
526528
{
527529
if (js.json_verbosity == JV_SILENT) { // silent means no responses
528530
return;
@@ -599,7 +601,7 @@ void json_print_response(uint8_t status)
599601

600602
// serialize the JSON response and print it if there were no errors
601603
if (json_serialize(nv_header, cs.out_buf, sizeof(cs.out_buf)) >= 0) {
602-
xio_writeline(cs.out_buf);
604+
xio_writeline(cs.out_buf, only_to_muted);
603605
}
604606
}
605607

g2core/json_parser.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ typedef enum {
5757
typedef enum { // json output print modes
5858
JSON_NO_PRINT = 0, // don't print anything if you find yourself in JSON mode
5959
JSON_OBJECT_FORMAT, // print just the body as a json object
60-
JSON_RESPONSE_FORMAT // print the header/body/footer as a response object
60+
JSON_RESPONSE_FORMAT, // print the header/body/footer as a response object
61+
JSON_RESPONSE_TO_MUTED_FORMAT // print the header/body/footer as a response object, only to muted channels
6162
} jsonFormats;
6263

6364
typedef struct jsSingleton {
@@ -85,7 +86,7 @@ void json_parser(char *str);
8586
void json_parse_for_exec(char *str, bool execute);
8687
uint16_t json_serialize(nvObj_t *nv, char *out_buf, uint16_t size);
8788
void json_print_object(nvObj_t *nv);
88-
void json_print_response(uint8_t status);
89+
void json_print_response(uint8_t status, const bool only_to_muted = false);
8990
void json_print_list(stat_t status, uint8_t flags);
9091

9192
stat_t json_set_jv(nvObj_t *nv);

g2core/settings/settings_Printrbot_Simple_1608.h

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
#define COMM_MODE JSON_MODE // one of: TEXT_MODE, JSON_MODE
6161
#define XIO_ENABLE_FLOW_CONTROL FLOW_CONTROL_RTS // FLOW_CONTROL_OFF, FLOW_CONTROL_RTS
62+
#define XIO_UART_MUTES_WHEN_USB_CONNECTED 1 // Mute the UART when USB connects
6263

6364
#define TEXT_VERBOSITY TV_VERBOSE // one of: TV_SILENT, TV_VERBOSE
6465
#define JSON_VERBOSITY JV_LINENUM // one of: JV_SILENT, JV_FOOTER, JV_CONFIGS, JV_MESSAGES, JV_LINENUM, JV_VERBOSE

g2core/settings/settings_default.h

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@
141141
#define XIO_ENABLE_FLOW_CONTROL FLOW_CONTROL_RTS // FLOW_CONTROL_OFF, FLOW_CONTROL_XON, FLOW_CONTROL_RTS
142142
#endif
143143

144+
#ifndef XIO_UART_MUTES_WHEN_USB_CONNECTED
145+
#define XIO_UART_MUTES_WHEN_USB_CONNECTED 0 // UART will be muted when USB connected (off by default)
146+
#endif
147+
144148
#ifndef JSON_VERBOSITY
145149
#define JSON_VERBOSITY JV_MESSAGES // {jv: JV_SILENT, JV_FOOTER, JV_CONFIGS, JV_MESSAGES, JV_LINENUM, JV_VERBOSE
146150
#endif

0 commit comments

Comments
 (0)