#include #include "esp_adc_cal.h" #include "Button2.h" #include "dndlogo.h" #define ADC_EN 14 //ADC_EN is the ADC detection enable port #define ADC_PIN 34 #define BUTTON_1 35 #define BUTTON_2 0 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library Button2 btn1(BUTTON_1); Button2 btn2(BUTTON_2); // MENU byte menuItems[] = {20, 12, 10, 8, 6, 4, 3, 2}; byte menuPosition = 0; int num_items = sizeof(menuItems) / sizeof(byte); String temp; char currentPrintOut[10]; int vref = 1100; bool showing = false; byte showingDuration = 2; unsigned long lastPush = 0; void setup() { randomSeed(analogRead(1)); Serial.begin(115200); button_init(); tft.init(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); tft.setSwapBytes(true); tft.pushImage(0, 0, 240, 135, dndlogo); tft.setSwapBytes(false); espDelay(1000); drawMenu(); } void showVoltage() { static uint64_t timeStamp = 0; if (millis() - timeStamp > 1000) { timeStamp = millis(); uint16_t v = analogRead(ADC_PIN); float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0); String voltage = "Voltage :" + String(battery_voltage) + "V"; Serial.println(voltage); //tft.fillScreen(TFT_BLACK); tft.setTextDatum(BL_DATUM); tft.drawString(voltage, tft.width() / 1.5, tft.height()); } } void throwDice() { int dice = menuItems[menuPosition]; int number = random(dice) + 1; tft.setTextPadding(tft.width()); tft.setTextDatum(BL_DATUM); tft.drawString(String(number), tft.width() / 1.5, tft.height()); } void drawMenu() { tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2); tft.drawRect(0, 20, TFT_HEIGHT, 1, TFT_WHITE); int boxWidth = round(tft.width() / num_items); for (int i = 1; i <= num_items; i++) { tft.drawFastVLine(i * boxWidth, 0, 20, TFT_WHITE); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2); tft.setTextDatum(MC_DATUM); tft.setTextPadding(0); temp = String(menuItems[i - 1]); temp.toCharArray(currentPrintOut, 10); tft.drawString(currentPrintOut, (i * boxWidth) - (boxWidth / 2), 10); } drawSelectedMenu(); } void drawSelectedMenu() { int boxWidth = round(tft.width() / num_items); tft.drawRect(menuPosition * boxWidth, 0, boxWidth, 20, TFT_WHITE); tft.fillRect(menuPosition * boxWidth, 0, boxWidth, 20, TFT_WHITE); tft.setTextColor(TFT_BLACK, TFT_WHITE); tft.setTextDatum(MC_DATUM); tft.setTextPadding(0); temp = String(menuItems[menuPosition]); temp.toCharArray(currentPrintOut, 10); tft.drawString(currentPrintOut, ((menuPosition + 1) * boxWidth) - (boxWidth / 2), 10); } void drawSelectedDice() { tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(6); tft.setTextDatum(MC_DATUM); tft.setTextPadding(tft.height()); temp = "D"; temp += String(menuItems[menuPosition]); temp.toCharArray(currentPrintOut, 10); tft.drawString(currentPrintOut, tft.width() / 2, tft.height() / 2); } void clearScreen() { tft.fillScreen(TFT_BLACK); drawMenu(); } void loop() { button_loop(); } void button_init() { // btn1.setLongClickHandler([](Button2 &b) // { // tft.setSwapBytes(true); // tft.pushImage(0, 0, 240, 135, arne); // tft.setSwapBytes(false); // espDelay(1000); // clearScreen(); // drawMenu(); // }); btn1.setPressedHandler([](Button2 &b) { throwDice(); espDelay(1000); }); btn2.setPressedHandler([](Button2 &b) { menuPosition++; menuPosition = menuPosition++ % num_items; drawMenu(); drawSelectedDice(); espDelay(1000); clearScreen(); }); } void button_loop() { btn1.loop(); btn2.loop(); } void espDelay(int ms) { esp_sleep_enable_timer_wakeup(ms * 1000); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); esp_light_sleep_start(); }