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

DigitalInput::wait() optimizations #21

Merged
merged 3 commits into from
Feb 10, 2022
Merged
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
54 changes: 27 additions & 27 deletions firmware/gameport-adapter/DigitalPin.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,60 +99,60 @@ class DigitalInput {
}
}

/// Gets the value of the input.
bool get() const {
/// Read raw bit data
uint8_t read() const {
return m_input & m_pin.mask;
}

/// Checks if the input is high.
bool isHigh() const {
return get();
return read();
}

/// Checks if the input is low
bool isLow() const {
return !get();
return !read();
}

/// Waits for an edge with given timeout.
/// @param[in] edge is the type of edge to wait for
/// @param[in] timeount is the timeout in microseconds
uint16_t wait(Edge edge, uint16_t timeout) const {
auto last = get();
for (; timeout; timeout--) {
const auto next = get();
if (last == next) {
continue;
}
switch (edge) {
case Edge::falling:
if (last > next) {
return timeout;
}
break;
case Edge::rising:
if (last < next) {
return timeout;
}
break;
case Edge::any:
return timeout;
}
last = next;
if (edge == Edge::falling) {
return waitImpl(timeout, [](uint8_t a, uint8_t) {return a;});
}
return 0u;
if (edge == Edge::rising) {
return waitImpl(timeout, [](uint8_t, uint8_t b) {return b;});
}
// edge == Edge::rising
return waitImpl(timeout, [](uint8_t a, uint8_t b) {return a|b;});
}

/// Waits for a state with given timeout.
/// @param[in] state is the state to wait for
/// @param[in] timeount is the timeout in microseconds
uint16_t wait(bool state, uint16_t timeout) const {
for (; state != get() && timeout; timeout--)
for (; state != isHigh() && timeout; timeout--)
;
return timeout;
}

private:
DigitalPin<Id> m_pin;
volatile typename DigitalPin<Id>::RegType &m_input;

template <typename T>
uint16_t waitImpl(uint16_t timeout, T compare) const {
auto last = read();
for (; timeout; timeout--) {
const auto next = read();
if (last != next) {
if (compare(last, next)) {
return timeout;
}
last = next;
}
}
return 0u;
}
};
2 changes: 1 addition & 1 deletion firmware/gameport-adapter/GrIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GrIP : public Joystick {
if (!m_clock.wait(Edge::falling, 100)) {
return 0u;
}
result |= uint32_t(m_data.get()) << i;
result |= uint32_t(m_data.isHigh()) << i;
}

// alighn the bits to have the binary tag in front. This code
Expand Down
4 changes: 3 additions & 1 deletion firmware/gameport-adapter/Logitech.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ class Logitech : public Joystick {
}

byte readData() const {
return m_data0.get() | m_data1.get() << 1;
const auto b0 = m_data0.read();
const auto b1 = m_data1.read();
return bool(b0) | bool(b1) << 1;
}

Packet readPacket() const {
Expand Down
8 changes: 4 additions & 4 deletions firmware/gameport-adapter/Sidewinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ class Sidewinder : public Joystick {
if (!m_clock.wait(Edge::rising, wait_duration)) {
break;
}
const uint8_t b1 = m_data0.get();
const uint8_t b2 = m_data1.get();
const uint8_t b3 = m_data2.get();
packet.data[packet.size] = b1 | (b2 << 1) | (b3 << 2);
const auto b1 = m_data0.read();
const auto b2 = m_data1.read();
const auto b3 = m_data2.read();
packet.data[packet.size] = bool(b1) | bool(b2) << 1 | bool(b3) << 2;
}
}
m_trigger.setLow();
Expand Down
2 changes: 1 addition & 1 deletion firmware/gameport-adapter/gameport-adapter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static Joystick *createJoystick(byte sw) {
static HidJoystick hidJoystick;

void setup() {
//Serial.begin(9600);
Serial.begin(9600);
//while(!Serial);
const auto sw1 = DigitalInput<14, true>{}.isLow();
const auto sw2 = DigitalInput<15, true>{}.isLow();
Expand Down