diff --git a/firmware/esp8266-tamagotchi/.vscode/c_cpp_properties.json b/firmware/esp8266-tamagotchi/.vscode/c_cpp_properties.json deleted file mode 100644 index a5cf27c..0000000 --- a/firmware/esp8266-tamagotchi/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,65 +0,0 @@ -// -// !!! WARNING !!! AUTO-GENERATED FILE! -// PLEASE DO NOT MODIFY IT AND USE "platformio.ini": -// https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags -// -{ - "configurations": [ - { - "name": "PlatformIO", - "includePath": [ - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/include", - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/src", - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/.pio/libdeps/nanoatmega328/U8g2/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/SPI/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/Wire/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/cores/arduino", - "/home/dronische/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/HID/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", - "" - ], - "browse": { - "limitSymbolsToIncludedHeaders": true, - "path": [ - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/include", - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/src", - "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/.pio/libdeps/nanoatmega328/U8g2/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/SPI/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/Wire/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/cores/arduino", - "/home/dronische/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/HID/src", - "/home/dronische/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", - "" - ] - }, - "defines": [ - "PLATFORMIO=60105", - "ARDUINO_AVR_NANO", - "SERIAL_BAUD=74880", - "SCREEN_WIDTH=128", - "SCREEN_HEIGHT=64", - "ENABLE_AUTO_SAVE_STATUS", - "ENABLE_LOAD_STATE_FROM_EEPROM", - "AUTO_SAVE_MINUTES=2", - "TAMA_DISPLAY_FRAMERATE=3", - "F_CPU=16000000L", - "ARDUINO_ARCH_AVR", - "ARDUINO=10808", - "__AVR_ATmega328P__", - "" - ], - "cStandard": "c11", - "cppStandard": "c++11", - "compilerPath": "/home/dronische/.platformio/packages/toolchain-atmelavr@1.70300.191015/bin/avr-gcc", - "compilerArgs": [ - "-mmcu=atmega328p", - "" - ] - } - ], - "version": 4 -} diff --git a/firmware/esp8266-tamagotchi/.vscode/launch.json b/firmware/esp8266-tamagotchi/.vscode/launch.json deleted file mode 100644 index 87679ce..0000000 --- a/firmware/esp8266-tamagotchi/.vscode/launch.json +++ /dev/null @@ -1,44 +0,0 @@ -// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY -// -// PIO Unified Debugger -// -// Documentation: https://docs.platformio.org/page/plus/debugging.html -// Configuration: https://docs.platformio.org/page/projectconf/section_env_debug.html - -{ - "version": "0.2.0", - "configurations": [ - { - "type": "platformio-debug", - "request": "launch", - "name": "PIO Debug", - "executable": "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/.pio/build/nanoatmega328/firmware.elf", - "projectEnvName": "nanoatmega328", - "toolchainBinDir": "/home/dronische/.platformio/packages/toolchain-atmelavr@1.70300.191015/bin", - "internalConsoleOptions": "openOnSessionStart", - "preLaunchTask": { - "type": "PlatformIO", - "task": "Pre-Debug" - } - }, - { - "type": "platformio-debug", - "request": "launch", - "name": "PIO Debug (skip Pre-Debug)", - "executable": "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/.pio/build/nanoatmega328/firmware.elf", - "projectEnvName": "nanoatmega328", - "toolchainBinDir": "/home/dronische/.platformio/packages/toolchain-atmelavr@1.70300.191015/bin", - "internalConsoleOptions": "openOnSessionStart" - }, - { - "type": "platformio-debug", - "request": "launch", - "name": "PIO Debug (without uploading)", - "executable": "/media/dronische/storage/prj/esp-tamagotchi/github-tamagotchi/firmware/esp8266-tamagotchi/.pio/build/nanoatmega328/firmware.elf", - "projectEnvName": "nanoatmega328", - "toolchainBinDir": "/home/dronische/.platformio/packages/toolchain-atmelavr@1.70300.191015/bin", - "internalConsoleOptions": "openOnSessionStart", - "loadMode": "manual" - } - ] -} diff --git a/firmware/esp8266-tamagotchi/platformio.ini b/firmware/esp8266-tamagotchi/platformio.ini index 3051c6b..4f6b1f8 100644 --- a/firmware/esp8266-tamagotchi/platformio.ini +++ b/firmware/esp8266-tamagotchi/platformio.ini @@ -13,6 +13,8 @@ build_flags = -D ENABLE_AUTO_SAVE_STATUS -D ENABLE_LOAD_STATE_FROM_EEPROM -D AUTO_SAVE_MINUTES=2 + -D ENABLE_TAMA_SOUND + -D BUTTON_VOLTAGE_LEVEL_PRESSED HIGH [env:nanoatmega328] platform = atmelavr @@ -28,7 +30,6 @@ build_flags = ${env.build_flags} -D TAMA_DISPLAY_FRAMERATE=8 -D ESP8266_KIT_A - -D ENABLE_TAMA_SOUND -D ENABLE_TAMA_SOUND_ACTIVE_LOW -D ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START -D ENABLE_SERIAL_DEBUG_INPUT @@ -49,3 +50,17 @@ build_flags = ${env.build_flags} -D ESP8266_KIT_B -D TAMA_DISPLAY_FRAMERATE=8 + + +[env:esp32] +platform = espressif32 +board = esp32dev +upload_protocol = esptool +board_build.filesystem = littlefs +build_flags = + ${env.build_flags} + -D ESP32 + -D BUTTON_VOLTAGE_LEVEL_PRESSED LOW + +lib_deps = + lbernstone/Tone32@^1.0.0 \ No newline at end of file diff --git a/firmware/esp8266-tamagotchi/src/cpu.c b/firmware/esp8266-tamagotchi/src/cpu.c index 8f0d068..2b4d1d2 100644 --- a/firmware/esp8266-tamagotchi/src/cpu.c +++ b/firmware/esp8266-tamagotchi/src/cpu.c @@ -27,6 +27,7 @@ #include "hal.h" #include "rom_12bit.h" + #define CPU_SPEED_RATIO 0 #define TICK_FREQUENCY 32768 // Hz @@ -838,7 +839,7 @@ static void op_retd_cb(u8_t arg0, u8_t arg1) sp = (sp + 3) & 0xFF; SET_M(x, arg0 & 0xF); SET_M(x + 1, (arg0 >> 4) & 0xF); - x = (x + 2) & 0xFFF; + x = ((x + 2) & 0xFF) | (XP << 8); call_depth--; } @@ -857,12 +858,12 @@ static void op_halt_cb(u8_t arg0, u8_t arg1) static void op_inc_x_cb(u8_t arg0, u8_t arg1) { - x = (x + 1) & 0xFFF; + x = ((x + 1) & 0xFF) | (XP << 8); } static void op_inc_y_cb(u8_t arg0, u8_t arg1) { - y = (y + 1) & 0xFFF; + y = ((y + 1) & 0xFF) | (YP << 8); } static void op_ld_x_cb(u8_t arg0, u8_t arg1) @@ -1032,32 +1033,32 @@ static void op_ld_mn_b_cb(u8_t arg0, u8_t arg1) static void op_ldpx_mx_cb(u8_t arg0, u8_t arg1) { SET_M(x, arg0); - x = (x + 1) & 0xFFF; + x = ((x + 1) & 0xFF) | (XP << 8); } static void op_ldpx_r_cb(u8_t arg0, u8_t arg1) { SET_RQ(arg0, RQ(arg1)); - x = (x + 1) & 0xFFF; + x = ((x + 1) & 0xFF) | (XP << 8); } static void op_ldpy_my_cb(u8_t arg0, u8_t arg1) { SET_M(y, arg0); - y = (y + 1) & 0xFFF; + y = ((y + 1) & 0xFF) | (YP << 8); } static void op_ldpy_r_cb(u8_t arg0, u8_t arg1) { SET_RQ(arg0, RQ(arg1)); - y = (y + 1) & 0xFFF; + y = ((y + 1) & 0xFF) | (YP << 8); } static void op_lbpx_cb(u8_t arg0, u8_t arg1) { SET_M(x, arg0 & 0xF); SET_M(x + 1, (arg0 >> 4) & 0xF); - x = (x + 2) & 0xFFF; + x = ((x + 2) & 0xFF) | (XP << 8); } static void op_set_cb(u8_t arg0, u8_t arg1) @@ -1486,7 +1487,7 @@ static void op_acpx_cb(u8_t arg0, u8_t arg1) if (tmp >> 4) { SET_C(); } else { CLEAR_C(); } } if (!M(x)) { SET_Z(); } else { CLEAR_Z(); } - x = (x + 1) & 0xFFF; + x = ((x + 1) & 0xFF) | (XP << 8); } static void op_acpy_cb(u8_t arg0, u8_t arg1) @@ -1507,7 +1508,7 @@ static void op_acpy_cb(u8_t arg0, u8_t arg1) if (tmp >> 4) { SET_C(); } else { CLEAR_C(); } } if (!M(y)) { SET_Z(); } else { CLEAR_Z(); } - y = (y + 1) & 0xFFF; + y = ((y + 1) & 0xFF) | (YP << 8); } static void op_scpx_cb(u8_t arg0, u8_t arg1) @@ -1526,7 +1527,7 @@ static void op_scpx_cb(u8_t arg0, u8_t arg1) } if (tmp >> 4) { SET_C(); } else { CLEAR_C(); } if (!M(x)) { SET_Z(); } else { CLEAR_Z(); } - x = (x + 1) & 0xFFF; + x = ((x + 1) & 0xFF) | (XP << 8); } static void op_scpy_cb(u8_t arg0, u8_t arg1) @@ -1545,7 +1546,7 @@ static void op_scpy_cb(u8_t arg0, u8_t arg1) } if (tmp >> 4) { SET_C(); } else { CLEAR_C(); } if (!M(y)) { SET_Z(); } else { CLEAR_Z(); } - y = (y + 1) & 0xFFF; + y = ((y + 1) & 0xFF) | (YP << 8); } static void op_not_cb(u8_t arg0, u8_t arg1) @@ -2003,4 +2004,4 @@ int cpu_step(void) } */ return 0; -} +} \ No newline at end of file diff --git a/firmware/esp8266-tamagotchi/src/main.cpp b/firmware/esp8266-tamagotchi/src/main.cpp index 02d0247..7c5d168 100644 --- a/firmware/esp8266-tamagotchi/src/main.cpp +++ b/firmware/esp8266-tamagotchi/src/main.cpp @@ -1,7 +1,8 @@ /* - * ArduinoGotchi - A real Tamagotchi emulator for Arduino UNO + * ArduinoGotchi - A real Tamagotchi emulator for Arduino ESP32 * - * Copyright (C) 2022 Gary Kwok + * Copyright (C) 2022 Gary Kwok - Arduino Uno Implementation + * Copyright (C) 2022 Marcel Ochsendorf - ESP32 Plattform Support * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,10 +29,14 @@ #include "savestate.h" #endif +#if defined(ESP32) +#include "Tone32.h" +#endif + /***** Set display orientation, U8G2_MIRROR_VERTICAL is not supported *****/ -//#define U8G2_LAYOUT_NORMAL -#define U8G2_LAYOUT_ROTATE_180 -//#define U8G2_LAYOUT_MIRROR +#define U8G2_LAYOUT_NORMAL +// #define U8G2_LAYOUT_ROTATE_180 +// #define U8G2_LAYOUT_MIRROR /**************************************************************************/ #ifdef U8G2_LAYOUT_NORMAL @@ -58,11 +63,15 @@ U8G2_SSD1306_128X64_NONAME_2_HW_I2C display(U8G2_MIRROR); #define PIN_BUZZER 0 #define ENABLE_TAMA_SOUND #define ENABLE_TAMA_SOUND_ACTIVE_LOW + #elif defined(ESP32) -#define PIN_BTN_L 255 -#define PIN_BTN_M 255 -#define PIN_BTN_R 255 -#define PIN_BUZZER 255 +#define PIN_BTN_L 18 +#define PIN_BTN_M 19 +#define PIN_BTN_R 23 +#define PIN_BUZZER 15 +#define BUZZER_CHANNEL 0 +#define TONE_CHANNEL 15 + #else #define PIN_BTN_L 2 #define PIN_BTN_M 3 @@ -70,12 +79,30 @@ U8G2_SSD1306_128X64_NONAME_2_HW_I2C display(U8G2_MIRROR); #define PIN_BUZZER 9 #endif +#if defined(ESP32) +void esp32_noTone(uint8_t pin, uint8_t channel) +{ + ledcDetachPin(pin); + ledcWrite(channel, 0); +} + +void esp32_tone(uint8_t pin, unsigned int frequency, unsigned long duration, uint8_t channel) +{ + if (!ledcRead(channel)) + { + ledcAttachPin(pin, channel); + } + + ledcWriteTone(channel, frequency); +} +#endif + void displayTama(); /**** TamaLib Specific Variables ****/ static uint16_t current_freq = 0; static bool_t matrix_buffer[LCD_HEIGHT][LCD_WIDTH / 8] = {{0}}; -//static byte runOnceBool = 0; +// static byte runOnceBool = 0; static bool_t icon_buffer[ICON_NUM] = {0}; static cpu_state_t cpuState; static unsigned long lastSaveTimestamp = 0; @@ -144,14 +171,18 @@ static void hal_play_frequency(bool_t en) #ifdef ENABLE_TAMA_SOUND if (en) { - tone(PIN_BUZZER, current_freq); + +#if defined(ESP32) + esp32_tone(PIN_BUZZER, current_freq, 500, BUZZER_CHANNEL); +#else +#endif } else { - noTone(PIN_BUZZER); - #ifdef ENABLE_TAMA_SOUND_ACTIVE_LOW - digitalWrite(PIN_BUZZER, HIGH); - #endif +#if defined(ESP32) + esp32_noTone(PIN_BUZZER, BUZZER_CHANNEL); +#else +#endif } #endif } @@ -190,8 +221,9 @@ static int hal_handler(void) hw_set_button(BTN_RIGHT, BTN_STATE_RELEASED); } } -#else - if (digitalRead(PIN_BTN_L) == HIGH) +#endif + + if (digitalRead(PIN_BTN_L) == BUTTON_VOLTAGE_LEVEL_PRESSED) { hw_set_button(BTN_LEFT, BTN_STATE_PRESSED); } @@ -199,7 +231,8 @@ static int hal_handler(void) { hw_set_button(BTN_LEFT, BTN_STATE_RELEASED); } - if (digitalRead(PIN_BTN_M) == HIGH) + + if (digitalRead(PIN_BTN_M) == BUTTON_VOLTAGE_LEVEL_PRESSED) { hw_set_button(BTN_MIDDLE, BTN_STATE_PRESSED); } @@ -207,7 +240,8 @@ static int hal_handler(void) { hw_set_button(BTN_MIDDLE, BTN_STATE_RELEASED); } - if (digitalRead(PIN_BTN_R) == HIGH) + + if (digitalRead(PIN_BTN_R) == BUTTON_VOLTAGE_LEVEL_PRESSED) { hw_set_button(BTN_RIGHT, BTN_STATE_PRESSED); } @@ -215,21 +249,6 @@ static int hal_handler(void) { hw_set_button(BTN_RIGHT, BTN_STATE_RELEASED); } -// #ifdef ENABLE_AUTO_SAVE_STATUS -// if (digitalRead(PIN_BTN_SAVE) == HIGH) -// { -// if (button4state == 0) -// { -// saveStateToEEPROM(&cpuState); -// } -// button4state = 1; -// } -// else -// { -// button4state = 0; -// } -// #endif -#endif return 0; } @@ -400,7 +419,10 @@ void setup() pinMode(PIN_BTN_L, INPUT); pinMode(PIN_BTN_M, INPUT); pinMode(PIN_BTN_R, INPUT); - pinMode(PIN_BUZZER, OUTPUT); + +#if defined(ESP32) + ledcSetup(BUZZER_CHANNEL, NOTE_C4, 8); +#endif display.begin(); @@ -408,13 +430,17 @@ void setup() tamalib_set_framerate(TAMA_DISPLAY_FRAMERATE); tamalib_init(1000000); +#if defined(ENABLE_AUTO_SAVE_STATUS) || defined(ENABLE_LOAD_STATE_FROM_EEPROM) initEEPROM(); +#endif #ifdef ENABLE_LOAD_STATE_FROM_EEPROM if (validEEPROM()) { loadStateFromEEPROM(&cpuState); - } else { + } + else + { Serial.println(F("No magic number in state, skipping state restore")); } #elif ENABLE_LOAD_HARCODED_STATE_WHEN_START @@ -438,15 +464,18 @@ void loop() saveStateToEEPROM(&cpuState); } - if (digitalRead(PIN_BTN_M) == HIGH) { - if (millis() - right_long_press_started > AUTO_SAVE_MINUTES * 1000) + if (digitalRead(PIN_BTN_M) == BUTTON_VOLTAGE_LEVEL_PRESSED) + { + if (millis() - right_long_press_started > AUTO_SAVE_MINUTES * 1000) { eraseStateFromEEPROM(); - #if defined(ESP8266) || defined(ESP32) +#if defined(ESP8266) || defined(ESP32) ESP.restart(); - #endif +#endif } - } else { + } + else + { right_long_press_started = millis(); } #endif