From 8cce9c35f37be9202480add526022126fcf02f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Thu, 2 May 2024 21:16:41 +0200 Subject: [~] Moved the screen related code in a component --- main/main.c | 230 ++---------------------------------------------------------- 1 file changed, 5 insertions(+), 225 deletions(-) (limited to 'main/main.c') diff --git a/main/main.c b/main/main.c index a7f2019..4e73a00 100644 --- a/main/main.c +++ b/main/main.c @@ -1,231 +1,11 @@ -#include -#include -#include "driver/gpio.h" -#include "driver/i2c_master.h" +#include "screen.h" -#define MCP_I2C_ADDR CONFIG_MCP_I2C_ADDR -#define SDA_PIN CONFIG_SDA_PIN -#define SCL_PIN CONFIG_SCL_PIN -#define N0_P0 CONFIG_N0_P0 -#define N0_P1 CONFIG_N0_P1 -#define N0_P2 CONFIG_N0_P2 -#define N0_P3 CONFIG_N0_P3 -#define N1_P0 CONFIG_N1_P0 -#define N1_P1 CONFIG_N1_P1 -#define N1_P2 CONFIG_N1_P2 -#define N1_P3 CONFIG_N1_P3 -#define N2_P0 CONFIG_N2_P0 -#define N2_P1 CONFIG_N2_P1 -#define N2_P2 CONFIG_N2_P2 -#define N2_P3 CONFIG_N2_P3 -#define N3_P0 CONFIG_N3_P0 -#define N3_P1 CONFIG_N3_P1 -#define N3_P2 CONFIG_N3_P2 -#define N3_P3 CONFIG_N3_P3 -#define MCP_GPIO_OUTPUT 0 -#define REG_IODIRA 0x00 -#define REG_IODIRB 0x01 -#define REG_GPIOA 0x12 -#define REG_GPIOB 0x13 -#define BV(x) (1 << (x)) +struct Screen screen; -struct Nixie -{ - uint8_t digit; - uint8_t reg; - uint8_t D; - uint8_t C; - uint8_t B; - uint8_t A; -}; - -struct Nixie screen[4]; -i2c_master_bus_handle_t i2c_bus; -i2c_master_dev_handle_t mcp; - - -esp_err_t write_reg_bit_16(uint8_t reg, bool val, uint8_t bit) -{ - uint8_t buf[3]; - - // Read the current value from the register - buf[0] = reg; // register address - ESP_ERROR_CHECK(i2c_master_transmit(mcp, &buf[0], sizeof(uint8_t), -1)); - ESP_ERROR_CHECK(i2c_master_receive(mcp, &buf[1], sizeof(uint8_t), -1)); - // Update the value in the buffer - - buf[1] = (buf[1] & ~BV(bit)) | (val ? BV(bit) : 0); - - // Write the updated value back to the register - buf[0] = reg; // Send register address - ESP_ERROR_CHECK(i2c_master_transmit(mcp, &buf[0], sizeof(uint16_t), -1)); - - return ESP_OK; -} - -esp_err_t mcp_set_mode(uint8_t reg, uint8_t pin, bool mode) -{ - return write_reg_bit_16(reg, mode, pin); -} - -esp_err_t mcp_set_level(uint8_t reg, uint8_t pin, bool val) -{ - return write_reg_bit_16(reg, val, pin); - -} - -void configure_screen(void) -{ - i2c_master_bus_config_t i2c_bus_config = { - .clk_source = I2C_CLK_SRC_DEFAULT, - .i2c_port = 0, - .sda_io_num = SDA_PIN, - .scl_io_num = SCL_PIN, - .glitch_ignore_cnt = 7, - .flags.enable_internal_pullup = true, - }; - ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus)); - - i2c_device_config_t mcp_cfg = { - .dev_addr_length = I2C_ADDR_BIT_LEN_7, - .device_address = MCP_I2C_ADDR, - .scl_speed_hz = 1000000, - }; - ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus, &mcp_cfg, &mcp)); - - printf("I2C MCP Configured!\n"); - - screen[0].digit = 0; - screen[0].reg = REG_GPIOA; - screen[0].C = N0_P0; - screen[0].B = N0_P1; - screen[0].D = N0_P2; - screen[0].A = N0_P3; - screen[1].digit = 0; - screen[1].reg = REG_GPIOA; - screen[1].C = N1_P0; - screen[1].B = N1_P1; - screen[1].D = N1_P2; - screen[1].A = N1_P3; - screen[2].digit = 0; - screen[2].reg = REG_GPIOB; - screen[2].C = N2_P0; - screen[2].B = N2_P1; - screen[2].D = N2_P2; - screen[2].A = N2_P3; - screen[3].digit = 0; - screen[3].reg = REG_GPIOB; - screen[3].C = N3_P0; - screen[3].B = N3_P1; - screen[3].D = N3_P2; - screen[3].A = N3_P3; - - for (uint8_t i = 0; i <= 3; ++i) - { - uint8_t reg = REG_IODIRA; - if(i > 1) reg = REG_IODIRB; - mcp_set_mode(reg, screen[i].D, MCP_GPIO_OUTPUT); - mcp_set_mode(reg, screen[i].C, MCP_GPIO_OUTPUT); - mcp_set_mode(reg, screen[i].B, MCP_GPIO_OUTPUT); - mcp_set_mode(reg, screen[i].A, MCP_GPIO_OUTPUT); - } - - - printf("MCP GPIO Configured!\n"); - return; -} - -void set_digit(struct Nixie* n) -{ - switch (n->digit) { - case 0: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 0); - break; - case 1: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 1); - break; - case 2: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 1); - mcp_set_level(n->reg, n->A, 0); - break; - case 3: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 1); - mcp_set_level(n->reg, n->A, 1); - break; - case 4: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 1); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 0); - break; - case 5: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 1); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 1); - break; - case 6: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 1); - mcp_set_level(n->reg, n->B, 1); - mcp_set_level(n->reg, n->A, 0); - break; - case 7: - mcp_set_level(n->reg, n->D, 0); - mcp_set_level(n->reg, n->C, 1); - mcp_set_level(n->reg, n->B, 1); - mcp_set_level(n->reg, n->A, 1); - break; - case 8: - mcp_set_level(n->reg, n->D, 1); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 0); - break; - case 9: - mcp_set_level(n->reg, n->D, 1); - mcp_set_level(n->reg, n->C, 0); - mcp_set_level(n->reg, n->B, 0); - mcp_set_level(n->reg, n->A, 1); - break; - default: - mcp_set_level(n->reg, n->D, 1); - mcp_set_level(n->reg, n->C, 1); - mcp_set_level(n->reg, n->B, 1); - mcp_set_level(n->reg, n->A, 1); - break; - return; - } -} - -void test_screen(void) +void app_main(void) { printf("Starting up...\n"); - configure_screen(); + configure_screen(&screen); printf("Screen Configured!\n"); - - while(1){ - for (uint8_t i = 0; i <= 9; ++i) { - for (int j = 0; j < 4; ++j) { - screen[j].digit = (i + j) % 10; // Update the digit cyclically - set_digit(&screen[j]); // Call set_digit function - } - sleep(1); - } - } -} - -void app_main(void) -{ - test_screen(); + display(&screen, "2110"); } \ No newline at end of file -- cgit v1.2.3