anyleaf 0.1.0

Driver for the AnyLeaf pH sensor
Documentation

Anyleaf

For use with the Anyleaf pH sensor

For use with embedded systems, and single-board computers.

Example for Rasperry Pi, or other Linux systems:

Cargo.toml:

[package]
name = "anyleaf_linux_example"
version = "0.1.0"
authors = ["Anyleaf <david.alan.oconnor@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "^0.2.3"
linux-embedded-hal = "^0.3.0"
anyleaf = "^0.1.0"

main.rs:

use embedded_hal::blocking::delay::DelayMs;
use linux_embedded_hal::{Delay, I2cdev};
use anyleaf::{PhSensor, CalPt, CalSlot};

fn main() {
    let i2c = I2cdev::new("/dev/i2c-1").unwrap();

    let mut ph_sensor = PhSensor::new(i2c);

    // 2 or 3 pt calibration both give acceptable results.
    // Calibrate with known values
    ph_sensor.calibrate_all(
        CalPt::new(0., 7., 25.), CalPt::new(-0.18, 4., 25.), Some(CalPt::new(0.18, 10., 25.))
    );

    // Or, call these with the sensor in the appropriate buffer solution.
    // ph_sensor.calibrate(CalSlot::One, 7.);
    // ph_sensor.calibrate(CalSlot::Two, 4.);

    // Ideally, store the calibration parameters somewhere, so they persist
    // between program runs.

    let mut delay = Delay {};

    loop {
        let pH = ph_sensor.read().unwrap();
        println!("pH: {}", pH);

        delay.delay_ms(500_u16);
    }
}

Example for Stm32F3:

Cargo.toml:

[package]
name = "anyleaf_stm32_example"
version = "0.1.0"
authors = ["Anyleaf <david.alan.oconnor@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "^0.6.2"
cortex-m-rt = "^0.6.12"
embedded-hal = "^0.2.3"
stm32f3xx-hal = { version = "^0.4.3", features=["stm32f303xc", "rt"] }
anyleaf = {version = "^0.0.1"}

main.rs:

#![no_main]
#![no_std]

//pub use cortex_m::{self, asm, iprint, iprintln, peripheral::{itm::Stim, ITM}};
use embedded_hal::blocking::delay::DelayMs;
use cortext_m::Peripherals;
use cortex_m_semihosting::hprintln;
use cortex_m_rt::entry;
//use core::fmt::{Debug, Write};
use stm32f3xx_hal as hal;
use hal::{prelude::*, delay::Delay, i2c::I2c, stm32};
use anyleaf::{PhSensor, CalPt};

//use panic_itm; // panic handler

//type Sensor:


#[entry]
fn main() -> ! {
    // Set up i2C.
    let mut cp = Peripherals::take().unwrap();
    let dp = stm32::Peripherals::take().unwrap();

    let mut rcc = dp.RCC.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let mut gpiob = dp.GPIOB.split(&mut rcc.ahb); // PB GPIO pins
    let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
    let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);

    let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 100.khz(), clocks, &mut rcc.apb1);

    let mut ph_sensor = anyleaf_ph::PhSensor::new(i2c);

    // 2 or 3 pt calibration both give acceptable results.
    // Calibrate with known values
    ph_sensor.calibrate_all(
        CalPt::new(0., 7., 25.), CalPt::new(-0.18, 4., 25.), Some(CalPt::new(0.18, 10., 25.))
    );

    // Or, call these with the sensor in the appropriate buffer solution.
    // ph_sensor.calibrate(CalSlot::One, 7.);
    // ph_sensor.calibrate(CalSlot::Two, 4.);

    // Ideally, store the calibration parameters somewhere, so they persist
    // between program runs.

    let mut delay = Delay::new(cp.SYST, clocks);

    loop {
        let pH = ph_sensor.read().unwrap();
        hprintln!("pH: {}", pH);

        delay.delay_ms(500_u16);
    }
}