-
Notifications
You must be signed in to change notification settings - Fork 155
Add Stepper motor driver #359
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
87971d7
wip: Add A4988 stepper motor driver
Grazfather 4007566
wip: Rewrite driver in terms of time types
Grazfather 412682f
Finish using mdf time
Grazfather 913a43e
example is not rp2040 only
Grazfather 869cbe8
Generalize driver and add DRV8825 support
Grazfather bc57680
mv to generic name
Grazfather 26789dd
Cleanup todos
Grazfather 18296be
Update readme
Grazfather 0b20144
Update module comment
Grazfather de51caa
Set DRV times
Grazfather f00fcf2
Actually wait board's sleep time
Grazfather 954632f
More explanatory comments
Grazfather 1bd2962
Fix typos and cleanup
Grazfather 2e3b742
add stepper.zig to build.zig.zon
Grazfather 63de581
Add a few more tests
Grazfather File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //! | ||
| //! An abstract clock device | ||
| //! | ||
| //! Clock_Devices can be used to track time & sleep | ||
| //! | ||
|
|
||
| const std = @import("std"); | ||
| const mdf = @import("../framework.zig"); | ||
|
|
||
| const Clock_Device = @This(); | ||
|
|
||
| /// Pointer to the object implementing the driver. | ||
| /// | ||
| /// If the implementation requires no `object` pointer, | ||
| /// you can safely use `undefined` here. | ||
| object: *anyopaque, | ||
|
|
||
| /// Virtual table for the digital i/o functions. | ||
| vtable: *const VTable, | ||
|
|
||
| /// API | ||
| pub fn is_reached(td: Clock_Device, time: mdf.time.Absolute) bool { | ||
| const now = td.get_time_since_boot(); | ||
| return time.is_reached_by(now); | ||
| } | ||
|
|
||
| pub fn make_timeout(td: Clock_Device, timeout: mdf.time.Duration) mdf.time.Absolute { | ||
| return @as(mdf.time.Absolute, @enumFromInt(td.get_time_since_boot().to_us() + timeout.to_us())); | ||
| } | ||
|
|
||
| pub fn make_timeout_us(td: Clock_Device, timeout_us: u64) mdf.time.Absolute { | ||
| return @as(mdf.time.Absolute, @enumFromInt(td.get_time_since_boot().to_us() + timeout_us)); | ||
| } | ||
|
|
||
| pub fn sleep_ms(td: Clock_Device, time_ms: u32) void { | ||
| td.sleep_us(time_ms * 1000); | ||
| } | ||
|
|
||
| pub fn sleep_us(td: Clock_Device, time_us: u64) void { | ||
| const end_time = td.make_timeout_us(time_us); | ||
| while (!td.is_reached(end_time)) {} | ||
| } | ||
|
|
||
| /// VTable methods | ||
| pub fn get_time_since_boot(td: Clock_Device) mdf.time.Absolute { | ||
| return td.vtable.get_time_since_boot(td.object); | ||
| } | ||
|
|
||
| pub const VTable = struct { | ||
| get_time_since_boot: *const fn (*anyopaque) mdf.time.Absolute, | ||
| }; | ||
|
|
||
| pub const Test_Device = struct { | ||
| time: u64 = 0, | ||
|
|
||
| pub fn init() Test_Device { | ||
| return Test_Device{}; | ||
| } | ||
|
|
||
| pub fn elapse_time(dev: *Test_Device, time_us: u64) void { | ||
| dev.time += time_us; | ||
| } | ||
|
|
||
| pub fn set_time(dev: *Test_Device, time_us: u64) void { | ||
| dev.time = time_us; | ||
| } | ||
|
|
||
| pub fn clock_device(dev: *Test_Device) Clock_Device { | ||
| return Clock_Device{ | ||
| .object = dev, | ||
| .vtable = &vtable, | ||
| }; | ||
| } | ||
|
|
||
| pub fn get_time_since_boot_fn(ctx: *anyopaque) mdf.time.Absolute { | ||
| const dev: *Test_Device = @ptrCast(@alignCast(ctx)); | ||
| return @enumFromInt(dev.time); | ||
| } | ||
|
|
||
| const vtable = VTable{ | ||
| .get_time_since_boot = Test_Device.get_time_since_boot_fn, | ||
| }; | ||
| }; | ||
|
|
||
| test Test_Device { | ||
| var ttd = Test_Device.init(); | ||
|
|
||
| const td = ttd.clock_device(); | ||
|
|
||
| // Check if time elapses between calls | ||
| try std.testing.expectEqual(0, td.get_time_since_boot().to_us()); | ||
| ttd.elapse_time(2); | ||
| try std.testing.expectEqual(2, td.get_time_since_boot().to_us()); | ||
|
|
||
| try std.testing.expect(!td.is_reached(@enumFromInt(4))); | ||
| ttd.elapse_time(2); | ||
| try std.testing.expect(td.is_reached(@enumFromInt(4))); | ||
|
|
||
| // Timeouts | ||
| try std.testing.expectEqual( | ||
| 54, | ||
| @intFromEnum(td.make_timeout(mdf.time.Duration.from_us(50))), | ||
| ); | ||
| ttd.elapse_time(50); | ||
| try std.testing.expectEqual( | ||
| 104, | ||
| @intFromEnum(td.make_timeout_us(50)), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,5 +10,6 @@ | |
| "display", | ||
| "input", | ||
| "io_expander", | ||
| "stepper", | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.