1 /** 
2  * Copyright: Enalye
3  * License: Zlib
4  * Authors: Enalye
5  */
6 module minuit.util.output;
7 
8 import std.exception;
9 import std.string;
10 import std.stdio;
11 import std.conv;
12 
13 import minuit.device;
14 import minuit.util.misc;
15 
16 final class MnOutput {
17 	private {
18 		MnOutputPort _port;
19 		MnOutputHandle _handle;
20 		bool _isOpen = false;
21 	}
22 
23 	@property {
24 		bool isOpen() const { return _isOpen; }
25 		
26 		MnOutputPort port() { return _port; }
27 		MnOutputPort port(MnOutputPort newPort) { return _port = newPort; }
28 
29 		string name() const { return _port ? _port.name : ""; }
30 	}
31 
32 	this() {}
33 
34 	this(MnOutputPort newPort) {
35 		_port = newPort;
36 	}
37 
38 	~this() {
39 		close();
40 	}
41 
42 	bool open(uint num = 0u) {
43 		if(_isOpen) {
44 			close();
45 			_isOpen = false;
46 		}
47 
48 		_handle = mnOpenOutput(num);
49 		if(_handle) {
50 			_isOpen = true;
51 		}
52 		return _isOpen;
53 	}
54 
55 	bool open(string name) {
56 		if(_isOpen) {
57 			close();
58 			_isOpen = false;
59 		}
60 
61 		_handle = mnOpenOutput(name);
62 		if(_handle) {
63 			_isOpen = true;
64 		}
65 		return _isOpen;
66 	}
67 
68 	bool open(MnOutputPort port) {
69 		if(_isOpen) {
70 			close();
71 			_isOpen = false;
72 		}
73 
74 		_port = port;
75 		_handle = mnOpenOutput(port);
76 		if(_handle) {
77 			_isOpen = true;
78 		}
79 		return _isOpen;
80 	}
81 
82 	bool close() {
83 		if(_isOpen) {
84 			mnCloseOutput(_handle);
85 			_isOpen = false;
86 		}
87 		return !_isOpen;
88 	}
89 
90 	void send(ubyte a) {
91 		if(!_isOpen)
92 			return;
93 		mnSendOutput(_handle, a);
94 	}
95 
96 	void send(ubyte a, ubyte b) {
97 		if(!_isOpen)
98 			return;
99 		mnSendOutput(_handle, a, b);
100 	}
101 
102 	void send(ubyte a, ubyte b, ubyte c) {
103 		if(!_isOpen)
104 			return;
105 		mnSendOutput(_handle, a, b, c);
106 	}
107 
108 	void send(ubyte a, ubyte b, ubyte c, ubyte d) {
109 		if(!_isOpen)
110 			return;
111 		mnSendOutput(_handle, a, b, c, d);
112 	}
113 
114 	void send(const(ubyte)[] data) {
115 		if(!_isOpen)
116 			return;
117 		mnSendOutput(_handle, data);
118 	}
119 }