Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions source/BleSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ BleSerial::read (
return c;
}

uint16_t
BleSerial::readBlocking(
void
) {
uint16_t c = static_cast<uint16_t>(-1);

if (available()) {
std::lock_guard<std::mutex> lock(_q_lock);
c = _rx.front();
_rx.pop();
}

return c;
}

void
BleSerial::unlock (
void
Expand Down
6 changes: 6 additions & 0 deletions source/BleSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ public ref class BleSerial sealed : public IStream
void
);

virtual
uint16_t
readBlocking(
void
);

virtual
void
unlock (
Expand Down
31 changes: 30 additions & 1 deletion source/BluetoothSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ BluetoothSerial::available(
if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in UsbSerial::read() and your connection has been lost.");
ConnectionLost(L"A fatal error has occurred in BluetoothSerial::available() and your connection has been lost.");
return 0;
}

Expand Down Expand Up @@ -398,6 +398,35 @@ BluetoothSerial::read(
return c;
}

uint16_t
BluetoothSerial::readBlocking(
void
)
{
uint16_t c = static_cast<uint16_t>(-1);

if (!_rx->UnconsumedBufferLength)
{
if (_current_load_operation->Status != Windows::Foundation::AsyncStatus::Started)
{
_current_load_operation = _rx->LoadAsync(MAX_READ_SIZE);
}

create_task(_current_load_operation).wait();

if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in BluetoothSerial::readBlocking() and your connection has been lost.");
return c;
}
}

c = _rx->ReadByte();

return c;
}

void
BluetoothSerial::unlock(
void
Expand Down
6 changes: 6 additions & 0 deletions source/BluetoothSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public ref class BluetoothSerial sealed : public IStream
void
);

virtual
uint16_t
readBlocking(
void
);

virtual
void
unlock(
Expand Down
8 changes: 8 additions & 0 deletions source/CurieBleSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public ref class CurieBleSerial sealed : public IStream
return _bleSerial->read();
}

virtual inline
uint16_t
readBlocking (
void
) {
return _bleSerial->readBlocking();
}

virtual inline
void
unlock (
Expand Down
8 changes: 8 additions & 0 deletions source/DfRobotBleSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public ref class DfRobotBleSerial sealed : public IStream
return _bleSerial->read();
}

virtual inline
uint16_t
readBlocking (
void
) {
return _bleSerial->readBlocking();
}

virtual inline
void
unlock (
Expand Down
9 changes: 9 additions & 0 deletions source/IStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ public interface struct IStream
void
) = 0;

///<summary>
///Attempts to read one byte once bytes are available
///</summary>
virtual
uint16_t
readBlocking(
void
) = 0;

///<summary>
///Places one byte into the outbound queue. Data will not be sent until `flush()` is called explicitly.
///</summary>
Expand Down
31 changes: 30 additions & 1 deletion source/NetworkSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ NetworkSerial::available(
if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in UsbSerial::read() and your connection has been lost.");
ConnectionLost(L"A fatal error has occurred in NetworkSerial::available() and your connection has been lost.");
return 0;
}

Expand Down Expand Up @@ -338,6 +338,35 @@ NetworkSerial::read(
return c;
}

uint16_t
NetworkSerial::readBlocking(
void
)
{
uint16_t c = static_cast<uint16_t>(-1);

if (!_rx->UnconsumedBufferLength)
{
if (_current_load_operation->Status != Windows::Foundation::AsyncStatus::Started)
{
_current_load_operation = _rx->LoadAsync(MAX_READ_SIZE);
}

create_task(_current_load_operation).wait();

if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in NetworkSerial::readBlocking() and your connection has been lost.");
return c;
}
}

c = _rx->ReadByte();

return c;
}

void
NetworkSerial::unlock(
void
Expand Down
6 changes: 6 additions & 0 deletions source/NetworkSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public ref class NetworkSerial sealed : public IStream
void
);

virtual
uint16_t
readBlocking(
void
);

virtual
void
unlock(
Expand Down
8 changes: 8 additions & 0 deletions source/RedBearLabBleSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public ref class RedBearLabBleSerial sealed : public IStream
return _bleSerial->read();
}

virtual inline
uint16_t
readBlocking (
void
) {
return _bleSerial->readBlocking();
}

virtual inline
void
unlock (
Expand Down
35 changes: 32 additions & 3 deletions source/USBSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ UsbSerial::available(
if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in UsbSerial::read() and your connection has been lost.");
ConnectionLost(L"A fatal error has occurred in UsbSerial::available() and your connection has been lost.");
return 0;
}

Expand Down Expand Up @@ -392,17 +392,46 @@ UsbSerial::print(
uint16_t
UsbSerial::read(
void
)
)
{
uint16_t c = static_cast<uint16_t>(-1);

if ( available() ) {
if (available()) {
c = _rx->ReadByte();
}

return c;
}

uint16_t
UsbSerial::readBlocking(
void
)
{
uint16_t c = static_cast<uint16_t>(-1);

if (!_rx->UnconsumedBufferLength)
{
if (_current_load_operation->Status != Windows::Foundation::AsyncStatus::Started)
{
_current_load_operation = _rx->LoadAsync(MAX_READ_SIZE);
}

create_task(_current_load_operation).wait();

if (_current_load_operation->Status == Windows::Foundation::AsyncStatus::Error)
{
_connection_ready = false;
ConnectionLost(L"A fatal error has occurred in UsbSerial::readBlocking() and your connection has been lost.");
return c;
}
}

c = _rx->ReadByte();

return c;
}

void
UsbSerial::unlock(
void
Expand Down
6 changes: 6 additions & 0 deletions source/USBSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ public ref class UsbSerial sealed : public IStream
void
);

virtual
uint16_t
readBlocking(
void
);

virtual
void
unlock(
Expand Down