Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Science/src/.DS_Store
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try adding .DS_STORE to your global gitignore

Binary file not shown.
45 changes: 35 additions & 10 deletions Science/src/temp_humidity/temp_humidity.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
#include "temp_humidity.h"
#define REG 0x00
#define ADDR 0x40

TempHumiditySensor::TempHumiditySensor() : sensor(&Wire, 0x44, 4) { }
void TempHumiditySensor::readReg(){
Wire.beginTransmission(ADDR);
// Rewrote with overloaded method so that you don't need to use & on a literal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the reason we used &Wire instead of just Wire is that there are actually several different Wire objects, one for each I2C chip on the Teensy itself -- Wire, Wire1, and Wire3. Depending on which pins the electrical team decides to use, a different chip may be needed.

In the past, we've found it tricky to switch back and forth (eg for testing or debugging) because we were using Wire all over the file. So we used a reference to a wire object to allow passing in different wire objects and be sure they all changed at once. Also acceptable is #define WIRE Wire and then that can be changed as well.

Wire.write(REG);
uint8_t code = Wire.endTransmission();
if(code != 0){
// Wiping buffer if there is an error
memset(buf, 0, 4);
Serial.print("Error: Could not read temperature/humidity (Error code ");
Serial.print(code);
Serial.println(")");
return;
}

void TempHumiditySensor::setup() {
delay(20);

// Requesting 4 bytes to get data from registers 0x00 and 0x01 and putting it in buf[0:4]
Wire.requestFrom(ADDR, 4);
for(int i = 0; i < 4; i++){
buf[i] = Wire.read();
}
return;
}

void TempHumiditySensor::setup(){
Serial.print("Initializing temp/humidity sensor...");
while (sensor.begin() != 0) {
while (Wire.begin() != 0) {
Serial.print(".");
delay(500);
}
Serial.println(" Done!");
if (!sensor.softReset()) {
Serial.println("Failed to Initialize the chip....");
}
}

float TempHumiditySensor::getTemperature() {
return sensor.getTemperatureC();
float TempHumiditySensor::getTemperature(){
readReg();
uint16_t temp = buf[0] << 8 | buf[1];
return ((float) temp * 165 / 65535.0) - 40.0;
Comment on lines +61 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a style choice, but consider this: getTemperature() and getHumidity() are probably going to be called in the same loop() function, and so readReg() will be run twice in a row. Ignoring the delays, it's unlikely that the sensor itself will have new data in such a short time.

Our other classes have a void update() method -- something meant to be run once per loop(). Maybe you can put readReg() in there, and save the temperature and humidity into their own fields. Then, when these functions are called, just return the last read value from the fields directly.

It's a bit of a trade-off from efficiency vs real-time accuracy, so your call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the feedback! I will implement all of these changes. As for the .begin() error, my code is an adaptation of example code that was provided to me, so I definitely should've looked into the function definitions a little more thoroughly!

}

float TempHumiditySensor::getHumidity() {
return sensor.getHumidityRH();
float TempHumiditySensor::getHumidity(){
readReg();
uint16_t hum = buf[2] << 8 | buf[3];
return ((float) hum / 65535.0) * 100;
}
16 changes: 9 additions & 7 deletions Science/src/temp_humidity/temp_humidity.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once
#ifndef TEMP_HUMIDITY_H
#define TEMP_HUMIDITY_H

#include <Arduino.h>
#include "dfrobot/DFRobot_SHT3x.h"
#include "Wire.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important for this PR, but in general, you can have two "categories" of includes:

  • libraries that contains types you are using in your API (include in your .h file)
  • libraries that contain code you'll need to implement your logic (include in your cpp file)

Basically, think of it as "do others who use this file also need this library?" If so, the include goes into the header file. If not, then it can just be in the ,cpp file. So in this case, you can move the include to the .cpp file.

Again, it doesn't actually break or change any functionality, but it's good practice as it means that including one header gives you the least amount of transitive dependencies.


class TempHumiditySensor {
class TempHumiditySensor{
private:
DFRobot_SHT3x sensor;

uint8_t buf[4] = {0};
void readReg();

public:
TempHumiditySensor();

void setup();
float getTemperature();
float getHumidity();
};

#endif