forked from tock/libtock-rs
-
Notifications
You must be signed in to change notification settings - Fork 9
First iteration of air_quality API based on the temperature API #16
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
RaresCon
wants to merge
10
commits into
UPB-CS-OpenSourceUpstream:master
Choose a base branch
from
RaresCon:air_quality_api
base: master
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
70467b8
First iteration of air_quality API
RaresCon 47f5278
Modified the API to include the suggested changes
RaresCon bfd3784
Added fake `AirQuality` driver
RaresCon 8fe570b
Added unittests for AirQuality API
RaresCon 5afcd38
Added suggested changes to AirQuality API
RaresCon 6e82822
Changed alphabetical order inside `Cargo.toml` and `lib.rs`
RaresCon 44b5247
Merge branch 'tock:master' into air_quality_api
RaresCon 6873b50
Added requested changes
RaresCon 35cd6b7
Added documentation and fixed CI bug
RaresCon 04f500a
Merge branch 'master' into air_quality_api
RaresCon 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,14 @@ | ||
| [package] | ||
| name = "libtock_air_quality" | ||
| version = "0.1.0" | ||
| authors = ["Tock Project Developers <[email protected]>"] | ||
| license = "MIT/Apache-2.0" | ||
| edition = "2021" | ||
| repository = "https://www.github.com/tock/libtock-rs" | ||
| description = "libtock air quality driver" | ||
|
|
||
| [dependencies] | ||
| libtock_platform = { path = "../../platform" } | ||
|
|
||
| [dev-dependencies] | ||
| libtock_unittest = { path = "../../unittest" } |
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,128 @@ | ||
| #![no_std] | ||
|
|
||
| use core::cell::Cell; | ||
| use libtock_platform::subscribe::OneId; | ||
| use libtock_platform::{ | ||
| share::scope, share::Handle, DefaultConfig, ErrorCode, Subscribe, Syscalls, Upcall, | ||
| }; | ||
| use Value::{Tvoc, CO2}; | ||
|
|
||
| enum Value { | ||
| CO2 = READ_CO2 as isize, | ||
| Tvoc = READ_TVOC as isize, | ||
| } | ||
|
|
||
| pub struct AirQuality<S: Syscalls>(S); | ||
|
|
||
| impl<S: Syscalls> AirQuality<S> { | ||
| /// Returns Ok() if the driver was present.This does not necessarily mean | ||
| /// that the driver is working. | ||
| pub fn exists() -> Result<(), ErrorCode> { | ||
| S::command(DRIVER_NUM, EXISTS, 0, 0).to_result() | ||
| } | ||
|
|
||
| /// Register an events listener | ||
| pub fn register_listener<'share, F: Fn(u32)>( | ||
| listener: &'share AirQualityListener<F>, | ||
| subscribe: Handle<Subscribe<'share, S, DRIVER_NUM, 0>>, | ||
| ) -> Result<(), ErrorCode> { | ||
| S::subscribe::<_, _, DefaultConfig, DRIVER_NUM, 0>(subscribe, listener) | ||
| } | ||
|
|
||
| /// Unregister the events listener | ||
| pub fn unregister_listener() { | ||
| S::unsubscribe(DRIVER_NUM, 0) | ||
| } | ||
|
|
||
| /// Initiate a CO2 measurement. | ||
| /// | ||
| /// This function is used both for synchronous and asynchronous readings | ||
| pub fn read_co2() -> Result<(), ErrorCode> { | ||
| S::command(DRIVER_NUM, READ_CO2, 0, 0).to_result() | ||
| } | ||
|
|
||
| /// Initiate a TVOC measurement. | ||
| /// | ||
| /// This function is used both for synchronous and asynchronous readings | ||
| pub fn read_tvoc() -> Result<(), ErrorCode> { | ||
| S::command(DRIVER_NUM, READ_TVOC, 0, 0).to_result() | ||
| } | ||
|
|
||
| /// Public wrapper for `read_data_sync` for CO2 synchronous measurement | ||
| pub fn read_co2_sync() -> Result<u32, ErrorCode> { | ||
| Self::read_data_sync(CO2) | ||
| } | ||
|
|
||
| /// Public wrapper for `read_data_sync` for TVOC synchronous measurement | ||
| pub fn read_tvoc_sync() -> Result<u32, ErrorCode> { | ||
| Self::read_data_sync(Tvoc) | ||
| } | ||
|
|
||
| /// Read both CO2 and TVOC values synchronously | ||
| pub fn read_sync() -> Result<(u32, u32), ErrorCode> { | ||
| match (Self::read_data_sync(CO2), Self::read_data_sync(Tvoc)) { | ||
| (Ok(co2_value), Ok(tvoc_value)) => Ok((co2_value, tvoc_value)), | ||
| (Err(co2_error), _) => Err(co2_error), | ||
| (_, Err(tvoc_error)) => Err(tvoc_error), | ||
| } | ||
| } | ||
|
|
||
| /// Initiate a synchronous CO2 or TVOC measurement, based on the `read_type`. | ||
| /// Returns Ok(value) if the operation was successful | ||
| fn read_data_sync(read_type: Value) -> Result<u32, ErrorCode> { | ||
| let data_cell: Cell<Option<u32>> = Cell::new(None); | ||
| let listener = AirQualityListener(|data_val| { | ||
| data_cell.set(Some(data_val)); | ||
| }); | ||
|
|
||
| scope(|subscribe| { | ||
| Self::register_listener(&listener, subscribe)?; | ||
| match read_type { | ||
| CO2 => { | ||
| Self::read_co2()?; | ||
| while data_cell.get() == None { | ||
| S::yield_wait(); | ||
| } | ||
|
|
||
| match data_cell.get() { | ||
| None => Err(ErrorCode::Fail), | ||
| Some(co2_value) => Ok(co2_value), | ||
| } | ||
| } | ||
| Tvoc => { | ||
| Self::read_tvoc()?; | ||
| while data_cell.get() == None { | ||
| S::yield_wait(); | ||
| } | ||
|
|
||
| match data_cell.get() { | ||
| None => Err(ErrorCode::Fail), | ||
| Some(tvoc_value) => Ok(tvoc_value), | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| pub struct AirQualityListener<F: Fn(u32)>(pub F); | ||
| impl<F: Fn(u32)> Upcall<OneId<DRIVER_NUM, 0>> for AirQualityListener<F> { | ||
| fn upcall(&self, data_val: u32, _arg1: u32, _arg2: u32) { | ||
| self.0(data_val) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Driver number and command IDs | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| const DRIVER_NUM: u32 = 0x60007; | ||
|
|
||
| // Command IDs | ||
|
|
||
| const EXISTS: u32 = 0; | ||
| const READ_CO2: u32 = 2; | ||
| const READ_TVOC: u32 = 3; |
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,120 @@ | ||
| use crate::AirQualityListener; | ||
| use core::cell::Cell; | ||
| use libtock_platform::{share::scope, ErrorCode, Syscalls, YieldNoWaitReturn}; | ||
| use libtock_unittest::fake; | ||
|
|
||
| type AirQuality = super::AirQuality<fake::Syscalls>; | ||
|
|
||
| #[test] | ||
| fn no_driver() { | ||
| let _kernel = fake::Kernel::new(); | ||
| assert_eq!(AirQuality::exists(), Err(ErrorCode::NoDevice)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn driver_check() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| assert_eq!(AirQuality::exists(), Ok(())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn read_co2() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| assert_eq!(AirQuality::read_co2(), Ok(())); | ||
| assert!(driver.is_busy()); | ||
|
|
||
| assert_eq!(AirQuality::read_co2(), Err(ErrorCode::Busy)); | ||
| assert_eq!(AirQuality::read_co2_sync(), Err(ErrorCode::Busy)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn read_tvoc() { | ||
RaresCon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| assert_eq!(AirQuality::read_tvoc(), Ok(())); | ||
| assert!(driver.is_busy()); | ||
|
|
||
| assert_eq!(AirQuality::read_tvoc(), Err(ErrorCode::Busy)); | ||
| assert_eq!(AirQuality::read_tvoc_sync(), Err(ErrorCode::Busy)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn register_unregister_listener() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| let data_cell: Cell<Option<u32>> = Cell::new(None); | ||
| let listener = AirQualityListener(|data_val| { | ||
| data_cell.set(Some(data_val)); | ||
| }); | ||
|
|
||
| scope(|subscribe| { | ||
| assert_eq!(AirQuality::read_co2(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::NoUpcall); | ||
|
|
||
| assert_eq!(AirQuality::read_tvoc(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::NoUpcall); | ||
|
|
||
| assert_eq!(AirQuality::register_listener(&listener, subscribe), Ok(())); | ||
|
|
||
| assert_eq!(AirQuality::read_co2(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::Upcall); | ||
| assert_eq!(data_cell.get(), Some(100)); | ||
|
|
||
| assert_eq!(AirQuality::read_tvoc(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::Upcall); | ||
| assert_eq!(data_cell.get(), Some(100)); | ||
|
|
||
| AirQuality::unregister_listener(); | ||
| assert_eq!(AirQuality::read_co2(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::NoUpcall); | ||
|
|
||
| assert_eq!(AirQuality::read_tvoc(), Ok(())); | ||
| driver.set_value(100); | ||
| assert_eq!(fake::Syscalls::yield_no_wait(), YieldNoWaitReturn::NoUpcall); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn read_co2_sync() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| driver.set_value_sync(100); | ||
| assert_eq!(AirQuality::read_co2_sync(), Ok(100)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn read_tvoc_sync() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| driver.set_value_sync(100); | ||
| assert_eq!(AirQuality::read_tvoc_sync(), Ok(100)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn read_sync() { | ||
| let kernel = fake::Kernel::new(); | ||
| let driver = fake::AirQuality::new(); | ||
| kernel.add_driver(&driver); | ||
|
|
||
| driver.set_values_sync(100, 200); | ||
| assert_eq!(AirQuality::read_sync(), Ok((100, 200))) | ||
| } | ||
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.
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.