#include <stdbool.h>
#include <stdio.h>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "wendy_core.h"
#include "driver/i2c_master.h"

void app_main(void)
{
    ESP_ERROR_CHECK(wendy_core_init());
    const i2c_master_bus_config_t config = {
        .i2c_port = I2C_NUM_0,
        .sda_io_num = GPIO_NUM_6, .scl_io_num = GPIO_NUM_7,
        .clk_source = I2C_CLK_SRC_DEFAULT, .glitch_ignore_cnt = 7,
    };
    i2c_master_bus_handle_t bus;
    ESP_ERROR_CHECK(i2c_new_master_bus(&config, &bus));
    while (true) {
        unsigned found = 0;
        for (unsigned address = 8; address < 0x78; ++address) {
            esp_err_t result = i2c_master_probe(bus, address, 50);
            if (result == ESP_OK) {
                printf("ACK at 0x%02x\n", address);
                ++found;
            } else if (result != ESP_ERR_NOT_FOUND) {
                printf("Probe failed at 0x%02x: %s\n", address, esp_err_to_name(result));
            }
        }
        printf("Scan complete: %u responding address(es)\n", found);
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}
