Esempio n. 1
0
/*
 * 	relaySetChannel() Python Analogue
 */
static PyObject* pyRelaySetChannel(PyObject* self, PyObject* args)
{
	int 	status, addr, channel, state;

	// parse the arguments
	if (!PyArg_ParseTuple(args, "iii", &addr, &channel, &state) ) {
		return NULL;
	}

	// make the relay-exp call
	status 	= relaySetChannel (addr, channel, state);

	if (status != EXIT_SUCCESS) {
		PyErr_SetString(PyExc_IOError, wrmsg_i2c);
		return NULL;
	}

	return Py_BuildValue("i", status);
}
Esempio n. 2
0
int main(int argc, char** argv)
{
	const char *progname;
	char 	*switchAddr;
	
	int 	status;
	int 	verbose;
	int 	init;
	int 	ch;

	int 	channel;
	int 	relayState;

	int 	devAddr;
	int 	bInitialized;

	// set defaults
	init 		= 0;
	verbose 	= ONION_VERBOSITY_NORMAL;

	switchAddr 	= malloc(RELAY_EXP_ADDR_SWITCH_NUM * sizeof *switchAddr);
	strcpy(switchAddr, RELAY_EXP_ADDR_SWITCH_DEFAULT_VAL);

	// save the program name
	progname 	= argv[0];	

	//// parse the option arguments
	while ((ch = getopt(argc, argv, "vqhis:")) != -1) {
		switch (ch) {
		case 'v':
			// verbose output
			verbose++;
			break;
		case 'q':
			// quiet output
			verbose = ONION_VERBOSITY_NONE;
			break;
		case 'i':
			// perform init sequence
			init 	= 1;
			break;
		case 's':
			// capture binary 
			strcpy (switchAddr, optarg);
			break;
		default:
			usage(progname);
			return 0;
		}
	}

	// set the verbosity
	onionSetVerbosity(verbose);


	// advance past the option arguments
	argc 	-= optind;
	argv	+= optind;


	// process the switch address
	status = processSwitchAddr(switchAddr, &devAddr);
	if (status == EXIT_FAILURE) {
		usage(progname);
		onionPrint(ONION_SEVERITY_FATAL, "ERROR: invalid switch address argument!\n");
		return 0;
	}
	if (strcmp(switchAddr, RELAY_EXP_ADDR_SWITCH_DEFAULT_VAL) != 0) {
		onionPrint(ONION_SEVERITY_INFO, "> Switch configuration: %s\n", switchAddr);
	}


	// check if just initialization
	if ( argc == 0 && init == 1 ) {
		status = relayDriverInit(devAddr);
		if (status == EXIT_FAILURE) {
			onionPrint(ONION_SEVERITY_FATAL, "main-relay-exp:: relay init failed!\n");
		}
		return 0;
	}

	// ensure correct number of arguments
	if ( argc != 2) {
		usage(progname);
		onionPrint(ONION_SEVERITY_FATAL, "ERROR: invalid amount of arguments!\n");
		return 0;
	}


	//// parse the arguments
	// first arg - channel
	channel 	= readChannelArgument(argv[0]);

	// second arg - relay state (on or off)
	if 	(	strcmp(argv[1], "off") == 0 ||
			strcmp(argv[1], "Off") == 0 ||
			strcmp(argv[1], "OFF") == 0
		) 
	{
		relayState 	= 0;
	}
	else if (	strcmp(argv[1], "on") == 0 ||
				strcmp(argv[1], "On") == 0 ||
				strcmp(argv[1], "ON") == 0
		) 
	{
		relayState 	= 1;
	}
	else {
		relayState 	= (int)strtol(argv[1], NULL, 10);
	}

	// validate the arguments
	status 	= validateArguments(channel, relayState);
	if (status == EXIT_FAILURE) {
		return 0;
	}


	//// RELAY PROGRAMMING
	// check if initialized
	status = relayCheckInit(devAddr, &bInitialized);

	// exit the app if i2c reads fail
	if (status == EXIT_FAILURE) {
		onionPrint(ONION_SEVERITY_FATAL, "> ERROR: Relay Expansion not found!\n");
		return 0;
	}
	

	// perform initialization
	if (init == 1 || bInitialized == 0) {
		status 	= relayDriverInit(devAddr);
		if (status == EXIT_FAILURE) {
			onionPrint(ONION_SEVERITY_FATAL, "main-relay-exp:: relay init failed!\n");
		}
	}

	// set the relay state
	if (channel < 0) {
		// program both relays at once
		status 	= relaySetAllChannels(devAddr, relayState);
		if (status == EXIT_FAILURE) {
			onionPrint(ONION_SEVERITY_FATAL, "main-relay-exp:: all relay setup failed!\n");
		}
	}
	else {
		// program just one relay
		status 	= relaySetChannel(devAddr, channel, relayState);
		if (status == EXIT_FAILURE) {
			onionPrint(ONION_SEVERITY_FATAL, "main-relay-exp:: relay %d setup failed!\n", channel);
		}
	}


	return 0;
}