Skip to content

Commit

Permalink
Implement sensor states
Browse files Browse the repository at this point in the history
  • Loading branch information
ltan10 committed Apr 19, 2024
1 parent e7cfc22 commit 1538541
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 13 deletions.
111 changes: 111 additions & 0 deletions src/SDI12Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,117 @@ bool SDI12Sensor::RuleIsIdentifyGroup(const SDI12SensorCommand_e cmd1, const SDI
}


/**
* @brief Set the sensor state based on a command structure set.
*
* @param[in] command_set Received command structure set
* @return true State was different and change was successfull
* @return false Same state as previous or command set received does not correspond
* to the sensor instance
*
* @see State(void)
* @see SetState(int8_t)
*/
bool SDI12Sensor::DefineState(const SDI12CommandSet_s command_set) {
if (command_set.address != sensor_address_) {
return false;
}
SDI12SensorState_e state = (SDI12SensorState_e)state_;
switch ((SDI12SensorCommand_e)command_set.primary) {
case kUnknown:
case kAcknowledge:
case kAddressChange:
case kAddressQuery:
case kByteDataRequest:
case kDataRequest:
state = kStateReady;
break;
case kIdentification:
// Set state to normal Ready state for normal Identify or Identify Parameter commands
// For Identify Measurement commands, set to appropriate measurement state to handle
switch (command_set.secondary) {
case kMeasurement:
state = kStateMeasurement;
break;
case kConcurrentMeasurement:
state = kStateConcurrent;
break;
case kHighVolumeASCII:
case kHighVolumeByte:
state = kStateHighMeasurement;
break;
case kVerification:
state = kStateVerify;
break;
default:
state = kStateReady;
break;
}
if (command_set.param2 >= 0) {
state = kStateReady;
}
crc_requested_ = command_set.crc_requested;
break;
case kVerification:
state = kStateVerify;
break;
case kMeasurement:
state = kStateMeasurement;
crc_requested_ = command_set.crc_requested;
break;
case kConcurrentMeasurement:
state = kStateConcurrent;
crc_requested_ = command_set.crc_requested;
break;
case kHighVolumeASCII:
// Set to same state as HighVolumeByte request
case kHighVolumeByte:
state = kStateHighMeasurement;
crc_requested_ = command_set.crc_requested;
break;
case kContinuousMeasurement:
state = kStateContinuous;
crc_requested_ = command_set.crc_requested;
break;
}

return SetState(state);
}


/**
* @brief Manually set the state of a sensor instance.
*
* @param[in] state State to set the sensor instance to.
* @return true New state is different to old state
* @return false New state is the same as the old state
*
* @see DefineState(SDI12CommandSet_s)
* @see State(void)
*/
bool SDI12Sensor::SetState(const int8_t state) {
if (state != state_) {
state_ = state;
return true;
}
return false;
}


/**
* @brief Gets the sensor current state.
*
* @return int8_t Integer or SDI12SensorState_e enumerated representation of the
* sensor state.
*
* @see DefineState(SDI12CommandSet_s)
* @see SetState(int8_t)
*/
int8_t SDI12Sensor::State(void) const {
return state_;
}


// void SDI12Sensor::SendSensorAddress() {
// char message[5];
// sprintf(message, "%c\r\n", sensor_address_);
Expand Down
32 changes: 19 additions & 13 deletions src/SDI12Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ typedef enum SDI12SensorCommand_e: uint8_t {
kByteDataRequest = 12 // aDB0~999! for high volume byte
} SDI12SensorCommand_e;

/**
* @brief Enumerated type to reference the different sensor operational state
*
*/
typedef enum SDI12SensorState_e : uint8_t {
kStateLowPower = 0, // Perform low power operations
kStateReady = 1, // General non measurement state, includes identify meta requests
kStateVerify = 2, // When received aV! or aIV!
kStateMeasurement = 3, // When received aM! or aIM!
kStateConcurrent = 4, // When received aC! or aIC!
kStateContinuous = 5, // When received aRx!
kStateHighMeasurement = 6 // When received aHA! or aIHA! or aHB! or aIHB!
} SDI12SensorState_e;

/**
* @brief References SDI12 command structure.
Expand All @@ -72,20 +85,10 @@ typedef struct SDI12CommandSet_s {
} SDI12CommandSet_s;


// enum SDI12SlaveState_e {
// LOW_POWER = 0,
// WAIT = 1,
// ACKNOWLEDGEMENT = 2,
// ADDRESS_QUERY = 3,
// ADDRESS_UPDATE = 4,
// MEASUREMENT_SINGLE = 5,
// MEASUREMENT_CONCURRENT = 6,
// MEASUREMENT_CONTINUOUS = 7,
// SENDING_DATA = 8
// } SDI12SlaveState_e;


class SDI12Sensor {
public:
int8_t state_ = kStateReady;

private:
/* Interal Variables */
static SDI12Sensor *last_set_active_object_; // Reference to the last set active SDI12Sensor object
Expand All @@ -108,6 +111,9 @@ class SDI12Sensor {
void SetCrcRequest(const bool crc_request);
bool CrcRequested(void) const;
static const SDI12CommandSet_s ParseCommand(const char* received, const char ack_address = '\0');
bool DefineState(const SDI12CommandSet_s command_set);
bool SetState(const int8_t state);
int8_t State(void) const;
// void SendSensorAddress();
// void SendSensorID();

Expand Down

0 comments on commit 1538541

Please sign in to comment.