コード例 #1
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
char *get_current_program_name() {

	unsigned char cm[2] = { 0x00, 0x11 };
	char *rbuf;
	int *rlen, cmret;
	unsigned char fname[19];
	char *fret = NULL;

	if (dodebug) {
		printf("get_current_program_name:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to get current program name\n");
		if (rbuf) free(rbuf);
		return(NULL);
	}

	memset(&fname, 0, 19);
	memcpy(&fname, &rbuf[3], 19);
	fret = (char *) strdup(fname);

	free(rbuf);
	return(fret);
}
コード例 #2
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int set_brick_name(char *name) {

	unsigned char cm[18];
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("set_brick_name %s:\n", name);
	}

	memset(&cm[0], 0, 18);
	cm[0] = (unsigned char) 0x01;
	cm[1] = (unsigned char) 0x98;
	memcpy(&cm[2], name, strlen(name));

	cmret = _do_cmd((char *)&cm, 18, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to set the brick name\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #3
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int get_battery_level() {

	char *rbuf;
	int *rlen, cmret;
	short int slen;
	unsigned char cm[2] = { 0x0, 0x0b };

	if (dodebug) {
		printf("get_battery_level:\n");
	}

	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	memcpy(&slen, (char **)&rbuf[3], 2);
	if (cmret) {
		fprintf(stderr, "Failed to get battery level\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (dodebug) {
		printf("battery %d\n", (int)slen);
	}

	free(rbuf);
	return((int)slen);
}
コード例 #4
0
int _set_output_state(unsigned char port, signed char power, unsigned char mode, unsigned char regulation, signed char ratio, unsigned char runstate, unsigned long tacholimit) {

	char *rbuf;
	unsigned char cm[13];
	int *rlen, cmret;

	cm[0] = (unsigned char) 0x00;
	cm[1] = (unsigned char) 0x04;
	cm[2] = (unsigned char) port;
	cm[3] = (signed char) power;
	cm[4] = (unsigned char) mode;
	cm[5] = (unsigned char) regulation;
	cm[6] = (signed char) ratio;
	cm[7] = (unsigned char) runstate;
	memcpy(&cm[8], &tacholimit, 5);

	if (dodebug) {
		printf("_set_output_state:\n");
	}

	cmret = _do_cmd((char *)&cm, 13, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to run _set_output_state command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	free(rbuf);
	return(0);
}
コード例 #5
0
ファイル: poll.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
/* one of POLL_BUFFER OR HS_BUFFER */
int poll_command_length(unsigned char bufnum) {

	unsigned char cm[3];
	char *rbuf;
	int *rlen, cmret;
	int rret = 0;

	cm[0] = (unsigned char) 0x01;
	cm[1] = (unsigned char) 0xA1;
	cm[2] = (unsigned char) bufnum;

	if (dodebug) {
		printf("poll_command_length %d:\n", bufnum);
	}

	cmret = _do_cmd((char *)&cm, 3, (char **)&rbuf, (int *)&rlen, 3);
	if (cmret) {
		fprintf(stderr, "Failed to poll command length buffer\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (dodebug) {
		printf("poll_command_length: got extra lengths of %d\n", (int)(rlen - 3));
	}

	memcpy(&rret, &cm[4], (int)(rlen - 3));

	free(rbuf);
	return(rret);
}
コード例 #6
0
ファイル: poll.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int poll_command(unsigned char bufnum, unsigned char length, char **buf, int *len) {

	unsigned char cm[4] = { 0x01, 0xA2, bufnum, length };
	char *rbuf;
	int *rlen, cmret;
	unsigned char lod;

	if (dodebug) {
		printf("poll_command %d %d:\n", bufnum, length);
	}
	cmret = _do_cmd((char *)&cm, 4, (char **)&rbuf, (int *)&rlen, 3);
	if (cmret) {
		fprintf(stderr, "Failed to poll command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	lod = (unsigned char) rbuf[4]; /* length of data */
	if (dodebug) {
		printf("got lod %d\n", lod);
	}
	*len = (int) lod;
	if (dodebug) {
		printf("got len %d\n", (int)len);
	}

	*buf = (char *) malloc((int)len);
	memcpy(*buf, &rbuf[5], (int)len);

	free(rbuf);
	return((int)len);
}
コード例 #7
0
ファイル: msg.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int message_read(unsigned char rinbox, unsigned char linbox, unsigned char remove, unsigned char *inbox, unsigned char *size, unsigned char **data) {

	unsigned char cm[256];
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("message_read:\n");
	}

	cm[0] = (unsigned char) 0x0;
	cm[1] = (unsigned char) 0x13;
	cm[2] = (unsigned char) rinbox;
	cm[3] = (unsigned char) linbox;
	cm[4] = (unsigned char) remove; /* remove? non-zero clears message from inbox */

	if (dodebug) {
		printf("message_read:\n");
	}

	cmret = _do_cmd((char *)&cm, 5, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to run message_read command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	*inbox = (unsigned char) cm[3];
	*size = (unsigned char) cm[4];
	*data = (unsigned char *) malloc((int)size);
	memcpy(*data, &cm[5], (int)size);

	free(rbuf);
	return(0);
}
コード例 #8
0
ファイル: msg.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int message_write(unsigned char inbox, unsigned char size, unsigned char *data) {

	unsigned char cm[256];
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("message_write:\n");
	}

	cm[0] = (unsigned char) 0x0;
	cm[1] = (unsigned char) 0x09;
	cm[2] = (unsigned char) inbox;
	cm[3] = (unsigned char) size;
	memcpy(&cm[4], data, (int)size);

	if (dodebug) {
		printf("message_write:\n");
	}
	cmret = _do_cmd((char *)&cm, 4+(int)size, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to run message_write command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	free(rbuf);
	return(0);
}
コード例 #9
0
int _get_output_state(unsigned char port, signed char *power, unsigned char *mode, unsigned char *regulation, signed char *ratio, unsigned char *runstate, unsigned long *tacholimit, int *tachocount, int *blocktachocount, int *rotationcount) {

	char *rbuf;
	unsigned char cm[3] = { 0x0, 0x06, port };
	int *rlen, cmret;
	unsigned long t1 = 0;
	int t2;

	if (dodebug) { printf("_set_output_state:\n"); }
	cmret = _do_cmd((char *)&cm, 3, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to run _set_output_state command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	*power = (signed char) rbuf[4];
	*mode = (unsigned char) rbuf[5];
	*regulation = (unsigned char) rbuf[6];
	*ratio = (signed char) rbuf[7];
	*runstate = (unsigned char) rbuf[8];

	memcpy(&t1, &rbuf[9], 5);
	*tacholimit = t1;
	memcpy(&t2, &rbuf[13], 4);
	*tachocount = t2;
	memcpy(&t2, &rbuf[17], 4);
	*blocktachocount = t2;
	memcpy(&t2, &rbuf[21], 4);
	*rotationcount = t2;

	free(rbuf);
	return(0);
}
コード例 #10
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int keep_alive() {

	unsigned char cm[2] = { 0x00, 0x0D };
	char *rbuf;
	int *rlen, cmret;
	unsigned int sleep = 0;

	if (dodebug) {
		printf("keep_alive:\n");
	}

	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to stop sound playback\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	memcpy(&sleep, &rbuf[3], 4);
	if (dodebug) {
		printf("keep_alive: %d\n", (unsigned int)sleep);
	}

	free(rbuf);
	return((int)sleep);
}
コード例 #11
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
char *get_firmware_version() {

	unsigned char cm[2] = { 0x01, 0x88 };
	char *rbuf, *bret;
	int *rlen, cmret;

	if (dodebug) {
		printf("get_firmware_version:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to get firmware version");
		if (rbuf) free(rbuf);
		return(NULL);
	}

	if (dodebug) {
		printf("got rlen %d\n", (int)rlen);
	}

	bret = (char *) malloc(40);
	memset(bret, 0, 40);
	sprintf(bret, "Protocol: %u.%u Firmware: %u.%u", (unsigned char)rbuf[4], (unsigned char)rbuf[3], (unsigned char)rbuf[6], (unsigned char)rbuf[5]);

	free(rbuf);
	return(bret);
}
コード例 #12
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int _get_device_info(char **name, int *bsignal, int *flash) {

	unsigned char cm[2] = { 0x01, 0x9b };
	char *rbuf;
	int *rlen, cmret;
	char nxtname[14], nxtaddr[6];
	int bs, fl;

	if (dodebug) {
		printf("get_device_info:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to get device info\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (dodebug) {
		printf("got rlen %d\n", (int)rlen);
	}

	debug_buf(&rbuf[3], 4);

	memset(&nxtname, 0, 14);
	memset(&nxtaddr, 0, 6);
	memcpy(&nxtname, &rbuf[3], 14);
	memcpy(&nxtaddr, &rbuf[18], 6);
	memcpy(&bs, &rbuf[25], 4);
	memcpy(&fl, &rbuf[29], 4);

	if (dodebug) {
		printf("nxtname %s\n", nxtname);
		printf("nxtaddr %s\n", nxtaddr);
		printf("bs %d\n", bs);
		printf("flash %d\n", fl);
	}

	*name = malloc(strlen(nxtname)+1);
	memset(*name, 0, strlen(nxtname)+1);
	memcpy(*name, &nxtname, strlen(nxtname));
	*bsignal = bs;
	*flash = fl;

	free(rbuf);
	return(0);
}
コード例 #13
0
ファイル: find.c プロジェクト: DeforaOS/utils
static int _find_do(Prefs * prefs, char const * pathname, int cmdc,
		char * cmdv[])
{
	struct stat st;

	if(stat(pathname, &st) != 0) /* XXX TOCTOU, danger of infinite loop */
	{
		if(errno != ENOENT || *prefs & FIND_PREFS_L
				|| stat(pathname, &st) != 0)
			return _find_error(pathname, 1);
	}
	if(_do_cmd(prefs, pathname, &st, cmdc, cmdv) != 0)
		return 0;
	if(S_ISDIR(st.st_mode))
		return _do_dir(prefs, pathname, cmdc, cmdv);
	return 0;
}
コード例 #14
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int stop_sound_playback() {

	unsigned char cm[2] = { 0x00, 0x0C };
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("stop_sound_playback:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to stop sound playback\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #15
0
int reset_motor_position(unsigned char port, unsigned char relative) {

	char *rbuf;
	unsigned char cm[4] = { 0x0, 0x0A, port, relative };
	int *rlen, cmret;

	if (dodebug) {
		printf("_set_output_state:\n");
	}
	cmret = _do_cmd((char *)&cm, 3, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to run _set_output_state command\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #16
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int stop_program() {

	unsigned char cm[2] = { 0x00, 0x01 };
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("stop_program:\n");
	}

	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to stop running program\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #17
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int start_program(char *name) {

	char *rbuf;
	int *rlen, cmret;
	unsigned char cm[22];

	if (dodebug) { printf("start_program: %s\n", name); }
	memset(&cm, 0, 22);
	cm[0] = (unsigned char) 0x0;
	cm[1] = (unsigned char) 0x0;
	memcpy(&cm[2], name, strlen(name));

	cmret = _do_cmd((char *)&cm, 22, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to execute program %s\n", name);
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #18
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
/* I have no idea what this does */
int delete_user_flash() {

	unsigned char cm[2] = { 0x01, 0xA0 };
	char *rbuf;
	int *rlen, cmret;
	unsigned char ret;

	if (dodebug) {
		printf("delete_user_flash:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	ret = (unsigned char) rbuf[0];
	free(rbuf);
	if (cmret) {
		fprintf(stderr, "Failed to delete user flash\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #19
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
/*
 * HZ range 200 - 14000
 * Duration uword ms
 */
int play_tone(short int hz, short int duration) {

	unsigned char cm[6] = { 0x00, 0x03 };
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("play_tone:\n");
	}

	memcpy(&cm[2], &hz, 2);
	memcpy(&cm[4], &duration, 2);

	cmret = _do_cmd((char *)&cm, 6, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to play tone at hz %d duration %d\n", hz, duration);
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}
コード例 #20
0
ファイル: nxt.c プロジェクト: DonaldSeo/LegoNXT-robotSoccer
int play_sound_file(int boolean, char *name) {

	unsigned char cm[23];
	char *rbuf;
	int *rlen, cmret;

	if (dodebug) {
		printf("play_sound_file:\n");
	}
	memset(&cm, 0, 23);
	cm[0] = (unsigned char) 0x0;
	cm[1] = (unsigned char) 0x02;
	cm[2] = (unsigned char) boolean; /* loop indefinitely? */
	memcpy(&cm[3], name, strlen(name));
	cmret = _do_cmd((char *)&cm, 23, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to play sound file\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (rbuf) free(rbuf);
	return(0);
}