Converting from C++ language to C (from class to struct) to use HDC1080 sensor with ATTiny841 -
today i'm trying convert library: arduino library closedcube_hdc1080 humidity , temperature sensor use attiny841 attiny841 datasheet, i'm going use fleury's library use attiny841 master , hdc1080 slave.
my problem have issues convert c++ language used in hdc1080 library arduino attiny, use c language, show header files explain myself.
hdc1080_registers; class closedcube_hdc1080 { public: closedcube_hdc1080(); void begin(uint8_t address); uint16_t readmanufacturerid(); // 0x5449 id of texas instruments uint16_t readdeviceid(); // 0x1050 id of device hdc1080_registers readregister(); void writeregister(hdc1080_registers reg); void heatup(uint8_t seconds); float readtemperature(); float readhumidity(); float readt(); // short-cut readtemperature float readh(); // short-cut readhumidity private: uint8_t _address; uint16_t readdata(uint8_t pointer); }; #endif
this header (.h) file of hdc1080 sensor, read class function not available in c language, decide use struct function, saw available in c language, in examples saw on internet, use struct function declare variables, , functions not like:
- closedcube_hdc1080();
- void begin(uint8_t address);
- hdc1080_registers readregister();
- void writeregister(hdc1080_registers reg);
- void heatup(uint8_t seconds);
they define outside of struct function, confused void's inside of class function, reason why looking help. need know:
- how declare functions work struct
- is going easier define them later in c (.c) file?
thanks time , patience, first time converting 1 language another.
because internal state in class single uint8_t
, , code run on microcontroller, better supply address parameter each function.
first, note suggestions below derivative of closedcube hdc1080 arduino library, , therefore licensed under same license:
/* arduino library texas instruments hdc1080 digital humidity , temperature sensor written aa closedcube; suggested conversion nominal animal --- mit license (mit) copyright (c) 2016-2017 closedcube limited permission hereby granted, free of charge, person obtaining copy of software , associated documentation files (the "software"), deal in software without restriction, including without limitation rights use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of software, , permit persons whom software furnished so, subject following conditions: above copyright notice , permission notice shall included in copies or substantial portions of software. software provided "as is", without warranty of kind, express or implied, including not limited warranties of merchantability, fitness particular purpose , noninfringement. in no event shall authors or copyright holders liable claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection software or use or other dealings in software. */
attinys small 8-bit microcontrollers, it's best avoid using float
type. instead, suggest use units of 1/1000 degrees celsius temperature (25000 referring 25 degrees celsius), , 1/100000 humidity (50000 referring 50% of relative humidity).
the interface adapted library can quite simple:
void hdc1080_begin(const uint8_t address); void hdc1080_heater(const uint8_t address, const uint8_t seconds); uint16_t hdc1080_manufacturer(const uint8_t address); uint16_t hdc1080_device_id(const uint8_t address); int32_t hdc1080_temperature(const uint8_t address); uint32_t hdc1080_humidity(const uint8_t address);
if assume keep temperature , humidity precision @ 14 bits (configured hdc1080_begin()
, don't need user set or read configuration register.
the example code shows hdc1080_manufacturer()
should return 0x5449
, , hdc1080_device_id()
should return 0x1050
.
since don't know kind of interface "fleury's library" has, shall show implementation of above functions comments (starting /* i2c:
) instead of actual library calls.
the static
ones internal functions used abovementioned functions, not "user" code.
#define hdc1080_temperature ((uint8_t)0x00) #define hdc1080_humidity ((uint8_t)0x01) #define hdc1080_configuration ((uint8_t)0x02) #define hdc1080_serial_id_first ((uint8_t)0xfb) #define hdc1080_serial_id_mid ((uint8_t)0xfc) #define hdc1080_serial_id_last ((uint8_t)0xfd) #define hdc1080_manufacturer_id ((uint8_t)0xfe) #define hdc1080_device_id ((uint8_t)0xff) /* bits 15..8 of hdc1080 configuration. heater disabled, 14 bit precision both temperature , relative humidity. see page 15 of http://www.ti.com/lit/ds/symlink/hdc1080.pdf other possibilities. */ #define hdc1080_config_hi ((uint8_t)0) static uint16_t hdc1080_read(const uint8_t address, const uint8_t item) { uint8_t hi, lo; /* i2c: prepare send i2c address 'address'. */ /* i2c: send 8 bits 'item'. */ /* i2c: end transmission. */ /* delay 9 ms (0.009 seconds). */ /* i2c: prepare read 2 bytes i2c address 'address'. */ hi = /* i2c: read byte. */ lo = /* i2c: read byte. */ return (((uint16_t)hi) << 8) | lo; } void hdc1080_begin(const uint8_t address) { /* i2c: prepare send i2c address 'address'. */ /* i2c: send 8 bits: hdc1080_configuration (=0x02). */ /* i2c: send 8 bits: hdc1080_config_hi (=0x00). */ /* i2c: send 8 bits: 0. */ /* i2c: end transmission. */ /* delay 10 ms = 0.01 seconds. */ } void hdc1080_heater(const uint8_t address, const uint8_t seconds) { uint8_t s, i; /* i2c: prepare send i2c address 'address'. */ /* i2c: send 8 bits: hdc1080_configuration (=0x02). */ /* i2c: send 8 bits: 48 (=0x30). */ /* i2c: send 8 bits: 0. */ /* i2c: end transmission. */ /* delay 10 ms = 0.01 seconds. */ (s = 0; s < seconds; s++) { (i = 0; < 66; i++) { /* i2c: prepare send i2c address 'address'. */ /* i2c: send 8 bits 'item'. */ /* i2c: end transmission. */ /* delay 20 ms (0.020 seconds). */ /* i2c: prepare read 4 bytes i2c address 'address'. */ /* i2c: read byte. (ignore value.) */ /* i2c: read byte. (ignore value.) */ /* i2c: read byte. (ignore value.) */ /* i2c: read byte. (ignore value.) */ } } /* i2c: prepare send i2c address 'address'. */ /* i2c: send 8 bits: hdc1080_configuration (=0x02). */ /* i2c: send 8 bits: hdc1080_config_hi (=0x00). */ /* i2c: send 8 bits: 0. */ /* i2c: end transmission. */ /* delay 10 ms = 0.01 seconds. */ } uint16_t hdc1080_manufacturer(const uint8_t address) { return hdc1080_read(address, hdc1080_manufacturer_id); } uint16_t hdc1080_device_id(const uint8_t address) { return hdc1080_read(address, hdc1080_device_id); } int32_t hdc1080_temperature(const uint8_t address) { uint16_t temp1 = hdc1080_read(address, hdc1080_temperature) >> 2; return (int_t)(((uint32_t)20625 * temp1 + (uint32_t)1024) >> 11) - (int32_t)40000; } uint32_t hdc1080_humidity(const uint8_t address) { uint16_t temp1 = hdc1080_read(address, hdc1080_humidity); return ((uint32_t)3125 * temp1 + (uint32_t)1024) >> 11; }
note relative humidity range 0 99998, i.e. 0%rh 99.998% rh. not error, , formula taken datasheet , losslessly scaled new range. similarly, possible temperature range -40000 +124989, corresponding -40°c +124.989°c, , according ti datasheet, losslessly scaled new range. + (uint32_t)1024
term in both ensures correct rounding (to nearest).
you still need implementing /* i2c:
, /* delay
comments using whatever library have @ hand.
Comments
Post a Comment