/**
* @file MDC04_ATY.h
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2026 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief functions of MDC04 sensor for all embedded device
*
* @version
* - 1_00_250101 > ATY
* -# Initial version, support OneWire and I2C modes
********************************************************************************
*/
#ifndef __MDC04_ATY_H
#define __MDC04_ATY_H
#include "INCLUDE_ATY.h"
/******************************* For user *************************************/
// MDC04 Communication Modes
#define MDC04_MODE_ONEWIRE 0x01
#define MDC04_MODE_I2C 0x02
// MDC04 Commands
#define MDC04_CMD_READ_TEMP 0x44 // Start temperature conversion
#define MDC04_CMD_READ_ROM 0x33 // Read ROM (OneWire mode)
#define MDC04_CMD_SKIP_ROM 0xCC // Skip ROM (OneWire mode)
#define MDC04_CMD_SEARCH_ROM 0xF0 // Search ROM (OneWire mode)
// MDC04 I2C Default Address
#define MDC04_I2C_ADDR_DEFAULT 0x48
// MDC04 Status Codes
#define MDC04_STATUS_OK 0x00
#define MDC04_STATUS_ERROR 0x01
#define MDC04_STATUS_TIMEOUT 0x02
#define MDC04_STATUS_CRC_ERROR 0x03
#define MDC04_STATUS_NO_DEVICE 0x04
// MDC04 Timing Constants (microseconds)
#define MDC04_RESET_PULSE_TIME 480
#define MDC04_PRESENCE_WAIT_TIME 70
#define MDC04_PRESENCE_PULSE_TIME 240
#define MDC04_WRITE_0_LOW_TIME 60
#define MDC04_WRITE_1_LOW_TIME 6
#define MDC04_READ_LOW_TIME 6
#define MDC04_READ_SAMPLE_TIME 9
#define MDC04_SLOT_TIME 60
/******************************************************************************/
struct MDC04_ATY_Dev
{
// Communication mode
uint8_t mode; // MDC04_MODE_ONEWIRE or MDC04_MODE_I2C
// OneWire mode functions
void (*oneWireSetHigh)(void); // Set data line high
void (*oneWireSetLow)(void); // Set data line low
uint8_t (*oneWireRead)(void); // Read data line state
void (*delayUs)(uint32_t us); // Microsecond delay function
// I2C mode functions
uint8_t (*i2cStart)(void); // I2C start condition
uint8_t (*i2cStop)(void); // I2C stop condition
uint8_t (*i2cWriteByte)(uint8_t data); // I2C write byte
uint8_t (*i2cReadByte)(uint8_t ack); // I2C read byte
uint8_t i2cAddress; // I2C device address
// Device data
uint8_t romCode[8]; // ROM code for OneWire mode
float temperature; // Last read temperature
uint16_t rawTemperature; // Raw temperature data
// Status and control
uint8_t devicePresent; // Device presence flag
uint8_t lastStatus; // Last operation status
uint8_t initialized; // Initialization flag
uint32_t conversionTime; // Temperature conversion time (ms)
// Debug and logging
uint8_t lock;
uint8_t debugEnable;
void (*LOG)(const char*, ...);
};
// Function prototypes
uint8_t MDC04_Init(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_Reset(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_ReadTemperature(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_StartConversion(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_ReadROM(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_SearchDevices(struct MDC04_ATY_Dev *dev, uint8_t *deviceCount);
// OneWire specific functions
uint8_t MDC04_OneWire_WriteByte(struct MDC04_ATY_Dev *dev, uint8_t data);
uint8_t MDC04_OneWire_ReadByte(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_OneWire_WriteBit(struct MDC04_ATY_Dev *dev, uint8_t bit);
uint8_t MDC04_OneWire_ReadBit(struct MDC04_ATY_Dev *dev);
uint8_t MDC04_OneWire_CRC8(uint8_t *data, uint8_t len);
// I2C specific functions
uint8_t MDC04_I2C_WriteRegister(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t data);
uint8_t MDC04_I2C_ReadRegister(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t *data);
uint8_t MDC04_I2C_ReadMultiple(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t *data, uint8_t len);
// Utility functions
float MDC04_ConvertRawToTemperature(uint16_t rawTemp);
uint8_t MDC04_GetDeviceStatus(struct MDC04_ATY_Dev *dev);
void MDC04_SetI2CAddress(struct MDC04_ATY_Dev *dev, uint8_t address);
#endif /* __MDC04_ATY_H */
/******************************** End Of File *********************************/