From 9a78a31bbc37d537ce7697a8f729e0a6b2707390 Mon Sep 17 00:00:00 2001 From: Rubberazer <47650457+Rubberazer@users.noreply.github.com> Date: Fri, 28 Jun 2024 07:20:37 +0200 Subject: [PATCH] adding Zig examples --- EXAMPLES_Zig/jetgpio_PWM_example.zig | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 EXAMPLES_Zig/jetgpio_PWM_example.zig diff --git a/EXAMPLES_Zig/jetgpio_PWM_example.zig b/EXAMPLES_Zig/jetgpio_PWM_example.zig new file mode 100644 index 0000000..18e5c0f --- /dev/null +++ b/EXAMPLES_Zig/jetgpio_PWM_example.zig @@ -0,0 +1,45 @@ +// Usage example of the JETGPIO library +// Compile with: zig build-exe jetgpio_PWM_example.zig -ljetgpio -lc +// Execute with: sudo ./jetgpio_PWM_example + +const std = @import("std"); +const jetgpio = @cImport({ + @cInclude("jetgpio.h"); +}); + +pub fn main() !void { + const init = jetgpio.gpioInitialise(); + + if (init < 0) { + std.debug.print("Error initiating jetgpio: {}\n", .{init}); + std.process.exit(1); + } + + // Setting up PWM frequency=10kHz @ pin 32 + + const PWMstat = jetgpio.gpioSetPWMfrequency(32, 10000); + if (PWMstat < 0) { + std.debug.print("PWM frequency set up failed. Error code: {}\n", .{PWMstat}); + std.process.exit(1); + } + + // Set up PWM duty cycle to approx 50% (0=0% to 256=100%) @ pin 32 + + const PWMstat2 = jetgpio.gpioPWM(32, 128); + + if (PWMstat2 < 0) { + std.debug.print("PWM start failed. Error code: {}\n", .{PWMstat2}); + std.process.exit(1); + } + + var x: i32 = 0; + std.debug.print("PWM going at pin 32 for 60 seconds\n", .{}); + + while (x < 30) { + std.time.sleep(2000000000); + x += 1; + } + + // Terminating library + jetgpio.gpioTerminate(); +}