Skip to content

Commit

Permalink
adding Zig examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubberazer committed Jun 28, 2024
1 parent 2d568f3 commit 9a78a31
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions EXAMPLES_Zig/jetgpio_PWM_example.zig
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 9a78a31

Please sign in to comment.