Skip to content
Closed
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
15 changes: 15 additions & 0 deletions drivers/src/input/rotary_encoder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub fn RotaryEncoder(comptime options: Options) type {
defer enc.last_a = a;
defer enc.last_b = b;

// Detect invalid transitions: both bits changed simultaneously
const a_changed = a.value() ^ enc.last_a.value();
const b_changed = b.value() ^ enc.last_b.value();
if (a_changed != 0 and b_changed != 0) {
return .@"error";
}

const enable = a.value() ^ b.value() ^ enc.last_a.value() ^ enc.last_b.value();
const direction = a.value() ^ enc.last_b.value();

Expand Down Expand Up @@ -136,4 +143,12 @@ test RotaryEncoder {
try std.testing.expectEqual(.decrement, try encoder.poll());
try std.testing.expectEqual(.idle, try encoder.poll());
}

// Test invalid state: both A and B change simultaneously
// Current state is (high, high) from the previous test loop
// Force both bits to change at once
a.state = .low;
b.state = .low;
try std.testing.expectEqual(.@"error", try encoder.poll());
try std.testing.expectEqual(.idle, try encoder.poll());
}
1 change: 1 addition & 0 deletions drivers/src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ test {
_ = sensor.TLV493D;
_ = sensor.TMP117;
_ = sensor.AHT30;
_ = sensor.AS5600;

_ = @import("stepper/common.zig");
_ = stepper.A4988;
Expand Down
8 changes: 4 additions & 4 deletions drivers/src/sensor/AS5600.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub const AS5600 = struct {
}

pub fn read_zero_position(self: *const Self) !u16 {
const zpos = self.read2_raw(register.ZPOS);
const zpos = try self.read2_raw(register.ZPOS);
return zpos & 0xFFF;
}

Expand All @@ -120,7 +120,7 @@ pub const AS5600 = struct {
}

pub fn read_max_position(self: *const Self) !u16 {
const mpos = self.read2_raw(register.MPOS);
const mpos = try self.read2_raw(register.MPOS);
return mpos & 0xFFF;
}

Expand All @@ -132,7 +132,7 @@ pub const AS5600 = struct {
}

pub fn read_max_angle(self: *const Self) !u16 {
const mang = self.read2_raw(register.MANG);
const mang = try self.read2_raw(register.MANG);
return mang & 0xFFF;
}

Expand All @@ -144,7 +144,7 @@ pub const AS5600 = struct {
}

pub fn read_configuration(self: *const Self) !u16 {
const configuration = self.read2_raw(register.CONF);
const configuration = try self.read2_raw(register.CONF);
return @bitCast(configuration);
}

Expand Down
11 changes: 9 additions & 2 deletions drivers/src/sensor/MPU_6050.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ pub const MPU_6050 = struct {

self.accel_range = .@"2G";
self.gyro_range = .@"250d";

while ((try self.read_reg(.pwr_mgmt_1, regs.PWR_MGMT_1)).DEVICE_RESET) {}
// wait for device to wake up
var timeout: usize = 1000;
while ((try self.read_reg(.pwr_mgmt_1, regs.PWR_MGMT_1)).DEVICE_RESET) {
timeout -= 1;
if (timeout == 0) {
return Error.Timeout;
}
self.clock.sleep_ms(1);
}

try self.modify_reg(.user_ctrl, regs.USER_CTRL, .{
.SIG_COND_RESET = true,
Expand Down
Loading