From 67a5277d2a553b88d24344a6ba4bf8f30f868d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Kopi=C4=87?= Date: Sun, 16 Jun 2024 11:14:06 +0200 Subject: [PATCH] Optimizations --- CubeGotchi.ino | 175 ++++++++++++++++++------------------------------- 1 file changed, 63 insertions(+), 112 deletions(-) diff --git a/CubeGotchi.ino b/CubeGotchi.ino index 027ca4a..d3d9e86 100644 --- a/CubeGotchi.ino +++ b/CubeGotchi.ino @@ -13,30 +13,14 @@ /****************************************/ /***** Tama Setting and Features *****/ -#define TAMA_DISPLAY_FRAMERATE 12 +#define TAMA_DISPLAY_FRAMERATE 12 #define ENABLE_TAMA_SOUND #define ENABLE_REAL_TIME #define ENABLE_SAVE_STATUS #define AUTO_SAVE_MINUTES 10 // Auto save for every 10 minutes #define ENABLE_LOAD_STATE_FROM_EEPROM -//#define ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START -// #define ENABLE_SERIAL_DUMP -//#define ENABLE_SERIAL_DEBUG_INPUT -//#define ENABLE_LOAD_HARCODED_STATE_WHEN_START -/***************************/ - -/***** Time and clock *****/ -// ESP8266 is not a very good time keeping device, each one of these little shits has a different clock oscilator. -// it also seems that the temperature also effects the clock frequency so time keeps on drifting and it get's noticable after some time. -// You can play around with these 2 to try and reduce the drift: - #define EMULATOR_CLOCK_SPEED 1000000 -// If game time is going faster than it should, increase this value by 1 to slow it down. - -// fixed microsecond sleep between each cycle used to fine tune the emulator time drift even more -#define FINE_TUNING_DELAY 1 -// If game time is going faster than it should, increase this value -/**************************************************************************/ +/***************************/ /***** Set display orientation, U8G2_MIRROR_VERTICAL is not supported *****/ //#define U8G2_LAYOUT_NORMAL @@ -59,7 +43,6 @@ U8G2_SSD1306_128X64_NONAME_2_HW_I2C display(U8G2_MIRROR); #define PIN_BTN_L 12 // D6 #define PIN_BTN_M 13 // D7 #define PIN_BTN_R 15 // D8 -//define PIN_BTN_SAVE 3 #define PIN_BUZZER 0 // D5 /**** TamaLib Specific Variables ****/ @@ -81,11 +64,10 @@ static void hal_log(log_level_t level, char *buff, ...) { static void hal_sleep_until(timestamp_t ts) { #ifdef ENABLE_REAL_TIME - int32_t remaining = (int32_t) (ts - hal_get_timestamp()); + int32_t remaining = (int32_t)(ts - hal_get_timestamp()); if (remaining > 0) { delayMicroseconds(remaining); } - delayMicroseconds(FINE_TUNING_DELAY); // fixed delay to compensate for emulator time drifting #endif } @@ -179,17 +161,15 @@ static int hal_handler(void) { #ifdef ENABLE_SAVE_STATUS if (digitalRead(PIN_BTN_L) == HIGH && digitalRead(PIN_BTN_M) == HIGH && digitalRead(PIN_BTN_R) == HIGH) { if (button4state==0) { - saveStateToEEPROM(&cpuState); - noTone(PIN_BUZZER); - tone(PIN_BUZZER, 700,100); + tone(PIN_BUZZER, 700, 100); delay(120); noTone(PIN_BUZZER); - tone(PIN_BUZZER, 880,100); + tone(PIN_BUZZER, 880, 100); delay(120); noTone(PIN_BUZZER); - tone(PIN_BUZZER, 1175,100); + tone(PIN_BUZZER, 1175, 100); delay(120); noTone(PIN_BUZZER); } @@ -198,7 +178,6 @@ static int hal_handler(void) { button4state = 0; } #endif - #endif return 0; } @@ -216,32 +195,22 @@ static hal_t hal = { .handler = &hal_handler, }; -/* -void drawTriangle(uint8_t x, uint8_t y) { - //display.drawLine(x,y,x+6,y); - display.drawLine(x+1,y+1,x+5,y+1); - display.drawLine(x+2,y+2,x+4,y+2); - display.drawLine(x+3,y+3,x+3,y+3); -} -*/ - void drawTamaRow(uint8_t tamaLCD_y, uint8_t ActualLCD_y, uint8_t thick) { uint8_t i; for (i = 0; i < LCD_WIDTH; i++) { uint8_t mask = 0b10000000; mask = mask >> (i % 8); - if ( (matrix_buffer[tamaLCD_y][i/8] & mask) != 0) { - display.drawBox(i+i+i+16,ActualLCD_y,2,thick); + if ((matrix_buffer[tamaLCD_y][i/8] & mask) != 0) { + display.drawBox(i+i+i+16, ActualLCD_y, 2, thick); } } } void drawTamaSelection(uint8_t y) { uint8_t i; - for(i=0;i<8;i++) { + for (i = 0; i < 8; i++) { if (icon_buffer[i]) { - // drawTriangle(i*16+5,y); - display.drawXBMP(i*16+4,y+6,8,8,bitmaps+i*8); + display.drawXBMP(i * 16 + 4, y + 6, 8, 8, bitmaps + i * 8); } } } @@ -250,50 +219,50 @@ void displayTama() { uint8_t j; display.firstPage(); #ifdef U8G2_LAYOUT_ROTATE_180 - drawTamaSelection(49); - display.nextPage(); + drawTamaSelection(49); + display.nextPage(); - for (j = 11; j < LCD_HEIGHT; j++) { - drawTamaRow(j,j+j+j,2); - } - display.nextPage(); + for (j = 11; j < LCD_HEIGHT; j++) { + drawTamaRow(j, j + j + j, 2); + } + display.nextPage(); - for (j = 5; j <= 10; j++) { - if (j==5) { - drawTamaRow(j,j+j+j+1,1); - } else { - drawTamaRow(j,j+j+j,2); - } + for (j = 5; j <= 10; j++) { + if (j == 5) { + drawTamaRow(j, j + j + j + 1, 1); + } else { + drawTamaRow(j, j + j + j, 2); } - display.nextPage(); + } + display.nextPage(); - for (j = 0; j <= 5; j++) { - if (j==5) { - drawTamaRow(j,j+j+j,1); - } else { - drawTamaRow(j,j+j+j,2); - } - } - display.nextPage(); + for (j = 0; j <= 5; j++) { + if (j == 5) { + drawTamaRow(j, j + j + j, 1); + } else { + drawTamaRow(j, j + j + j, 2); + } + } + display.nextPage(); #else - for (j = 0; j < LCD_HEIGHT; j++) { - if (j!=5) drawTamaRow(j,j+j+j,2); - if (j==5) { - drawTamaRow(j,j+j+j,1); - display.nextPage(); - drawTamaRow(j,j+j+j+1,1); - } - if (j==10) display.nextPage(); + for (j = 0; j < LCD_HEIGHT; j++) { + if (j != 5) drawTamaRow(j, j + j + j, 2); + if (j == 5) { + drawTamaRow(j, j + j + j, 1); + display.nextPage(); + drawTamaRow(j, j + j + j + 1, 1); } - display.nextPage(); - drawTamaSelection(49); - display.nextPage(); + if (j == 10) display.nextPage(); + } + display.nextPage(); + drawTamaSelection(49); + display.nextPage(); #endif } #if defined(ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START) || defined(ENABLE_SERIAL_DUMP) void dumpStateToSerial() { - uint16_t i, count=0; + uint16_t i, count = 0; char tmp[10]; cpu_get_state(&cpuState); u4_t *memTemp = cpuState.memory; @@ -301,37 +270,28 @@ void dumpStateToSerial() { Serial.println(""); Serial.println("static const uint8_t hardcodedState[] PROGMEM = {"); - for(i=0;i AUTO_SAVE_INTERVAL) { lastSaveTimestamp = currentMillis; saveStateToEEPROM(&cpuState); } - #endif +#endif bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH); bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH); @@ -400,9 +351,9 @@ void loop() { if (middleLeftPressed) { if ((currentMillis - middle_long_press_started) > 2000) { eraseStateFromEEPROM(); - #if defined(ESP8266) || defined(ESP32) +#if defined(ESP8266) || defined(ESP32) ESP.restart(); - #endif +#endif } } else { middle_long_press_started = currentMillis;