Compare commits

...

3 Commits

Author SHA1 Message Date
6d41563226
new -flip arg to rotate 180 degrees
Fixes #4
2023-04-08 17:31:10 -07:00
4a55140672
do not assume filename is last arg 2023-04-08 17:30:14 -07:00
50dc69ae9b
remove allocation of Options on the heap 2023-04-08 17:26:05 -07:00

View File

@ -38,6 +38,7 @@ fn usage(args: [][]u8) !void {
const Options = struct {
background_filename: [:0]u8,
device_file: [:0]u8,
flip: bool,
};
pub fn main() !void {
defer deinit();
@ -83,12 +84,15 @@ pub fn main() !void {
line_inx += 1;
}
}
const opts = try processArgs(alloc, args, &lines);
defer alloc.destroy(opts);
const opts = try processArgs(args, &lines);
if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename});
var pixels: [display.WIDTH * display.HEIGHT]u8 = undefined;
try convertImage(opts.background_filename, &pixels, noTextForLine);
try addTextToImage(alloc, &pixels, &lines);
if (opts.flip) {
try stdout.print("Flipping\n", .{});
flipImage(&pixels);
}
try bw.flush();
// We should take the linux device file here, then inspect for ttyUSB vs
@ -114,24 +118,31 @@ fn deinit() void {
font_arena = null;
}
fn processArgs(allocator: std.mem.Allocator, args: [][:0]u8, line_array: *[display.LINES]*const [:0]u8) !*Options {
fn processArgs(args: [][:0]u8, line_array: *[display.LINES]*const [:0]u8) !Options {
if (args.len < 2) try usage(args);
const prefix = "/dev/ttyUSB";
var opts = try allocator.create(Options);
opts.device_file = args[1];
var opts = Options{
.device_file = args[1],
.flip = false,
.background_filename = @constCast(""),
};
if (!std.mem.eql(u8, opts.device_file, "-") and !std.mem.startsWith(u8, opts.device_file, prefix)) try usage(args);
opts.background_filename = @constCast("");
var is_filename = false;
var line_number: ?usize = null;
for (args[1..], 1..) |arg, i| {
if (std.mem.eql(u8, "-flip", arg)) {
opts.flip = true;
continue;
}
if (std.mem.eql(u8, "-bg", arg)) {
is_filename = true;
continue;
}
if (is_filename) {
is_filename = false;
opts.background_filename = args[i]; // arg capture changes value...
break;
continue;
}
if ((arg[0] == '-' and arg.len > 1) and areDigits(arg[1..])) {
line_number = try std.fmt.parseInt(usize, arg[1..], 10);
@ -160,6 +171,15 @@ fn areDigits(bytes: []u8) bool {
}
return true;
}
fn flipImage(pixels: *[display.WIDTH * display.HEIGHT]u8) void {
for (pixels[0..(display.WIDTH * display.HEIGHT / 2)], 0..) |_, i| {
const dest_pixel = pixels[pixels.len - 1 - i];
const src_pixel = pixels[i];
pixels[pixels.len - 1 - i] = src_pixel;
pixels[i] = dest_pixel;
}
}
fn addTextToImage(allocator: std.mem.Allocator, pixels: *[display.WIDTH * display.HEIGHT]u8, data: []*[]u8) !void {
var maybe_font_data = try getFontData(allocator, DEFAULT_FONT);