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

Add callback for muxed buttons using 47165 #39

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/EncoderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace EncoderTool
bool buttonChanged();

counter_t update(uint_fast8_t phaseA, uint_fast8_t phaseB, uint_fast8_t btn = 0);
counter_t updateBtn(uint_fast8_t btn);

protected:
EncoderBase() = default;
Expand Down Expand Up @@ -210,12 +211,6 @@ namespace EncoderTool
template <typename counter_t>
counter_t EncoderBase<counter_t>::update(uint_fast8_t phaseA, uint_fast8_t phaseB, uint_fast8_t btn)
{
if (button.update(btn))
{
btnChanged = true;
if (btnCallback != nullptr) { btnCallback(button.read()); }
}

unsigned input = (phaseA << 1 | phaseB) ^ invert; // invert signals if necessary
if (stateMachine == nullptr) return 0; // tick might get called from yield before class is initialized

Expand Down Expand Up @@ -278,6 +273,31 @@ namespace EncoderTool
return false;
}

template <typename counter_t>
counter_t EncoderBase<counter_t>::updateBtn(uint_fast8_t btn)
{
if (button.update(btn)) //button changed
{
btnChanged = true;
if (btnCallback != nullptr)
{
btnCallback(button.read());
return -1;
}
else{
return button.read();
}
}
else
{
return -1;
}

//#if defined(USE_ERROR_CALLBACKS)
// TODO: implement error callback for button
// #endif
}

template <typename counter_t>
const uint8_t EncoderBase<counter_t>::stateMachineQtr[7][4]{
// 00 01 10 11
Expand Down
10 changes: 10 additions & 0 deletions src/Multiplexed/EncPlex74165.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ namespace EncoderTool
int btn = Btn.pin < NUM_DIGITAL_PINS ? directRead(Btn) : LOW;

int delta = EncPlexBase<counter_t>::encoders[0].update(a, b, btn);
int btnState = EncPlexBase<counter_t>::encoders[0].updateBtn(btn);

if (delta != 0 && EncPlexBase<counter_t>::callback != nullptr)
{
EncPlexBase<counter_t>::callback(0, EncPlexBase<counter_t>::encoders[0].getValue(), delta);
}
if (btnState != -1 && EncPlexBase<counter_t>::btnCallback != nullptr)
{
EncPlexBase<counter_t>::btnCallback(0, btnState);
}
for (unsigned i = 1; i < EncPlexBase<counter_t>::encoderCount; i++) // shift in the the rest of the encoders
{
directWrite(CLK, HIGH);
Expand All @@ -108,6 +113,11 @@ namespace EncoderTool
{
EncPlexBase<counter_t>::callback(i, EncPlexBase<counter_t>::encoders[i].getValue(), delta);
}
int btnState = EncPlexBase<counter_t>::encoders[i].updateBtn(Btn.pin < NUM_DIGITAL_PINS ? directRead(Btn) : -1);
if (btnState != -1 && EncPlexBase<counter_t>::btnCallback != nullptr)
{
EncPlexBase<counter_t>::btnCallback(i, btnState);
}
directWrite(CLK, LOW);
delay50ns();
}
Expand Down
8 changes: 8 additions & 0 deletions src/Multiplexed/EncPlexBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace EncoderTool
#endif

void attachCallback(allCallback_t callback);
void attachBtnCallback(allBtnCallback_t btnCallback);
EncoderBase<counter_t>& operator[](size_t idx);

protected:
Expand All @@ -26,11 +27,13 @@ namespace EncoderTool

void begin(CountMode mode = CountMode::quarter);
void begin(allCallback_t, CountMode mode = CountMode::quarter);
void begin(allCallback_t, allBtnCallback_t, CountMode mode = CountMode::quarter);

const size_t encoderCount;
EncoderBase<counter_t>* encoders;

allCallback_t callback = nullptr;
allBtnCallback_t btnCallback = nullptr;
counter_t c;
};

Expand Down Expand Up @@ -67,4 +70,9 @@ namespace EncoderTool
{
callback = _callback;
}
template <typename counter_t>
void EncPlexBase<counter_t>::attachBtnCallback(allBtnCallback_t _btnCallback)
{
btnCallback = _btnCallback;
}
}