From 04535c92e772e394ff3f3197ec0dc48e3307b20d Mon Sep 17 00:00:00 2001
From: Rene Stange <rsta2@o2online.de>
Date: Sat, 19 Mar 2022 05:44:01 +0100
Subject: [PATCH] pckeyboard: Fake MIDI events

* on MIDI cable 0, channel 0
* instead of calling key[down|up]()
* derive from class CMIDIDevice
---
 src/minidexed.cpp  |  2 +-
 src/pckeyboard.cpp | 21 ++++++---------------
 src/pckeyboard.h   |  8 ++++----
 3 files changed, 11 insertions(+), 20 deletions(-)

diff --git a/src/minidexed.cpp b/src/minidexed.cpp
index c3e0273..3cc44e8 100644
--- a/src/minidexed.cpp
+++ b/src/minidexed.cpp
@@ -37,7 +37,7 @@ CMiniDexed::CMiniDexed (CConfig *pConfig, CInterruptSystem *pInterrupt,
 #endif
 	m_pConfig (pConfig),
 	m_UI (this, pGPIOManager, pConfig),
-	m_PCKeyboard (this),
+	m_PCKeyboard (this, pConfig),
 	m_SerialMIDI (this, pInterrupt, pConfig),
 	m_bUseSerial (false),
 	m_pSoundDevice (0),
diff --git a/src/pckeyboard.cpp b/src/pckeyboard.cpp
index 90a7e9d..a3f6878 100644
--- a/src/pckeyboard.cpp
+++ b/src/pckeyboard.cpp
@@ -18,8 +18,6 @@
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //
 #include "pckeyboard.h"
-#include "minidexed.h"
-#include "config.h"
 #include <circle/devicenameservice.h>
 #include <circle/util.h>
 #include <assert.h>
@@ -62,8 +60,8 @@ static TKeyInfo KeyTable[] =
 
 CPCKeyboard *CPCKeyboard::s_pThis = 0;
 
-CPCKeyboard::CPCKeyboard (CMiniDexed *pSynthesizer)
-:	m_pSynthesizer (pSynthesizer),
+CPCKeyboard::CPCKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig)
+:	CMIDIDevice (pSynthesizer, pConfig),
 	m_pKeyboard (0)
 {
 	s_pThis = this;
@@ -73,8 +71,6 @@ CPCKeyboard::CPCKeyboard (CMiniDexed *pSynthesizer)
 
 CPCKeyboard::~CPCKeyboard (void)
 {
-	m_pSynthesizer = 0;
-
 	s_pThis = 0;
 }
 
@@ -101,7 +97,6 @@ void CPCKeyboard::Process (boolean bPlugAndPlayUpdated)
 void CPCKeyboard::KeyStatusHandlerRaw (unsigned char ucModifiers, const unsigned char RawKeys[6])
 {
 	assert (s_pThis != 0);
-	assert (s_pThis->m_pSynthesizer != 0);
 
 	// report released keys
 	for (unsigned i = 0; i < 6; i++)
@@ -113,10 +108,8 @@ void CPCKeyboard::KeyStatusHandlerRaw (unsigned char ucModifiers, const unsigned
 			u8 ucKeyNumber = GetKeyNumber (ucKeyCode);
 			if (ucKeyNumber != 0)
 			{
-				for (unsigned nTG = 0; nTG < CConfig::ToneGenerators; nTG++)
-				{
-					s_pThis->m_pSynthesizer->keyup (ucKeyNumber, nTG);
-				}
+				u8 NoteOff[] = {0x80, ucKeyNumber, 0};
+				s_pThis->MIDIMessageHandler (NoteOff, sizeof NoteOff);
 			}
 		}
 	}
@@ -131,10 +124,8 @@ void CPCKeyboard::KeyStatusHandlerRaw (unsigned char ucModifiers, const unsigned
 			u8 ucKeyNumber = GetKeyNumber (ucKeyCode);
 			if (ucKeyNumber != 0)
 			{
-				for (unsigned nTG = 0; nTG < CConfig::ToneGenerators; nTG++)
-				{
-					s_pThis->m_pSynthesizer->keydown (ucKeyNumber, 100, nTG);
-				}
+				u8 NoteOn[] = {0x90, ucKeyNumber, 100};
+				s_pThis->MIDIMessageHandler (NoteOn, sizeof NoteOn);
 			}
 		}
 	}
diff --git a/src/pckeyboard.h b/src/pckeyboard.h
index 26fc991..6baf520 100644
--- a/src/pckeyboard.h
+++ b/src/pckeyboard.h
@@ -20,16 +20,18 @@
 #ifndef _pckeyboard_h
 #define _pckeyboard_h
 
+#include "mididevice.h"
+#include "config.h"
 #include <circle/usb/usbkeyboard.h>
 #include <circle/device.h>
 #include <circle/types.h>
 
 class CMiniDexed;
 
-class CPCKeyboard
+class CPCKeyboard : public CMIDIDevice
 {
 public:
-	CPCKeyboard (CMiniDexed *pSynthesizer);
+	CPCKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig);
 	~CPCKeyboard (void);
 
 	void Process (boolean bPlugAndPlayUpdated);
@@ -44,8 +46,6 @@ private:
 	static void DeviceRemovedHandler (CDevice *pDevice, void *pContext);
 
 private:
-	CMiniDexed *m_pSynthesizer;
-
 	CUSBKeyboardDevice * volatile m_pKeyboard;
 
 	u8 m_LastKeys[6];