Skip to content

Commit

Permalink
Serial: Small organizing
Browse files Browse the repository at this point in the history
Put the serial interface class into the separated head
  • Loading branch information
Wohlstand committed May 6, 2024
1 parent 71b6ea2 commit 71e85f2
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 150 deletions.
1 change: 1 addition & 0 deletions src/opl/chips/chipset.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 58,6 @@ if(ENABLE_SERIAL_PORT)
list(APPEND CHIPS_SOURCES
"src/opl/chips/opl_serial_port.cpp"
"src/opl/chips/opl_serial_port.h"
"src/opl/chips/opl_serial_misc.h"
)
endif()
171 changes: 171 additions & 0 deletions src/opl/chips/opl_serial_misc.h
Original file line number Diff line number Diff line change
@@ -0,0 1,171 @@
/*
* Interfaces over Yamaha OPL3 (YMF262) chip emulators
*
* Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef __unix__
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#endif


class ChipSerialPortBase
{
protected:
std::string m_portName;

public:
ChipSerialPortBase() {}
virtual ~ChipSerialPortBase() {}

bool isOpen()
{
return false;
}

void close() {}

bool open(const std::string &/*portName*/, unsigned /*baudRate*/)
{
return false;
}

int write(uint8_t */*data*/, size_t /*size*/)
{
return 0;
}
};



#ifdef __unix__
class ChipSerialPort : public ChipSerialPortBase
{
int m_port;
struct termios m_portSetup;

static unsigned int baud2enum(unsigned int baud)
{
if(baud == 0)
return B0;
else if(baud <= 50)
return B50;
else if(baud <= 75)
return B75;
else if(baud <= 110)
return B110;
else if(baud <= 134)
return B134;
else if(baud <= 150)
return B150;
else if(baud <= 200)
return B200;
else if(baud <= 300)
return B300;
else if(baud <= 600)
return B600;
else if(baud <= 1200)
return B1200;
else if(baud <= 1800)
return B1800;
else if(baud <= 2400)
return B2400;
else if(baud <= 4800)
return B4800;
else if(baud <= 9600)
return B9600;
else if(baud <= 19200)
return B19200;
else if(baud <= 38400)
return B38400;
else if(baud <= 57600)
return B57600;
else if(baud <= 115200)
return B115200;
else
return B230400;
}

public:
ChipSerialPort() : ChipSerialPortBase()
{
m_port = 0;
memset(&m_portSetup, 0, sizeof(struct termios));
}

virtual ~ChipSerialPort()
{
close();
}

bool isOpen()
{
return m_port != 0;
}

void close()
{
if(m_port)
::close(m_port);

m_port = 0;
}

bool open(const std::string &portName, unsigned baudRate)
{
if(m_port)
this->close();

std::string portPath = "/dev/" portName;
m_port = ::open(portPath.c_str(), O_WRONLY);

if(m_port < 0)
{
m_port = 0;
return false;
}

if(tcgetattr(m_port, &m_portSetup) != 0)
{
close();
return false;
}

cfsetospeed(&m_portSetup, baud2enum(baudRate));

if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0)
{
close();
return false;
}

return true;
}

int write(uint8_t *data, size_t size)
{
if(!m_port)
return 0;

return ::write(m_port, data, size);
}
};

#endif // __unix__
152 changes: 14 additions & 138 deletions src/opl/chips/opl_serial_port.cpp
Original file line number Diff line number Diff line change
@@ -1,153 1,29 @@
/*
* OPL Bank Editor by Wohlstand, a free tool for music bank editing
* Copyright (c) 2016-2023 Vitaly Novichkov <[email protected]>
* Interfaces over Yamaha OPL3 (YMF262) chip emulators
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand)
*
* This program is distributed in the hope that it will be useful,
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "opl_serial_port.h"

#ifdef ENABLE_HW_OPL_SERIAL_PORT

#include <QSerialPort>

#ifdef __linux__
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#endif


class ChipSerialPort
{
std::string m_portName;

#ifdef __unix__
int m_port;
struct termios m_portSetup;

static unsigned int baud2enum(unsigned int baud)
{
if(baud == 0)
return B0;
else if(baud <= 50)
return B50;
else if(baud <= 75)
return B75;
else if(baud <= 110)
return B110;
else if(baud <= 134)
return B134;
else if(baud <= 150)
return B150;
else if(baud <= 200)
return B200;
else if(baud <= 300)
return B300;
else if(baud <= 600)
return B600;
else if(baud <= 1200)
return B1200;
else if(baud <= 1800)
return B1800;
else if(baud <= 2400)
return B2400;
else if(baud <= 4800)
return B4800;
else if(baud <= 9600)
return B9600;
else if(baud <= 19200)
return B19200;
else if(baud <= 38400)
return B38400;
else if(baud <= 57600)
return B57600;
else if(baud <= 115200)
return B115200;
else
return B230400;
}
#endif

public:
ChipSerialPort()
{
#ifdef __linux__
m_port = 0;
memset(&m_portSetup, 0, sizeof(struct termios));
#endif
}

~ChipSerialPort()
{
close();
}

bool isOpen()
{
return m_port != 0;
}

void close()
{
if(m_port)
::close(m_port);

m_port = 0;
}

bool open(const std::string &portName, unsigned baudRate)
{
if(m_port)
this->close();

std::string portPath = "/dev/" portName;
m_port = ::open(portPath.c_str(), O_WRONLY);

if(m_port < 0)
{
m_port = 0;
return false;
}

if(tcgetattr(m_port, &m_portSetup) != 0)
{
close();
return false;
}

cfsetospeed(&m_portSetup, baud2enum(baudRate));

if(tcsetattr(m_port, TCSANOW, &m_portSetup) != 0)
{
close();
return false;
}

return true;
}

int write(uint8_t *data, size_t size)
{
if(!m_port)
return 0;

return ::write(m_port, data, size);
}
};

#include "opl_serial_misc.h"


static size_t retrowave_protocol_serial_pack(const uint8_t *buf_in, size_t len_in, uint8_t *buf_out)
Expand Down
25 changes: 14 additions & 11 deletions src/opl/chips/opl_serial_port.h
Original file line number Diff line number Diff line change
@@ -1,21 1,24 @@
/*
* OPL Bank Editor by Wohlstand, a free tool for music bank editing
* Copyright (c) 2016-2023 Vitaly Novichkov <[email protected]>
* Interfaces over Yamaha OPL3 (YMF262) chip emulators
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* Copyright (c) 2017-2023 Vitaly Novichkov (Wohlstand)
*
* This program is distributed in the hope that it will be useful,
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#ifndef OPL_SERIAL_PORT_H
#define OPL_SERIAL_PORT_H

Expand Down
2 changes: 1 addition & 1 deletion src/opl/chips/opl_serial_port.pri
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
QT = serialport

HEADERS = $$PWD/opl_serial_port.h
HEADERS = $$PWD/opl_serial_port.h $$PWD/opl_serial_misc.h
SOURCES = $$PWD/opl_serial_port.cpp

0 comments on commit 71e85f2

Please sign in to comment.