1919 @brief DS18X20Hardware constructor
2020 @param onewire_pin
2121 The OneWire bus pin to use.
22+ @param sensor_num
23+ Unique identifier for the sensor driver.
2224*/
2325/* **********************************************************************/
24- DS18X20Hardware::DS18X20Hardware (uint8_t onewire_pin) : _drv_therm(_ow) {
26+ DS18X20Hardware::DS18X20Hardware (uint8_t onewire_pin, int sensor_num)
27+ : _drv_therm(_ow) {
2528 is_read_temp_c = false ;
2629 is_read_temp_f = false ;
30+ _sensor_num = sensor_num;
2731 // Initialize the OneWire bus object
2832 _onewire_pin = onewire_pin;
29- new (&_ow) OneWireNg_CurrentPlatform (onewire_pin, false );
3033}
3134
3235/* **********************************************************************/
@@ -39,13 +42,40 @@ DS18X20Hardware::~DS18X20Hardware() {
3942 INPUT); // Set the pin to hi-z and release it for other uses
4043}
4144
42- /* **********************************************************************/
45+ /* ************************************************************************** * /
4346/* !
44- @brief Get the sensor's ID
45- @returns True if the sensor was successfully identified, False otherwise.
47+ @brief Initializes the DS18X20 sensor driver and verifies that the
48+ sensor is present on the OneWire bus.
49+ @returns True if the sensor was successfully initialized, False
50+ otherwise.
4651*/
47- /* **********************************************************************/
52+ /* ************************************************************************** * /
4853bool DS18X20Hardware::GetSensor () {
54+ // Initialize the DS18X20 driver
55+ #ifdef ARDUINO_ARCH_RP2040
56+ // RP2040 is special - it uses the PIO's rather than the standard bitbang
57+ // implementation In this implementation, we select the PIO instance based on
58+ // driver identifier
59+
60+ // NOTE: We are only going to allow *up to* 7 DS18x20 sensors on RP2040
61+ // because the status pixel requires a PIO SM to be allocated prior.
62+ int pio_num;
63+ if (_sensor_num <= 4 ) {
64+ // drivers 0 thru 3 are handled by PIO0/SM0..4
65+ new (&_ow) OneWireNg_CurrentPlatform (_onewire_pin, false , 0 );
66+ } else if (_sensor_num <= 7 ) {
67+ // drivers 5 thru 8 are handled by PIO1/SM1..4
68+ new (&_ow) OneWireNg_CurrentPlatform (_onewire_pin, false , 1 );
69+ } else {
70+ WS_DEBUG_PRINTLN (
71+ " [ds18x20] ERROR: You may only add up to 7 sensors on RP2040!" );
72+ return false ;
73+ }
74+ #else
75+ // Initialize the standard bit-banged DS18X20 driver object
76+ new (&_ow) OneWireNg_CurrentPlatform (_onewire_pin, false );
77+ #endif
78+
4979 OneWireNg::ErrorCode ec = _ow->readSingleId (_sensorId);
5080 return ec == OneWireNg::EC_SUCCESS;
5181}
0 commit comments