File: SerialMidi.h

package info (click to toggle)
libexadrums 0.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: cpp: 6,970; ansic: 220; makefile: 160; sh: 12
file content (172 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef LIBEXADRUMS_IO_SERIALMIDI_H
#define LIBEXADRUMS_IO_SERIALMIDI_H

#include <array>
#include <optional>
#include <ranges>
#include <string>

#include <cstdio>      // Standard input / output functions
#include <cstdlib>
#include <cstring>     // String function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <cerrno>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions

namespace IO
{

    static constexpr auto nbBytesPerMessage = 3;
    using MidiBytes_t = std::array<uint8_t, nbBytesPerMessage>;

    struct MidiMessage
    {
        uint8_t command{};
        uint8_t channel{};
        uint8_t param1{};
        uint8_t param2{};

        MidiBytes_t ToBytes() const
        {
            return {static_cast<uint8_t>((command & 0xF0) | (channel & 0x0F)), param1, param2};
        }

        static MidiMessage FromBytes(const MidiBytes_t& bytes)
        {
            MidiMessage message{};   
            message.command = bytes[0] & 0xF0;
            message.channel = bytes[0] & 0x0F;
            message.param1 = bytes[1];
            message.param2 = bytes[2];

            return message;
        }
    };

    class SerialMidi
    {

    public:

        SerialMidi() = default;
        ~SerialMidi() noexcept
        {
            Close();
        }

        void SetPort(const std::string& serialPort) noexcept
        {
            port = serialPort;
        }

        void SetBaudRate(std::size_t br) noexcept
        {
            baudRate = br;
        }

        bool Open()
        {
            handle = ::open(port.data(), O_RDWR);

            termios tty;

            if(tcgetattr(handle, &tty) != 0)
            {
                //error
                return false;
            }

            // Set baud rate
            cfsetospeed(&tty, GetSpeedTFromBaudRate(baudRate));
            cfsetispeed(&tty, GetSpeedTFromBaudRate(baudRate));

            tty.c_cflag &= 	~PARENB;            // Make 8n1
            tty.c_cflag &= 	~CSTOPB;
            tty.c_cflag &= 	~CSIZE;
            tty.c_cflag |= 	CS8;

            tty.c_cflag &= 	~CRTSCTS;           // no flow control
            tty.c_cc[VMIN] = 0;                 // read blocks
            tty.c_cc[VTIME] = 10;               // 1 second read timeout
            tty.c_cflag |=	CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

            // Raw mode
		    cfmakeraw(&tty);

            // Flush Port, then applies attributes
            tcflush(this->handle, TCIFLUSH);
    
            if(tcsetattr (this->handle, TCSANOW, &tty) != 0)
            {
                return false;
            }

            isOpen = true;

            return isOpen;
        }

        void Close()
        {
            if(isOpen)
            {
                ::close(handle);
            }
        }

        uint8_t ReadByte() const
        {
            uint8_t byte{};

            ::read(handle, &byte, sizeof byte);
            
            return byte;        
        }

        std::optional<MidiMessage> GetMessage() const
        {

            MidiBytes_t midiBytes;

            for(auto i : std::views::iota(0, nbBytesPerMessage))
            {
                const auto byte = ReadByte();
                const auto isStatusByte =  byte >> 7 != 0;

                if(!isStatusByte && i == 0)
                {
                    return {};
                }

                midiBytes[i] = byte;
            }

            return MidiMessage::FromBytes(midiBytes);
        }

        auto GetIsOpen() const noexcept { return isOpen; }

    private:

        static speed_t GetSpeedTFromBaudRate(std::size_t baudRate)
        {
            switch(baudRate)
            {
            case 115'200: return static_cast<speed_t>(B115200);
            
            default: return static_cast<speed_t>(B0);
            }
        }

        std::string port{};
        std::size_t baudRate{};
        int handle{};
        bool isOpen{false};

    };
    
} // namespace IO


#endif /* LIBEXADRUMS_IO_SERIALMIDI_H */