capsules_extra

Module distance

Source
Expand description

Provides userspace with access to distance sensor.

§Userspace Interface

§subscribe System Call

The subscribe system call supports the single subscribe_number zero, which is used to provide a callback that will return back the result of a distance sensor reading. The subscribe call return codes indicate the following:

  • Ok(()): the callback has been successfully been configured.
  • ENOSUPPORT: Invalid subscribe_number.
  • NOMEM: No sufficient memory available.
  • INVAL: Invalid address of the buffer or other error.

§command System Call

The command system call supports one argument cmd which is used to specify the specific operation. Currently, the following commands are supported:

  • 0: check whether the driver exists.
  • 1: read the distance.
  • 2: get the minimum distance that the sensor can measure based on the datasheet, in millimeters.
  • 3: get the maximum distance that the sensor can measure based on the datasheet, in millimeters.

The possible returns from the command system call indicate the following:

  • Ok(()): The operation has been successful.
  • NOACK: No acknowledgment was received from the sensor during distance measurement.
  • INVAL: Invalid measurement, such as when the object is out of range or no valid echo is received.
  • ENOSUPPORT: Invalid cmd.
  • NOMEM: Insufficient memory available.
  • INVAL: Invalid address of the buffer or other error.

The upcall has the following parameters:

  • 0: Indicates a successful distance measurement, with the second parameter containing the distance, in millimeters.
  • Non-zero: Indicates an error, with the first parameter containing the error code, and the second parameter being 0.

Components for the distance sensor.

§Usage

You need a device that provides the hil::sensors::Distance trait. Here is an example of how to set up a distance sensor with the HC-SR04.

use components::hcsr04::HcSr04Component;
let trig_pin = peripherals.pins.get_pin(RPGpio::GPIO4);
let echo_pin = peripherals.pins.get_pin(RPGpio::GPIO5);

let distance_sensor = components::hcsr04::HcSr04Component::new(
    mux_alarm,
    trig_pin,
    echo_pin
).finalize(components::hcsr04_component_static!());

distance_sensor.set_client(distance_sensor_client);

Structs§

Constants§