Comment code

This commit is contained in:
Theodor Teslia
2023-03-18 14:43:10 +01:00
parent e5181b23cd
commit 265245e6ef
2 changed files with 45 additions and 8 deletions

View File

@@ -5,11 +5,14 @@ namespace sta {
this->device_ = device;
this->osr_ = level;
// Reset device on start-up
this->reset();
// Read the PROM e.g. constants
this->readPROM();
}
int32_t MS5607::getPressure() {
// Get pure ADC value from the sensor
uint32_t d1 = readPressure();
// Since pressure is temp-dependant, temperature needs to be polled
@@ -22,22 +25,29 @@ namespace sta {
}
uint32_t MS5607::readPressure() {
// Request pressure value conversion
this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
delayUs(MS5607::ADC_DELAY);
uint8_t d1Arr[3];
// Request read of ADC value
uint8_t d1Arr[3];
this->device_->beginTransmission();
this->device_->receive(d1Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16);
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
int32_t MS5607::calculatePressure(uint32_t d1, int32_t dT) {
int64_t offset = (this->off << 17) + ((this->tco * dT) >> 6);
int64_t sensitivity = (this->sens << 16) + ((this->tcs * dT) >> 7);
@@ -46,28 +56,36 @@ namespace sta {
}
int32_t MS5607::getTemperature() {
// Get pure ADC value from the sensor
uint32_t d2 = readTemp();
calculateTemperature(d2);
return calculateTemperature(d2);
}
uint32_t MS5607::readTemp() {
// Request ADC conversion of temperature
this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D2_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
delayUs(MS5607::ADC_DELAY);
uint8_t d2Arr[3];
// Request ADC readout
uint8_t d2Arr[3];
this->device_->beginTransmission();
this->device_->receive(d2Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16;
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
int32_t MS5607::calculateTemperature(uint32_t d2) {
int32_t dT = d2 - (this->t_ref << 8);
int32_t temp = 2000 + ((dT * this->tempsens) >> 23);
@@ -78,6 +96,7 @@ namespace sta {
return temp;
}
// Reset sequence as described in datasheet
void MS5607::reset() {
this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::RESET);
@@ -85,6 +104,10 @@ namespace sta {
delayUs(MS5607::RESET_DELAY);
}
// Read all constants from the PROM
// May be moved to be called in reset() function in future
// Request value x -> Read value x; Then request value y etc.
// Could be optimized; Not as important since only needed once at start-up
void MS5607::readPROM() {
this->device_->beginTransmission();
@@ -121,7 +144,10 @@ namespace sta {
this->device_->endTransmission();
}
uint16_t MS5607::uint_8BufferTouint16_t(uint8_t* buffer) {
// Helper function:
// Take first bytes from buffer, swap them and store those in uint16_t
// Swap may not be necessary
uint16_t uint_8BufferTouint16_t(uint8_t* buffer) {
return (buffer[0] << 8) | buffer[1];
}
}