// (C) 2024 by Folkert van Heusden // Released under MIT license #pragma once #include #include #include #include #include #include #if defined(_WIN32) #include #include #else #define SOCKET int #define INVALID_SOCKET -1 #endif #include "device.h" #include "gen.h" #include "bus.h" #include "log.h" #define DC11_RCSR 0174000 // receiver status register #define DC11_BASE DC11_RCSR #define DC11_END (DC11_BASE + (4 * 4 + 1) * 2) // 4 interfaces, + 2 to point behind it class Stream; class bus; struct pollfd; // 4 interfaces constexpr const int dc11_n_lines = 4; class dc11: public device { private: int base_port { 1100 }; bus *const b { nullptr }; uint16_t registers[4 * dc11_n_lines] { 0 }; std::atomic_bool stop_flag { false }; std::thread *th { nullptr }; // not statically allocated because of compiling problems on arduino #if defined(_WIN32) WSAPOLLFD *pfds { nullptr }; #else pollfd *pfds { nullptr }; #endif std::vector recv_buffers[dc11_n_lines]; mutable std::mutex input_lock[dc11_n_lines]; std::atomic_bool serial_thread_running { false }; bool serial_enabled { false }; #if IS_POSIX std::thread *serial_th { nullptr }; int serial_fd { -1 }; #endif void trigger_interrupt(const int line_nr, const bool is_tx); bool is_rx_interrupt_enabled(const int line_nr) const; bool is_tx_interrupt_enabled(const int line_nr) const; public: dc11(const int base_port, bus *const b); virtual ~dc11(); #if IS_POSIX // json_t *serialize(); // static tty *deserialize(const json_t *const j, bus *const b, console *const cnsl); #endif void reset() override; void show_state(console *const cnsl) const override; void test_serial(const std::string & txt) const; #if defined(ESP32) void set_serial(const int bitrate, const int rx, const int tx); void serial_handler(); #elif IS_POSIX void set_serial(const int bitrate, const std::string & device_name); void serial_handler(); #endif uint8_t read_byte(const uint16_t addr) override; uint16_t read_word(const uint16_t addr) override; void write_byte(const uint16_t addr, const uint8_t v) override; void write_word(const uint16_t addr, const uint16_t v) override; void operator()(); };