-
Notifications
You must be signed in to change notification settings - Fork 852
feat: add SDS011 dust sensor instrument #3611
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
Open
thisisanubhav
wants to merge
7
commits into
fossasia:main
Choose a base branch
from
thisisanubhav:feat/2988-dust-sensor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,165
−4
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d263e3e
feat(dust-sensor): restore DSM-501A instrument
thisisanubhav 3d3fe7f
fix(dust-sensor): handle lifecycle edge cases
thisisanubhav 2e3e3fc
feat(dust-sensor): read SDS011 over UART2
thisisanubhav 61c7c69
docs(dust-sensor): add SDS011 instrument screenshot
thisisanubhav 0f1f9d5
fix(dust-sensor): validate UART firmware setup
thisisanubhav 2d592c2
fix(dust-sensor): serialize UART operations
thisisanubhav f6659a5
fix(dust-sensor): enable SDS011 query mode
thisisanubhav 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,81 @@ | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:pslab/communication/commands_proto.dart'; | ||
| import 'package:pslab/communication/packet_handler.dart'; | ||
|
|
||
| class Uart2 { | ||
| static const double _clockRate = 64e6; | ||
|
|
||
| final PacketHandler packetHandler; | ||
| final bool requiresWriteAcknowledgement; | ||
| final CommandsProto _commands = CommandsProto(); | ||
|
|
||
| Uart2(this.packetHandler, {this.requiresWriteAcknowledgement = false}); | ||
|
|
||
| Future<void> configure(int baudRate) async { | ||
| if (baudRate <= 0) { | ||
| throw ArgumentError.value(baudRate, 'baudRate', 'Must be positive'); | ||
| } | ||
| if (PacketHandler.boardType == BoardType.scpi) { | ||
| throw UnsupportedError('UART2 is not available on this PSLab model'); | ||
| } | ||
|
|
||
| final divider = ((_clockRate / baudRate) / 4 - 1).round(); | ||
| if (divider < 0 || divider > 0xffff) { | ||
| throw ArgumentError.value( | ||
| baudRate, | ||
| 'baudRate', | ||
| 'Cannot be generated by the UART2 clock', | ||
| ); | ||
| } | ||
|
|
||
| packetHandler.sendByte(_commands.uart2); | ||
| packetHandler.sendByte(_commands.setBaud); | ||
| packetHandler.sendInt(divider); | ||
| final acknowledgement = await packetHandler.getAcknowledgement(); | ||
| if (acknowledgement != 1) { | ||
| throw StateError( | ||
| 'UART2 rejected baud-rate configuration (ACK $acknowledgement)', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Future<bool> hasData() async { | ||
| packetHandler.sendByte(_commands.uart2); | ||
| packetHandler.sendByte(_commands.readUart2Status); | ||
| return await _readResponseByte() != 0; | ||
| } | ||
|
|
||
| Future<int> readByte() async { | ||
| packetHandler.sendByte(_commands.uart2); | ||
| packetHandler.sendByte(_commands.readByte); | ||
| return _readResponseByte(); | ||
| } | ||
|
|
||
| Future<void> writeByte(int value) async { | ||
| if (value < 0 || value > 0xff) { | ||
| throw ArgumentError.value(value, 'value', 'Must fit in one byte'); | ||
| } | ||
| packetHandler.sendByte(_commands.uart2); | ||
| packetHandler.sendByte(_commands.sendByte); | ||
| packetHandler.sendByte(value); | ||
| if (requiresWriteAcknowledgement) { | ||
| await packetHandler.getAcknowledgement(); | ||
| } | ||
| } | ||
|
|
||
| Future<void> write(List<int> values) async { | ||
| for (final value in values) { | ||
| await writeByte(value); | ||
| } | ||
| } | ||
|
|
||
| Future<int> _readResponseByte() async { | ||
| final response = Uint8List(1); | ||
| final bytesRead = await packetHandler.read(response, 1); | ||
| if (bytesRead != 1) { | ||
| throw StateError('UART2 response timed out'); | ||
| } | ||
| return response[0]; | ||
| } | ||
| } | ||
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
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.