From f2b768eabaa99ebb0acf5454823871ddf5675a59 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 5 Jun 2023 13:02:06 +0800 Subject: [PATCH] Refactor for NuttX --- feature-phone.zig | 2 +- nuttx.zig | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/feature-phone.zig b/feature-phone.zig index c1d875c..fe94239 100644 --- a/feature-phone.zig +++ b/feature-phone.zig @@ -179,7 +179,7 @@ export fn eventHandler(e: ?*c.lv_event_t) void { if (builtin.cpu.arch == .wasm32 or builtin.cpu.arch == .wasm64) { debug("Running in WebAssembly, simulate the Phone Call", .{}); } else { - debug("Running on PinePhone, make an actual Phone Call", {}); + debug("Running on PinePhone, make an actual Phone Call", .{}); } } else if (std.mem.eql(u8, span, "Cancel")) { // If Cancel is clicked, erase the last digit diff --git a/nuttx.zig b/nuttx.zig index 34ccbc0..59a2f77 100644 --- a/nuttx.zig +++ b/nuttx.zig @@ -1 +1,70 @@ //! LVGL Functions specific to Apache NuttX RTOS + +/// Import the Zig Standard Library +const std = @import("std"); +const builtin = @import("builtin"); + +/////////////////////////////////////////////////////////////////////////////// +// Panic Handler + +/// Called by Zig when it hits a Panic. We print the Panic Message, Stack Trace and halt. See +/// https://andrewkelley.me/post/zig-stack-traces-kernel-panic-bare-bones-os.html +/// https://github.com/ziglang/zig/blob/master/lib/std/builtin.zig#L763-L847 +pub fn panic(message: []const u8, _stack_trace: ?*std.builtin.StackTrace) noreturn { + // Print the Panic Message + _ = _stack_trace; + _ = puts("\n!ZIG PANIC!"); + _ = puts(@ptrCast([*c]const u8, message)); + + // Print the Stack Trace + _ = puts("Stack Trace:"); + var it = std.debug.StackIterator.init(@returnAddress(), null); + while (it.next()) |return_address| { + _ = printf("%p\n", return_address); + } + + // Halt + while (true) {} +} + +/////////////////////////////////////////////////////////////////////////////// +// Logging + +/// Called by Zig for `std.log.debug`, `std.log.info`, `std.log.err`, ... +/// https://gist.github.com/leecannon/d6f5d7e5af5881c466161270347ce84d +pub fn log( + comptime _message_level: std.log.Level, + comptime _scope: @Type(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + _ = _message_level; + _ = _scope; + + // Format the message + var buf: [100]u8 = undefined; // Limit to 100 chars + var slice = std.fmt.bufPrint(&buf, format, args) catch { + _ = puts("*** log error: buf too small"); + return; + }; + + // Terminate the formatted message with a null + var buf2: [buf.len + 1:0]u8 = undefined; + std.mem.copy(u8, buf2[0..slice.len], slice[0..slice.len]); + buf2[slice.len] = 0; + + // Print the formatted message + _ = puts(&buf2); +} + +/////////////////////////////////////////////////////////////////////////////// +// Imported Functions and Variables + +/// For safety, we import these functions ourselves to enforce Null-Terminated Strings. +/// We changed `[*c]const u8` to `[*:0]const u8` +extern fn printf(format: [*:0]const u8, ...) c_int; +extern fn puts(str: [*:0]const u8) c_int; + +/// Aliases for Zig Standard Library +const assert = std.debug.assert; +const debug = std.log.debug;