mcci-catena / MCCI-Catena-HS300x

Arduino library for IDT HS300x (HS3001, HS3002) temperature/humidity sensor
MIT License
3 stars 4 forks source link

Error on compilation for <cstdint> #1

Open nimasaj opened 4 years ago

nimasaj commented 4 years ago

Thanks for sharing your library. However, I get error on compiling it for Arduino pro mini (328P-3,3V) with following errors:

In file included from C:\Users\XXX\AppData\Local\Temp\arduino_modified_sketch_299657\hs300x-simple.ino:16:0:

C:\Users\XXX\Documents\Arduino\libraries\MCCI-Catena-HS300x\src/Catena-HS300x.h:20:21: fatal error: cstdint.h: No such file or directory

compilation terminated.

It points to line 20, #include <cstdint>, in Catena-HS300x.h file. Could you please share missing library/files?

terrillmoore commented 4 years ago

<cstdint> is a standard c++ include file: See https://en.cppreference.com/w/cpp/header/cstdint. I don't know why your compiler doesn't include it, but it is so standard that I suspect an installation problem on your compiler.

Best regards, --Terry

nimasaj commented 4 years ago

Dear @terrillmoore , I am using Arduino IDE 1.8.9 to compile the example code. Have you tested it before on Arduino?

terrillmoore commented 4 years ago

That's exactly what I use. I have not tested with 8-bit Arduino, but I test all the time with ARM.

This is a defect in the Arduino library; they're not likely to fix it. The workaround is to detect AVR, then #include and name things. So for example:

#ifdef AVR // or whatever -- check the compiler docs, I don't know the standard way to check this offhand
# define NO_CSTDINT 1  // AVR arduino has no <cstdint>; but we're coding to portable C++. So substitute.
#endif

// unless we know otherwise, use the compiler's <cstdint>
#ifndef NO_CSTDINT
# include <cstdint>
#else
// no <cstdint> -- make sure std:: contains the things we need.
# include <stdint.h>

namespace std {
  using ::int8_t;             
  using ::uint8_t;            

  using ::int16_t;            
  using ::uint16_t;           

  using ::int32_t;            
  using ::uint32_t;           
}

#endif

(This is what boost does, https://github.com/vancegroup/arduino-boost/blob/master/boost/cstdint.hpp)

Since I don't use AVR, I've got no way to test, though I could of course compile. But compilation doesn't really count. Feel free to submit a working pull request.

nimasaj commented 4 years ago

I could of course compile. But compilation doesn't really count.

I applied proposed changes to Catena-HS300x.h, and still I couldn't compile the code. Following errors were generated.

In file included from C:\Users\XXX\AppData\Local\Temp\arduino_modified_sketch_775748\hs300x-simple.ino:16:0:

C:\Users\XXX\Documents\Arduino\libraries\MCCI-Catena-HS300x\src/Catena-HS300x.h: In static member function 'static constexpr uint16_t McciCatenaHs300x::cHS300x::celsiusToRawT(float)':

C:\Users\XXX\Documents\Arduino\libraries\MCCI-Catena-HS300x\src/Catena-HS300x.h:152:9: error: body of constexpr function 'static constexpr uint16_t McciCatenaHs300x::cHS300x::celsiusToRawT(float)' not a return-statement

          }

          ^

C:\Users\XXX\Documents\Arduino\libraries\MCCI-Catena-HS300x\src/Catena-HS300x.h: In static member function 'static constexpr uint16_t McciCatenaHs300x::cHS300x::percentRHtoRaw(float)':

C:\Users\XXX\Documents\Arduino\libraries\MCCI-Catena-HS300x\src/Catena-HS300x.h:163:9: error: body of constexpr function 'static constexpr uint16_t McciCatenaHs300x::cHS300x::percentRHtoRaw(float)' not a return-statement

          }

          ^

Then, I changed the board to MKRFox1200 which is running on Atmel SAMD21 (based on ARM 32bit architecture). Still, I couldn't compile the code due to the same errors as above.

Reverting changes on Catena-HS300x.h file, didn't help too. Still, same errors, showing functions celsiusToRawT(float t) and percentRHtoRaw(float rh) are not return-statements.

terrillmoore commented 4 years ago

Sorry you're having problems. It works for me. Sounds like the compilers in your packages don't support more recent C++. It works for me with STM32 and the compilers I have. I don't have a lot of time to look at this; if you're feeling brave, try changing the if's to equivalent return (test) ? v1 : v2:

    static constexpr float celsiusBias(float t) { return t + 40.0f; }

    // convert Celsius temperature to raw format.
    static constexpr std::uint16_t celsiusToRawT(float t)
        {
       return (celsiusBias(t) < 0.0f ? 0 : 
               celsiusBias(t) > 165.0f ? 0xFFFCu :
               (std::uint16_t) ((t / 165.0f) * float(0xFFFC));
        }

    // convert RH as percentage to raw format.
    static constexpr std::uint16_t percentRHtoRaw(float rh)
        {
        return (rh > 100.0) ? 0xFFFCu :
               (rh < 0.0) ? 0 :
               (std::uint16_t) (float(0xFFFC) * (rh / 100.0));
        }
terrillmoore commented 4 years ago

@nimasaj branch https://github.com/mcci-catena/MCCI-Catena-HS300x/tree/issue1 compiles correctly for AVR. I have no way of knowing if it works, but if it works for you, I'll merge onto head. There was one additional problem; in AVR, <Arduino.h> is not included by default, but apparently it is on the ARM builds. The fixes will also support older compilers, and there's an easy way to add new platforms that don't support <cstdint>.

nimasaj commented 4 years ago

@terrillmoore Thanks for fixing compilation issues. Unfortunately, the driver is not working on Arduino pro mini.

"gHs300x.begin() failed"

k2biru commented 4 years ago

@nimasaj i make a simple HS300x library here k2biru/HS300xlib using arduino Wire Library.

nimasaj commented 4 years ago

@k2biru It's working fine with small modifications. There were typos on type uint8_t that I proposed in your repository.

Shiny380 commented 3 years ago

can confirm the issue 1 branch is working correctly for arduino UNO and arduino Pro Mini (ATmega328p). master branch does not

terrillmoore commented 3 years ago

Hi @Shiny380, I have not tested. I do have a substitute for <cstdint> that I use in other projects, but I have not ported it back here, primarily because I can't test. If there's interest, I'll help to make this work.