⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crelay.cpp

📁 good luck to everyone!
💻 CPP
字号:
#include "SensorBase.h"
#include "DigitalModule.h"
#include "Relay.h"
#include "CRelay.h"

static Relay* relays[SensorBase::kDigitalModules][SensorBase::kRelayChannels];
static bool initialized = false;
static Relay::Direction s_direction = Relay::kBothDirections;

/**
 * Internal function to allocate Relay objects.
 * This function handles the mapping between channel/slot numbers to relay objects. It also
 * allocates Relay objects if they are not already allocated.
 *
 * @param slot The slot the digital module is plugged into
 * @param channel The relay channel for this device */
static Relay *AllocateRelay(UINT32 slot, UINT32 channel)
{
	if (!initialized)
	{
		for (unsigned i = 0; i < SensorBase::kDigitalModules; i++)
			for (unsigned j = 0; j < SensorBase::kRelayChannels; j++)
				relays[i][j] = NULL;
		initialized = true;
	}
	if (SensorBase::CheckRelayModule(slot) && SensorBase::CheckRelayChannel(channel))
	{
		unsigned slotOffset = DigitalModule::SlotToIndex(slot);
		if (relays[slotOffset][channel - 1] == NULL)
		{
			relays[slotOffset][channel - 1] = new Relay(slot, channel, s_direction);
		}
		return relays[slotOffset][channel - 1];
	}
	return NULL;
}

/**
 * Set the direction that this relay object will control.
 *
 * @param slot The slot the digital module is plugged into
 * @param channel The relay channel number for this object
 * @param direction The direction that the relay object will control */
void InitRelay(UINT32 slot, UINT32 channel, RelayDirection direction)
{
	switch (direction)
	{
	case kBothDirections:
		s_direction = Relay::kBothDirections;
		break;
	case kForwardOnly:
		s_direction = Relay::kForwardOnly;
		break;
	case kReverseOnly:
		s_direction = Relay::kReverseOnly;
		break;
	default:
		s_direction = Relay::kBothDirections;
	}
	AllocateRelay(slot, channel);
}

/**
 * Set the direction that this relay object will control.
 *
 * @param channel The relay channel number for this object
 * @param direction The direction that the relay object will control
 */
void InitRelay(UINT32 channel, RelayDirection direction)
{
	InitRelay(SensorBase::GetDefaultDigitalModule(), channel, direction);
}

/**
 * Free up the resources associated with this relay.
 * Delete the underlying Relay object and make the channel/port available for reuse.
 *
 * @param slot The slot that the digital module is plugged into
 * @param channel The relay channel number for this object */
void DeleteRelay(UINT32 slot, UINT32 channel)
{
	if (SensorBase::CheckRelayModule(slot) && SensorBase::CheckRelayChannel(channel))
	{
		unsigned slotOffset = DigitalModule::SlotToIndex(slot);
		delete relays[slotOffset][channel - 1];
		relays[slotOffset][channel - 1] = NULL;
	}
}

/**
 * Free up the resources associated with this relay.
 * Delete the underlying Relay object and make the channel/port available for reuse.
 *
 * @param channel The relay channel number for this object
 */
void DeleteRelay(UINT32 channel)
{
	DeleteRelay(SensorBase::GetDefaultDigitalModule(), channel);
}

/**
 * Set the relay state.
 *
 * Valid values depend on which directions of the relay are controlled by the object.
 *
 * When set to kBothDirections, the relay can only be one of the three reasonable
 *    values, 0v-0v, 0v-12v, or 12v-0v.
 *
 * When set to kForwardOnly or kReverseOnly, you can specify the constant for the
 *    direction or you can simply specify kOff and kOn.  Using only kOff and kOn is
 *    recommended.
 *
 * @param slot The slot that the digital module is plugged into
 * @param channel The relay channel number for this object
 * @param value The state to set the relay.
 */
void SetRelay(UINT32 slot, UINT32 channel, RelayValue value)
{
	Relay *relay = AllocateRelay(slot, channel);
	if (relay != NULL)
	{
		switch (value)
		{
			case kOff: relay->Set(Relay::kOff); break;
			case kOn: relay->Set(Relay::kOn); break;
			case kForward: relay->Set(Relay::kForward); break;
			case kReverse: relay->Set(Relay::kReverse); break;
		}
	}
}

/**
 * Set the relay state.
 *
 * Valid values depend on which directions of the relay are controlled by the object.
 *
 * When set to kBothDirections, the relay can only be one of the three reasonable
 *    values, 0v-0v, 0v-12v, or 12v-0v.
 *
 * When set to kForwardOnly or kReverseOnly, you can specify the constant for the
 *    direction or you can simply specify kOff and kOn.  Using only kOff and kOn is
 *    recommended.
 *
 * @param channel The relay channel number for this object
 * @param value The state to set the relay.
 */
void SetRelay(UINT32 channel, RelayValue value)
{
	SetRelay(SensorBase::GetDefaultDigitalModule(), channel, value);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -