-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPushDisplay.js
More file actions
204 lines (183 loc) · 4.65 KB
/
Copy pathPushDisplay.js
File metadata and controls
204 lines (183 loc) · 4.65 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Push character codes for value bars
var BARS_NON = String.fromCharCode (6);
var BARS_ONE = String.fromCharCode (3);
var BARS_TWO = String.fromCharCode (5);
var BARS_ONE_L = String.fromCharCode (4);
var NON_4 = BARS_NON + BARS_NON + BARS_NON + BARS_NON;
var RIGHT_ARROW = String.fromCharCode (127);
var SPACES =
[
'',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' '
];
var DASHES =
[
'',
BARS_NON,
BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON,
BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON + BARS_NON
];
var SYSEX_CLEAR =
[
"F0 47 7F 15 18 00 45 00 ",
"F0 47 7F 15 19 00 45 00 ",
"F0 47 7F 15 1A 00 45 00 ",
"F0 47 7F 15 1B 00 45 00 "
];
var SYSEX_MESSAGE =
[
"F0 47 7F 15 1C 00 00 F7",
"F0 47 7F 15 1D 00 00 F7",
"F0 47 7F 15 1E 00 00 F7",
"F0 47 7F 15 1F 00 00 F7"
];
PushDisplay.FORMAT_RAW = 0;
PushDisplay.FORMAT_VALUE = 1;
PushDisplay.FORMAT_PAN = 2;
// 4 rows (0-3) with 4 blocks (0-3). Each block consists of
// 17 characters or 2 cells (0-7).
function PushDisplay (output)
{
this.output = output;
this.currentMessage = initArray (null, 4);
this.message = initArray (null, 4);
this.cells = initArray (null, 4 * 8);
}
PushDisplay.prototype.setRow = function (row, str)
{
this.message[row] = str;
return this;
};
PushDisplay.prototype.clearRow = function (row)
{
for (var i = 0; i < 4; i++)
this.clearBlock (row, i);
return this;
};
PushDisplay.prototype.setBlock = function (row, block, value)
{
var cell = 2 * block;
if (value.length > 9)
{
this.cells[row * 8 + cell] = value.substr (0, 9);
this.cells[row * 8 + cell + 1] = this.pad (value.substring (9), 8, ' ');
}
else
{
this.cells[row * 8 + cell] = this.pad (value, 9, ' ');
this.clearCell (row, cell + 1);
}
return this;
};
PushDisplay.prototype.clearBlock = function (row, block)
{
var cell = 2 * block;
this.clearCell (row, cell);
this.clearCell (row, cell + 1);
return this;
};
PushDisplay.prototype.setCell = function (row, cell, value, format)
{
this.cells[row * 8 + cell] = this.pad (this.formatStr (value, format), 8, ' ') + (cell % 2 == 0 ? ' ' : '');
return this;
};
PushDisplay.prototype.clearCell = function (row, cell)
{
this.cells[row * 8 + cell] = cell % 2 == 0 ? ' ' : ' ';
return this;
};
PushDisplay.prototype.done = function (row)
{
var index = row * 8;
this.message[row] = '';
for (var i = 0; i < 8; i++)
this.message[row] += this.cells[index + i];
return this;
};
PushDisplay.prototype.flush = function (row)
{
for (var row = 0; row < 4; row++)
{
if (this.currentMessage[row] == this.message[row])
continue;
this.currentMessage[row] = this.message[row];
if (this.currentMessage[row] == null)
this.output.sendSysex (SYSEX_CLEAR[row]);
else
{
var array = [];
for (var i = 0; i < this.currentMessage[row].length; i++)
array[i] = this.currentMessage[row].charCodeAt(i);
this.output.sendSysex (SYSEX_CLEAR[row] + toHexStr (array) + "F7");
}
}
};
PushDisplay.prototype.formatValue = function (value)
{
var noOfBars = Math.round (16 * value / 128);
var n = '';
for (var j = 0; j < Math.floor (noOfBars / 2); j++)
n += BARS_TWO;
if (noOfBars % 2 == 1)
n += BARS_ONE;
return this.pad (n, 8, BARS_NON);
};
PushDisplay.prototype.formatPan = function (pan)
{
if (pan == 64)
return NON_4 + NON_4;
var isLeft = pan < 64;
var pos = isLeft ? 64 - pan : pan - 64;
var noOfBars = Math.round (16 * pos / 128);
var n = '';
for (var i = 0; i < Math.floor (noOfBars / 2); i++)
n += BARS_TWO;
if (noOfBars % 2 == 1)
n += isLeft ? BARS_ONE_L : BARS_ONE;
n = NON_4 + this.pad (n, 4, BARS_NON);
return isLeft ? this.reverseStr (n) : n;
};
PushDisplay.prototype.pad = function (str, length, character)
{
if (typeof (str) == 'undefined' || str == null)
str = '';
var diff = length - str.length;
if (diff < 0)
return str.substr (0, length);
if (diff > 0)
return str + (character == ' ' ? SPACES[diff] : DASHES[diff]);
return str;
};
PushDisplay.prototype.formatStr = function (value, format)
{
switch (format)
{
case PushDisplay.FORMAT_VALUE:
return this.formatValue (value);
case PushDisplay.FORMAT_PAN:
return this.formatPan (value);
default:
return value;
}
};
PushDisplay.prototype.reverseStr = function (str)
{
var r = '';
for (var i = 0; i < str.length; i++)
r = str[i] + r;
return r;
};