split cpp and h files

This commit is contained in:
Mitchell Marino 2024-08-07 19:06:51 -05:00
parent ed9554171b
commit 75ddfb4e78
19 changed files with 190 additions and 149 deletions

View File

@ -8,6 +8,7 @@ set(SOURCES
"sd.cpp"
"speaker.cpp"
"sseg.cpp"
"tft.cpp"
"wires.cpp"
)

View File

@ -65,8 +65,6 @@ bool get_touch_state(void);
bool get_touch_pressed(void);
bool get_touch_released(void);
static void poll_bottom_task(void *arg);
void init_bottom_half();
#endif /* BOTTOM_HALF_HPP */

View File

@ -1,68 +1,6 @@
#ifndef TFT_HPP
#define TFT_HPP
#include "tft.h"
/*
* Adapted from an example under the MIT license:
* Copyright © 2022 atanisoft (github.com/atanisoft)
*
* MIT LICENSE:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <driver/gpio.h>
#include <driver/ledc.h>
#include <driver/spi_master.h>
#include <esp_err.h>
#include <esp_freertos_hooks.h>
#include <esp_log.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_vendor.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_ili9488.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lvgl.h>
#include <stdio.h>
#include "sdkconfig.h"
// Uncomment the following line to enable using double buffering of LVGL color
// data.
// #define USE_DOUBLE_BUFFERING 1
static const char *TFT_TAG = "tft_driver";
// rotation swaps the horizontal and vertical pixel counts
#define DISPLAY_HORIZONTAL_PIXELS 480
#define DISPLAY_VERTICAL_PIXELS 320
#define DISPLAY_COMMAND_BITS 8
#define DISPLAY_PARAMETER_BITS 8
#define DISPLAY_REFRESH_HZ 40000000
#define DISPLAY_SPI_QUEUE_LEN 10
#define SPI_MAX_TRANSFER_SIZE 32768
#define TFT_PIN_MOSI GPIO_NUM_17
#define TFT_PIN_MISO GPIO_NUM_18
#define TFT_PIN_CLK GPIO_NUM_16
#define TFT_PIN_CS GPIO_NUM_NC
#define TFT_PIN_DC GPIO_NUM_15
#define TFT_PIN_RESET GPIO_NUM_8
#define TFT_INVERT_COLOR false
// Default to 50 lines of color data
#define LV_BUFFER_SIZE DISPLAY_HORIZONTAL_PIXELS * 50
#define LVGL_UPDATE_PERIOD_MS 5
#define BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
#define BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
#define BACKLIGHT_LEDC_TIMER LEDC_TIMER_1
#define BACKLIGHT_LEDC_TIMER_RESOLUTION LEDC_TIMER_10_BIT
#define BACKLIGHT_LEDC_FRQUENCY 5000
static const char* TAG = "tft";
static esp_lcd_panel_io_handle_t lcd_io_handle = NULL;
static esp_lcd_panel_handle_t lcd_handle = NULL;
@ -104,8 +42,8 @@ static void IRAM_ATTR lvgl_tick_cb(void *param) {
lv_tick_inc(LVGL_UPDATE_PERIOD_MS);
}
void initialize_spi() {
ESP_LOGI(TFT_TAG, "Initializing SPI bus (MOSI:%d, MISO:%d, CLK:%d)",
static void initialize_spi() {
ESP_LOGI(TAG, "Initializing SPI bus (MOSI:%d, MISO:%d, CLK:%d)",
TFT_PIN_MOSI, TFT_PIN_MISO, TFT_PIN_CLK);
spi_bus_config_t bus = {
@ -128,7 +66,7 @@ void initialize_spi() {
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus, SPI_DMA_CH_AUTO));
}
void initialize_display() {
static void initialize_display() {
const esp_lcd_panel_io_spi_config_t io_config = {
.cs_gpio_num = TFT_PIN_CS,
.dc_gpio_num = TFT_PIN_DC,
@ -181,19 +119,19 @@ void initialize_display() {
#endif
}
void initialize_lvgl() {
ESP_LOGI(TFT_TAG, "Initializing LVGL");
static void initialize_lvgl() {
ESP_LOGI(TAG, "Initializing LVGL");
lv_init();
ESP_LOGI(TFT_TAG, "Allocating %zu bytes for LVGL buffer", LV_BUFFER_SIZE * sizeof(lv_color_t));
ESP_LOGI(TAG, "Allocating %zu bytes for LVGL buffer", LV_BUFFER_SIZE * sizeof(lv_color_t));
lv_buf_1 = (lv_color_t *)heap_caps_malloc(LV_BUFFER_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA);
#if USE_DOUBLE_BUFFERING
ESP_LOGI(TFT_TAG, "Allocating %zu bytes for second LVGL buffer", LV_BUFFER_SIZE * sizeof(lv_color_t));
ESP_LOGI(TAG, "Allocating %zu bytes for second LVGL buffer", LV_BUFFER_SIZE * sizeof(lv_color_t));
lv_buf_2 = (lv_color_t *)heap_caps_malloc(LV_BUFFER_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA);
#endif
ESP_LOGI(TFT_TAG, "Creating LVLG display buffer");
ESP_LOGI(TAG, "Creating LVLG display buffer");
lv_disp_draw_buf_init(&lv_disp_buf, lv_buf_1, lv_buf_2, LV_BUFFER_SIZE);
ESP_LOGI(TFT_TAG, "Initializing %dx%d display", DISPLAY_HORIZONTAL_PIXELS, DISPLAY_VERTICAL_PIXELS);
ESP_LOGI(TAG, "Initializing %dx%d display", DISPLAY_HORIZONTAL_PIXELS, DISPLAY_VERTICAL_PIXELS);
lv_disp_drv_init(&lv_disp_drv);
lv_disp_drv.hor_res = DISPLAY_HORIZONTAL_PIXELS;
lv_disp_drv.ver_res = DISPLAY_VERTICAL_PIXELS;
@ -203,7 +141,7 @@ void initialize_lvgl() {
// lv_disp_drv.rotated = LV_DISP_ROT_90;
lv_display = lv_disp_drv_register(&lv_disp_drv);
ESP_LOGI(TFT_TAG, "Creating LVGL tick timer");
ESP_LOGI(TAG, "Creating LVGL tick timer");
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &lvgl_tick_cb,
.arg = NULL,
@ -273,7 +211,7 @@ void create_demo_ui() {
lv_anim_start(&a);
}
void tick_timer_task(void* arg) {
static void tick_timer_task(void* arg) {
while (1)
{
vTaskDelay(pdMS_TO_TICKS(10));
@ -288,7 +226,5 @@ void init_tft() {
xTaskCreate(tick_timer_task, "tick_lvgl", 4096, NULL, 5, NULL);
ESP_LOGI(TFT_TAG, "TFT initialization Successful");
ESP_LOGI(TAG, "TFT initialization Successful");
}
#endif /* TFT_HPP */

68
main/drivers/tft.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef TFT_H
#define TFT_H
/*
* Adapted from an example under the MIT license:
* Copyright © 2022 atanisoft (github.com/atanisoft)
*
* MIT LICENSE:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <driver/gpio.h>
#include <driver/ledc.h>
#include <driver/spi_master.h>
#include <esp_err.h>
#include <esp_freertos_hooks.h>
#include <esp_log.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_vendor.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_ili9488.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lvgl.h>
#include <stdio.h>
#include "sdkconfig.h"
// Uncomment the following line to enable using double buffering of LVGL color
// data.
// #define USE_DOUBLE_BUFFERING 1
// rotation swaps the horizontal and vertical pixel counts
#define DISPLAY_HORIZONTAL_PIXELS 480
#define DISPLAY_VERTICAL_PIXELS 320
#define DISPLAY_COMMAND_BITS 8
#define DISPLAY_PARAMETER_BITS 8
#define DISPLAY_REFRESH_HZ 40000000
#define DISPLAY_SPI_QUEUE_LEN 10
#define SPI_MAX_TRANSFER_SIZE 32768
#define TFT_PIN_MOSI GPIO_NUM_17
#define TFT_PIN_MISO GPIO_NUM_18
#define TFT_PIN_CLK GPIO_NUM_16
#define TFT_PIN_CS GPIO_NUM_NC
#define TFT_PIN_DC GPIO_NUM_15
#define TFT_PIN_RESET GPIO_NUM_8
#define TFT_INVERT_COLOR false
// Default to 50 lines of color data
#define LV_BUFFER_SIZE DISPLAY_HORIZONTAL_PIXELS * 50
#define LVGL_UPDATE_PERIOD_MS 5
#define BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
#define BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
#define BACKLIGHT_LEDC_TIMER LEDC_TIMER_1
#define BACKLIGHT_LEDC_TIMER_RESOLUTION LEDC_TIMER_10_BIT
#define BACKLIGHT_LEDC_FRQUENCY 5000
void create_demo_ui();
void init_tft();
#endif /* TFT_H */

View File

@ -4,7 +4,7 @@
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/i2c.h"
#include "drivers/tft.hpp"
#include "drivers/tft.h"
#include "drivers/wires.h"
#include "drivers/bottom_half.h"
#include "drivers/sd.h"
@ -16,13 +16,13 @@
#include "helper.h"
#include "steps/step0.hpp"
#include "steps/step1.hpp"
#include "steps/step2.hpp"
#include "steps/step0.h"
#include "steps/step1.h"
#include "steps/step2.h"
#include "steps/step3.hpp"
#include "steps/step4.hpp"
#include "steps/step5.hpp"
#include "steps/step6.hpp"
#include "steps/step6.h"
static const char *TAG = "main";

View File

@ -1,4 +1,10 @@
set(SOURCES
"setup_wires.cpp"
"step0.cpp"
"step1.cpp"
"step2.cpp"
"step4.cpp"
"step6.cpp"
"wires_puzzle.cpp"
)

View File

@ -1,12 +1,6 @@
#ifndef SETUP_WIRES_HPP
#define SETUP_WIRES_HPP
#include "setup_wires.h"
#include "../drivers/bottom_half.h"
#include "../drivers/char_lcd.h"
#include "wires_puzzle.h"
#include <esp_log.h>
uint8_t wires_state = 0;
static uint8_t wires_state = 0;
void print_wires(WireColor* wires, int editing_idx) {
bool cut[NUM_WIRES];
@ -89,5 +83,3 @@ void setup_wires(void) {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
#endif /* SETUP_WIRES_HPP */

12
main/steps/setup_wires.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SETUP_WIRES_H
#define SETUP_WIRES_H
#include "../drivers/bottom_half.h"
#include "../drivers/char_lcd.h"
#include "../drivers/wires.h"
#include "wires_puzzle.h"
#include <esp_log.h>
void setup_wires(void);
#endif /* SETUP_WIRES_H */

View File

@ -1,14 +1,6 @@
#ifndef STEP_0_HPP
#define STEP_0_HPP
#include "step0.h"
#include "../drivers/bottom_half.h"
#include "../drivers/char_lcd.h"
#include "../drivers/wires.h"
#include "setup_wires.hpp"
static const char *STEP0_TAG = "step0";
#define STRING_MAX_LEN 8
static const char* TAG = "step0";
/// Wait for "*9819"
void step0(void) {
@ -71,5 +63,3 @@ void step0(void) {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
#endif /* STEP_0_HPP */

14
main/steps/step0.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef STEP_0_H
#define STEP_0_H
#include "../drivers/bottom_half.h"
#include "../drivers/char_lcd.h"
#include "../drivers/wires.h"
#include "setup_wires.h"
#define STRING_MAX_LEN 8
/// Wait for "*9819"
void step0(void);
#endif /* STEP_0_H */

View File

@ -1,7 +1,4 @@
#ifndef STEP_1_HPP
#define STEP_1_HPP
#include <random>
#include "step1.h"
static const char *STEP1_TAG = "step1";
@ -26,15 +23,16 @@ static uint8_t NEOPIXEL_COLORS[4][3] = {
{15, 0, 20},
};
std::random_device my_rd;
std::mt19937 my_gen(my_rd());
std::uniform_int_distribution<> zero_to_one(0, 1);
std::uniform_int_distribution<> zero_to_two(0, 2);
std::uniform_int_distribution<> zero_to_three(0, 3);
static std::random_device my_rd;
static std::mt19937 my_gen(my_rd());
static std::uniform_int_distribution<> zero_to_one(0, 1);
static std::uniform_int_distribution<> zero_to_two(0, 2);
static std::uniform_int_distribution<> zero_to_three(0, 3);
static lv_obj_t *scr;
static lv_obj_t *text = NULL;
static lv_style_t style_screen;
static lv_style_t green_text;
static lv_style_t red_text;
static lv_style_t yellow_text;
@ -49,7 +47,7 @@ static lv_style_t* color_styles[] = {
&purple_text
};
void init_step(void) {
static void init_step(void) {
scr = lv_disp_get_scr_act(NULL);
// Set the background color of the display to black.
@ -83,12 +81,12 @@ void init_step(void) {
text = lv_label_create(scr);
}
void clean_up_step(void) {
static void clean_up_step(void) {
lv_obj_clean(text);
lv_obj_clean(scr);
}
void generate_switch_leds(void) {
static void generate_switch_leds(void) {
int colors[4] = {0, 1, 2, 3};
int idx = zero_to_three(my_gen);
@ -114,7 +112,7 @@ void generate_switch_leds(void) {
led_strip_refresh(leds);
}
int generate_part_a(void) {
static int generate_part_a(void) {
int text_color = zero_to_three(my_gen);
int text_idx = zero_to_three(my_gen);
@ -130,7 +128,7 @@ int generate_part_a(void) {
return text_color;
}
int generate_part_b(void) {
static int generate_part_b(void) {
int is_color = zero_to_one(my_gen) == 0;
if (is_color) {
return generate_part_a();
@ -151,7 +149,7 @@ int generate_part_b(void) {
return text_number;
}
int generate_part_c(void) {
static int generate_part_c(void) {
int type = zero_to_two(my_gen);
if (type != 0) {
return generate_part_b();
@ -170,7 +168,7 @@ int generate_part_c(void) {
return text_color + 4;
}
void part_a(void) {
static void part_a(void) {
stop_module_timer();
set_module_time(30);
@ -209,7 +207,7 @@ void part_a(void) {
play_raw(MOUNT_POINT "/correct.pcm");
}
void part_b(void) {
static void part_b(void) {
stop_module_timer();
set_module_time(25);
@ -248,7 +246,7 @@ void part_b(void) {
play_raw(MOUNT_POINT "/correct.pcm");
}
void part_c(void) {
static void part_c(void) {
stop_module_timer();
set_module_time(20);
@ -304,7 +302,3 @@ void step1(void) {
part_c();
clean_up_step();
}
#endif /* STEP_1_HPP */

15
main/steps/step1.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef STEP_1_H
#define STEP_1_H
#include <random>
#include "../drivers/bottom_half.h"
#include "../drivers/tft.h"
#include "../drivers/leds.h"
#include "../drivers/wires.h"
#include "../drivers/speaker.h"
#include "../drivers/game_timer.h"
#include "../drivers/char_lcd.h"
void step1(void);
#endif /* STEP_1_H */

10
main/steps/step2.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef STEP_2_HPP
#define STEP_2_HPP
static const char *STEP2_TAG = "step2";
void step2(void) {
}
#endif /* STEP_2_HPP */

5
main/steps/step4.cpp Normal file
View File

@ -0,0 +1,5 @@
static const char *TAG = "step4";
void step4(void) {
}

6
main/steps/step4.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef STEP_4_H
#define STEP_4_H
void step4(void);
#endif /* STEP_4_H */

View File

@ -1,10 +0,0 @@
#ifndef STEP_4_HPP
#define STEP_4_HPP
static const char *STEP4_TAG = "step4";
void step4(void) {
}
#endif /* STEP_4_HPP */

View File

@ -1,15 +1,9 @@
#ifndef STEP_6_HPP
#define STEP_6_HPP
#include "step6.h"
#include "wires_puzzle.h"
#include "drivers/wires.h"
#include "drivers/bottom_half.h"
static const char *STEP6_TAG = "step6";
static const char *TAG = "step6";
static uint8_t cut_wires = 0;
void step6(void) {
get_cut_wires();
clear_all_pressed_released();
@ -53,5 +47,3 @@ void step6(void) {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
#endif /* STEP_6_HPP */

12
main/steps/step6.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef STEP_6_H
#define STEP_6_H
#include "wires_puzzle.h"
#include "drivers/wires.h"
#include "drivers/bottom_half.h"
#include "drivers/sd.h"
#include "drivers/speaker.h"
void step6(void);
#endif /* STEP_6_H */