Compare commits

..

No commits in common. "a7b917d3e96f97f1ebf8ac428a0341b76f8c23d7" and "26c52dd2789fdc531fdfe7dc30d7113a606fe3bb" have entirely different histories.

3 changed files with 57 additions and 105 deletions

View File

@ -66,13 +66,9 @@ pub fn build(b: *std.build.Builder) !void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe_tests.linkLibrary(im_dep.artifact("MagickWand"));
exe_tests.linkLibrary(z_dep.artifact("z"));
exe_tests.linkLibrary(i2cdriver);
exe_tests.addIncludePath("lib/i2cdriver");
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.run().step); test_step.dependOn(&exe_tests.step);
} }
// Should be able to remove this // Should be able to remove this

View File

@ -24,7 +24,7 @@ const LINES = 8;
// Device specifications // Device specifications
const PAGES = 8; const PAGES = 8;
var lines: [LINES]*[:0]u8 = undefined; var lines: [LINES]*const [:0]u8 = undefined;
fn usage(args: [][]u8) !void { fn usage(args: [][]u8) !void {
const writer = std.io.getStdErr().writer(); const writer = std.io.getStdErr().writer();
@ -33,85 +33,40 @@ fn usage(args: [][]u8) !void {
try writer.print("\te.g. \"-1 'hello world'\" will display \"hello world\" on line 1\n", .{}); try writer.print("\te.g. \"-1 'hello world'\" will display \"hello world\" on line 1\n", .{});
std.os.exit(1); std.os.exit(1);
} }
const Options = struct {
background_filename: [:0]u8,
device_file: [:0]u8,
};
pub fn main() !void { pub fn main() !void {
const alloc = std.heap.c_allocator; const alloc = std.heap.c_allocator;
//defer alloc.deinit(); //defer alloc.deinit();
const args = try std.process.argsAlloc(alloc); const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args); defer std.process.argsFree(alloc, args);
if (args.len < 2) try usage(args);
const prefix = "/dev/ttyUSB";
const device = try alloc.dupeZ(u8, args[1]);
defer alloc.free(device);
if (!std.mem.eql(u8, device, "-") and !std.mem.startsWith(u8, device, prefix)) try usage(args);
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer(); const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file); var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer(); const stdout = bw.writer();
defer bw.flush() catch unreachable; // don't forget to flush! defer bw.flush() catch unreachable; // don't forget to flush!
// If we have stdin redirected, we want to process that before var filename: [:0]u8 = @constCast("");
// we take command line options. So cli will overwrite any lines specified var b: [1]u8 = undefined;
// from the file const nothing: [:0]u8 = try std.fmt.bufPrintZ(&b, "", .{});
const stdin_file = std.io.getStdIn();
var stdin_data: [WIDTH * HEIGHT + 1]u8 = undefined;
// Need this to support deallocation of memory
var line_inx: usize = 0;
var stdin_lines: [LINES][:0]u8 = undefined;
defer {
for (0..line_inx) |i| {
alloc.free(stdin_lines[i]);
}
}
var nothing: [:0]u8 = @constCast("");
for (lines, 0..) |_, i| { for (lines, 0..) |_, i| {
lines[i] = &nothing; lines[i] = &nothing;
} }
if (!stdin_file.isTty()) {
const read = try stdin_file.readAll(&stdin_data);
if (read == stdin_data.len) {
try std.io.getStdErr().writer().print("ERROR: data provided exceeds what can be sent to device!\n", .{});
try usage(args);
}
var read_data = stdin_data[0..read];
var it = std.mem.split(u8, read_data, "\n");
while (it.next()) |line| {
if (line.len == 0) continue;
stdin_lines[line_inx] = try alloc.dupeZ(u8, line);
lines[line_inx] = &stdin_lines[line_inx];
line_inx += 1;
}
}
std.debug.print("delme: {s}\n", .{lines[0].*});
const opts = try processArgs(alloc, args, &lines);
defer alloc.destroy(opts);
if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename});
var pixels: [WIDTH * HEIGHT]u8 = undefined;
try convertImage(opts.background_filename, &pixels, textForLine);
try bw.flush();
// We should take the linux device file here, then inspect for ttyUSB vs
// i2c whatever and do the right thing from there...
try sendPixels(&pixels, opts.device_file, 0x3c);
// try stdout.print("Run `zig build test` to run the tests.\n", .{});
}
fn processArgs(allocator: std.mem.Allocator, args: [][:0]u8, line_array: *[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];
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, 0..) |arg, i| {
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) {
opts.background_filename = args[i]; // arg capture changes value... filename = args[i]; // arg capture changes value...
break; break;
} }
if ((arg[0] == '-' and arg.len > 1) and areDigits(arg[1..])) { if ((arg[0] == '-' and arg.len > 1) and areDigits(arg[1..])) {
@ -126,13 +81,30 @@ fn processArgs(allocator: std.mem.Allocator, args: [][:0]u8, line_array: *[LINES
); );
std.os.exit(1); std.os.exit(1);
} }
std.log.debug("line {d} text: \"{s}\"\n", .{ line, arg }); std.debug.print("line {d} text: \"{s}\"\n", .{ line, arg });
line_array.*[line] = &args[i]; lines[line] = &args[i];
line_number = null; line_number = null;
continue; continue;
} }
} }
return opts;
if (filename.len > 0) try stdout.print("Converting {s}\n", .{filename});
var pixels: [WIDTH * HEIGHT]u8 = undefined;
try convertImage(alloc, filename, &pixels, textForLine);
try stdout.print("Sending pixels to display\n", .{});
// var i: usize = 0;
// while (i < HEIGHT) {
// try stdout.print("{d:0>2}: {s}\n", .{ i, fmtSliceGreyscaleImage(pixels[(i * WIDTH)..((i + 1) * WIDTH)]) });
// // try stdout.print("{d:0>2}: {s}\n", .{ i, std.fmt.fmtSliceHexLower(pixels[(i * WIDTH)..((i + 1) * WIDTH)]) });
// i += 1;
// }
// We should take the linux device file here, then inspect for ttyUSB vs
// i2c whatever and do the right thing from there...
try sendPixels(&pixels, device, 0x3c);
try stdout.print("done\n", .{});
// try stdout.print("Run `zig build test` to run the tests.\n", .{});
} }
fn areDigits(bytes: []u8) bool { fn areDigits(bytes: []u8) bool {
@ -195,7 +167,6 @@ fn sendPixelsThroughI2CDriver(pixels: []const u8, file: [*:0]const u8, device_id
defer bw.flush() catch unreachable; // don't forget to flush! defer bw.flush() catch unreachable; // don't forget to flush!
try stdout.print("Connecting to I2CDriver on {s}. If progress stalls, unplug device and re-insert.\n", .{c_file}); try stdout.print("Connecting to I2CDriver on {s}. If progress stalls, unplug device and re-insert.\n", .{c_file});
try bw.flush(); try bw.flush();
try stdout.print("Sending pixels to display...", .{});
c.i2c_connect(&i2c, c_file); c.i2c_connect(&i2c, c_file);
try stdout.print("Device connected\n", .{}); try stdout.print("Device connected\n", .{});
if (i2c.connected != 1) return error.I2CConnectionFailed; if (i2c.connected != 1) return error.I2CConnectionFailed;
@ -218,11 +189,9 @@ fn sendPixelsThroughI2CDriver(pixels: []const u8, file: [*:0]const u8, device_id
// Write data to device // Write data to device
try i2cWrite(&i2c, &pixels_write_command); try i2cWrite(&i2c, &pixels_write_command);
// Leaving this here because we'll need it later I think for (0..HEIGHT) |i| {
// for (0..HEIGHT) |i| { std.debug.print("{d:0>2}: {s}\n", .{ i, fmtSliceGreyscaleImage(pixels[(i * WIDTH)..((i + 1) * WIDTH)]) });
// std.log.debug("{d:0>2}: {s}\n", .{ i, fmtSliceGreyscaleImage(pixels[(i * WIDTH)..((i + 1) * WIDTH)]) }); }
// }
try stdout.print("done\n", .{});
} }
fn packPixelsToDeviceFormat(pixels: []const u8, packed_pixels: []u8) void { fn packPixelsToDeviceFormat(pixels: []const u8, packed_pixels: []u8) void {
@ -305,7 +274,8 @@ fn reportMagickError(mw: ?*c.MagickWand) !void {
fn textForLine(line: usize) []u8 { fn textForLine(line: usize) []u8 {
return lines[line].*; return lines[line].*;
} }
fn convertImage(filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const fn (usize) []u8) !void { fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const fn (usize) []u8) !void {
_ = alloc;
c.MagickWandGenesis(); c.MagickWandGenesis();
defer c.MagickWandTerminus(); defer c.MagickWandTerminus();
var mw = c.NewMagickWand(); var mw = c.NewMagickWand();
@ -339,13 +309,13 @@ fn convertImage(filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const f
const w = c.MagickGetImageWidth(mw); const w = c.MagickGetImageWidth(mw);
const h = c.MagickGetImageHeight(mw); const h = c.MagickGetImageHeight(mw);
std.log.debug("Original dimensions: {d}x{d}\n", .{ w, h }); std.debug.print("Original dimensions: {d}x{d}\n", .{ w, h });
// This should be 48x64 with our test // This should be 48x64 with our test
// Command line resize works differently than this. Here we need to find // Command line resize works differently than this. Here we need to find
// new width and height based on the input aspect ratio ourselves // new width and height based on the input aspect ratio ourselves
const resize_dimensions = getNewDimensions(w, h, WIDTH, HEIGHT); const resize_dimensions = getNewDimensions(w, h, WIDTH, HEIGHT);
std.log.debug("Dimensions for resize: {d}x{d}\n", .{ resize_dimensions.width, resize_dimensions.height }); std.debug.print("Dimensions for resize: {d}x{d}\n", .{ resize_dimensions.width, resize_dimensions.height });
status = c.MagickResizeImage(mw, resize_dimensions.width, resize_dimensions.height, c.UndefinedFilter); status = c.MagickResizeImage(mw, resize_dimensions.width, resize_dimensions.height, c.UndefinedFilter);
if (status == c.MagickFalse) if (status == c.MagickFalse)
@ -392,6 +362,7 @@ fn convertImage(filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const f
} }
break; break;
} }
std.debug.print("left_spaces: {d}. Text: \"{s}\"\n", .{ left_spaces, text });
x += (FONT_WIDTH * left_spaces); x += (FONT_WIDTH * left_spaces);
mw = try drawString(mw, text[@intCast(usize, left_spaces)..], x, y); mw = try drawString(mw, text[@intCast(usize, left_spaces)..], x, y);
} }
@ -517,38 +488,23 @@ fn getNewDimensions(width: usize, height: usize, desired_width: usize, desired_h
}; };
} }
test "gets correct bytes" { fn logo() !void {
const bg_file: [:0]u8 = @constCast("logo:"); c.MagickWandGenesis();
const opts = .{ .background_filename = bg_file, .device_file = "-" };
const empty: [:0]u8 = @constCast("");
for (&lines) |*line| {
line.* = &empty;
}
const line: [:0]u8 = @constCast("Hello\\!");
lines[5] = &line;
var pixels: [WIDTH * HEIGHT]u8 = undefined;
var expected_pixels: *const [WIDTH * HEIGHT]u8 = @embedFile("testExpectedBytes.bin"); // Create a wand
var mw = c.NewMagickWand();
// [_]u8{..,..,..} // Read the input image
try convertImage(opts.background_filename, &pixels, textForLine); _ = c.MagickReadImage(mw, "logo:"); // TODO: What is the return val?
// try writeBytesToFile("testExpectedBytes.bin", &pixels); // write it
try std.testing.expectEqualSlices(u8, expected_pixels, &pixels); _ = c.MagickWriteImage(mw, "logo.jpg"); // TODO: What is the return val?
}
fn writeBytesToFile(filename: []const u8, bytes: []u8) !void { // Tidy up
const file = try std.fs.cwd().createFile(filename, .{ if (mw) |w| mw = c.DestroyMagickWand(w);
.read = false,
.truncate = true, c.MagickWandTerminus();
.lock = .Exclusive,
.lock_nonblocking = false,
.mode = 0o666,
.intended_io_mode = .blocking,
});
defer file.close();
const writer = file.writer();
try writer.writeAll(bytes);
// try writer.print("pub const chars = &[_][]const u8{{\n", .{});
} }
test "simple test" { test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator); var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak! defer list.deinit(); // try commenting this out and see if zig detects the memory leak!

Binary file not shown.