Skip to content

Commit

Permalink
refactor(include): reduce the amount the active mode command is sent …
Browse files Browse the repository at this point in the history
…to the pressure driver (#667)
  • Loading branch information
Laura-Danielle authored May 16, 2023
1 parent a5a18fd commit 26b7900
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 120 deletions.
12 changes: 4 additions & 8 deletions include/sensors/core/tasks/pressure_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class MMR920C04 {
if (!reset(_registers.reset)) {
return false;
}
// default measurement mode is MODE 4. 1 sample every 3.21 msec.
set_measure_mode(measurement_mode_rate);
_initialized = true;
return true;
}
Expand All @@ -118,7 +120,7 @@ class MMR920C04 {
command_data = build_register_command(_registers.pressure_command);
}
total_baseline_reads = number_reads;
set_measure_mode(measurement_mode_rate);

poller.single_register_poll(
mmr920C04::ADDRESS, command_data, 3, number_reads,
mode_delay_with_buffer, own_queue,
Expand All @@ -133,7 +135,6 @@ class MMR920C04 {
build_register_command(_registers.temperature_command);
total_baseline_reads = number_reads;

set_measure_mode(measurement_mode_rate);
poller.single_register_poll(
mmr920C04::ADDRESS, command_data, 3, number_reads,
mode_delay_with_buffer, own_queue,
Expand All @@ -150,7 +151,6 @@ class MMR920C04 {
command_data = build_register_command(_registers.pressure_command);
}

set_measure_mode(measurement_mode_rate);
poller.continuous_single_register_poll(
mmr920C04::ADDRESS, command_data, 3, mode_delay_with_buffer,
own_queue, utils::build_id(mmr920C04::ADDRESS, command_data, tags));
Expand All @@ -163,7 +163,6 @@ class MMR920C04 {
auto command_data =
build_register_command(_registers.temperature_command);

set_measure_mode(measurement_mode_rate);
poller.continuous_single_register_poll(
mmr920C04::ADDRESS, command_data, 3, mode_delay_with_buffer,
own_queue, utils::build_id(mmr920C04::ADDRESS, command_data, tags));
Expand Down Expand Up @@ -269,7 +268,6 @@ class MMR920C04 {
if (!bind_sync && !echoing) {
auto reg_id = utils::reg_from_id<mmr920C04::Registers>(m.id.token);
stop_continuous_polling(m.id.token, static_cast<uint8_t>(reg_id));
reset_readings();
}

// Pressure is always a three-byte value
Expand Down Expand Up @@ -308,7 +306,6 @@ class MMR920C04 {
if (!bind_sync && !echoing) {
auto reg_id = utils::reg_from_id<mmr920C04::Registers>(m.id.token);
stop_continuous_polling(m.id.token, static_cast<uint8_t>(reg_id));
reset_readings();
}

// Pressure is always a three-byte value
Expand Down Expand Up @@ -349,7 +346,6 @@ class MMR920C04 {
if (!m.id.is_completed_poll) {
return;
}
reset_readings();

auto current_pressure_baseline_pa =
pressure_running_total / total_baseline_reads;
Expand Down Expand Up @@ -396,7 +392,7 @@ class MMR920C04 {
if (!m.id.is_completed_poll) {
return;
}
reset_readings();

auto current_temperature_baseline =
temperature_running_total / total_baseline_reads;
auto offset_fixed_point =
Expand Down
2 changes: 1 addition & 1 deletion include/sensors/core/tasks/pressure_sensor_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PressureMessageHandler {
-> PressureMessageHandler & = delete;
auto operator=(const PressureMessageHandler &&)
-> PressureMessageHandler && = delete;
~PressureMessageHandler() = default;
~PressureMessageHandler() { driver.reset_readings(); }

void handle_message(const utils::TaskMessage &m) {
std::visit([this](auto o) { this->visit(o); }, m);
Expand Down
61 changes: 5 additions & 56 deletions sensors/tests/test_pressure_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,12 @@ SCENARIO("Testing the pressure sensor driver") {
.read_buffer = {0x3D, 0x09, 0x00}}; // 40 cmH20
WHEN("the the limited poll is initiated") {
driver.poll_limited_pressure(1, tags_as_int);
THEN(
"the i2c queue is populated with a MEASURE_MODE command and "
"the poller queue is populated with a PRESSURE_READ") {
REQUIRE(i2c_queue.get_size() == 1);
THEN("the poller queue is populated with a PRESSURE_READ") {
REQUIRE(i2c_queue.get_size() == 0);
REQUIRE(i2c_poll_queue.get_size() == 1);
auto mode_command =
get_message<i2c::messages::Transact>(i2c_queue);
auto poller_command =
get_message<i2c::messages::SingleRegisterPollRead>(
i2c_poll_queue);
REQUIRE(mode_command.transaction.write_buffer[0] ==
static_cast<uint8_t>(
sensors::mmr920C04::Registers::MEASURE_MODE_4));
REQUIRE(
poller_command.first.write_buffer[0] ==
static_cast<uint8_t>(
Expand Down Expand Up @@ -153,14 +146,6 @@ SCENARIO("Testing the pressure sensor driver") {
.read_buffer = {0x3D, 0x09, 0x00}}; // 40 cmH20
WHEN("a completed poll is sent to handle baseline response") {
driver.handle_baseline_pressure_response(message);
THEN("the i2c queue is populated with a RESET command") {
REQUIRE(i2c_queue.get_size() == 1);
auto reset_command =
get_message<i2c::messages::Transact>(i2c_queue);
REQUIRE(
reset_command.transaction.write_buffer[0] ==
static_cast<uint8_t>(sensors::mmr920C04::Registers::RESET));
}
THEN("the can queue is populated with a baseline sensor request") {
can_queue.try_read(&empty_can_msg);
REQUIRE(std::holds_alternative<
Expand All @@ -181,20 +166,12 @@ SCENARIO("Testing the pressure sensor driver") {
auto tags_as_int = sensors::utils::byte_from_tags(tags);
WHEN("the continuous poll function is called") {
driver.poll_continuous_pressure(tags_as_int);
THEN(
"the i2c queue is populated with a MEASURE_MODE_4 and "
"a READ_PRESSURE command only") {
REQUIRE(i2c_queue.get_size() == 1);
THEN("a READ_PRESSURE command only") {
REQUIRE(i2c_queue.get_size() == 0);
REQUIRE(i2c_poll_queue.get_size() == 1);
auto mode_command =
get_message<i2c::messages::Transact>(i2c_queue);
auto read_command = get_message<
i2c::messages::ConfigureSingleRegisterContinuousPolling>(
i2c_poll_queue);
REQUIRE(mode_command.transaction.write_buffer[0] ==
static_cast<uint8_t>(
sensors::mmr920C04::Registers::MEASURE_MODE_4));
;
REQUIRE(
read_command.first.write_buffer[0] ==
static_cast<uint8_t>(
Expand Down Expand Up @@ -232,16 +209,8 @@ SCENARIO("Testing the pressure sensor driver") {
auto tags_as_int = sensors::utils::byte_from_tags(tags);
WHEN("a limited poll for 3 reads is set") {
driver.poll_limited_pressure(3, tags_as_int);
THEN(
"the i2c queue receives a MEASURE_MODE_4 command and a "
"pressure read poll") {
REQUIRE(i2c_queue.get_size() == 1);
THEN("a pressure read poll") {
REQUIRE(i2c_poll_queue.get_size() == 1);
auto mode_command =
get_message<i2c::messages::Transact>(i2c_queue);
REQUIRE(mode_command.transaction.write_buffer[0] ==
static_cast<uint8_t>(
sensors::mmr920C04::Registers::MEASURE_MODE_4));
auto read_command =
get_message<i2c::messages::SingleRegisterPollRead>(
i2c_poll_queue);
Expand All @@ -253,26 +222,6 @@ SCENARIO("Testing the pressure sensor driver") {
REQUIRE(read_command.polling == 3);
}
}
WHEN("The limited finishes a RESET command is sent") {
auto id = i2c::messages::TransactionIdentifier{
.token = sensors::utils::build_id(
sensors::mmr920C04::ADDRESS,
static_cast<uint8_t>(
sensors::mmr920C04::Registers::LOW_PASS_PRESSURE_READ),
tags_as_int),
.is_completed_poll = true,
.transaction_index = static_cast<uint8_t>(0)};
auto sensor_response = i2c::messages::TransactionResponse{
.id = id, .bytes_read = 3, .read_buffer = {0xFF, 0xFF, 0xFF}};
driver.handle_baseline_pressure_response(sensor_response);
THEN("a RESET command is sent") {
auto reset_command =
get_message<i2c::messages::Transact>(i2c_queue);
REQUIRE(
reset_command.transaction.write_buffer[0] ==
static_cast<uint8_t>(sensors::mmr920C04::Registers::RESET));
}
}
}

GIVEN("a baseline sensor request with 10 reads is processed") {
Expand Down
55 changes: 0 additions & 55 deletions sensors/tests/test_pressure_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,61 +58,6 @@ SCENARIO("Receiving messages through the pressure sensor message handler") {
auto sensor = sensors::tasks::PressureMessageHandler{
writer, poller, queue_client, response_queue, mock_hw, sensor_id};

GIVEN("a request to take a single read of the pressure sensor") {
auto single_read =
sensors::utils::TaskMessage(can::messages::ReadFromSensorRequest(
{}, 0xdeadbeef, pressure_id, sensor_id_int));
sensor.handle_message(single_read);
WHEN("the handler function receives the message") {
THEN("the i2c queue is populated with a MEASURE MODE 4 command") {
REQUIRE(i2c_queue.get_size() == 1);
}
auto transact_message =
get_message<i2c::messages::Transact>(i2c_queue);
REQUIRE(transact_message.transaction.address ==
sensors::mmr920C04::ADDRESS);
}
}
GIVEN("a request to take multiple reads of the pressure sensor") {
auto single_read =
sensors::utils::TaskMessage(can::messages::BaselineSensorRequest(
{}, 0xdeadbeef, pressure_id, sensor_id_int));
sensor.handle_message(single_read);
WHEN("the handler function receives the message") {
THEN("the i2c queue is populated with a MEASURE MODE 4 command") {
REQUIRE(i2c_queue.get_size() == 1);
}
auto transact_message =
get_message<i2c::messages::Transact>(i2c_queue);
REQUIRE(transact_message.transaction.address ==
sensors::mmr920C04::ADDRESS);
}
}

GIVEN("a request to take a single read of the temperature sensor") {
auto single_read =
sensors::utils::TaskMessage(can::messages::ReadFromSensorRequest(
{}, 0xdeadbeef, pressure_temperature_id, sensor_id_int));
sensor.handle_message(single_read);
WHEN("the handler function receives the message") {
THEN("the i2c queue is populated with a transact command") {
REQUIRE(i2c_queue.get_size() == 1);
}
AND_WHEN("we read the message from the queue") {
auto transact_message =
get_message<i2c::messages::Transact>(i2c_queue);

THEN("The command addresses are correct") {
REQUIRE(transact_message.transaction.address ==
sensors::mmr920C04::ADDRESS);
REQUIRE(transact_message.transaction.write_buffer[0] ==
static_cast<uint8_t>(
sensors::mmr920C04::Registers::MEASURE_MODE_4));
}
}
}
}

GIVEN("A TransactionResponse message") {
can_queue.reset();
i2c::messages::TransactionResponse response_details{
Expand Down

0 comments on commit 26b7900

Please sign in to comment.