main.c (3243B)
1 #ifndef _GNU_SOURCE 2 #define _GNU_SOURCE 3 #endif 4 5 #include <gpiod.h> 6 #include <termios.h> 7 #include <fcntl.h> 8 #include <stdlib.h> 9 10 11 const char* CHIPPATH = "/dev/gpiochip0"; 12 const unsigned int GPIO16_OFFSET = 16; 13 const char* UART0 = "/dev/ttyS0"; 14 15 //CLI Controlls 16 enum command{ 17 quit = 'q', 18 up = 'w', 19 down = 's' 20 }; 21 static char keyInput = down; 22 23 24 int main(void) 25 { 26 printf("CA_inhibit\n"); 27 28 struct gpiod_line_request* request_line16; 29 struct gpiod_line_settings* settings; 30 int ret; 31 32 int uart0_filestream = -1; 33 int rx_length = -1; 34 int lword = 0; 35 36 // Config UART0 37 uart0_filestream = open(UART0, O_RDONLY | O_NOCTTY | O_NDELAY); 38 // O_RDONLY: read only 39 // O_NOCTTY: The port never becomes the controlling terminal of the process 40 // O_NDELAY: Use non-blocking I/O 41 if (uart0_filestream == -1) 42 { 43 perror("Fehler - UART konnte nicht geƶffnet werden\n"); 44 } 45 46 47 struct termios options; 48 tcgetattr(uart0_filestream, &options); 49 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // RAW input 50 options.c_cflag |= PARENB; // Parity bit enabled 51 options.c_iflag |= (INPCK | ISTRIP); // check and strip paryty bit 52 options.c_cflag |= B9600; // Baud rate 9600 53 options.c_cflag |= CS8; // Size: 8 Bits 54 options.c_cflag &= (CREAD | CLOCAL); // These will ensure that your program does not become the 'owner' of the port subject to sporatic job control and hangup signals, and also that the serial interface driver will read incoming data bytes. 55 options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow controll 56 57 tcsetattr(uart0_filestream, TCSANOW, &options); //Flush buffers and apply settings 58 59 60 //Config GPIO 61 settings = gpiod_make_settings(GPIOD_LINE_DIRECTION_OUTPUT,GPIOD_LINE_BIAS_PULL_UP,GPIOD_LINE_DRIVE_OPEN_DRAIN,1); 62 if (!settings) 63 { 64 perror("gpiod_make_settings() failed"); 65 return -1; 66 } 67 68 request_line16 = gpiod_make_line_request(CHIPPATH,GPIO16_OFFSET,settings); 69 if (!request_line16) 70 { 71 perror("gpiod_make_line_request() failed"); 72 return -1; 73 } 74 75 while(keyInput!=quit) 76 { 77 scanf(" %c", &keyInput);// controlls 78 if(keyInput == up) 79 { 80 printf("ACTIVE\n"); 81 82 //Byte lesen 83 if(uart0_filestream != -1) 84 { 85 char * byte; 86 rx_length = read(uart0_filestream,byte,1); 87 if(rx_length>0) 88 { 89 print("recived byte: %c \n", byte); 90 } 91 }; 92 93 ret = gpiod_line_request_set_value(request_line16,GPIO16_OFFSET, GPIOD_LINE_VALUE_ACTIVE); 94 } 95 96 else if (keyInput == down) 97 { 98 printf("INACTIVE\n"); 99 ret = gpiod_line_request_set_value(request_line16,GPIO16_OFFSET, GPIOD_LINE_VALUE_INACTIVE); 100 } 101 if(ret == -1) 102 { 103 perror("gpiod_line_request_set_value() failed"); 104 return -1; 105 } 106 } 107 108 gpiod_line_request_release(request_line16); 109 close(uart0_filestream); 110 return 0; 111 }