-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Menu controlling via serial port #57
Comments
I have written a demo works with
#ifndef _SERIAL_KEY_H
#define _SERIAL_KEY_H
#include <Arduino.h>
#include <GEM_u8g2.h>
class SerialKey {
public:
SerialKey(int up_ascii_, int right_ascii_, int down_ascii_, int left_ascii_,
int cancel_ascii_, int ok_ascii_);
byte trigger = GEM_KEY_NONE;
byte current = GEM_KEY_NONE;
void detect();
private:
int _up_ascii;
int _right_ascii;
int _down_ascii;
int _left_ascii;
int _cancel_ascii;
int _ok_ascii;
};
#endif
#include "SerialKey.h"
#include <Arduino.h>
#include <GEM_u8g2.h>
SerialKey::SerialKey(int up_ascii_, int right_ascii_, int down_ascii_,
int left_ascii_, int cancel_ascii_, int ok_ascii_)
: _up_ascii(up_ascii_),
_right_ascii(right_ascii_),
_down_ascii(down_ascii_),
_left_ascii(left_ascii_),
_cancel_ascii(cancel_ascii_),
_ok_ascii(ok_ascii_) {}
void SerialKey::detect() {
boolean pressed = false;
if (Serial.available() > 0) {
int incomingByte = Serial.read();
// Serial.print("Received: ");
// Serial.println(incomingByte, DEC);
if (incomingByte == _up_ascii) {
current = GEM_KEY_UP;
pressed = true;
// Serial.println("Pressed up");
} else if (incomingByte == _right_ascii) {
current = GEM_KEY_RIGHT;
pressed = true;
// Serial.println("Pressed right");
} else if (incomingByte == _down_ascii) {
current = GEM_KEY_DOWN;
pressed = true;
// Serial.println("Pressed down");
} else if (incomingByte == _left_ascii) {
current = GEM_KEY_LEFT;
pressed = true;
// Serial.println("Pressed left");
} else if (incomingByte == _cancel_ascii) {
current = GEM_KEY_CANCEL;
pressed = true;
// Serial.println("Pressed cancel");
} else if (incomingByte == _ok_ascii) {
current = GEM_KEY_OK;
pressed = true;
// Serial.println("Pressed ok");
}
} else {
pressed = false;
}
if (!pressed) {
current = GEM_KEY_NONE;
}
trigger = current;
} Usage: #include "SerialKey.h"
// w (119) -> ↑
// d (100) -> →
// s (115) -> ↓
// a (97) -> ←
// q (113) -> cancel
// e (101) -> ok
SerialKey serialKey(/*Up=*/119, /*Right/Next=*/100,
/*Down=*/115, /*Left/Prev=*/97,
/*Home/Cancel=*/113, /*Select/OK=*/101);
void loop() {
if (menu.readyForKey()) {
serialKey.detect();
menu.registerKeyPress(serialKey.trigger);
}
delay(100);
} |
Oh, that's great! I've just wanted to reply to an original post that it is totally possible to control GEM via Serial from the user sketch (and link to this section of Wiki for details) and you beat me to it with a complete example=) Thanks for sharing, it can be useful for someone else! I will add link to this solution in wiki=) |
If you change U8g2 specific aliases to more general GEM ones (e.g. |
Okay no problem, do you mind if I just edit the file above or should I post the new one below here? |
Feel free to edit your original post, that should be fine! |
Done, it's looks good to you? |
Seems to be fine, thank you! |
Link to this example added to Wiki. |
When there aren't enough components around to build a prototype, maybe this is a good way to test.
The text was updated successfully, but these errors were encountered: