blk_box_tc/main/main.cpp
2024-08-02 17:18:06 -05:00

147 lines
3.9 KiB
C++
Executable File

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/i2c.h"
#include "drivers/tft.hpp"
#include "drivers/wires.hpp"
#include "drivers/bottom_half.hpp"
#include "drivers/sd.hpp"
#include "drivers/game_timer.hpp"
#include "drivers/speaker.hpp"
#include "drivers/char_lcd.hpp"
#include "esp_rom_gpio.h"
#include "steps/step0.hpp"
#define WIRES_ADDR 125
#define WIRES_REG_WIRES
static const char *TAG = "main";
static void relay_task(void *arg);
extern "C" void app_main(void) {
printf("app_main\n");
init_sd();
init_tft();
init_speaker();
init_sseg();
init_game_timer();
init_wires();
set_game_time(30000);
init_bottom_half();
init_char_lcd();
init_wires();
step0();
step1();
// create_demo_ui();
// play_example();
// deinit_sd();
// set_game_timer(1234, 1);
// set_module_timer(5678, 2);
// while (1) {
// vTaskDelay(pdMS_TO_TICKS(10));
// lv_timer_handler();
// }
gpio_reset_pin(GPIO_NUM_41);
gpio_reset_pin(GPIO_NUM_42);
i2c_config_t wires_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_41,
.scl_io_num = GPIO_NUM_42,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 100000,
}
};
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_1, &wires_conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_1, wires_conf.mode, 0, 0, 0));
// uint8_t write[8] = {1, 2, 4, 8, 16, 32, 64, 128};
return;
while (1) {
vTaskDelay(pdMS_TO_TICKS(5000));
uint8_t read[8] = {0};
uint8_t reg;
// reg = 1;
// ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(I2C_NUM_0, 126, &reg, 1, read, 1, (100 / portTICK_PERIOD_MS)));
// ESP_LOGI("main", "delta: %d", read[0]);
// read[0] = 0;
// reg = 2;
// ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(I2C_NUM_0, 126, &reg, 1, read, 2, (100 / portTICK_PERIOD_MS)));
// ESP_LOGI("main", "keypad: %d %d", read[0], read[1]);
// WIRES
reg = 1;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(I2C_NUM_1, 125, &reg, 1, read, 1, (1000 / portTICK_PERIOD_MS)));
ESP_LOGI("main", "wires: %d", read[0]);
// Issue strike
// vTaskDelay(pdMS_TO_TICKS(10));
vTaskDelay(pdMS_TO_TICKS(20000));
// vTaskDelay(pdMS_TO_TICKS(5000));
}
}
#define BUF_SIZE (1024)
#define TX_PIN GPIO_NUM_
static void relay_task(void *arg)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
int intr_alloc_flags = 0;
#if CONFIG_UART_ISR_IN_IRAM
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_0, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_NC, GPIO_NUM_NC));
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
// Read data from the UART
int len = uart_read_bytes(UART_NUM_0, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
// Write data back to the UART
// uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
if (len) {
data[len] = '\0';
// ESP_LOGI(TAG, "%s", (char *) data);
printf("%s", (char *) data);
}
}
}