~xdavidwu/motion-control

795dab8901d4cfc0ab8c60bcb40a44432b613d79 — xdavidwu 1 year, 9 months ago 1367834
use net::dial
M cmd/motion-control/main+gy801.ha => cmd/motion-control/main+gy801.ha +5 -17
@@ 7,7 7,7 @@ use io;
use linux::timerfd;
use math;
use net;
use net::ip;
use net::dial;
use net::udp;
use os;
use proto::control;


@@ 48,11 48,11 @@ fn sigusr1(sig: int, info: *signal::siginfo, ucontext: *void) void = {
export fn main() int = {
	const help: []getopt::help = [
		"control pointerd with motion",
		"bus addr port",
		"bus address",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);
	if (len(cmd.args) != 3) {
	if (len(cmd.args) != 2) {
		getopt::printusage(os::stderr, os::args[0], help);
		os::exit(-1);
	};


@@ 62,29 62,17 @@ export fn main() int = {
	case =>
		fmt::fatal("bus should be a integer");
	};
	const addr = match (ip::parse(cmd.args[1])) {
	case let addr: ip::addr =>
		yield addr;
	case ip::invalid =>
		fmt::fatalf("Invalid address {}", cmd.args[1]);
	};
	const port = match (strconv::stou16(cmd.args[2])) {
	case let port: u16 =>
		yield port;
	case =>
		fmt::fatalf("Invalid port {}", cmd.args[2]);
	};

	const buf = control::request {
		code = control::code::RELATIVE_MOVEMENT,
		...,
	};

	const sock = match (udp::connect(addr, port)) {
	const sock = match (dial::dial("udp", cmd.args[1], strconv::u16tos(control::DEFAULT_PORT))) {
	case let s: net::socket =>
		yield s;
	case let err: net::error =>
		fmt::fatalf("Cannot connect() {}:{}: {}", ip::string(addr), port, net::strerror(err));
		fmt::fatalf("Cannot dial() {}: {}", cmd.args[1], net::strerror(err));
	};
	defer net::close(sock)!;


M cmd/motion-control/main+joycon.ha => cmd/motion-control/main+joycon.ha +5 -17
@@ 6,7 6,7 @@ use io;
use linux::timerfd;
use math;
use net;
use net::ip;
use net::dial;
use net::udp;
use os;
use proto::control;


@@ 47,37 47,25 @@ fn sigusr1(sig: int, info: *signal::siginfo, ucontext: *void) void = {
export fn main() int = {
	const help: []getopt::help = [
		"control pointerd with motion",
		"addr port",
		"address",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);
	if (len(cmd.args) != 2) {
	if (len(cmd.args) != 1) {
		getopt::printusage(os::stderr, os::args[0], help);
		os::exit(-1);
	};
	const addr = match (ip::parse(cmd.args[0])) {
	case let addr: ip::addr =>
		yield addr;
	case ip::invalid =>
		fmt::fatalf("Invalid address {}", cmd.args[0]);
	};
	const port = match (strconv::stou16(cmd.args[1])) {
	case let port: u16 =>
		yield port;
	case =>
		fmt::fatalf("Invalid port {}", cmd.args[1]);
	};

	const buf = control::request {
		code = control::code::RELATIVE_MOVEMENT,
		...,
	};

	const sock = match (udp::connect(addr, port)) {
	const sock = match (dial::dial("udp", cmd.args[0], strconv::u16tos(control::DEFAULT_PORT))) {
	case let s: net::socket =>
		yield s;
	case let err: net::error =>
		fmt::fatalf("Cannot connect() {}:{}: {}", ip::string(addr), port, net::strerror(err));
		fmt::fatalf("Cannot dial() {}: {}", cmd.args[0], net::strerror(err));
	};
	defer net::close(sock)!;


M tools/buttonc/main.ha => tools/buttonc/main.ha +5 -17
@@ 2,7 2,7 @@ use fmt;
use getopt;
use io;
use net;
use net::ip;
use net::dial;
use net::udp;
use os;
use os::exec;


@@ 23,7 23,7 @@ export fn main() int = {
	const help: []getopt::help = [
		"control pointerd with buttons",
		('m', "pid", "motion-control pid, for attitude reset key combo"),
		"addr port",
		"address",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);


@@ 42,28 42,16 @@ export fn main() int = {
		};
	};

	if (len(cmd.args) != 2) {
	if (len(cmd.args) != 1) {
		getopt::printusage(os::stderr, os::args[0], help);
		os::exit(-1);
	};
	const addr = match (ip::parse(cmd.args[0])) {
	case let addr: ip::addr =>
		yield addr;
	case ip::invalid =>
		fmt::fatalf("Invalid address {}", cmd.args[0]);
	};
	const port = match (strconv::stou16(cmd.args[1])) {
	case let port: u16 =>
		yield port;
	case =>
		fmt::fatalf("Invalid port {}", cmd.args[1]);
	};

	const sock = match (udp::connect(addr, port)) {
	const sock = match (dial::dial("udp", cmd.args[0], strconv::u16tos(control::DEFAULT_PORT))) {
	case let s: net::socket =>
		yield s;
	case let err: net::error =>
		fmt::fatalf("Cannot connect() {}:{}: {}", ip::string(addr), port, net::strerror(err));
		fmt::fatalf("Cannot dial() {}: {}", cmd.args[0], net::strerror(err));
	};
	defer net::close(sock)!;


M tools/pointerc/main.ha => tools/pointerc/main.ha +11 -23
@@ 1,6 1,6 @@
use fmt;
use net;
use net::ip;
use net::dial;
use net::udp;
use os;
use proto::control;


@@ 18,39 18,27 @@ fn send(sock: net::socket, bytes: []u8) void = {
};

export fn main() int = {
	if (len(os::args) > 5 || len(os::args) < 4) {
		fmt::fatalf("Usage: {} addr port (x y | left | right)", os::args[0]);
	};
	const addr = match (ip::parse(os::args[1])) {
	case let addr: ip::addr =>
		yield addr;
	case ip::invalid =>
		fmt::fatalf("Invalid address {}", os::args[1]);
	};
	const port = match (strconv::stou16(os::args[2])) {
	case let port: u16 =>
		yield port;
	case =>
		fmt::fatalf("Invalid port {}", os::args[2]);
	if (len(os::args) > 4 || len(os::args) < 3) {
		fmt::fatalf("Usage: {} address (x y | left | right)", os::args[0]);
	};

	const sock = match (udp::connect(addr, port)) {
	const sock = match (dial::dial("udp", os::args[1], strconv::u16tos(control::DEFAULT_PORT))) {
	case let s: net::socket =>
		yield s;
	case let err: net::error =>
		fmt::fatalf("Cannot connect() {}:{}: {}", ip::string(addr), port, net::strerror(err));
		fmt::fatalf("Cannot dial() {}: {}", os::args[1], net::strerror(err));
	};
	defer net::close(sock)!;

	let buf = control::request {...};

	if (strings::compare(os::args[3], "left") == 0) {
	if (strings::compare(os::args[2], "left") == 0) {
		buf.code = control::code::KEY_PRESSED;
		buf.key = control::key::BTN_LEFT;
		send(sock, buf.bytes);
		buf.code = control::code::KEY_RELEASED;
		send(sock, buf.bytes);
	} else if (strings::compare(os::args[3], "right") == 0) {
	} else if (strings::compare(os::args[2], "right") == 0) {
		buf.code = control::code::KEY_PRESSED;
		buf.key = control::key::BTN_RIGHT;
		send(sock, buf.bytes);


@@ 58,17 46,17 @@ export fn main() int = {
		send(sock, buf.bytes);
	} else {
		buf.code = control::code::RELATIVE_MOVEMENT;
		buf.x = match (strconv::stoi8(os::args[3])) {
		buf.x = match (strconv::stoi8(os::args[2])) {
		case let n: i8 =>
			yield n;
		case =>
			fmt::fatalf("Invalid x {}", os::args[3]);
			fmt::fatalf("Invalid x {}", os::args[2]);
		};
		buf.y = match (strconv::stoi8(os::args[4])) {
		buf.y = match (strconv::stoi8(os::args[3])) {
		case let n: i8 =>
			yield n;
		case =>
			fmt::fatalf("Invalid y {}", os::args[4]);
			fmt::fatalf("Invalid y {}", os::args[3]);
		};
		send(sock, buf.bytes);
	};