From 100ae6d3b7a7091f2aaebbca1a0b387757828b22 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 19 May 2024 21:20:02 +0200 Subject: [PATCH] moved Serial.* to comm-class --- ESP32/comm_arduino.cpp | 53 +++++++++++++++++++++++++++++ ESP32/comm_arduino.h | 29 ++++++++++++++++ ESP32/console_esp32.cpp | 13 +++---- ESP32/console_esp32.h | 8 ++--- ESP32/console_shabadge.cpp | 6 ++-- ESP32/console_shabadge.h | 4 +-- ESP32/main.ino | 70 +++++++++++++++----------------------- comm.cpp | 14 ++++++++ comm.h | 3 ++ dc11.cpp | 4 +-- 10 files changed, 142 insertions(+), 62 deletions(-) create mode 100644 ESP32/comm_arduino.cpp create mode 100644 ESP32/comm_arduino.h diff --git a/ESP32/comm_arduino.cpp b/ESP32/comm_arduino.cpp new file mode 100644 index 0000000..6b7090d --- /dev/null +++ b/ESP32/comm_arduino.cpp @@ -0,0 +1,53 @@ +// (C) 2024 by Folkert van Heusden +// Released under MIT license + +#include "gen.h" + +#if defined(ESP32) +#include + +#include "comm_arduino.h" +#include "utils.h" + + +comm_arduino::comm_arduino(Stream *const s, const std::string & name): s(s), name(name) +{ +} + +comm_arduino::~comm_arduino() +{ +} + +bool comm_arduino::begin() +{ + return true; +} + +std::string comm_arduino::get_identifier() const +{ + return name; +} + +bool comm_arduino::is_connected() +{ + return true; +} + +bool comm_arduino::has_data() +{ + return s->available(); +} + +uint8_t comm_arduino::get_byte() +{ + while(!has_data()) + vTaskDelay(5 / portTICK_PERIOD_MS); + + return s->read(); +} + +void comm_arduino::send_data(const uint8_t *const in, const size_t n) +{ + s->write(in, n); +} +#endif diff --git a/ESP32/comm_arduino.h b/ESP32/comm_arduino.h new file mode 100644 index 0000000..716090e --- /dev/null +++ b/ESP32/comm_arduino.h @@ -0,0 +1,29 @@ +// (C) 2024 by Folkert van Heusden +// Released under MIT license + +#include "gen.h" +#include +#include "comm.h" + + +class comm_arduino: public comm +{ +private: + Stream *const s; + std::string name; + +public: + comm_arduino(Stream *const s, const std::string & name); + virtual ~comm_arduino(); + + bool begin() override; + + std::string get_identifier() const; + + bool is_connected() override; + + bool has_data() override; + uint8_t get_byte() override; + + void send_data(const uint8_t *const in, const size_t n) override; +}; diff --git a/ESP32/console_esp32.cpp b/ESP32/console_esp32.cpp index 72483da..2f85240 100644 --- a/ESP32/console_esp32.cpp +++ b/ESP32/console_esp32.cpp @@ -13,9 +13,9 @@ #include "utils.h" -console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, std::vector & io_ports, const int t_width, const int t_height) : +console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, comm *const io_port, const int t_width, const int t_height) : console(stop_event, t_width, t_height), - io_ports(io_ports) + io_port(io_port) { } @@ -32,10 +32,8 @@ void console_esp32::set_panel_mode(const panel_mode_t pm) int console_esp32::wait_for_char_ll(const short timeout) { for(short i=0; iavailable()) - return port->read(); - } + if (io_port->has_data()) + return io_port->get_byte(); vTaskDelay(10 / portTICK_PERIOD_MS); } @@ -45,8 +43,7 @@ int console_esp32::wait_for_char_ll(const short timeout) void console_esp32::put_char_ll(const char c) { - for(auto port : io_ports) - port->print(c); + io_port->send_data(reinterpret_cast(&c), 1); } void console_esp32::put_string_lf(const std::string & what) diff --git a/ESP32/console_esp32.h b/ESP32/console_esp32.h index cfc730d..7764c93 100644 --- a/ESP32/console_esp32.h +++ b/ESP32/console_esp32.h @@ -1,9 +1,9 @@ // (C) 2018-2024 by Folkert van Heusden // Released under MIT license -#include #include +#include "comm.h" #include "console.h" @@ -13,8 +13,8 @@ public: enum panel_mode_t { PM_BITS, PM_POINTER }; private: - std::vector io_ports; - panel_mode_t panel_mode { PM_BITS }; // TODO: atomic_int + comm *const io_port; + panel_mode_t panel_mode { PM_BITS }; // TODO: atomic_int protected: int wait_for_char_ll(const short timeout) override; @@ -22,7 +22,7 @@ protected: void put_char_ll(const char c) override; public: - console_esp32(std::atomic_uint32_t *const stop_event, std::vector & io_ports, const int t_width, const int t_height); + console_esp32(std::atomic_uint32_t *const stop_event, comm *const io_port, const int t_width, const int t_height); virtual ~console_esp32(); void set_panel_mode(const panel_mode_t pm); diff --git a/ESP32/console_shabadge.cpp b/ESP32/console_shabadge.cpp index 3348bf9..f7ad3ce 100644 --- a/ESP32/console_shabadge.cpp +++ b/ESP32/console_shabadge.cpp @@ -1,4 +1,4 @@ -// (C) 2023 by Folkert van Heusden +// (C) 2023-2024 by Folkert van Heusden // Released under MIT license #include @@ -15,8 +15,8 @@ #define COLORED 0 #define UNCOLORED 1 -console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, std::vector & io_ports) : - console_esp32(stop_event, io_ports, 296 / 8, 128 / 8) +console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, comm *const io_port): + console_esp32(stop_event, io_port, 296 / 8, 128 / 8) { if (epd.Init() != 0) printf("Init of DEPG0290B01 failed"); diff --git a/ESP32/console_shabadge.h b/ESP32/console_shabadge.h index bf09419..279caff 100644 --- a/ESP32/console_shabadge.h +++ b/ESP32/console_shabadge.h @@ -1,4 +1,4 @@ -// (C) 2023 by Folkert van Heusden +// (C) 2023-2024 by Folkert van Heusden // Released under MIT license #include @@ -22,7 +22,7 @@ private: void put_char_ll(const char c) override; public: - console_shabadge(std::atomic_uint32_t *const stop_event, std::vector & io_ports); + console_shabadge(std::atomic_uint32_t *const stop_event, comm *const io_port); virtual ~console_shabadge(); void panel_update_thread() override; diff --git a/ESP32/main.ino b/ESP32/main.ino index aa4b17f..27a0e51 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -4,9 +4,6 @@ #include #include #include -#if !defined(BUILD_FOR_RP2040) -#include -#endif #include #include #include @@ -23,6 +20,7 @@ #endif #include "comm.h" +#include "comm_arduino.h" #include "comm_esp32_hardwareserial.h" #include "comm_tcp_socket_client.h" #include "comm_tcp_socket_server.h" @@ -76,6 +74,8 @@ bool trace_output { false }; ntp *ntp_ { nullptr }; +comm *cs { nullptr }; // Console Serial + static void console_thread_wrapper_panel(void *const c) { console *const cnsl = reinterpret_cast(c); @@ -207,14 +207,13 @@ void start_network(console *const c) if (!dc11_loaded) { dc11_loaded = true; - Serial.println(F("* Adding DC11")); + cs->println("* Adding DC11"); std::vector comm_interfaces; #if !defined(BUILD_FOR_RP2040) && defined(TTY_SERIAL_RX) uint32_t bitrate = load_serial_speed_configuration(); - Serial.printf("* Init TTY (on DC11), baudrate: %d bps, RX: %d, TX: %d", bitrate, TTY_SERIAL_RX, TTY_SERIAL_TX); - Serial.println(F("")); + cs->println(format("* Init TTY (on DC11), baudrate: %d bps, RX: %d, TX: %d", bitrate, TTY_SERIAL_RX, TTY_SERIAL_TX)); comm_interfaces.push_back(new comm_esp32_hardwareserial(1, TTY_SERIAL_RX, TTY_SERIAL_TX, bitrate)); #endif @@ -233,7 +232,7 @@ void start_network(console *const c) dc11 *dc11_ = new dc11(b, comm_interfaces); b->add_DC11(dc11_); - Serial.println(F("* Starting (NTP-) clock")); + cs->println("* Starting (NTP-) clock"); ntp_ = new ntp("188.212.113.203"); // TODO configurable ntp_->begin(); @@ -260,17 +259,14 @@ void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const cha void setup() { Serial.begin(115200); - while(!Serial) delay(100); - Serial.println(F("PDP11 emulator, by Folkert van Heusden")); - Serial.print(F("GIT hash: ")); - Serial.println(version_str); - Serial.println(F("Build on: " __DATE__ " " __TIME__)); + cs = new comm_arduino(&Serial, "Serial"); - Serial.print(F("Size of int: ")); - Serial.println(sizeof(int)); + cs->println("PDP11 emulator, by Folkert van Heusden"); + cs->println(format("GIT hash: %s", version_str)); + cs->println("Build on: " __DATE__ " " __TIME__); #if defined(ESP32) heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook); @@ -279,11 +275,6 @@ void setup() { set_hostname(); #endif -#if !defined(BUILD_FOR_RP2040) - Serial.print(F("CPU clock frequency (MHz): ")); - Serial.println(getCpuFrequencyMhz()); -#endif - #if defined(BUILD_FOR_RP2040) SPI.setRX(MISO); SPI.setTX(MOSI); @@ -293,7 +284,7 @@ void setup() { if (SD.begin(false, SD_SCK_MHZ(10), SPI)) break; - Serial.println(F("Cannot initialize SD card")); + cs->println("Cannot initialize SD card"); } #endif @@ -304,18 +295,17 @@ void setup() { LittleFS.setConfig(cfg); #else if (!LittleFS.begin(true)) - Serial.println(F("LittleFS.begin() failed")); + cs->println("LittleFS.begin() failed"); #endif - Serial.println(F("* Init bus")); + cs->println("* Init bus"); b = new bus(); - Serial.println(F("* Allocate memory")); + cs->println("* Allocate memory"); uint32_t n_pages = DEFAULT_N_PAGES; #if !defined(BUILD_FOR_RP2040) - Serial.print(F("Free RAM after init (decimal bytes): ")); - Serial.println(ESP.getFreeHeap()); + cs->println(format("Free RAM after init (decimal bytes): %d", ESP.getFreeHeap())); if (psramInit()) { constexpr const uint32_t leave_unallocated = 32768; @@ -323,32 +313,29 @@ void setup() { uint32_t free_psram = ESP.getFreePsram(); if (free_psram > leave_unallocated) { n_pages = min((free_psram - leave_unallocated) / 8192, uint32_t(256)); // start size is 2 MB max (with 1 MB, UNIX 7 behaves strangely) - Serial.printf("Free PSRAM: %d decimal bytes (or %d pages (see 'ramsize' in the debugger))", free_psram, n_pages); - Serial.println(F("")); + cs->println(format("Free PSRAM: %d decimal bytes (or %d pages (see 'ramsize' in the debugger))", free_psram, n_pages)); } } #endif - Serial.printf("Allocating %d (decimal) pages", n_pages); + cs->println(format("Allocating %d (decimal) pages", n_pages)); b->set_memory_size(n_pages); - Serial.println(F("")); - Serial.println(F("* Init CPU")); + cs->println("* Init CPU"); c = new cpu(b, &stop_event); b->add_cpu(c); - std::vector serial_ports { &Serial }; #if defined(SHA2017) - cnsl = new console_shabadge(&stop_event, serial_ports); + cnsl = new console_shabadge(&stop_event, cs); #elif defined(ESP32) || defined(BUILD_FOR_RP2040) - cnsl = new console_esp32(&stop_event, serial_ports, 80, 25); + cnsl = new console_esp32(&stop_event, cs, 80, 25); #endif cnsl->set_bus(b); cnsl->begin(); running = cnsl->get_running_flag(); - Serial.println(F("* Connect RK05 and RL02 devices to BUS")); + cs->println("* Connect RK05 and RL02 devices to BUS"); auto rk05_dev = new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); rk05_dev->begin(); b->add_rk05(rk05_dev); @@ -357,14 +344,14 @@ void setup() { rl02_dev->begin(); b->add_rl02(rl02_dev); - Serial.println(F("* Adding TTY")); + cs->println("* Adding TTY"); tty_ = new tty(cnsl, b); b->add_tty(tty_); - Serial.println(F("* Adding TM-11")); + cs->println("* Adding TM-11"); b->add_tm11(new tm_11(b)); - Serial.println(F("* Starting KW11-L")); + cs->println("* Starting KW11-L"); b->getKW11_L()->begin(cnsl); #if !defined(SHA2017) @@ -375,19 +362,16 @@ void setup() { #endif #if !defined(BUILD_FOR_RP2040) && (defined(NEOPIXELS_PIN) || defined(HEARTBEAT_PIN)) - Serial.println(F("Starting panel")); + cs->println("Starting panel"); xTaskCreate(&console_thread_wrapper_panel, "panel", 3072, cnsl, 1, nullptr); #endif #if !defined(BUILD_FOR_RP2040) uint32_t free_heap = ESP.getFreeHeap(); - Serial.printf("Free RAM after init: %d decimal bytes", free_heap); - Serial.println(F("")); + cs->println(format("Free RAM after init: %d decimal bytes", free_heap)); #endif - Serial.flush(); - - Serial.println(F("* Starting console")); + cs->println("* Starting console"); cnsl->start_thread(); cnsl->put_string_lf("PDP-11/70 emulator, (C) Folkert van Heusden"); diff --git a/comm.cpp b/comm.cpp index 0d609ab..7c15352 100644 --- a/comm.cpp +++ b/comm.cpp @@ -2,6 +2,8 @@ // Released under MIT license #include "gen.h" +#include + #include "comm.h" @@ -12,3 +14,15 @@ comm::comm() comm::~comm() { } + +void comm::println(const char *const s) +{ + send_data(reinterpret_cast(s), strlen(s)); + send_data(reinterpret_cast("\r\n"), 2); +} + +void comm::println(const std::string & in) +{ + send_data(reinterpret_cast(in.c_str()), in.size()); + send_data(reinterpret_cast("\r\n"), 2); +} diff --git a/comm.h b/comm.h index d9c5739..a2db043 100644 --- a/comm.h +++ b/comm.h @@ -25,4 +25,7 @@ public: virtual uint8_t get_byte() = 0; virtual void send_data(const uint8_t *const in, const size_t n) = 0; + + void println(const char *const s); + void println(const std::string & in); }; diff --git a/dc11.cpp b/dc11.cpp index cfdde41..48629a8 100644 --- a/dc11.cpp +++ b/dc11.cpp @@ -56,8 +56,8 @@ void dc11::show_state(console *const cnsl) const #if 0 // TODO if (i == serial_line) { - cnsl->put_string_lf(format(" Serial thread running: %s", serial_thread_running ? "true": "false" )); - cnsl->put_string_lf(format(" Serial enabled: %s", serial_enabled ? "true": "false" )); + cnsl->put_string_lf(format(" TTY thread running: %s", serial_thread_running ? "true": "false" )); + cnsl->put_string_lf(format(" TTY enabled: %s", serial_enabled ? "true": "false" )); } else { if (pfds[dc11_n_lines + i].fd != INVALID_SOCKET)