Skip to content

Commit

Permalink
Merge pull request #87 from vosc/api-18
Browse files Browse the repository at this point in the history
Fixed encoding text that gets send to NI and Mackie controllers for d…
  • Loading branch information
vosc authored Apr 23, 2024
2 parents 7174bac + d59b5d1 commit 81390ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.bitwig.extensions.controllers.mackie.StringUtil;
import com.bitwig.extensions.controllers.mackie.section.SectionType;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -233,9 +234,9 @@ public void sendToDisplay(final DisplaySource source, final int row, final Strin

private void sendFullRow(final int row, final String text) {
rowDisplayBuffer[6] = (byte) (row * LcdDisplay.ROW2_START);
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < displayLen; i++) {
rowDisplayBuffer[i + 7] = i < ca.length ? (byte) ca[i] : 32;
rowDisplayBuffer[i + 7] = i < ca.length ? ca[i] : 32;
}
displayRep.line(row).text().setValue(text);
midiOut.sendSysex(rowDisplayBuffer);
Expand Down Expand Up @@ -263,17 +264,17 @@ public void sendToRowFull(final DisplaySource source, final int row, final int s

private void sendTextSegFull(final DisplaySource source, final int row, final int segment, final String text) {
segBuffer[6] = (byte) (row * LcdDisplay.ROW2_START + segment * segmentLength + segmentOffset);
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < segmentLength; i++) {
segBuffer[i + 7] = i < ca.length ? (byte) ca[i] : 32;
segBuffer[i + 7] = i < ca.length ? ca[i] : 32;
lines[row][segment * 7 + i] = (char) segBuffer[i + 7];
}
midiOut.sendSysex(segBuffer);
displayRep.line(row).text().setValue(String.valueOf(lines[row]));
}

private void sendTextSeg(final DisplaySource source, final int row, final int segment, final String text) {
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
if (segment == 8) {
if (isLowerDisplay()) {
handleLastCell(row, segment, ca);
Expand All @@ -292,10 +293,10 @@ private void sendTextSeg(final DisplaySource source, final int row, final int se
}
}

private void handleLastCell(final int row, final int segment, final char[] ca) {
private void handleLastCell(final int row, final int segment, final byte[] ca) {
segBufferExp[6] = (byte) (row * LcdDisplay.ROW2_START + segment * segmentLength + segmentOffset);
for (int i = 0; i < segmentLength; i++) {
segBufferExp[i + 7] = i < ca.length ? (byte) ca[i] : 32;
segBufferExp[i + 7] = i < ca.length ? ca[i] : 32;
}
if (segment < segmentLength + 1) {
segBufferExp[6 + segmentLength] = ' ';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bitwig.extensions.controllers.mcu.display;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

Expand All @@ -19,7 +20,7 @@
public class LcdDisplay {
private static final int DISPLAY_LEN = 55;
private static final int ROW2_START = 56;

private final byte[] rowDisplayBuffer = { //
(byte) 0XF0, 0, 0, 0X66, 0x14, 0x12, 0, // z: the grid number Zone number 0-3 * 28
32, 32, 32, 0, 0, 0, 0, 0, 0, 0, // 7: 10 Chars
Expand All @@ -29,7 +30,7 @@ public class LcdDisplay {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7: 10 Chars
0, 0, 0, 0, 0, 32, 32, (byte) 247
};

private final byte[] segBuffer;
private final byte[] segBufferExp;
private final DisplayPart part;
Expand All @@ -51,7 +52,7 @@ public class LcdDisplay {
private final boolean hasDedicatedVu;
private final char[][] lines = new char[2][60];
public String sysHead;

public LcdDisplay(final Context context, final int sectionIndex, final MidiOut midiOut, final SectionType type,
final DisplayPart part, final ControllerConfig controllerConfig) {
this.midiOut = midiOut;
Expand All @@ -61,9 +62,9 @@ public LcdDisplay(final Context context, final int sectionIndex, final MidiOut m
final HardwareSurface surface = context.getService(HardwareSurface.class);
final GlobalStates states = context.getService(GlobalStates.class);
displayRep = surface.createHardwareTextDisplay("DISPLAY_SIMU_" + part + "_" + sectionIndex, 2);

//initSimulation(driver, sectionIndex, part);

if (part == DisplayPart.LOWER) {
isLowerDisplay = true;
rowDisplayBuffer[3] = 0X67;
Expand Down Expand Up @@ -100,7 +101,7 @@ public LcdDisplay(final Context context, final int sectionIndex, final MidiOut m
}
}
displayLen = LcdDisplay.DISPLAY_LEN + (part == DisplayPart.LOWER && type != SectionType.XTENDER ? 1 : 0);

if (part == DisplayPart.LOWER) {
segmentLength = 6;
segmentOffset = 2;
Expand All @@ -110,29 +111,29 @@ public LcdDisplay(final Context context, final int sectionIndex, final MidiOut m
}
setVuMode(states.getVuMode().get());
}

// private void initSimulation(final MackieMcuProExtension driver, final int sectionIndex, final DisplayPart
// part) {
// driver.getControllerConfig().getSimulationLayout().layoutDisplay(part, sectionIndex, displayRep);
// for (int i = 0; i < 2; i++) {
// Arrays.fill(lines[i], ' ');
// }
// }

public int getSegmentLength() {
return segmentLength;
}

public boolean isLowerDisplay() {
return isLowerDisplay;
}

public void setFullTextMode(final int row, final boolean fullTextMode) {
this.fullTextMode[row] = fullTextMode;
setDisplayBarGraphEnabled(!isFullModeActive());
refreshDisplay();
}

public void setDisplayBarGraphEnabled(final boolean displayBarGraphEnabled) {
if (this.displayBarGraphEnabled == displayBarGraphEnabled) {
return;
Expand All @@ -146,11 +147,11 @@ public void setDisplayBarGraphEnabled(final boolean displayBarGraphEnabled) {
switchVuMode(VuMode.LED);
}
}

private boolean isFullModeActive() {
return fullTextMode[0] | fullTextMode[1];
}

public void setVuMode(final VuMode mode) {
if (hasDedicatedVu) {
return;
Expand All @@ -161,7 +162,7 @@ public void setVuMode(final VuMode mode) {
refreshDisplay();
}
}

private void switchVuMode(final VuMode mode) {
switch (mode) {
case LED:
Expand All @@ -188,20 +189,20 @@ private void switchVuMode(final VuMode mode) {
break;
}
}

private void resetGrids(final int row) {
Arrays.fill(lastSendGrids[row], " ");
}

public void centerText(final int row, final String text) {
sendToDisplay(row, pad4Center(text));
}

@Override
public String toString() {
return "LcdDisplay " + part;
}

private String pad4Center(final String text) {
final int fill = displayLen - text.length();
if (fill < 0) {
Expand All @@ -212,7 +213,7 @@ private String pad4Center(final String text) {
}
return StringUtil.padString(text, fill / 2);
}

public void sendDirect(final String topString, final String bottomString) {
lastSentRows[0] = topString;
lastSentRows[1] = bottomString;
Expand All @@ -221,14 +222,14 @@ public void sendDirect(final String topString, final String bottomString) {
sendFullRow(0, topString);
sendFullRow(1, bottomString);
}

public void sendSegmented(final int row, final List<String> texts) {
for (int i = 0; i < 8; i++) {
final String text = i < texts.size() ? texts.get(i) : "";
sendToRowFull(row, i, text);
}
}

public void sendToDisplay(final int row, final String text) {
// if (text.equals(lastSentRows[row])) {
// return;
Expand All @@ -237,17 +238,17 @@ public void sendToDisplay(final int row, final String text) {
resetGrids(row);
sendFullRow(row, text);
}

public void sendFullRow(final int row, final String text) {
rowDisplayBuffer[6] = (byte) (row * LcdDisplay.ROW2_START);
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < displayLen; i++) {
rowDisplayBuffer[i + 7] = i < ca.length ? (byte) ca[i] : 32;
rowDisplayBuffer[i + 7] = i < ca.length ? ca[i] : 32;
}
displayRep.line(row).text().setValue(text);
midiOut.sendSysex(rowDisplayBuffer);
}

public void sendToRow(final int row, final int segment, final String text) {
if (row > 1 || row < 0) {
return;
Expand All @@ -257,7 +258,7 @@ public void sendToRow(final int row, final int segment, final String text) {
sendTextSeg(row, segment, text);
}
}

public void sendToRowFull(final int row, final int segment, final String text) {
if (row > 1 || row < 0) {
return;
Expand All @@ -267,20 +268,20 @@ public void sendToRowFull(final int row, final int segment, final String text) {
sendTextSegFull(row, segment, text);
}
}

private void sendTextSegFull(final int row, final int segment, final String text) {
segBuffer[6] = (byte) (row * LcdDisplay.ROW2_START + segment * segmentLength + segmentOffset);
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < segmentLength; i++) {
segBuffer[i + 7] = i < ca.length ? (byte) ca[i] : 32;
segBuffer[i + 7] = i < ca.length ? ca[i] : 32;
lines[row][segment * 7 + i] = (char) segBuffer[i + 7];
}
midiOut.sendSysex(segBuffer);
displayRep.line(row).text().setValue(String.valueOf(lines[row]));
}

private void sendTextSeg(final int row, final int segment, final String text) {
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
if (segment == 8) {
if (isLowerDisplay()) {
handleLastCell(row, segment, ca);
Expand All @@ -298,18 +299,18 @@ private void sendTextSeg(final int row, final int segment, final String text) {
displayRep.line(row).text().setValue(String.valueOf(lines[row]));
}
}
private void handleLastCell(final int row, final int segment, final char[] ca) {

private void handleLastCell(final int row, final int segment, final byte[] ca) {
segBufferExp[6] = (byte) (row * LcdDisplay.ROW2_START + segment * segmentLength + segmentOffset);
for (int i = 0; i < segmentLength; i++) {
segBufferExp[i + 7] = i < ca.length ? (byte) ca[i] : 32;
segBufferExp[i + 7] = i < ca.length ? ca[i] : 32;
}
if (segment < segmentLength + 1) {
segBufferExp[6 + segmentLength] = ' ';
}
midiOut.sendSysex(segBufferExp);
}

public void refreshDisplay() {
for (int row = 0; row < 2; row++) {
if (fullTextMode[row]) {
Expand All @@ -321,27 +322,27 @@ public void refreshDisplay() {
}
}
}

public void sendChar(final int index, final char cx) {
midiOut.sendMidi(Midi.CC, 0x30, cx);
}

public void clearAll() {
midiOut.sendSysex(sysHead + "62 f7");
sendToDisplay(0, "");
sendToDisplay(1, "");
}

public void exitMessage() {
midiOut.sendSysex(sysHead + "62 f7");
centerText(topRowFlipped ? 1 : 0, "Bitwig Studio");
centerText(topRowFlipped ? 0 : 1, "... not running ...");
}

public void clearText() {
sendToDisplay(0, "");
sendToDisplay(1, "");
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bitwig.extensions.controllers.nativeinstruments.komplete.midi;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import com.bitwig.extension.controller.api.MidiOut;

/**
Expand Down Expand Up @@ -34,8 +37,9 @@ private void send(final MidiOut midiOut, final int value, final int track, final
dataArray[12] = (byte) track;
final byte[] sendarray = new byte[dataArray.length + text.length()];
System.arraycopy(dataArray, 0, sendarray, 0, 13);
for (int i = 0; i < text.length(); i++) {
sendarray[13 + i] = (byte) text.charAt(i);
final byte[] chars = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < chars.length; i++) {
sendarray[13 + i] = chars[i];
}
sendarray[sendarray.length - 1] = SYSEX_END;
midiOut.sendSysex(sendarray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.bitwig.extensions.framework.time.TimedEvent;
import com.bitwig.extensions.framework.values.BooleanValueObject;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -961,9 +962,9 @@ public void sendToDisplay(final int grid, final String text) {
}
lastSenGrids[grid] = text;
displayBuffer[6] = (byte) (Math.min(grid, 3) * 28);
final char[] ca = text.toCharArray();
final byte[] ca = text.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < 28; i++) {
displayBuffer[i + 7] = i < ca.length ? (byte) ca[i] : 32;
displayBuffer[i + 7] = i < ca.length ? ca[i] : 32;
}
midiOut.sendSysex(displayBuffer);
}
Expand Down

0 comments on commit 81390ba

Please sign in to comment.