Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dvorak committed Aug 3, 2024
1 parent ed2e784 commit 4bb61e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 71 deletions.
86 changes: 20 additions & 66 deletions src/drivers/hygrometer/sht4x/sht4x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ SHT4X::SHT4X(const I2CSPIDriverConfig &config) :
}


uint8_t SHT4X::calc_crc(uint8_t data[2])
uint8_t
SHT4X::calc_crc(uint8_t data[2])
{
uint8_t crc = 0xFF;

Expand All @@ -71,7 +72,8 @@ uint8_t SHT4X::calc_crc(uint8_t data[2])
}


uint8_t SHT4X::validate_crc(uint8_t data[6])
uint8_t
SHT4X::validate_crc(uint8_t data[6])
{

uint8_t crc_err = 0;
Expand All @@ -91,33 +93,10 @@ uint8_t SHT4X::validate_crc(uint8_t data[6])



// int SHT4X::read_data(uint8_t command, uint8_t *data_ptr, uint8_t length)
// {

// uint8_t raw_data[length/2*3];
// transfer(&command, 1, &raw_data[0], length/2*3);
// for (int i = 0; i < length/2*3; i++) {
// PX4_INFO("> %x", raw_data[i]);
// }

// uint8_t crc_err = 0;
// for (int i = 0; i < length / 3; ++i) {
// uint8_t crc_data[2] = {raw_data[i * 3], raw_data[i * 3 + 1]};

// if (raw_data[i * 3 + 2] != calc_crc(crc_data)) {
// crc_err ++;
// }

// *(data_ptr + i * 2) = raw_data[i * 3];
// *(data_ptr + i * 2 + 1) = raw_data[i * 3 + 1];
// }

// return crc_err;
// }



uint8_t SHT4X::read_serialnumber()
uint8_t
SHT4X::read_serialnumber()
{
uint8_t data[6];
uint8_t addr = SHT4x_CMD_READ_SERIAL;
Expand All @@ -136,7 +115,8 @@ uint8_t SHT4X::read_serialnumber()



uint8_t SHT4X::sensor_compouse_msg(bool send)
uint8_t
SHT4X::sensor_compouse_msg(bool send)
{

uint8_t data[6];
Expand Down Expand Up @@ -183,7 +163,8 @@ SHT4X::probe()
return read_serialnumber();
}

int SHT4X::init()
int
SHT4X::init()
{
if (I2C::init() != PX4_OK) {
return PX4_ERROR;
Expand All @@ -198,54 +179,39 @@ int SHT4X::init()



int SHT4X::init_sensor()
int
SHT4X::init_sensor()
{
//probe();

PX4_INFO("Connected to SHT4x sensor, SN: %ld", _sht_info.serial_number);
PX4_INFO("Last values, T: %.3f, H: %.3f", (double)measured_temperature, (double)measured_humidity);

return PX4_OK;
}

void SHT4X::RunImpl()
void
SHT4X::RunImpl()
{
PX4_INFO("SHT4X: RunImpl");
sensor_compouse_msg(true);
}


// void
// SHT4X::action_cli(const BusCLIArguments &cli)
// {
// switch (cli.custom1) {

// case 1: {
// PX4_INFO("Last measured values (%.3fs ago, #%d)", (double)(hrt_absolute_time() - measurement_time) / 1000000.0,
// measurement_index);
// PX4_INFO("Temp: %.3f, Hum: %.3f", (double)measured_temperature, (double)measured_humidity);

// }
// break;

// }
// }



void SHT4X::print_status()
void
SHT4X::print_status()
{
PX4_INFO("SHT4X sensor");
I2CSPIDriverBase::print_status();
PX4_INFO("SN: %ld", _sht_info.serial_number);
PX4_INFO("Last measured values (%.3fs ago, #%d)", (double)(hrt_absolute_time() - measurement_time) / 1000000.0,
measurement_index);
PX4_INFO("Temp: %.3f, Hum: %.3f", (double)measured_temperature, (double)measured_humidity);

}


void SHT4X::print_usage()
void
SHT4X::print_usage()
{
PRINT_MODULE_DESCRIPTION(
R"DESCR_STR(
Expand All @@ -260,12 +226,6 @@ CLI usage example:
$ sht4x status
Print driver status
$ sht4x values
Print last measured values
$ sht4x reset
Reinitialize senzor, reset flags
)DESCR_STR");

PRINT_MODULE_USAGE_NAME("sht4x", "driver");
Expand All @@ -277,11 +237,10 @@ CLI usage example:
PRINT_MODULE_USAGE_PARAMS_I2C_KEEP_RUNNING_FLAG();
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();

PRINT_MODULE_USAGE_COMMAND_DESCR("values", "Print actual data");

}

int sht4x_main(int argc, char *argv[])
int
sht4x_main(int argc, char *argv[])
{
using ThisDriver = SHT4X;
BusCLIArguments cli{true, false};
Expand Down Expand Up @@ -310,11 +269,6 @@ int sht4x_main(int argc, char *argv[])
return ThisDriver::module_status(iterator);
}

// if (!strcmp(verb, "values")) {
// cli.custom1 = 1;
// return ThisDriver::action_cli(cli, iterator, false);
// }


ThisDriver::print_usage();
return -1;
Expand Down
6 changes: 1 addition & 5 deletions src/drivers/hygrometer/sht4x/sht4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ class SHT4X : public device::I2C, public ModuleParams, public I2CSPIDriver<SHT4X

//void action_cli(const BusCLIArguments &cli);


//int read_data(uint8_t command, uint8_t *data_ptr, uint8_t length);
int write_data(uint8_t command, uint8_t buffer[], uint8_t length);
//int write_data(uint8_t command, uint8_t buffer[], uint8_t length);

uint8_t read_serialnumber();

Expand All @@ -108,8 +106,6 @@ class SHT4X : public device::I2C, public ModuleParams, public I2CSPIDriver<SHT4X
uint16_t measurement_index = 0;

sht_info _sht_info;
//int _state = sht4x_state::INIT;
//int _last_state = sht4x_state::INIT;
uint32_t _time_in_state = hrt_absolute_time();
uint16_t _last_command = 0;
uORB::PublicationMulti<sensor_hygrometer_s> _sensor_hygrometer_pub{ORB_ID(sensor_hygrometer)};
Expand Down

0 comments on commit 4bb61e6

Please sign in to comment.