hw_test/main/hw_test.c
2026-04-26 11:43:59 +02:00

196 lines
4.9 KiB
C

#include "bosch456.h"
#include "driver/gpio.h"
#include "driver/i2c_master.h"
#include "driver/i2c_types.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "led_strip.h"
#include "uart.h"
#include <stdint.h>
#include <sys/types.h>
#define LED_GPIO 21
#define TASTER_GPIO 12
#define V_LIPO_1 1
#define V_LIPO_2 12
#define DIP_MASTER 4
#define I2C_SCL 5
#define I2C_SDA 6
#define LED_RING 7
#define RING_LEDS 95
#define I2C_PORT 0
#define IO_EXPANDER_ADDRESS 0x20
static const char *TAG = "LED_TASK";
static led_strip_handle_t led_strip;
static led_strip_handle_t led_ring;
static i2c_master_bus_handle_t bus_handle;
static i2c_master_dev_handle_t io_expander;
/* LED Task */
void vTaskLed(void *pvParameters) {
ESP_LOGI(TAG, "LED task started");
uint8_t state = 0;
while (1) {
if (state) {
// GREEN
led_strip_set_pixel(led_strip, 0, 25, 0, 0);
} else {
led_strip_set_pixel(led_strip, 0, 0, 0, 0);
}
led_strip_refresh(led_strip);
state = !state;
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void vTaskInput(void *pvParameters) {
ESP_LOGI(TAG, "Input Test");
uint8_t master;
uint8_t taster;
uint8_t lastMaster = 0;
uint8_t lastTaster = 0;
gpio_reset_pin(DIP_MASTER);
gpio_set_direction(DIP_MASTER, GPIO_MODE_INPUT);
gpio_reset_pin(TASTER_GPIO);
gpio_set_direction(TASTER_GPIO, GPIO_MODE_INPUT);
while (1) {
master = gpio_get_level(DIP_MASTER);
taster = gpio_get_level(TASTER_GPIO);
if (master != lastMaster) {
ESP_LOGI(TAG, "Master Pin changed from %d to %d", lastMaster, master);
lastMaster = master;
}
if (taster != lastTaster) {
ESP_LOGI(TAG, "Taster Pin changed from %d to %d", lastTaster, taster);
lastTaster = taster;
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void vTaskRingBlink(void *pvParameters) {
ESP_LOGI("GPIO_TASK", "GPIO ring blink task started");
static bool state;
uint8_t c1 = 255, c2 = 255, c3 = 255;
while (1) {
if (state) {
for (int i = 0; i < RING_LEDS; i++) {
led_strip_set_pixel(led_ring, i, c1, c2, c3);
led_strip_refresh(led_ring);
vTaskDelay(pdMS_TO_TICKS(10));
}
} else {
for (int i = 0; i < RING_LEDS; i++) {
led_strip_set_pixel(led_ring, i, 0, 0, 0);
led_strip_refresh(led_ring);
vTaskDelay(pdMS_TO_TICKS(10));
}
}
state = !state;
uint8_t temp = c1;
c1 = c3;
c3 = c2;
c2 = temp;
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void app_main(void) {
ESP_LOGI(TAG, "Init LED strip");
esp_err_t err;
/* On Board LED Config */
led_strip_config_t strip_config = {
.strip_gpio_num = LED_GPIO,
//.strip_gpio_num = LED_RING,
.max_leds = 1,
};
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10 MHz
};
err = led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Onboard LED Task started");
xTaskCreate(vTaskLed, "led_task", 2048, NULL, 5, NULL);
}
/* LED Ring config */
led_strip_config_t ring_config = {
.strip_gpio_num = LED_RING,
.max_leds = RING_LEDS,
};
led_strip_rmt_config_t rmt_ring_config = {
.resolution_hz = 10 * 1000 * 1000, // 10 MHz
};
err = led_strip_new_rmt_device(&ring_config, &rmt_ring_config, &led_ring);
if (err == ESP_OK) {
ESP_LOGI(TAG, "GPIO_RING_TASK started");
xTaskCreate(vTaskRingBlink, "gpio_ring_task", 4096, NULL, 5, NULL);
}
/* Input Monitor Task */
ESP_LOGI(TAG, "Input Monitor Task");
xTaskCreate(vTaskInput, "monitor_input", 4096, NULL, 5, NULL);
/* Init I2C IO Expander */
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT,
.scl_io_num = I2C_SCL,
.sda_io_num = I2C_SDA,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
err = i2c_new_master_bus(&i2c_mst_config, &bus_handle);
if (err != ESP_OK) {
ESP_LOGI(TAG, "I2C Master Could not Init Bus");
}
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = IO_EXPANDER_ADDRESS,
.scl_speed_hz = 100000,
};
err = i2c_master_bus_add_device(bus_handle, &dev_cfg, &io_expander);
if (err != ESP_OK) {
ESP_LOGI(TAG, "Could not add IO Expnader to Bus %d", err);
} else {
uint8_t write_buf[2] = {0x03, 0xFF}; // 0xFF setzt alle Bits auf Input
err = i2c_master_transmit(io_expander, write_buf, sizeof(write_buf), -1);
if (err != ESP_OK) {
ESP_LOGI(TAG, "Could not set IO Expander in input mode %d", err);
}
uint8_t reg_addr = 0x00;
uint8_t read_val = 0;
err = i2c_master_transmit_receive(io_expander, &reg_addr, 1, &read_val, 1,
-1);
if (err != ESP_OK) {
ESP_LOGI(TAG, "Could not read IO Expander input %d", err);
}
ESP_LOGI(TAG, "Read INPUT %d", read_val);
}
init_bma456(bus_handle);
init_uart();
}