Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Revision 4:caf56f265a13, committed 2018-01-19
- Comitter:
- IanBenzMaxim
- Date:
- Fri Jan 19 10:25:02 2018 -0600
- Parent:
- 3:f818ea5172ed
- Child:
- 5:3523abdda8ed
- Child:
- 6:a8c83a2e6fa4
- Commit message:
- Cleaned old files.
Changed in this revision
--- a/Platforms/dotnet/DS9481P_300.cpp Thu Jan 11 13:50:39 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,283 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -*******************************************************************************/ - -#include <utility> -#include <msclr/marshal_cppstd.h> -#include <MaximInterface/Utilities/Error.hpp> -#include "DS9481P_300.hpp" - -using System::IO::Ports::SerialPort; -using msclr::interop::marshal_as; -using std::string; - -namespace MaximInterface { - -DS9481P_300::DS9481P_300() - : currentBus(Bus::None), linkLayerMaster("{DS9097U_DS9480}"), - oneWireMaster_(*this), serial(), ds9400(serial), i2cMaster_(*this) {} - -DS9481P_300::~DS9481P_300() { disconnect(); } - -error_code DS9481P_300::connect(const string & portName) { - return connected() ? make_error_code(ConnectionStateError) - : selectBus(Bus::OneWire, portName); -} - -void DS9481P_300::disconnect() { selectBus(Bus::None); } - -string DS9481P_300::portName() const { - switch (currentBus) { - case Bus::I2C: - return serial.portName(); - - case Bus::OneWire: - return linkLayerMaster.portName(); - } - return ""; -} - -error_code DS9481P_300::selectBus(Bus newBus) { - return connected() ? selectBus(newBus, portName()) - : make_error_code(ConnectionStateError); -} - -error_code DS9481P_300::selectBus(Bus newBus, const string & portName) { - error_code result; - if (currentBus != newBus) { - const auto selectI2C = [this](const string & portName) { - auto result = serial.connect(portName); - if (!result) { - // Escape DS2480 Mode. - result = serial.writeByte(0xE5); - } - if (!result) { - // Wait for awake notification. - uint_least8_t data; - result = serial.readByte(data); - } - return result; - }; - - const auto selectOneWire = [this](const string & portName) { - return linkLayerMaster.connect(portName); - }; - - switch (currentBus) { - case Bus::None: - switch (newBus) { - case Bus::OneWire: - result = selectOneWire(portName); - break; - - case Bus::I2C: - result = selectI2C(portName); - break; - } - break; - - case Bus::OneWire: - linkLayerMaster.disconnect(); - switch (newBus) { - case Bus::I2C: - result = selectI2C(portName); - break; - } - break; - - case Bus::I2C: { - constexpr uint_least8_t buffer[] = {'C', 'O'}; - result = serial.writeBlock(buffer, sizeof(buffer) / sizeof(buffer[0])); - if (!result) { - serial.disconnect(); - switch (newBus) { - case Bus::OneWire: - result = selectOneWire(portName); - break; - } - } - } break; - } - if (!result) { - currentBus = newBus; - } - } - return result; -} - -error_code DS9481P_300::OneWireMasterImpl::reset() { - return selectAndExecute([this] { return OneWireMasterDecorator::reset(); }); -} - -error_code DS9481P_300::OneWireMasterImpl::touchBitSetLevel(bool & sendRecvBit, - Level afterLevel) { - return selectAndExecute([this, &sendRecvBit, afterLevel] { - return OneWireMasterDecorator::touchBitSetLevel(sendRecvBit, afterLevel); - }); -} - -error_code -DS9481P_300::OneWireMasterImpl::writeByteSetLevel(uint_least8_t sendByte, - Level afterLevel) { - return selectAndExecute([this, sendByte, afterLevel] { - return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel); - }); -} - -error_code -DS9481P_300::OneWireMasterImpl::readByteSetLevel(uint_least8_t & recvByte, - Level afterLevel) { - return selectAndExecute([this, &recvByte, afterLevel] { - return OneWireMasterDecorator::readByteSetLevel(recvByte, afterLevel); - }); -} - -error_code -DS9481P_300::OneWireMasterImpl::writeBlock(const uint_least8_t * sendBuf, - size_t sendLen) { - return selectAndExecute([this, sendBuf, sendLen] { - return OneWireMasterDecorator::writeBlock(sendBuf, sendLen); - }); -} - -error_code DS9481P_300::OneWireMasterImpl::readBlock(uint_least8_t * recvBuf, - size_t recvLen) { - return selectAndExecute([this, recvBuf, recvLen] { - return OneWireMasterDecorator::readBlock(recvBuf, recvLen); - }); -} - -error_code DS9481P_300::OneWireMasterImpl::setSpeed(Speed newSpeed) { - return selectAndExecute( - [this, newSpeed] { return OneWireMasterDecorator::setSpeed(newSpeed); }); -} - -error_code DS9481P_300::OneWireMasterImpl::setLevel(Level newLevel) { - return selectAndExecute( - [this, newLevel] { return OneWireMasterDecorator::setLevel(newLevel); }); -} - -error_code DS9481P_300::OneWireMasterImpl::triplet(TripletData & data) { - return selectAndExecute( - [this, &data] { return OneWireMasterDecorator::triplet(data); }); -} - -template <typename Func> -error_code DS9481P_300::OneWireMasterImpl::selectAndExecute(Func operation) { - auto result = parent->selectBus(Bus::OneWire); - if (!result) { - result = operation(); - } - return result; -} - -error_code DS9481P_300::I2CMasterImpl::start(uint_least8_t address) { - return selectAndExecute( - [this, address] { return I2CMasterDecorator::start(address); }); -} - -error_code DS9481P_300::I2CMasterImpl::stop() { - return selectAndExecute([this] { return I2CMasterDecorator::stop(); }); -} - -error_code DS9481P_300::I2CMasterImpl::writeByte(uint_least8_t data) { - return selectAndExecute( - [this, data] { return I2CMasterDecorator::writeByte(data); }); -} - -error_code DS9481P_300::I2CMasterImpl::writeBlock(const uint_least8_t * data, - size_t dataLen) { - return selectAndExecute([this, data, dataLen] { - return I2CMasterDecorator::writeBlock(data, dataLen); - }); -} - -error_code DS9481P_300::I2CMasterImpl::writePacket(uint_least8_t address, - const uint_least8_t * data, - size_t dataLen, - bool sendStop) { - return selectAndExecute([this, address, data, dataLen, sendStop] { - return I2CMasterDecorator::writePacket(address, data, dataLen, sendStop); - }); -} - -error_code DS9481P_300::I2CMasterImpl::readByte(AckStatus status, - uint_least8_t & data) { - return selectAndExecute([this, status, &data] { - return I2CMasterDecorator::readByte(status, data); - }); -} - -error_code DS9481P_300::I2CMasterImpl::readBlock(AckStatus status, - uint_least8_t * data, - size_t dataLen) { - return selectAndExecute([this, status, data, dataLen] { - return I2CMasterDecorator::readBlock(status, data, dataLen); - }); -} - -error_code DS9481P_300::I2CMasterImpl::readPacket(uint_least8_t address, - uint_least8_t * data, - size_t dataLen, - bool sendStop) { - return selectAndExecute([this, address, data, dataLen, sendStop] { - return I2CMasterDecorator::readPacket(address, data, dataLen, sendStop); - }); -} - -template <typename Func> -error_code DS9481P_300::I2CMasterImpl::selectAndExecute(Func operation) { - auto result = parent->selectBus(Bus::I2C); - if (!result) { - result = operation(); - } - return result; -} - -const error_category & DS9481P_300::errorCategory() { - static class : public error_category { - public: - virtual const char * name() const { return "DS9481P_300"; } - - virtual std::string message(int condition) const { - switch (condition) { - case ConnectionStateError: - return "Connection State Error"; - - default: - return defaultErrorMessage(condition); - } - } - } instance; - return instance; -} - -} // namespace MaximInterface \ No newline at end of file
--- a/Platforms/dotnet/DS9481P_300.hpp Thu Jan 11 13:50:39 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,148 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -*******************************************************************************/ - -#pragma once - -#include <string> -#include <MaximInterface/Devices/DS9400.hpp> -#include <MaximInterface/Links/I2CMasterDecorator.hpp> -#include <MaximInterface/Links/OneWireMasterDecorator.hpp> -#include <MaximInterface/Utilities/Export.h> -#include "MoveOnly.hpp" -#include "OneWireLinkLayerMaster.hpp" -#include "Uart.hpp" - -namespace MaximInterface { - -/// DS9481P-300 USB to 1-Wire and I2C adapter. -class DS9481P_300 : private MoveOnly { -public: - enum ErrorValue { ConnectionStateError = 1 }; - - MaximInterface_EXPORT DS9481P_300(); - MaximInterface_EXPORT ~DS9481P_300(); - - DS9481P_300(DS9481P_300 &&) = default; - DS9481P_300 & operator=(DS9481P_300 &&) = default; - - /// Access the 1-Wire master when connected to an adapter. - OneWireMaster & oneWireMaster() { return oneWireMaster_; } - - /// Access the I2C master when connected to an adapter. - I2CMaster & i2cMaster() { return i2cMaster_; } - - /// Connect to an adapter on the specified COM port. - MaximInterface_EXPORT error_code connect(const std::string & portName); - - /// Disconnect from the adapter on the current COM port. - MaximInterface_EXPORT void disconnect(); - - /// Check if currently connected to an adapter. - /// @returns True if connected. - bool connected() const { return currentBus != Bus::None; } - - /// Get the currently connected COM port name. - MaximInterface_EXPORT std::string portName() const; - - MaximInterface_EXPORT static const error_category & errorCategory(); - -private: - class OneWireMasterImpl : public OneWireMasterDecorator { - public: - explicit OneWireMasterImpl(DS9481P_300 & parent) - : OneWireMasterDecorator(parent.linkLayerMaster), parent(&parent) {} - - virtual error_code reset() override; - virtual error_code touchBitSetLevel(bool & sendRecvBit, - Level afterLevel) override; - virtual error_code writeByteSetLevel(uint_least8_t sendByte, - Level afterLevel) override; - virtual error_code readByteSetLevel(uint_least8_t & recvByte, - Level afterLevel) override; - virtual error_code writeBlock(const uint_least8_t * sendBuf, - size_t sendLen) override; - virtual error_code readBlock(uint_least8_t * recvBuf, - size_t recvLen) override; - virtual error_code setSpeed(Speed newSpeed) override; - virtual error_code setLevel(Level newLevel) override; - virtual error_code triplet(TripletData & data) override; - - private: - DS9481P_300 * parent; - - template <typename Func> error_code selectAndExecute(Func operation); - }; - - class I2CMasterImpl : public I2CMasterDecorator { - public: - explicit I2CMasterImpl(DS9481P_300 & parent) noexcept - : I2CMasterDecorator(parent.ds9400), parent(&parent) {} - - virtual error_code start(uint_least8_t address) override; - virtual error_code stop() override; - virtual error_code writeByte(uint_least8_t data) override; - virtual error_code writeBlock(const uint_least8_t * data, - size_t dataLen) override; - virtual error_code writePacket(uint_least8_t address, - const uint_least8_t * data, size_t dataLen, - bool sendStop) override; - virtual error_code readByte(AckStatus status, - uint_least8_t & data) override; - virtual error_code readBlock(AckStatus status, uint_least8_t * data, - size_t dataLen) override; - virtual error_code readPacket(uint_least8_t address, uint_least8_t * data, - size_t dataLen, bool sendStop) override; - - private: - DS9481P_300 * parent; - - template <typename Func> error_code selectAndExecute(Func operation); - }; - - enum class Bus { None, OneWire, I2C }; - - Bus currentBus; - OneWireLinkLayerMaster linkLayerMaster; - OneWireMasterImpl oneWireMaster_; - dotnet::Uart serial; - DS9400 ds9400; - I2CMasterImpl i2cMaster_; - - error_code selectBus(Bus newBus); - error_code selectBus(Bus newBus, const std::string & portName); -}; - -inline error_code make_error_code(DS9481P_300::ErrorValue e) { - return error_code(e, DS9481P_300::errorCategory()); -} - -} // namespace MaximInterface \ No newline at end of file
--- a/Platforms/dotnet/Uart.cpp Thu Jan 11 13:50:39 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,218 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -*******************************************************************************/ - -#include <msclr/auto_gcroot.h> -#include <msclr/marshal_cppstd.h> -#include <MaximInterface/Utilities/Error.hpp> -#include "ChangeSizeType.hpp" -#include "Sleep.hpp" -#include "Uart.hpp" - -using msclr::interop::marshal_as; -using std::string; -using namespace System; -using System::IO::Ports::SerialPort; -using System::Runtime::InteropServices::Marshal; - -namespace MaximInterface { -namespace dotnet { - -template <typename Type, typename Input> static bool isType(Input in) { - return dynamic_cast<Type>(in) != nullptr; -} - -template <typename Func> -static error_code executeTryCatchOperation(Func tryOperation) { - return executeTryCatchOperation(tryOperation, [] {}); -} - -template <typename TryFunc, typename CatchFunc> -static error_code executeTryCatchOperation(TryFunc tryOperation, - CatchFunc catchOperation) { - error_code result; - try { - tryOperation(); - } catch (Exception ^ e) { - catchOperation(); - if (isType<ArgumentException ^>(e)) { - result = make_error_code(Uart::ArgumentError); - } else if (isType<InvalidOperationException ^>(e)) { - result = make_error_code(Uart::InvalidOperationError); - } else if (isType<System::IO::IOException ^>(e)) { - result = make_error_code(Uart::IOError); - } else if (isType<UnauthorizedAccessException ^>(e)) { - result = make_error_code(Uart::UnauthorizedAccessError); - } else if (isType<TimeoutException ^>(e)) { - result = make_error_code(Uart::TimeoutError); - } else { - throw; - } - } - return result; -} - -template <typename Func> -static error_code executeIfConnected(const Uart & serial, Func operation) { - return serial.connected() ? operation() - : make_error_code(Uart::NotConnectedError); -} - -struct Uart::Data { - msclr::auto_gcroot<SerialPort ^> port; -}; - -Uart::Uart() : data(std::make_unique<Data>()) {} - -Uart::~Uart() = default; - -Uart::Uart(Uart &&) noexcept = default; - -Uart & Uart::operator=(Uart &&) noexcept = default; - -error_code Uart::connect(const string & portName) { - data->port = gcnew SerialPort; - return executeTryCatchOperation( - [this, &portName] { - data->port->PortName = marshal_as<String ^>(portName); - data->port->DtrEnable = true; - data->port->Open(); - }, - [this] { data->port.reset(); }); -} - -void Uart::disconnect() { data->port.reset(); } - -bool Uart::connected() const { return data->port.get() != nullptr; } - -string Uart::portName() const { - return connected() ? marshal_as<string>(data->port->PortName) : ""; -} - -error_code Uart::setBaud(int_least32_t baud) { - return executeIfConnected(*this, [this, baud] { - return executeTryCatchOperation( - [this, baud] { data->port->BaudRate = baud; }); - }); -} - -error_code Uart::sendBreak() { - return executeIfConnected(*this, [this] { - return executeTryCatchOperation([this] { - data->port->BreakState = true; - sleep(1); - data->port->BreakState = false; - }); - }); -} - -error_code Uart::clearReadBuffer() { - return executeIfConnected(*this, [this] { - return executeTryCatchOperation([this] { data->port->ReadExisting(); }); - }); -} - -error_code Uart::writeByte(uint_least8_t data) { return writeBlock(&data, 1); } - -error_code Uart::writeBlock(const uint_least8_t * data, size_t dataLen) { - return executeIfConnected(*this, [this, data, dataLen] { - return executeTryCatchOperation([this, data, dataLen] { - changeSizeType<int>( - [this](const uint_least8_t * dataChunk, int dataChunkSize) { - auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize); - Marshal::Copy( - static_cast<IntPtr>(const_cast<uint_least8_t *>(dataChunk)), - dataManaged, 0, dataChunkSize); - this->data->port->Write(dataManaged, 0, dataChunkSize); - }, - data, dataLen); - }); - }); -} - -error_code Uart::readByte(uint_least8_t & data) { - return executeIfConnected(*this, [this, &data] { - return executeTryCatchOperation( - [this, &data] { data = this->data->port->ReadByte(); }); - }); -} - -error_code Uart::readBlock(uint_least8_t * data, size_t dataLen) { - return executeIfConnected(*this, [this, data, dataLen] { - return executeTryCatchOperation([this, data, dataLen] { - changeSizeType<int>( - [this](uint_least8_t * dataChunk, int dataChunkSize) { - auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize); - int bytesRead = 0; - do { - bytesRead += this->data->port->Read(dataManaged, bytesRead, - dataChunkSize - bytesRead); - } while (bytesRead < dataChunkSize); - Marshal::Copy(dataManaged, 0, static_cast<IntPtr>(dataChunk), - dataChunkSize); - }, - data, dataLen); - }); - }); -} - -const error_category & Uart::errorCategory() { - static class : public error_category { - public: - virtual const char * name() const { return "dotnet UART"; } - - virtual string message(int condition) const { - switch (condition) { - case NotConnectedError: - return "Not Connected Error"; - - case ArgumentError: - return "Argument Error"; - - case InvalidOperationError: - return "Invalid Operation Error"; - - case IOError: - return "IO Error"; - - case UnauthorizedAccessError: - return "Unauthorized Access Error"; - - default: - return defaultErrorMessage(condition); - } - } - } instance; - return instance; -} - -} // namespace dotnet -} // namespace MaximInterface \ No newline at end of file
--- a/Platforms/dotnet/Uart.hpp Thu Jan 11 13:50:39 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -*******************************************************************************/ - -#pragma once - -#include <memory> -#include <string> -#include <MaximInterface/Links/Uart.hpp> -#include <MaximInterface/Utilities/Export.h> -#include "MoveOnly.hpp" - -namespace MaximInterface { -namespace dotnet { - -/// UART interface using .NET type System::IO::Ports::SerialPort. -class Uart : public MaximInterface::Uart, private MoveOnly { -public: - enum ErrorValue { - NotConnectedError = 1, - ArgumentError, - InvalidOperationError, - IOError, - UnauthorizedAccessError - }; - - MaximInterface_EXPORT Uart(); - MaximInterface_EXPORT ~Uart(); - - MaximInterface_EXPORT Uart(Uart &&) noexcept; - MaximInterface_EXPORT Uart & operator=(Uart &&) noexcept; - - /// Connect a specified COM port. - MaximInterface_EXPORT error_code connect(const std::string & portName); - - /// Disconnect from the current COM port. - MaximInterface_EXPORT void disconnect(); - - /// Check if currently connected to a COM port. - /// @returns True if connected. - MaximInterface_EXPORT bool connected() const; - - /// Get the currently connected COM port name. - MaximInterface_EXPORT std::string portName() const; - - MaximInterface_EXPORT virtual error_code setBaud(int_least32_t baud) override; - MaximInterface_EXPORT virtual error_code sendBreak() override; - MaximInterface_EXPORT virtual error_code clearReadBuffer() override; - MaximInterface_EXPORT virtual error_code - writeByte(uint_least8_t data) override; - MaximInterface_EXPORT virtual error_code - writeBlock(const uint_least8_t * data, size_t dataLen) override; - MaximInterface_EXPORT virtual error_code - readByte(uint_least8_t & data) override; - MaximInterface_EXPORT virtual error_code readBlock(uint_least8_t * data, - size_t dataLen) override; - - MaximInterface_EXPORT static const error_category & errorCategory(); - -private: - struct Data; - std::unique_ptr<Data> data; -}; - -inline error_code make_error_code(Uart::ErrorValue e) { - return error_code(e, Uart::errorCategory()); -} - -} // namespace dotnet -} // namespace MaximInterface \ No newline at end of file