From 3495c03d65784ac646f16ea8e9210b6d33a9e8ac Mon Sep 17 00:00:00 2001 From: dgis Date: Wed, 12 Jun 2019 00:27:37 +0200 Subject: [PATCH] Add the printer simulator draft. --- app/src/main/cpp/android-layer.c | 4 +- app/src/main/cpp/emu-jni.c | 11 ++ app/src/main/cpp/win32-layer.h | 1 + .../emulator/forty/eight/MainActivity.java | 19 +++ .../forty/eight/PrinterSimulator.java | 147 ++++++++++++++++++ .../main/res/menu/activity_main_drawer.xml | 4 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/pref_general.xml | 2 +- 8 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java diff --git a/app/src/main/cpp/android-layer.c b/app/src/main/cpp/android-layer.c index f076ca9..48d97e1 100644 --- a/app/src/main/cpp/android-layer.c +++ b/app/src/main/cpp/android-layer.c @@ -28,9 +28,7 @@ VOID ResetUdp(VOID) { } BOOL SendByteUdp(BYTE byData) { - - LOGD("SendByteUdp(%d -> 0x%02x -> '%c')", byData, byData, byData); - + sendByteUdp(byData); return FALSE; } diff --git a/app/src/main/cpp/emu-jni.c b/app/src/main/cpp/emu-jni.c index 265558d..1e52baf 100644 --- a/app/src/main/cpp/emu-jni.c +++ b/app/src/main/cpp/emu-jni.c @@ -253,6 +253,17 @@ void performHapticFeedback() { } } +void sendByteUdp(unsigned char byteSent) { + JNIEnv *jniEnv = getJNIEnvironment(); + if(jniEnv) { + jclass mainActivityClass = (*jniEnv)->GetObjectClass(jniEnv, mainActivity); + if(mainActivityClass) { + jmethodID midStr = (*jniEnv)->GetMethodID(jniEnv, mainActivityClass, "sendByteUdp", "(I)V"); + (*jniEnv)->CallVoidMethod(jniEnv, mainActivity, midStr, (int)byteSent); + (*jniEnv)->DeleteLocalRef(jniEnv, mainActivityClass); + } + } +} JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_start(JNIEnv *env, jobject thisz, jobject assetMgr, jobject bitmapMainScreen0, jobject activity, jobject view) { diff --git a/app/src/main/cpp/win32-layer.h b/app/src/main/cpp/win32-layer.h index 5ffeec6..2a97d2a 100644 --- a/app/src/main/cpp/win32-layer.h +++ b/app/src/main/cpp/win32-layer.h @@ -1255,5 +1255,6 @@ BOOL getFirstKMLFilenameForType(BYTE chipsetType, TCHAR * firstKMLFilename, size void clipboardCopyText(const TCHAR * text); const TCHAR * clipboardPasteText(); void performHapticFeedback(); +void sendByteUdp(unsigned char byteSent); typedef int SOCKET; diff --git a/app/src/main/java/org/emulator/forty/eight/MainActivity.java b/app/src/main/java/org/emulator/forty/eight/MainActivity.java index b2505bb..e8fc06f 100644 --- a/app/src/main/java/org/emulator/forty/eight/MainActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/MainActivity.java @@ -100,6 +100,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On } }; + private PrinterSimulator printer = new PrinterSimulator(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -325,6 +327,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On OnBackupDelete(); } else if (id == R.id.nav_change_kml_script) { OnViewScript(); + } else if (id == R.id.nav_show_printer) { + OnViewPrinter(); } else if (id == R.id.nav_create_ram_card) { OnCreateRAMCard(); } else if (id == R.id.nav_help) { @@ -783,6 +787,17 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On showKMLPicker(true); } + private void OnViewPrinter() { + String printerText = printer.getText(); + new AlertDialog.Builder(this) + .setTitle(getString(R.string.message_printer)) + .setMessage(printerText) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + } + }).show(); + } + private void showKMLPicker(final boolean changeKML) { extractKMLScripts(); @@ -1327,6 +1342,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mainScreenView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } + void sendByteUdp(int byteSent) { + printer.write(byteSent); + } + private void setPort1Settings(boolean port1Plugged, boolean port1Writable) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("settings_port1en", port1Plugged); diff --git a/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java b/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java new file mode 100644 index 0000000..5ddb6d9 --- /dev/null +++ b/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java @@ -0,0 +1,147 @@ +package org.emulator.forty.eight; + +import java.util.ArrayList; + +/* + * Based on the free HP82240B Printer Simulator by Christoph Giesselink + */ +public class PrinterSimulator { + + private ArrayList data = new ArrayList<>(); + private StringBuilder m_Text = new StringBuilder(""); + + + private final int ESC = 27; // ESC + + private final byte PM_HP82240A = 0; + private final byte PM_HP82240B = 1; + private final byte PM_GenericSerial = 2; + + private byte m_ePrinterModel;// selected printer model + private boolean m_bExpChar; // printing expanded characters + private boolean m_bUnderLined; // printing underlined characters + private boolean m_bEcma94; // Roman 8 / ECMA 94 character set + + private boolean m_bEsc; // not ESC sequence + private int m_byGraphLength; // remaining no. of graphic bytes + + public PrinterSimulator() { + m_ePrinterModel = PM_HP82240B; // HP82240B printer + + reset(); // reset printer state machine + } + + // + // reset printer state machine + // + void reset() { + m_bExpChar = false; // printing normal-width characters + m_bUnderLined = false; // printing non underlined characters + + // Roman 8 character set for HP82240A and HP82240B, ECMA 94 for serial printer type + m_bEcma94 = (m_ePrinterModel == PM_GenericSerial); + + m_bEsc = false; // not ESC sequence + m_byGraphLength = 0; // no remaining graphic bytes + } + + // + // printer selftest + // + void selftest() { + // self test normally run in an endless loop, that's very hard to implement, + // so this implementation printing all chracters only one time and then + // go back to the communication interface + + int i,nLF; + + reset(); // reset printer state machine + + nLF = 1; + write((byte)4); // begin with special LF + write((byte)95); // '_' instead if ' ' as first character + for (i = 33; i <= 256; ++i) { // all ROMAN8 characters + if (nLF == 0) write((byte)4); // LF after 24 characters printing + write((byte)i); // print character + nLF = ++nLF % 24; + } + + // post code, identification number: HP82240A = D, HP82240B = G + write((byte)(m_ePrinterModel == PM_HP82240A ? 'D' : 'G')); + + // rest of post code is battery state full + String szPostPrt = "\u0004\u0004BAT: 5\u0004\u0004"; + + for (int ci = 0; ci < szPostPrt.length(); ci++) { // write post print + char c = szPostPrt.charAt(ci); + write((byte)c); + } + } + + public synchronized void write(int byData) { + data.add(byData); + + do { + // check for begin of ESC sequence + if (byData == ESC && !m_bEsc && m_byGraphLength == 0) { + m_bEsc = true; // ESC sequence mode + break; + } + + if (m_bEsc) { // byte is ESC command + switch (byData) { + case 255: // reset + reset(); + break; + case 254: // start self test + selftest(); + break; + case 253: // start printing expanded characters + m_bExpChar = true; + break; + case 252: // return to normal-width characters + m_bExpChar = false; + break; + case 251: // start printing underlined characters + m_bUnderLined = true; + break; + case 250: // stop underlining + m_bUnderLined = false; + break; + case 249: // ECMA 94 character set + if (m_ePrinterModel == PM_HP82240B) + m_bEcma94 = true; + break; + case 248: // Roman 8 character set + if (m_ePrinterModel == PM_HP82240B) + m_bEcma94 = false; + break; + default: + // graphic data + if (byData >= 1 && byData <= 166) { + // remaining graphic bytes + m_byGraphLength = byData; + } + } + m_bEsc = false; + break; + } + + // normal character + if (m_byGraphLength == 0) { // not a graphic character + m_Text.append((char)byData); // output to text window + } + + // output to graphic window + //m_Graph.WriteChar(byData,m_byGraphLength > 0); + + if (m_byGraphLength > 0) { // in graphic mode + --m_byGraphLength; // graphic character printed + } + } while (false); + } + + String getText() { + return m_Text.toString(); + } +} diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 519df2c..65c8de2 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -88,6 +88,10 @@ android:id="@+id/nav_change_kml_script" android:icon="@drawable/ic_wallpaper_black_24dp" android:title="@string/nav_change_kml_script" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a55829..ef72a89 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,6 +40,7 @@ Delete Backup View Change KML Script... + Show the Printer Simulator... Tool Create RAM Card... Help @@ -67,6 +68,7 @@ KML Script Compilation Result Need Android 5.0 To select the KML folder, you need at least Android version 5.0 + Printer Simulator General Authentic Calculator Speed diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 003750b..1184259 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -141,7 +141,7 @@ -