mt-32-display.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 19 May 2020 17:31:26 +0200
branchv_0
changeset 3 51a8362261a9
parent 2 a84830179027
permissions -rw-r--r--
SysEx documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     1
/**
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     2
 * SysEx message encoder for Roland MT-32
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     4
 *
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     8
 *
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    12
 * GNU General Public License for more details.
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    13
 *
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    14
 * You should have received a copy of the GNU General Public License
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6733bd832b61 license: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    16
 */
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <iostream>
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <iomanip>
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
/**
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
 * Translates text from standard input to a hex-formatted System Exclusive (SysEx) message for Roland MT-32,
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
 * which instructs the unit to show given text on the display.
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
 * 
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
 * Roland MT-32 is capable to display 20 characters.
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
 * Longer messages are silently truncated by the MT-32 unit (this software does not check the length).
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
 * 
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 * The SysEx message contains a checksum.
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 * If the checksum is wrong, the MT-32 unit shows the "Exc. Checksum error" message for few seconds
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * and then returns back to the default screen.
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 * 
2
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    31
 * Only printable ASCII characters are supported. Other characters (bytes) are replaced by "."
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    32
 * 
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    33
 * Some characters are displayed differently:
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    34
 *   ASCII   MT-32
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    35
 *       ~   →
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    36
 *       \   ¥
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    37
 * 
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    38
 * Usage examples:
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 *     amidi --port="hw:2,0,0" --send-hex="$(echo -n '   Run GNU/Linux    ' | ./mt-32-display )"
2
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    40
 *     while read message; do amidi --port="hw:2,0,0" --send-hex="$(echo -n "$message" | ./mt-32-display )"; done
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
 * 
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
 * @param argc
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
 * @param argv
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
 * @return 
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
 */
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
int main(int argc, char**argv) {
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::cout << "f0 41 10 16 12 20 00 00 ";
3
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
	// f0 = start of SysEx message
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
	// 41 = Roland manufacturer ID, see https://www.midi.org/specifications-old/item/manufacturer-id-numbers
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
	// 10 = device ID
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
	// 16 = model ID
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
	// 12 = command ID
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
	// 20 00 00 = show message on the display
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
	// The start and end of the SysEx message and the manufacturer ID are part of the MIDI standard.
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
	// The rest of the bytes is Roland specific.
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
	// TODO: allow changing device ID to support controlling multiple devices in daisy-chain
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
	// TODO: allow custom replacement character ('.')
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	std::cout << std::hex << std::setfill('0');
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	int sum = 0;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	// 20 00 00 = display message
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	sum += 0x20;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	sum += 0x00;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	sum += 0x00;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	for (char ch; std::cin.read(&ch, 1).good();) {
2
a84830179027 filter ASCII characters
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
		if (ch < 32 || ch > 126) ch = '.';
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		std::cout << std::setw(2) << ((int) ch) << " ";
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		sum += ch;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	sum %= 128;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	sum = 128 - sum;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	std::cout << std::setw(2) << sum;
3
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	std::cout << " f7";
3
51a8362261a9 SysEx documentation
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    81
	// f7 = end of SysEx message
0
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	std::cout << std::endl;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	return 0;
a7af46af7903 first working version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
}