Skip to content

Commit 4e9f6dd

Browse files
committed
halui -code clenup, GUI based MDI command feature
1 parent 663a4a8 commit 4e9f6dd

1 file changed

Lines changed: 133 additions & 44 deletions

File tree

src/emc/usr_intf/halui.cc

Lines changed: 133 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static int axis_mask = 0;
203203
FIELD(hal_bit_t,home_all) /* pin for homing all joints in sequence */ \
204204
FIELD(hal_bit_t,abort) /* pin for aborting */ \
205205
ARRAY(hal_bit_t,mdi_commands,MDI_MAX) \
206+
ARRAY(hal_bit_t,gui_mdi_commands,MDI_MAX) \
206207
\
207208
FIELD(hal_float_t,units_per_mm) \
208209

@@ -246,6 +247,9 @@ static int lastaxis = -1;
246247

247248
static char *mdi_commands[MDI_MAX];
248249
static int num_mdi_commands=0;
250+
251+
static char *gui_mdi_commands[MDI_MAX];
252+
static int num_gui_mdi_commands = 0;
249253
static int have_home_all = 0;
250254

251255
static int comp_id, done; /* component ID, main while loop */
@@ -551,6 +555,65 @@ int halui_export_pin_OUT_bit(hal_bit_t **pin, const char *name)
551555
return 0;
552556
}
553557

558+
static int py_call_get_mdi_count() {
559+
int value = 0;
560+
// check socket messages for jogspeed
561+
pFuncRead = PyObject_GetAttrString(pInstance, "getMdiCount");
562+
if (pFuncRead && PyCallable_Check(pFuncRead)) {
563+
pValue = PyObject_CallNoArgs(pFuncRead);
564+
if (pValue == NULL){
565+
if (PyErr_Occurred()) PyErr_Print();
566+
fprintf(stderr, "Halui Bridge: getMdiCountfunction failed: returned NULL\n");
567+
value = -1;
568+
}else{
569+
if (PyLong_Check(pValue)) {
570+
value = (int) PyLong_AsLong(pValue);
571+
//fprintf(stderr, "axis value %d\n",value);
572+
if (PyErr_Occurred()) {
573+
value = -1;
574+
// Handle conversion error
575+
PyErr_Print();
576+
// Clear the error state if needed
577+
PyErr_Clear();
578+
}
579+
}
580+
}
581+
Py_DECREF(pValue);
582+
}
583+
Py_DECREF(pFuncRead);
584+
return value;
585+
}
586+
587+
// turns a undex number into a macro name
588+
static char* py_call_get_mdi_name( int num) {
589+
pFuncWrite = PyObject_GetAttrString(pInstance, "getMdiName");
590+
591+
if (pFuncWrite && PyCallable_Check(pFuncWrite)) {
592+
pValue = PyObject_CallFunction(pFuncWrite, "i", num);
593+
594+
if (pValue != NULL) {
595+
if (PyUnicode_Check(pValue)) {
596+
PyObject *pBytes = PyUnicode_AsUTF8String(pValue);
597+
char *result_string = PyBytes_AsString(pBytes);
598+
Py_XDECREF(pBytes);
599+
printf("Python function returned: %s %i\n", result_string,num);
600+
Py_DECREF(pValue);
601+
Py_DECREF(pFuncWrite);
602+
return result_string;
603+
} else {
604+
fprintf(stderr, "Return value is not a string. %i\n", num);
605+
}
606+
} else {
607+
PyErr_Print(); // Print Python error if call failed
608+
}
609+
Py_DECREF(pValue);
610+
}else{
611+
if (PyErr_Occurred()) PyErr_Print();
612+
fprintf(stderr, "halui Bridge: Failed python function");
613+
}
614+
Py_DECREF(pFuncWrite);
615+
return NULL;
616+
}
554617

555618
/********************************************************************
556619
*
@@ -941,6 +1004,12 @@ int halui_hal_init(void)
9411004
if (retval < 0) return retval;
9421005
}
9431006

1007+
for (int n=0; n<num_gui_mdi_commands; n++) {
1008+
printf("MDI name returned: %s %i\n", py_call_get_mdi_name(n),n);
1009+
retval = hal_pin_bit_newf(HAL_IN, &(halui_data->gui_mdi_commands[n]), comp_id, "halui.mdi-command-%s", py_call_get_mdi_name(n));
1010+
if (retval < 0) return retval;
1011+
}
1012+
9441013
hal_ready(comp_id);
9451014
return 0;
9461015
}
@@ -1575,6 +1644,13 @@ static int iniLoad(const char *filename)
15751644
mdi_commands[num_mdi_commands++] = strdup(mc->c_str());
15761645
}
15771646

1647+
int temp = py_call_get_mdi_count();
1648+
for (int n=0; n<temp; n++) {
1649+
printf("MDI process %i\n", n);
1650+
gui_mdi_commands[num_gui_mdi_commands++] = py_call_get_mdi_name(n);
1651+
}
1652+
1653+
15781654
// close it
15791655
inifile.Close();
15801656

@@ -1689,7 +1765,7 @@ static bool jogging_selected_axis(local_halui_str &hal) {
16891765
return (hal.ajog_plus[EMCMOT_MAX_AXIS] || hal.ajog_minus[EMCMOT_MAX_AXIS]);
16901766
}
16911767

1692-
static double write_msg_axis_get_jogspeed() {
1768+
static double py_call_axis_get_jogspeed() {
16931769
double jspd = 0;
16941770
// check socket messages for jogspeed
16951771
pFuncRead = PyObject_GetAttrString(pInstance, "getJogRate");
@@ -1716,7 +1792,7 @@ static double write_msg_axis_get_jogspeed() {
17161792
return jspd;
17171793
}
17181794

1719-
static void write_msg_axis_jogspeed(double speed)
1795+
static void py_call_axis_jogspeed(double speed)
17201796
{
17211797
pFuncWrite = PyObject_GetAttrString(pInstance, "setJogRate");
17221798

@@ -1736,7 +1812,7 @@ static void write_msg_axis_jogspeed(double speed)
17361812
Py_DECREF(pFuncWrite);
17371813
}
17381814

1739-
static int write_msg_get_axis_selected() {
1815+
static int py_call_get_axis_selected() {
17401816
int value = 0;
17411817
// check socket messages for jogspeed
17421818
pFuncRead = PyObject_GetAttrString(pInstance, "getSelectedAxis");
@@ -1765,7 +1841,7 @@ static int write_msg_get_axis_selected() {
17651841
return value;
17661842
}
17671843

1768-
static void write_msg_axis_changed( int axis)
1844+
static void py_call_axis_changed( int axis)
17691845
{
17701846
pFuncWrite = PyObject_GetAttrString(pInstance, "setSelectedAxis");
17711847

@@ -1784,6 +1860,27 @@ static void write_msg_axis_changed( int axis)
17841860
}
17851861
Py_DECREF(pFuncWrite);
17861862
}
1863+
1864+
static void py_call_request_MDI( int index)
1865+
{
1866+
pFuncWrite = PyObject_GetAttrString(pInstance, "runIndexedMacro");
1867+
1868+
if (pFuncWrite && PyCallable_Check(pFuncWrite)) {
1869+
pValue = PyObject_CallFunction(pFuncWrite, "i", index);
1870+
if (pValue == NULL){
1871+
fprintf(stderr, "halui bridge: runIndexedMacro function failed: returned NULL\n");
1872+
if (PyErr_Occurred()) PyErr_Print();
1873+
}else{
1874+
Py_DECREF(pValue);
1875+
}
1876+
1877+
}else{
1878+
if (PyErr_Occurred()) PyErr_Print();
1879+
fprintf(stderr, "halui Bridge: Failed python function runIndexedMacro");
1880+
}
1881+
Py_DECREF(pFuncWrite);
1882+
}
1883+
17871884
// this function looks if any of the hal pins has changed
17881885
// and sends appropriate messages if so
17891886
static void check_hal_changes()
@@ -1816,7 +1913,7 @@ static void check_hal_changes()
18161913
}
18171914

18181915
// check socket messages for current axis selection
1819-
int value = write_msg_get_axis_selected();
1916+
int value = py_call_get_axis_selected();
18201917

18211918
local_halui_str new_halui_data_mutable;
18221919

@@ -2054,13 +2151,13 @@ static void check_hal_changes()
20542151
old_halui_data.ajog_speed = new_halui_data.ajog_speed;
20552152
internaljogspeed = new_halui_data.ajog_speed;
20562153
ajog_speed_changed = 1;
2057-
write_msg_axis_jogspeed(internaljogspeed);
2154+
py_call_axis_jogspeed(internaljogspeed);
20582155
} else {
20592156
ajog_speed_changed = 0;
20602157
}
20612158

20622159
// check socket messages for jogspeed
2063-
jogspeed = write_msg_axis_get_jogspeed();
2160+
jogspeed = py_call_axis_get_jogspeed();
20642161
if (fabs(jogspeed - lastjogspeed) > 0.00001) {
20652162
ajog_speed_changed = 1;
20662163
lastjogspeed = jogspeed;
@@ -2208,7 +2305,7 @@ static void check_hal_changes()
22082305
if (bit != 0) {
22092306
is_any_axis_selected = 1;
22102307
*halui_data->axis_selected = axis_num;
2211-
write_msg_axis_changed(axis_num);
2308+
py_call_axis_changed(axis_num);
22122309
aselect_changed = axis_num; // flag that we changed the selected axis
22132310
}else{
22142311
deselected = 1;
@@ -2218,7 +2315,7 @@ static void check_hal_changes()
22182315
}
22192316
// last axis has been deselected - no axis is selected now
22202317
if (is_any_axis_selected == 0 and deselected == 1) {
2221-
write_msg_axis_changed(-1);
2318+
py_call_axis_changed(-1);
22222319
*halui_data->axis_selected = -1;
22232320
}
22242321

@@ -2321,10 +2418,19 @@ static void check_hal_changes()
23212418
old_halui_data.ajog_increment_minus[EMCMOT_MAX_AXIS] = bit;
23222419
}
23232420

2421+
// run HALUI commands
23242422
for(int n = 0; n < num_mdi_commands; n++) {
23252423
if (check_bit_changed(new_halui_data.mdi_commands[n], old_halui_data.mdi_commands[n]) != 0)
23262424
sendMdiCommand(n);
23272425
}
2426+
2427+
// request GUI ti run MDI commands
2428+
for(int n = 0; n < num_gui_mdi_commands; n++) {
2429+
if (check_bit_changed(new_halui_data.gui_mdi_commands[n], old_halui_data.gui_mdi_commands[n]) != 0){
2430+
printf("GUI MDI command called index: %i\n", n);
2431+
py_call_request_MDI(n);
2432+
}
2433+
}
23282434
}
23292435

23302436
// this function looks at the received NML status message
@@ -2547,22 +2653,6 @@ int main(int argc, char *argv[])
25472653
exit(1);
25482654
}
25492655

2550-
// get configuration information
2551-
if (0 != iniLoad(emc_inifile)) {
2552-
rcs_print_error("iniLoad error\n");
2553-
exit(2);
2554-
}
2555-
2556-
//init HAL and export pins
2557-
if (0 != halui_hal_init()) {
2558-
rcs_print_error("hal_init error\n");
2559-
exit(1);
2560-
}
2561-
2562-
//initialize safe values
2563-
hal_init_pins();
2564-
2565-
25662656
/* import the python module and get references for needed function */
25672657

25682658
PyConfig config;
@@ -2572,35 +2662,34 @@ int main(int argc, char *argv[])
25722662
PyConfig_SetString(&config, &config.program_name, wname);
25732663
Py_Initialize();
25742664

2575-
PyRun_SimpleString("print('PYTHON EMBEDDED!!')\n"
2576-
);
2665+
PyRun_SimpleString("print('PYTHON EMBEDDED!!')\n");
25772666
pModule = PyImport_ImportModule("bridgeui.bridge");
25782667
if (pModule != NULL) {
25792668
pClass = PyObject_GetAttrString(pModule, "Bridge");
25802669
pInstance = PyObject_CallObject(pClass, NULL);
2581-
pFuncWrite = PyObject_GetAttrString(pInstance, "update");
2582-
2583-
if (pFuncWrite && PyCallable_Check(pFuncWrite)) {
2584-
pValue = PyObject_CallFunction(pFuncWrite, "Olllh", pClass, 0, 0, 0, 0);
2585-
if (pValue == NULL){
2586-
fprintf(stderr, "Panelui: update function failed: returned NULL\n");
2587-
}
2588-
if (PyErr_Occurred()) PyErr_Print();
2589-
}else{
2590-
if (PyErr_Occurred()){
2591-
PyErr_Print();
2592-
}
2593-
fprintf(stderr, "Bridge: Failed python function");
2594-
exit(1);
2595-
Py_DECREF(pValue);
2596-
}
2597-
Py_DECREF(pFuncWrite);
25982670
}else{
25992671
PyErr_Print();
26002672
fprintf(stderr, "bridge: Failed to load \"%s\"\n", "pyui");
26012673
exit(1);
26022674
}
26032675

2676+
// get configuration information
2677+
if (0 != iniLoad(emc_inifile)) {
2678+
rcs_print_error("iniLoad error\n");
2679+
exit(2);
2680+
}
2681+
2682+
//init HAL and export pins
2683+
if (0 != halui_hal_init()) {
2684+
rcs_print_error("hal_init error\n");
2685+
exit(1);
2686+
}
2687+
2688+
//initialize safe values
2689+
hal_init_pins();
2690+
2691+
2692+
26042693

26052694
// init NML
26062695
if (0 != tryNml()) {

0 commit comments

Comments
 (0)