Skip to content
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

Shared / borrowed encoder #887

Closed
simoniddqd opened this issue Feb 6, 2023 · 5 comments
Closed

Shared / borrowed encoder #887

simoniddqd opened this issue Feb 6, 2023 · 5 comments

Comments

@simoniddqd
Copy link

In my project I am sharing a rotary encoder between an instance of PBAbsoluteEncoder and an instance of CCRotaryEncoder by physically switching pins with a switch. However I would like to make this more simple, perhaps with a small button switch or maybe one of the multipurposebutton commands, and wonder if it could be done with code. I've looked at this but it seems to be outdated. Any help is greatly appreciated

@simoniddqd
Copy link
Author

This is the compilation error I get when trying to compile the example from the thread

`/private/var/folders/mx/kjcsv4q907jblll2vbk5807w0000gn/T/.arduinoIDE-unsaved202318-2467-1jcxd3g.ygz3k/sketch_feb8a/sketch_feb8a.ino:16:1: error: could not convert '{enc, CS::MCU::V_POT_1, 1, 4, ()}' from '' to 'CS::BorrowedMIDIRotaryEncoderCS::ContinuousCCSender {aka CS::GenericMIDIRotaryEncoder<CS::AHEncoder&, CS::ContinuousCCSender>}'
};
^
/private/var/folders/mx/kjcsv4q907jblll2vbk5807w0000gn/T/.arduinoIDE-unsaved202318-2467-1jcxd3g.ygz3k/sketch_feb8a/sketch_feb8a.ino:23:1: error: could not convert '{enc, CS::CHANNEL_1, 127, 4, ()}' from '' to 'CS::BorrowedMIDIAbsoluteEncoder<CS::PitchBendSender<14> > {aka CS::GenericMIDIAbsoluteEncoder<CS::AHEncoder&, CS::PitchBendSender<14> >}'
};
^
/private/var/folders/mx/kjcsv4q907jblll2vbk5807w0000gn/T/.arduinoIDE-unsaved202318-2467-1jcxd3g.ygz3k/sketch_feb8a/sketch_feb8a.ino: In function 'void loop()':
/private/var/folders/mx/kjcsv4q907jblll2vbk5807w0000gn/T/.arduinoIDE-unsaved202318-2467-1jcxd3g.ygz3k/sketch_feb8a/sketch_feb8a.ino:37:11: error: 'CS::BorrowedMIDIAbsoluteEncoder<CS::PitchBendSender<14> > {aka class CS::GenericMIDIAbsoluteEncoder<CS::AHEncoder&, CS::PitchBendSender<14> >}' has no member named 'resetPositionOffset'
pbenc.resetPositionOffset();
^~~~~~~~~~~~~~~~~~~
/private/var/folders/mx/kjcsv4q907jblll2vbk5807w0000gn/T/.arduinoIDE-unsaved202318-2467-1jcxd3g.ygz3k/sketch_feb8a/sketch_feb8a.ino:41:11: error: 'CS::BorrowedMIDIRotaryEncoderCS::ContinuousCCSender {aka class CS::GenericMIDIRotaryEncoder<CS::AHEncoder&, CS::ContinuousCCSender>}' has no member named 'resetPositionOffset'
ccenc.resetPositionOffset();
^~~~~~~~~~~~~~~~~~~
Multiple libraries were found for "MIDIUSB.h"
Used: /Users/simon_mbpm1/Documents/Arduino/libraries/MIDIUSB
Not used: /Users/simon_mbpm1/Documents/Arduino/libraries/MIDIUSB-master
exit status 1

Compilation error: could not convert '{enc, CS::MCU::V_POT_1, 1, 4, ()}' from '' to 'CS::BorrowedMIDIRotaryEncoderCS::ContinuousCCSender {aka CS::GenericMIDIRotaryEncoder<CS::AHEncoder&, CS::ContinuousCCSender>}'`

@tttapa
Copy link
Owner

tttapa commented Feb 8, 2023

Untested, but this should compile using the latest version (02374bc):

#include <Control_Surface.h>

USBDebugMIDI_Interface midi;

Button button {4};

AHEncoder enc {2, 3};
BorrowedMIDIRotaryEncoder<ContinuousCCSender> ccenc {
  enc, 
  MCU::V_POT_1,
  1,   // multiplier
  4,   // pulses per step
  {},  // MIDI sender
};
BorrowedMIDIAbsoluteEncoder<PitchBendSender<14>> pbenc {
  enc, 
  CHANNEL_1,
  127, // multiplier
  4,   // pulses per step
  {},  // MIDI sender
};

void setup() {
  button.begin();
  Control_Surface.begin();
  pbenc.disable();
}

void loop() {
  Control_Surface.loop();

  auto buttonState = button.update();
  if (buttonState == Button::Falling) {
    ccenc.disable();
    pbenc.resetPositionOffset();
    pbenc.enable();
  } else if (buttonState == Button::Rising) {
    pbenc.disable();
    ccenc.resetPositionOffset();
    ccenc.enable();
  } 
}

@simoniddqd
Copy link
Author

simoniddqd commented Feb 8, 2023

Thank you Pieter for looking into this, it sure does compile, but nothing comes up in the serial monitor. I've double checked my wiring, and checked if the sketch works with each encoder object individually, and it does. However, when introducing both encoder objects at the same time while disabling one of them, nothing shows in serial monitor.

#include <Control_Surface.h>

USBDebugMIDI_Interface midi;

AHEncoder enc {2, 3};
BorrowedMIDIRotaryEncoder<ContinuousCCSender> ccenc {
  enc, 
  MCU::V_POT_1,
  1,   // multiplier
  4,   // pulses per step
  {},  // MIDI sender
};
BorrowedMIDIAbsoluteEncoder<PitchBendSender<14>> pbenc {
  enc, 
  CHANNEL_1,
  127, // multiplier
  4,   // pulses per step
  {},  // MIDI sender
};

void setup() {

  Control_Surface.begin();
  pbenc.disable();
}

void loop() {
  Control_Surface.loop();
}

I am really at the boundaries of my knowledge here, so any help or pointers in the right direction is hugely appreciated :)

@tttapa
Copy link
Owner

tttapa commented Feb 8, 2023

I think the issue that the encoder is initialized twice, and the second time, it raises an error because the interrupts are already in use. You can try moving the pbenc.disable() line before Control_Surface.begin(), so Control Surface won't initialize pbenc.

Alternatively, you can try 39182bb, which should make initializing encoders multiple times no longer fail.

@simoniddqd
Copy link
Author

Fantastic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants