Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions lib/bus/comlynx/comlynx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -105,20 +105,20 @@ 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()
{
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);

Expand All @@ -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;
Expand All @@ -159,17 +159,17 @@ 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;
if (elapsed > 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -316,37 +316,36 @@ 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);
// turn off Comlynx Indicator LED
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()
Expand All @@ -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();
}

Expand All @@ -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()
Expand Down
31 changes: 22 additions & 9 deletions lib/bus/comlynx/comlynx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Comlynx Routines
*/

#include "UARTChannel.h"
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>

Expand Down Expand Up @@ -248,6 +249,7 @@ class systemBus
lynxFuji *_fujiDev = nullptr;
lynxPrinter *_printerDev = nullptr;

UARTChannel _port;

void _comlynx_process_cmd();
void _comlynx_process_queue();
Expand All @@ -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

Expand All @@ -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 */
2 changes: 1 addition & 1 deletion lib/device/comlynx/lynxFuji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading