Compare commits

...

2 Commits

Author SHA1 Message Date
8c6d9431c8
main: some minor cleanup 2024-08-10 10:54:15 -07:00
02381539a7
main: draw something resembling a user interface
Very simplistic, but, and here's the key, it works.
2024-08-10 10:53:30 -07:00

View File

@ -3,7 +3,6 @@ const builtin = @import("builtin");
const vaxis = @import("vaxis");
const xev = @import("xev");
const Cell = vaxis.Cell;
const networking = @import("./networking.zig");
const rotctl = @import("./rotctl.zig");
@ -132,6 +131,7 @@ pub const RotInt = struct {
.get_position => |pos| {
self.current_posture = pos;
if (self.state == .rotator_connected) self.stateEvent(.rotator_ready);
self.draw() catch {};
},
.status => |code| if (code != .okay)
self.warn("rotctl error {s}", .{@tagName(code)}),
@ -162,6 +162,7 @@ pub const RotInt = struct {
.set_position => |pos| {
self.requested_posture = pos;
self.server.respond(self.loop, .okay);
self.draw() catch {};
},
.stop => self.server.respond(self.loop, .okay),
@ -174,47 +175,92 @@ pub const RotInt = struct {
}
fn draw(self: *RotInt) !void {
const Static = struct {
const lower_limit: u8 = 30;
const next_ms: u64 = 8;
var color_idx: u8 = lower_limit;
var dir: enum { up, down } = .up;
};
const style: vaxis.Style = .{
.fg = .{ .rgb = [_]u8{ Static.color_idx, Static.color_idx, Static.color_idx } },
};
const segment: vaxis.Segment = .{
.text = "yeah ok",
.style = style,
};
const win = self.vx.window();
win.clear();
const y_off = (win.height / 2) -| (6 / 2);
const x_off = (win.width / 2) -| (30 / 2);
const center = win.child(.{
.x_off = x_off + win.x_off,
.y_off = y_off + win.y_off,
.width = .{ .limit = 30 },
.height = .{ .limit = 6 },
.border = .{ .where = .all, .style = style },
});
_ = try center.printSegment(segment, .{ .wrap = .grapheme });
switch (Static.dir) {
.up => {
Static.color_idx += 1;
if (Static.color_idx == 255) Static.dir = .down;
},
.down => {
Static.color_idx -= 1;
if (Static.color_idx == Static.lower_limit) Static.dir = .up;
},
}
var lines: [3][128]u8 = undefined;
const offsets: vaxis.Segment = .{ .text = try std.fmt.bufPrint(
lines[0][0..],
"Offsets: Az: {d: >6.1}, El: {d: >6.1}",
.{ self.offsets.az, self.offsets.el },
) };
const requested: vaxis.Segment = .{ .text = try std.fmt.bufPrint(
lines[1][0..],
"Requested: Az: {d: >6.1}, El: {d: >6.1}",
.{ self.requested_posture.az, self.requested_posture.el },
) };
const current: vaxis.Segment = .{ .text = try std.fmt.bufPrint(
lines[2][0..],
"Current: Az: {d: >6.1}, El: {d: >6.1}",
.{ self.current_posture.az, self.current_posture.el },
) };
const center = vaxis.widgets.alignment.center(win, offsets.text.len, 1);
_ = try center.printSegment(offsets, .{});
const center_up = win.initChild(center.x_off, center.y_off + 1, .{ .limit = requested.text.len }, .{ .limit = 1 });
_ = try center_up.printSegment(requested, .{});
const center_down = win.initChild(center.x_off, center.y_off - 1, .{ .limit = current.text.len }, .{ .limit = 1 });
_ = try center_down.printSegment(current, .{});
try self.vx.render(self.termbuffer.writer().any());
try self.termbuffer.flush();
}
fn terminalEvent(
self_: ?*RotInt,
loop: *xev.Loop,
watcher: *vaxis.xev.TtyWatcher(RotInt),
event: vaxis.xev.Event,
) xev.CallbackAction {
const self = self_.?;
switch (event) {
.key_press => |key| keyp: {
var mods = key.mods;
mods.caps_lock = false;
mods.num_lock = false;
const scale: f64 = if (std.meta.eql(mods, .{ .shift = true })) 1 else 10;
const delta: AzEl = switch (key.codepoint) {
vaxis.Key.left, vaxis.Key.kp_left => .{ .az = -0.1 * scale, .el = 0 },
vaxis.Key.right, vaxis.Key.kp_right => .{ .az = 0.1 * scale, .el = 0 },
vaxis.Key.up, vaxis.Key.kp_up => .{ .az = 0, .el = 0.1 * scale },
vaxis.Key.down, vaxis.Key.kp_down => .{ .az = 0 * scale, .el = -0.1 * scale },
'l' => {
if (std.meta.eql(mods, .{ .ctrl = true }))
self.vx.queueRefresh();
break :keyp;
},
'c' => {
if (std.meta.eql(mods, .{ .ctrl = true })) {
loop.stop();
return .disarm;
}
break :keyp;
},
'q' => {
if (std.meta.eql(mods, .{})) {
loop.stop();
return .disarm;
}
break :keyp;
},
else => break :keyp,
};
self.offsets.az += delta.az;
self.offsets.el += delta.el;
self.draw() catch {
self.warn("draw failure", .{});
};
},
.winsize => |ws| {
watcher.vx.resize(self.allocator, watcher.tty.anyWriter(), ws) catch
return .disarm;
},
else => {},
}
return .rearm;
}
};
pub fn main() !void {
@ -245,7 +291,7 @@ pub fn main() !void {
try app.initInPlace();
var vx_loop: vaxis.xev.TtyWatcher(RotInt) = undefined;
try vx_loop.init(&tty, &vx, &loop, &app, eventCallback);
try vx_loop.init(&tty, &vx, &loop, &app, RotInt.terminalEvent);
try vx.enterAltScreen(tty.anyWriter());
try vx.queryTerminalSend(tty.anyWriter());
@ -256,72 +302,3 @@ pub fn main() !void {
try loop.run(.until_done);
}
fn eventCallback(
self_: ?*RotInt,
loop: *xev.Loop,
watcher: *vaxis.xev.TtyWatcher(RotInt),
event: vaxis.xev.Event,
) xev.CallbackAction {
const self = self_.?;
switch (event) {
.key_press => |key| keyp: {
var mods = key.mods;
mods.caps_lock = false;
mods.num_lock = false;
const scale: f64 = if (std.meta.eql(mods, .{ .shift = true })) 1 else 10;
const delta: AzEl = switch (key.codepoint) {
vaxis.Key.left, vaxis.Key.kp_left => .{ .az = -0.1 * scale, .el = 0 },
vaxis.Key.right, vaxis.Key.kp_right => .{ .az = 0.1 * scale, .el = 0 },
vaxis.Key.up, vaxis.Key.kp_up => .{ .az = 0, .el = 0.1 * scale },
vaxis.Key.down, vaxis.Key.kp_down => .{ .az = 0 * scale, .el = -0.1 * scale },
'l' => {
if (std.meta.eql(mods, .{ .ctrl = true }))
self.vx.queueRefresh();
break :keyp;
},
'c' => {
if (std.meta.eql(mods, .{ .ctrl = true })) {
loop.stop();
return .disarm;
}
break :keyp;
},
'q' => {
if (std.meta.eql(mods, .{})) {
loop.stop();
return .disarm;
}
break :keyp;
},
else => break :keyp,
};
self.offsets.az += delta.az;
self.offsets.el += delta.el;
},
.winsize => |ws| {
watcher.vx.resize(self.allocator, watcher.tty.anyWriter(), ws) catch
return .disarm;
},
else => {},
}
return .rearm;
}
fn timerCallback(
ud: ?*RotInt,
loop: *xev.Loop,
completion: *xev.Completion,
err: xev.Timer.RunError!void,
) xev.CallbackAction {
_ = err catch @panic("timer error");
var app = ud orelse return .disarm;
app.draw() catch @panic("couldn't draw");
const timer = xev.Timer.init() catch unreachable;
timer.run(loop, completion, 8, RotInt, app, timerCallback);
return .disarm;
}