diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index a97008996..7dc80a920 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -87,9 +87,9 @@ void virtualDevice::comlynx_send(uint8_t b) SYSTEM_BUS.wait_for_idle(); // Write the byte - fnUartBUS.write(b); - fnUartBUS.flush(); - fnUartBUS.read(); + SYSTEM_BUS.write(b); + SYSTEM_BUS.flush(); + SYSTEM_BUS.read(); } void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) @@ -105,8 +105,8 @@ void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) if (SYSTEM_BUS._udpDev->udpstreamActive) SYSTEM_BUS.wait_for_idle(); - fnUartBUS.write(buf, len); - fnUartBUS.readBytes(buf, len); + SYSTEM_BUS.write(buf, len); + SYSTEM_BUS.read(buf, len); } bool virtualDevice::comlynx_recv_ck() @@ -114,11 +114,11 @@ bool virtualDevice::comlynx_recv_ck() uint8_t recv_ck, ck; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) fnSystem.yield(); // get checksum - recv_ck = fnUartBUS.read(); + recv_ck = SYSTEM_BUS.read(); ck = comlynx_checksum(recvbuffer, recvbuffer_len); @@ -138,10 +138,10 @@ uint8_t virtualDevice::comlynx_recv() { uint8_t b; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) fnSystem.yield(); - b = fnUartBUS.read(); + b = SYSTEM_BUS.read(); // Add to receive buffer recvbuffer[recvbuffer_len] = b; @@ -159,7 +159,7 @@ bool virtualDevice::comlynx_recv_timeout(uint8_t *b, uint64_t dur) start = current = esp_timer_get_time(); elapsed = 0; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) { current = esp_timer_get_time(); elapsed = current - start; @@ -167,9 +167,9 @@ bool virtualDevice::comlynx_recv_timeout(uint8_t *b, uint64_t dur) break; } - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { - *b = (uint8_t)fnUartBUS.read(); + *b = (uint8_t)SYSTEM_BUS.read(); timeout = false; } // else // Debug_printf("duration: %llu\n", elapsed); @@ -202,7 +202,7 @@ unsigned short virtualDevice::comlynx_recv_buffer(uint8_t *buf, unsigned short l { unsigned short b; - b = fnUartBUS.readBytes(buf, len); + b = SYSTEM_BUS.read(buf, len); // Add to receive buffer memcpy(&recvbuffer[recvbuffer_len], buf, len); @@ -257,7 +257,7 @@ bool systemBus::wait_for_idle() dur = current - start; // Did we get any data in the FIFO while waiting? - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) return false; } while (dur < IDLE_TIME); @@ -270,7 +270,7 @@ bool systemBus::wait_for_idle() void virtualDevice::comlynx_process(uint8_t b) { - fnUartDebug.printf("comlynx_process() not implemented yet for this device. Cmd received: %02x\n", b); + fnDebugConsole.printf("comlynx_process() not implemented yet for this device. Cmd received: %02x\n", b); } void virtualDevice::comlynx_control_status() @@ -316,28 +316,27 @@ void virtualDevice::comlynx_idle() void systemBus::_comlynx_process_cmd() { - uint8_t b; - - b = fnUartBUS.read(); - //start_time = esp_timer_get_time(); + uint8_t d, b; - uint8_t d = b & 0x0F; - - #ifdef DEBUG - if ((b & 0xF0) == (MN_ACK<<4)) - Debug_println("Lynx sent ACK"); - else { - Debug_println("---"); - Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); - } - #endif + b = SYSTEM_BUS.read(); + d = b & 0x0F; + // Find device ID and pass control to it if (_daisyChain.count(d) < 1) { } else if (_daisyChain[d]->device_active == true) { + #ifdef DEBUG + if ((b & 0xF0) == (MN_ACK<<4)) + Debug_println("Lynx sent ACK"); + else { + Debug_println("---"); + Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); + } + #endif + // turn on Comlynx Indicator LED fnLedManager.set(eLed::LED_BUS, true); _daisyChain[d]->comlynx_process(b); @@ -345,8 +344,8 @@ void systemBus::_comlynx_process_cmd() fnLedManager.set(eLed::LED_BUS, false); } - //wait_for_idle(); // to avoid failing edge case where device is connected but disabled. - fnUartBUS.flush_input(); + //SYSTEM_BUS.flush_input(); + SYSTEM_BUS.flush(); } void systemBus::_comlynx_process_queue() @@ -359,7 +358,7 @@ void systemBus::service() if (_udpDev != nullptr && _udpDev->udpstreamActive) _udpDev->comlynx_handle_udpstream(); // Process anything waiting - else if (fnUartBUS.available() > 0) + else if (SYSTEM_BUS.available() > 0) _comlynx_process_cmd(); } @@ -380,7 +379,11 @@ void systemBus::setup() _udpDev = new lynxUDPStream(); // Set up UART - fnUartBUS.begin(COMLYNX_BAUDRATE); + _port.begin(ChannelConfig() + .deviceID(FN_UART_BUS) + .baud(COMLYNX_BAUDRATE) + .parity(UART_PARITY_ODD) + ); } void systemBus::shutdown() diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index b79568029..dc832b02d 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -5,6 +5,7 @@ * Comlynx Routines */ +#include "UARTChannel.h" #include #include @@ -248,6 +249,7 @@ class systemBus lynxFuji *_fujiDev = nullptr; lynxPrinter *_printerDev = nullptr; + UARTChannel _port; void _comlynx_process_cmd(); void _comlynx_process_queue(); @@ -272,15 +274,15 @@ class systemBus //int64_t comlynx_idle_time = 1000; int numDevices(); - void addDevice(virtualDevice *pDevice, FujiDeviceID device_id); + void addDevice(virtualDevice *pDevice, fujiDeviceID_t device_id); void remDevice(virtualDevice *pDevice); - void remDevice(FujiDeviceID device_id); - bool deviceExists(FujiDeviceID device_id); - void enableDevice(FujiDeviceID device_id); - void disableDevice(FujiDeviceID device_id); - virtualDevice *deviceById(FujiDeviceID device_id); - void changeDeviceId(virtualDevice *pDevice, FujiDeviceID device_id); - bool deviceEnabled(FujiDeviceID device_id); + void remDevice(fujiDeviceID_t device_id); + bool deviceExists(fujiDeviceID_t device_id); + void enableDevice(fujiDeviceID_t device_id); + void disableDevice(fujiDeviceID_t device_id); + virtualDevice *deviceById(fujiDeviceID_t device_id); + void changeDeviceId(virtualDevice *pDevice, fujiDeviceID_t device_id); + bool deviceEnabled(fujiDeviceID_t device_id); QueueHandle_t qComlynxMessages = nullptr; void setUDPHost(const char *newhost, int port); // Set new host/ip & port for UDP Stream @@ -289,9 +291,20 @@ class systemBus bool shuttingDown = false; // TRUE if we are in shutdown process bool getShuttingDown() { return shuttingDown; }; + + // Everybody thinks "oh I know how a serial port works, I'll just + // access it directly and bypass the bus!" ಠ_ಠ + size_t read(void *buffer, size_t length) { return _port.read(buffer, length); } + size_t read() { return _port.read(); } + size_t write(const void *buffer, size_t length) { return _port.write(buffer, length); } + size_t write(int n) { return _port.write(n); } + size_t available() { return _port.available(); } + void flush() { _port.flushOutput(); } + size_t print(int n, int base = 10) { return _port.print(n, base); } + size_t print(const char *str) { return _port.print(str); } + size_t print(const std::string &str) { return _port.print(str); } }; extern systemBus SYSTEM_BUS; -//extern systemBus ComLynx; #endif /* COMLYNX_H */ diff --git a/lib/device/comlynx/lynxFuji.cpp b/lib/device/comlynx/lynxFuji.cpp index dc7c9a7ab..6d1391528 100644 --- a/lib/device/comlynx/lynxFuji.cpp +++ b/lib/device/comlynx/lynxFuji.cpp @@ -144,7 +144,7 @@ void lynxFuji::comlynx_net_scan_result() if (n < _countScannedSSIDs) fnWiFi.get_scan_result(n, detail.ssid, &detail.rssi); - Debug_printf("SSID: %s - RSSI: %u\n", detail.ssid, detail.rssi); + Debug_printf("SSID: %s - RSSI: %d\n", detail.ssid, detail.rssi); memset(response, 0, sizeof(response)); memcpy(response, &detail, sizeof(detail)); diff --git a/lib/device/comlynx/modem.cpp b/lib/device/comlynx/modem.cpp index 6c9055e70..fd30e316b 100644 --- a/lib/device/comlynx/modem.cpp +++ b/lib/device/comlynx/modem.cpp @@ -7,7 +7,6 @@ #include "fnSystem.h" #include "fnConfig.h" -#include "fnUART.h" #include "fnWiFi.h" #include "utils.h" @@ -41,7 +40,7 @@ static void _telnet_event_handler(telnet_t *telnet, telnet_event_t *ev, void *us switch (ev->type) { case TELNET_EV_DATA: - if (ev->data.size && fnUartBUS.write((uint8_t *)ev->data.buffer, ev->data.size) != ev->data.size) + if (ev->data.size && SYSTEM_BUS.write((uint8_t *)ev->data.buffer, ev->data.size) != ev->data.size) Debug_printf("_telnet_event_handler(%d) - Could not write complete buffer to SIO.\n", ev->type); break; case TELNET_EV_SEND: @@ -128,8 +127,8 @@ void lynxModem::at_connect_resultCode(int modemBaud) resultCode = 1; break; } - fnUartBUS.print(resultCode); - fnUartBUS.write(ASCII_CR); + SYSTEM_BUS.print(resultCode); + SYSTEM_BUS.write(ASCII_CR); } /** @@ -138,9 +137,9 @@ void lynxModem::at_connect_resultCode(int modemBaud) */ void lynxModem::at_cmd_resultCode(int resultCode) { - fnUartBUS.print(resultCode); - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.print(resultCode); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } /** @@ -153,14 +152,14 @@ void lynxModem::at_cmd_println() if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(const char *s, bool addEol) @@ -168,20 +167,20 @@ void lynxModem::at_cmd_println(const char *s, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(s); + SYSTEM_BUS.print(s); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(int i, bool addEol) @@ -189,20 +188,20 @@ void lynxModem::at_cmd_println(int i, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(i); + SYSTEM_BUS.print(i); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(std::string s, bool addEol) @@ -210,20 +209,20 @@ void lynxModem::at_cmd_println(std::string s, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(s); + SYSTEM_BUS.print(s); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_handle_wificonnect() @@ -489,7 +488,7 @@ void lynxModem::at_handle_answer() CRX = true; cmdMode = false; - fnUartBUS.flush(); + SYSTEM_BUS.flush(); answerHack = false; } } @@ -1030,11 +1029,11 @@ void lynxModem::sio_handle_modem() // In command mode - don't exchange with TCP but gather characters to a string //if (SIO_UART.available() /*|| blockWritePending == true */ ) - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { // get char from Atari SIO //char chr = SIO_UART.read(); - char chr = fnUartBUS.read(); + char chr = SYSTEM_BUS.read(); // Return, enter, new line, carriage return.. anything goes to end the command if ((chr == ASCII_LF) || (chr == ASCII_CR) || (chr == ATASCII_EOL)) @@ -1059,9 +1058,9 @@ void lynxModem::sio_handle_modem() // Clear with a space if (commandEcho == true) { - fnUartBUS.write(ASCII_BACKSPACE); - fnUartBUS.write(' '); - fnUartBUS.write(ASCII_BACKSPACE); + SYSTEM_BUS.write(ASCII_BACKSPACE); + SYSTEM_BUS.write(' '); + SYSTEM_BUS.write(ASCII_BACKSPACE); } } } @@ -1074,7 +1073,7 @@ void lynxModem::sio_handle_modem() { cmd.erase(len - 1); if (commandEcho == true) - fnUartBUS.write(ATASCII_BACKSPACE); + SYSTEM_BUS.write(ATASCII_BACKSPACE); } } // Take into account arrow key movement and clear screen @@ -1082,7 +1081,7 @@ void lynxModem::sio_handle_modem() ((chr >= ATASCII_CURSOR_UP) && (chr <= ATASCII_CURSOR_RIGHT))) { if (commandEcho == true) - fnUartBUS.write(chr); + SYSTEM_BUS.write(chr); } else { @@ -1092,7 +1091,7 @@ void lynxModem::sio_handle_modem() cmd += chr; } if (commandEcho == true) - fnUartBUS.write(chr); + SYSTEM_BUS.write(chr); } } } @@ -1124,7 +1123,7 @@ void lynxModem::sio_handle_modem() } //int sioBytesAvail = SIO_UART.available(); - int sioBytesAvail = std::min(0, fnUartBUS.available()); + int sioBytesAvail = std::min((size_t) 0, SYSTEM_BUS.available()); // send from Atari to Fujinet if (sioBytesAvail && tcpClient.connected()) @@ -1139,7 +1138,7 @@ void lynxModem::sio_handle_modem() // Read from serial, the amount available up to // maximum size of the buffer - int sioBytesRead = fnUartBUS.readBytes(&txBuf[0], //SIO_UART.readBytes(&txBuf[0], + int sioBytesRead = SYSTEM_BUS.read(&txBuf[0], //SIO_UART.read(&txBuf[0], (sioBytesAvail > TX_BUF_SIZE) ? TX_BUF_SIZE : sioBytesAvail); // Disconnect if going to AT mode with "+++" sequence @@ -1189,8 +1188,8 @@ void lynxModem::sio_handle_modem() } else { - fnUartBUS.write(buf, bytesRead); - fnUartBUS.flush(); + SYSTEM_BUS.write(buf, bytesRead); + SYSTEM_BUS.flush(); } // And dump to sniffer, if enabled. diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index 81bb917d6..08ff10837 100644 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -617,17 +617,7 @@ void lynxNetwork::json_query(unsigned short s) json.setReadQuery(inp_string, cmdFrame.aux2); - /*json_bytes_remaining = json->json_bytes_remaining; - - std::vector tmp(json_bytes_remaining); - json->readValue(tmp.data(), json_bytes_remaining); - - // don't copy past first nul char in tmp - auto null_pos = std::find(tmp.begin(), tmp.end(), 0); - *receiveBuffer += std::string(tmp.begin(), null_pos);*/ - Debug_printf("lynxNetwork::json_query(%s)\n", inp_string.c_str()); - } void lynxNetwork::json_parse() @@ -897,13 +887,12 @@ void lynxNetwork::comlynx_control_receive_channel_json() if (jsonRecvd == false) { response_len = json.readValueLen(); - json.readValue(response,response_len); - jsonRecvd=true; + json.readValue(response, response_len); + jsonRecvd = true; comlynx_response_ack(); } else { - //ComLynx.start_time = esp_timer_get_time(); if (response_len > 0) comlynx_response_ack(); else @@ -1111,4 +1100,4 @@ void lynxNetwork::comlynx_set_timer_rate() // comlynx_complete(); } -#endif /* BUILD_LYNX */ \ No newline at end of file +#endif /* BUILD_LYNX */ diff --git a/lib/device/comlynx/udpstream.cpp b/lib/device/comlynx/udpstream.cpp index b967c9b08..63c5bea0b 100644 --- a/lib/device/comlynx/udpstream.cpp +++ b/lib/device/comlynx/udpstream.cpp @@ -87,28 +87,28 @@ void lynxUDPStream::comlynx_handle_udpstream() if (good_packet) { // Send to Lynx UART _comlynx_bus->wait_for_idle(); - fnUartBUS.write(buf_net, packetSize); + SYSTEM_BUS.write(buf_net, packetSize); #ifdef DEBUG_UDPSTREAM Debug_print("UDP-IN: "); util_dump_bytes(buf_net, packetSize); #endif - fnUartBUS.readBytes(buf_net, packetSize); // Trash what we just sent over serial + SYSTEM_BUS.read(buf_net, packetSize); // Trash what we just sent over serial } } // Read the data until there's a pause in the incoming stream - fnUartBUS.flush(); + SYSTEM_BUS.flush(); buf_stream_index = 0; - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { while (true) { - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { // Collect bytes read in our buffer - buf_stream[buf_stream_index] = (char)fnUartBUS.read(); + buf_stream[buf_stream_index] = (char)SYSTEM_BUS.read(); if (redeye_mode && (buf_stream_index == 0)) { // Check first byte if ((buf_stream[0] < 1) || (buf_stream[0] > 6)) // discard bad size byte (must be between 1 and 6) continue; @@ -120,7 +120,7 @@ void lynxUDPStream::comlynx_handle_udpstream() else { fnSystem.delay_microseconds(UDPSTREAM_PACKET_TIMEOUT); - if (fnUartBUS.available() <= 0) + if (SYSTEM_BUS.available() <= 0) break; } }