diff --git a/g2core/gpio.cpp b/g2core/gpio.cpp index fdd857c5..ff816e68 100644 --- a/g2core/gpio.cpp +++ b/g2core/gpio.cpp @@ -472,6 +472,42 @@ bool gpio_read_input(const uint8_t input_num_ext) return (d_in[input_num_ext-1].state); } +/* + * gpio_set_output() - Set output pins + */ +stat_t gpio_set_output(uint8_t output_num, float value) { + ioMode outMode = d_out[output_num].mode; + if (outMode == IO_MODE_DISABLED) { + value = 0; // Inactive? + } else { + bool invert = (outMode == 0); + if (invert) { + value = 1.0 - value; + } + switch (output_num+1) { + // Generated with: + // perl -e 'for($i=1;$i<14;$i++) { print "case ${i}: { output_${i}_pin = value; } break;\n";}' + // BEGIN generated + case 1: { output_1_pin = value; } break; + case 2: { output_2_pin = value; } break; + case 3: { output_3_pin = value; } break; + case 4: { output_4_pin = value; } break; + case 5: { output_5_pin = value; } break; + case 6: { output_6_pin = value; } break; + case 7: { output_7_pin = value; } break; + case 8: { output_8_pin = value; } break; + case 9: { output_9_pin = value; } break; + case 10: { output_10_pin = value; } break; + case 11: { output_11_pin = value; } break; + case 12: { output_12_pin = value; } break; + case 13: { output_13_pin = value; } break; + // END generated + default: { value = 0; } // inactive + } + } + return STAT_OK; +} + /*********************************************************************************** * CONFIGURATION AND INTERFACE FUNCTIONS @@ -616,43 +652,17 @@ stat_t io_get_output(nvObj_t *nv) } /* - * io_set_output() - read and return output state given an nv object + * io_set_output() - set output state given an nv object */ stat_t io_set_output(nvObj_t *nv) { uint8_t output_num = _io(nv->index); + return gpio_set_output(output_num, nv->value_flt); - ioMode outMode = d_out[output_num-1].mode; - if (outMode == IO_MODE_DISABLED) { - nv->valuetype = TYPE_NULL; // reports back as NULL - - } else { - // send normal or inverted value to output - float value = (outMode == IO_ACTIVE_HIGH) ? nv->value_flt : 1.0 - nv->value_flt; - switch (output_num+1) { // add 1 to get logical pin numbers - // Generated with: - // perl -e 'for($i=1;$i<14;$i++) { print "case ${i}: { output_${i}_pin = value; } break;\n";}' - // BEGIN generated - case 1: { output_1_pin = value; } break; - case 2: { output_2_pin = value; } break; - case 3: { output_3_pin = value; } break; - case 4: { output_4_pin = value; } break; - case 5: { output_5_pin = value; } break; - case 6: { output_6_pin = value; } break; - case 7: { output_7_pin = value; } break; - case 8: { output_8_pin = value; } break; - case 9: { output_9_pin = value; } break; - case 10: { output_10_pin = value; } break; - case 11: { output_11_pin = value; } break; - case 12: { output_12_pin = value; } break; - case 13: { output_13_pin = value; } break; // END generated - default: { nv->value_flt = 0; } // inactive - } - } - return (STAT_OK); } + /*********************************************************************************** * TEXT MODE SUPPORT * Functions to print variables from the cfgArray table diff --git a/g2core/gpio.h b/g2core/gpio.h index 1c437d39..80801eb8 100644 --- a/g2core/gpio.h +++ b/g2core/gpio.h @@ -146,10 +146,11 @@ void gpio_reset(void); void input_reset(void); void output_reset(void); -bool gpio_read_input(const uint8_t input_num); void gpio_set_homing_mode(const uint8_t input_num, const bool is_homing); void gpio_set_probing_mode(const uint8_t input_num, const bool is_probing); int8_t gpio_get_probing_input(void); +bool gpio_read_input(const uint8_t input_num); +stat_t gpio_set_output(uint8_t output_num, float value); stat_t io_get_mo(nvObj_t *nv); stat_t io_set_mo(nvObj_t *nv);