You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					33 lines
				
				923 B
			
		
		
			
		
	
	
					33 lines
				
				923 B
			| 
											4 years ago
										 | #include "common/gpio.h"
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | #include <fcntl.h>
 | ||
| 
											5 years ago
										 | #include <unistd.h>
 | ||
|  | 
 | ||
| 
											4 years ago
										 | #include <cstring>
 | ||
|  | 
 | ||
| 
											4 years ago
										 | #include "common/util.h"
 | ||
| 
											5 years ago
										 | 
 | ||
|  | // We assume that all pins have already been exported on boot,
 | ||
|  | // and that we have permission to write to them.
 | ||
|  | 
 | ||
| 
											4 years ago
										 | int gpio_init(int pin_nr, bool output) {
 | ||
| 
											5 years ago
										 |   char pin_dir_path[50];
 | ||
|  |   int pin_dir_path_len = snprintf(pin_dir_path, sizeof(pin_dir_path),
 | ||
|  |                            "/sys/class/gpio/gpio%d/direction", pin_nr);
 | ||
| 
											4 years ago
										 |   if(pin_dir_path_len <= 0) {
 | ||
| 
											5 years ago
										 |     return -1;
 | ||
| 
											5 years ago
										 |   }
 | ||
| 
											5 years ago
										 |   const char *value = output ? "out" : "in";
 | ||
| 
											5 years ago
										 |   return util::write_file(pin_dir_path, (void*)value, strlen(value));
 | ||
| 
											5 years ago
										 | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | int gpio_set(int pin_nr, bool high) {
 | ||
| 
											5 years ago
										 |   char pin_val_path[50];
 | ||
|  |   int pin_val_path_len = snprintf(pin_val_path, sizeof(pin_val_path),
 | ||
|  |                            "/sys/class/gpio/gpio%d/value", pin_nr);
 | ||
| 
											4 years ago
										 |   if(pin_val_path_len <= 0) {
 | ||
| 
											5 years ago
										 |     return -1;
 | ||
| 
											5 years ago
										 |   }
 | ||
| 
											5 years ago
										 |   return util::write_file(pin_val_path, (void*)(high ? "1" : "0"), 1);
 | ||
| 
											5 years ago
										 | }
 |