Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dakhnod/NRF51-IO-module
Browse files Browse the repository at this point in the history
  • Loading branch information
dakhnod committed Jan 16, 2025
2 parents 654db0b + aa5bc27 commit 13cf738
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 52 deletions.
37 changes: 23 additions & 14 deletions src/ble/sensor_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,35 @@ void ble_init() {
nrf_delay_ms(3);
}

void ble_handle_input_change(uint32_t index, gpio_config_input_digital_t *config)
void ble_handle_input_change(int highest_changed_index)
{
#if FEATURE_ENABLED(AUTOMATION_IO)
ble_aio_handle_input_change(index, config);
#endif
// these services only process a single changed input
for(int input_index = 0; input_index <= highest_changed_index; input_index++) {
gpio_config_input_digital_t *config = gpio_find_input_by_index(input_index);
if(!config->changed){
continue;
}

#if FEATURE_ENABLED(GPIO_ASM)
ble_gpio_asm_handle_input_change(index, config);
#endif
#if FEATURE_ENABLED(CYCLING_SPEED_CADENCE)
ble_csc_handle_input_change(input_index, config);
#endif

#if FEATURE_ENABLED(CYCLING_SPEED_CADENCE)
ble_csc_handle_input_change(index, config);
#endif
#if FEATURE_ENABLED(HID)
ble_hid_handle_input_change(input_index, config);
#endif

#if FEATURE_ENABLED(HID)
ble_hid_handle_input_change(index, config);
#if FEATURE_ENABLED(BINARY_SENSOR)
ble_bss_handle_input_change(input_index, config);
#endif
}

// threse services should have access to all changed pins at once
#if FEATURE_ENABLED(AUTOMATION_IO)
ble_aio_handle_input_change(highest_changed_index);
#endif

#if FEATURE_ENABLED(BINARY_SENSOR)
ble_bss_handle_input_change(index, config);
#if FEATURE_ENABLED(GPIO_ASM)
ble_gpio_asm_handle_input_change();
#endif

#if FEATURE_ENABLED(CUSTOM_ADVERTISEMENT_DATA)
Expand Down
2 changes: 1 addition & 1 deletion src/ble/sensor_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ uint32_t dfu_init(void);
void services_init(void);
void advertising_stop(void);
void ble_init(void);
void ble_handle_input_change(uint32_t index, gpio_config_input_digital_t *config);
void ble_handle_input_change(int highest_changed_index);
void ble_disable_rf();


Expand Down
17 changes: 10 additions & 7 deletions src/ble/services/automation_io/ble_automation_io_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,28 +371,31 @@ void ble_aio_on_ble_evt(ble_evt_t *p_ble_evt)
}
}

void ble_aio_handle_input_change(uint32_t index, gpio_config_input_digital_t *config)
void ble_aio_handle_input_change(int highest_changed_index)
{
if (ble_aio_connection_handle == BLE_CONN_HANDLE_INVALID || (!ble_aio_send_digital_input_updates))
{
return;
}

// only need this many bytes since we only report the changed pin
uint32_t input_count = index + 1;
uint32_t input_count = highest_changed_index + 1;

uint16_t data_length = encoding_get_byte_count_from_pins(index + 1);
uint16_t data_length = encoding_get_byte_count_from_pins(input_count);

uint8_t data[data_length];
uint8_t input_states[input_count];

// set every state before out changed index to undefined
for(uint8_t i = 0; i < (input_count - 1); i++){
input_states[i] = 0b11;
for(uint8_t i = 0; i < input_count; i++){
gpio_config_input_digital_t *config = gpio_find_input_by_index(i);
if(!config->changed){
input_states[i] = 0b11;
continue;
}
input_states[i] = config->state;
}

input_states[index] = config->state;

encode_states_to_bytes(input_states, input_count, data, data_length);

ret_code_t err_code;
Expand Down
2 changes: 1 addition & 1 deletion src/ble/services/automation_io/ble_automation_io_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C" {
void ble_aio_on_authorize(ble_evt_t *p_ble_evt);
void encode_states_to_bytes(uint8_t *states, uint32_t state_count, uint8_t *buffer, uint16_t buffer_len);
uint32_t ble_aio_get_byte_count_from_pins(uint32_t pin_count);
void ble_aio_handle_input_change(uint32_t index, gpio_config_input_digital_t *config);
void ble_aio_handle_input_change(int highest_changed_index);
void ble_aio_handle_pin_digital_data(uint8_t *pin_data, uint32_t pin_data_length);
void ble_aio_handle_pin_analog_data(uint32_t index, uint16_t duty_cycle);

Expand Down
4 changes: 2 additions & 2 deletions src/ble/services/ble_gpio_asm/ble_gpio_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ uint8_t ble_gpio_asm_handle_data_write(uint8_t *data, uint32_t length)
return is_overflown;
}

void ble_gpio_asm_handle_input_change(uint32_t index, gpio_config_input_digital_t *config)
void ble_gpio_asm_handle_input_change()
{
gpioasm_handle_digital_input_update(&engine, index, config->state);
gpioasm_handle_digital_input_update(&engine);
}

void ble_gpio_asm_on_connect(ble_evt_t *p_ble_evt)
Expand Down
2 changes: 1 addition & 1 deletion src/ble/services/ble_gpio_asm/ble_gpio_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

void ble_gpio_asm_init();
void ble_gpio_asm_on_ble_evt(ble_evt_t *p_ble_evt);
void ble_gpio_asm_handle_input_change(uint32_t index, gpio_config_input_digital_t *config);
void ble_gpio_asm_handle_input_change();
4 changes: 4 additions & 0 deletions src/config/feature_config.template.h
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,10 @@

// </e>

#ifndef VIRTUAL_INPUT_PIN_COUNT
#define VIRTUAL_INPUT_PIN_COUNT 0
#endif

// </h>

// <<< end of configuration section >>>
Expand Down
4 changes: 4 additions & 0 deletions src/config/feature_config.template.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@

// </e>

#ifndef VIRTUAL_INPUT_PIN_COUNT
#define VIRTUAL_INPUT_PIN_COUNT 0
#endif

// </h>

// <<< end of configuration section >>>
Expand Down
60 changes: 41 additions & 19 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 @@ -56,7 +58,7 @@ gpio_config_output_digital_t *find_gpio_output_by_index(uint32_t index){
return &(config->pin.output);
}

gpio_config_input_digital_t *find_gpio_input_by_index(uint32_t index){
gpio_config_input_digital_t *gpio_find_input_by_index(uint32_t index){
gpio_config_t *config = find_gpio_config_by_index(index, INPUT);
if(config == NULL){
return NULL;
Expand Down Expand Up @@ -122,11 +124,11 @@ uint8_t gpio_get_output_digital_state(uint32_t index) {
}

bool gpio_get_input_digital_state(uint32_t index) {
gpio_config_input_digital_t *config = find_gpio_input_by_index(index);
gpio_config_input_digital_t *config = gpio_find_input_by_index(index);
if(config == NULL){
return false;
}
return find_gpio_input_by_index(index)->state;
return gpio_find_input_by_index(index)->state;
}

void gpio_encode_states(uint8_t *buffer, direction_t direction){
Expand Down Expand Up @@ -204,7 +206,7 @@ void gpio_configure_aio_outputs_analog(){
}

void on_pin_changed(uint32_t index) {
gpio_config_input_digital_t *config = find_gpio_input_by_index(index);
gpio_config_input_digital_t *config = gpio_find_input_by_index(index);
if(config == NULL){
return;
}
Expand All @@ -214,17 +216,21 @@ void on_pin_changed(uint32_t index) {
config->trigger_count++;
}

config->changed = true;

if (gpio_input_change_handler != NULL) {
gpio_input_change_handler(index, config);
gpio_input_change_handler(index);
}

config->changed = false;

config->ignore_input = true;

sensor_timer_debounce_timer_start(index);
}

void gpio_debounce_timeout_handler(uint32_t timer_index) {
gpio_config_input_digital_t *config = find_gpio_input_by_index(timer_index);
gpio_config_input_digital_t *config = gpio_find_input_by_index(timer_index);
if(config == NULL){
return;
}
Expand Down Expand Up @@ -279,6 +285,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 +313,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,7 +332,7 @@ 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;
Expand All @@ -336,26 +345,39 @@ void gpio_init(gpio_input_change_handler_t input_change_handler) {
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 = preconfigured_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 Expand Up @@ -439,7 +461,7 @@ void gpio_init(gpio_input_change_handler_t input_change_handler) {
}

for (int i = 0; i < gpio_input_digital_pin_count; i++) {
gpio_config_input_digital_t *config = find_gpio_input_by_index(i);
gpio_config_input_digital_t *config = gpio_find_input_by_index(i);
NRF_LOG_DEBUG("pin input: %d\n", config->pin);
NRF_LOG_DEBUG("pin pull: %d\n", config->pull);
NRF_LOG_DEBUG("pin invert: %d\n", config->invert);
Expand Down
5 changes: 4 additions & 1 deletion src/gpio/sensor_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ 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;

typedef void (*gpio_input_change_handler_t)(uint32_t pin_index, gpio_config_input_digital_t *config);
typedef void (*gpio_input_change_handler_t)(int highest_changed_index);

void gpio_init(gpio_input_change_handler_t input_change_handler);
void gpio_configure_aio_outputs();
Expand All @@ -45,5 +47,6 @@ uint8_t gpio_get_output_digital_state(uint32_t index);
bool gpio_get_input_digital_state(uint32_t index);
void gpio_encode_output_states(uint8_t *buffer);
void gpio_encode_input_states(uint8_t *buffer);
gpio_config_input_digital_t *gpio_find_input_by_index(uint32_t index);

#endif
2 changes: 1 addition & 1 deletion src/gpioasm
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include "feature_config.h"
#include "watchdog.h"

void main_handle_input_change(uint32_t index, gpio_config_input_digital_t *config)
void main_handle_input_change(int highest_changed_index)
{
#if FEATURE_ENABLED(SLEEP_MODE)
sleep_handle_gpio_event(index, config);
sleep_handle_gpio_event();
#endif
ble_handle_input_change(index, config);
ble_handle_input_change(highest_changed_index);
}

int main(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/sleep/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void sleep_timeout_handler(void *context){
#endif
}

void sleep_handle_gpio_event(uint32_t index, gpio_config_input_digital_t *input){
void sleep_handle_gpio_event(){
inactivity_count = 0;
sleep_allow_advertise = true;
// ensure that the watchdog is active
Expand Down
2 changes: 1 addition & 1 deletion src/sleep/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ typedef void (*sleep_enter_handler_t)();

void sleep_init(sleep_enter_handler_t);
bool sleep_get_allow_advertise();
void sleep_handle_gpio_event(uint32_t index, gpio_config_input_digital_t *input);
void sleep_handle_gpio_event();

0 comments on commit 13cf738

Please sign in to comment.