Write a program, sonar.c, compiled with GCC on the RPi 3b/3b+, that performs the following steps using the provided sysfs_gpio files: a. Include the sysfs_gpio.h file b. Initializes the TRIGGER pin to be an output by calling gpioOutput() c. Initializes the ECHO pin to be an input by calling gpioInput() d. Sets Trig signal high (1) and then low (0) e. Zero a count variable f. Enter a while loop that waits until the Echo signal goes high There is no code in the while loop g. The program exits the first loop h. Enter another while loop that waits until the Echo signal goes low The while loop contains code that increments the count variable i. The program exits the second loop j. The count value is now proportional to the distance traveled k. Print the raw count value l. Convert the value to a distance (cm) by empirical means (the desk is about 4 feet or 122 cm in length)  sysfs_gpio.c: #include //----------------------------------------------------------------------------- // Subroutines //----------------------------------------------------------------------------- void gpioOutput(int pin) { FILE* file; char str[35]; file = fopen("/sys/class/gpio/export", "w"); fprintf(file, "%d", pin); fclose(file); sprintf(str, "/sys/class/gpio/gpio%d/direction", pin); file = fopen(str, "w"); fprintf(file, "out"); fclose(file); } void gpioInput(int pin) { FILE* file; char str[35]; file = fopen("/sys/class/gpio/export", "w"); fprintf(file, "%d", pin); fclose(file); sprintf(str, "/sys/class/gpio/gpio%d/direction", pin); file = fopen(str, "w"); fprintf(file, "in"); fclose(file); } void gpioWrite(int pin, int value) {   FILE* file; char str[35]; sprintf(str, "/sys/class/gpio/gpio%d/value", pin); file = fopen(str, "w"); fprintf(file, "%d", value); fclose(file); } int gpioRead(int pin) { FILE* file; int result; char str[30]; sprintf(str, "/sys/class/gpio/gpio%d/value", pin); file = fopen(str, "rw"); fscanf(file, "%d", &result); fclose(file); return result; } sysfs_gpio.h : #ifndef SYSFS_GPIO_H_ #define SYSFS_GPIO_H_   // Called to configure the pin as an input or output void gpioOutput(int pin); void gpioInput(int pin);   // Called to set and output pin to a given value (1 or 0) void gpioWrite(int pin, int value);   // Called to get the status (1 or 0) of a pin int gpioRead(int pin);   #endif

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a program, sonar.c, compiled with GCC on the RPi 3b/3b+, that performs the following steps
using the provided sysfs_gpio files:
a. Include the sysfs_gpio.h file
b. Initializes the TRIGGER pin to be an output by calling gpioOutput()
c. Initializes the ECHO pin to be an input by calling gpioInput()
d. Sets Trig signal high (1) and then low (0)
e. Zero a count variable
f. Enter a while loop that waits until the Echo signal goes high
There is no code in the while loop
g. The program exits the first loop
h. Enter another while loop that waits until the Echo signal goes low
The while loop contains code that increments the count variable
i. The program exits the second loop
j. The count value is now proportional to the distance traveled
k. Print the raw count value
l. Convert the value to a distance (cm) by empirical means
(the desk is about 4 feet or 122 cm in length) 

sysfs_gpio.c:

#include <stdio.h>
//-----------------------------------------------------------------------------
// Subroutines
//-----------------------------------------------------------------------------
void gpioOutput(int pin)
{
FILE* file;
char str[35];
file = fopen("/sys/class/gpio/export", "w");
fprintf(file, "%d", pin);
fclose(file);
sprintf(str, "/sys/class/gpio/gpio%d/direction", pin);
file = fopen(str, "w");
fprintf(file, "out");
fclose(file);
}
void gpioInput(int pin)
{
FILE* file;
char str[35];
file = fopen("/sys/class/gpio/export", "w");
fprintf(file, "%d", pin);
fclose(file);
sprintf(str, "/sys/class/gpio/gpio%d/direction", pin);
file = fopen(str, "w");
fprintf(file, "in");
fclose(file);
}
void gpioWrite(int pin, int value)
{
 
FILE* file;
char str[35];
sprintf(str, "/sys/class/gpio/gpio%d/value", pin);
file = fopen(str, "w");
fprintf(file, "%d", value);
fclose(file);
}

int gpioRead(int pin)
{
FILE* file;
int result;
char str[30];
sprintf(str, "/sys/class/gpio/gpio%d/value", pin);
file = fopen(str, "rw");
fscanf(file, "%d", &result);
fclose(file);
return result;
}
sysfs_gpio.h :

#ifndef SYSFS_GPIO_H_

#define SYSFS_GPIO_H_

 

// Called to configure the pin as an input or output

void gpioOutput(int pin);

void gpioInput(int pin);

 

// Called to set and output pin to a given value (1 or 0)

void gpioWrite(int pin, int value);

 

// Called to get the status (1 or 0) of a pin

int gpioRead(int pin);

 

#endif

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Huffman coding
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education