Skip to content

Commit

Permalink
Merge pull request #600 from UncleRus/fix/mcp23017-example
Browse files Browse the repository at this point in the history
fix: fix example for mcp23017
  • Loading branch information
UncleRus committed Feb 3, 2024
2 parents bae3559 + b58cb56 commit d798bd4
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions examples/mcp23x17/mcp23017/main/main.c
@@ -1,19 +1,40 @@
#include <stdio.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <mcp23x17.h>
#include <driver/gpio.h>
#include <string.h>

static const char *TAG = "mcp23017_example";

static EventGroupHandle_t eg = NULL;
static mcp23x17_t dev = { 0 };

#define BIT_BUTTON_CHANGED BIT(0)

static void IRAM_ATTR intr_handler(void *arg)
{
printf("Interrupt!\n");
// On interrupt set bit in event group
BaseType_t hp_task;
if (xEventGroupSetBitsFromISR(eg, BIT_BUTTON_CHANGED, &hp_task) != pdFAIL)
portYIELD_FROM_ISR(hp_task);
}

void button_handler(void *pvParameters)
{
while (1)
{
// wait for BIT_BUTTON_CHANGED, clear it on exit
if (xEventGroupWaitBits(eg, BIT_BUTTON_CHANGED, pdTRUE, pdTRUE, portMAX_DELAY) != BIT_BUTTON_CHANGED)
continue;
// OK, we got this bit set
ESP_LOGI(TAG, "Button was pressed!");
}
}

void test(void *pvParameters)
{
mcp23x17_t dev;
memset(&dev, 0, sizeof(mcp23x17_t));
eg = xEventGroupCreate();

ESP_ERROR_CHECK(mcp23x17_init_desc(&dev, CONFIG_EXAMPLE_I2C_ADDR, 0, CONFIG_EXAMPLE_I2C_MASTER_SDA, CONFIG_EXAMPLE_I2C_MASTER_SCL));

Expand All @@ -22,6 +43,10 @@ void test(void *pvParameters)
// Setup interrupt on it
mcp23x17_set_interrupt(&dev, 0, MCP23X17_INT_ANY_EDGE);

// Run button handler
xTaskCreate(button_handler, "button_handler", 4096, NULL, 5, NULL);

// Setup GPIO interrupt
gpio_set_direction(CONFIG_EXAMPLE_INTA_GPIO, GPIO_MODE_INPUT);
gpio_set_intr_type(CONFIG_EXAMPLE_INTA_GPIO, GPIO_INTR_ANYEDGE);
gpio_install_isr_service(0);
Expand All @@ -44,4 +69,3 @@ void app_main()
ESP_ERROR_CHECK(i2cdev_init());
xTaskCreate(test, "test", configMINIMAL_STACK_SIZE * 6, NULL, 5, NULL);
}

0 comments on commit d798bd4

Please sign in to comment.