Compare commits

..

No commits in common. 'arguments' and 'main' have entirely different histories.

@ -1,182 +1,10 @@
# GPIO Command-Line Tool in C
A simple command-line tool written in C to control Raspberry Pi GPIO pins using the Linux GPIO `sysfs` interface.
The program supports configuring a GPIO as an **input** or **output**, writing a digital value, and reading the state of an input.
## Compile
Compile the program using GCC:
```bash
gcc main.c -o gpio
```
## Usage
The general command syntax is:
```bash
./gpio <gpio> <direction> [value]
```
### GPIO as Output
Configure GPIO 17 as an output and set it HIGH:
```bash
./gpio 17 out 1
```
Set GPIO 17 LOW:
```bash
./gpio 17 out 0
```
When `out` is selected, the value argument is required:
```text
./gpio <gpio> out <0|1>
```
For example, the following command is invalid:
```bash
./gpio 17 out
```
because the output value is missing.
### GPIO as Input
Configure GPIO 17 as an input and read its current digital state:
# Introduction
How to compile
```bash
./gpio 17 in
```
The program reads the GPIO `value` file and displays either:
```text
0
```
or:
```text
1
gcc main.c -o hello
```
No additional value argument is required for an input.
## Program Flow
The program follows this sequence:
```text
Command-line arguments
│
▼
Validate direction
│
├── out → verify value argument
│
└── in → no value required
│
▼
Export GPIO
│
▼
Set GPIO direction
│
├── out → gpio_write()
│
└── in → gpio_read()
│
▼
Unexport GPIO
```
## Functions
The program is divided into the following functions:
```c
int gpio_export(const char *gpio);
int gpio_direction(const char *gpio, const char *direction);
int gpio_write(const char *gpio, const char *value);
int gpio_read(const char *gpio);
int gpio_unexport(const char *gpio);
```
Their responsibilities are:
- `gpio_export()` — exports the selected GPIO.
- `gpio_direction()` — configures the GPIO as `in` or `out`.
- `gpio_write()` — writes `0` or `1` to an output.
- `gpio_read()` — reads the digital state of an input.
- `gpio_unexport()` — releases the GPIO after the operation.
## GPIO sysfs Files
For GPIO 17, the program accesses files such as:
```text
/sys/class/gpio/chip0/gpio17/direction
/sys/class/gpio/chip0/gpio17/value
```
The `direction` file determines whether the GPIO is configured as an input or output.
The `value` file is used to read or write the digital state of the GPIO.
## Example
Output:
```bash
./gpio 17 out 1
```
Input:
```bash
./gpio 17 in
```
Invalid output command:
```bash
./gpio 17 out
```
Expected error:
```text
Error: GPIO out option requires a value.
Usage: ./gpio <gpio> out <0|1>
```
## Current Features
- GPIO export and unexport
- Input/output direction configuration
- Digital output (`0` or `1`)
- Digital input reading
- Command-line argument validation
- Invalid direction detection
## Future Work
A future version will include a `blink` operation:
```bash
./gpio 17 blink 500
```
where `500` represents the blinking interval in milliseconds.
This version now reflects the structure of your current C implementation, including the separation between argument validation and GPIO operations.
# Issues
- [ ] Error catcher

150
main.c

@ -1,142 +1,66 @@
// Gerardo Marx 24/09/2026
// gpio command tool
// eg. ./gpio 17 out 1
// ./gpio 17 in
// ./gpio 17 blink 500(ms)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
// prototype functions
int gpio_export(const char *gpio);
int gpio_direction(const char *gpio, const char *direction); // this function
int gpio_write(const char *gpio, const char *value); // write function
int gpio_read(const char *gpio); //read function
int gpio_unexport(const char *gpio);
int main(int argc, char *argv[]){
int fd;
const char *pin;
const char *dir;
const char *val = NULL;
// checking that at least gpio and direction is passed
if(argc<3){
printf("Usage:\n");
printf(" %s <gpio> in\n", argv[0]);
printf(" %s <gpio> out <value>\n", argv[0]);
return 1;
}
pin = argv[1]; // get gpio number from argument
dir = argv[2]; // get gpio direction from arguments
//const char *val = argv[3];
// checking the gpio direction
if(strcmp(dir, "out") == 0) {
if(argc<4){
printf("Error: GPIO out option requires a value.\n");
printf("Usage: %s <gpio> out <0|1>\n", argv[0]);
return 1;
}
val = argv[3];
}
// performing the read/write operation
gpio_export(pin);
gpio_direction(pin, dir);
//gpio_write(pin, val);
if (strcmp(dir,"out") == 0) {
gpio_write(pin, val);
} else if (strcmp(dir, "in") == 0) {
gpio_read(pin);
} else {
perror("invalid direction argument\n");
return -12;
}
gpio_unexport(pin);
}
// comple functions
//
// gpio_export
int gpio_export(const char *gpio){
int fd;
// export
fd = open("/sys/class/gpio/chip0/export", O_WRONLY);
if(fd<0){
perror("Error opening the GPIO path during export");
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
write(fd,gpio,strlen(gpio));
close(fd);
return 0;
}
// gpio direction
int gpio_direction(const char *gpio, const char *direction){
// pin direction - convert into functions
int fd;
char path[64];
snprintf(path, sizeof(path),
"/sys/class/gpio/chip0/gpio%s/direction", gpio);
fd = open(path, O_WRONLY); //gpio number
write(fd,"17",2);
close(fd);
// pin direction
fd = open("/sys/class/gpio/chip0/gpio17/direction", O_WRONLY);
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
write(fd,direction,strlen(direction));
write(fd,"out",3);
close(fd);
return 0;
}
int gpio_write(const char *gpio, const char *value){
int fd;
char path[64];
snprintf(path, sizeof(path),
"/sys/class/gpio/chip0/gpio%s/value", gpio);
fd = open(path, O_WRONLY);
if(fd<0){
perror("Error opening the GPIO path during write");
// pin value to 1
fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY);
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
if(write(fd,value, strlen(value)) < 0) {
perror("write");
close(fd);
write(fd,"1",1);
close(fd);
sleep(1);
// pin valu to 0
fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY);
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
//write(fd, value, strlen(value));
write(fd,"0",1);
close(fd);
return 0;
}
// gpio read
int gpio_read(const char *gpio){
printf(" Test read function for gpio %s\n", gpio);
return 0;
}
// unexport
int gpio_unexport(const char *gpio){
int fd;
sleep(1);
// unexport
fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY);
if(fd<0)
{
perror("Error opening the GPIO path during unexport");
return -1;
}
if(write(fd,gpio, strlen(gpio)) < 0) {
perror("unexport");
close(fd);
perror("Error opening the GPIO path");
return -1;
}
write(fd,"17",2);
close(fd);
printf("Hello from Raspberry Pi!\n");
printf("Number of arguments: %d\n", argc);
printf("Program is reciving: %s\n", argv[0]);
if(argc > 1 )
printf("Argument: %s\n", argv[1]);
return 0;
}

Loading…
Cancel
Save