|
@@ -0,0 +1,107 @@
|
|
|
+/**
|
|
|
+ * MCS Media Computer Software
|
|
|
+ * <one line to give the program's name and a brief idea of what it does.>
|
|
|
+ * Copyright (C) 2018 by Wilfried Klaas
|
|
|
+ * Project: SPSEmulator
|
|
|
+ * File: ConvertJsonData2Hex.java
|
|
|
+ * EMail: W.Klaas@gmx.de
|
|
|
+ * Created: 24.12.2018 wklaa
|
|
|
+ *
|
|
|
+ * This program is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU General Public License
|
|
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
+ */
|
|
|
+package de.mcs.tools.midicontroller;
|
|
|
+
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonParseException;
|
|
|
+import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
+
|
|
|
+import de.mcs.tools.midicontroller.data.ProgramData;
|
|
|
+import de.mcs.tools.sps.utils.IntelHex;
|
|
|
+import de.mcs.utils.JacksonUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wklaa
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ConvertJsonData2Hex {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param args
|
|
|
+ * @throws IOException
|
|
|
+ * @throws JsonMappingException
|
|
|
+ * @throws JsonParseException
|
|
|
+ */
|
|
|
+ public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
|
|
|
+ InputStream source = ClassLoader.getSystemResourceAsStream("programdata.json");
|
|
|
+ ProgramData[] programDatas = JacksonUtils.getJsonMapper().readValue(source, ProgramData[].class);
|
|
|
+
|
|
|
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
|
|
+ for (ProgramData programData : programDatas) {
|
|
|
+ System.out.println(programData.toString());
|
|
|
+ byte[] name = copyInto(getEmptyByteArray(12), getStringAsByte(programData.getName(), 12));
|
|
|
+
|
|
|
+ out.write(name);
|
|
|
+
|
|
|
+ out.write((byte) programData.getPrgNumber());
|
|
|
+
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ byte[] button = getEmptyByteArray(8);
|
|
|
+ if ((programData.getButtons() != null) && (i < programData.getButtons().length)) {
|
|
|
+ button = copyInto(button, getStringAsByte(programData.getButtons()[i].getName(), 8));
|
|
|
+ }
|
|
|
+ out.write(button);
|
|
|
+ }
|
|
|
+ out.write(0xFF);
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ byte[] outBytes = out.toByteArray();
|
|
|
+ System.out.printf("The file has %d bytes.\r\n", outBytes.length);
|
|
|
+ File dest = new File("programData.hex");
|
|
|
+ try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(dest))) {
|
|
|
+ IntelHex intelHex = new IntelHex();
|
|
|
+ intelHex.writeHexStream(output, outBytes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] getStringAsByte(String value, int count) throws UnsupportedEncodingException {
|
|
|
+ String newValue = value;
|
|
|
+ if (newValue.length() > count) {
|
|
|
+ newValue = newValue.substring(0, count);
|
|
|
+ }
|
|
|
+ return newValue.getBytes("US-ASCII");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] getEmptyByteArray(int count) {
|
|
|
+ byte[] value = new byte[count];
|
|
|
+ for (int i = 0; i < value.length; i++) {
|
|
|
+ value[i] = 0;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] copyInto(byte[] dest, byte[] source) {
|
|
|
+ for (int i = 0; i < source.length; i++) {
|
|
|
+ dest[i] = source[i];
|
|
|
+ }
|
|
|
+ return dest;
|
|
|
+ }
|
|
|
+}
|