Skip to content

Commit

Permalink
introduced virtual inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dakhnod committed Jan 14, 2025
1 parent c78fca6 commit a81a759
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/gpio/sensor_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ uint32_t gpio_output_digital_pin_count = 0;
uint32_t gpio_output_analog_pin_count = 0;
uint32_t gpio_input_digital_pin_count = 0;

uint32_t preconfigured_pin_count = 0;

uint16_t analog_output_values[2] = {0};

typedef enum {
Expand Down Expand Up @@ -279,6 +281,9 @@ void gpio_configure_aio_inputs_digital() {
continue;
}
current_index++;
if(config->pin.input.virtual){
continue;
}
gpio_config_input_digital_t *pin_config = &(config->pin.input);
uint32_t pin = pin_config->pin;
uint8_t pull = pin_config->pull;
Expand All @@ -304,7 +309,7 @@ void gpio_configure_aio_inputs_digital() {
}

void gpio_handle_parse_output_digital(uint32_t index, uint32_t pin, uint8_t default_state, uint8_t invert) {
gpio_config_t *config = gpio_configs + index;
gpio_config_t *config = gpio_configs + index + preconfigured_pin_count;
config->direction = OUTPUT;
config->pin.output.pin = pin;
config->pin.output.default_state = default_state;
Expand All @@ -323,39 +328,54 @@ void gpio_handle_parse_output_analog(uint32_t index, uint32_t pin, uint8_t inver
}

void gpio_handle_parse_input_digital(uint32_t index, uint32_t pin, uint8_t pull, uint8_t invert) {
gpio_config_t *config = gpio_configs + index;
gpio_config_t *config = gpio_configs + index + preconfigured_pin_count;
config->direction = INPUT;
config->pin.input.pin = pin;
config->pin.input.pull = pull;
config->pin.input.invert = invert;
config->pin.input.ignore_input = false;
}

#define VIRTUAL_INPUT_PIN_COUNT 1

void gpio_init(gpio_input_change_handler_t input_change_handler) {
ret_code_t err_code;
err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);

uint32_t current_index;
uint32_t current_index = 0;

// initialize virtual inputs first
gpio_input_digital_pin_count = VIRTUAL_INPUT_PIN_COUNT;

for(uint8_t i = 0; i < VIRTUAL_INPUT_PIN_COUNT; i++){
gpio_config_t *config = gpio_configs + i;
config->direction = INPUT;

config->pin.input.virtual = true;
}

// this is in a macro instead of a function
// to make it usable with the configuration macros
#define GPIO_CONFIGURATION_CHECK(pin_index) \
do{ \
current_index = gpio_input_digital_pin_count + gpio_output_digital_pin_count; \
gpio_config_t *gpio_config = gpio_configs + current_index; \
if(GPIO_CONFIGURATION_PIN_##pin_index##_MODE == GPIO_CONFIGURATION_PIN_MODE_INPUT){ \
gpio_input_digital_pin_count++; \
gpio_configs[current_index].direction = INPUT; \
gpio_configs[current_index].pin.input.pin = pin_index; \
gpio_configs[current_index].pin.input.invert = GPIO_CONFIGURATION_PIN_##pin_index##_INVERT; \
gpio_configs[current_index].pin.input.pull = GPIO_CONFIGURATION_PIN_##pin_index##_PULL; \
preconfigured_pin_count++; \
gpio_config->direction = INPUT; \
gpio_config->pin.input.pin = pin_index; \
gpio_config->pin.input.invert = GPIO_CONFIGURATION_PIN_##pin_index##_INVERT; \
gpio_config->pin.input.pull = GPIO_CONFIGURATION_PIN_##pin_index##_PULL; \
}else if(GPIO_CONFIGURATION_PIN_##pin_index##_MODE == GPIO_CONFIGURATION_PIN_MODE_OUTPUT){ \
gpio_output_digital_pin_count++; \
gpio_configs[current_index].direction = OUTPUT; \
gpio_configs[current_index].pin.output.pin = pin_index; \
gpio_configs[current_index].pin.output.invert = GPIO_CONFIGURATION_PIN_##pin_index##_INVERT; \
gpio_configs[current_index].pin.output.default_state = GPIO_CONFIGURATION_PIN_##pin_index##_DEFAULT_OUTPUT; \
gpio_configs[current_index].pin.output.state = 0xff; \
preconfigured_pin_count++; \
gpio_config->direction = OUTPUT; \
gpio_config->pin.output.pin = pin_index; \
gpio_config->pin.output.invert = GPIO_CONFIGURATION_PIN_##pin_index##_INVERT; \
gpio_config->pin.output.default_state = GPIO_CONFIGURATION_PIN_##pin_index##_DEFAULT_OUTPUT; \
gpio_config->pin.output.state = 0xff; \
} \
}while (false)

Expand Down
2 changes: 2 additions & 0 deletions src/gpio/sensor_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct
uint8_t invert;
uint8_t ignored_state;
uint8_t ignore_input;
uint8_t changed;
uint8_t virtual;
uint32_t trigger_count;
} gpio_config_input_digital_t;

Expand Down

0 comments on commit a81a759

Please sign in to comment.