~xdavidwu/motion-control

e2f6235629fd00bbc56e7dece9d549300551eabd — xdavidwu 2 years ago e04579c
buttonc: add key combo to signal motion-control
1 files changed, 30 insertions(+), 0 deletions(-)

M tools/buttonc/main.ha
M tools/buttonc/main.ha => tools/buttonc/main.ha +30 -0
@@ 5,6 5,7 @@ use net;
use net::ip;
use net::udp;
use os;
use os::exec;
use proto::control;
use strconv;
use unix::tty;


@@ 21,10 22,26 @@ fn send(sock: io::file, bytes: []u8) void = {
export fn main() int = {
	const help: []getopt::help = [
		"control pointerd with buttons",
		('m', "pid", "motion-control pid, for attitude reset key combo"),
		"addr port",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);

	let motion_pid = 0;
	for (let i = 0z; i < len(cmd.opts); i += 1) {
		const opt = cmd.opts[i];
		switch (opt.0) {
		case 'm' =>
			motion_pid = match(strconv::stoi(opt.1)) {
			case let pid: int =>
				yield pid;
			case =>
				fmt::fatalf("Invalid pid {}", opt.1);
			};
		};
	};

	if (len(cmd.args) != 2) {
		getopt::printusage(os::stderr, os::args[0], help);
		os::exit(-1);


@@ 61,6 78,8 @@ export fn main() int = {
	};
	if (tty) defer tty::termios_restore(termios);

	let left = false, right = false;
	let motion_blocked = false;
	for (true) {
		let i: []u8 = [0];
		match (io::read(os::stdin_file, i)) {


@@ 75,22 94,33 @@ export fn main() int = {
		case 'A' =>
			buf.code = control::code::KEY_PRESSED;
			buf.key = control::key::BTN_LEFT;
			left = true;
			send(sock, buf.bytes);
		case 'a' =>
			buf.code = control::code::KEY_RELEASED;
			buf.key = control::key::BTN_LEFT;
			left = false;
			send(sock, buf.bytes);
		case 'B' =>
			buf.code = control::code::KEY_PRESSED;
			buf.key = control::key::BTN_RIGHT;
			right = true;
			send(sock, buf.bytes);
		case 'b' =>
			buf.code = control::code::KEY_RELEASED;
			buf.key = control::key::BTN_RIGHT;
			right = false;
			send(sock, buf.bytes);
		case 4 => // EOT
			break;
		};
		if (motion_pid != 0) {
			if ((!motion_blocked && left && right) ||
					(motion_blocked && (!left || !right))) {
				exec::kill(motion_pid, exec::signal::SIGUSR1)!;
				motion_blocked = !motion_blocked;
			};
		};
	};

	return 0;