ofxPDSP
SerialOut.h
1 
2 // SerialOut.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016
5 
6 #ifndef OFXPDSPMIDI_PDSPSERIALOUT_H_INCLUDED
7 #define OFXPDSPMIDI_PDSPSERIALOUT_H_INCLUDED
8 
9 #include "ofMain.h"
10 
11 #ifndef TARGET_OF_IOS
12 #ifndef __ANDROID__
13 
14 #include <chrono>
15 #include <algorithm>
16 #include <thread>
17 #include <mutex>
18 #include <condition_variable>
19 #include <atomic>
20 #include "../DSP/pdspCore.h"
21 #include "../sequencer/SequencerSection.h"
22 
27 namespace pdsp{ namespace serial {
28 
29 class Output : public pdsp::ExtSequencer, public pdsp::Preparable {
30 
31 private:
32 
38  class ScheduledSerialMessage{
39  public:
40  ScheduledSerialMessage();
41  ScheduledSerialMessage(int channel, float message, chrono::high_resolution_clock::time_point schedule);
42  ScheduledSerialMessage(const ScheduledSerialMessage &other);
43  ScheduledSerialMessage& operator= (const ScheduledSerialMessage &other);
44  ~ScheduledSerialMessage();
45 
46  signed char channel;
47  signed char message;
48  chrono::high_resolution_clock::time_point scheduledTime;
49  };
50 
51 
52  static bool scheduledSort(const ScheduledSerialMessage &lhs, const ScheduledSerialMessage &rhs );
53 
59 public:
60  Output();
61  ~Output();
62 
68  void openPort(int index, int baudRate=250000);
69 
75  void openPort(string name, int baudRate=250000);
76 
80  void close() override;
81 
85  void listPorts();
86 
90  bool isConnected() { return connected; }
91 
96  void setVerbose( bool verbose );
97 
98 
103  pdsp::ExtSequencer& channel(int channelNumber);
104 
108  void process( int bufferSize ) noexcept override;
109 
110  void linkToMessageBuffer(pdsp::MessageBuffer &messageBuffer) override;
111  void unlinkMessageBuffer(pdsp::MessageBuffer &messageBuffer) override;
115 protected:
116  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
117  void releaseResources() override ;
118 
119 private:
120 
121  ofSerial serial;
122 
123  bool connected;
124  bool verbose;
125 
126  std::vector<ScheduledSerialMessage> messagesToSend;
127 
128  std:: vector<pdsp::MessageBuffer*> inputs;
129  std::vector<int> channels;
130 
131  int selectedChannel;
132  int messageCount;
133 
134 
135  //MIDI DAEMON MEMBERS---------------------------------------------------------------
136  void startDaemon();
137  void closeDaemon();
138  void daemonFunction() noexcept;
139  static void daemonFunctionWrapper(Output* parent);
140 
141  std::thread daemonThread;
142  std::atomic<bool> runDaemon;
143 
144  //serial output processing members
145  std::chrono::time_point<chrono::high_resolution_clock> bufferChrono;
146  bool chronoStarted;
147  double usecPerSample;
148  std::vector<ScheduledSerialMessage> circularBuffer;
149 
150  std::atomic<int> writeindex;
151  int send;
152 
153 };
154 
155 }} // end namespaces
156 
157 #endif // __ANDROID__
158 #endif // TARGET_OF_IOS
159 
160 #endif //OFXPDSPMIDI_PDSPSERIALOUT_H_INCLUDED
pdsp::ExtSequencer & channel(int channelNumber)
patch a pdsp::ScoreSection::out_message() method to the result of this method for message to the seri...
Definition: SerialOut.cpp:137
void setVerbose(bool verbose)
enable or disable diagnostic messages
Definition: SerialOut.cpp:80
Definition: SerialOut.h:29
void openPort(int index, int baudRate=250000)
open the port with the given index
Definition: SerialOut.cpp:88
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
void close() override
close the opened port
Definition: SerialOut.cpp:125
bool isConnected()
return true if the port has been sucessfully opened
Definition: SerialOut.h:90
Definition: Preparable.h:36
void listPorts()
list the available ports
Definition: SerialOut.cpp:84