Skip to content

Commit

Permalink
Support for up/down arrows as lcd control buttons (#348)
Browse files Browse the repository at this point in the history
* Support for up/down arrows as lcd control buttons

* Add arrow keys to scripting and tests

Co-authored-by: VintagePC <53943260+vintagepc@users.noreply.github.com>
  • Loading branch information
GilesBathgate and vintagepc authored Dec 6, 2021
1 parent 3bfe0ee commit ebc7a80
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions MK404.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ int initGL()
// Set up projection matrix
glutDisplayFunc(displayCB); /* set window's display callback */
glutKeyboardFunc(KeyCB); /* set window's key callback */
glutSpecialFunc(KeyController::GLSpecialKeyReceiver);
glutMouseFunc(MouseCB);
glutPassiveMotionFunc(PassiveMotionCB);
glutMotionFunc(MotionCB);
Expand Down
15 changes: 15 additions & 0 deletions parts/KeyController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "KeyController.h"
#include "IKeyClient.h"
#include "IScriptable.h"
#include <GL/glut.h>
#include <iostream>
#include <memory> // for allocator_traits<>::value_type
#include <string>
Expand All @@ -46,6 +47,14 @@ IScriptable::LineStatus KeyController::ProcessAction(unsigned int /*iAction*/, c
{
key = 0xd;
}
else if (vArgs.at(0) == "up")
{
key = GLUT_KEY_UP | SPECIAL_KEY_MASK;
}
else if (vArgs.at(0) == "down")
{
key = GLUT_KEY_DOWN | SPECIAL_KEY_MASK;
}
OnKeyPressed(key);
return LineStatus::Finished;
}
Expand Down Expand Up @@ -78,6 +87,12 @@ void KeyController::PutNiceKeyName(unsigned char key)
case 0xd:
std::cout << "Enter";
break;
case GLUT_KEY_UP | SPECIAL_KEY_MASK:
std::cout << "Up Arrow";
break;
case GLUT_KEY_DOWN | SPECIAL_KEY_MASK:
std::cout << "DOwn Arrow";
break;
default:
std::cout << key;
break;
Expand Down
3 changes: 3 additions & 0 deletions parts/KeyController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <string> // for string
#include <vector> // for vector

static constexpr uint8_t SPECIAL_KEY_MASK = 0x80;

class IKeyClient;

class KeyController: private Scriptable
Expand All @@ -46,6 +48,7 @@ class KeyController: private Scriptable
void PrintKeys(bool bMarkdown);

static inline void GLKeyReceiver(unsigned char key, int /*x*/, int /*y*/) { KeyController::GetController().OnKeyPressed(key); };
static inline void GLSpecialKeyReceiver(int key, int /*x*/, int /*y*/) { KeyController::GetController().OnKeyPressed(key | SPECIAL_KEY_MASK); };

protected:
KeyController();
Expand Down
7 changes: 6 additions & 1 deletion parts/components/RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
along with MK404. If not, see <http://www.gnu.org/licenses/>.
*/


#include "KeyController.h"
#include "RotaryEncoder.h"
#include "TelemetryHost.h"
#include "gsl-lite.hpp"
#include <GL/glut.h>
#include <algorithm> // for copy
#include <iostream>

Expand Down Expand Up @@ -173,11 +174,13 @@ void RotaryEncoder::OnKeyPress(const Key &key)
{
switch (key)
{
case GLUT_KEY_UP | SPECIAL_KEY_MASK:
case 'w':
std::cout << '<';
Twist(RotaryEncoder::CCW_CLICK);
//if (m_pVis) m_pVis->TwistKnob(true);
break;
case GLUT_KEY_DOWN | SPECIAL_KEY_MASK:
case 's':
std::cout << '>';
Twist(RotaryEncoder::CW_CLICK);
Expand All @@ -202,7 +205,9 @@ RotaryEncoder::RotaryEncoder(bool bVerbose):Scriptable("Encoder"),IKeyClient(),m
RegisterActionAndMenu("TwistCCW", "Twists the encoder once cycle counterclockwise",ActTwistCCW);

RegisterKeyHandler('w', "Twists encoder CCW");
RegisterKeyHandler(GLUT_KEY_UP | SPECIAL_KEY_MASK, "Twists encoder CCW");
RegisterKeyHandler('s', "Twists encoder CW");
RegisterKeyHandler(GLUT_KEY_DOWN | SPECIAL_KEY_MASK, "Twists encoder CW");
RegisterKeyHandler('h', "Pushes and long-holds the encoder button.");
RegisterKeyHandler(0xd, "Pushes and releases the encoder button");
}
Binary file added scripts/tests/snaps/ext1/GFXLiteKey13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion scripts/tests/test_lite_gfx_keys.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Board::WaitMs(10)
3DView::SnapRect(tests/snaps/GFXLiteKey03,477,711,88,88)
KeyCtl::Key(s)
Board::WaitMs(1)
KeyCtl::Key(s)
KeyCtl::Key(down)
Board::WaitMs(10)
3DView::SnapRect(tests/snaps/GFXLiteKey04,477,711,88,88)
KeyCtl::Key(c)
Expand Down Expand Up @@ -48,4 +48,7 @@ GLHelper::SnapRect(tests/snaps/GFXLiteKey11,0,0,500,200)
KeyCtl::Key(t)
Board::WaitMs(100)
GLHelper::SnapRect(tests/snaps/GFXLiteKey11a,0,0,500,200)
KeyCtl::Key(up)
Board::WaitMs(10)
3DView::SnapRect(tests/snaps/GFXLiteKey13,477,711,88,88)
KeyCtl::Key(q)
13 changes: 10 additions & 3 deletions utility/MK3SGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ MK3SGL::MK3SGL(const std::string &strModel, bool bMMU, Printer *pParent):Scripta
RegisterAction("MouseMove", "Simulates a mouse move (x,y)", ActMouseMove, {ArgType::Int,ArgType::Int});

RegisterKeyHandler('`', "Reset camera view to default");
RegisterKeyHandler('w',"");
RegisterKeyHandler('s',"");
RegisterKeyHandler('w', "");
RegisterKeyHandler(GLUT_KEY_UP | SPECIAL_KEY_MASK, "");
RegisterKeyHandler('s', "");
RegisterKeyHandler(GLUT_KEY_DOWN | SPECIAL_KEY_MASK, "");

// RegisterKeyHandler('5',"");
// RegisterKeyHandler('6',"");
Expand All @@ -132,6 +134,7 @@ MK3SGL::MK3SGL(const std::string &strModel, bool bMMU, Printer *pParent):Scripta
glutDisplayFunc(fcnDraw);

glutKeyboardFunc(KeyController::GLKeyReceiver); // same func as main window.
glutSpecialFunc(KeyController::GLSpecialKeyReceiver);

auto fwd = [](int button, int state, int x, int y) {g_pMK3SGL->MouseCB(button,state,x,y);};
glutMouseFunc(fwd);
Expand Down Expand Up @@ -210,9 +213,13 @@ void MK3SGL::OnKeyPress(const Key& key)
case '`':
ResetCamera();
break;
case GLUT_KEY_UP | SPECIAL_KEY_MASK:
case 'w':
TwistKnob(true);
break;
case GLUT_KEY_DOWN | SPECIAL_KEY_MASK:
case 's':
TwistKnob(key=='w');
TwistKnob(false);
break;
// case '5':
// case '6':
Expand Down

0 comments on commit ebc7a80

Please sign in to comment.