|
| 1 | +/*! |
| 2 | + * @file WipperSnapper_I2C_Driver_TMP119.h |
| 3 | + * |
| 4 | + * Device driver for the TMP119 high-accuracy temperature sensor. |
| 5 | + * |
| 6 | + * Adafruit invests time and resources providing this open source code, |
| 7 | + * please support Adafruit and open-source hardware by purchasing |
| 8 | + * products from Adafruit! |
| 9 | + * |
| 10 | + * Copyright (c) Tyeth Gundry 2025 for Adafruit Industries. |
| 11 | + * |
| 12 | + * MIT license, all text here must be included in any redistribution. |
| 13 | + * |
| 14 | + */ |
| 15 | +#ifndef WipperSnapper_I2C_Driver_TMP119_H |
| 16 | +#define WipperSnapper_I2C_Driver_TMP119_H |
| 17 | + |
| 18 | +#include "WipperSnapper_I2C_Driver.h" |
| 19 | +#include <Adafruit_TMP119.h> |
| 20 | + |
| 21 | +/**************************************************************************/ |
| 22 | +/*! |
| 23 | + @brief Class that provides a driver interface for the TMP119 sensor. |
| 24 | +*/ |
| 25 | +/**************************************************************************/ |
| 26 | +class WipperSnapper_I2C_Driver_TMP119 : public WipperSnapper_I2C_Driver { |
| 27 | + |
| 28 | +public: |
| 29 | + /*******************************************************************************/ |
| 30 | + /*! |
| 31 | + @brief Constructor for a TMP119 sensor. |
| 32 | + @param i2c |
| 33 | + The I2C interface. |
| 34 | + @param sensorAddress |
| 35 | + 7-bit device address. |
| 36 | + */ |
| 37 | + /*******************************************************************************/ |
| 38 | + WipperSnapper_I2C_Driver_TMP119(TwoWire *i2c, uint16_t sensorAddress) |
| 39 | + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { |
| 40 | + _i2c = i2c; |
| 41 | + _sensorAddress = sensorAddress; |
| 42 | + } |
| 43 | + |
| 44 | + /*******************************************************************************/ |
| 45 | + /*! |
| 46 | + @brief Destructor for a TMP119 sensor. |
| 47 | + */ |
| 48 | + /*******************************************************************************/ |
| 49 | + ~WipperSnapper_I2C_Driver_TMP119() { delete _tmp119; } |
| 50 | + |
| 51 | + /*******************************************************************************/ |
| 52 | + /*! |
| 53 | + @brief Initializes the TMP119 sensor and begins I2C. |
| 54 | + @returns True if initialized successfully, False otherwise. |
| 55 | + */ |
| 56 | + /*******************************************************************************/ |
| 57 | + bool begin() { |
| 58 | + _tmp119 = new Adafruit_TMP119(); |
| 59 | + if (!_tmp119->begin((uint8_t)_sensorAddress, _i2c)) |
| 60 | + return false; |
| 61 | + // Pin defaults explicitly so future library changes don't silently |
| 62 | + // alter WipperSnapper behavior. |
| 63 | + _tmp119->setMeasurementMode(TMP117_MODE_CONTINUOUS); |
| 64 | + _tmp119->setAveragedSampleCount(TMP117_AVERAGE_8X); |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + /*******************************************************************************/ |
| 69 | + /*! |
| 70 | + @brief Gets the TMP119's current temperature. |
| 71 | + @param tempEvent |
| 72 | + Pointer to an Adafruit_Sensor event. |
| 73 | + @returns True if the temperature was obtained successfully, False |
| 74 | + otherwise. |
| 75 | + */ |
| 76 | + /*******************************************************************************/ |
| 77 | + bool getEventAmbientTemp(sensors_event_t *tempEvent) { |
| 78 | + if (!_readSensor()) |
| 79 | + return false; |
| 80 | + *tempEvent = _cachedTemp; |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | +protected: |
| 85 | + Adafruit_TMP119 *_tmp119 = nullptr; ///< TMP119 driver object |
| 86 | + unsigned long _lastRead = 0; ///< Last sensor read time in ms |
| 87 | + sensors_event_t _cachedTemp = {0}; ///< Cached temperature event |
| 88 | + |
| 89 | + /*******************************************************************************/ |
| 90 | + /*! |
| 91 | + @brief Reads the TMP119 sensor data, caching the result so only |
| 92 | + the first call per cycle performs the I2C transaction. |
| 93 | + @returns True if sensor data is available, False otherwise. |
| 94 | + */ |
| 95 | + /*******************************************************************************/ |
| 96 | + bool _readSensor() { |
| 97 | + if (_lastRead != 0 && millis() - _lastRead < 1000) |
| 98 | + return true; // use cached value |
| 99 | + if (!_tmp119->getEvent(&_cachedTemp)) |
| 100 | + return false; |
| 101 | + _lastRead = millis(); |
| 102 | + return true; |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +#endif // WipperSnapper_I2C_Driver_TMP119_H |
0 commit comments