Skip to content

Commit

Permalink
Split out utility crate for host unit testing
Browse files Browse the repository at this point in the history
Split source code into a hangman crate and a hangman_utils crate. This
allows for easy host unit testing of the utilities.
  • Loading branch information
kesyog committed Jun 1, 2024
1 parent 3339c51 commit 462f674
Show file tree
Hide file tree
Showing 44 changed files with 367 additions and 119 deletions.
File renamed without changes.
9 changes: 9 additions & 0 deletions Cargo.lock → hangman/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml → hangman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ embassy-usb = { version = "0.1", features = ["defmt"], optional = true}
embedded-alloc = "0.5"
embedded-storage = "0.3"
embedded-storage-async = "0.4"
hangman-utils = { path = "../hangman_utils" }
hex = { version = "0.4", default-features = false }
median = { version = "0.3", default-features = false }
nrf-softdevice = { version = "0.1", features = ["s113", "ble-gatt-server", "ble-peripheral", "critical-section-impl", "defmt"] }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions src/nonvolatile.rs → hangman/src/nonvolatile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,3 @@ impl Nvm {
.expect("Write to succeed");
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn addresses() {
// Ensure that all of the registers and 4-byte checksum can fit on our Flash page
assert!(4 * (RegisterRead::COUNT + 1) <= MAX_ADDR - MIN_ADDR);
}
}
File renamed without changes.
29 changes: 29 additions & 0 deletions hangman/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::pac;

pub unsafe fn disable_all_gpio_sense() {
#[cfg(feature = "nrf52840")]
{
let p1 = unsafe { &(*pac::P1::ptr()) };
for cnf in &p1.pin_cnf {
cnf.modify(|_, w| w.sense().disabled());
}
}
let p0 = unsafe { &(*pac::P0::ptr()) };
for cnf in &p0.pin_cnf {
cnf.modify(|_, w| w.sense().disabled());
}
}
4 changes: 2 additions & 2 deletions src/weight/ads1230.rs → hangman/src/weight/ads1230.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/// Ads1230 driver using embassy_nrf-friendly types
use super::{Sample, SampleProducerMut};
use crate::{blocking_hal::prelude::_embedded_hal_blocking_delay_DelayUs, util, SharedDelay};
use crate::{blocking_hal::prelude::_embedded_hal_blocking_delay_DelayUs, SharedDelay};
use embassy_nrf::gpio::{AnyPin, Input, Output};
use embassy_time::Instant;
use embassy_time::{Duration, Timer};
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'d> Ads1230<'d> {
// The ADS1230 gives a 20-bit signed reading, which is initially stored in a u32 container.
// Unsigned for sane shifting and 32-bit because there is no u20 Rust primitive. Convert it
// to a signed integer so that it is interpreted correctly.
let value = util::convert_signed_to_i32::<20>(raw_reading);
let value = hangman_utils::convert_signed_to_i32::<20>(raw_reading);
// HX711 sometimes spontaneously returns -1 (0xFFFFFF)
if value == -1 && n_skips < 3 {
n_skips += 1;
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/weight/hx711.rs → hangman/src/weight/hx711.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/// Hx711 driver using embassy_nrf-friendly types
use super::{Sample, SampleProducerMut};
use crate::{blocking_hal::prelude::_embedded_hal_blocking_delay_DelayUs, util, SharedDelay};
use crate::{blocking_hal::prelude::_embedded_hal_blocking_delay_DelayUs, SharedDelay};
use embassy_nrf::gpio::{AnyPin, Input, Output};
use embassy_time::{Duration, Instant, Timer};

Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'d> Hx711<'d> {
// The HX711 gives a 24-bit signed reading, which is initially stored in a u32 container.
// Unsigned for sane shifting and 32-bit because there is no u24 Rust primitive. Convert it
// to a signed integer so that it is interpreted correctly.
let value = util::convert_signed_to_i32::<24>(raw_reading);
let value = hangman_utils::convert_signed_to_i32::<24>(raw_reading);
// HX711 sometimes spontaneously returns -1 (0xFFFFFF)
if value == -1 && n_skips < 3 {
n_skips += 1;
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/weight/mod.rs → hangman/src/weight/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
pub mod ads1230;
pub mod average;
mod calibrate;
mod factory_calibration;
pub mod hx711;
pub mod median;
mod random;
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 7 additions & 5 deletions src/weight/task.rs → hangman/src/weight/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use super::calibrate::Calibrator;
use super::factory_calibration::{self, CalPoint, TwoPoint};
use super::tare::Tarer;
#[cfg(feature = "nrf52832")]
use super::Ads1230;
Expand All @@ -25,6 +24,7 @@ use crate::MeasureCommandReceiver;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_time::{Duration, Instant, Timer};
use hangman_utils::two_point_cal::{self, CalPoint, TwoPoint};
use nrf_softdevice::Softdevice;
use static_cell::make_static;

Expand All @@ -51,7 +51,7 @@ struct MeasurementContext {
calibrator: &'static SharedCalibrator,
tarer: Tarer<&'static SharedCalibrator>,
nvm: Nvm,
factory_cal: TwoPoint,
factory_cal: TwoPoint<RawReading>,
}

async fn handle_command(cmd: Command, context: &mut MeasurementContext, adc: &SharedAdc) {
Expand Down Expand Up @@ -113,11 +113,13 @@ async fn handle_command(cmd: Command, context: &mut MeasurementContext, adc: &Sh
}
let Sample { value, .. } = context.median.sample().await;
let reading = filter.add_sample(value).unwrap();
context.factory_cal.add_point(CalPoint { weight, reading });
context.factory_cal.add_point(CalPoint {
expected_value: weight,
measured_value: reading,
});
}
Command::SaveCalibration => {
if let Some(factory_calibration::Constants { m, b }) =
context.factory_cal.get_cal_constants()
if let Some(two_point_cal::Constants { m, b }) = context.factory_cal.get_cal_constants()
{
defmt::info!("New calibration: m = {=f32} b = {=i32}", m, b);
super::write_calibration(&mut context.nvm, m, b).await;
Expand Down
159 changes: 159 additions & 0 deletions hangman_utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions hangman_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hangman-utils"
version = "0.1.0"
edition = "2021"

[dependencies]
defmt = { version = "0.3" }
num-traits = { version = "0.2", default-features = false }
23 changes: 6 additions & 17 deletions src/util.rs → hangman_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,21 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::pac;
#![cfg_attr(not(test), no_std)]
#![forbid(unsafe_op_in_unsafe_fn)]

pub unsafe fn disable_all_gpio_sense() {
#[cfg(feature = "nrf52840")]
{
let p1 = unsafe { &(*pac::P1::ptr()) };
for cnf in &p1.pin_cnf {
cnf.modify(|_, w| w.sense().disabled());
}
}
let p0 = unsafe { &(*pac::P0::ptr()) };
for cnf in &p0.pin_cnf {
cnf.modify(|_, w| w.sense().disabled());
}
}
#[macro_use]
pub mod log;
pub mod two_point_cal;

/// Convert a signed integer in a u32 container to a signed integer
pub const fn convert_signed_to_i32<const BITS: u32>(mut input: u32) -> i32 {
Expand All @@ -38,8 +29,6 @@ pub const fn convert_signed_to_i32<const BITS: u32>(mut input: u32) -> i32 {
input as i32
}

// TODO: figure out how to actually run these tests on host
// I promise I ran them in the playground.
#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 462f674

Please sign in to comment.