libraries / Mitov / Mitov_Maxim_OneWire_Thermometer.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_MAXIM_ONEWIRE_THERMOMETER_h
  11#define _MITOV_MAXIM_ONEWIRE_THERMOMETER_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class OneWireThermometer : public Mitov::CommonSource, public Mitov::ClockingSupport
  18        {
  19                typedef Mitov::CommonSource inherited;
  20
  21        public:
  22                OpenWire::SinkPin       OneWireInputPin;
  23                OpenWire::SourcePin     OneWireOutputPin;
  24
  25                OpenWire::SourcePin     AddressOutputPin;
  26
  27                OpenWire::SourcePin     AlarmOutputPin;
  28
  29        public:
  30                bool    AutoDiscover : 1;
  31                bool    InFahrenheit : 1;
  32                uint8_t Resolution = 9;
  33
  34                float   AlarmHighTemperature = 100.0f;
  35                float   AlarmLowTemperature = 0.0f;
  36
  37        public:
  38                void SetResolution( uint8_t AValue )
  39                {
  40                        if( Resolution == AValue )
  41                                return;
  42
  43                        Resolution = AValue;
  44                        if( FThermometer )
  45                                FThermometer->setResolution( FAddress, Resolution );
  46                }
  47
  48        protected:
  49//              bool    FIsChained;
  50                int             FDigitalPin;
  51                int     FIndex;
  52//              float   FCurrentValue;
  53
  54        protected:
  55                OneWire                         *FOneWire = nullptr;
  56                // Migrate to direct OpenWire with DallasTemperature rewrite!
  57                DallasTemperature       *FThermometer = nullptr;
  58                DeviceAddress           FAddress;
  59
  60        protected:
  61                virtual void SystemInit()
  62                {
  63                        if( FIndex )
  64                        {
  65//                              OneWireOutputPin.Notify( &FOneWire );
  66                                // Migrate to OpenWire with DallasTemperature rewrite!
  67                                OneWireOutputPin.Notify( &FThermometer );
  68                        }
  69
  70                        else
  71                        {
  72                                GetOneWire();
  73
  74                                FThermometer = new DallasTemperature( FOneWire );
  75                                FThermometer->begin();
  76                        }
  77
  78                        if( AutoDiscover )
  79                                FThermometer->getAddress( FAddress, FIndex );
  80
  81                        FThermometer->setResolution( FAddress, Resolution );
  82
  83
  84/*
  85                        if( FIndex )
  86                        {
  87                                Serial.println( FThermometer->getDeviceCount() );
  88                                Serial.println( FIndex );
  89                                for (uint8_t i = 0; i < 8; i++)
  90                                        Serial.println( FAddress[ i ] );
  91                        }
  92*/
  93                        inherited::SystemInit();
  94                }
  95
  96                virtual void SystemStart()
  97                {
  98                        if( FIndex == 0 )
  99                        {
 100                                FThermometer->setCheckForConversion( true );
 101                                FThermometer->requestTemperatures();
 102                                FThermometer->setCheckForConversion( false );
 103                        }
 104
 105                        if( AddressOutputPin.IsConnected() )
 106                        {
 107/*
 108                                String AAddress = String( FAddress[ 0 ], HEX );
 109                                for( int i = 1; i < 8; ++i )
 110                                        AAddress += '-' + String( FAddress[ i ], HEX );
 111
 112                                AddressOutputPin.Notify( (void *)AAddress.c_str() );
 113*/
 114                                char format[ 24 ];
 115                                sprintf( format, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X", FAddress[ 0 ], FAddress[ 1 ], FAddress[ 2 ], FAddress[ 3 ], FAddress[ 4 ], FAddress[ 5 ], FAddress[ 6 ], FAddress[ 7 ] );
 116                                AddressOutputPin.Notify( format );
 117                        }
 118
 119                        if( AlarmOutputPin.IsConnected() )
 120                        {
 121                                FThermometer->setHighAlarmTemp( FAddress, (char)( AlarmHighTemperature + 0.5 ));
 122                                FThermometer->setLowAlarmTemp( FAddress, (char)( AlarmLowTemperature + 0.5 ));
 123
 124//                              float AValue = ( AlarmHighTemperature - (-55) ) / ( 125 - -55 ) * 255;
 125//                              FThermometer->setHighAlarmTemp( FAddress, (char)( AValue + 0.5 ));
 126
 127//                              AValue = ( AlarmLowTemperature - (-55) ) / ( 125 - -55 ) * 255;
 128//                              FThermometer->setLowAlarmTemp( FAddress, (char)( AValue + 0.5 ));
 129                        }
 130
 131                        inherited::SystemStart();
 132                }
 133
 134                virtual void SystemLoopBegin( unsigned long currentMicros )
 135                {
 136                        if( ! ClockInputPin.IsConnected() )
 137                                ReadTemperature();
 138
 139                        // Needs to be changed to handle when the clock is different for the thermometers!
 140                        if( FIndex == 0 )
 141                                FThermometer->requestTemperatures();
 142
 143                        inherited::SystemLoopBegin( currentMicros );
 144                }
 145
 146        protected:
 147                void GetOneWire()
 148                {
 149                        if( ! FOneWire )
 150                                FOneWire = new OneWire( FDigitalPin );
 151                }
 152
 153                void ReadTemperature()
 154                {
 155                        if( AlarmOutputPin.IsConnected() )
 156                        {
 157                                bool AValue = FThermometer->hasAlarm( FAddress );
 158                                AlarmOutputPin.Notify( &AValue );
 159                        }
 160
 161                        float   AValue = (InFahrenheit) 
 162                                ? FThermometer->getTempF( FAddress )
 163                                : FThermometer->getTempC( FAddress );
 164
 165                        OutputPin.Notify( &AValue );
 166                }
 167
 168        protected:
 169                void DoOneWireReceive( void *_Data )
 170                {
 171                        if( FIndex )
 172                                OneWireOutputPin.Notify( _Data );
 173
 174                        else
 175                        {
 176                                GetOneWire();
 177//                              *(OneWire**)_Data = FOneWire;
 178                                // Migrate to OpenWire with DallasTemperature rewrite!
 179                                *(DallasTemperature**)_Data = FThermometer;
 180                        }
 181                }
 182
 183                virtual void DoClockReceive( void *_Data ) override
 184                {
 185                        ReadTemperature();
 186                }
 187
 188        public:
 189                OneWireThermometer( int ADigitalPin, int AIndex ) :
 190                        FDigitalPin( ADigitalPin ),
 191                        FIndex( AIndex ),
 192                        AutoDiscover( true ),
 193                        InFahrenheit( false )
 194                {
 195                        memset( FAddress, 0, 8 );
 196
 197                        OneWireInputPin.SetCallback( MAKE_CALLBACK( OneWireThermometer::DoOneWireReceive ));
 198                }
 199
 200                OneWireThermometer( int ADigitalPin, int AIndex, uint8_t AByte0, uint8_t AByte1, uint8_t AByte2, uint8_t AByte3, uint8_t AByte4, uint8_t AByte5, uint8_t AByte6, uint8_t AByte7 ) :
 201                        FDigitalPin( ADigitalPin ),
 202                        FIndex( AIndex ),
 203                        AutoDiscover( true ),
 204                        InFahrenheit( false )
 205                {
 206                        FAddress[ 0 ] = AByte0;
 207                        FAddress[ 1 ] = AByte1;
 208                        FAddress[ 2 ] = AByte2;
 209                        FAddress[ 3 ] = AByte3;
 210                        FAddress[ 4 ] = AByte4;
 211                        FAddress[ 5 ] = AByte5;
 212                        FAddress[ 6 ] = AByte6;
 213                        FAddress[ 7 ] = AByte7;
 214
 215                        OneWireInputPin.SetCallback( MAKE_CALLBACK( OneWireThermometer::DoOneWireReceive ));
 216                }
 217
 218/*
 219                virtual ~OneWireThermometer()
 220                {
 221                        if( FIndex == 0 )
 222                        {
 223                                if( FThermometer )
 224                                        delete FThermometer;
 225
 226                                if( FOneWire )
 227                                        delete FOneWire;
 228                        }
 229                }
 230*/
 231        };
 232}
 233
 234#endif