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

📄 common.nc

📁 这是应用SAM7X256做LonWorks网关的代码
💻 NC
字号:
//{{NodeBuilder Code Wizard Start <CodeWizard Timestamp>
// Run on Fri Dec 28 21:24:20 2007, version 3.10.56
//
//}}NodeBuilder Code Wizard End

//////////////////////////////////////////////////////////////////////////////
// File: COMMON.NC
//
//
// Generated by NodeBuilder Code Wizard Version 3.10.56
// Copyright (c) 2001-2003 Echelon Corporation.  All rights reserved.
//                                                                                  
// ECHELON MAKES NO REPRESENTATION, WARRANTY, OR CONDITION OF
// ANY KIND, EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE OR IN
// ANY COMMUNICATION WITH YOU, INCLUDING, BUT NOT LIMITED TO,
// ANY IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY
// QUALITY, FITNESS FOR ANY PARTICULAR PURPOSE, 
// NONINFRINGEMENT, AND THEIR EQUIVALENTS.
//
//
// Written By: NodeBuilder Code Wizard  
//
// Description: Common utility functions. Note not all of these functions are
// being used by the default framework, and a compiler efficiency warning NCC#310
// might be seen when building the device (function 'xyz' defined but not used).
// The unused functions may be removed from the device implementation if desired.
//
//////////////////////////////////////////////////////////////////////////////

#include "common.h"

// Some status bits are preserved across power-cycle. See gw.h for the
// declaration of the FBLOCK_PERSISTENT_STATUS_STORAGE macro.
FBLOCK_PERSISTENT_STATUS_STORAGE TPersistentFblockStatus PersistentFblockStatus[TOTAL_FBLOCK_COUNT];
//////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////

FBLOCK_STATUS_STORAGE TFblockData fblockData[TOTAL_FBLOCK_COUNT];

far TDeviceState deviceState = {0};

/* Note the executeOnEachFblock() function retriggers the watchdog timer.
 * The default code framework calls that function from the when(reset),
 * when(online) and when(offline) system tasks, thereby avoiding running
 * into watchdog timeout resets during these phases in case the device
 * contains a large number of functional fblocks. Please note that 
 * calls to the executeOnEachFblock() function should therefore be
 * considered carefully, as the function may take significant time to
 * execute.
 */
void executeOnEachFblock(const TFblock_command cmd)
{
	TFblockIndex fbIndex;
	for (fbIndex = 0; fbIndex < get_fblock_count(); fbIndex++)  {
		fblock_director(fbIndex, (int)cmd);
		watchdog_update();
	}
}

void updateDeviceState(unsigned short nvIndex, unsigned short nvArrayIndex, TFblockIndex firstElementIndex)
{
	TFblockIndex fbIndex;
	fbIndex = fblock_index_map[nvIndex];
	deviceState.FblockIndex = fbIndex;
	deviceState.relFblockIndex = fbIndex - firstElementIndex;
	deviceState.nvArrayIndex =  nvArrayIndex;
	deviceState.nvIndex = nvIndex;
}


//////////////////////////////////////////////////////////////////
//
//      Fblock Functions
//
//////////////////////////////////////////////////////////////////
//

void clearFblockStatus(TFblockIndex fblockIndex)
{
	SNVT_obj_status previousStatus;

	previousStatus = fblockData[fblockIndex].objectStatus;		

	// Reset the status struct to 0
	memset(&fblockData[fblockIndex].objectStatus, 0,
		sizeof(fblockData[fblockIndex].objectStatus));

	// Restore any persistent values
	fblockData[fblockIndex].objectStatus.object_id = fblockIndex;
	if (PersistentFblockStatus[fblockIndex] & FBLOCK_DISABLED)  {
		fblockData[fblockIndex].objectStatus.disabled = TRUE;
	}  

	if (PersistentFblockStatus[fblockIndex] & FBLOCK_IN_OVERRIDE)  {
		fblockData[fblockIndex].objectStatus.in_override = TRUE;
	}

	// Locked out and failed selftest bits are preserved on clear
	fblockData[fblockIndex].objectStatus.locked_out = previousStatus.locked_out;
	fblockData[fblockIndex].objectStatus.fail_self_test = previousStatus.fail_self_test;
}

void initAllFblockData(void)
{
	TFblockIndex i;
	
	// initAllFblockData() loops through the fblocks governed by CodeWizard. These are all fblocks in the
	// range 0..TOTAL_FBLOCK_COUNT.
	for (i=0; i < TOTAL_FBLOCK_COUNT; i++)  {
		clearFblockStatus(i);
		// All fblocks are initially locked out after reset.
		fblockData[i].objectStatus.locked_out = TRUE;
	}
}

void setPersistentFblockStatus(TFblockIndex fblockIndex, 
							   TPersistentFblockStatus status, 
                               boolean set)
{
	if (set)  {
		PersistentFblockStatus[fblockIndex] |= status;
	} else {
		PersistentFblockStatus[fblockIndex] &= ~status;
	}
}

void updateNode_Status(void)
{
	// The node status is the ORing of all other fblock's status.  It would
	// be very expensive to OR in each bit field on at a time so do it
	// word-wise.
	int iFb;
	int i, iBytes;
	unsigned char *p;

	// Skip over the fblock index field (unsigned long) to point to the status bits
	p = ((unsigned char *)getObjStatus(NODEOBJ_INDEX))+sizeof(NBCW_NODE_STATUS.object_id);
	iBytes = sizeof(SNVT_obj_status) - sizeof(NBCW_NODE_STATUS.object_id);

	// Clear the node status before collecting the other fblocks' status
	for (i = 0; i < iBytes; ++i)  {
		*(p+i) = '\0';
	}

	// Now collect the other statuses for CodeWizard-goverened fblocks
	for (iFb=1; iFb<TOTAL_FBLOCK_COUNT; iFb++)  {
		unsigned char *q;

		// Skip over the fblock index field of this fblock status
		q = ((unsigned char *)getObjStatus(iFb))+sizeof(NBCW_NODE_STATUS.object_id);
		for (i = 0; i < iBytes; ++i)  {
			*(p+i) |= *(q+i);
		}
	}
}

void setFblockDisable(TFblockIndex fblockIndex, boolean disable)
{
	if (getObjStatus(fblockIndex)->disabled != disable)  {
		getObjStatus(fblockIndex)->disabled = disable;
		setPersistentFblockStatus(fblockIndex, FBLOCK_DISABLED, disable);
	}

	updateNode_Status();
}

void setFblockOverride(TFblockIndex fblockIndex, boolean override)
{
	if (getObjStatus(fblockIndex)->in_override != override)  {
		getObjStatus(fblockIndex)->in_override = override;
		setPersistentFblockStatus(fblockIndex, FBLOCK_IN_OVERRIDE, override);
	}

	updateNode_Status();
}

void setFblockInAlarm(TFblockIndex fblockIndex)
{
	getObjStatus(fblockIndex)->in_alarm = TRUE;
	updateNode_Status();
}

void setFblockOutOfLimits(TFblockIndex fblockIndex)
{
	getObjStatus(fblockIndex)->out_of_limits = TRUE;
	updateNode_Status();
}

//
// setFblockManualControlBit
//
// sets manual_control status bit of fblockIndex if
// cmd = TRUE and clears it otherwise.

void setFblockManualControlBit(TFblockIndex fblockIndex, boolean cmd)
{
	getObjStatus(fblockIndex)->manual_control = cmd;
	updateNode_Status();
}

//
// getFblockManualControlBit
//
boolean getFblockManualControlBit(TFblockIndex fblockIndex)
{
	return(getObjStatus(fblockIndex)->manual_control);
}

//
// setCommFailedState
//
// sets fblock corresponding to fblock index's comm_failure status bit if
// failed = TRUE.  Should not be cleared by App under normal circumstances.

void setCommFailedState(TFblockIndex fblockIndex, boolean failed)
{
	getObjStatus(fblockIndex)->comm_failure = failed;
	updateNode_Status();
}

//
// setLockedOutBit
//
// sets fblock corresponding to fblock index's locked_out status bit if
// flag = TRUE and clears it otherwise. Is set/cleared from app
void setLockedOutBit(TFblockIndex fblockIndex, boolean lock)
{
	if (fblockLockedOut(fblockIndex)!= lock)  {
		fblockData[fblockIndex].objectStatus.locked_out = lock;
		updateNode_Status();
	}
} // setLockedOutbit


// Sets fblock's fail_self_test bit
void setFblockFailedSelfTest(TFblockIndex fblockIndex, boolean failed)
{
	// This is the only function that should clear the fail_self_test
	// bit.  RQ_CLEAR_STATUS to the node fblock will not clear this bit.  The
	// selftest needs to be commanded and it must pass before this bit may be cleared.
	//
	getObjStatus(fblockIndex)->fail_self_test = failed;
	updateNode_Status();
}

// Sets fblock's over_range bit
void setFblockOverrange(TFblockIndex fblockIndex, boolean ovr)
{
	getObjStatus(fblockIndex)->over_range = ovr;
	updateNode_Status();
}

// Sets fblock's under_range bit
void setFblockUnderrange(TFblockIndex fblockIndex, boolean under)
{
	getObjStatus(fblockIndex)->under_range = under;
	updateNode_Status();
}

boolean fblockNormal(TFblockIndex fblockIndex)
{
	return !getObjStatus(fblockIndex)->disabled &&
		!getObjStatus(fblockIndex)->fail_self_test;
}

boolean fblockNormalNotLockedOut(TFblockIndex fblockIndex)
{
	SNVT_obj_status * pStatus;

	pStatus = &fblockData[fblockIndex].objectStatus;
	return !pStatus->disabled &&
		!pStatus->fail_self_test &&
		!pStatus->locked_out;
}

// An enabled fblock is not disabled and not in override
boolean fblockEnabled(TFblockIndex fblockIndex)
{
	return !getObjStatus(fblockIndex)->disabled &&
		!getObjStatus(fblockIndex)->in_override;
}

boolean fblockDisabled(TFblockIndex fblockIndex)
{
	return getObjStatus(fblockIndex)->disabled;
}

boolean fblockInOverride(TFblockIndex fblockIndex)
{
	return getObjStatus(fblockIndex)->in_override;
}


⌨️ 快捷键说明

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