Compare commits
2 Commits
33adcfc104
...
8c6d9431c8
Author | SHA1 | Date | |
---|---|---|---|
8c6d9431c8 | |||
02381539a7 |
167
src/main.zig
167
src/main.zig
@ -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,95 +175,43 @@ 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();
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
var tty = try vaxis.Tty.init();
|
||||
defer tty.deinit();
|
||||
|
||||
var vx = try vaxis.init(alloc, .{});
|
||||
defer vx.deinit(alloc, tty.anyWriter());
|
||||
|
||||
var pool = xev.ThreadPool.init(.{});
|
||||
var loop = try xev.Loop.init(.{
|
||||
.thread_pool = &pool,
|
||||
});
|
||||
defer loop.deinit();
|
||||
|
||||
var app: RotInt = .{
|
||||
.allocator = alloc,
|
||||
.termbuffer = tty.bufferedWriter(),
|
||||
.vx = &vx,
|
||||
.loop = &loop,
|
||||
.poller = try xev.Timer.init(),
|
||||
};
|
||||
try app.initInPlace();
|
||||
|
||||
var vx_loop: vaxis.xev.TtyWatcher(RotInt) = undefined;
|
||||
try vx_loop.init(&tty, &vx, &loop, &app, eventCallback);
|
||||
|
||||
try vx.enterAltScreen(tty.anyWriter());
|
||||
try vx.queryTerminalSend(tty.anyWriter());
|
||||
// Window size appears to be left uninitialized unless we manually set it here. This
|
||||
// seems sketchy to me (tty fd should be nonblocking for the event loop)
|
||||
const size = try vaxis.Tty.getWinsize(tty.fd);
|
||||
vx.resize(alloc, tty.anyWriter(), size) catch @panic("TODO");
|
||||
|
||||
try loop.run(.until_done);
|
||||
}
|
||||
|
||||
fn eventCallback(
|
||||
fn terminalEvent(
|
||||
self_: ?*RotInt,
|
||||
loop: *xev.Loop,
|
||||
watcher: *vaxis.xev.TtyWatcher(RotInt),
|
||||
event: vaxis.xev.Event,
|
||||
) xev.CallbackAction {
|
||||
) xev.CallbackAction {
|
||||
const self = self_.?;
|
||||
switch (event) {
|
||||
.key_press => |key| keyp: {
|
||||
@ -300,6 +249,9 @@ fn eventCallback(
|
||||
|
||||
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
|
||||
@ -308,20 +260,45 @@ fn eventCallback(
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
var tty = try vaxis.Tty.init();
|
||||
defer tty.deinit();
|
||||
|
||||
var vx = try vaxis.init(alloc, .{});
|
||||
defer vx.deinit(alloc, tty.anyWriter());
|
||||
|
||||
var pool = xev.ThreadPool.init(.{});
|
||||
var loop = try xev.Loop.init(.{
|
||||
.thread_pool = &pool,
|
||||
});
|
||||
defer loop.deinit();
|
||||
|
||||
var app: RotInt = .{
|
||||
.allocator = alloc,
|
||||
.termbuffer = tty.bufferedWriter(),
|
||||
.vx = &vx,
|
||||
.loop = &loop,
|
||||
.poller = try xev.Timer.init(),
|
||||
};
|
||||
try app.initInPlace();
|
||||
|
||||
var vx_loop: vaxis.xev.TtyWatcher(RotInt) = undefined;
|
||||
try vx_loop.init(&tty, &vx, &loop, &app, RotInt.terminalEvent);
|
||||
|
||||
try vx.enterAltScreen(tty.anyWriter());
|
||||
try vx.queryTerminalSend(tty.anyWriter());
|
||||
// Window size appears to be left uninitialized unless we manually set it here. This
|
||||
// seems sketchy to me (tty fd should be nonblocking for the event loop)
|
||||
const size = try vaxis.Tty.getWinsize(tty.fd);
|
||||
vx.resize(alloc, tty.anyWriter(), size) catch @panic("TODO");
|
||||
|
||||
try loop.run(.until_done);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user