moved Serial.* to comm-class

This commit is contained in:
folkert van heusden 2024-05-19 21:20:02 +02:00
parent 5ce34e61fa
commit 100ae6d3b7
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
10 changed files with 142 additions and 62 deletions

53
ESP32/comm_arduino.cpp Normal file
View file

@ -0,0 +1,53 @@
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#if defined(ESP32)
#include <driver/uart.h>
#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

29
ESP32/comm_arduino.h Normal file
View file

@ -0,0 +1,29 @@
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#include <Arduino.h>
#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;
};

View file

@ -13,9 +13,9 @@
#include "utils.h" #include "utils.h"
console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & 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), 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) int console_esp32::wait_for_char_ll(const short timeout)
{ {
for(short i=0; i<timeout / 10; i++) { for(short i=0; i<timeout / 10; i++) {
for(auto port : io_ports) { if (io_port->has_data())
if (port->available()) return io_port->get_byte();
return port->read();
}
vTaskDelay(10 / portTICK_PERIOD_MS); 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) void console_esp32::put_char_ll(const char c)
{ {
for(auto port : io_ports) io_port->send_data(reinterpret_cast<const uint8_t *>(&c), 1);
port->print(c);
} }
void console_esp32::put_string_lf(const std::string & what) void console_esp32::put_string_lf(const std::string & what)

View file

@ -1,9 +1,9 @@
// (C) 2018-2024 by Folkert van Heusden // (C) 2018-2024 by Folkert van Heusden
// Released under MIT license // Released under MIT license
#include <Arduino.h>
#include <vector> #include <vector>
#include "comm.h"
#include "console.h" #include "console.h"
@ -13,8 +13,8 @@ public:
enum panel_mode_t { PM_BITS, PM_POINTER }; enum panel_mode_t { PM_BITS, PM_POINTER };
private: private:
std::vector<Stream *> io_ports; comm *const io_port;
panel_mode_t panel_mode { PM_BITS }; // TODO: atomic_int panel_mode_t panel_mode { PM_BITS }; // TODO: atomic_int
protected: protected:
int wait_for_char_ll(const short timeout) override; int wait_for_char_ll(const short timeout) override;
@ -22,7 +22,7 @@ protected:
void put_char_ll(const char c) override; void put_char_ll(const char c) override;
public: public:
console_esp32(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & 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(); virtual ~console_esp32();
void set_panel_mode(const panel_mode_t pm); void set_panel_mode(const panel_mode_t pm);

View file

@ -1,4 +1,4 @@
// (C) 2023 by Folkert van Heusden // (C) 2023-2024 by Folkert van Heusden
// Released under MIT license // Released under MIT license
#include <SPI.h> #include <SPI.h>
@ -15,8 +15,8 @@
#define COLORED 0 #define COLORED 0
#define UNCOLORED 1 #define UNCOLORED 1
console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports) : console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, comm *const io_port):
console_esp32(stop_event, io_ports, 296 / 8, 128 / 8) console_esp32(stop_event, io_port, 296 / 8, 128 / 8)
{ {
if (epd.Init() != 0) if (epd.Init() != 0)
printf("Init of DEPG0290B01 failed"); printf("Init of DEPG0290B01 failed");

View file

@ -1,4 +1,4 @@
// (C) 2023 by Folkert van Heusden // (C) 2023-2024 by Folkert van Heusden
// Released under MIT license // Released under MIT license
#include <Arduino.h> #include <Arduino.h>
@ -22,7 +22,7 @@ private:
void put_char_ll(const char c) override; void put_char_ll(const char c) override;
public: public:
console_shabadge(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports); console_shabadge(std::atomic_uint32_t *const stop_event, comm *const io_port);
virtual ~console_shabadge(); virtual ~console_shabadge();
void panel_update_thread() override; void panel_update_thread() override;

View file

@ -4,9 +4,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <atomic> #include <atomic>
#if !defined(BUILD_FOR_RP2040)
#include <HardwareSerial.h>
#endif
#include <LittleFS.h> #include <LittleFS.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -23,6 +20,7 @@
#endif #endif
#include "comm.h" #include "comm.h"
#include "comm_arduino.h"
#include "comm_esp32_hardwareserial.h" #include "comm_esp32_hardwareserial.h"
#include "comm_tcp_socket_client.h" #include "comm_tcp_socket_client.h"
#include "comm_tcp_socket_server.h" #include "comm_tcp_socket_server.h"
@ -76,6 +74,8 @@ bool trace_output { false };
ntp *ntp_ { nullptr }; ntp *ntp_ { nullptr };
comm *cs { nullptr }; // Console Serial
static void console_thread_wrapper_panel(void *const c) static void console_thread_wrapper_panel(void *const c)
{ {
console *const cnsl = reinterpret_cast<console *>(c); console *const cnsl = reinterpret_cast<console *>(c);
@ -207,14 +207,13 @@ void start_network(console *const c)
if (!dc11_loaded) { if (!dc11_loaded) {
dc11_loaded = true; dc11_loaded = true;
Serial.println(F("* Adding DC11")); cs->println("* Adding DC11");
std::vector<comm *> comm_interfaces; std::vector<comm *> comm_interfaces;
#if !defined(BUILD_FOR_RP2040) && defined(TTY_SERIAL_RX) #if !defined(BUILD_FOR_RP2040) && defined(TTY_SERIAL_RX)
uint32_t bitrate = load_serial_speed_configuration(); 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); cs->println(format("* Init TTY (on DC11), baudrate: %d bps, RX: %d, TX: %d", bitrate, TTY_SERIAL_RX, TTY_SERIAL_TX));
Serial.println(F(""));
comm_interfaces.push_back(new comm_esp32_hardwareserial(1, TTY_SERIAL_RX, TTY_SERIAL_TX, bitrate)); comm_interfaces.push_back(new comm_esp32_hardwareserial(1, TTY_SERIAL_RX, TTY_SERIAL_TX, bitrate));
#endif #endif
@ -233,7 +232,7 @@ void start_network(console *const c)
dc11 *dc11_ = new dc11(b, comm_interfaces); dc11 *dc11_ = new dc11(b, comm_interfaces);
b->add_DC11(dc11_); 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_ = new ntp("188.212.113.203"); // TODO configurable
ntp_->begin(); ntp_->begin();
@ -260,17 +259,14 @@ void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const cha
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while(!Serial) while(!Serial)
delay(100); delay(100);
Serial.println(F("PDP11 emulator, by Folkert van Heusden")); cs = new comm_arduino(&Serial, "Serial");
Serial.print(F("GIT hash: "));
Serial.println(version_str);
Serial.println(F("Build on: " __DATE__ " " __TIME__));
Serial.print(F("Size of int: ")); cs->println("PDP11 emulator, by Folkert van Heusden");
Serial.println(sizeof(int)); cs->println(format("GIT hash: %s", version_str));
cs->println("Build on: " __DATE__ " " __TIME__);
#if defined(ESP32) #if defined(ESP32)
heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook); heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook);
@ -279,11 +275,6 @@ void setup() {
set_hostname(); set_hostname();
#endif #endif
#if !defined(BUILD_FOR_RP2040)
Serial.print(F("CPU clock frequency (MHz): "));
Serial.println(getCpuFrequencyMhz());
#endif
#if defined(BUILD_FOR_RP2040) #if defined(BUILD_FOR_RP2040)
SPI.setRX(MISO); SPI.setRX(MISO);
SPI.setTX(MOSI); SPI.setTX(MOSI);
@ -293,7 +284,7 @@ void setup() {
if (SD.begin(false, SD_SCK_MHZ(10), SPI)) if (SD.begin(false, SD_SCK_MHZ(10), SPI))
break; break;
Serial.println(F("Cannot initialize SD card")); cs->println("Cannot initialize SD card");
} }
#endif #endif
@ -304,18 +295,17 @@ void setup() {
LittleFS.setConfig(cfg); LittleFS.setConfig(cfg);
#else #else
if (!LittleFS.begin(true)) if (!LittleFS.begin(true))
Serial.println(F("LittleFS.begin() failed")); cs->println("LittleFS.begin() failed");
#endif #endif
Serial.println(F("* Init bus")); cs->println("* Init bus");
b = new bus(); b = new bus();
Serial.println(F("* Allocate memory")); cs->println("* Allocate memory");
uint32_t n_pages = DEFAULT_N_PAGES; uint32_t n_pages = DEFAULT_N_PAGES;
#if !defined(BUILD_FOR_RP2040) #if !defined(BUILD_FOR_RP2040)
Serial.print(F("Free RAM after init (decimal bytes): ")); cs->println(format("Free RAM after init (decimal bytes): %d", ESP.getFreeHeap()));
Serial.println(ESP.getFreeHeap());
if (psramInit()) { if (psramInit()) {
constexpr const uint32_t leave_unallocated = 32768; constexpr const uint32_t leave_unallocated = 32768;
@ -323,32 +313,29 @@ void setup() {
uint32_t free_psram = ESP.getFreePsram(); uint32_t free_psram = ESP.getFreePsram();
if (free_psram > leave_unallocated) { 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) 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); cs->println(format("Free PSRAM: %d decimal bytes (or %d pages (see 'ramsize' in the debugger))", free_psram, n_pages));
Serial.println(F(""));
} }
} }
#endif #endif
Serial.printf("Allocating %d (decimal) pages", n_pages); cs->println(format("Allocating %d (decimal) pages", n_pages));
b->set_memory_size(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); c = new cpu(b, &stop_event);
b->add_cpu(c); b->add_cpu(c);
std::vector<Stream *> serial_ports { &Serial };
#if defined(SHA2017) #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) #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 #endif
cnsl->set_bus(b); cnsl->set_bus(b);
cnsl->begin(); cnsl->begin();
running = cnsl->get_running_flag(); 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()); auto rk05_dev = new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag());
rk05_dev->begin(); rk05_dev->begin();
b->add_rk05(rk05_dev); b->add_rk05(rk05_dev);
@ -357,14 +344,14 @@ void setup() {
rl02_dev->begin(); rl02_dev->begin();
b->add_rl02(rl02_dev); b->add_rl02(rl02_dev);
Serial.println(F("* Adding TTY")); cs->println("* Adding TTY");
tty_ = new tty(cnsl, b); tty_ = new tty(cnsl, b);
b->add_tty(tty_); b->add_tty(tty_);
Serial.println(F("* Adding TM-11")); cs->println("* Adding TM-11");
b->add_tm11(new tm_11(b)); b->add_tm11(new tm_11(b));
Serial.println(F("* Starting KW11-L")); cs->println("* Starting KW11-L");
b->getKW11_L()->begin(cnsl); b->getKW11_L()->begin(cnsl);
#if !defined(SHA2017) #if !defined(SHA2017)
@ -375,19 +362,16 @@ void setup() {
#endif #endif
#if !defined(BUILD_FOR_RP2040) && (defined(NEOPIXELS_PIN) || defined(HEARTBEAT_PIN)) #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); xTaskCreate(&console_thread_wrapper_panel, "panel", 3072, cnsl, 1, nullptr);
#endif #endif
#if !defined(BUILD_FOR_RP2040) #if !defined(BUILD_FOR_RP2040)
uint32_t free_heap = ESP.getFreeHeap(); uint32_t free_heap = ESP.getFreeHeap();
Serial.printf("Free RAM after init: %d decimal bytes", free_heap); cs->println(format("Free RAM after init: %d decimal bytes", free_heap));
Serial.println(F(""));
#endif #endif
Serial.flush(); cs->println("* Starting console");
Serial.println(F("* Starting console"));
cnsl->start_thread(); cnsl->start_thread();
cnsl->put_string_lf("PDP-11/70 emulator, (C) Folkert van Heusden"); cnsl->put_string_lf("PDP-11/70 emulator, (C) Folkert van Heusden");

View file

@ -2,6 +2,8 @@
// Released under MIT license // Released under MIT license
#include "gen.h" #include "gen.h"
#include <cstring>
#include "comm.h" #include "comm.h"
@ -12,3 +14,15 @@ comm::comm()
comm::~comm() comm::~comm()
{ {
} }
void comm::println(const char *const s)
{
send_data(reinterpret_cast<const uint8_t *>(s), strlen(s));
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}
void comm::println(const std::string & in)
{
send_data(reinterpret_cast<const uint8_t *>(in.c_str()), in.size());
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}

3
comm.h
View file

@ -25,4 +25,7 @@ public:
virtual uint8_t get_byte() = 0; virtual uint8_t get_byte() = 0;
virtual void send_data(const uint8_t *const in, const size_t n) = 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);
}; };

View file

@ -56,8 +56,8 @@ void dc11::show_state(console *const cnsl) const
#if 0 // TODO #if 0 // TODO
if (i == serial_line) { if (i == serial_line) {
cnsl->put_string_lf(format(" Serial thread running: %s", serial_thread_running ? "true": "false" )); cnsl->put_string_lf(format(" TTY 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 enabled: %s", serial_enabled ? "true": "false" ));
} }
else { else {
if (pfds[dc11_n_lines + i].fd != INVALID_SOCKET) if (pfds[dc11_n_lines + i].fd != INVALID_SOCKET)