Skip to content

Instantly share code, notes, and snippets.

@devkid devkid/gpio-send.c
Created Aug 6, 2017

Embed
What would you like to do?
Send an arbitrary series of 0's and 1's to a GPIO with given intervals between the level change (e.g. for sending a code over a 433 MHz sender)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv) {
if(argc < 4 || argc % 2) {
printf(
"Usage: %s <path to GPIO value file> <interval 1> <interval 2> ... <interval n>\n\n"
"Provide the path to the 'value' file of the GPIO in sysfs and an even number of intervals\n", argv[0]);
return 1;
}
int fd = open(argv[1], O_WRONLY);
if(fd == -1) {
perror("open");
return 1;
}
char *nums = "10";
for(int j = 0; j < 5; j++) {
for(int i = 0; i < argc - 2; i++) {
if(write(fd, nums + (i % 2), 1) != 1) {
perror("write");
return 1;
}
usleep(atoi(argv[i+2]));
}
}
if(close(fd)) {
perror("close");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.