Compare commits

...

6 Commits

6 changed files with 43 additions and 24 deletions

View File

@ -48,7 +48,8 @@ bool LatchedButton::Update(Joystick* js) {
} }
PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, bool double_action, bool split) : Button(vbutton, reader) { PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, uint8_t release_delay, bool double_action, bool split) : Button(vbutton, reader) {
this->release_delay = release_delay;
this->release_time1 = 0; this->release_time1 = 0;
this->release_time2 = 0; this->release_time2 = 0;
if (double_action) { if (double_action) {
@ -78,20 +79,23 @@ bool PulsedButton::Update(Joystick* js) {
switch(type) { switch(type) {
case BUTTON_PULSED: case BUTTON_PULSED:
if (reader->On()) js->PressButton(vbutton); if (reader->On()) {
js->PressButton(vbutton);
release_time1 = millis() + release_delay;
}
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
js->PressButton(vbutton); js->PressButton(vbutton);
release_time1 = millis() + 250; release_time1 = millis() + release_delay;
break; break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (reader->On()) { if (reader->On()) {
js->PressButton(vbutton); js->PressButton(vbutton);
release_time1 = millis() + 250; release_time1 = millis() + release_delay;
} }
else { else {
js->PressButton(vbutton2); js->PressButton(vbutton2);
release_time2 = millis() + 250; release_time2 = millis() + release_delay;
} }
break; break;
} }
@ -100,13 +104,16 @@ bool PulsedButton::Update(Joystick* js) {
} }
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton, NULL) { EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold, uint8_t release_delay) : Button(vbutton, NULL) {
this->type = ENCODER_PULSED_SPLIT; this->type = ENCODER_PULSED_SPLIT;
this->vbutton2 = vbutton + 1; this->vbutton2 = vbutton + 1;
this->encoder = new Encoder(pin1, pin2); this->encoder = new Encoder(pin1, pin2);
this->last_value = encoder->read(); this->last_value = encoder->read();
this->release_delay = release_delay;
this->release_time1 = 0; this->release_time1 = 0;
this->release_time2 = 0; this->release_time2 = 0;
this->ticks = 0;
this->tick_threshold = tick_threshold;
} }
bool EncoderButton::Update(Joystick* js) { bool EncoderButton::Update(Joystick* js) {
@ -122,16 +129,23 @@ bool EncoderButton::Update(Joystick* js) {
bool changed = false; bool changed = false;
long new_value = encoder->read(); long new_value = encoder->read();
if (new_value > last_value) { if (new_value != last_value) {
ticks += last_value - new_value;
last_value = new_value;
}
if (ticks >= tick_threshold) {
js->PressButton(vbutton); js->PressButton(vbutton);
changed = true; changed = true;
release_time1 = millis() + 250; release_time1 = millis() + release_delay;
ticks = 0;
} }
else if (new_value < last_value) { else if (ticks <= tick_threshold * -1) {
js->PressButton(vbutton2); js->PressButton(vbutton2);
changed = true; changed = true;
release_time2 = millis() + 250; release_time2 = millis() + release_delay;
ticks = 0;
} }
last_value = new_value;
return changed; return changed;
} }

View File

@ -50,12 +50,13 @@ class LatchedButton : public Button {
class PulsedButton : public Button { class PulsedButton : public Button {
public: public:
PulsedButton(uint8_t vbutton, Reader* reader, bool double_action = false, bool split = false); PulsedButton(uint8_t vbutton, Reader* reader, uint8_t release_delay, bool double_action = false, bool split = false);
bool Update(Joystick* js); bool Update(Joystick* js);
protected: protected:
bool double_action; bool double_action;
bool split; bool split;
uint8_t release_delay;
unsigned long release_time1; unsigned long release_time1;
unsigned long release_time2; unsigned long release_time2;
uint8_t vbutton2; uint8_t vbutton2;
@ -63,15 +64,18 @@ class PulsedButton : public Button {
class EncoderButton : public Button { class EncoderButton : public Button {
public: public:
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton); EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold, uint8_t release_delay);
bool Update(Joystick* js); bool Update(Joystick* js);
protected: protected:
Encoder* encoder; Encoder* encoder;
long last_value; long last_value;
uint8_t vbutton2; uint8_t vbutton2;
uint8_t release_delay;
unsigned long release_time1; unsigned long release_time1;
unsigned long release_time2; unsigned long release_time2;
int8_t ticks;
int8_t tick_threshold;
}; };
#endif #endif

View File

@ -21,11 +21,12 @@ bool operator !=(JoyReport a, JoyReport b){
return !(a == b); return !(a == b);
} }
Joystick::Joystick(bool debug) { Joystick::Joystick(uint8_t release_delay, bool debug) {
_debug = debug; _debug = debug;
_virtual_buttons = 0; _virtual_buttons = 0;
_num_axes = 0; _num_axes = 0;
_num_buttons = 0; _num_buttons = 0;
this->release_delay = release_delay;
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
_joyReport.axis[i] = 0; _joyReport.axis[i] = 0;
@ -53,12 +54,12 @@ void Joystick::AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonT
_addButton(type, new MatrixReader(row, col, matrix, pullup)); _addButton(type, new MatrixReader(row, col, matrix, pullup));
} }
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) { void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type) {
Button *button; Button *button;
switch (type) { switch (type) {
case ENCODER_PULSED_SPLIT: case ENCODER_PULSED_SPLIT:
// add an encoder button. _BuildButton() doesn't do everything we need, however... // add an encoder button. _BuildButton() doesn't do everything we need, however...
button = new EncoderButton(pin1, pin2, _virtual_buttons); button = new EncoderButton(pin1, pin2, _virtual_buttons, tick_threshold, release_delay);
_buttons[_num_buttons] = button; _buttons[_num_buttons] = button;
_num_buttons++; _num_buttons++;
_virtual_buttons += 2; _virtual_buttons += 2;
@ -157,15 +158,15 @@ void Joystick::_addButton(ButtonType type, Reader* reader) {
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
button = new PulsedButton(_virtual_buttons, reader, false, false); button = new PulsedButton(_virtual_buttons, reader, release_delay, false, false);
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
button = new PulsedButton(_virtual_buttons, reader, true, false); button = new PulsedButton(_virtual_buttons, reader, release_delay, true, false);
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
button = new PulsedButton(_virtual_buttons, reader, true, true); button = new PulsedButton(_virtual_buttons, reader, release_delay, true, true);
_virtual_buttons += 2; _virtual_buttons += 2;
break; break;
case BUTTON_LATCHED_MOMENTARY: case BUTTON_LATCHED_MOMENTARY:

View File

@ -29,7 +29,7 @@ bool operator !=(JoyReport a, JoyReport b);
class Joystick { class Joystick {
public: public:
Joystick(bool debug=false); Joystick(uint8_t release_delay = 50, bool debug = false);
void Init(); void Init();
void Update(); void Update();
@ -49,7 +49,7 @@ class Joystick {
void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true); void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true);
// Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO) // Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO)
void AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type); void AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type);
// Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not. // Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not.
void AddAxis(uint8_t pin); void AddAxis(uint8_t pin);
@ -78,6 +78,7 @@ class Joystick {
JoyReport _joyReport; JoyReport _joyReport;
bool _debug; bool _debug;
uint8_t release_delay;
}; };
#endif #endif

View File

@ -19,7 +19,6 @@ void Matrix::Activate(uint8_t pin) {
_enable(pin); _enable(pin);
active_pin = pin; active_pin = pin;
delayMicroseconds(10); // allow ample time for signal to propagate
} }
void Matrix::_enable(uint8_t pin) { void Matrix::_enable(uint8_t pin) {

View File

@ -42,13 +42,13 @@ using namespace admux;
bool debug = true; bool debug = true;
Joystick js(debug); Joystick js(250, debug);
void setup() { void setup() {
js.Init(); js.Init();
Serial.println("Adding encoder."); Serial.println("Adding encoder.");
js.AddEncoder(ENCODER, ENCODER_PULSED_SPLIT); js.AddEncoder(ENCODER, 3, ENCODER_PULSED_SPLIT);
// Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's // Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's
// real state, and will be the most common, but you can also change how the button works in software like so... // real state, and will be the most common, but you can also change how the button works in software like so...