--- nono/vm/lunakbd.cpp 2026/04/29 17:05:06 1.1.1.12 +++ nono/vm/lunakbd.cpp 2026/04/29 17:05:33 1.1.1.16 @@ -4,13 +4,20 @@ // Licensed under nono-license.txt // -// キーボードとマウス // +// LUNA のキーボードとマウス +// + // キーボード/マウスからシリアルポートへの入力は、1バイトの送信自体に約1msec // (9600bps、スタートビット1、ストップビット1) で送信後に 1msec 空けるそう // なので、ここでは単純に 2msec を1スロットとする。 +// +// ブザー発声はキー入力とは本来あまり関係ないが、ブザー発声中もキー入力中と +// 同様に高速走行を抑制したいため、キー扱いとしていることに留意。 #include "lunakbd.h" +#include "monitor.h" +#include "scheduler.h" #include "sio.h" #include "uimessage.h" @@ -19,12 +26,9 @@ LunaKeyboard::LunaKeyboard() { led.resize(2); - event.func = (DeviceCallback_t)&LunaKeyboard::Callback; - event.SetName("Keyboard/Mouse"); - - monitor.func = (MonitorCallback_t)&LunaKeyboard::MonitorUpdate; - monitor.SetSize(40, 4); - monitor.Regist(ID_MONITOR_KEYBOARD); + monitor = gMonitorManager->Regist(ID_MONITOR_KEYBOARD, this); + monitor->func = ToMonitorCallback(&LunaKeyboard::MonitorUpdate); + monitor->SetSize(40, 4); } // デストラクタ @@ -32,12 +36,29 @@ LunaKeyboard::~LunaKeyboard() { } -// 電源オン +// 初期化 bool -LunaKeyboard::PowerOn() +LunaKeyboard::Init() +{ + if (inherited::Init() == false) { + return false; + } + + sio = GetSIODevice(); + + event.func = ToEventCallback(&LunaKeyboard::Callback); + event.SetName("Keyboard/Mouse"); + scheduler->RegistEvent(event); + + return true; +} + +// キーボード接続時の MD 固有処理 +void +LunaKeyboard::MDConnect() { // XXX リセットはマウスには届かないような気がするので - // 電源オンで初期化。 + // 接続時(電源オン)で初期化。 mouse_x = 0; mouse_y = 0; mouse_r = false; @@ -47,23 +68,25 @@ LunaKeyboard::PowerOn() prev_l = false; prev_m = false; - // ブザー停止 - buzzer_on = false; + // ブザー停止状態で初期化すると定義されている + BreakKey(KC_buzzer); - return true; + // デフォルトでマウス有効 + MouseOn(); } -// リセット +// キーボード取り外し時の MD 固有処理 void -LunaKeyboard::ResetHard() +LunaKeyboard::MDDisconnect() { - inherited::ResetHard(); + // 動作中のタイマーは停止 + scheduler->StopEvent(event); - // 動作中のタイマーは一旦とめる - event.Stop(); + // 電源が切れるのでブザー停止 + BreakKey(KC_buzzer); - // マウスサンプリングは電源オン時から有効 - MouseOn(); + // マウスサンプリングは停止 + MouseOff(); } // モニター @@ -88,7 +111,7 @@ LunaKeyboard::MonitorUpdate(Monitor *, T screen.Puts(x + 4, y, TA::OnOff(led[0]), "Kana"); y++; screen.Puts(0, y, "Buzzer:"); - screen.Print(x, y, buzzer_on ? TA::Normal : TA::Disable, + screen.Print(x, y, IsPressed(KC_buzzer) ? TA::Normal : TA::Disable, "%umsec %uHz", buzzer_msec, buzzer_freq); y++; @@ -244,12 +267,12 @@ LunaKeyboard::SendStart() void LunaKeyboard::Start(uint64 period) { - uint64 now = gMPU->GetVirtTime(); + uint64 now = scheduler->GetVirtTime(); uint64 target = ((now / period) + 1) * period; // XXX メンバの event.vtime 参照してるけどどうするかね if (event.IsRunning() == false || target < event.vtime) { event.time = target - now; - event.Start(); + scheduler->RestartEvent(event); } } @@ -259,7 +282,7 @@ LunaKeyboard::Start(uint64 period) void LunaKeyboard::Callback(Event& ev) { - uint64 now = gMPU->GetVirtTime(); + uint64 now = scheduler->GetVirtTime(); uint mdkey; // マウスサンプリングオンなら 20msec ごとにサンプリングを行う。 @@ -267,19 +290,18 @@ LunaKeyboard::Callback(Event& ev) MouseSampling(); } + // キューから取り出す。ここで取り出せるのは MDKey またはマウスデータ if (sendqueue.Dequeue(&mdkey)) { - // キューから取り出す。ここで取り出せるのは MDKey - // またはマウスデータ // 送信 - gSIO->Rx(1, mdkey); + sio->Rx(1, mdkey); } - if (buzzer_on && now > buzzer_end) { - buzzer_on = false; - Release(); + if (IsPressed(KC_buzzer) && now > buzzer_end) { + // VM スレッド内なので BreakKey() ではなくこっちを直接呼ぶ + KeyInput(KC_buzzer | KC_FLAG_BREAK); } - if (sendqueue.Empty() == false || buzzer_on) { + if (sendqueue.Empty() == false || IsPressed(KC_buzzer)) { // 送信データが残っていれば次は 2msec 後 Start(2_msec); } else if (mouse_on) { @@ -395,8 +417,8 @@ LunaKeyboard::Command(uint32 data) UIMessage::Post(UIMessage::KEYBOARD); if (loglevel >= 1) { const auto name = GetKeyName(LED2Keycode(code)); - putlogn("Command LED $%x(%s) %s", - code, name.c_str(), (on ? "ON" : "OFF")); + putlogn("Command $%02x LED $%x(%s) %s", + data, code, name.c_str(), (on ? "ON" : "OFF")); } break; } @@ -413,12 +435,13 @@ LunaKeyboard::Command(uint32 data) uint f = data & 7; buzzer_msec = buzzer_msec_table[t]; buzzer_freq = buzzer_freq_table[f]; - putlog(1, "Command Buzzer %dmsec, %dHz", buzzer_msec, buzzer_freq); + putlog(1, "Command $%02x Buzzer %umsec, %uHz", + data, buzzer_msec, buzzer_freq); - uint64 now = gMPU->GetVirtTime(); + uint64 now = scheduler->GetVirtTime(); buzzer_end = now + buzzer_msec * 1_msec; - buzzer_on = true; - Acquire(); + // VM スレッド内なので MakeKey() ではなく KeyInput() を直接呼ぶ + KeyInput(KC_buzzer | KC_FLAG_MAKE); Start(2_msec); break; } @@ -428,7 +451,7 @@ LunaKeyboard::Command(uint32 data) // +---+---+---+---+---+---+---+---+ // | 0 0 1 | x x x x x | // +---+---+---+---+---+---+---+---+ - putlog(1, "Command Mouse Sampling Off"); + putlog(1, "Command $%02x Mouse Sampling Off", data); MouseOff(); break; @@ -437,12 +460,12 @@ LunaKeyboard::Command(uint32 data) // +---+---+---+---+---+---+---+---+ // | 0 1 1 | x x x x x | // +---+---+---+---+---+---+---+---+ - putlog(1, "Command Mouse Sampling On"); + putlog(1, "Command $%02x Mouse Sampling On", data); MouseOn(); break; default: - putlog(1, "未定義コマンド受信 $%02x", data); + putlog(1, "Command $%02x Undefined", data); break; } } @@ -502,7 +525,7 @@ LunaKeyboard::keycode2lunakey_table[KC_m LunaKey_minus, // [2c] LunaKey_circum, // [2d] LunaKey_backslash, // [2e] - NoKey, // [2f] + LunaKey_buzzer, // [2f] NoKey, // [30] NoKey, // [31]