Compare commits
3 Commits
25e410c0b5
...
6d41563226
Author | SHA1 | Date | |
---|---|---|---|
6d41563226 | |||
4a55140672 | |||
50dc69ae9b |
34
src/main.zig
34
src/main.zig
|
@ -38,6 +38,7 @@ fn usage(args: [][]u8) !void {
|
||||||
const Options = struct {
|
const Options = struct {
|
||||||
background_filename: [:0]u8,
|
background_filename: [:0]u8,
|
||||||
device_file: [:0]u8,
|
device_file: [:0]u8,
|
||||||
|
flip: bool,
|
||||||
};
|
};
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
defer deinit();
|
defer deinit();
|
||||||
|
@ -83,12 +84,15 @@ pub fn main() !void {
|
||||||
line_inx += 1;
|
line_inx += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const opts = try processArgs(alloc, args, &lines);
|
const opts = try processArgs(args, &lines);
|
||||||
defer alloc.destroy(opts);
|
|
||||||
if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename});
|
if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename});
|
||||||
var pixels: [display.WIDTH * display.HEIGHT]u8 = undefined;
|
var pixels: [display.WIDTH * display.HEIGHT]u8 = undefined;
|
||||||
try convertImage(opts.background_filename, &pixels, noTextForLine);
|
try convertImage(opts.background_filename, &pixels, noTextForLine);
|
||||||
try addTextToImage(alloc, &pixels, &lines);
|
try addTextToImage(alloc, &pixels, &lines);
|
||||||
|
if (opts.flip) {
|
||||||
|
try stdout.print("Flipping\n", .{});
|
||||||
|
flipImage(&pixels);
|
||||||
|
}
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
|
||||||
// We should take the linux device file here, then inspect for ttyUSB vs
|
// We should take the linux device file here, then inspect for ttyUSB vs
|
||||||
|
@ -114,24 +118,31 @@ fn deinit() void {
|
||||||
font_arena = null;
|
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);
|
if (args.len < 2) try usage(args);
|
||||||
const prefix = "/dev/ttyUSB";
|
const prefix = "/dev/ttyUSB";
|
||||||
var opts = try allocator.create(Options);
|
var opts = Options{
|
||||||
opts.device_file = args[1];
|
.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);
|
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 is_filename = false;
|
||||||
var line_number: ?usize = null;
|
var line_number: ?usize = null;
|
||||||
for (args[1..], 1..) |arg, i| {
|
for (args[1..], 1..) |arg, i| {
|
||||||
|
if (std.mem.eql(u8, "-flip", arg)) {
|
||||||
|
opts.flip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (std.mem.eql(u8, "-bg", arg)) {
|
if (std.mem.eql(u8, "-bg", arg)) {
|
||||||
is_filename = true;
|
is_filename = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (is_filename) {
|
if (is_filename) {
|
||||||
|
is_filename = false;
|
||||||
opts.background_filename = args[i]; // arg capture changes value...
|
opts.background_filename = args[i]; // arg capture changes value...
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
if ((arg[0] == '-' and arg.len > 1) and areDigits(arg[1..])) {
|
if ((arg[0] == '-' and arg.len > 1) and areDigits(arg[1..])) {
|
||||||
line_number = try std.fmt.parseInt(usize, arg[1..], 10);
|
line_number = try std.fmt.parseInt(usize, arg[1..], 10);
|
||||||
|
@ -160,6 +171,15 @@ fn areDigits(bytes: []u8) bool {
|
||||||
}
|
}
|
||||||
return true;
|
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 {
|
fn addTextToImage(allocator: std.mem.Allocator, pixels: *[display.WIDTH * display.HEIGHT]u8, data: []*[]u8) !void {
|
||||||
var maybe_font_data = try getFontData(allocator, DEFAULT_FONT);
|
var maybe_font_data = try getFontData(allocator, DEFAULT_FONT);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user